/*
N.01 pchong 11-20-2006 WO13127 When tabbing also select to allow edits with cursor moves via pre-existing functions "autotab(...)"
*/
// This function formats all fields to the 0.00 format
function numberFix(z) {
		var newZ = "";
		for (i = 0; i < z.value.length; i++) {
	        var c = z.value.charAt(i);
	       	if (isnumeric(c) || c == ".") {		
				newZ += c;
			}

		}
		
		newZarray = newZ.split(".");
		if (newZarray.length >= 2) {
			if (newZarray[1] < 10) {
				newZ = newZarray[0] + ".0" + newZarray[1];
			} else {
				newZ = newZarray[0] + "." + newZarray[1];
			}
		}

		if (newZarray.length < 2) {
			newZ = newZ + ".00";
		}
		newZarray = newZ.split(".");
		newZ = Math.abs(newZarray[0]) + ".";
		if (Math.abs(newZarray[1].substring(0,2)) == 0) {
			newZ += "00";
		} else {
			newZ += Math.abs(newZarray[1].substring(0,2));
		}			
		return newZ;
	}	

function cancelme() {
	if (confirm ("Are you sure that you want to cancel?")) {
			location.href = "/home.jsp";
	}
}

function logoff() {
	if (confirm ("Are you sure that you want to logoff?")) {
			location.href = "/logoff.jsp";
	}
}
// This functions checks the droplist
function DroplistCheck(dlist){
	var l = "";	
	for (z=0; z < dlist.length; z++) 
	{
		if (dlist[z].selected) {
			l = dlist[z].value;
		}
	}
	if ((l == null) || (l == "") || isblank(l)) {
		return false;
	}
	return true;
}
// This function checks to see if the value sent to it is numeric
function isnumeric(z) {
	if ((z * 1 > 0) || (z == 0)) {
		return true;
	} else {
		return false;
	}
}

// this function returns true only if the field
// contains a positive decimal or integer number	
function isPosNumber(field){
	oneDecimal = false;
	inputStr = field.value.toString();
	for (var i = 0; i < inputStr.length; i++){
		var oneChar = inputStr.charAt(i);
		if (oneChar == "." && !oneDecimal){
			oneDecimal = true;
			continue;
		}			
		if (oneChar < "0" || oneChar > "9"){
			return false;
		}				
	}	
	return true;
}

// This method checks the input parameter (z) for alphabets. It returns true
// if numeric digits are present only else it returns false.
function isAllDigits(z) {
	for (var i = 0; i < z.value.length; i++) {
    	var c = z.value.charAt(i);        
		if ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ) {
           return false;
        }
		if (c >="0" && c<="9" ) {	
			// do nothing since digit is between 0 & 9, continue processing
		} else {
			return false;
		}		
	}
	return true;
}

function isEmpty(field) {
  str = field.value;
  if ((str == "") || (str == "undefined")){
    return true;
    }
  else {
    for(j=0; j<str.length; j++) {
      if(str.charAt(j) != " ") {
        return false;
        }
      }
    }
  return true;
  }
  
function inRange(inputStr, lo, hi){
		
	var num = parseInt(inputStr, 10);

	if (num < lo || num > hi) {
		return false;
	}
	return true;
}

// this function returns true only no of decimal digit
// is less than or equal to specified
function DecimalDgtCheck(inputStr,dcmlpnt){
	var oneChar = "-";
	for (i = 0 ; i < inputStr.length; i++){
		oneChar = inputStr.charAt(i);
		if (oneChar == "."){
			if ((inputStr.length - i - 1) > dcmlpnt) {
			return false;
			}			
		}
	}	
	return true;
}

//This function validates that the year falls between 1800 and 2999.
//The parameter inputYear is an integer representing the year.
function isValidYear(inputYear) {	
	var yearInt = parseInt(inputYear.value,10);
	if (isNaN(yearInt)) {
		return false;
	} else {
		if (!inRange(yearInt,1800,2999)) {
			return false;
		}
	}
}

//This function validates that the month falls between 1 and 12.
//The parameter inputMonth is an integer representing the Month.
function isValidMonth(inputMonth) {	
	var monthInt = parseInt(inputMonth.value,10);
	if (isNaN(monthInt)) {
		return false;
	} else {
		if (!inRange(monthInt,1,12)) {
			return false;
		}
	}
}


//This function validates that the start date is earlier than the end date.  
//If not, it returns a false.
function CrossValidateStartAndEndDates(inputStartMonth,inputStartDay,inputStartYear,inputEndMonth,inputEndDay,inputEndYear) {
	
	//All input parameters should be integers
	var startDate = "";
	var endDate = "";

	if ((inputStartMonth.toString()).length == 1 ) {
		inputStartMonth = "0" + inputStartMonth.toString();}
	
	if ((inputStartDay.toString()).length == 1 ) {
		inputStartDay = "0" + inputStartDay.toString();}	
	
	if ((inputEndMonth.toString()).length == 1 ) {
		inputEndMonth = "0" + inputEndMonth.toString();}		
	
	if ((inputEndDay.toString()).length == 1 ) {
		inputEndDay = "0" + inputEndDay.toString();}		
	
	startDate = inputStartYear.toString() + inputStartMonth.toString() + inputStartDay.toString();	
	endDate = inputEndYear.toString() + inputEndMonth.toString() + inputEndDay.toString();
	
	if (startDate >= endDate) {
		return false;
	}
	
	return true;
}

