
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// 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 when select country
function selCountry(country) 
{
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "frm_epr_xml_data_provider.php";
    requestUrl = requestUrl + "?actionn=selprovince&country="+country;
    //alert(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);	
			
	}
	return 1;
}

// called when form submitted
function frmSubmit() 
{
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "frm_epr_xml_data_provider.php?actionn=sendmail";
    requestUrl = requestUrl + "&year="+document.getElementById('frm_epr_year').value+ "&makemodel="+document.getElementById('frm_epr_makemodel').value;
    ob=document.getElementById('frm_epr_partselected');
    partselected='';
    for (var i = 0; i < ob.options.length; i++)
    {
        if (i < ob.options.length-1) partselected=partselected+ob.options[i].value+', ';
        else partselected=partselected+ob.options[i].value;
    }
    requestUrl = requestUrl + "&selparts="+partselected;
    requestUrl = requestUrl + "&country="+document.getElementById('frm_epr_country').value+ "&state="+document.getElementById('frm_epr_state').value+ "&city="+document.getElementById('frm_epr_city').value;
    requestUrl = requestUrl + "&name="+document.getElementById('frm_epr_name').value+ "&email="+document.getElementById('frm_epr_email').value+ "&phone="+document.getElementById('frm_epr_phone').value;
    requestUrl = requestUrl + "&message="+document.getElementById('frm_epr_msg').value;
    toemail=document.getElementById('frm_epr_toemail').value;
    //start=toemail.lastIndexOf("\\")+1; 
    //toemail1=toemail.substring(start);
    requestUrl = requestUrl + "&toemail="+toemail;
    requestUrl = requestUrl + "&redirecturl="+document.getElementById('frm_epr_url').value;
	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);	
			
	}
	return 1;
}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	//alert('readystate: ' + XmlHttpObj.readyState);
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{	
			var response = XmlHttpObj.responseXML.documentElement;
			var actionn = response.getAttribute("act");
			if (actionn=='selprovince') PopulateDropDown(response);
			else if (actionn=='sendmail') RetMsg(response);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the fields
function PopulateDropDown(Node)
{
    document.getElementById('frm_epr_state').value='';
	var List = document.getElementById('frm_epr_stated');

	var Nodes = Node.getElementsByTagName('field');
	var textValue; 
	var optionItem;
	
	textValue = GetInnerText(Nodes[0]);
	if (textValue=='no data')
   	{
	   	List.style.display="none";
	   	document.getElementById('frm_epr_state').style.display="";
   	}
   	else
   	{
	   	List.style.display="";
	   	document.getElementById('frm_epr_state').style.display="none";
		for (var count = List.options.length-1; count >0; count--)
		{
			List.options[count] = null;
		}
		
		// populate the dropdown list with data from the xml doc
		for (var count = 0; count < Nodes.length; count++)
		{
	   		textValue = GetInnerText(Nodes[count]);
			optionItem = new Option( textValue, textValue, false, false);
			List.options[List.length] = optionItem;
		}
	}
}

// populate the contents of the fields
function RetMsg(Node)
{

	var Nodes = Node.getElementsByTagName('field');
	var textValue; 
	
	textValue = GetInnerText(Nodes[0]);
	err=Nodes[0].getAttribute("err"); 
	if (err=='yes')
	{
		document.getElementById('frm_epr_msgspan_err').style.display="";
		document.getElementById('frm_epr_msgspan_success').style.display="none";
		document.getElementById('frm_epr_msgspan_err').innerHTML=textValue;
	}
	else
	{
		document.getElementById('frm_epr_msgspan_success').style.display="none";
		document.getElementById('frm_epr_msgspan_err').style.display="none";
		window.location=textValue;
	}	


}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}