// AJAXProductList.js
//
// Desciption : This javascript file is created to segregate the AJAX code
// from the C# code behind server source code.
//
// Author : Katerina Kostrioukova
// Revisions :
//Creating the instance of the XmlHttpRequest
// branch for native XMLHttpRequest object
// CONSTANTS
var COMPLETE = 4;
var OK = 200;
var AjaxServerUrlConst = "GetProductsList.aspx";
var AjaxServerUrl = "GetProductsList.aspx";
var ProductsListFlag = true;
var ProductsListRefreshRate = 0;
var ProductsListInterval;
var productsListClient = null;
var LastAccessDate;
var Seconds = 1;
var Minutes = 1;
//branch for Firefox version
if (window.XMLHttpRequest)
{
productsListClient = new XMLHttpRequest();
}
//branch for IE/Windows ActiveX version
else if (window.ActiveXObject)
{
productsListClient = new ActiveXObject("Microsoft.XMLHTTP");
}
//===============================================================
//AJAX PRODUCTS LIST
//Sending information to server
function getProductsList()
{
var txtInterval = document.getElementById("txtInterval");
Minutes = txtInterval.value;
LastAccessDate = new Date();
if (ProductsListInterval != null)
{
try
{
clearInterval(ProductsListInterval);
}
catch (ex)
{
}
}
var chkViewAjax = document.getElementById("chkViewAjax");
if(chkViewAjax.checked == true)
{
AjaxServerUrl = AjaxServerUrlConst + "?getList=1";
}
else
{
AjaxServerUrl = AjaxServerUrlConst + "?getList=0";
}
ProductsListInterval = setInterval('ProductsListRecursion()', Seconds * 1000);
}
//Sending information to server
function ProductsListRecursion()
{
try
{
//callBack;
if (!ProductsListFlag)
return;
var currentDate = new Date();
var url=AjaxServerUrl + "&date=" + currentDate;
productsListClient.open("GET", url);
productsListClient.onreadystatechange = getProductsListBack;
ProductsListFlag=false;
productsListClient.send(null);
}
catch(ex)
{
alert(ex.message);
}
}
//Waiting and processing server response
function getProductsListBack(response)
{
try
{
if(productsListClient.readyState == COMPLETE && productsListClient.status == OK)
{
ProductsListFlag = true;
//branch for IE/Windows ActiveX version
if (document.all)//IE
{
xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
xmlDocument.async = false;
//The responseText is loaded into XML document
xmlDocument.loadXML(productsListClient.responseText);
//Get ProductNodes
var productsListNodes = xmlDocument.selectNodes('NewDataSet/Products');
var rowCount=productsListNodes.length;
//SET DATA FOR EACH ROW
//===========================================================
for (var i=0; i<rowCount; i++)
{
//Get Page Elements
//Main Table Cells
var T1="T1_"+i;
var T2="T2_"+i;
var T3="T3_"+i;
var T4="T4_"+i;
var T5="T5_"+i;
var T6="T6_"+i;
var tdT1=window.document.getElementById(T1);
var tdT2=window.document.getElementById(T2);
var tdT3=window.document.getElementById(T3);
var tdT4=window.document.getElementById(T4);
var tdT5=window.document.getElementById(T5);
var tdT6=window.document.getElementById(T6);
var tr = tdT1.parentNode;
//Get Cells Content
var xmlElementProductId=productsListNodes[i].selectSingleNode('ProductID');
var xmlElementProductName=productsListNodes[i].selectSingleNode('ProductName');
var xmlElementQuantityPerUnit=productsListNodes[i].selectSingleNode('QuantityPerUnit');
var xmlElementPrice=productsListNodes[i].selectSingleNode('UnitPrice');
var xmlElementQuantity=productsListNodes[i].selectSingleNode('UnitsInStock');
var xmlElementReorderLevel=productsListNodes[i].selectSingleNode('ReorderLevel');
//tdT1.innerHTML = xmlElementProductId.text;
//tdT2.innerHTML = xmlElementProductName.text;
//tdT3.innerHTML = xmlElementQuantityPerUnit.text;
//tdT4.innerHTML = xmlElementPrice.text;
tdT5.innerHTML = xmlElementQuantity.text;
//tdT6.innerHTML = xmlElementReorderLevel.text;
var qty = parseInt(xmlElementQuantity.text);
var rol = parseInt(xmlElementReorderLevel.text);
if (qty < rol)
tr.style.color="#FF0000";
else
tr.style.color="#000000";
}
}
//branch for Firefox version
else if (document.implementation.createDocument)//Firefox
{
xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
xmlDocument.async = false;
//The responseText is loaded into XML document
xmlDocument.loadXML(productsListClient.responseText);
//Get ProductNodes
var productsListNodes = xmlDocument.selectNodes('NewDataSet/Products');
var rowCount=productsListNodes.length;
//SET DATA FOR EACH ROW
//===========================================================
for (var i=0; i<rowCount; i++)
{
//Get Page Elements
//Main Table Cells
var T1="T1_"+i;
var T2="T2_"+i;
var T3="T3_"+i;
var T4="T4_"+i;
var T5="T5_"+i;
var T6="T6_"+i;
var tdT1=window.document.getElementById(T1);
var tdT2=window.document.getElementById(T2);
var tdT3=window.document.getElementById(T3);
var tdT4=window.document.getElementById(T4);
var tdT5=window.document.getElementById(T5);
var tdT6=window.document.getElementById(T6);
var tr = tdT1.parentNode;
//Get Cells Content
var xmlElementProductId=productsListNodes[i].selectSingleNode('ProductID');
var xmlElementProductName=productsListNodes[i].selectSingleNode('ProductName');
var xmlElementQuantityPerUnit=productsListNodes[i].selectSingleNode('QuantityPerUnit');
var xmlElementPrice=productsListNodes[i].selectSingleNode('UnitPrice');
var xmlElementQuantity=productsListNodes[i].selectSingleNode('UnitsInStock');
var xmlElementReorderLevel=productsListNodes[i].selectSingleNode('ReorderLevel');
//tdT1.innerHTML = xmlElementProductId.text;
//tdT2.innerHTML = xmlElementProductName.text;
//tdT3.innerHTML = xmlElementQuantityPerUnit.text;
//tdT4.innerHTML = xmlElementPrice.text;
tdT5.innerHTML = xmlElementQuantity.textContent;
//tdT6.innerHTML = xmlElementReorderLevel.text;
var qty = parseInt(xmlElementQuantity.textContent);
var rol = parseInt(xmlElementReorderLevel.textContent);
if (qty < rol)
tr.style.color="#FF0000";
else
tr.style.color="#000000";
}
}
//Set date and time for LastUpdateMessages
var currentDateTime=document.getElementById("TopMessage");
var now = new Date();
currentDateTime.innerHTML=now;
var stopFlag = true;
//To Stop Streaming
for (var i=0; i<rowCount; i++)
{
//Get Quantity
var xmlElementQuantity=productsListNodes[i].selectSingleNode('UnitsInStock');
var qty = parseInt(xmlElementQuantity.text);
if (qty <100)
stopFlag = false;
}
var chkViewAjax = document.getElementById("chkViewAjax");
if(chkViewAjax.checked == false && stopFlag == true)
{
clearInterval(ProductsListInterval);
return;
}