var found = "<h2>Match Found.</h2> <p>Based on the property information entered, it appears Fannie Mae owns a loan at this address.</p><p>A &quot;Match Found&quot; status does not guarantee or imply that you will qualify for a Making Home Affordable refinance or modification.</p><p>If you're interested in a refinance, please contact your mortgage lender or servicer (the organization to whom you make your monthly mortgage payments) to confirm these results and ask about the Home Affordable Refinance Plus program.</p> <p>You can find more information at <a href='http://www.makinghomeaffordable.gov/' onclick='return openPopup(this.href);'>MakingHomeAffordable.gov</a>.</p> <p>View <a href='loanlookupfaq.pdf' onclick='return openPopup(this.href);'>Frequently Asked Questions</a> for this Loan Lookup tool.</p><p>Thank you for contacting Fannie Mae.</p>";
var notfound = "<h2>No Match Found.</h2><p>Based on the property information you entered, it appears Fannie Mae does not own a loan at this address.</p><p>Please contact your mortgage lender or servicer (the organization to whom you make your monthly mortgage payments) to confirm these results and obtain additional information regarding your mortgage.  Information that does not match our records exactly may return inaccurate results.  Your loan may be held by Freddie Mac or another investor eligible for participation in the Making Home Affordable program.</p><p>Please visit <a href='http://www.makinghomeaffordable.gov/' onclick='return openPopup(this.href);'>MakingHomeAffordable.gov</a> for additional information and next steps.</p><p>View <a href='loanlookupfaq.pdf' onclick='return openPopup(this.href);'>Frequently Asked Questions</a> for this Loan Lookup tool.</p><p>Thank you for contacting Fannie Mae.</p>";
var fmrequest = null;

var thefield = "";
var isError = 0;

function check_field(thefield) {
       
	var thelabel = thefield + "label";
	if (document.getElementById(thefield).value == "") {
		document.getElementById(thefield).className = "FormError";
		document.getElementById(thelabel).className = "FormErrorLabel";
		document.getElementById(thefield).focus();
		isError = 1;
	} else {
		document.getElementById(thefield).className = "FormFocus";
		document.getElementById(thelabel).className = "FormLabel";
	}
}

function validate() {
	document.getElementById('TheAnswer').innerHTML = "";
	document.getElementById('results').className = "";
	document.getElementById('results').style.display = "";
	document.getElementById('TheAnswer').style.background = "#fff";
	document.getElementById('results').style.background = "#fff";
	check_field("zip");
	check_field("streetAddr");
	var message = "";
	if (isError == 1) {
		message = "To use the Fannie Mae Loan Lookup tool at a minimum you must enter a valid Street Address and a valid Zip Code";
	}
	var checkedbox = document.getElementById("confirm").checked;
	if (checkedbox == true) {
		document.getElementById("confirm").value="confirm";
		document.getElementById("confirm").className = "";
		document.getElementById("confirmlabel").className = "FormLabel";
	} else {
		document.getElementById("confirm").value="";
		document.getElementById("confirm").className = "FormError";
		document.getElementById("confirmlabel").className = "FormErrorLabel";
		document.getElementById("confirm").focus();
		//If zip and street validation already failed
		if(isError == 1){
			message = message
			+ ", and you must be the owner of the property or have the consent of the owner.  If you are the owner or you have the owner's consent, please indicate this by checking the box";
		}else{
			//No other errors than confirmation box
			message = message
			+ "To use the Fannie Mae Loan Lookup tool you must be the owner of the property or have the consent of the owner. If you are the owner or have the owner's consent, please indicate this by checking the box";
		}
		isError = 1;
	}
	if (isError == 1) {
		alert(message + ".");
	}
	if (isError == 0) {
		callSearchService();
	}
	isError = 0;
}

// create the request
function createRequest() {

	try {
		fmrequest = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			fmrequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				fmrequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				fmrequest = null;
			}
		}
	}
	if (fmrequest == null)
		alert("Error creating the request!");
}

function newXMLHttpRequest() {
	var xmlreq = false;
	if (window.XMLHttpRequest) {
		// non-Microsoft browsers
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			// later versions of Internet Explorer
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				// older versions of Internet Explorer
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				// Unable to create
			}
		}
	}
	return xmlreq;
}

