
// 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();
	}
}

// functions to handle add/edit drop-downs

// called from onChange or onClick event of the address dropdown list
function BillAddressOnChange() 
{
    var catList = document.getElementById("selExistBillAddress");
    
    // get selected continent from dropdown list
    var selectedCat = catList.options[catList.selectedIndex].value;
	if (selectedCat != '') {
    	// url of page that will send xml data back to client browser
	    var requestUrl;
		// use the following line if using asp
		//requestUrl = "xml_data_provider.asp" + "?filter=" + encodeURIComponent(selectedContinent);
		// use the following line if using php
		 requestUrl = "addressxml.php" + "?type=" + encodeURIComponent(selectedCat);
		//alert ("Requesting page: " + 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 = NewBillAddressStateChangeHandler;
			
			// 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
// we're interested in the state that indicates data has been
// received from the server
function NewBillAddressStateChangeHandler()
{
	// 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)
		{			
			//alert("XML status is 200");
			PopulateBillAddress(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the project dropdown list
function PopulateBillAddress(addressNode)
{
	var addressNodes = addressNode.getElementsByTagName('address');
	// populate the address with data from the xml doc
	//alert("Populating project list");
	if (addressNodes.length > 0) {
		document.getElementById('bill_fname').value = addressNodes[0].getAttribute("fname");
		document.getElementById('bill_lname').value = addressNodes[0].getAttribute("lname");
		document.getElementById('bill_organization').value = addressNodes[0].getAttribute("organization");
		document.getElementById('bill_address1').value = addressNodes[0].getAttribute("address1");
		document.getElementById('bill_address2').value = addressNodes[0].getAttribute("address2");
		document.getElementById('bill_city').value = addressNodes[0].getAttribute("city");
		document.getElementById('bill_state').value = addressNodes[0].getAttribute("state");
		var countrylist = document.getElementById('bill_country');
		for (var i = 0; i < countrylist.length; i++) {
			if (countrylist.options[i].value == addressNodes[0].getAttribute("country")) {
				countrylist.options[i].selected = true;
			}
		}
		//document.getElementById('bill_country').value = addressNodes[0].getAttribute("country");
		document.getElementById('bill_zipcode').value = addressNodes[0].getAttribute("zipcode");
		document.getElementById('bill_dayphone').value = addressNodes[0].getAttribute("dayphone");
		document.getElementById('bill_evephone').value = addressNodes[0].getAttribute("evephone");
		document.getElementById('bill_fax').value = addressNodes[0].getAttribute("fax");
		document.getElementById('bill_email').value = addressNodes[0].getAttribute("email");
	}
}

// called from onChange or onClick event of the address dropdown list
function ShipAddressOnChange() 
{
    var catList = document.getElementById("selExistShipAddress");
    
    // get selected continent from dropdown list
    var selectedCat = catList.options[catList.selectedIndex].value;
	if (selectedCat != '') {
    	// url of page that will send xml data back to client browser
	    var requestUrl;
		// use the following line if using asp
		//requestUrl = "xml_data_provider.asp" + "?filter=" + encodeURIComponent(selectedContinent);
		// use the following line if using php
		 requestUrl = "addressxml.php" + "?type=" + encodeURIComponent(selectedCat);
		//alert ("Requesting page: " + 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 = NewShipAddressStateChangeHandler;
			
			// 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
// we're interested in the state that indicates data has been
// received from the server
function NewShipAddressStateChangeHandler()
{
	// 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)
		{			
			//alert("XML status is 200");
			PopulateShipAddress(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the project dropdown list
function PopulateShipAddress(addressNode)
{
	var addressNodes = addressNode.getElementsByTagName('address');
	// populate the address with data from the xml doc
	//alert("Populating project list");
	if (addressNodes.length > 0) {
		document.getElementById('ship_fname').value = addressNodes[0].getAttribute("fname");
		document.getElementById('ship_lname').value = addressNodes[0].getAttribute("lname");
		document.getElementById('ship_organization').value = addressNodes[0].getAttribute("organization");
		document.getElementById('ship_address1').value = addressNodes[0].getAttribute("address1");
		document.getElementById('ship_address2').value = addressNodes[0].getAttribute("address2");
		document.getElementById('ship_city').value = addressNodes[0].getAttribute("city");
		document.getElementById('ship_state').value = addressNodes[0].getAttribute("state");
		var countrylist = document.getElementById('ship_country');
		for (var i = 0; i < countrylist.length; i++) {
			if (countrylist.options[i].value == addressNodes[0].getAttribute("country")) {
				countrylist.options[i].selected = true;
			}
		}
		//document.getElementById('ship_country').value = addressNodes[0].getAttribute("country");
		document.getElementById('ship_zipcode').value = addressNodes[0].getAttribute("zipcode");
		document.getElementById('ship_dayphone').value = addressNodes[0].getAttribute("dayphone");
		document.getElementById('ship_evephone').value = addressNodes[0].getAttribute("evephone");
		document.getElementById('ship_fax').value = addressNodes[0].getAttribute("fax");
		document.getElementById('ship_email').value = addressNodes[0].getAttribute("email");
	}
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}