var domain="ratewindow.com";
document.write("<link rel='stylesheet' href='http://"+domain+"/services/qq/jquery/blue/style.css' type='text/css' media='screen' />");
document.write("<link rel='stylesheet' href='http://"+domain+"/services/qq/css/slider.css' type='text/css' media='screen' />");
document.write("<link rel='stylesheet' href='http://"+domain+"/services/qq/css/ddcolortabs.css' type='text/css' media='screen' />");


///----------------------------------------------------------------------------------------


var local_sites = false;


	function display_working(stage) {
		
		//if(stage=="rw_results" && local_sites)
			//return;
			
			var message = "RateWindow is Working";
			if(stage=="rw_results")
				message = "RateWindow is gathering your loan results <br>This can take up to 1 minute";
			else
				message = "RateWindow is working";
		//
//			if(local_sites) {
//				if(stage=="rw_results")
//					document.getElementById('qq_container').innerHTML = "<center><div style='padding-top:60px'><img src='http://"+domain+"/services/qq/images/loadingAnimation.gif' style='border:0px' /></div></center>";	
//			} else {
		document.getElementById('qq_container').innerHTML = "<center><div style='width:400px'><div style=\"padding-top:60px;font-size:1.2em; color:#6C88BA; font-family:arial;\"><div style='font-size:1.2em; background-color:#FFFFFF;text-align:center; border:1px solid #666666;margin-bottom:40px;padding:20px'>"+message+"<br /><img src='http://"+domain+"/services/qq/images/loadingAnimation.gif' style='border:0px' /></div></center>	";
			//}
		}

				function get_county_in_state() 	{
					var id = document.getElementById('states').options[document.getElementById('states').selectedIndex].value;
					var req  = 'http://'+domain+'/services/qq/services/rw_form_counties.php?id='+id; 
					bObj = new JSONscriptRequest(req); 
					bObj.buildScriptTag(); 
					bObj.addScriptTag();
				}
				
				
				var frmQQID=0;
				function get_county_in_state_2(stateval) 	{
				
					//var id = document.getElementById('states').options[document.getElementById('states').selectedIndex].value;
					var req  = 'http://'+domain+'/services/qq/services/rw_form_counties_2.php?id='+stateval; 
					bObj = new JSONscriptRequest(req); 
					bObj.buildScriptTag(); 
					bObj.addScriptTag();
				}
				
				
				
				function rw_results_counties_2(obj)
				{
						document.getElementById('countyrow_'+frmQQID).style.display="";
						document.getElementById('countycontent_'+frmQQID).innerHTML = obj;
					
					
					
			}
				
				
				function rw_results_counties(obj)
				{
						document.getElementById('countyrow').style.display="";
						document.getElementById('countycontent').innerHTML = obj;
					
					
					
				
					
					
					if(document.frmDET) {
						var cval =document.frmDET.tcounty.value;
							if(county!=0) {
								for(i=0;i<document.frmDET._CountyID.options.length;i++)
								if(document.frmDET._CountyID.options[i].value==cval)
								document.frmDET._CountyID.options[i].selected=true;
							}
					}
					
					
					
					
					if(document.frmQQ) {
						
						if(local_sites) {
								var cval = 0;
								if(document.getElementById("tcounty")) {
									cval = 	document.getElementById("tcounty").value;
								}
								if(cval!=0) {
									for(i=0;i<document.frmQQ._CountyID.options.length;i++)
										if(document.frmQQ._CountyID.options[i].value==cval)
											document.frmQQ._CountyID.options[i].selected=true;
								}
								
								
						} else {
						
							if(getURLVar("county_id").length>0) {
								var cval = getURLVar("county_id");
								if(cval!=0) {
									for(i=0;i<document.frmQQ._CountyID.options.length;i++)
										if(document.frmQQ._CountyID.options[i].value==cval)
											document.frmQQ._CountyID.options[i].selected=true;
								}
							}
						}
				   }
			}
				
				
			function hideLRFORM(name) {
				document.getElementById("reshow_apply_form_"+name).innerHTML = "";
				document.getElementById("results_rates_div_"+name).style.display = '';
			}	

			function reShowQQForm(name,stateid,property_type,credit_rating) {
					var qqformstr  = document.getElementById("qqform_div").innerHTML;
					qqformstr = qqformstr.replace(/frmQQ/g,"frmQQ_"+name);
					qqformstr = qqformstr.replace(/divnamex/g,name);
					qqformstr = qqformstr.replace(/countycontent/g,"countycontent_"+name);
					qqformstr = qqformstr.replace(/countyrow/g,"countyrow_"+name);
					
					document.getElementById("reshow_apply_form_"+name).innerHTML = qqformstr;
					document.getElementById("results_rates_div_"+name).style.display = 'none';
					var frm = document.getElementById("frmQQ_"+name);
					frmQQID = name;
					var states = frm._StateID;
					for(i=0;i<states.options.length;i++) {
						
						if(states.options[i].value==stateid) {
							
							states.options[i].selected = true;
							break;
						}
					}
					
					var pts = frm._PropertyType;
					for(i=0;i<pts.options.length;i++) {
						if(pts.options[i].value==property_type) {
							pts.options[i].selected = true;
							break;
						}
					}
					
					var cr = frm._CreditScore;
					for(i=0;i<cr.options.length;i++) {
						if(cr.options[i].value==credit_rating) {
							cr.options[i].selected = true;
							break;
						}
					}
					
				}	