function callSearchService() {
	var req = newXMLHttpRequest();
	var handlerFunction = getReadyStateHandler(req, processOutput);
	// Callback
	req.onreadystatechange = handlerFunction;
	 req.open("POST", "searchService", true);

	// Specify that the body of the request contains form data
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	// Loanlookup data
	req.send("streetAddress=" + document.getElementById("streetAddr").value
			+ "&unit=" + document.getElementById("unit").value + "&state="
			+ document.getElementById("stateDD").value + "&city="
			+ document.getElementById("city").value + "&zip="
			+ document.getElementById("zip").value + "&confirm="
			+ document.getElementById("confirm").value
			+ "&recaptcha_response_field="
			+ document.getElementById("recaptcha_response_field").value
			+ "&recaptcha_challenge_field="
			+ document.getElementById("recaptcha_challenge_field").value);
}

function getReadyStateHandler(req, responseXmlHandler) {
	// Return an anonymous function that listens to the
	// XMLHttpRequest instance
	return function() {
		// complete
		if (req.readyState == 4) {
			// successful server response
			if (req.status == 200) {
				responseXmlHandler(req.responseXML);
			} else {
				// An HTTP problem has occurred
				alert("Error Occurred. Please try again. The Loan Lookup Tool may not be available Sunday from 3am-6am EST for scheduled maintenance.");
			}
		}
	}
}

function processOutput(searchStatusXML) {
	var result = searchStatusXML.getElementsByTagName("RESULT")[0];
	var status = result.getElementsByTagName("STATUS")[0].childNodes[0].nodeValue;
	//document.getElementById('toplabel').innerHTML = "Search Results";

	var city = null;
	var cityObject = result.getElementsByTagName("CITY")[0];
	if (cityObject != null) {
		var cityElement = cityObject.childNodes[0];
		if(cityElement != null){
			city = cityElement.nodeValue;
			document.getElementById("city").value = city;
		}	
	}
	var stateObject = result.getElementsByTagName("STATE")[0];
	var state = null;
	if (stateObject != null){
		var stateElement = stateObject.childNodes[0];
		if(stateElement != null){
			state = stateElement.nodeValue;
		}
	}
	if (state != null) {
		var stateSelect = document.getElementById('stateDD');
		for ( var i = 0; i < stateSelect.options.length; i++) {
			if (stateSelect.options[i].value == state) {
				stateSelect.options[i].selected = true;
				break
			}
		}
	}

	var city = null;
	var cityObject = result.getElementsByTagName("ZIP")[0];
	if (cityObject != null) {
		var cityElement = cityObject.childNodes[0];
		if(cityElement != null){
			city = cityElement.nodeValue;
			document.getElementById("zip").value = city;
		}	
	}
	
	if (status == "NOT_FOUND") {
		document.getElementById('TheAnswer').innerHTML = notfound;
		document.getElementById('results').className = "loanNo";
		document.getElementById('results').style.display = "";
		document.getElementById('TheAnswer').style.background = "#ccccff";
		document.getElementById('results').style.background = "#ccccff";
		pageTracker._trackPageview('NOT_FOUND'); 
	}
	if (status == "FOUND") {
		document.getElementById('TheAnswer').innerHTML = found;
		document.getElementById('results').className = "loanYes";
		document.getElementById('results').style.display = "";
		document.getElementById('TheAnswer').style.background = "#cfedff";
		document.getElementById('results').style.background = "#cfedff";
		pageTracker._trackPageview('FOUND'); 
	}
	if (status == "CAPTCHA_ERROR") {
		alert("The security code entered does not match. Please make sure that you enter both security words shown in the box and try again.");
		pageTracker._trackPageview('CAPTCHA_ERROR'); 
	}
	if (status == "VALIDATION_ERROR") {
		var message = "To use the Fannie Mae Loan Lookup tool at a minimum you must enter a valid Street Address and a valid Zip Code.";
		alert(message);
		pageTracker._trackPageview('VALIDATION_ERROR'); 
	}
	if (status == "NO_CONFIRMATION") {
		var message = "To use the Fannie Mae Loan Lookup tool you must be the owner of the property or have the consent of the owner. If you are the owner or have the owner's consent, please indicate this by checking the box.";
		alert(message);
		pageTracker._trackPageview('NO_CONFIRMATION'); 
	}
	if (status == "ERROR") {
		alert("Error Occurred. Please try again. the Loan Lookup Tool may not be available Sunday from 3am-6am EST for scheduled maintenance.  ");
		pageTracker._trackPageview('ERROR'); 
	}
	Recaptcha.destroy();
	Recaptcha.create("6Ld9awUAAAAAAJrG6QQ1iwA26qy_0yfIRiRrUZsp", 'recapt1', {
		theme :'white',
		tabindex :0,
		callback :Recaptcha.focus_response_field
	});
	document.getElementById('confirm').checked = false;
}

function openPopup(url) {
	newwindow = window.open(url, "loanlookuppopup");
	newwindow.focus();
	return false;
}

