//<script> /* For enabling the sytax highlighting while editing in interdev  */
///////////////////////////////////////////////////////////////////////////////

/*
 * File Name: FormValidator.js
 *      Functions for validating form fields.
 * ----------------------------------------------------------------------------     
 * Revision History
 * 
 * On 21/12/2002 by Jagadish Chandra R
 *      Created
 * ----------------------------------------------------------------------------     
 * Copyright(c) 2002 MindTree Consulting(Pvt) Ltd.
 */

/**
*function to select the multiselect 
*
*/

function validateMultiSelect(fieldName){
  var i = fieldName.options.length;
  var bValidSelection = false;
 for(j=0;j<i;j++){
  if(fieldName.options[j].selected){
    str = Trim(fieldName.options[j].value);
   
    if(str !=null && str != ""){
     bValidSelection = true;
     break;
    }
   }
  }
  return bValidSelection;  
 
 }

/**
 * Function to check whether a given value is empty or not
 * if empty return true else false
 * @param Value of the field to be checked
 */
 
function isEmptyField(fieldValue) {

    if (fieldValue == "") {
        return true;
    } else {
        /* Check for the space char */
        var noOfChars  = 0;
        for (var i = 0; i < fieldValue.length; i++){
            if (fieldValue.charAt(i) != " ") {
                noOfChars = 1;
            }
        }
        
        if (noOfChars == 0){
            return true;
        } else {
            return false;
        }
    }
}


/**
 * Function validate the given textarea control
 * 
 * @param control Textarea control to be validated
 * @param min Minimum number of characters to be present
 * @param max Maximum number of characters to be allowed
 * @param messages Messages to be displayed for each error condition
 * -------------------------------------------------------------------
 * Message mapping with the error condition
 *      messages[0] -  Field is mandatory and when no value is entered
 *      messages[1] -  No of characters entered is less than min
 *      messages[2] -  No of characters greater than max
 */

function validateTextArea(control, min, max, mandatory, messages) {
    /* Check whether the control is valid */
    if (control) {
        
        if (mandatory) { 
            /* Checking for valid entry */
            if (isEmptyField(control.value)) {
                
                if (messages[0] != "") { // Alert the message
                    alert(messages[0]);
                }
                /* Reset the value and focus the control */
                control.value = "";
                control.focus();
                
                return false;
            }
        }
        
        /* Check whether user has entered the minimum no of chars reqd. */
        if (min > 0) {
            if (control.value.length < min) {
                /* Alert the message */
                if(messages[1] != "") {
                    alert(messages[1]);
                }
                /* Focus the control */
                control.focus();
                
                return false;
            }
        }
        
        /* Check whether the character limit is exceeded */
        if (max > 0) {
            if (control.value.length > max) {
                
                if(messages[2] != "") {
                    alert(messages[2]);
                }
                /* Focus the control */
                control.focus();
                
                return false;
            }
        }
        
        return true;
    }
}


    function validateText(control ,min , max , mandatory , messages )
    {
        if(control)
        {
            if(mandatory == true)
            {
                if(isEmptyTextField(control.value))
                {
                    control.value="";
                    control.focus();

                    if(messages[0] != "")
                        alert(messages[0]);
                    return -1;
                }
            }

            if(min > 0)
            {
                if(control.value.length < min)
                {
                    control.focus();
                    if(messages[1] != "")
                        alert(messages[1]);
                    return -1;
                }
            }

            if(max > 0)
            {
                if(control.value.length>max)
                {
                    control.focus();
                    if(messages[2] != "")
                        alert(messages[2]);
                    return -1;
                }
            }
            return 1;
        }
    }

    function checkFloatText( text , name ,disablepopup)
    {
            var data = text;
            var floatValue = new Number(data);
            if(isNaN(floatValue))
            {
                valShowError('notfloat',name,'','','','',disablepopup);
                return false;
            }
            else
            {
                return true;
            }

    }

    function checkIntegerText( text , name ,disablepopup)
    {

            var data = new String(text);
            if(text!="") {

                if((data.charAt(1)<'0' || data.charAt(1) > '9') && (data.charAt(1) != '-' ))
                {
                    valShowError('notinteger',name,'','','','',disablepopup);
                    return false;
                }

                for(var i = 1 ; i < data.length; i++)
                {
                    if(data.charAt(i)<'0' || data.charAt(i) > '9')
                    {
                        valShowError('notinteger',name,'','','','',disablepopup);
                        return false;
                    }

                }
            }
            return true;
    }

    function valShowError(errno,fldname,value1,value2,value3,value4,disablepopup)
    {
        if(disablepopup !=1){
            switch(errno){
                case 'notfloat' :
                    alert(fldname +" must be a valid number");
                    break;
                case 'notinteger' :
                    alert(fldname +" must be a valid integer");
                    break;
            }
        }
    }

    function isEmptyTextField(Checkfield) {
        if (Checkfield == "") {
            return true;
        }
        else {
            nochar  = 0;
            for(var i=0; i < Checkfield.length; i++){
                if(Checkfield.charAt(i) != " ") {
                    nochar = 1;
                }
            }
            if (nochar == 0){
                return true;
            }
            else {
                return false;
            }
        }
    }