function validateMail(email) {
				
				if(email.value =="")
					return false;
					var emailId = email.value;
				var apos=emailId.indexOf("@");
				var dotpos=emailId.lastIndexOf(".");
				var lastpos=emailId.length-1;
				if(lastpos-dotpos<2)
					return false;
				
				if(dotpos==lastpos)
					return false;
				 if (!(emailId.indexOf(' ')==-1 && 0 < emailId.indexOf('@') && 0 < emailId.indexOf('.') && emailId.indexOf('@')+1 < emailId.length && emailId.length >= 5)){
			
					return false;
				 }
				return true;
			}

		function submitContactForm(frm) {
			
			if(frm.email.value.length == 0)
			{
				alert ("Email is required.");
				return false
			}
			if(!validateMail(frm.email))
			{
				alert ("Email is invalid.");
				return false
			}
			
			if(frm.uname.value.length == 0)
			{
				alert ("Name is required.");
				return false
			}
				
			if(frm.captcha.value.length == 0)
			{
				alert ("Captcha is required.");
				return false
			}
			
				
			if(frm.message.value.length == 0)
			{
				alert ("Message is required.");
				return false
			}
			var str = $("#frmContact").serialize();
			var formdata = document.getElementById("qq_container").innerHTML;
			display_working("contact_data");
				$.getJSON("http://"+domain+"/services/qq/save_contact_details.php?"+str+"&jsoncallback=?",
        		function(data){
						result = data.header.code;
						if(result==-1) {
							alert("Error Saving Contact Data, Security Code is Incorrect.");
							document.getElementById("qq_container").innerHTML = formdata;
						}
						else {
							document.getElementById("qq_container").innerHTML = "<br><h1>Contact Information Sent Successfully.</h1></br>";
						}
				
				});
		
		}






	 // jquery functions
	 
	 	function submit_newsletter(name,loanid,dsid,account_key) {
			if(document.getElementById('nl_email_'+name).value == ''){
				alert ("Email address is required.");
				document.getElementById('nl_email_'+name).focus();
				return false;
			}
			if(!validateMail(document.getElementById('nl_email_'+name))) {
				alert ("Please enter the email address in correct format.");
				document.getElementById('nl_email_'+name).focus();
				return false;							 
			}
			
			var fullname = document.getElementById('nl_name_'+name).value;
			var email = document.getElementById('nl_email_'+name).value;
					
			var sfdiv = document.getElementById("subscription_form_div_"+name);
			sfdiv.innerHTML = "<div align='center'><div style=\"padding-top:60px;font-size:1.2em; color:#6C88BA; font-family:arial;\"><div style='font-size:1.2em; text-align:center; border-top:1px solid #00CC33; border-bottom:1px solid #00CC33; background-color:#E1EBD6'>Please wait!<br /><img src='http://"+domain+"/services/qq/images/loadingAnimation.gif' /></div>";
			
			var str = "full_name="+fullname+"&customer_email="+email+"&dsid="+dsid+"&loanid="+loanid+"&account_key="+account_key;
			$.getJSON("http://"+domain+"/services/qq/process_nw_request.php?"+str+"&jsoncallback=?",
       			 function(data){
					result = data.header.result;
					if(result==true)
						sfdiv.innerHTML = "Thank you, a confirmation email has been sent. Please confirm to receive our newsletter.";
						
					else if(result=='already')
						sfdiv.innerHTML = "You have already been subscribed to the newsletter";
					else
						sfdiv.innerHTML = "Error, Please try again.";
				 });
				

		}
	 
	 
	 
	 	var results_type = "qq";
		
		
		
		function processRequest(params) {
		
		var formdata = document.getElementById("qq_container").innerHTML;
		
		if(results_type=="detailed") {
				document.getElementById('qq_container').innerHTML = "<div align='center'><div style=\"padding-top:60px;font-size:1.2em; color:#6C88BA; font-family:arial;\"><div style='font-size:1.2em; text-align:center; border-top:1px solid #00CC33; border-bottom:1px solid #00CC33; background-color:#E1EBD6'>Submitting Application<br /><img src='http://"+domain+"/services/qq/images/loadingAnimation.gif' /></div>";
			} else
		display_working("rw_results");
		
		//window.open("http://'+domain+'/services/qq/process_loan_request.php?"+str+"&jsoncallback=?");
		$.getJSON("http://"+domain+"/services/qq/process_loan_request.php?"+params+"&jsoncallback=?",
        function(data){
        		
				loan_id = data.header.code;
				dsid = data.header.dsid;
				account_key = data.header.account_key;
					if(results_type=="detailed") {
						agent = data.body.text;
						document.getElementById("qq_container").innerHTML = "<div style='padding:10px;'><div><img src='http://"+domain+"/images/RWlogoFINAL_2.jpg' /></div><div>Thank you for submitting a loan application with RateWindow&trade;</div><div style='padding:10px;'>Your RateWindow&trade; mortgage broker will be contacting you shortly. In a few minutes you will receive an email with your personalized RateWindow&trade; loan rates. When your RateWindow&trade; mortgage broker contacts you, use these rates to determine the best rate and Freebate&trade; for you and your family needs.</div><br>"+agent+"</div>";
						
						} else {
								if(local_sites) {
								
									showLoanResults(loan_id,dsid,zip,results_type);
								
									
								} else 
								location.href=redirect+"?loan_id="+loan_id+"&dsid="+dsid+"&results_type="+results_type+"&widget_id="+widget_id+"&account_key="+account_key+"&zipcode="+zip;
						}
				
        
		});
		return false;	
		}
		
		
		function getResults(frm) {
			var str = $("#"+frm).serialize();
			if(frm=="frmDET") {
					results_type = "detailed";
				if(!check_form())
						return false;
			} else {
				if(!check_qq_form(frm)) {
					return false;
					}
			}
				
				processRequest(str);
		}
		
		function check_form()
		{
			if(document.getElementById('customer_email').value == '')
			{
				alert ("Email address is required.");
				return false
			}
			
			if(!validateMail(document.getElementById('customer_email'))) {
						
				alert ("Please enter the email address in correct format.");
				return false;							 
													 
			}
		return true;
	}
		
		function check_qq_form(frm)
		{
			
			var frm = document.getElementById(frm);
			
			if(frm._StateID.value == 0)
			{
				alert ("State is required.");
				return false
			}
			if(frm._CountyID.value == 0)
			{
				alert ("County is required.");
				return false
			}
			
			var property_value = frm._PropertyValue.value;
			if(property_value=="") {
				alert ("Property Value is required.");
				return false;
			}
			
			if(isNaN(property_value)) {
				alert("Property Value should be numeric.");	
				return false;
			}
			
			if(property_value.indexOf(".")>0) {
				var vals = property_value.split(".");
				var dec_val = vals[1];
				if(dec_val!="0" && dec_val!="00") {
					alert("Property Value should not contain figures after decimal.");	
					return false;
				}
			}
			
		return true;
	}
		
			
		function refreshResults() {
			showLoanResults(loan_id,results_type);
		}	
			
		function dhtmlLoadScript(url)
{
	document.write("<script type='text/jsvascript' src='"+url+"'></script>");
	
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e); 
}
	
			
			
