var acct = null;
var acctXML = null;

function emailAddressChanged(inpElem) {
	try {
		var lastElem = document.getElementById("lastEmailAddress");
		if (lastElem.value != inpElem.value) {
			document.getElementById("infoTr").style.display = "none";
			document.getElementById("passwordTr").style.display = "none";
			if (inpElem.value.indexOf("@") > 0) {
				window.status = "Searching...";
				url = "/cfapps/Internet/account/account.cfc?method=getAccountXML&emailAddress=" + inpElem.value;
				System.sendAsyncRequest(url, null, accountCallback);
			}
			lastElem.value = inpElem.value;
		}
	}
	catch(e) {
		return;
	}
}

function accountCallback(req, url) {
	window.status = "Done";
	
	try {
		elemArray = req.responseXML.getElementsByTagName("account_id");
		if (elemArray.length > 0) {
			acct = parseInt(elemArray[0].firstChild.data);
			if (!isNaN(acct)) {
				acctXML = req.responseXML;
				document.getElementById("infoTr").style.display = "block";
				document.getElementById("passwordTr").style.display = "block";
				document.getElementById("password").focus();
				document.getElementById("submitButton").disabled = true;
				// redraw content area
				setPageWidth();
			}
		}
		else {
			acct = null;
		}
	}
	catch(e) {}
}

function getAccountInfo() {
	try {
		if (!isNaN(acct)) {
			window.status = "Searching...";
			url = "/cfapps/Internet/account/account.cfc?method=authenticateRemote&account_id=" + acct + "&password=" + document.getElementById("password").value;
			System.sendAsyncRequest(url, null, authenticateCallback);
		}
	}
	catch(e) {}
}

function authenticateCallback(req, url) {
	window.status = "Done";
	
	try {
		elemArray = req.responseXML.getElementsByTagName("authenticated");
		if (elemArray.length > 0) {
			var auth = elemArray[0].firstChild.data;
			if (auth == "true") {
				document.getElementById("infoTr").style.display = "none";
				document.getElementById("passwordTr").style.display = "none";
				document.getElementById("submitButton").disabled = false;
				document.getElementById("createAccountDiv").style.display = "none";
				document.getElementById("createAccount").checked = false;
				importData();
			}
			else {
				alert("The password you supplied is incorrect.");
			}
		}
	}
	catch(e) {
		document.getElementById("submitButton").disabled = false;
	}
}

function importData() {
	var firstName, lastName;
	
	try {
		firstName = acctXML.getElementsByTagName("first_name")[0].firstChild.data;
		lastName = acctXML.getElementsByTagName("last_name")[0].firstChild.data;
		document.getElementById("personName").value = firstName + " " + lastName;
		
		document.getElementById("address").value = acctXML.getElementsByTagName("residence_street_address")[0].firstChild.data;
		document.getElementById("city").value = acctXML.getElementsByTagName("residence_city")[0].firstChild.data;
		document.getElementById("state").value = acctXML.getElementsByTagName("residence_state")[0].firstChild.data;
		document.getElementById("zipCode").value = acctXML.getElementsByTagName("residence_zipcode")[0].firstChild.data;
		// redraw content area
		setPageWidth();
		
		document.getElementById("comments").focus();
	}
	catch(e) {}
}

function checkForm() {
	var comments = document.getElementById("comments");
	if (comments.value.trim() == "") {
		alert("Please include your comments.");
		comments.focus();
		return false;
	}
	
	// if checkboxes for CM and CA, make sure at least one is checked
	var obj = document.getElementById("emailCM");
	if (obj.type.toLowerCase() == "checkbox") {
		if (!obj.checked) {
			if (!document.getElementById("emailCA").checked) {
				alert("Please select at least one recipient for your comments.");
				return false;
			}
		}
	}
	
	return true;
}

document.getElementById("emailAddress").focus();
