// File:  gsics.js
//

var gsics0=null;
try{
gsics0=new gsics();
gsics0.getNewFile();
}
catch(e)
{
document.getElementById("errormsg").innerHTML="Error: No Product data.";
}

//Class (Type) gsics
//This class is used to load and manipulate Product Information data taken from 
//the Product xml file (gsi.xml).
/////////////////////////////////////////////////////////////////////////////////////
function gsics()
{
	
this.xhr=false; //HttpRequest Loader
this.XMLDoc0=null; //XML Document
this.debug=false;

//function getNewFile()
//Makes an XML request.  Testing purposes.
/////////////////////////////////////////////////////////////////////////////////////
function getNewFile()
{
	XMLRequest("../xml/gsi.xml");
	return false;
	
}
this.getNewFile=getNewFile;

//function outputStatus(StatMsg, MsgType)
//Sends Status data to the status field to display for the user.
//StatMsg:string=The actual status string.
//MsgType:string=indicates the type of message to display. 
//Types include the following:
//"debug"=for debugging  "error"=for errors  "success"=for successful transactions.
/////////////////////////////////////////////////////////////////////////////////////
function outputStatus(StatMsg, MsgType)
{	
	try{	
	if(MsgType=="debug" && this.debug==true)
		document.getElementById("errormsg").innerHTML=StatMsg;	
	if(MsgType!="debug")
		document.getElementById("errormsg").innerHTML=StatMsg;
	}
	catch(e){}
}
this.outputStatus=outputStatus;

//function showContents()
//Displays the content of the xml data if it is loaded.
/////////////////////////////////////////////////////////////////////////////////////
function showContents()
{
	var outMsg="No Data";
	
	if(xhr.readyState==4)
	{
		if(xhr.status==200)
		{
			//The response was successfully recognized as xml.
			if(xhr.responseXML.contentType=="text/xml")	
			{
				XMLDoc0=xhr.responseXML;
						
				outMsg="Detected xml document: Mozilla/Firefox";	
			}
			//The response was not successfully recognized as xml.
			else
			{
				//Parse the response text manually into an xml document (standards).
				if (window.DOMParser)
  				{
  					XMLDoc0=(new DOMParser()).parseFromString(xhr.responseText, "text/xml");
					outMsg="Detected text document: Using standards compliant DOM Parser.";
 				}
				//Parse the response text manually into an xml document (Internet Explorer).
				else
  				{
  					XMLDoc0=new ActiveXObject("Microsoft.XMLDOM");
  					XMLDoc0.async="false";
  					XMLDoc0.loadXML(xhr.responseText);
					outMsg="Detected text document: Using Microsoft.XMLDOM Parser";
  				} 
				
			}
			outputStatus(outMsg, "debug");
			FillProductLinks();
		}
		else
		{
			outMsg="There was a problem with the request "+xhr.status;
			outputStatus(outMsg, "error");
		}
	}
	
}
this.showContents=showContents;


//function XMLRequest(url)
//Creates a new XMLHttpRequest and sends it to the server.
//url:string=url to get the xml data from.
/////////////////////////////////////////////////////////////////////////////////////
function XMLRequest(url)
{
	//For standards compliance browsers and Internet Explorer 7 or greater.
	if(window.XMLHttpRequest)	
	{
		xhr=new XMLHttpRequest();
		if(xhr.overrideMimeType)
		{
			xhr.overrideMimeType("text/xml");
		}
	}
	//For Internet Explorer 6 or lower.
	else
	{
		if(window.ActiveXObject)
		{
			try{
				xhr=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){}
		}
	}
	
	if(xhr)
	{
		xhr.onreadystatechange=showContents;
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else
	{
		outputStatus("Error:  Error creating XMLHTTP Request.", "error");
	}
}
this.XMLRequest=XMLRequest;

//function SearchProductId(ProdId0)
//Searches an XML document for a specified UserId and returns the entry. Returns null
//if the product is not found
//ProdId0:string:ProdId index to search for.
/////////////////////////////////////////////////////////////////////////////////////
function SearchProductId(ProdId0)
{
	if(XMLDoc0==null)
		return;
	var ProdElement=XMLDoc0.getElementsByTagName("ProductList")[0].getElementsByTagName("Product");
	var e0=0;
	var Value0="";
	
	for(e0=0; e0<ProdElement.length; e0++)
	{
		Value0=ProdElement[e0].getElementsByTagName("ProductID")[0].firstChild.data;
		if(ProdId0==Value0)
			return ProdElement[e0];
	}
	return null;
}
this.SearchProductId=SearchProductId;

//function FillProductLinks()
//Fills a Form with User information based on the specified UserId
//if the user is found.  Otherwise, the form is empty.
/////////////////////////////////////////////////////////////////////////////////////
function FillProductLinks()
{
	
	var ProdLen=XMLDoc0.getElementsByTagName("ProductList")[0].getElementsByTagName("Product").length;
	var ProdElement=null;
	var AElements=null;
	var SpanElements=null;
	
	var p0=0;
	var ele0=0;
	
	for(p0=0; p0<=ProdLen; p0++)
	{
		ProdElement=SearchProductId(p0);
		if(ProdElement!=null)
		{
			try{
				AElements=getElementsById("p"+p0,"a");
				SpanElements=getElementsById("s"+p0,"span");
				ele0=AElements.length;
				
				for(ele0=0; ele0<AElements.length; ele0++)
				{
					AElements[ele0].innerHTML="Add "+
					ProdElement.getElementsByTagName("Name")[0].firstChild.data+
					" to shopping cart.";
					AElements[ele0].href="javascript:gsics0.popUp('AddtoCart.aspx?p="+p0+"&q="+
					CurrentPageName()+"*"+ProdElement.getElementsByTagName("TrainingType")[0].firstChild.data
					+"',150,500);";
					try{
					SpanElements[ele0].innerHTML="<br/>Price: "+
					ProductPrice(ProdElement)+"<br/>";
					}
					catch(e0){}
				}
				
				}
			catch(e)
			{}
		}
	}
}
this.FillProductLinks=FillProductLinks;

//function popUp(URL,h,w) 
//Pops up a simple new window specified by the URL.
//URL:string=URL of the new window.
//h:integer=height of the new window in pixels.
//w:integer=width of the new window in piexels.
/////////////////////////////////////////////////////////////////////////////////////
function popUp(URL,h,w) 
{
	day = new Date();
	id = day.getTime();
	l=(screen.width - w)/2;
	t=(screen.height - h)/2;

	eval("page" + id + " = window.open(URL, '" + id + 	
		"','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,"+
		"width="+w+",height="+h+",left = "+l+",top = "+t+"');");
}
this.popUp=popUp;

//function popUpFull(URL,h,w) 
//Pops up a full new window (with address, status, menu, scroll) specified by the URL.
//URL:string=URL of the new window.
//h:integer=height of the new window in pixels.
//w:integer=width of the new window in piexels.
/////////////////////////////////////////////////////////////////////////////////////
function popUpFull(URL,h,w) 
{
	day = new Date();
	id = day.getTime();
	l=(screen.width - w)/4;
	t=(screen.height - h)/4;

	eval("page" + id + " = window.open(URL, '" + id +
		"','toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,"+
		"width="+w+",height="+h+",left = "+l+",top = "+t+"');");
}
this.popUpFull=popUpFull;

//function CurrentPageName()
//Returns the current document page with no arguements.
/////////////////////////////////////////////////////////////////////////////////////
function CurrentPageName() 
{
  var u0=document.location.href.substring(
    document.location.href.length
    , document.location.href.lastIndexOf('/') + 1
  );
  
  	if(u0.lastIndexOf('#')==-1 && u0.lastIndexOf('?')==-1)
  		return u0;
	if(u0.lastIndexOf('#')!=-1)
	  	return u0.slice(0, u0.lastIndexOf('#'));
	return u0.slice(0, u0.lastIndexOf('?'));
}
this.CurrentPageName=CurrentPageName;

//function ProductPrice(PElement)
//Returns the price of the product in the passed xml element.
//PElement:XMLElement=XML element with the product information.
/////////////////////////////////////////////////////////////////////////////////////
function ProductPrice(PElement) 
{
	var PP0=PElement.getElementsByTagName("Prices")[0].getElementsByTagName("Price")[0].firstChild.data;
	var RP0=PElement.getElementsByTagName("ReferencePrices")[0].getElementsByTagName("Price")[0].firstChild.data;
	var TP0=parseFloat(PP0)+parseFloat(RP0);
	
	//Return the price if only a single price.
	if(PElement.getElementsByTagName("Prices").length==1)
		return "$"+TP0.toFixed(2);
		
	//Multiple prices for a product	based on quantities
	var MQty0=PElement.getElementsByTagName("Prices")[1].getElementsByTagName("MaxQty")[0].firstChild.data
	var PString0="$"+TP0.toFixed(2)+" for quantities under "+MQty0+".";
	var p0=1;
	
	for(p0=1; p0<PElement.getElementsByTagName("Prices").length; p0++)
	{
		PP0=PElement.getElementsByTagName("Prices")[p0].getElementsByTagName("Price")[0].firstChild.data;
		try{
		RP0=PElement.getElementsByTagName("ReferencePrices")[p0].getElementsByTagName("Price")[0].firstChild.data;
		}
		catch(e){RP0=0.00;}
		try{
		MQty0=PElement.getElementsByTagName("Prices")[p0+1].getElementsByTagName("MaxQty")[0].firstChild.data;
		}
		catch(e){MQty0=100; break;}
		
		TP0=parseFloat(PP0)+parseFloat(RP0);
		PString0+="<br/>Price: $"+TP0.toFixed(2)+" for quantities under "+MQty0+".";
	}
	return PString0;
}

//function getElementsById(id, tagName)
//Returns all elements with an id name and specified by a certain tag e.g. "div"
//id:string=name of tag to look for.
//tagName:string=type of tag to look for.
/////////////////////////////////////////////////////////////////////////////////////
function getElementsById(id, tagName)
{
   var divs = document.getElementsByTagName(tagName);
   var arr = new Array();
   
   for (var i=0; i < divs.length; i++)
   {
      if (divs[i].id == id)
      {
         arr[arr.length] = divs[i];
      }
   }
   return arr;
}
this.getElementsById=getElementsById;

//function EvaluateContentType()
//Returns the content type of the response.  Testing purposes.
/////////////////////////////////////////////////////////////////////////////////////
function EvaluateContentType()
{
	var outMsg="No Data";
	if(xhr.responseXML.contentType=="text/xml")	
		outMsg="Detected XML document.";	
	else
	if(xhr.contentType=="text/plain")
		outMsg="Detected text document.";
	else
		outMsg="Detected unknown document type: "+xhr.contentType;
	outputStatus(outMsg, "debug"); 
}
this.EvaluateContentType=EvaluateContentType;

}
