/*
	©Copyright 2005 Ford Motor Company. All rights reserved.
*/

// cross-browser function to return an element
// supports IE5.0+, Netscape 6.0+ and Mozilla
function finance_getElementRef(id) {
    if (document.all) {
        return document.all[id];
    }
    if (document.getElementById) {
        return document.getElementById(id);
    }
    return null;
}

//  check for valid numeric strings	
function IsNumeric(strString)
{
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}


// Used when the "Select Finance Plan" button is clicked to
// submit the finance form.
function selectFinancePlan(index) {
	showHourglassLayer();
	var frm=document.forms['financeForm'];
	frm.financePlanIndex.value=index;
	frm.submit();
}

function calculate() {
	var frm=document.forms['financeForm'];

	// Show an error if no deposit value.	
	if(!IsNumeric(frm.deposit.value)) {
		var layer=finance_getElementRef('deposit_error');
		if(layer!=null) {
			layer.style.visibility='visible';
		}
		frm.deposit.focus();
	}
	else {
		showHourglassLayer();
		frm.action.value='calculate';
		frm.submit();
		return true;
	}
}

function changePlan() {
	showHourglassLayer();
	var frm=document.forms['financeForm'];
	frm.action.value='changePlan';
	frm.submit();
}

function resetFinance() {
	showHourglassLayer();
	var frm=document.forms['financeForm'];
	frm.action.value='reset';
	frm.submit();
}

function deposit_keypress() {

	var characterCode;  //literal character code will be stored in this variable
	
	e = event;
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
	
	if(characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
		if(!calculate()) {
			e.keyCode=0;
			return false;
		}
	}
	
	return true;
}
