﻿// JavaScript Document
	var xmlHttp; 
	var requestURL = 'AjaxResponse.aspx';                        
	var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0;

	function getHttpRequest(){
		// need to try different methods as each broser has its own way of doing this.
		if (navigator.appName=="Microsoft Internet Explorer") {
			 return new ActiveXObject("Msxml2.XMLHTTP");
		}
		else {
			return new XMLHttpRequest();
		}
	}
	
	function setupWhereSold(ModelNo, CalledFrom) {
	    var USState = $('select#selUSState option:selected').val();
	    var CANState = $('select#selCanState option:selected').val();
	    var Country  = $('select#selWorld option:selected').val();

        clearWhereSold(CalledFrom);
        
        document.getElementById('divAjaxResp').innerHTML = "Searching...";
	    
	    if (CANState != '' && CalledFrom=='selCanState'){
	        showWhereSold(ModelNo, 'CA', CANState);
	        return;
	    }
	    if (USState != '' && CalledFrom=='selUSState'){
	        showWhereSold(ModelNo, 'US', USState);
	        return;
	    }
	    if (Country != '' && CalledFrom=='selWorld'){
	        showWhereSold(ModelNo, Country, 'XX');
	        return;
	    }	    
	}
	
	function clearWhereSold(CalledFrom) {
	    //$("#divWhereSoldBox > select:not(#" + CalledFrom + ")").each(function() {
		$(".divWhereSoldSelector > select:not(#" + CalledFrom + ")").each(function() {
		    this.selectedIndex = 0;
	    });
    }

    function showWhereSold(ModelNo, CountryCd, StateCd)
	{
		// the q=random gives us a radom request to stop caching.
		// send fn=fName so that AjaxResponse can figure out what data to respond with
		var url = requestURL + '?' + 'fn=WhereSold&q=' + Math.random() + '&Model=' + ModelNo + '&CountryCd=' + CountryCd  + '&StateCd=' + StateCd;
		
		xmlHttp = getHttpRequest();
		xmlHttp.onreadystatechange = CallBackWhereSold;

		//Send the xmlHttp get to the specified url
		xmlHttp.open('GET', url, true); 
		xmlHttp.send(null);
	}
	
	// Handles callback for WhereSold data returned and puts the formatted HTML into divAjaxResp specified.
	function CallBackWhereSold() 
	{
		// State of 4 or 'complete' represents that data has been returned 
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){ 
			//Gather the results from the callback 
			var str = xmlHttp.responseText;
       
			//Populate the innerHTML of the div with the results 
			document.getElementById('divAjaxResp').innerHTML = str; 
			sizeVerticals();
		} 
	}
		
    // Online Dealers Ajax routines		
	function setupOnlineDealers(ModelNo) {

        var SelValue  = $('select#selOnlineDealers option:selected').val();
        
        //Parse out Color / Size / Country
        // Format is: Color & " ^ " & SizeCd & " ^ " & CountryCd
        var strary = (SelValue.split('^'));
        ColorCd = jQuery.trim(strary[0]);
        SizeCd = jQuery.trim(strary[1]);
        CountryCd = jQuery.trim(strary[2]);
        
        document.getElementById('divAjaxDealers').innerHTML = "Searching...";

	    showOnlineDealers(ModelNo, ColorCd, SizeCd, CountryCd);
	    
	    $("#productPurchaseOnlineForm").hide();
	    $("#divProductPurchaseOnlineDescr").hide();
	    //$("#productPurchaseOnlineLogos").show();
	}
	
	function resetOnlineDealers() {
	    $("#productPurchaseOnlineForm").show();
	    $("#productPurchaseOnlineLogos").hide();
	    $("#divProductPurchaseOnlineDescr").show();
	    $("#productPurchaseSorry").hide();
	}
	
	function showOnlineDealers(ModelNo, ColorCd, SizeCd, CountryCd)
	{
		// the q=random gives us a radom request to stop caching.
		// send fn=fName so that AjaxResponse can figure out what data to respond with
		var url = requestURL + '?' + 'fn=OnlineDealer&q=' + Math.random() + '&Model=' + ModelNo + '&ColorCd=' + ColorCd + '&SizeCd=' + SizeCd + '&CountryCd=' + CountryCd;
		
		xmlHttp = getHttpRequest();                                 
		xmlHttp.onreadystatechange = CallBackOnlineDealer;

		//Send the xmlHttp get to the specified url
		xmlHttp.open('GET', url, true); 
		xmlHttp.send(null);
	}
	
    // Handles callback for OnlineDealer data returned and puts the formatted HTML into divAjaxDealers specified.
	function CallBackOnlineDealer() 
	{
		// State of 4 or 'complete' represents that data has been returned 
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){ 
			//Gather the results from the callback 
			var str = xmlHttp.responseText;
      
			//Populate the innerHTML of the div with the results 
			document.getElementById('divAjaxDealers').innerHTML = str; 
			$("#productPurchaseSorry").show();
			$("#productPurchaseOnlineLogos").show();
			$(".productPurchaseOnlineLogo").tooltip();
			sizeVerticals();
			
			var trackRun = $("#divTrackRun").attr("value");
			if (trackRun != "") {
			    pageTracker._trackPageview(trackRun);
			}
		} 
	}
		
	function showData(fName, ModelNo)
	{
		// the q=random gives us a radom request to stop caching.
		// send fn=fName so that AjaxResponse can figure out what data to respond with
		var url = requestURL + '?' + 'fn=' + fName + '&q=' + Math.random();
		
		xmlHttp = getHttpRequest();                                 
		xmlHttp.onreadystatechange = CallBackHdlr;

		//Send the xmlHttp get to the specified url
		xmlHttp.open('GET', url, true); 
		xmlHttp.send(null);
	}





	function showServerTime()
	{             
		//Append the name to search for to the requestURL 
		//var url = requestURL;
		var url = requestURL + '?' + 'fn=None' + '&q=' + Math.random();
	            
		xmlHttp = getHttpRequest();                                 
		xmlHttp.onreadystatechange = CallBackHandler;
	            
		//Send the xmlHttp get to the specified url
		xmlHttp.open('GET', url, true); 
		xmlHttp.send(null);                        
	} 

	// This function will be called when the state
	// has changed, i.e. when data is returned from the server
	function CallBackHandler() 
	{ 
		// State of 4 or 'complete' represents that data has been returned 
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){ 
			//Gather the results from the callback 
			var str = xmlHttp.responseText; 
	        
			//Populate the innerHTML of the div with the results 
			document.getElementById('serverTime').innerHTML = str; 
		} 
	}
