intValRE = new RegExp(/[^0-9]/g);
function showError (b, msg, field) {
	b.setHTML(msg);
	b.setStyles({"display": "block"});
	field.focus();
	return false;
}
function openGenericWindow(url){
	var props = "";
	switch (arguments.length){
		case 1:
			props = "width=440,height=500";
		break;
		case 2:
			var temp = new Array();
			var i = 0;
			if(typeof arguments[1] == "object"){
				for(p in arguments[1]){
					temp[i] = p + "=" + arguments[1][p];
					i++;
				}
			}
			props = temp.join(",");
		break;
		case 3:
			props = "width="+arguments[1] + ",height="+arguments[2];
	}
	var l = window.open(url,'abc','scrollbars=1,menubar=0,resizable=1,toolbar=0,location=0,status=0,'+props);
}
function sendLenderFeedback (/*DOM node*/ a, /*int*/ rateId) {
	var url = "http://www.loan.com/lender-feedback/"+rateId;
	a = $(a);
	var co = a.getPosition();
	var props = {width: 500, height: 412, left: co.x-500, top: co.y+50};
	openGenericWindow(url, props);
}

function submitRateOptionsForm(FHALoanLimit, VALoanLimit){
	var mBox = $("RatesStatus");
	var fel = document.getElementById("RateOptions").elements;
	var loanAmt = parseInt(fel["USER_LOAN_AMOUNT"].value.replace(intValRE, ""));


	if(fel['USER_STATE'].selectedIndex == 0){
		return showError(mBox, "Please select a state.", fel["USER_STATE"]);
	}
	if(fel['USER_LOAN_PROGRAM'].selectedIndex == 0){
		return showError(mBox, "Please select a loan type.", fel["USER_LOAN_PROGRAM"]);
	}
	if(isNaN(loanAmt) || loanAmt <= 0){
		return showError(mBox, "Please enter a valid loan amount.", fel["USER_LOAN_AMOUNT"]);
	}

	/*if (fel["USER_LOAN_PROGRAM"].options[fel["USER_LOAN_PROGRAM"].selectedIndex].text.indexOf("Home Equity") >=0) {
		var propVal = parseInt(fel["USER_PROPERTY_VALUE"].value.replace(intValRE, ""));
		var mortBal = parseInt(fel["USER_MORTGAGE_BALANCE"].value.replace(intValRE, ""));

		if(loanAmt < 10000){
			return showError(mBox, "Home Equity loan amount must be at least $10,000.", fel["USER_LOAN_AMOUNT"]);
		}
		if(isNaN(propVal) || propVal < 50000){
			return showError(mBox, "Property value must be at least $50,000.", fel["USER_PROPERTY_VALUE"]);
		}
		if(isNaN(mortBal)){
			return showError(mBox, "Please enter a valid mortgage balance.", fel["USER_MORTGAGE_BALANCE"]);
		}
		var cLTV = (loanAmt + mortBal) / propVal;
		if(cLTV >= 1.05){
			return showError(mBox, "LTV must be less than 105%.", fel["USER_MORTGAGE_BALANCE"]);
		}

		
	} else {*/
		if (loanAmt < 75000) {
			return showError(mBox, "Mortgage loan amount must be at least $75,000.", fel["USER_LOAN_AMOUNT"]);
		}
	//}
	if (fel["USER_LOAN_PROGRAM"].options[fel["USER_LOAN_PROGRAM"].selectedIndex].text.indexOf("FHA") >= 0) {
		if (loanAmt > FHALoanLimit) {
			return showError(mBox, "Maximum FHA Loan Amount is $"+FHALoanLimit, fel["USER_LOAN_AMOUNT"]);
		}
	} else if (fel["USER_LOAN_PROGRAM"].options[fel["USER_LOAN_PROGRAM"].selectedIndex].text.indexOf("VA") >= 0) {
		if (loanAmt > VALoanLimit) {
			return showError(mBox, "Maximum VA Loan Amount is $"+VALoanLimit, fel["USER_LOAN_AMOUNT"]);
		}
	}

	//validation complete, hide error box.
	mBox.setStyles({"display": "none"});
	document.getElementById('RateOptions').submit();
}


function setupLenderRating() {
	if ($("ResultsTable")) {
		var rows = $("ResultsTable").tBodies[0].rows;
		var LenderRatingTips = Array();
		for(var i = 0, row; row = rows[i]; i++) {
			var img = row.cells[8].getElementsByTagName("img");
			if (img.length > 0) {
				var img = row.cells[8].getElementsByTagName("img");
				var TipStr = "";
				for (var j=0; j<img.length; j++) {
					TipStr += "<div class=\"StarTip\">"+img[j].getAttribute("alt")+"</div>\n";
				}
				row.cells[8].title = TipStr;
				LenderRatingTips.push(row.cells[8]);
			}
		}
		var ret = new Tips(LenderRatingTips, {offsets: {x:0, y:30}, className: "LenderRating"});
	}
}

IE = document.all ? true : false;
mouseX = 0;
mouseY = 0; 
if (!IE) {
  document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove =  function(e) {
	var ret = '';

	if (IE) {
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
	} else {
		mouseX = e.pageX;
		mouseY = e.pageY;
	}  
	if (mouseX < 0) { mouseX = 0; }
	if (mouseY < 0) { mouseY = 0; }
	return true;
 };
var CIRObj = {
  cache : [],
  fired : [],
  tips : [],
   
  init: function(){
	 this.tip = new Element('div').setStyles({
		'position': 'absolute',
		'top': '0',
		'left': '0',
		'display':'none'
	}).addClass('StarHover-tip').injectInside(document.body);
  }
};
    
function showTip(rid){

  if($chk(CIRObj.fired[rid]) && !$chk($chk(CIRObj.cache[rid]))) return;

  if($chk(CIRObj.cache[rid])){
    disp(rid);
    return;
  } else {
    CIRObj.fired[rid] = true;
    var url = '/remote/Starhover.php?RATE_ID=' + rid;
    var myAjax = new Ajax(url, {method: 'get',onComplete : createTip});
    myAjax.request();
  }
}

function createTip(x){
  var cb = eval( "(" + x + ")");
  CIRObj.cache[cb.id] = true;
  var html = '';
  cb.ratings.each(function(item){html += "<h2>" + item + "</h2>"});
  CIRObj.tips[cb.id] = html;
  disp(cb.id);
}

function disp(rid){
  CIRObj.tip.innerHTML = CIRObj.tips[rid];
  CIRObj.tip.style.display='block';
  CIRObj.tip.style.left= (mouseX - 300)+'px';
  CIRObj.tip.style.top= (mouseY + 20)+'px';
}

function hideTip(rid){
	CIRObj.tip.style.display='none';
}
    
window.addEvent("domready", function(){
	//because of callLender() this script is called in 2 more pages.
	
		//fdTableSort.init();
		//MortgageRates();
		//toggleHEFields();
		//setupTooltips();
		//setupLenderRating();	
		CIRObj.init();
});


