// JavaScript Document
function searchRates(cid) {
	if (verifyValues()) {
		var loan = getLoanData();
		var vals = document.getElementsByName('occupancy');
		for (i=0; i<vals.length; i++) {
			if (vals[i].checked) {
				var occval = vals[i].value;
				break;
			}
		}
		vals = document.getElementsByName('purpose');
		for (i=0; i<vals.length; i++) {
			if (vals[i].checked) {
				var purpval = vals[i].value;
				break;
			}
		}
		streamLine = 0;
		if (purpval == '2') { //check to see what type of refi we have selected
			purpval = document.getElementById('refi_purpose').value;
			if (purpval == '6') {
				streamLine = 1;
				purpval = 5;
			}
		}
		
		var params = "";
		
		params += "&a1=1,3,13";
		params += "&a2=30";
		params += "&a3=3";
		params += "&a4=1";
		params += "&a5=1.0";
		params += "&a6=1";
		params += "&a7=0";
		params += "&a8=1";
		params += "&a9=Yes";
		params += "&a10=Yes";
		params += "&a11=No";
		params += "&a12=No";
		params += "&a13=No";
		params += "&a14=0";
		params += "&a15=0";
		params += "&a16="+document.getElementById('bfico').value;
		params += "&a17="+document.getElementById('blastname').value;
		params += "&a18="+loan[0]; //app or sale val.
		params += "&a19="+loan[1]; //loan val
		params += "&a20="+document.getElementById('saddress').value;
		params += "&a21="+document.getElementById('scity').value;
		params += "&a22="+document.getElementById('sstate').value;
		params += "&a23="+document.getElementById('szip').value;
		params += "&a24="+document.getElementById('scounty').value;
		params += "&a25="+occval;
		params += "&a26="+purpval;
		params += "&a27="+numericReal(document.getElementById('totalincome').value);
		params += "&a28="+numericReal(document.getElementById('housingpmt').value);
		params += "&a29="+numericReal(document.getElementById('otherpmt').value);
		params += "&a30="+streamLine;
	
		var xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
		 alert ("You need to be using a Mozilla Firefox browser or at least IE 7.0");
		 return;
		}
		xmlHttp.open("POST",'/lvl1/support/program_request.php',true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = function() { stateChanged(xmlHttp); };	
		xmlHttp.send(params);
		
		document.getElementById('rates_div').innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td class="header_bg_2"><img src="/images/springwater.gif" width="1" height="1" /></td></tr><tr><td><img src="/images/springwater.gif" width="1" height="2" /></td></tr><tr><td class="background_yellow"><img src="/images/springwater.gif" width="1" height="1" /></td></tr><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td><img src="/images/springwater.gif" alt="" width="1" height="20"></td></tr><tr><td class="process_header_1" align="center">Please wait while we communicate the search request...</td></tr><tr><td><img src="/images/springwater.gif" alt="" width="1" height="20"></td></tr><tr><td align="center"><img src="/images/loading.gif" alt="mConnects" width="188" height="188"></td></tr><tr><td><img src="/images/springwater.gif" alt="" width="1" height="20"></td></tr></table></td></tr><tr><td class="background_gray"><img src="/images/springwater.gif" width="1" height="1" /></td></tr><tr><td><img src="/images/springwater.gif" width="1" height="2" /></td></tr><tr><td class="proc_footer"><img src="/images/springwater.gif" width="1" height="1" /></td></tr></table>';
//		setScroll('large_'+cid);
	
	}
}

function getLoanData() {
	var result = Array();
	if (document.getElementById("purpose_1").checked) { //Purchase		
		result[0] = numericReal(document.getElementById('saleval').value);
		result[1] = numericReal(document.getElementById('loanval_p').value);
	
	} else if (document.getElementById("purpose_2").checked) {
		result[0] = numericReal(document.getElementById('appval').value);
		result[1] = numericReal(document.getElementById('loanval_r').value);
	}
	return result;
}