function CheckForm(form)
{
    var i;
    var arr;
    var str;
    var x;
    
    for (i=0;i<form.elements.length;i++)
    {
        if(form.elements[i].name.substr(0,4) == "CHK_")
        {           
            
            str = GetFieldValueByName(form,form.elements[i].name.substr(4));
            
            if(str != null)
            {
            
                str = Trim(str);
                            
                arr = form.elements[i].value.split("|");            
                
                switch(arr[0])
                {
                    case "STR":
                        //check mandatory field                         
                        if ((str.length == 0) && (arr[3] == "True"))
                        {
                            alert("The field >" + arr[4] + "< must be filled in");
                            return false;
                        }
                        
                        //check min length  
                        if (str.length < parseInt(arr[1]))
                        {
                            alert("The value in field >" + arr[4] + "< is too short");
                            return false;
                        }
                        
                        //check max length
                        if (str.length > parseInt(arr[2]))
                        {
                            alert("The value in field >" + arr[4] + "< is too long");
                            return false;
                        }
                        break;
                                            
                    case "INT":
                                                                
                        //check mandatory field
                        if ((str.length == 0) && (arr[3] == "True"))
                        {
                            alert("The field >" + arr[4] + "< must be filled in");
                            return false;
                        }
                        
                        //check sanity
                        if(!(IsNumeric(str)))
                        {
                            alert("The value in field >" + arr[4] + "< is numeric only");
                            return false;
                        }
                                                
                        //check min value
                        if (arr[1] != "x")
                        {
                            if (parseInt(str) < parseInt(arr[1]))
                            {
                                alert("The value in field >" + arr[4] + "< is too small");
                                return false;
                            }
                        }
                        
                        //check max length
                        if (arr[2] != "x")
                        {
                            if (parseInt(str) > parseInt(arr[2]))
                            {
                                alert("The value in field >" + arr[4] + "< is too big");
                                return false;
                            }
                        }
                        
                        break;
                    
                    case "FLOAT":
                                                                        
                        //check mandatory field
                        if ((str.length == 0) && (arr[3] == "True"))
                        {
                            alert("The field >" + arr[4] + "< must be filled in");
                            return false;
                        }
                        
                        //check sanity
                        if(!(IsNumeric(str)))
                        {
                            alert("The value in field >" + arr[4] + "< is numeric only");
                            return false;
                        }
                                                
                        //check min value
                        if (arr[1] != "x")
                        {
                            if (parseFloat(str) < parseFloat(arr[1]))
                            {
                                alert("The value in field >" + arr[4] + "< is too small");
                                return false;
                            }
                        }
                        
                        //check max length
                        if (arr[2] != "x")
                        {
                            if (parseFloat(str) > parseFloat(arr[2]))
                            {
                                alert("The value in field >" + arr[4] + "< is too big");
                                return false;
                            }
                        }
                        
                        break;
                                        
                        
                    case "DATE":
                        
                        //check mandatory field
                        if ((str.length == 0) && (arr[3] == "True"))
                        {
                            alert("The field >" + arr[4] + "< must be filled in");
                            return false;
                        }
                        
                        //check sanity
                        if(!(IsDate(str)))
                        {
                            alert("The value in field >" + arr[4] + "< is not a valid date (MM/DD/YYYY)");
                            return false;
                        }
                        
                        //check min value
                        if (arr[1] != "x")
                        {
                            if (GetDate(str) < GetDate(arr[1]))
                            {
                                alert("The date in field >" + arr[4] + "< is less than the minimum date");
                                return false;
                            }
                        }
                        
                        //check max length
                        if (arr[2] != "x")
                        {
                            if (GetDate(str) > GetDate(arr[2]))
                            {
                                alert("The value in field >" + arr[4] + "< is later than the maximum date");
                                return false;
                            }
                        }
                        
                        break;
                }
            }
        }
    }
    
    return true;

}

//============================================================================================

function Replace(string,replacechar,replacewith) 
{
    var temp = "";
    var i;
    
    string = '' + string;
    splitstring = string.split(replacechar);
    for(i = 0; i < splitstring.length; i++)
    {
        if(i < (splitstring.length -1))
            temp += splitstring[i] + replacewith;
        else
            temp += splitstring[i];
    }
    return temp;
}

//============================================================================================

function GetFieldValueByName(form,name)
{
    var i;
    
    for (i=0;i<form.elements.length;i++)
    {
        if(form.elements[i].name == name)
            return (form.elements[i].value);
    }
    return null;
}

/*

======================= STANDARD HELPER FUNCTIONS BELOW =======================================

*/
function IsNumeric(str)
{
    var i;
    for(i=0;i<str.length;i++)
    {
        if(!(   ((str.charAt(i) <= '9') && (str.charAt(i) >= '0')) ||
                (str.charAt(i) == ' ') || (str.charAt(i) == '.') || (str.charAt(i) == '-')))
            return(false);
    }
    return(true);
}
//============================================================================================
function IsPositiveNumber(str)
{
    if(IsNumeric(str)){
        if(str<0){
            return(false);  
        }else{
            return(true);
        }
    }else{
        return(false);
    }   
        
}

