var xmlHttp;
function setHttpRequest() {

	try	{    // Firefox, Opera 8.0+, Safari    
		xmlHttp=new XMLHttpRequest();    
	} catch (e) {    // Internet Explorer    
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
			} catch (e) {
				alert("Your browser does not support AJAX!");
		        return null;        
		    }      
		}    
	}  
}
function postWaitMessage() {
	var hintdiv = document.getElementById("hintdiv");
	hintdiv.innerHTML = '<span style="font-size: 10pt;">Please wait while we gather your account information... <img src="/images/ajax-loader.gif"></span>';
}

function verifyAccount() {
	setHttpRequest(); //-- sets the proper XMLHttpRequest objext
    xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {
			//alert(xmlHttp.responseText);
 			var xmldoc = xmlHttp.responseXML;

 			var status = xmldoc.getElementsByTagName('account')[0].getAttribute('status');
 			
			var unknown = document.getElementById("unknown");
			var registration = document.getElementById("registration");
			var existing = document.getElementById("existing");
			var acctmsg = document.getElementById("acctmsg");
			
			if (status == "A") {
				
				existing.style.display = '';
				registration.style.display = 'none';
				unknown.style.display = 'none';
 				acctmsg.style.display = 'none';
				document.account_change.account.value = document.regform.account.value;
				//var hint = xmldoc.getElementsByTagName('hint')[0].firstChild.nodeValue;
 				//var acctdiv = document.getElementById("acctmsg");
 				//acctdiv.innerHTML = '<span style=\"color: red;\">This account is already registered. Only one registration per account number is allowed.</span><br><b>Did you forget your password?</b><br>Fill in your account number in the Login "Account Number" field (above), click "Forgot Your Password?" and follow the instructions for password recovery. If the email address shown is incorrect, proceed with registration (below) and someone will contact you to verify your update request.<br><b>Need to update registration information?</b><br>If you would like to update the email address and password on the registration for this account, continue to register using the form below and someone will contact you to verify your update request.';
			} 
 			else if (status == "V") { // valid prelude account not yet registered
 				//var hint = xmldoc.getElementsByTagName('hint')[0].firstChild.nodeValue;
 				
				existing.style.display = 'none';
				registration.style.display = '';
				document.newregform.account.value = document.regform.account.value;
				unknown.style.display = 'none';
				acctmsg.style.display = 'none';
				//var acctdiv = document.getElementById("acctmsg");
 				//acctdiv.innerHTML = 'This account is already registered. Here is the password hint : <SPAN CLASS="red2">' + hint + '</span>';
			} 
			else if (status == "N") {
 				 				
				existing.style.display = 'none';
				registration.style.display = 'none';
				unknown.style.display = 'none';
				acctmsg.style.display = '';
			} else {
  				//var acctdiv = document.getElementById("acctmsg");
				
				existing.style.display = 'none';
				registration.style.display = 'none';
				unknown.style.display = '';
				acctmsg.style.display = 'none';
				//acctdiv.innerHTML = '<SPAN CLASS="red2">Unrecognized Account Number. Please continue registration below.';
			}
    	}
    }
    
    var url="/ap/services?option=verifyaccount&account=" + document.regform.account.value;
  //  alert(url);
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);	
}
function verifyReg() {
	var check = true;
	if (document.regform.aname.value == "") {
		document.regform.aname.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.aname.style.backgroundColor = "#ffffff";
	}
	if (document.regform.add1.value == "") {
		document.regform.add1.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.add1.style.backgroundColor = "#ffffff";
	}
	if (document.regform.city.value == "") {
		document.regform.city.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.city.style.backgroundColor = "#ffffff";
	}
	if (document.regform.postalcode.value == "") {
		document.regform.postalcode.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.postalcode.style.backgroundColor = "#ffffff";
	}
	if (document.regform.state.value == "") {
		document.regform.state.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.state.style.backgroundColor = "#ffffff";
	}
	if (document.regform.phone.value == "") {
		document.regform.phone.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.phone.style.backgroundColor = "#ffffff";
	}
	if (document.regform.fname.value == "" || document.regform.lname.value == "" || document.regform.salute.value == "") {
		document.regform.fname.style.backgroundColor = "yellow";
		document.regform.lname.style.backgroundColor = "yellow";
		document.regform.salute.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.fname.style.backgroundColor = "#ffffff";
		document.regform.lname.style.backgroundColor = "#ffffff";
		document.regform.salute.style.backgroundColor = "#ffffff";
	}
	if (document.regform.email.value == "" || document.regform.email.value != document.regform.emailcfm.value) {
		document.regform.email.style.backgroundColor = "yellow";
		document.regform.emailcfm.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.email.style.backgroundColor = "#ffffff";
		document.regform.emailcfm.style.backgroundColor = "#ffffff";
	}
	if (document.regform.password.value == ""|| document.regform.password.value != document.regform.passwordcfm.value) {
		document.regform.password.style.backgroundColor = "yellow";
		document.regform.passwordcfm.style.backgroundColor = "yellow";
		check = false;
	} else {
		document.regform.password.style.backgroundColor = "#ffffff";
		document.regform.passwordcfm.style.backgroundColor = "#ffffff";
	}
	if (check == false) {
		 document.getElementById("regmsg").innerHTML = "Fields highlighted in yellow are required.";
	}
	return check;
}
function displayCustomer() { 
	setHttpRequest(); //-- sets the proper XMLHttpRequest objext
    xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {
 			var xmldoc = xmlHttp.responseXML;
//			alert(xmlHttp.responseText);
 			var custs = xmldoc.getElementsByTagName("Customer");
			for (var i=0; i < custs.length; i++) {
				var cust =  xmldoc.getElementsByTagName('Customer')[i];
				if (document.orderform.customers[document.orderform.customers.selectedIndex].value == cust.getAttribute('custnum')) {
					var div = document.getElementById("shipadd");
					var shipadd = "<b>Ship Address:</b><br>";
					shipadd += cust.getElementsByTagName("AccountName")[0].firstChild.nodeValue + "<br>";
					shipadd += cust.getElementsByTagName("Add1")[0].firstChild.nodeValue + "<br>";
//					shipadd += cust.getElementsByTagName("Add2")[0].firstChild.nodeValue + "<br>";
					shipadd += cust.getElementsByTagName("City")[0].firstChild.nodeValue + "&nbsp;&nbsp;";
					shipadd += cust.getElementsByTagName("State")[0].firstChild.nodeValue + "&nbsp;&nbsp;";
					shipadd += cust.getElementsByTagName("PostalCode")[0].firstChild.nodeValue + "&nbsp;&nbsp;";
					div.innerHTML = shipadd;
		 			var contacts = xmldoc.getElementsByTagName("Customer")[i].getElementsByTagName("Contact");
					for (var j=0; j < contacts.length; j++) {
 						var contact = contacts[j].getElementsByTagName("ContactName")[0].firstChild.nodeValue;
			 			var id = contacts[j].getAttribute("id");
						document.orderform.contacts.options[document.orderform.contacts.options.length] = new Option(contact,id);
					}
				}
			}
    	}
    }
    
    var url="/ap/services?option=displaycustomer";
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);	
}
function setAttentionView()  {
	var attndiv = document.getElementById("shipattn");
	attndiv.innerHTML = "<b>Attention</b> : " +  document.orderform.contacts[document.orderform.contacts.selectedIndex].text + "<br>";
	document.getElementById("ordheaddiv").innerHTML = "Order Summary for " + document.orderform.contacts[document.orderform.contacts.selectedIndex].text;
}
function checkLoginPassword() {
    if (document.loginform.password.value == "") {
		document.getElementById("hintdiv").innerHTML = "Please fill in password.";
		return false;
	} else {
		return true;
	}
}
function openPasswordForm() {
	setHttpRequest(); //-- sets the proper XMLHttpRequest objext
    xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {
 			var xmldoc = xmlHttp.responseXML;
// 			alert(xmlHttp.responseText);
 			var status = xmldoc.getElementsByTagName('account')[0].getAttribute('status');
 			if (status == "X") {
 				//-- handle invalid
			} else {
 				var email = xmldoc.getElementsByTagName('email')[0].firstChild.nodeValue;
 				var emaildiv = document.getElementById("emaildiv");
 				emaildiv.innerHTML = email;
			}
    	}
    }
    
    var url="/ap/services?option=passwordform&account=" + document.loginform.account.value;
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);	
}

