/*
 Parameter: id of form element
 When a form has text fields with default values such as "Enter Amount",
 this function will clear the default value when user focuses on the text field.
*/
function clearFieldsOnClick(/*String*/ FormId){
	var f = $(FormId);
	if (f) {
		var inp = f.getElementsByTagName("INPUT");
		for (var i=0; i<inp.length; i++) {
			if (inp[i].getAttribute("type") == "text") {
				inp[i].setAttribute("origVal", inp[i].value);
				inp[i].onfocus = function(){
					if(this.getAttribute("origVal") == this.value){
						this.value = "";
					}
				}
			}
		}
	}
}

function highlightFieldVal (/*form obj*/ f) {
	var inp = f.getElementsByTagName("INPUT");
	for(var i=0; i<inp.length; i++) {
		inp[i].onfocus = function(){
			this.select();
		}
	}
}

function goToSelectedUrl(sel){
	var val = sel.options[sel.selectedIndex].value;
	if(sel.selectedIndex != 0 && val.length > 0){
		window.location.href = val;
	}
}

/*
this function assigns alternating row colors to table with id=tableId

Usage:
2. If dojo is used on the report page, make a call to paintRows() after </table>. E.g.:
	<script type="text/javascript">paintRows('table1');</script>

3. If dojo is not used, window.onload call to paintRows() can be used. E.g.:
	window.onload = function(){
		paintRows('table1');
	}

4. Default class names are 'even' and 'odd'.
*/
function paintRows(tableId){
	var t = document.getElementById(tableId);
	if(t){
		switch (arguments.length){
			case 1:
				var classEven = 'even';
				var classOdd = 'odd';
				break;
			case 2:
				var classEven = arguments[1];
				var classOdd = 'odd';
				break;
			case 3:
				var classEven = arguments[1];
				var classOdd = arguments[2];
		}
		if(t.tBodies.length > 0){
			var r = t.tBodies[0].rows; //get reference to an array of table rows. Assign class names to even / odd rows.
			for(var i=0; i < r.length; i++){
				var tmp = r[i].className;
				if(tmp.length > 0)
					r[i].className = (i % 2 == 0) ? tmp+' '+classEven : tmp+' '+classOdd; 
				else	
					r[i].className = (i % 2 == 0) ? classEven : classOdd; 
			}
		}
	}
}

function openBorrowersBillOfRights(){
	window.location.href = "/borrowers-bill-of-rights";
}

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);
}

/*format numeric string into dollar amount*/
function formatDollarAmount(/*String number*/ str){
	str = str.replace(/[^0-9.]/g, "");
	num = parseFloat(str);
	if(!isNaN(num)){
		num = Math.round(num);
		str = num+"";
		var RE = /(\d+)(\d{3})/;
		while (RE.test(str)){
			str = str.replace(RE, '$1' + ',' + '$2');
		}
		return "$"+str;
	}
	else
		return "";
}

function formatLoanAmount(/*Form input object*/ obj){
	if(obj.value && obj.value.length > 3){
		obj.value = formatDollarAmount(obj.value);
	}
}

function validateMtgRates() {
	var f = document.forms["StaticMtgRates"];
	var fel = f.elements;

	var validated = true;
	if(fel["State"].selectedIndex == 0){
		document.getElementById('errorState').innerHTML = "Please select a state";
		validated = false;
	}
	if(fel["LoanAmount"].value.length == 0){
		document.getElementById('errorPrincipal').innerHTML = "Please enter a loan amount";
		validated = false;
	}

	if (validated == false) {
		return validated;
	}

	var url = "/mortgage-rates";
	url += "/"+fel["State"].options[fel["State"].selectedIndex].value;
	url += "/"+fel["LoanType"].value;
	url += "/"+fel["LoanPoints"].value;
	url += "/"+fel["LoanAmount"].value.replace(/[^0-9]/g, "");

	if (fel['src'] && fel['src'].value.length > 0) {
		url += '?src=' + fel['src'].value;
	}

	window.location.href=url;
	return false;
}
