/*********************************************************
* Name : javascript.jsp
*
* Input Parameters :	
*						
*
* Purpose :	
*
* Dependecies: 	
*				
*
* Change History
* ==========================================================
*       CCR#    Updated   	Updated 
* Ver   PIR#    By        	On          Description
* ====  ====	========  	=========== ======================
* eRI.01          						Initial Coding
* P.01	5160	APandian	09/19/2001	decimal point validation function
* P.02	5473	Scott		09/25/2001	renamed file and removed script tags from top and bottom.
* P.03	5360	pnekkala	09/24/2001	Validate that the month and day 
										should b 2 digits and month <=12
* P.04	7778	APandain	12/14/2001  created function RoundToNdp
* P.05  8899    AThampy     02/28/2002  Added functions for auto tab 
* P.06  8446    AThampy     03/08/2002  Added function for calculating currency amounts
* P.07	6556	Satyanand	09/27/2002	Added code to Handle 5 digit Ext Nr.
* P.08  SCR 776 SCostello   10/07/2002  Added function that checks for spaces
* P.09  6556	Gangadhar	10/10/2002	Removed the code to check extension is less than 4 digits
* P.10	10457   kkishore	09/15/2004	Added a new function to validate if the field is number (Including -ve).
* P.11	80827	Chandrika	10/09/2009	Added validations code for Date of Birth field.
*/

// 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){
	//inputStr is an object??
	//lo is an integer
	//hi is an integer
	
//	alert("inputStr = " + inputStr.value);
//	alert("lo = " + lo);
	//alert("hi = " + hi);	
		
	var num = parseInt(inputStr, 10);
//	alert("num = " + num);
	
	if (num < lo || num > hi) {
		return false;
	}
	return true;
}

//14A@P.01
// 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;
		}
	}
}

//Start A@P.11
//This function validates that the year falls between 1900 and 2999.
//The parameter inputYear is an integer representing the year.
function isValidYearForDOB(inputYear) {	
	var yearInt = parseInt(inputYear.value,10);
	if (isNaN(yearInt)) {
		return false;
	} else {
		if (!inRange(yearInt,1900,2999)) {
			return false;
		}
	}
}
//End A@P.11

//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();

	//alert(startDate + " - " + endDate);
	
	if (startDate >= endDate) {
		//alert("about to return false");
		return false;
	}
	
	return true;
}

