/**
  * COMMON JAVASCRIPT FUNCTIONS
**/
//NAV FOR TABLE ROWS - MAKES THEM LINKS
function DoNav(theUrl){
  document.location.href = theUrl;
}


/* SET POSTACTION ON FORM SUBMIT */
function submitForm(POSTACTION){
	if(POSTACTION=="ASK_DELETE_REC"){
		var answer = confirm("Are you sure you want to delete this record?");
		if(answer==true){
			document.form.POST_ACTION.value = "DELETE_REC";
			document.form.submit();
		}
	}else if(POSTACTION=="NEXT"){
		var answer = confirm("Are you sure you want to continue?  You will not be able to change anything on your application if you continue.");
		if(answer==true){
			document.form.POST_ACTION.value = "DOWNLOAD";
			document.form.submit();
		}
	}else{
		document.form.POST_ACTION.value = POSTACTION;
		document.form.submit();
	}
}


/* PLACE FOCUS ON FIRST FORM OBJECT */
function placeFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
				document.forms[0].elements[i].focus();
				break;
			}
		}
	}
}


/* AUTO TAB */
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
		found = true;
	else
		index++;
		return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	
	return true;
}

/* SHOWS AND HIDES A DIV'S CONTENT - 
<a href="javascript:ShowContent('uniquename')">Click to show.</a> - SHOW
<a href="javascript:HideContent('uniquename')">Click to hide.</a> - HIDE
<a href="javascript:ReverseDisplay('uniquename')">Click to show/hide.</a> - TOGGLE
*/
function HideContent(d) {
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
	document.getElementById(d).style.display = "block";
}

function ReverseDisplay(d) {
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
	else { document.getElementById(d).style.display = "none"; }
}

//FORM VALIDATION *******************************************
//***********************************************************

//ONLY ENTER NUMBERS - onkeypress="return numbersonly(this, event)"
function numbersonly(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
    return true;

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;

  // decimal point jump
  else if (dec && (keychar == ".")) {
    myfield.form.elements[dec].focus();
    return false;
  } else
    return false;
}

//ENTER NUMBERS AND DECIMAL POINT - onkeypress="return numbersdecimal(this, event)"
function numbersdecimal(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
    return true;

  // numbers
  else if ((("0123456789.").indexOf(keychar) > -1))
    return true;

  // decimal point jump
  else if (dec && (keychar == ".")) {
    myfield.form.elements[dec].focus();
    return false;
  } else
    return false;
}

//CURRENCY - onchange="currency(this);"
function currency(which){
	currencyValue = which.value;
	currencyValue = currencyValue.replace(",", "");
	decimalPos = currencyValue.lastIndexOf(".");
	if (decimalPos != -1){
			decimalPos = decimalPos + 1;
	}
	if (decimalPos != -1){
			decimal = currencyValue.substring(decimalPos, currencyValue.length);
			if (decimal.length > 2){
					decimal = decimal.substring(0, 2);
			}
			if (decimal.length < 2){
					while(decimal.length < 2){
						 decimal += "0";
					}
			}
	}
	if (decimalPos != -1){
			fullPart = currencyValue.substring(0, decimalPos - 1);
	} else {
			fullPart = currencyValue;
			decimal = "00";
	}
	newStr = "";
	for(i=0; i < fullPart.length; i++){
			newStr = fullPart.substring(fullPart.length-i-1, fullPart.length - i) + newStr;
			if (((i+1) % 3 == 0) & ((i+1) > 0)){
					if ((i+1) < fullPart.length){
						 newStr = "," + newStr;
					}
			}
	}
	which.value = newStr + "." + decimal;
}

function normalize(which){
	alert("Normal");
	val = which.value;
	val = val.replace(",", "");
	which.value = val;
}
//END CURRENCY


/* NEXT -  */