function sendPassword() {
	setHttpRequest(); //-- sets the proper XMLHttpRequest objext
    xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {
			var xmldoc = xmlHttp.responseXML;
// 			alert(xmlHttp.responseText);
 			var status = xmldoc.getElementsByTagName('account')[0].getAttribute('status');
 			if (status == "true") {
	 			overlayclose('subpass');
			} else {
				// handle unsuccessfull 
			}
    	}
    }
    
    var url="/ap/services?option=sendpassword&account=" + document.loginform.account.value;
//    alert(url);
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);	
}

function checkPasswords() {
	if (document.changepasswordform.cfmpwd.value != document.changepasswordform.newpwd.value) {
		alert("New passwords do not match, please try again.");
		return false;
	}
}

function sendAddressChange() {
	setHttpRequest(); //-- sets the proper XMLHttpRequest objext
	var url="/ap/services?option=changeaddress&text=" + document.addressform.text.value;
	xmlHttp.open("POST",url,true);
	xmlHttp.onreadystatechange=function() {
	  	if(xmlHttp.readyState==4) {
//			alert(xmlHttp.responseText);
			var xmldoc = xmlHttp.responseXML;
			var addformdiv = document.getElementById("addformdiv");
			if (document.getElementById && document.createElement) {
				clearElement(addformdiv);
				addformdiv.appendChild(document.createTextNode("The address change below has been successfully submitted. Please allow 24 hours for the changes to occur."));
				addformdiv.appendChild(document.createElement('br'));
				addformdiv.appendChild(document.createTextNode(xmldoc.getElementsByTagName("response")[0].firstChild.nodeValue));
			}
//			addformdiv.innerHTML = text;
		}
	}
	xmlHttp.send(null);	
	overlayclose('subcontent');
	return false;
}

function clearElement(e) {
	if (e.hasChildNodes()) {
		while (e.childNodes.length > 0) {
			e.removeChild(e.firstChild);
		}
	}
}

function sortInvoices() {
	var add = "";
	for (var i=0; i < document.sortform.sort.options.length; i++) {
		if (document.sortform.sort.options[i].selected) {
			add = document.sortform.sort.options[i].value;
		}
	}
	document.sortform.action = add;
	document.sortform.submit();
}