function ValidateDate(inputMonth,inputDay,inputYear,dateDescription) {
	var validDays = 0;
	var msg = "";
	
	//9A@P.03
	//validate that the month value is valid
	if (inputMonth.value > 12) {
		msg += "Please enter a motnh 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;
}

//Start A@P.11
function ValidateDateForDOB(inputMonth,inputDay,inputYear,dateDescription) {
	var validDays = 0;
	var msg = "";
	if ((inputMonth.value.length != 0) || (inputDay.value.length != 0) || (inputYear.value.length != 0))
	{
	//validate that the month value is valid
		if (inputMonth.value > 12 || inputMonth.value == 0) {
			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";
		}

		if (!(isnumeric(inputMonth.value))) {
			msg += "Please enter a Valid Month. \n";
		} else if (!(isnumeric(inputDay.value))) {
			msg += "Please enter a Valid Day. \n";
		} else if (!(isnumeric(inputYear.value))) {
			msg += "Please enter a Valid Year. \n";
		}
	
	//Validate that the number of days is valid for the month
		validDays = GetValidNumberOfDays(inputMonth.value);
		if (!inRange(inputDay.value,1,validDays) || inputDay.value == 0) {
			msg += "Please enter a day between 01 and " + validDays + " for the " + dateDescription + " date.\n";
		}
	
	//Validate that the year falls within the range of 1900 - 2999
		if (isValidYearForDOB(inputYear) == false || inputYear.value == 0) {
			msg += "Please enter a year between 1900 and 2999 for the " + dateDescription + " date.\n";
		}
	}
	return msg;
}
//End A@P.11

//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";
	}
	
	/* 3D@P.09
	if (extension.value.length < 4) {
		msg += phoneType;

		//1M@p.07
		msg += "Extension Number should have 4 or 5 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;
}
//4A@P.04
// Rounds to N Decimal point
function RoundToNdp(X, N) 
{ var T = Number('1e'+N)
    return Math.round(X*T)/T }

//26A@P.05
// Original Code Credit: Please do not remove! Anil Thampy
// Original:  Cyanide_7 (leo7278@hotmail.com)
// Web Site:  http://members.xoom.com/cyanide_7
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();
  }

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
}

// 6A@P.08 Function added to check for spaces
function containsSpaces(s){
    var found = false;
    if(s.indexOf(" ") >= 0)
        found = true;
    return found;
}

//Added for P.10 kishore

var strSeparator = ("/");
var vYearLength =2; //Set to 4 to force the user to enter 4 digits for the year.
//var msg;
// Call this function to format and validate the date. The parameters are object, object.value, event and datecheck boolean variable
// The function should be called from 
//		- onKeyUp event (Signature -> DateFormat(this, this.value, event, false) 
//		- onBlur event (Signature -> DateFormat(this, this.value, event, true) 
function DateFormat(vDateName, vDateValue, e, dateCheck)  {

  mDateValue = vDateValue;

// IE doesn't recognizes e.which. IE and Netscape recognizes keycode. Not sure if NE 4.x does.
//  var whichCode = (window.event)? e.which : e.keyCode;
  var whichCode = e.keyCode;
  if ((vDateValue.length > 2) && (vDateValue.charAt(2)!= strSeparator)){
        vDateName.value = vDateName.value.substr(0, 2)+ strSeparator;    
  }

  if ((vDateValue.length > 5) && (vDateValue.charAt(5)!= strSeparator)){
        vDateName.value = vDateName.value.substr(0, 5) + strSeparator;    
  }
//  alert(whichCode);
// Remove the char if the char is not 0-9 or "/"  
   if (! dateCheck) {
      //ignore the backspace , Tab, shift+tab. left & right arrow.
	  // This if clause needs to be modified to also consider any non printable key stroke like home ,end, esc etc
      if (whichCode  == 8 || whichCode  == 9 ||  whichCode  == 16  ||  whichCode  == 37 ||  whichCode  == 39){  
         return false
      }
      else if (((whichCode  > 47) && (whichCode  < 58)) || 
  	  		   ((whichCode  > 95) && (whichCode  < 106)) || 
	            (whichCode  == 191 &&(vDateValue.length==3 || vDateValue.length==6))) {
         // Do nothing
      }else {
         vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
         return false;
     }
  }

  if (vDateValue.length == 2 || vDateValue.length == 5) {
     vDateName.value = vDateName.value + strSeparator;
     return true;
  }         

  if (vDateValue.length == 8 && dateCheck){

     // if navigate outside the field and year is of 2 char then change the year to 4 char.
     // if the year is less than 50 make it 20xx else make it 19xx.              
    
     var yearYY  = vDateName.value.substr(6,2);
     var yearYYYY;
     if (yearYY > 50 ) {
       yearYYYY = '19' +yearYY;
     }else {
       yearYYYY = '20' +yearYY;
     }
//     alert(yearYYYY) ;
     vDateName.value = vDateName.value.substr(0, 6)+yearYYYY;
  }
  if (dateCheck) {
    Valid_Date(vDateName, vDateName.value);
  }
}


function Valid_Date(vDateName, vDateValue){
        
        var indate = vDateValue;
        if (vDateValue.length ==0){
           return true;
        }
//        alert(indate);
        if (vDateValue.length !=10){
           alert ("Please enter a valid date");
           vDateName.value="";
            return false;
        }

        if (indate.indexOf("-")!=-1){
           var sdate = indate.split("-")
        }
        else {
           var sdate = indate.split("/")
        }

        var chkDate=new Date(Date.parse(indate))
        var cmpDate=(chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(chkDate.getFullYear())
        var indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))

        if (indate2!=cmpDate){
                alert("You've entered an invalid date or date format.  Please use the MM/DD/YY or MM/DD/YYYY format.");
                vDateName.value ="";   
				return false;             
        }
        else {
                if (cmpDate=="NaN/NaN/NaN"){
                    myStatus(cmpDate);
                    alert("You've entered an invalid date or date format.  Please use the MM/DD/YY or MM/DD/YYYY format.");
                    vDateName.value ="";
					vDateName.focus();
  					return false;					
                }
        } 

}

