var myclose = false;

function ConfirmClose(){		
	if (event.clientY < -1){		
		setTimeout('myclose=false',100);
		myclose=true;
	}
}

function HandleOnClose(url, width, height, name, scrollbars){
	if (myclose==true){		
		//uncomment the below 3 lines to turn the pop under on
		popWindow(url, width, height, name, scrollbars);
		popup.blur();
		window.focus();
	} 		
}




function popWindow(url, width, height, name, scrollbars){
	if (!scrollbars) scrollbars = false;
	if (!width) width = 500;
	if (!height) height = 500;
 	var realW = (width)/2;
	var realH = (height)/2;
 	if (document.all){		
        var xMax = screen.width, yMax = screen.height;
    }else if (document.layers){       
        var xMax = screen.width, yMax = screen.height;
    }else if (document.getElementById){
		var xMax = screen.width, yMax = screen.height;
	}else{
		var xMax = 800, yMax=600;
	}
	var xOffset = ((xMax)/2) - realW, yOffset = (((yMax)/2)) - realH; 
	if (yMax==600){
		yOffset = 0;
	}	
	
	if (scrollbars) {
		var options = "directories=no,status=no,location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=yes" + ",screenX=" + xOffset + ",screenY=" + yOffset + ",top=" + yOffset + ",left=" + xOffset + ",width=" + width + ",height=" + height;
	}else{
		var options = "directories=no,status=no,location=no,scrollbars=no,menubar=no,toolbar=no,resizable=no" + ",screenX=" + xOffset + ",screenY=" + yOffset + ",top=" + yOffset + ",left=" + xOffset + ",width=" + width + ",height=" + height;
	}
	
	if (!name) name = "default_window";
	
	popup = window.open(url,name,options);
	popup.focus();	
}



function moveFormCursor(ctl){		
	for(i=0; i < document.forms.length; i++){
		for(j=0; j < document.forms[i].elements.length; j++){
			if ((document.forms[i].elements[j].name == ctl.name) && (ctl.name.indexOf("AreaCode") > -1 || ctl.name.indexOf("Prefix") > -1 || ctl.name.indexOf("SSNumber_Set1") > -1 || ctl.name.indexOf("SSNumber_Set2") > -1)){
				var currentValue = ctl.value;
				var newValue = parseFloat(currentValue).toString();				
				if (ctl.name.indexOf("SSNumber_Set2") > -1){
					if (currentValue.length == newValue.length && newValue != "NaN" && currentValue.length == 2) {
						document.forms[i].elements[j+1].focus();
						break;
						break;
					}
				}else{
					if (currentValue.length == newValue.length && newValue != "NaN" && currentValue.length == 3) {
						document.forms[i].elements[j+1].focus();
						break;
						break;
					}
				}			
			}			
		}
	}
}

function testAuthorization(ctl){	
	if (document.all){
		var myElement = document.all(ctl);
	}else{
		var myElement = document.getElementById(ctl);
	}		
	for(i=0; i < document.forms.length; i++){
		for(j=0; j < document.forms[i].elements.length; j++){			
			if ((document.forms[i].elements[j].type == "radio") && (document.forms[i].elements[j].name == "rbAuthorization")){
				//alert(document.forms[i].elements[j].value);
				if ((document.forms[i].elements[j].checked == true) && (document.forms[i].elements[j].value == "No")) {		
					myElement.style.display = 'block';
					myElement.innerHTML = '<div class="appErrorMessHead">Did you have a question regarding our terms and conditions?</div><div class="appErrorMessage"><p>In order to access your credit information and give you a quote, you must agree to the terms and conditions. Please contact us at 800-322-5004 with any questions or complete our <a href="/site/contact_us.aspx">contact us</a> form.</p></div>';			
					
				}else{
					myElement.style.display = 'none';
					myElement.innerHTML = '';
				}							
			}			
		}
	}
}