//============================================================================================

function IsAlphaNumeric(str)
{
    var i;
    for(i=0;i<str.length;i++)
    {
        if(!(   ((str.charAt(i) <= '9') && (str.charAt(i) >= '0')) ||
                ((str.charAt(i) <= 'z') && (str.charAt(i) >= 'a')) ||
                (str.charAt(i) == ' ') || (str.charAt(i) == '-') ||
                (str.charAt(i) == 'ü') || (str.charAt(i) == 'ä') ||
                (str.charAt(i) == 'ö') || (str.charAt(i) == 'Ü') ||
                (str.charAt(i) == 'Ä') || (str.charAt(i) == 'Ö') ||
                (str.charAt(i) == 'é') || (str.charAt(i) == 'ß') ||
                ((str.charAt(i) <= 'Z') && (str.charAt(i) >= 'A')) ))
            return(false);
    }
    return(true);
}

//============================================================================================

function IsAlpha(str)
{
    var i;
    
    for(i=0;i<str.length;i++)
    {
        if(!(   ((str.charAt(i) <= 'z') && (str.charAt(i) >= 'a')) ||
                (str.charAt(i) == ' ') || (str.charAt(i) == '-') ||
                (str.charAt(i) == 'ü') || (str.charAt(i) == 'ä') ||
                (str.charAt(i) == 'ö') || (str.charAt(i) == 'Ü') ||
                (str.charAt(i) == 'Ä') || (str.charAt(i) == 'Ö') ||
                (str.charAt(i) == 'é') || (str.charAt(i) == 'ß') ||
                ((str.charAt(i) <= 'Z') && (str.charAt(i) >= 'A')) ))
            return(false);
    }
    return(true);
}

//============================================================================================

function Trim(strTrim)
{
   if(strTrim != null){
    //trim leding spaces
    while(true)
    {
        
          if(strTrim.charAt(0) == ' ')
            strTrim = strTrim.substr(1);
        else
            break;
    }
    
    //trim trailing spaces
    while(true)
    {
        if(strTrim.charAt(strTrim.length-1) == ' ')
            strTrim = strTrim.substr(0,strTrim.length-1);
        else
            break;
    }
   } 
    return(strTrim);    
}

//============================================================================================

function IsDate(argDate)
{
    var date_split;
    var i;
    var tdate, tmonth, tyear;
    
    date_split = argDate.split('/');
    
    //check for date parts
    if(date_split.length != 3)
        return(false);
        
    //check for zero values
    for(i=0;i<date_split.length;i++)
    {
        if(parseInt(date_split[i]) == 0)
            return(false);
    }
    
    //check for 4-digit year
    if(date_split[2].length != 4)
        return(false);
        
    //check for valid date, e.g. 02/29/1997
    tdate = parseInt(date_split[1]);
    tmonth = parseInt(date_split[0]);
    tyear = parseInt(date_split[2]);
    
    var date = new Date(parseInt(date_split[2]),parseInt(date_split[0])-1,parseInt(date_split[1]));
    
    if(date.getDate() != tdate)
        return(false);
    
    if(date.getMonth() != (tmonth-1))
        return(false);
    
    if(date.getFullYear() != tyear)
        return(false);
        
    return(true);
}

//function to validate the date format MON-YYYY
               
               function checkYOM(num){
                    
                    if (num!= "") {
                    
                        var data = new String(num);
                        
                        //find out whether '-' is present in the string
                        if(data.indexOf('-') == -1){
                            return false;
                        }
                        
                        //separate the month and year
                        var number = data.split('-');
                            
                            //remove the white spaces
                            var month = Trim(number[0]).toUpperCase();
                            var year = Trim(number[1].toUpperCase());
                            
                        
                            //check for the month
                            if(month=="JAN" || month=="FEB"|| month=="MAR" || month=="APR" || 
                                month=="MAY" || month=="JUN" || month=="JUL" || month=="AUG" || month=="SEP" ||
                                month=="OCT" || month=="NOV" || month=="DEC"){
                                
                               if(year.length == 4){
                                for(var i=0;i < 4;i++){
                        
                                    if(year.charAt(i)<'0' || year.charAt(i) > '9'){
                                        return false;
                                    }
                                }
                               }else{
                                    return false;   
                               }    
                            }else{
                                return false;
                            }
                    }   
            
            return true;
       }



function GetDate(argDate)
{
    //use IsDate() first !!!!
    
    var date_split; 
    var tdate, tmonth, tyear;
    
    date_split = argDate.split('/');
    
        
    
    tdate = parseInt(date_split[1]);
    tmonth = parseInt(date_split[0]);
    tyear = parseInt(date_split[2]);
    
    return date = new Date(parseInt(date_split[2]),parseInt(date_split[0])-1,parseInt(date_split[1]));
        
}

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
    result = true;
  }
  return result;
}

///////////////////////////////////////////////////////////////////////////////
//</script> /* For enabling the sytax highlighting while editing in interdev */