// JavaScript Document
var xhr = false;

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ajaxObject(){
  var xhr;
	try{xhr=new XMLHttpRequest();}	// Firefox, Opera 8.0+, Safari  
	catch (e){	// Internet Explorer
		try{xhr=new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e){	// IE 7 on Vista
			try{xhr=new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){        
				alert("Asynchronis Data Transfer unavailable, please use Internet Explorer!");        
				return false;
			}
		}
	}
	return xhr;
}

function showSubCat(a){
	var dtToday = new Date();
	var theLabel = document.getElementById('subTypeLabel');
	theLabel.innerHTML='Please wait:';
	theLabel.style.display='block';
	var theDiv = document.getElementById('subType');
	theDiv.innerHTML='<img src="images/ajax-loader.gif"> Loading...';
	theDiv.style.display='block';
	if (a == ''){
		theLabel.style.display='none';
		theDiv.style.display='none';
		return true;
	}
	var url = 'async/showSubCat.cfm?type='+a+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					if (a == 'Employee') {
						theLabel.innerHTML='Company:';
					}
					else {
						theLabel.innerHTML=a+':';
					}
					theDiv.innerHTML=xhr.responseText;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function showEntities(a){
	document.getElementById('reportsToLabel').innerHTML='';
	document.getElementById('reportsToSelect').innerHTML='';
	var dtToday = new Date();
	var theLabel = document.getElementById('subTypeLabel');
	theLabel.innerHTML='Please wait:';
	var theDiv = document.getElementById('subType');
	theDiv.innerHTML='<img src="images/ajax-loader.gif"> Loading...';
	if (a == ''){
		return true;
	}
	var url = 'async/showEntities.cfm?type='+a+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getSeconds() + dtToday.getMilliseconds();
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					if (a == 'Employee') {
						theLabel.innerHTML='Company:';
					}
					else {
						theLabel.innerHTML=a+':';
					}
					theDiv.innerHTML=xhr.responseText;
					xhr.close;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function showCust(a){
	if (a.length <= 2) {
		return true;
	}
	var dtToday = new Date();
	var theDiv = document.getElementById('custList');
	theDiv.innerHTML='<img src="images/ajax-loader.gif"> Loading...';
	theDiv.style.display='block';
	if (a == ''){
		theDiv.style.display='none';
		return true;
	}
	var url = 'async/showCust.cfm?part='+a+'&aid='+dtToday.getHours()+'x'+dtToday.getMinutes()+'x'+dtToday.getSeconds()+'x'+ dtToday.getMilliseconds();
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4) {
				if (xhr.status == 200){
					theDiv.innerHTML=xhr.responseText;
					xhr.close;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
	xhr.close;
}

function showReportsTo(){
	var dtToday = new Date();
	var theLabel = document.getElementById('reportsToLabel');
	theLabel.innerHTML='Please wait:';
	theLabel.style.display='block';
	var theDiv = document.getElementById('reportsToSelect');
	theDiv.innerHTML='<img src="images/ajax-loader.gif"> Loading...';
	theDiv.style.display='block';
	var ct = document.getElementById('contactType').value;
	var cti = document.getElementById('contactTypeId').value;
	var url = 'async/showReportsTo.cfm?contactType='+ct+'&contactTypeId='+cti+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getSeconds() + dtToday.getMilliseconds();
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theLabel.innerHTML='ReportsTo:';
					theDiv.innerHTML=xhr.responseText;
					xhr.close;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function showPriContact(a){
	if (a.length >= 1){
		document.getElementById('existingPriContact').innerHTML='&nbsp;(By checking, you will replace '+a+' as the primary contact)';
	}
}

function searchFor(theValue,theFile){
	var dtToday = new Date();
	var theDiv = document.getElementById('custList');
//	var theDiv = document.getElementById(theFile);
	if (theValue.length == 0) {
		theDiv.innerHTML='<br>Please type 3 more characters.';
		return true;
	}
	if (theValue.length == 1) {
		theDiv.innerHTML='<br>Please type 2 more characters.';
		return true;
	}
	if (theValue.length == 2) {
		theDiv.innerHTML='<br>Please type 1 more character.';
		return true;
	}
	theDiv.innerHTML='<br><br><img src="images/ajax-loader.gif">';
	var url = theFile+'.cfm?searchString='+theValue+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	//theDiv.innerHTML=url;
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theDiv.innerHTML=xhr.responseText;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function editThis(theValue,theFile){
	var dtToday = new Date();
	var theDiv = document.getElementById(theFile);
	theDiv.innerHTML='<br><br><img src="images/ajax-loader.gif">';
	var url = theFile+'.cfm?theid='+theValue+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	//theDiv.innerHTML=url;
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theDiv.innerHTML=xhr.responseText;
					getProfile(theValue,0);
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function editOrder(theValue,theid){
	var dtToday = new Date();
	var theDiv = document.getElementById('custDetail');
	theDiv.innerHTML='<br><br><img src="images/ajax-loader.gif">';
	var url = 'order1Detail.cfm?theoid='+theValue+'&theid='+theid+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	//theDiv.innerHTML=url;
	//alert(url);
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theDiv.innerHTML=xhr.responseText;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function getProfile(theid,profileid){
	var dtToday = new Date();
	var theDiv = document.getElementById('profileResults');
	theDiv.innerHTML='<br><br><img src="images/ajax-loader.gif">';
	var url = 'profileResults.cfm?theid='+theid+'&pid='+profileid+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	//theDiv.innerHTML=url;
	//return true;
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theDiv.style.width='620px';
					theDiv.innerHTML=xhr.responseText;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function getPrice(productid,OrderDetailID){
	var dtToday = new Date();
	var theDiv = document.getElementById('unitprice'+OrderDetailID);
	//theDiv.innerHTML='<br><br><img src="images/ajax-loader.gif">';
	var url = 'getPrice.cfm?productid='+productid+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	//return true;
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theDiv.value=trim(xhr.responseText);
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function logit(wref,wip){
	var dtToday = new Date();
	var url = 'wanderingInsert.cfm?wref='+wref+'&wip='+wip+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					xhr.close;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function getFields(theTable){
	var dtToday = new Date();
	var theDiv = document.getElementById('fields');
	//theDiv.innerHTML='<br><br><img src="images/ajax-loader.gif">';
	var url = 'getFields.cfm?theTable='+theTable+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	//return true;
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theDiv.innerHTML=xhr.responseText;
					getFields1(theTable);
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function getFields1(theTable){
	var dtToday = new Date();
	var theDiv = document.getElementById('where');
	//theDiv.innerHTML='<br><br><img src="images/ajax-loader.gif">';
	var url = 'getFields1.cfm?theTable='+theTable+'&aid='+dtToday.getHours() + dtToday.getMinutes() + dtToday.getMilliseconds();
	//return true;
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					theDiv.innerHTML=xhr.responseText;
				}
				else{
					xhr.close;
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}