function tryAgain(cid) {
	objDIV = document.getElementById('rates_div');//Write the resutls to objDIV
	top.loadPage(objDIV,'/images/springwater.gif');
}
function expandFailures(obj) {
	if (obj.className == 'expand_off') {
		obj.className = 'expand_on';
		document.getElementById('failed_loans').className = 'show';
	} else {
		obj.className = 'expand_off';
		document.getElementById('failed_loans').className = 'hide';
	}
}

var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
function InitializeTimer(oldysp) {
	// Set the length of the timer, in seconds
	secs = 2;
	StopTheClock();
	StartTheTimer(oldysp);	
}

function StopTheClock() {
	if(timerRunning) clearTimeout(timerID);
	timerRunning = false;
}
function StartTheTimer(oldysp) {
	if (secs==0) {
		StopTheClock();		
		objDIV = document.getElementById('rates_div');//Write the resutls to objDIV	
		loadPage(objDIV,'/lvl1/support/program_display.php','&oldysp='+oldysp);
		document.getElementById('signup').className = 'show';

	} else {
		self.status = secs;
		secs = secs - 1;
		timerRunning = true;
		timerID = self.setTimeout("StartTheTimer("+oldysp+")", delay);
	}
}

function updateLoanApp() {
	checks = document.getElementsByName('options');
	checked = false;
	for (i=0; i<checks.length; i++) {
		if (checks[i].checked) {
			checked = true;
			val = checks[i].value;
			rate = numericOnly(document.getElementById('rate_'+val).firstChild.nodeValue);
			pmt = numericOnly(document.getElementById('pmt_'+val).firstChild.nodeValue);
			product = trim(document.getElementById('product_name').firstChild.nodeValue);
			lenderid = trim(document.getElementById('lenderid').value);
			productid = trim(document.getElementById('programid').value);
			term = trim(document.getElementById('term').firstChild.nodeValue);
			io = trim(document.getElementById('interest_only').firstChild.nodeValue);
			lockds = numericOnly(document.getElementById('lockdays').firstChild.nodeValue);
			price = numericOnly(document.getElementById('price_'+val).firstChild.nodeValue);
			type = trim(document.getElementById('loan_type').firstChild.nodeValue);
		}
	}
	if (checked)  {
		conid = document.getElementById('cid').value;
		if (type == 'ARM') type = 'arm';
		if (type == 'Fixed') type = 'fixed';
		
		parameters = "&aa=10"; //tablename
		parameters += "&a0=1";
		parameters += "&a1=01="+conid;
		parameters += "&02="+rate;
		parameters += "&03="+term;
		parameters += "&04="+product;
		parameters += "&05="+lenderid;
		parameters += "&06="+productid;
		parameters += "&07="+pmt;
		if (io == 'Yes') parameters += "&08=60";
		else parameters += "&08=";
		parameters += "&09="+lockds;
		parameters += "&10="+price;
		parameters += "&11="+type; //ARM or FIXED
		if (type == 'arm') parameters += "&12="+product;
		top.postGeneral(parameters,conid);
		
		if (type == 'arm') {
			margin = numericOnly(document.getElementById('margin').firstChild.nodeValue);
			lifecap = numericOnly(document.getElementById('life_cap').firstChild.nodeValue);
			fadj = trim(document.getElementById('firstadj').firstChild.nodeValue).split("/");
			padj = trim(document.getElementById('padj').firstChild.nodeValue).split("/");
	
			parameters = "&aa=11"; //tablename
			parameters += "&a0=1";
			parameters += "&a1=01="+conid;
			parameters += "&07="+margin;
			parameters += "&06="+lifecap;
			parameters += "&02="+fadj[1];
			parameters += "&03="+fadj[0];
			parameters += "&04="+padj[1];
			parameters += "&05="+padj[0];
			
			top.postGeneral(parameters,conid);
		} else {
			parameters = "&aa=11"; //tablename
			parameters += "&a0=1";
			parameters += "&a1=01="+conid;
			parameters += "&07=";
			parameters += "&06=";
			parameters += "&02=";
			parameters += "&03=";
			parameters += "&04=";
			parameters += "&05=";
			
			top.postGeneral(parameters,conid);
		}		
		//refresh the Loan Application
//		conid = window.open('http://secure.mconnects.contrastex.com/loan_app/index.php?cid='+conid, conid);
//		conid.location.reload();
//		self.parent.parent.parent.frames['menu'].conid.frames['bodyframe'].document.GetElementById('mainFrame').src = '/loan_app/1003/summary.php?cid='+conid;
		//Write the rate to the Loan App if it is open in a tab...
		//																							//
		//																							//
		
		//Write the lender name to the Pipeline
		lennm = trim(document.getElementById('lender_name').firstChild.nodeValue);
		appData = parent.document.getElementById('lender_'+conid);
		appData.className = "active_link";
		appData.onclick = function () { parent.openLender(this,lenderid); };
		appData.firstChild.nodeValue = lennm;
		parent.document.getElementById('lenderid_'+conid).value = lenderid;
		parent.document.getElementById('lendersubtype_'+conid).value = document.getElementById('lendersubtype').value;
		
		//Check to see if there is a nextSibling, if there is, remove it...
		if (appData.nextSibling)
			appData.parentNode.removeChild(appData.nextSibling);
		
		//Write the interest rate to the Pipeline
		rateData = parent.document.getElementById('rate_'+conid);
		rateData.innerHTML = rate;
		
		parent.details.close();
		
	} else {
		alert('Please select an Option before saving to the Loan Application.');
	}
}
function printPrequal(cid) {
	checks = document.getElementsByName('options');
	checked = false;
	for (i=0; i<checks.length; i++) {
		if (checks[i].checked) {
			checked = true;
			val = checks[i].value;
			rate = numericOnly(document.getElementById('rate_'+val).firstChild.nodeValue);
			pmt = numericOnly(document.getElementById('pmt_'+val).firstChild.nodeValue);
			product = trim(document.getElementById('product_name').firstChild.nodeValue);
			lenderid = trim(document.getElementById('lenderid').value);
			productid = trim(document.getElementById('programid').value);
			term = trim(document.getElementById('term').firstChild.nodeValue);
			io = trim(document.getElementById('interest_only').firstChild.nodeValue);
			lockds = numericOnly(document.getElementById('lockdays').firstChild.nodeValue);
			price = numericOnly(document.getElementById('price_'+val).firstChild.nodeValue);
			type = trim(document.getElementById('loan_type').firstChild.nodeValue);
		}
	}
	if (checked)  {
		params = '';
		if (type == 'ARM') {
			margin = numericOnly(document.getElementById('margin').firstChild.nodeValue);
			lifecap = numericOnly(document.getElementById('life_cap').firstChild.nodeValue);
			lennm = trim(document.getElementById('lender_name').firstChild.nodeValue);
			
			params = '?cid='+cid+'&rate='+rate+'&term='+term+'&lenid='+lenderid+'&pid='+productid+'&prod='+product+'&ptype='+type+'&io='+io+'&price='+price+'&lock='+lockds+'&marg='+margin+'&lcap='+lifecap+'&lennm='+lennm;
		} else {
			params = '?cid='+cid+'&rate='+rate+'&term='+term+'&lenid='+lenderid+'&pid='+productid+'&prod='+product+'&ptype='+type+'&io='+io+'&price='+price+'&lock='+lockds;
		}		
		document.getElementById('prequal').src = '/docs/support/pipeline/programs/prequal_results.php'+params;
		updateLoanApp();
	} else {
		alert('Please select an Option before printing the Pre-Qualification.');
	}
}
function trim(string) {
	return string.replace(/^\s+|\s+$/g,"");
}
function numericOnly(str) {
	var strValidChars = "0123456789.";
  var strChar;
  var numString = '';
	if (str.length == 0) return false;	
	for (i = 0; i < str.length; i++) {
		strChar = str.charAt(i);
		if (strValidChars.indexOf(strChar) > -1) {
			numString = numString +''+strChar;
		}
	}
	return numString*1;
}