﻿// 全局变量，定义异步xml通信
var gNewxmlHttp = null;
// 当前显示的第一个新产品结构式
var nIndex = 0;
// 暂缺
var sLost = "Lost";

function CreateNewAjaxObj()
{
   gNewxmlHttp = CreatAjaxObj();
   if ( null == gNewxmlHttp )
   {
      alert( "Error initializing XMLHttpRequest!" );
   }
}

function ViewNext(forward)
{
   // 向前翻一张查看
   if ( ! forward)
   {
      nIndex = (nIndex > 0) ? nIndex = nIndex - 1 : nIndex = 4;
   }
   else // 向后翻一张
   {
      nIndex = (nIndex < 4) ? nIndex = nIndex + 1 : nIndex = 0;
   }
   if (gNewxmlHttp != null)
   {
      ShowNewProPicture();
   }
   else
   {
      GetNewProduct();
   }
}

function GetNewProduct()
{
   if (gNewxmlHttp == null)
   {
      CreateNewAjaxObj();
   }
   url = "../NewProduct.ashx?bForBast=true";
   gNewxmlHttp.open( "get", url, true );
   gNewxmlHttp.setRequestHeader("Cache-Control","no-cache"); 
   gNewxmlHttp.onreadystatechange = ShowProduct;
   gNewxmlHttp.send( null );
}

function ShowProduct()
{

   if ( gNewxmlHttp.readyState == 4 )
   {
      if( gNewxmlHttp.status == 200 )
      {
         try
         {
            var xmlResponse = gNewxmlHttp.responseXML;
            var sProID = xmlResponse.getElementsByTagName( "ProID" )[nIndex].firstChild.nodeValue;
            ShowNewProPicture();
            // 安装一个定时器，动态更新 5秒钟一次
            setInterval("ViewNext(true)" , 5000);
         }
         catch(failed)
         {
            
         }
      }
      else  // 超时处理，
      {
         //alert("服务超时，请稍后重试！");
      }
   }
}

function ShowNewProPicture()
{
   try
   {
      var xmlResponse = gNewxmlHttp.responseXML;
      var sMolstr = xmlResponse.getElementsByTagName( "Mol" )[nIndex].firstChild.nodeValue;
      if (sMolstr == sLost || sMolstr.length <= 0)
      {
         document.JMENewProduct.reset();
      }
      else
      {
         document.JMENewProduct.readMolFile(sMolstr);
      }
      var sProID = xmlResponse.getElementsByTagName( "ProID" )[nIndex].firstChild.nodeValue;
      var sHtml = "<li>New Product：" + sProID + "</li>";
      var ctr = document.getElementById("ProID");
      setInnerHTML(ctr, sHtml);
   }
   catch (Failed)
   {
      
   }
}
//window.onload = function()
//{
//    GetNewProduct() ;
//}
