//Validation on html objects
/////////////////////////////////////////////////////////////////////////////
function isValidDateTextField(txtObj){
    trimTextField(txtObj);
    if(!isValidDate(txtObj.value)){
        txtObj.focus();
        return false;
    }
    return true;
}
/////////////////////////////////////////////////////////////////////////////
//e.g. 12.344   numeric values with period for the fraction
function isValidAmountTextField(txtObj){
    trimTextField(txtObj);
    if(!isNumeric(txtObj.value)){
        txtObj.focus();
        return false;
    }
    return true;
}
/////////////////////////////////////////////////////////////////////////////
//Numbers like account number and sequence numbers , which is only digits
function isValidNumberTextField(txtObj){
    trimTextField(txtObj);
    if(!isAllDigits(txtObj.value)){
        txtObj.focus();
        return false;
    }
    return true;
}
/////////////////////////////////////////////////////////////////////////////
function isEmptyTextField(txtObj){
    trimTextField(txtObj);
   if(txtObj.value==""){
    return true;
  }
  return false;
}
//////////////////////////////////////////////////////////////////////////////
function trimTextField(txtObj){
    txtObj.value=trim(txtObj.value);
}
/////////////////////////////////////////////////////////////////////////////
//Used to clear text field value related to another drop downlist (date condition and date in batch filter)
//so that the text field value will be reset when the list is selected on ALL
//Note : it should be set on onChange action of the specified list
function clearTextFieldOnNoListValue(lst,txtField){
    if(lst.value=="" || lst.value==-1){
        txtField.value="";
    }
}
/////////////////////////////////////////////////////////////////////////////
//checks weather any item of the array of check boxes is checked , like chequesList in the inward and outward pages
function isAnyItemCheckedInList(chkArr){
    if(chkArr==null){
        return false;
    }
    if(chkArr.length != null){//check weather it is array of check boxes or single entry
        var i;
        for(i=0;i<chkArr.length;i++){
            if(chkArr[i].checked){
                return true;
            }
        }
    }else{//this means there is only one item passed to this method;
         return chkArr.checked;
    }
    return false;
}
/////////////////////////////////////////////////////////////////////////////
//generic validation functions
/////////////////////////////////////////////////////////////////////////////
function isAllDigits(s) {
    var test = "" + s;
    if(test.length==0){
        return false;
    }
    for (var k = 0; k < test.length; k++){
        if (!isDigit(test.charAt(k)) ){
            return false;
            }
        }
    return true;
    }
//////////////////////////////////////////////////////////////////////////////
//Trim Functions , trim from the right and the left
function trim(str){
  return str.replace(/^\s*|\s*$/g,"");
}
//////////////////////////////////////////////////////////////////////////////
function fixFileName(str){
  return (str.replace(/\/|:|\)|\(|\+|\-/g,"-"));
}

//////////////////////////////////////////////////////////////////////////////
//Left trim
function lTrim(str){
	if (str==null){return null;}
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
	}
//////////////////////////////////////////////////////////////////////////////
//right trim
function rTrim(str){
	if (str==null){
          return null;
        }
	for(var i=str.length-1;str.charAt(i)==" ";i--);
        return str.substring(0,i+1);
	}
/////////////////////////////////////////////////////////////////////////////
//
//Single characters validation
//
/////////////////////////////////////////////////////////////////////////////
function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
	}
///////////////////////////////////////////
function isLetter(ch) {
    return (IsUpper(ch) || IsLower(ch));
}
///////////////////////////////////////////
function isAlnum(ch){
  return isDigit(ch)||isLetter(ch);
}
///////////////////////////////////////////
function isLower(ch) {
    var lowers;
    lowers = "abcdefghijklmnopqrstuvwxyz";
    return (lowers.indexOf(ch) != -1);
}
///////////////////////////////////////////
function isUpper(ch) {
    var uppers;
    uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    return (uppers.indexOf(ch) != -1);
}
///////////////////////////////////////////
function isVowel(ch) {
    var vowels;
    vowels = "aeiouAEIOU";
    return (vowels.indexOf(ch) != -1);
}
///////////////////////////////////////////
function isConsonant(ch) {
    return (IsLetter(ch) && !IsVowel(ch));
}
///////////////////////////////////////////
function isWhitespace(ch){
    var white;
    white = " \t\n\r";
    return (white.indexOf(ch.toLowerCase()) != -1);
}
///////////////////////////////////////////
/*
Purpose: return true if the date is valid, false otherwise

Arguments: day integer representing day of month
month integer representing month of year
year integer representing year

Variables: dteDate - date object

*/
function isValidDate(dateStr,langCode){
var arr=dateStr.split(/-|\//);
if(arr.length!=3){
return false;
}
var day,month,year;
    switch(langCode){
    case 1:day=arr[0];month=arr[1]-1;year=arr[2];break;
    case 2:day=arr[2];month=arr[1]-1;year=arr[0];break;
    default:
        day=arr[0];month=arr[1]-1;year=arr[2];
    }
    day=parseInt(day);
    month=parseInt(month);
    year=parseInt(year);

    if(!isNumInRange(day,1,31) ||
       !isNumInRange(month,0,11) ||
       !isNumInRange(year,2000,2100) ){
        return false;
       }
var dteDate;
//set up a Date object based on the day, month and year arguments
//javascript months start at 0 (0-11 instead of 1-12)
dteDate=new Date(year,month,day);
/*
Javascript Dates are a little too forgiving and will change the date to a
reasonable guess if it's invalid. We'll use this to our advantage by creating
the date object and then comparing it to the details we put it. If the Date
object is different, then it must have been an invalid date to start with...
*/
//alert(month+"=="+dteDate.getMonth())
return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

/////////////////////////////////////////////////////////////////////////
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) and optionally,
  a valid country suffix.  Since email has many
  forms this expression only tests for near valid
  address.  Some additional validation may be
  required.
*************************************************/
function isValidEmail( strValue) {
var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  //check for valid email
  return objRegExp.test(strValue);
}
/////////////////////////////////////////////////////////////////////////
function isNumInRange(num,from,to){
    return num>=from && num<=to;
}
/////////////////////////////////////////////////////////////////////////
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
******************************************************************************/
function  isNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  //check for numeric characters
  return objRegExp.test(strValue);
}

