
// declare a global  XMLHTTP Request object
var XmlHttpObj;
var fddval = "";
var sddval = "";
function disabledd(){
    var oZip = document.getElementById("zipcode");
	var oMake = document.getElementById("make");
	var oModel = document.getElementById("model");
	var oStyle = document.getElementById("style");
	var oDescription = document.getElementById("descr");
	oZip.focus();
    oMake.disabled = true;
    oModel.disabled = true;
    oStyle.disabled = true;
    oDescription.disabled = true;
    
}
function checkzip(zip){
    if (zip.value.length == 5 && isNumber(zip.value)){
        findzip(zip.value);
    }
}

function validate(){
	var err = false;
	var oZip = document.getElementById("zipcode");
	var astMsgZip = document.getElementById("astzip");
	var astMsgYear = document.getElementById("astyear");
	var astMsgMake = document.getElementById("astmake");
	var astMsgModel = document.getElementById("astmodel");
	var astMsgStyle = document.getElementById("aststyle");
	var astMsgGlass = document.getElementById("astglass");
	//
    var oMsgZip = document.getElementById("msgzip");
	//
	var oYear = document.getElementById("years");
	var oMake = document.getElementById("make");
	var oModel = document.getElementById("model");
	var oStyle = document.getElementById("style");
	var odescription = document.getElementById("descr");
			
		
	if (oZip.value == ""){
		astMsgZip.firstChild.data = "*";
		oMsgZip.firstChild.data = " Zip code must be 5 digit.";
		err = true;
	} else if (oZip.value.length  != 5) {
		oMsgZip.firstChild.data = " Zip code must be 5 digit.";
		astMsgZip.firstChild.data = "*";
		err = true;	
		
	} else if (! isNumber(oZip.value)) {
		astMsgZip.firstChild.data = "*";
		oMsgZip.firstChild.data = " Zip code must be 5 digit.";
		err = true;
	
	} else {
		astMsgZip.firstChild.data = "";
		oMsgZip.firstChild.data = "";
		err = false;
	}

	if (oYear.options[oYear.selectedIndex].value == ""){
		astMsgYear.firstChild.data = "*";
		err = true;
	} else {
		astMsgYear.firstChild.data = "";
	}
		
	if (oMake.options[oMake.selectedIndex].value == ""){
		astMsgMake.firstChild.data = "*";
		err = true;
	} else {
		astMsgMake.firstChild.data = "";
	}

	if (oModel.options[oModel.selectedIndex].value == ""){
		astMsgModel.firstChild.data = "*";
		err = true;
	} else {
		astMsgModel.firstChild.data = "";
	}	
	
	if (oStyle.options[oStyle.selectedIndex].value == ""){
		astMsgStyle.firstChild.data = "*";
		err = true;
	} else {
		astMsgStyle.firstChild.data = "";
	}
	if (odescription.options[odescription.selectedIndex].value == ""){
		astMsgGlass.firstChild.data = "*";
		err = true;
	} else {
		astMsgGlass.firstChild.data = "";
	}		
	
	if (! err){
		document.frm.submit();
	}
}
// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the continent dropdown list
function dropdownOnChange(fdd, ndd) 
{

	fddval = fdd;
    sddval = ndd;
    
    // define dropdown list
    var sYear ="";
    var sMake ="";
    var sModel ="";
    var sStyle = "";
    var sDescription ="";

	//span_textnode.data = "";
	
	//alert(span_text);
	var oYear = document.getElementById("years");
	var oMake = document.getElementById("make");
	var oModel = document.getElementById("model");
	var oStyle = document.getElementById("style");
	var oDescription = document.getElementById("descr");
	
	var curItem = document.getElementById(fdd);
	var oNdd = document.getElementById(ndd);

    if (oYear.selectedIndex != 'undefined'){
        sYear = oYear.options[oYear.selectedIndex].value;
		oNdd.disabled = false;
    }

    if (oMake.selectedIndex != 'undefined'){
        try {
            sMake = oMake.options[oMake.selectedIndex].value;
        }catch(err){
            //Handle errors here
        }    
        
		oNdd.disabled = false;
    }
	

    if (oModel.selectedIndex != 'undefined'){
        try {
            sModel = oModel.options[oModel.selectedIndex].value;
        }catch(err){
            //Handle errors here
        }     
        
		oNdd.disabled = false;
    }
    if (oStyle.selectedIndex != 'undefined'){
        try {
            sStyle = oStyle.options[oStyle.selectedIndex].value;
        }catch(err){
            //Handle errors here
        }    
        
		oNdd.disabled = false;
    }    
    if (oDescription.selectedIndex != 'undefined'){
        try {
            sDescription = oDescription.options[oDescription.selectedIndex].value;
        }catch(err){
            //Handle errors here
        }     
        
		oNdd.disabled = false;
    }    
       		          
    //clear follow dropdown itme  
    if (fdd == 'years'){
			if (curItem.selectedIndex == 0){
				clearNode('make');
				oMake.disabled = true; 
			}

            clearNode('model');
            clearNode('style');
            clearNode('descr'); //PROBLEM HERE
			oModel.disabled = true;
			oStyle.disabled = true;
			oDescription.disabled = true;
    }    
  
    if (fdd == 'make'){
			if (curItem.selectedIndex == 0){
				clearNode('model');
				oModel.disabled = true; 
			}
            clearNode('style');
            clearNode('descr');
			oStyle.disabled = true;
			oDescription.disabled = true;            
    } 

    if (fdd == 'model'){
			if (curItem.selectedIndex == 0){
				clearNode('style');
				oStyle.disabled = true; 
			}
            clearNode('descr');
			
			oDescription.disabled = true;               
            
    }               
  
    if (fdd == 'style'){
			if (curItem.selectedIndex == 0){
				clearNode('descr');
				oDescription.disabled = true;               
			}
    }  

    // retrieve data from ajax style if select is not blank.
    if (curItem.selectedIndex != 0){
        var curItemVal = curItem.options[curItem.selectedIndex].value;
        //alert(curItemVal);
        var curDropdown = document.getElementById(fdd);
        //alert(ndd);
        // get selected continent from dropdown list
        var selectedContinent = curDropdown.options[curDropdown.selectedIndex].value;
        
        // url of page that will send xml data back to client browser
        var requestUrl;
        // call asp file and return xml data
        requestUrl = "auto_xml.ashx" + "?fdd=" + encodeURIComponent(fdd) + "&ndd=" + encodeURIComponent(ndd) + "&item=" + encodeURIComponent(curItemVal) + "&year=" + encodeURIComponent(sYear) + "&make=" + encodeURIComponent(sMake) + "&model=" + encodeURIComponent(sModel) + "&style=" + encodeURIComponent(sStyle)+ "&description=" + encodeURIComponent(sDescription)
        var oMsg = document.getElementById("msg");
        oMsg.value = requestUrl;
        
	    CreateXmlHttpObj();
    	
	    // verify XmlHttpObj variable was successfully initialized
	    if(XmlHttpObj)
	    {
            // assign the StateChangeHandler function ( defined below in this file)
            // to be called when the state of the XmlHttpObj changes
            // receiving data back from the server is one such change
		    XmlHttpObj.onreadystatechange = StateChangeHandler;
    		
		    // define the iteraction with the server -- true for as asynchronous.
		    XmlHttpObj.open("GET", requestUrl,  true);
    		
		    // send request to server, null arg  when using "GET"
		    XmlHttpObj.send(null);		
	    }
	}
}