//28A@eNV.01 - Added for use in W&C - SCarlson
//This function validates that the start date is earlier than or equal to the end date.  
//If not, it returns a false.
function CrossValidateStartAndEndDatesAllowEqual(inputStartMonth,inputStartDay,inputStartYear,inputEndMonth,inputEndDay,inputEndYear) {
	
	//All input parameters should be integers
	var startDate = "";
	var endDate = "";

	if ((inputStartMonth.toString()).length == 1 ) {
		inputStartMonth = "0" + inputStartMonth.toString();}
	
	if ((inputStartDay.toString()).length == 1 ) {
		inputStartDay = "0" + inputStartDay.toString();}	
	
	if ((inputEndMonth.toString()).length == 1 ) {
		inputEndMonth = "0" + inputEndMonth.toString();}		
	
	if ((inputEndDay.toString()).length == 1 ) {
		inputEndDay = "0" + inputEndDay.toString();}		
	
	startDate = inputStartYear.toString() + inputStartMonth.toString() + inputStartDay.toString();	
	endDate = inputEndYear.toString() + inputEndMonth.toString() + inputEndDay.toString();

	if (startDate > endDate) {
		return false;
	}

	return true;
}

function ValidateDate(inputMonth,inputDay,inputYear,dateDescription) {
	var validDays = 0;
	var msg = "";
	

	//validate that the month value is valid
	if (inputMonth.value > 12) {
		msg += "Please enter a month between 01 and 12. \n";
	}
	
	//validate that the month and day are 2 digits
	if (inputMonth .value.length !=2){
		msg += "Make sure that the month value is 2 digits. \n";
	}
	if (inputDay .value.length !=2){
		msg += "Make sure that the day value is 2 digits. \n";
	}
	
	//Validate that the number of days is valid for the month
	validDays = GetValidNumberOfDays(inputMonth.value);
	if (!inRange(inputDay.value,1,validDays)) {
		msg += "Please enter a day between 01 and " + validDays + " for the " + dateDescription + " date.\n";
	}
	
	//Validate that the year falls within the range of 1800 - 2999
	if (isValidYear(inputYear) == false) {
		msg += "Please enter a year between 1800 and 2999 for the " + dateDescription + " date.\n";
	}
	
	return msg;
}

//This function gets the number of days in a month
function GetValidNumberOfDays(monthVal) {
	//monthVal is an integer representing the month
	var indexMonth = parseInt(monthVal,10);
	var monthMax = new Array (31,31,29,31,30,31,30,31,31,30,31,30,31);
	return monthMax[indexMonth];
}

//This function gets the integer value corresponding to a month
//The input is a month field
function GetNumberForMonth(inputMonthField) {
	if ((inputMonthField.value) == "JAN") {return 1;}	
	if ((inputMonthField.value) == "FEB") {return 2;}
	if ((inputMonthField.value) == "MAR") {return 3;}
	if ((inputMonthField.value) == "APR") {return 4;}	
	if ((inputMonthField.value) == "MAY") {return 5;}
	if ((inputMonthField.value) == "JUN") {return 6;}		
	if ((inputMonthField.value) == "JUL") {return 7;}	
	if ((inputMonthField.value) == "AUG") {return 8;}
	if ((inputMonthField.value) == "SEP") {return 9;}	
	if ((inputMonthField.value) == "OCT") {return 10;}	
	if ((inputMonthField.value) == "NOV") {return 11;}
	if ((inputMonthField.value) == "DEC") {return 12;}	
}

	// This checks a droplist to make sure a valid option was selected
	function isOptionSelected(selectobj){	
		var l = "";
		for (z=0; z < selectobj.length; z++){	
			if (selectobj.options[z].selected){
				l = selectobj.options[z].value;
			}
		}		
		if ((l == null) || (l == "") || (isblank(l))){
			return false;
		}else{
			return true;
		}
	}

	// This checks if a variable is blank
	function isblank(s) {
		for(var i = 0; i < s.length; i++) {
			var c = s.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
		return true;	
	}

	
	// This validates that a field ONLY has letters in it.  No numbers or special symbols
	
	function isLettersOnly(field) {
	
		var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1"){
				return false;
			}
		}
	
		return true;
	}	

	// This validates that a field ONLY has alphanumeric characters in it.  No special symbols.
	
	function isAlphanumeric(field) {
	
		var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1"){
				return false;
			}
		}
	
		return true;
	}	


	
