/************************************************************************************/
/*                                                                                 	*/
/*                      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 = "/images/faqs/left-tab-light.gif";		
				document.getElementById("imgRightTab" + tab).src = "/images/faqs/right-tab-light.gif";
			}
			else
			{
				changeClass(contentTab + i, "DarkTab");
				changeClass("linkTab" + i, "DarkTabLinks");
				document.getElementById("imgLeftTab" + i).src = "/images/faqs/left-tab-dark.jpg";		
				document.getElementById("imgRightTab" + i).src = "/images/faqs/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);
		//alert(elem);
		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, type) 
	{
	    var contentTabs;
	    var iterations;
	    var leftTab = "imgLeftTab";
	    var rightTab = "imgRightTab";
	    var contentTab = "contentTab";
	    var linkTab = "linkTab";
	    
	    if (type == "support")
	    {
	        contentTabs = new Array("txtProductContent", "txtProductFAQ", "txtProductSpec", "txtProductManual", "txtProductTroubleshoot", "txtProductTestimonial", "txtProductDownloads", "txtLabReport", "txtComparison", "txtFacts");
	        var iterations = 10;
	    }
	    else 
	    {
	        if (type == "manager")
	        {
	            contentTabs = new Array("txtAdvertising", "txtCustomerService", "txtProcessing", "txtWeb");
	            var iterations = 4;
	        }
	        if (type == "views") 
	        {
	            contentTabs = new Array("pnlAdditional", "pnlViews");
	            var iterations = 2;
	            leftTab = "imgViewLeft";
	            rightTab = "imgViewRight";
	            contentTab = "contentView";
	            linkTab = "lnkView"
	        }
	    }
		
	
		for (var i = 1; i <= iterations; i++)
		{
			var leftImage = findComponent(leftTab + i);
			var rightImage = findComponent(rightTab + i);
			
			if (i == tab)
			{
				changeClass(contentTabs[i - 1], "Show");
				changeClass(contentTab + i, "LightContentTab");
				changeClass(linkTab + i, "LightTabLinks");
				leftImage.src = "/images/navigation/left-tab-light.jpg";
				rightImage.src = "/images/navigation/right-tab-light.jpg"
			}
			else
			{
				changeClass(contentTabs[i - 1], "Hidden");
				changeClass(contentTab + i, "DarkTab");
				changeClass(linkTab + i, "DarkTabLinks");
				if (leftImage != null)
					leftImage.src = "/images/navigation/left-tab-dark.jpg";
				if (rightImage != null)
					rightImage.src = "/images/navigation/right-tab-dark.jpg";
			}
		}
    }

    /************************************/
    /* Sign in on the Enter key			*/
    /* 10/30/08							*/
    /************************************/
    function SignIn() {
        // ED 10/02/07
        // Firefox cannot click a button programmatically.
        // 10/30/08 Firefox version 3.0.3 still does not have this functionality
        var txtSIEmail = findComponent("ctl00_ContentHolder_txtSIEmail");
        var txtSIPassword = findComponent("ctl00_ContentHolder_txtSIPassword");
        if (txtSIEmail != null && txtSIPassword != null) 
        {
            if (txtSIEmail.value == "" && txtSIPassword.value == "") 
            {
                var btnCreate = findComponent("ctl00_ContentHolder_btnCreate");
                if (btnCreate != null)
                    btnCreate.click();
            }
            else 
            {
                // Make sure that the data in the create account is complete
                // before submitting to the server
                var btnSignIn = findComponent("ctl00_ContentHolder_btnSignIn");
                if (btnSignIn != null)
                    btnSignIn.click();
            }
        }
    }

    function chooseButton(evt, page) {
        if (evt.keyCode == 13)
            SignIn();
    }

    function onSignIn(evt) {
        evt = evt || event;
        if (is_ie5up)
            chooseButton(evt);
        else if (is_gecko)
            chooseButton(evt);
    }

    function copyBillingAddress() {
        var sameBilling = findComponent("ctl00_ContentHolder_BillToShipTo");
        if (sameBilling != null) {
            var s_nickname = findComponent("ctl00_ContentHolder_ShipAddressList")
            var s_fname = findComponent("ctl00_ContentHolder_Ship_FirstName");
            var s_mi = findComponent("ctl00_ContentHolder_Ship_MI");
            var s_lname = findComponent("ctl00_ContentHolder_Ship_LastName");
            var s_suffix = findComponent("ctl00_ContentHolder_ShipSuffix");
            var s_company = findComponent("ctl00_ContentHolder_Ship_Company");
            var s_address1 = findComponent("ctl00_ContentHolder_Ship_Address1");
            var s_address2 = findComponent("ctl00_ContentHolder_Ship_Address2");
            var s_city = findComponent("ctl00_ContentHolder_Ship_City");
            var s_state = findComponent("ctl00_ContentHolder_Ship_State");
            var s_zip = findComponent("ctl00_ContentHolder_Ship_Zip");
            var s_country = findComponent("ctl00_ContentHolder_Ship_Country");
            var s_phone = findComponent("ctl00_ContentHolder_Ship_Phone");
            var s_ext = findComponent("ctl00_ContentHolder_ShipPhoneExt");

            if (sameBilling.checked) {
                var b_nickname = findComponent("ctl00_ContentHolder_BillAddressList")
                var b_fname = findComponent("ctl00_ContentHolder_Bill_FirstName");
                var b_mi = findComponent("ctl00_ContentHolder_Bill_MI");
                var b_lname = findComponent("ctl00_ContentHolder_Bill_LastName");
                var b_suffix = findComponent("ctl00_ContentHolder_BillSuffix");
                var b_company = findComponent("ctl00_ContentHolder_Bill_Company");
                var b_address1 = findComponent("ctl00_ContentHolder_Bill_Address1");
                var b_address2 = findComponent("ctl00_ContentHolder_Bill_Address2");
                var b_city = findComponent("ctl00_ContentHolder_Bill_City");
                var b_state = findComponent("ctl00_ContentHolder_Bill_State");
                var b_zip = findComponent("ctl00_ContentHolder_Bill_Zip")
                var b_country = findComponent("ctl00_ContentHolder_Bill_Country");
                var b_phone = findComponent("ctl00_ContentHolder_Bill_Phone");
                var b_ext = findComponent("ctl00_ContentHolder_BillPhoneExt");

				if (b_nickname) {
	                s_nickname.selectedIndex = b_nickname.selectedIndex; }
                s_fname.value = b_fname.value;
                s_mi.value = b_mi.value;
                s_lname.value = b_lname.value;
                s_suffix.selectedIndex = b_suffix.selectedIndex
                s_company.value = b_company.value;
                s_address1.value = b_address1.value;
                s_address2.value = b_address2.value;
                s_city.value = b_city.value;
                s_state.selectedIndex = b_state.selectedIndex;
                s_zip.value = b_zip.value;
                s_country.selectedIndex = b_country.selectedIndex;
                s_country.disabled = b_country.disabled
                s_phone.value = b_phone.value;
                s_ext.value = b_ext.value;
            }
            else {
                s_nickname.selectedIndex = 0;
                s_fname.value = "";
                s_mi.value = "";
                s_lname.value = "";
                s_suffix.selectedIndex = 0
                s_company.value = "";
                s_address1.value = "";
                s_address2.value = "";
                s_city.value = "";
                s_state.selectedIndex = 0;
                s_zip.value = "";
                s_country.selectedIndex = 0;
                s_phone.value = "";
                s_ext.value = "";
            }
        }
    }

    function displayAdvertiserPrompts() {
        var list = findComponent("ctl00_ContentHolder_listRef");
        if (list != null) {
            var index = list.selectedIndex;
            if (index > 0) {
                var lbl = findComponent("ctl00_ContentHolder_lblSourceMessage");
                if (lbl != null) {
                    switch (index) {
                        case 3:
                            lbl.innerHTML = "<b>Please specify which magazine</b>";
                            break
                        case 4:
                            lbl.innerHTML = "<b>Please specify which newspaper</b>";
                            break
                        case 5:
                            lbl.innerHTML = "<b>Please specify which website</b>";
                            break
                        case 6:
                            lbl.innerHTML = "<b>Please specify host, station or program</b>";
                            break
                        case 7:
                            lbl.innerHTML = "<b>Please specify which search engine</b>";
                            break
                        case 8:
                            lbl.value = "<b>Please specify Friend, Relative, Company, Etc.</b>";
                            break
                        default:
                            lbl.value = "";
                            break
                    }
                }
            }
        }
    }

    function showSelection(choice) {
        if (choice == "P") {
            // The customer wants a printed catalog
            document.getElementById("printedCatalog").style.display = "inline";
            document.getElementById("electronicCatalog").style.display = "none";
            document.getElementById("audioCatalog").style.display = "none";
        }
        else if (choice == "E") {
            // The customer wants an electronic catalog
            document.getElementById("printedCatalog").style.display = "none";
            document.getElementById("audioCatalog").style.display = "none";
            document.getElementById("electronicCatalog").style.display = "inline";
        }
        else if (choice == "A") {
            // The customer wants an audio catalog
            document.getElementById("printedCatalog").style.display = "none";
            document.getElementById("electronicCatalog").style.display = "none";
            document.getElementById("audioCatalog").style.display = "inline";
        }
    }
    
    function displayPanel(image, panel, path) {
        var img = findComponent("ctl00_ContentHolder_" + image);
        var pnl = findComponent("ctl00_ContentHolder_" + panel);
	    if (pnl.style.display == "block") {
		    img.src = path + "plus.jpg";
		    pnl.style.display = "none";
	    }
	    else
	    {
		    img.src = path + "minus.jpg";
		    pnl.style.display = "block";
	    }
    }
    
    function toggleHiddenPanel(panel) {
    	var pnl = findComponent(panel);
	    if (pnl.style.display == "block") {
		    pnl.className = "flyout Show"
	    }
	    else
	    {
		    pnl.className = "flyout Hidden"
	    }
    }       