// json functions				
				function JSONscriptRequest(fullUrl){
						this.fullUrl = fullUrl;this.noCacheIE='&noCacheIE='+(new Date()).getTime();
						this.headLoc = document.getElementsByTagName("head").item(0);this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
				}
				JSONscriptRequest.scriptCounter=1;
				JSONscriptRequest.prototype.buildScriptTag=function(){
								this.scriptObj = document.createElement("script");
								this.scriptObj.setAttribute("type", "text/javascript");
								this.scriptObj.setAttribute("charset", "utf-8");
								this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
								this.scriptObj.setAttribute("id", this.scriptId);
				}
				JSONscriptRequest.prototype.removeScriptTag=function(){this.headLoc.removeChild(this.scriptObj);  }
				JSONscriptRequest.prototype.addScriptTag=function(){this.headLoc.appendChild(this.scriptObj);}
				function json(url){bObj = new JSONscriptRequest(url); bObj.buildScriptTag(); bObj.addScriptTag();}


function send_request(stage, post_data,callback)
				{
					
					callback=stage;
					if(stage!='qq_hosted_form')
						display_working(stage);
					var url = 'http://'+domain+'/services/qq/ratewindow_qq_request.php?callback='+callback+'&rw_stage='+stage+post_data+"&widget_id="+widget_id; 
					
					json(url);
				}
				
     			function rw_display_detailed_form(obj){document.getElementById('qq_container').innerHTML = obj; 
						
						var cval = document.frmDET.tstate.value;
						for(i=0;i<document.frmDET._StateID.options.length;i++)
						if(document.frmDET._StateID.options[i].value==cval)
						document.frmDET._StateID.options[i].selected=true;
						get_county_in_state();
						zipcode = document.frmDET.zipcode.value;
						var level = 'zipcode';
						var limit = 1;
						var state;
						var city;
						var county;
				}
				
				function rw_display_page(obj) {
					document.getElementById('qq_container').innerHTML = obj; 
					var title = document.getElementById("ptitle").innerHTML;
					document.title =title; 
				}
					
				
				
				function rw_display_form(obj){
						
					document.getElementById('temp').innerHTML = obj; 
					
					account_key  = document.getElementById('div_mbid').innerHTML;
					statecode = document.getElementById('div_statecode').innerHTML;
					if(agentdiv)
						document.getElementById("agent_div").innerHTML= document.getElementById('agent_card').innerHTML;
					document.frmQQ.account_key.value = account_key;
					document.frmQQ.statecode.value = statecode;
					
					if(getURLVar("state_id").length>0) {
						statecode = getURLVar("state_id");
						document.frmQQ.statecode.value = statecode;
					}
					
					for(i=0;i<document.frmQQ._StateID.options.length;i++)
							if(document.frmQQ._StateID.options[i].value==statecode) {
								document.frmQQ._StateID.options[i].selected=true;
							}
					
					if(getURLVar("property_value").length>0)
						document.frmQQ._PropertyValue.value = getURLVar("property_value");
						
					if(getURLVar("property_type").length>0) {
						var pt = getURLVar("property_type");
						for(i=0;i<document.frmQQ._PropertyType.options.length;i++)
							if(document.frmQQ._PropertyType.options[i].value==pt) {
								document.frmQQ._PropertyType.options[i].selected=true;
							}
					}
	
					if(getURLVar("credit_score").length>0) {
						var cs = getURLVar("credit_score");
						for(i=0;i<document.frmQQ._CreditScore.options.length;i++)
							if(document.frmQQ._CreditScore.options[i].value==cs) {
								document.frmQQ._CreditScore.options[i].selected=true;
							}
					}
	
	
					if(statecode!=0)
						get_county_in_state();
						
					//document.frmQQ.submit.disabled=false;
					
				}
				function rw_display_results(obj){document.getElementById('qq_container').innerHTML = obj; 
					 
					 		initSlider("30yearSLIDER");
							initSlider("15yearSLIDER");
							initSlider("armSLIDER");
							
							
							$.tablesorter.addParser({id: 'rebate',is: function(s) { return false; }, format: function(s) { return formatIfNumber(s); }, type: 'numeric' }); 
							//$("#tbl_30yearSLIDER").tablesorter({ headers: {  1: { sorter:'rebate' } ,4:{sorter:false} }    });
							//$("#tbl_15yearSLIDER").tablesorter({ headers: {  1: { sorter:'rebate' } ,4:{sorter:false} }    });
							//$("#tbl_armSLIDER").tablesorter({ headers: {  1: { sorter:'rebate' } ,4:{sorter:false} }    });
						
							initSliderValues("30yearSLIDER");
							//initSliderValues("15yearSLIDER");
							
				
				}

					

				var actual_ps =0;
				function initSliderValues(slidername) {
					var start = parseFloat($("#"+slidername+"_min_value").val());
					var end = parseFloat($("#"+slidername+"_max_value").val());
					var initial_rate = parseFloat($("#"+slidername+"_init_value").val());
					var d = (end-start)/100;
					//var ps = Math.ceil(Math.abs(start)/Math.abs(d));
					
					actual_ps = ((initial_rate-start/100)*100)/d;
					var ps=Math.round(((initial_rate-start/100)*100)/d);
					//alert(start+"="+initial_rate+"="+d+"="+ps);
					//jQuery('#'+slidername).slider( "moveTo", start, 0 );
					jQuery('#'+slidername).slider( "moveTo", ps, 0 );
					//applyFilter(slidername,start,d,5,true)
						
						//showPostiveOnly(ps,slidername);
					}


				

				function initSlider(slidername) {
					var start = parseFloat($("#"+slidername+"_min_value").val());
					var end = parseFloat($("#"+slidername+"_max_value").val());
					var d = (end-start)/100;
					//var ps = Math.ceil(Math.abs(start)/Math.abs(d));
						$('#'+slidername).slider( { 
														
						minValue: 0,
						maxValue: 100,
						change: function(e,ui) {
							
							applyFilter(slidername,start,d,5);
						},
						slide: function(e,ui) { 

								//var mincvalue = Math.ceil($('#'+slidername).slider('value',0)*d+start);
								//var maxcvalue = Math.ceil($('#'+slidername).slider('value',1)*d+start);
								
								//$("#"+slidername+"_min_value").val(mincvalue);
								//$("#"+slidername+"_max_value").val(maxcvalue);
								
								//$("#"+slidername+"_min_cont").html("$ "+ formatMoney (mincvalue,"",",","."));
								//$("#"+slidername+"_max_cont").html("$ "+formatMoney (maxcvalue,"",",","."));
						}
					 } );
						
				}


