var xmlHttp;

function GetXmlHttpObject()	{
	var xmlHttp=null;
	try	{
	  	// Firefox, Opera 8.0+, Safari
	  	xmlHttp=new XMLHttpRequest();
  	}
	catch (e) {
  		// Internet Explorer
		try	{
			// assume IE6 or older
		    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
		                                    "MSXML2.XMLHTTP.5.0",
		                                    "MSXML2.XMLHTTP.4.0",
		                                    "MSXML2.XMLHTTP.3.0",
		                                    "MSXML2.XMLHTTP",
		                                    "Microsoft.XMLHTTP");
		    // try every id until one works
		    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
		      try { 
		        // try to create XMLHttpRequest object
		        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
		      } 
		      catch (e) {} // ignore potential error
		    }
	    }
	  	catch (e)	{
	    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
  	}
	return xmlHttp;
}

/****************************************************
* Customer 
*****************************************************/
// make asynchronous HTTP request using the XMLHttpRequest object
// to get the customer infomation 
function ajaxCustomer() { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}

  	var customer = document.getElementById("cust_company").value;
	
	if(customer == "" || customer == '0') {
		alert("Please select a company name");
		document.getElementById("cust_company").value 	= "";
		document.getElementById("cust_attn").value 		= "";
		document.getElementById("cust_phone").value 	= "";
		document.getElementById("cust_mobile").value 	= "";
		document.getElementById("cust_fax").value 		= "";
		document.getElementById("cust_pobox").value 	= "";
		document.getElementById("cust_email").value 	= "";
		document.getElementById("cust_city").value 		= "";
		return false;
	}// proceed only if the xmlHttp object isn't busy 
	else {
		var url="./ajax/AjaxCustomers.php?cust_company=" + customer;
		
		xmlHttp.onreadystatechange = customerServerResponse;
		xmlHttp.open("GET",url,true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xmlHttp.send(null);
	}
}

function customerServerResponse() { 
	if (xmlHttp.readyState==4)	{ 
		var response =  xmlHttp.responseText;
		var update = new Array();
		
	    if(response.indexOf('|') != -1) {
	            
	    	update = response.split('|');

			if (document.getElementById('cust_id'))
			{
				document.getElementById('cust_id').value 	= update[7];
			}
	        // update the client display using the data received from the server 
			document.getElementById('cust_attn').value 		= update[0];
			document.getElementById('cust_phone').value 	= update[1];
			document.getElementById('cust_mobile').value	= update[2];
			document.getElementById('cust_fax').value 		= update[3];
			document.getElementById('cust_pobox').value 	= update[4];
			document.getElementById('cust_email').value 	= update[5];
			document.getElementById('cust_city').value 		= update[6];
			document.getElementById('inv_customer').value 	= update[7];
	
	    }
	}
}


/****************************************************
* Customer 
*****************************************************/
// make asynchronous HTTP request using the XMLHttpRequest object
// to get the customer infomation 
function ajaxInvoice() { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}

  	var inv_id = document.getElementById("inv_id").value;
	
	if(inv_id == "" || inv_id == 0) {
		alert("Please select a company name");
		document.getElementById("inv_id").value 		= "";
		document.getElementById("inv_customer").value 	= "";
		document.getElementById("inv_discount").value 	= "";
		document.getElementById("inv_total").value 		= "";
		document.getElementById("inv_advance").value 	= "";
		document.getElementById("inv_balance").value 	= "";
		document.getElementById("inv_date").value 		= "";
		return false;
	}// proceed only if the xmlHttp object isn't busy 
	else {
		var url="./ajax/AjaxInvoice.php?inv_id=" + inv_id;
		
		xmlHttp.onreadystatechange = invoiceServerResponse;
		xmlHttp.open("GET",url,true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xmlHttp.send(null);
	}
}

function invoiceServerResponse() { 
	if (xmlHttp.readyState==4)	{ 
		var response =  xmlHttp.responseText;
		var update = new Array();
		
	    if(response.indexOf('|') != -1) {
	            
	    	update = response.split('|');

	        // update the client display using the data received from the server 
			document.getElementById('inv_id').value 		= update[0];
			document.getElementById('cust_id').value 		= update[1];
			document.getElementById('inv_customer').value 	= update[2];
			document.getElementById('inv_discount').value	= update[3];
			document.getElementById('inv_total').value 		= update[4];
			document.getElementById('inv_advance').value 	= update[5];
			document.getElementById('inv_balance').value 	= update[6];
			document.getElementById('inv_date').value 		= update[7];
	
	    }
	}
}