function checkApplyNow(){
	bolSendForm = true;
	bolCorrectLoanType = true; 
	bolCorrectLoanState = true; 
	for(i=0; i < document.forms.length; i++){
		for(j=0; j < document.forms[i].elements.length; j++){			
			if ((document.forms[i].elements[j].value == 0) && (document.forms[i].elements[j].name.indexOf("ddLoanType") > -1)){
				bolCorrectLoanType = false; 
				bolSendForm = false;						
			}		
			if ((document.forms[i].elements[j].value == 0) && (document.forms[i].elements[j].name.indexOf("ddLoanState") > -1)){
				bolCorrectLoanState = false;
				bolSendForm = false;								
			}		
		}
	}	
	
	if (bolCorrectLoanType == false && bolCorrectLoanState == false){
		alert('Please select a loan type and state.');
	}else if (bolCorrectLoanType == false){
		alert('Please select a loan type.');
	}else if (bolCorrectLoanState == false){
		alert('Please select a loan state.');
	}
	
	return bolSendForm;	
}

function addInfoLimit(source,arguments){
	if(arguments.length > 2000)
	{
		alert('Additional Information can not exceed 2000 characters.');
		return false;
	}else{
		return true;
	}
}

function setUrchin(){
	var z = _uGC(document.cookie, "__utmz=",";");	
	for(i=0; i < document.forms.length; i++){
		for(j=0; j < document.forms[i].elements.length; j++){			
			if ((document.forms[i].elements[j].name == "urchin_source") && (document.forms[i].elements[j].type == "hidden")){				
				document.forms[i].elements[j].value = _uGC(z,"utmcsr=","|");						
			}
			if ((document.forms[i].elements[j].name == "urchin_medium") && (document.forms[i].elements[j].type == "hidden")){
				document.forms[i].elements[j].value = _uGC(z,"utmcmd=","|");					
			}
			if ((document.forms[i].elements[j].name == "urchin_term") && (document.forms[i].elements[j].type == "hidden")){
				document.forms[i].elements[j].value = _uGC(z,"utmctr=","|");						
			}
			if ((document.forms[i].elements[j].name == "urchin_content") && (document.forms[i].elements[j].type == "hidden")){
				document.forms[i].elements[j].value = _uGC(z,"utmcct=","|");			
				
			}
			if ((document.forms[i].elements[j].name == "urchin_campaign") && (document.forms[i].elements[j].type == "hidden")){
				document.forms[i].elements[j].value = _uGC(z,"utmccn=","|");						
			}
		}
	}	
}

//IMAGE ROLLOVERS
function imgOn(imgName) { document[imgName].src = eval(imgName + "_on.src"); }
function imgOff(imgName) { document[imgName].src = eval(imgName + "_off.src"); }

//IMAGE CACHING
var oImgPath = new Object();
oImgPath.topnav = "../images/ph2_top_nav/";
oImgPath.rateBox = "../images/buttons/";
oImgPath.secNavTabs = "../images/page_tabs/";

function cacheImg(name,src,rollover,type){
	if (!type) var type = "gif";
	if (rollover==true){
		eval(name + "_off =  new Image");
		eval(name + "_off.src = \'" + src + name + "_off." + type + "\'");
		eval(name + "_on =  new Image");
		eval(name + "_on.src = \'" + src + name + "_on." + type + "\'");
	} else {
		eval(name + " = new Image");
		eval(name + ".src = \'"+src+name+"."+type+"\'");
	}
}



//-----Main Nav
cacheImg('tnav_advantage',oImgPath['topnav'],true);
cacheImg('tnav_fha',oImgPath['topnav'],true);
cacheImg('tnav_home',oImgPath['topnav'],true);
cacheImg('tnav_lcenter',oImgPath['topnav'],true);
cacheImg('tnav_new_purchase',oImgPath['topnav'],true);
cacheImg('tnav_refinancing',oImgPath['topnav'],true);

cacheImg('btn_home_rates',oImgPath['rateBox'],true);
cacheImg('btn_home_loanProg',oImgPath['rateBox'],true);
cacheImg('btn_home_help',oImgPath['rateBox'],true);
cacheImg('btn_home_apply',oImgPath['rateBox'],true);

function getElmtRef(strElmtId) {
	if (!document.getElementById) {
		return document.all[strElmtId];
	} else {
		return document.getElementById(strElmtId);
	}
}

function leftNavRoll(ctl,state){
	if(state){
		getElmtRef(ctl.id).className = 'leftNavOver';
	}else{
		getElmtRef(ctl.id).className = 'leftNavOff';
	}						
}

function findGlobalPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findGlobalPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
		curtop += 1;
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}