function resetSliders(name) {
	jQuery('#'+name).slider( "moveTo", 0, 0 );
	jQuery('#'+name).slider( "moveTo", 100, 1 );
	jQuery('#'+name).slider( "moveTo", 0, 0 );
	jQuery('#'+name).slider( "moveTo", 100, 1 );
	
		}
		
	var first_time = true;	
	function applyFilter(name,start,d,max_rows) {
	
		
		//var min_rate = parseFloat($("#"+name+"_min_value").val());
		var min_rate = $('#'+name).slider('value',0)*d+start;
		if(first_time) {
			min_rate = actual_ps*d+start;
			//alert("first time " + actual_ps + "   " + min_rate);
		}
		
		
		min_rate = min_rate/100;

		//var max_rebate = parseFloat($("#"+name+"_max_value").val());
		var rows = 0;
		var hide_all=false;
		 $("#tbl_"+name+" tr:has(td)").each(function(){  
			
			rvalue = $('td', this).filter(':nth-child(1)').text(); //ch to 1 from 2
			
			rvalue = rvalue.replace(/,/g, '');
			rvalue = rvalue.replace(/\$/g, '');
			
			rate = parseFloat(rvalue); // 
				
				if ( rate>=min_rate && hide_all==false) {
				rows++;
						$(this).show()
				
				if(rows==max_rows) {
					hide_all = true;
					
					}
				} else {
					$(this).hide()
				} 
		 		
		 });
		  
		  first_time=false;
	}		
			
			
			function open_window(url, name, height, width){var iMyWidth;var iMyHeight;iMyWidth = (window.screen.width/2) - (width/2 + 10);iMyHeight = (window.screen.height/2) - (height/2 + 50);var newwindow = window.open(url, name, 'location=0,status=0,scrollbars=1,width='+width+',height='+height+', resizable=no,left=' + iMyWidth + ',top=' + iMyHeight + ',screenX=' + iMyWidth + ',screenY=' + iMyHeight + ',toolbar=no,menubar=no,directories=no');if(window.focus) newwindow.focus();}
	
	function showPostiveOnly(mt,name) {
	

	//alert(mt);
	//if(document.getElementById(name+"_chkShowPositive").checked) {
	jQuery('#'+name).slider( "moveTo", mt, 0 );
	jQuery('#'+name).slider( "moveTo", 100, 1);
	jQuery('#'+name).slider( "moveTo", mt, 0 );
	jQuery('#'+name).slider( "moveTo", 100, 1);
	//} else {
	//resetSliders(name);
	//}
	}
	
	function isThousands(position) {
if (Math.floor(position/3)*3==position) return true;
return false;
};