// this function called when state of  XmlHttpObj changes

function StateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulatedropDownList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

function StateChangeHandlerZip()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			dozip(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
// populate the contents of the country dropdown list
function PopulatedropDownList(countryNode)
{

	var dropDownList = document.getElementById(sddval);

	// clear the dropdown item list 
	for (var count = dropDownList.options.length-1; count >-1; count--)
	{
		dropDownList.options[count] = null;
	}

	var countryNodes = countryNode.getElementsByTagName('glass');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < countryNodes.length; count++)
	{
   		textValue = GetInnerText(countryNodes[count]);
		idValue = countryNodes[count].getAttribute("id");
		optionItem = new Option( URLDecode(textValue), URLDecode(idValue),  false, false);
		dropDownList.options[dropDownList.length] = optionItem;
	}
}

function dozip(xmlnode){
    var oCity = xmlnode.getElementsByTagName('city');
	var oShopname = xmlnode.getElementsByTagName('shop');
	var oPhone = xmlnode.getElementsByTagName('phone');
	
	
	var idValue;
	var textValue; 
	var optionItem;
	var txtShop;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < oCity.length; count++)
	{
   		textValue = GetInnerText(oCity[count]);
		txtShop = GetInnerText(oShopname[count]);
		document.getElementById('shop').innerHTML = txtShop ;
		document.getElementById('phone').innerHTML = GetInnerText(oPhone[count]);
		//$("#companyinfo").innerHtml = txtShop;
   		//textValue = textValue.toUperCase();
   		
   		//mystr = mystr.substr(0,1).toUpperCase()+mystr.substr(1);
   		var oMsgZip = document.getElementById("msgzip");
   		var oZip = document.getElementById("zipcode");
   		if (textValue == "none"){
   		    oMsgZip.firstChild.data = oZip.value + " is not in our service area";
   		} else {
   		    oMsgZip.firstChild.data = titleCase(textValue);
   		}
   		
   		//oMsgZip.firstChild.val
	}    

}
//
function findzip(zipcode){
    var x = "zip_xml.ashx?zip";
        // url of page that will send xml data back to client browser
    var requestUrl;
    // call asp file and return xml data
    requestUrl = "zip_xml.ashx?zip=" + encodeURIComponent(zipcode);
    var oMsg = document.getElementById("msg");
    oMsg.value = requestUrl;
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerZip;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}

}
// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function clearNode(sNode){
	//alert(sNode);
     var dropDownList = document.getElementById(sNode);
	// clear the dropdown item list 
	if (dropDownList.options.length > 1){
	    for (var count = dropDownList.options.length; count >-1; count--)
	    {
	    
	    	dropDownList.options[count] = null;
	    }
        var optionItem;
        optionItem = new Option( sNode, sNode,  false, false);
        dropDownList.options[0] = optionItem;
    }
	
}

function isNumber(val){
	if (isNaN(val))	{
		return false;
	} else {
		return true;
	} 
}

//
function titleCase(txt){
    var myValue = txt;

    var htext = myValue;
    htext = htext.toLowerCase();

    htext = htext.substr(0, 1).toUpperCase() + htext.substring(1, htext.length);
    for(var i=1; i<htext.length; i++)
    {
    if(htext.substr(i, 1) == " ")
    {
    while(htext.substr(i, 1) == " ")
    i++;

    if(i + 1 < htext.length)
    htext = htext.substr(0, i) + htext.substr(i, 1).toUpperCase() + htext.substring(i + 1, htext.length);
    else
    htext = htext.substr(0, i) + htext.substr(i, 1).toUpperCase();
    }
    }

    return htext;
}


function URLDecode(txt)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(txt).replace(lsRegExp, " ")); 
};

 function amcheck(am){
  oCbam = document.getElementById("am");
  if (oCbam.checked == true){
   document.getElementById("pm").checked = false;
  }
 }
 
 function pmcheck(pm){
  oCbpm = document.getElementById("pm");
  if (oCbpm.checked == true){
   document.getElementById("am").checked = false;
  }
 }
 