function isValidEmailAddress(z) {
	var c;
	var atOk = "no";
	var dotOk = "no";

	for (var i = 0; i < z.length; i++) {
  		c = z.charAt(i);
		if  (c == "@") {atOk = "yes";}	
		if  (c == ".") {dotOk = "yes";}
	}
	if ((atOk == "no") || (dotOk == "no")) {
		return false;
	} else {
		return true;
	}


}	


/* This function validiates Phone Numbers, Fax Numbers, Mobile Numbers:
	
	Argument 'phType':
	
	P = "Phone ";
	H = "Home Phone ";
	W = "Work Phone ";
	M = "Mobile Phone ";
	F = "Fax ";

	Author: Elango Narayanasamy
	Date:	07/30/2001
*/

function validatePhoneNumber(phType, areaCode, exchNum, lineNo) {
var phoneType = "";
var msg = "";

	// Set Phone Type
	if (phType == "P") { phoneType = "Phone " }
	else if (phType == "H") { phoneType = "Home Phone " }
	else if (phType == "W") { phoneType = "Work Phone " }
	else if (phType == "M") { phoneType = "Mobile Phone " }
	else if (phType == "F") { phoneType = "Fax " }


	// Validate Area Code
	if (isblank(areaCode.value) == true) {
		msg += "Enter your ";
		msg += phoneType;
		msg += "Area Code.\n";
	}
	if (isAllDigits(areaCode) == false) {
		msg += "Enter a numeric value for the ";
		msg += phoneType;
		msg += "Area Code.\n";
	}
	if (areaCode.value.length < 3) {
		msg += phoneType;
		msg += "Area Code should have 3 Digits.\n";
	}

	// Validate Exchange Number
	if (isblank(exchNum.value) == true) {
		msg += "Enter your ";
		msg += phoneType;
		msg += "Exchange Number.\n";
	}
	if (isAllDigits(exchNum) == false) {
		msg += "Enter a numeric value for the ";
		msg += phoneType;
		msg += "Exchange Number.\n";
	}
	if (exchNum.value.length < 3) {
		msg += phoneType;
		msg += "Exchange Number should have 3 Digits.\n";
	}

	// Validate Line Number
	if (isblank(lineNo.value) == true) {
		msg += "Enter your ";
		msg += phoneType;
		msg += "Line Number.\n";
	}
	if (isAllDigits(lineNo) == false) {
		msg += "Enter a numeric value for the ";
		msg += phoneType;
		msg += "Line Number.\n";
	}
	if (lineNo.value.length < 4) {
		msg += phoneType;
		msg += "Line Number should have 4 Digits.\n";
	}

	return msg;

}


function validatePhoneNumberNExtn(phType, areaCode, exchNum, lineNo, extension) {
var phoneType = "";
var msg = "";


	msg += validatePhoneNumber(phType, areaCode, exchNum, lineNo);

	// Set Phone Type
	if (phType == "P") { phoneType = "Phone " }
	else if (phType == "H") { phoneType = "Home Phone " }
	else if (phType == "W") { phoneType = "Work Phone " }
	else if (phType == "M") { phoneType = "Mobile Phone " }
	else if (phType == "F") { phoneType = "Fax " }


	// Validate Extension Number
	if (isblank(extension.value) == true) {
		msg += "Enter your ";
		msg += phoneType;
		msg += "Extension Number.\n";
	}
	if (isAllDigits(extension) == false) {
		msg += "Enter a numeric value for the ";
		msg += phoneType;
		msg += "Extension Number.\n";
	}
	if (extension.value.length < 4) {
		msg += phoneType;
		msg += "Extension Number should have 4 Digits.\n";
	}

	return msg;

}

//Validates if a button from a radio button group exists
function isButtonSelected(buttonGroup){
		for (var i = 0; i < buttonGroup.length; i++){
			if (buttonGroup[i].checked){
				return true;
			}
		}
		return false;	
}

function validateSSN(ssn) {
	//ssn is a string
	//Validate that the SSN is numeric and is exactly 9 digits.
	
	var msg = "";
	if (!(isnumeric(ssn))) {
		msg += "The social security number must be all numbers.";
	} else {
		if (ssn.length != 9) {
			msg += "The social security number must be exactly 9 numbers.";
		}
	}
	return msg;
}

// Rounds to N Decimal point
function RoundToNdp(X, N) 
{ var T = Number('1e'+N)
    return Math.round(X*T)/T }

function autoTab(input,len, e){
	var isNN = (navigator.appName.indexOf("Netscape")!=-1);
	if(isNN){
	  document.captureEvents(Event.KEYPRESS);
	}  
  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();
	// 1A@N.01
	input.form[(getIndex(input)+1)%input.form.length].select();
  }

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;
}

//Function added for displaying currency fields 

function toDollarsAndCents(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}