function formatMoney (theNumber,theCurrency,theThousands,theDecimal) {


var negative = "";
if(theNumber<0)
	{
			negative ="-";
			theNumber = theNumber*-1;
	
	}

var theDecimalDigits =
Math.round((theNumber*100)-(Math.floor(theNumber)*100));
theDecimalDigits= ""+ (theDecimalDigits + "0").substring(0,2);
theNumber = ""+Math.floor(theNumber);
var theOutput = theCurrency+negative;
for (x=0; x<theNumber.length; x++) {
theOutput += theNumber.substring(x,x+1);
if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1
!=0)) {
theOutput += theThousands;
};
};
theOutput += theDecimal + theDecimalDigits;
return theOutput;
};
			
		
	var tabs1 = Array("pd","tab4","tab5");
	var tabs2 = Array("30year","15year","arm","tab4","tab5");
	var fifteenyear_clicked = false;
	var arm_clicked = false;
	
function selectTab(tab,stype) {
	var tabs = tabs1;
	
	if(stype==2)
		tabs = tabs2;
		
	for(i=0;i<tabs.length;i++) {
		if(tabs[i]==tab) {
			if(tabs[i]=="tab4" || tabs[i]=="tab5")
				document.getElementById(tabs[i]).className = "content_current";
			else	
			document.getElementById(tabs[i]).className = "current";
			document.getElementById(tabs[i]+"_cont").style.display = "block";
		} else {
			if(tabs[i]=="tab4" || tabs[i]=="tab5")
				document.getElementById(tabs[i]).className = "content";
			else	
				document.getElementById(tabs[i]).className = "";
			
			document.getElementById(tabs[i]+"_cont").style.display = "none";
		
		} 
	}
	if(tab=="15year") {
			if(!fifteenyear_clicked) {
				first_time = true;
				initSliderValues("15yearSLIDER");
				fifteenyear_clicked = true;
			}
		}
		
	
	if(tab=="arm") {
			if(!arm_clicked) {
				first_time =true;
				initSliderValues("armSLIDER");
				arm_clicked = true;
			}
		}	

}		
	
	

