/*
**	function isBlank(p_inputVal)
** 	function isPosInteger(p_inputVal)
** 	function isInteger(p_inputVal)
** 	function isNumber(p_inputVal)
** 	function isPosNumber(p_inputVal)
** 	function getCheckedIndex(p_objRadio)
** 	function isChecked(p_obj)
**  	function isSelected(p_objSelect)
** 	function isNumericCurrency(oField, strThousandDelimiter, strDecimalDelimiter)
** 	function stripCurrency(p_inputVal, strThousandDelimiter)
** 	function isDate(p_inputVal)
** 	function padDate(p_number)
** 	function formatDate(p_date, p_strInFormat, p_strOutFormat)
**  	function selectByText(p_objSelect, strText)
** 	function truncateFloat(inputVal, strDecimalDelimiter, maxNumberOfDecimals)
**  	function getTomorrow()
**  	function getFutureDate( sInputDate )
**  	function ensureInFuture( sInputDate, sDateName )
**  	function checkDateOrder( inpBefore, inpAfter, sBeforeDateName, sAfterDateName )
**  	function extractPhone( areaCd, phone )
*/

	function isBlank(p_p_inputVal) {
		if (p_p_inputVal == "" || p_p_inputVal == null) {
			return true;
		} else {
			return false;
		}
	}

	function isPosInteger(p_inputVal) {
		inputStr = p_inputVal.toString();
		for (var i = 0; i < inputStr.length; i++) {
			var oneChar = inputStr.charAt(i);
      		if ( oneChar < "0" || oneChar > "9" ) {
				return false;
			}
		}
		return true;
	}

	function isInteger(p_inputVal) {
		inputStr = p_inputVal.toString();
		for (var i = 0; i < inputStr.length; i++) {
			var oneChar = inputStr.charAt(i);
			if (i == 0 && oneChar == "-") {
				continue;
			}
			if (oneChar < "0" || oneChar > "9") {
				return false;
			}
		}
		return true;
	}

	function isNumber(p_inputVal) {
		oneDecimal = false;
		inputStr = p_inputVal.toString();
		for (var i = 0; i < inputStr.length; i++) {
			var oneChar = inputStr.charAt(i);
			if (i == 0 && oneChar == "-") {
				continue;
			}
			if (oneChar == "." && !oneDecimal) {
				oneDecimal = true;
				continue;
			}
			if (oneChar < "0" || oneChar > "9") {
				return false;
			}
		}
		return true;
	}

	function isPosNumber(p_inputVal) {
		oneDecimal = false;
		inputStr = p_inputVal.toString();
		for (var i = 0; i < inputStr.length; i++) {
			var oneChar = inputStr.charAt(i);
			if (i == 0 && oneChar == "-") {
				return false;
			}
			if (oneChar == "." && !oneDecimal) {
				oneDecimal = true;
				continue;
			}
			if (oneChar < "0" || oneChar > "9") {
				return false;
			}
		}
		return true;
	}

	function getCheckedIndex(p_objRadio) {
		for (var i = 0; i < p_objRadio.length; i++) {
			if (eval('p_objRadio[' + i + '].checked')) {
				return i;
			}
		}
		return null;
	}

	function isChecked(p_obj) {
		for (var i = 0; i < p_obj.length; i++) {
			if (eval('p_obj[' + i + '].checked')) {
				return true;
			}
		}
		return false;
	}

	function isSelected(p_objSelect) {
		if (p_objSelect.options[p_objSelect.selectedIndex].value == "") {
			return false;
		} else {
			return true;
		}
	}

	function isNumericCurrency(p_inputVal, strThousandDelimiter, strDecimalDelimiter) {
		var inputStr = p_inputVal.toString();
		var DecimaldelimiterIndex = inputStr.indexOf(strDecimalDelimiter);
		var integerPart;
		var decimalPart;
		if (DecimaldelimiterIndex > 0) {
			integerPart = inputStr.substring(0, DecimaldelimiterIndex);
			decimalPart = inputStr.substring(DecimaldelimiterIndex + 1, inputStr.length);
			if (decimalPart.length > 2) {
				return false;
			}
		}
                //check for a negative number
                if(inputStr.indexOf('-') != -1){
                  return false;
                }
		var intDotCount = 0;
		for (var i = 0; i < inputStr.length; i++) {
			var oneChar = inputStr.charAt(i);
			if ((oneChar < "0" || oneChar > "9") && (oneChar != strThousandDelimiter) && (oneChar != strDecimalDelimiter)) {
				return false;
			} else {
				if (oneChar == strDecimalDelimiter) {
					intDotCount += 1;
					if (intDotCount > 1) {
						return false;
					}
				}
			}
		}
		return true;
	}

	function stripCurrency(p_inputVal, strThousandDelimiter) {
		var inputStr = p_inputVal.toString();
		var strippedFloat = "";
		for (var i = 0; i < inputStr.length; i++) {
			var oneChar = inputStr.charAt(i);
			if (oneChar != strThousandDelimiter) {
				strippedFloat += oneChar;
			}
		}
		strippedFloat = parseFloat(strippedFloat);
		return strippedFloat;
	}

	function isDate(p_inputVal) {

		var bLeapYear = false;
		var strLength = p_inputVal.length;

		if (strLength > 10) {
			return false;
		}

		if (p_inputVal.indexOf("-") != -1){
			while (p_inputVal.indexOf("-") != -1){
				p_inputVal = p_inputVal.replace(/-/, "/");
			}
		}

		var month = p_inputVal.substring(0, p_inputVal.indexOf("/"));
		var day = p_inputVal.substring(p_inputVal.indexOf("/") + 1, p_inputVal.lastIndexOf("/"));
		var year = p_inputVal.substring(p_inputVal.lastIndexOf("/") + 1, strLength);

		//Year validation, get information about corresponding months
		if (year.length != 4) {
			return false;
		} else {
			if (isPosNumber(year)){
				year = parseInt(year);
				if (!(year >= 1700)){
					return false;
				}
				var arrNonLeapMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
				var arrLeapMonth =  [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
				/*
				Check for leap year ..
				1.Years evenly divisible by four are normally leap years, except for...
				2.Years also evenly divisible by 100 are not leap years, except for...
				3.Years also evenly divisible by 400 are leap years.
				*/
				if ((year % 4) == 0) {
					if ((year % 100) == 0 && (year % 400) != 0){
						bLeapYear = false;
					} else {
						bLeapYear = true;
					}
				} else {
					bLeapYear = false;
				}
			} else {
				return false;
			}
		}

		//Month validation
		if (!(isPosNumber(month))){
			return false;
		} else {
			month = parseFloat(month);
			if (!(month >= 1 && month <= 12)){
				return false;
			}
		}

		//Day validation
		if (!(isPosNumber(day))){
			return false;
		} else {
			day = parseFloat(day);
			if (bLeapYear){
				if (!(day >= 1 && day <= arrLeapMonth[month - 1])){
					return false;
				}
			} else {
				if (!(day >= 1 && day <= arrNonLeapMonth[month - 1])){
					return false;
				}
			}
		}

		return true;
	}

    	function padDate(p_number){
        	var strNumber = "" + p_number;
        	if (strNumber.length < 2){
           		return "0" + p_number;
        	} else {
            		return p_number;
        	}
    	}


	function formatDate(p_date, p_strInFormat, p_strOutFormat){
		var strTempFormat = "";
		var strLength = p_date.length;
		var month, day, year;

		if (p_date.indexOf("-") != -1){
			while (p_date.indexOf("-") != -1){
				p_date = p_date.replace(/-/, "/");
			}
		}

		switch (p_strInFormat){
			case "mm/dd/yyyy":
				month = p_date.substring(0, p_date.indexOf("/"));
				day = p_date.substring(p_date.indexOf("/") + 1, p_date.lastIndexOf("/"));
				year = p_date.substring(p_date.lastIndexOf("/") + 1, strLength);
				strTempFormat = year + padDate(month) + padDate(day);
				break;
		}

		switch (p_strOutFormat){
			default:
				return strTempFormat;
		}
	}

    	function selectByText(p_objSelect, strText)
    	{
        	for( var i=0; i < p_objSelect.options.length; i++ )
        	{
            	if( p_objSelect.options[i].value == strText )
            	{
                	p_objSelect.options[i].selected = true;
            	}
        	}
    	}

	function truncateFloat(inputVal, strDecimalDelimiter, maxNumberOfDecimals) {
		var inputStr = inputVal.toString();
		var DecimaldelimiterIndex = inputStr.indexOf(strDecimalDelimiter);
		var integerPart;
		var decimalPart;
		var roundedFloat;
		if (DecimaldelimiterIndex > 0) {
			integerPart = inputStr.substring(0, DecimaldelimiterIndex);
			decimalPart = inputStr.substring(DecimaldelimiterIndex + 1, inputStr.length);
			if (decimalPart.length > maxNumberOfDecimals) {
				droppedDecimal = decimalPart.substr(maxNumberOfDecimals, 1);
				decimalPart = decimalPart.substr(0, maxNumberOfDecimals);
			}
			return integerPart + strDecimalDelimiter + decimalPart;
		} else {
			return inputVal;
		}
	}


  	/**
   	* gets the string in the 'mm/dd/yyyy' format representing
   	* tomorrow's date
   	*/
  	function getTomorrow()
  	{
    		return getDateRelative(1);
  	}

  	/**
   	* compares the input date with tomorrow's date. if input date is
   	* less that tomorrow's date, then it returns tomorrow's date.
   	* otherwise it returns the input date.
   	* note: both the input, and output date formats are assumed to be
   	*       'mm/dd/yyyy/'
   	*/
  	function getFutureDate( sInputDate )
  	{
    		sDate = parseFloat(formatDate(sInputDate, "mm/dd/yyyy", "yyyymmdd"));

    		var sTomorrow = getTomorrow();
    		var tomorrowDate = parseFloat( formatDate( sTomorrow, "mm/dd/yyyy", "yyyymmdd" ) );

    		// if input date is in the future, then use it
    		if( sDate >= tomorrowDate )
    		{
      			return sInputDate;
    		}
    		// otherwise, use tomorrow's date
    		else
    		{
      			return sTomorrow;
    		}
  	}

  	/**
   	* compares the input date with tomorrow's date. if the input date is not
   	* in future (is today, or in the past) then it returns 'false'; otherwise
   	* it returns 'true'.
   	* note: the date is expected in the 'mm/dd/yyyy' format
   	*/
  	function isInFuture( sInputDate )
  	{
    		sDate = parseFloat(formatDate(sInputDate, "mm/dd/yyyy", "yyyymmdd"));

    		var sTomorrow = getTomorrow();
    		var tomorrowDate = parseFloat( formatDate( sTomorrow, "mm/dd/yyyy", "yyyymmdd" ) );

    		// if date is not in future, then prompt the user
    		if( sDate < tomorrowDate )
    		{
      			return false;
    		}
    		else
    		{
      			return true;
    		}
  	}

  	/**
   	* checks the order in which the two dates are. if 'after date' is less than
   	* 'before date' then it returns 'false'; otherwise it returns 'true'.
   	* note: the dates are expected in the 'mm/dd/yyyy' format
   	*/
  	function checkDateOrder( sBeforeDate, sAfterDate )
  	{
    		sBeforeDate = parseFloat(formatDate(sBeforeDate, "mm/dd/yyyy", "yyyymmdd"));
    		sAfterDate = parseFloat(formatDate(sAfterDate, "mm/dd/yyyy", "yyyymmdd"));

    		if( sAfterDate < sBeforeDate )
    		{
      			return false
    		}
    		else
    		{
      			return true;
    		}
  	}

  	/**
   	* Process area code and phone number, removing any
   	* '(', ')', '-' and ' ' characters
   	*/
  	function extractPhone(areaCd, phone)
  	{
    		sAreaCd = areaCd.value;
    		sPhone = phone.value;
    		while (sAreaCd.indexOf("(") != -1)
    		{
      			sAreaCd = sAreaCd.replace("(", "");
    		}
    		while (sAreaCd.indexOf(")") != -1)
    		{
      			sAreaCd = sAreaCd.replace(")", "");
    		}
    		while (sPhone.indexOf("-") != -1)
    		{
      			sPhone = sPhone.replace("-", "");
    		}
    		while (sPhone.indexOf(" ") != -1)
    		{
    			sPhone = sPhone.replace(" ", "");
    		}

    		areaCd = sAreaCd;
    		phone = sPhone;
  	}

  	function isValidPhone(areaCd, phone)
  	{
    		sAreaCd = areaCd.value;
    		sPhone = phone.value;

    		while (sAreaCd.indexOf("(") != -1)
    		{
      			sAreaCd = sAreaCd.replace("(", "");
    		}	
    		while (sAreaCd.indexOf(")") != -1)
    		{
      			sAreaCd = sAreaCd.replace(")", "");
    		}
    		while (sPhone.indexOf("-") != -1)
    		{
      			sPhone = sPhone.replace("-", "");
    		}
    		while (sPhone.indexOf(" ") != -1)
    		{
      			sPhone = sPhone.replace(" ", "");
    		}
    		if (!isPosInteger(sAreaCd) || !isPosInteger(sPhone))
    		{
      			return false;
    		}	
    		if (sAreaCd.length != 3 || sPhone.length != 7)
    		{
      			return false;
    		}

    		areaCd.value = sAreaCd;
    		phone.value = sPhone;

    		return true;
  	}