/************************************************************************************/
/*                                                                                 	*/
/*                      Copy Right C. Crane Company                                	*/
/*                                 2007                                            	*/
/*                                                                                 	*/
/*                       CONFIDENTIAL DO NOT READ                                  	*/
/*                                                                                 	*/
/*                                                                                 	*/
/*  File Name: ajax.js                                                  			*/
/*  File Location: /AJAX/                                   						*/
/*                                                                                 	*/
/*  File Date: 09/01/07                                                     		*/
/*                                                                                 	*/
/*  File Description: This file will be used to make AJAX calls for the C. Crane	*/
/*					  Company Web Site and therefore improve the customer 			*/
/*					  experience.													*/
/*                                                                                 	*/
/*  Author: Edgardo G. Diaz                                                        	*/
/*                                                                                 	*/
/*  File Version: 1.1.1                                                     		*/
/*  Last revision date: 11/14/07                                                   	*/
/*  Last revised by: Edgardo G. Diaz                                               	*/
/*                                                                                 	*/
/*  Fix/Feature History:                                                           	*/
/*----------------------------------------------------------------------------------*/
/*     Date    : 11/14/07                                                          	*/
/*     Feature : Added support to the PageContent component so that the 			*/
/*				 content and support for a product is dynamically loaded and use	*/
/*				 javascript to display the content.									*/
/*     By      : Edgardo G. Diaz                                                   	*/
/*----------------------------------------------------------------------------------*/
/*     Date    : 10/11/07                                                          	*/
/*     Feature : Added AJAX support to the SmallListOrderBotTemplate so that no 	*/
/*				 portback takes place when an item is selected from the drop down	*/
/*				 menu of the order bot.												*/
/*     By      : Edgardo G. Diaz                                                   	*/
/*----------------------------------------------------------------------------------*/
/*     Date    : 10/09/07                                                          	*/
/*     Feature : Added AJAX support to the CountryMile WiFi web site so that 		*/
/*				 content is displayed after clicking a number of choices.			*/
/*     By      : Edgardo G. Diaz                                                   	*/
/************************************************************************************/

	/************************************************/
	/* Get the XML HTTP Object from the web browser	*/
	/* We will use this object to get AJAX responses*/
	/************************************************/
	function GetXmlHttpObject() 
	{
		var xmlHttp=null;
		try 
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest(); 
		}
		catch (e) 
		{
			// Internet Explorer
	  		try 
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
			}
			catch (e) 
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
		}
			
		return xmlHttp; 
	}
	
	/****************************************************************/
	/* This function is called from the product support components	*/
	/* and is used to determine which tab was clicked, once this is	*/
	/* determined, then we use AJAX to make a call to the required	*/
	/* file.  Once this is done, we change the color of the 		*/
	/* selected tab.												*/
	/****************************************************************/
	function ProductSupportFinder(alias, tab, contentTab)
	{ 
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  			alert ("Your browser does not support AJAX!");
	  		return;
  		} 
	
		var url
		
		switch (tab)
		{
			case 1:
				url = "/AJAX/SpecFinder.aspx?Alias=" + alias;
				break
			case 2:
				url = "/AJAX/FAQFinder.aspx?Alias=" + alias;
				break
			case 3:
				url = "/AJAX/ManualFinder.aspx?Alias=" + alias;
				break
			case 4:
				url = "/AJAX/TroubleshootFinder.aspx?Alias=" + alias;
				break
			case 5:
				url = "/AJAX/ProductContentFinder.aspx?Alias=" + alias;
				break	
		}
		
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		setSelectedTab(tab, contentTab);
	}
	
	/********************************************************************************/
	/* When AJAX determines that the requested file has successfully fetched, then	*/
	/* this function will be called and the appropriate innect HTML values will be	*/
	/* assigned to the appropriate element.											*/
	/********************************************************************************/
	function stateChanged() 
	{ 
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("txtProductSupport").innerHTML=xmlHttp.responseText;
		}
	}
	
	/********************************************/
	/* Changes the color of the selected tab	*/
	/********************************************/
	function setSelectedTab(tab, contentTab)
	{
		for (var i = 1; i <= 4; i++)
		{
			if (i == tab)
			{
				changeClass(contentTab + tab, "LightContentTab");
				changeClass("linkTab" + tab, "LightTabLinks");
				document.getElementById("imgLeftTab" + tab).src = "/faqs/images/left-tab-light.gif";		
				document.getElementById("imgRightTab" + tab).src = "/faqs/images/right-tab-light.gif";
			}
			else
			{
				changeClass(contentTab + i, "DarkTab");
				changeClass("linkTab" + i, "DarkTabLinks");
				document.getElementById("imgLeftTab" + i).src = "/faqs/images/left-tab-dark.jpg";		
				document.getElementById("imgRightTab" + i).src = "/faqs/images/right-tab-dark.jpg";
			}
		}
	}
	
	function findComponent(component)
	{
		var elem;
		if (document.getElementById)
		{
			var elem = document.getElementById(component);
		}
		else if (document.all)
		{
			var elem = document.all[component];
		}
		return elem;
	}
	
	/************************************************************************/
	/* Called from the setSelectedTab to change the style class of a tab	*/
	/************************************************************************/
	function changeClass(component, newClass)
	{
		var elem = findComponent(component);
		if (elem != null)
		{
			elem.className = newClass;
		}
	}

	/********************************************/
	/* Set the focus to the FAQ Tab by default	*/
	/********************************************/
	function focusTab(tab)
	{
		var value = document.getElementById('linkTab2').href;
		var array = value.replace("javascript:ProductSupportFinder(", "").split(",");
		var product = array[0].replace("'", "").replace("'", "");
		ProductSupportFinder(product, tab, 'contentTab');
	}

	/***********************************************************************/
	/* Determine which option was selected from the Country Mile WiFi Site */
	/***********************************************************************/
	var t;
	var i = '';
	var b = '';
	
	function FindWiFiSelection(obj, target)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  			alert ("Your browser does not support AJAX!");
	  		return;
  		} 
	
		var url;
		if (i != '')
		{
			url= "/AJAX/" + obj.options[obj.selectedIndex].id + ".aspx?Bot=" + b + "&Item=" + i;
		}
		else
		{
			url = "/AJAX/" + obj.options[obj.selectedIndex].id + ".aspx";
		}	
		t = target;
		
		xmlHttp.onreadystatechange=WiFiStateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	
	/********************************************************************************/
	/* When AJAX determines that the requested file has successfully fetched, then	*/
	/* this function will be called and the appropriate innect HTML values will be	*/
	/* assigned to the appropriate element.											*/
	/********************************************************************************/
	function WiFiStateChanged() 
	{ 
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById(t).innerHTML=xmlHttp.responseText;
		}
	}
	
	/************************************************************************/
	/* Reload the content when an item is selected from the drop down menu	*/
	/************************************************************************/
	function reloadContent(bot, control)
	{
		var s = document.getElementById(bot + '_' + control);
		i = s.options[s.selectedIndex].value;
		b = bot;
		FindWiFiSelection(document.getElementById("levelOption"), 'thirdLevel');
	}
	
	function addProduct(productID)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  			alert ("Your browser does not support AJAX!");
	  		return;
  		} 
	
		var url = "/AJAX/AddToCart.aspx?ProductID=" + productID;
		
		xmlHttp.onreadystatechange=AddToCart;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	
	function AddToCart() 
	{ 
		if (xmlHttp.readyState==4)
		{ 
			alert("added product to the shopping cart");
		}
	}
	
	/********************************************/
	/* Changes the color of the selected tab	*/
	/********************************************/
	function DisplaySelectedTab(tab)
	{
		var contentTabs = new Array("txtProductContent", "txtProductFAQ", "txtProductSpec", "txtProductManual", "txtProductTroubleshoot")
	
		for (var i = 1; i <= 5; i++)
		{
			var leftImage = findComponent("imgLeftTab" + i);
			var rightImage = findComponent("imgRightTab" + i);
			
			if (i == tab)
			{
				changeClass(contentTabs[i - 1], "Show");
				changeClass("contentTab" + i, "LightContentTab");
				changeClass("linkTab" + i, "LightTabLinks");
				leftImage.src = "/faqs/images/left-tab-light.gif";
				rightImage.src = "/faqs/images/right-tab-light.gif"
			}
			else
			{
				changeClass(contentTabs[i - 1], "Hidden");
				changeClass("contentTab" + i, "DarkTab");
				changeClass("linkTab" + i, "DarkTabLinks");
				if (leftImage != null)
					leftImage.src = "/faqs/images/left-tab-dark.jpg";
				if (rightImage != null)
					rightImage.src = "/faqs/images/right-tab-dark.jpg";
			}
		}
	}