function formatIfNumber(data) {
data = Trim(data);
data = data.replace(/\$/g,'');
data = data.replace(/\,/g,'');
return data;	
}
				
//$.tablesorter.addParser({id: 'rebate',is: function(s) { return false; }, format: function(s) { return formatIfNumber(s); }, type: 'numeric' }); 

				function Trim(str)
				{
				  return str.replace(/^\s*|\s*$/g,"");
				 
				}




var account_key = '';
var widget_id='';
var redirect = "";
var state='';
var county='';
var property_value='';
var credit=0;
var loan_id=0;
var zip=0;

function open_pop_window(url, name, height, width){
	var iMyWidth;var iMyHeight;iMyWidth = (window.screen.width/2) - (width/2 + 10);iMyHeight = (window.screen.height/2) - (height/2 + 50);
	var newwindow = window.open(url, name, 'location=0,status=0,scrollbars=1,width='+width+',height='+height+', resizable=no,left=' + iMyWidth + ',top=' + iMyHeight + ',screenX=' + iMyWidth + ',screenY=' + iMyHeight + ',toolbar=no,menubar=no,directories=no');
	
	if(window.focus) newwindow.focus();}
	

function showQQForm(ak) {
if(ak!="")
		account_key = ak;

	send_request('qq_form', '&account_key='+account_key,'rw_display_form');
}

function showQQFormByZipcode(zip) {
	send_request('qq_form', '&zipcode='+zip,'rw_display_form');
}


function showQQFormHostedByZipcode() {
	if(document.frmQQ.zipcode.value!='')
		zip = document.frmQQ.zipcode.value;
	else
		zip = '99999';
	
	var astr = "";
		
			var state_id=getURLVar('state_id');
			if(state_id.length>0)
				astr='&state_id='+state_id;
			
			
			var county_id=getURLVar('county_id');
			if(county_id.length>0)
				astr+='&county_id='+county_id;
		
	
	send_request('qq_hosted_form', '&hosted_type=zipcode&zipcode='+zip+astr,'rw_display_form');
}

var agentdiv = true;
function showQQFormHostedByWidetID() {

	widget_id = document.frmQQ.widget_id.value;
		var astr = "";
		
			var state_id=getURLVar('state_id');
			if(state_id.length>0)
				astr='&state_id='+state_id;
			
			
			var county_id=getURLVar('county_id');
			if(county_id.length>0)
				astr+='&county_id='+county_id;
	
		var str = "wid="+widget_id;
		$.getJSON("http://"+domain+"/services/qq/get_account_key_by_widget_id.php?"+str+"&jsoncallback=?",
				function(data){
						account_key  = data.header.code;
						document.frmQQ.account_key.value = account_key;
						if(account_key=='search_mbs') {
							//alert("this will be a search for mbs");	
						}
						else if(account_key<=0) {
								alert("Error loading the widget, ("+account_key+")");
						} else {
							send_request('qq_hosted_form', '&hosted_type=ak&account_key='+account_key+astr,'rw_display_form');
						}
				});	
}



function showQQFormHostedByMBKEY() {

	account_key = document.frmQQ.account_key.value;
		var astr = "";
		
			var state_id=getURLVar('state_id');
			if(state_id.length>0)
				astr='&state_id='+state_id;
			
			
			var county_id=getURLVar('county_id');
			if(county_id.length>0)
				astr+='&county_id='+county_id;
	send_request('qq_hosted_form', '&hosted_type=ak&account_key='+account_key+astr,'rw_display_form');
}

	function showQQFormCityState(city,state) {
		send_request('qq_form', '&city='+city+'&state='+state,'rw_display_form');
	}

function showLoanResults(loan_id,dsid,zip,rtype) {

	send_request('loan_results', '&loan_id='+loan_id+'&dsid='+dsid+'&zipcode='+zip+'&results_type='+rtype+'&account_key='+account_key+'&county_id='+county,'rw_display_results');
}

function openDetailedForm() {
	location.href='?request=detailed_form&widget_id='+widget_id+'&account_key='+account_key+'&zipcode='+zip+'&loan_id='+loan_id;
	}


function openDetailedFormWithRate(rate_id) {
	location.href='?request=detailed_form&widget_id='+widget_id+'&account_key='+account_key+'&zipcode='+zip+'&loan_id='+loan_id+"&rate_id="+rate_id;
	}


function showDetailedForm(rate_id) {
	
	send_request('detailed_form','&account_key='+account_key+'&zipcode='+zip+'&loan_id='+loan_id+"&rate_id="+rate_id,'rw_display_detailed_form');
}

function openPage(pagename,account_key)  {
	send_request('open_page','&pagename='+pagename+'&account_key='+account_key);
	}
	

function tryAgain() {
			account_key = getURLVar("account_key");
			zip = getURLVar("zipcode");
			dsid = getURLVar("dsid");
			location.href=redirect+"?zipcode="+zipcode+"&dsid="+dsid;
	}
	
	
function getURLVar(urlVarName) {
	
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
			var urlVars = urlHalves[1].split('&');
				for(i=0; i<=(urlVars.length); i++){
						if(urlVars[i]){
							var urlVarPair = urlVars[i].split('=');
								if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
										urlVarValue = urlVarPair[1];
								}
						}
				}
	}
	return urlVarValue;
}

function showNewsletterForm(name) {
	$("#container_loanresults_"+name).hide();
	$("#container_newsletter_"+name).show();
	
	}
	
function hideNewsletterForm(name) {
	$("#container_loanresults_"+name).show();
	$("#container_newsletter_"+name).hide();
	
	}	