/*******************************************************************/
/*******************************************************************/
/*

JsClientUtils Version 1.2

# Updated isDate.

*/
/*******************************************************************/
/*******************************************************************/
function trim(inString) {
  var outString;
  var frontIndex = 0;
  var backIndex  = inString.length - 1;        
        
  while (inString.charAt( frontIndex ) == ' ' || inString.charAt( frontIndex ) == '\t' || inString.charAt( frontIndex ) == '\n' || inString.charAt( frontIndex ) == '\r') {
    frontIndex++;
  }

  while (inString.charAt( backIndex ) == ' ' || inString.charAt( backIndex ) == '\t' || inString.charAt( backIndex ) == '\n' || inString.charAt( backIndex ) == '\r' ) {
    backIndex--;
  }

  if (frontIndex==inString.length) {
    outString = '';
  }
  else {
    outString = inString.substring( frontIndex, (backIndex + 1) );
  }
  return outString;
}
/*******************************************************************/
function isEmail(strng) {
// \w  Matches any word character including underscore. Equivalent to [A-Za-z0-9_]
  var emailFilter=/^\w[\w\.-]*@\w[\w\.-]+\.[a-z]{2,4}$/;
  if (!(emailFilter.test(strng))) {
    return (false);
  }
  else {
    //test email for illegal characters: ( ) < > [ ] , ; : \ /
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    if (strng.match(illegalChars)) {
      return (false);
    }
  }
  return (true);
}
/*******************************************************************/
function PasswordValidation(oForm,sFieldName,sLabel,iMinLength,iMaxLength,bRequired) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  var oField1 = eval("document."+oForm.name+"."+sFieldName+"1");
/*
  alert("oField.name : " + oField.name +
        "\noField1.name : " + oField1.name +
        "\noField.value : " + oField.value +
        "\noField1.value : " + oField1.value);
*/
  var illegalChars = /[\W_]/; // allow only letters and numbers

  if ((oField.value.length==0) && (bRequired==true)) {
    alert ("Please enter a value for the \""+sLabel+"\" field.\n");
    oField.focus();
    oField.select();
    return (false);
  }
  else {
    if (oField.value.length>0) {
      if ((iMinLength>0) && (oField.value.length<iMinLength)) {
        alert ("Please enter at least "+iMinLength+" characters in the \""+sLabel+"\" field.\n");
        oField.focus();
        oField.select();
        return (false);
      }
      if ((iMaxLength>0) && (oField.value.length>iMaxLength)) {
        alert ("Please enter at most "+iMaxLength+" characters in the \""+sLabel+"\" field.\n");
        oField.focus();
        oField.select();
        return (false);
      }
      if (oField.value!=oField1.value) {
        alert ("Please retype your password\nBoth passwords do not match.\n");
        oField1.value = "";
        oField1.focus();
        oField1.select();
        return (false);
      }
    }
    else if (illegalChars.test(strng)) {
      alert ("Please retype your password.\nThe password contains illegal characters.\n");
      oField.value = "";
      oField1.value = "";
      oField.focus();
      oField.select();
      return (false);
    }
  }
  return (true);
}
/*******************************************************************/
function TextValidation(oForm,sFieldName,sLabel,iMinLength,iMaxLength,bRequired) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  oField.value = trim(oField.value);

  if ((oField.value.length==0) && (bRequired==true)) {
    alert ("Please enter a value for the \""+sLabel+"\" field.\n");
    oField.focus();
    oField.select();
    return (false);
  }
  else {
    if (oField.value.length>0) {
      if ((iMinLength>0) && (oField.value.length<iMinLength)) {
        alert ("Please enter at least "+iMinLength+" characters in the \""+sLabel+"\" field.\n");
        oField.focus();
        oField.select();
        return (false);
      }
      if ((iMaxLength>0) && (oField.value.length>iMaxLength)) {
        alert ("Please enter at most "+iMaxLength+" characters in the \""+sLabel+"\" field.\n");
        oField.focus();
        oField.select();
        return (false);
      }
    }
  }
  return (true);
}
/*******************************************************************/
function EmailValidation(oForm,sFieldName,sLabel,bRequired) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  oField.value = trim(oField.value);
  oField.value = oField.value.toLowerCase();
  if ((oField.value.length==0) && (bRequired==true)) {
    alert ("Please enter a value for the \""+sLabel+"\" field.\n");
    oField.focus();
    oField.select();
    return (false);
  }
  else {
    if (oField.value.length>0) {
      if (!isEmail(oField.value)) {
        alert ("Please enter a valid email address in the \""+sLabel+"\" field.\n");
        oField.focus();
        oField.select();
        return (false);
      }
    }
  }
  return (true);
}
/*******************************************************************/
function DropdownValidation(oForm,sFieldName,sLabel) {
  var oField = eval("document."+oForm.name+"."+sFieldName);

  if (oField.selectedIndex==0) {
      alert ("Please select an option in the \""+sLabel+"\" field.\n");
      oField.focus();
      return (false);
  }
  return (true);
}
/*******************************************************************/
function NumberValidation(oForm,sFieldName,sLabel,iMinLength,iMaxLength,bRequired) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  oField.value = trim(oField.value);

  if (TextValidation(oForm,sFieldName,sLabel,iMinLength,iMaxLength,bRequired)==true) {
    if (isNumeric(oField.value)==true) {
      return (true);
    }
    else {
      alert ("Please enter numeric values in the \""+sLabel+"\" field.\n");
      oField.focus();
      oField.select();
      return (false);
    }
  }
  else {
    return (false);
  }
}
/*******************************************************************/
function NumberValidation2(oForm,sFieldName,sLabel,iMin,iMax,bRequired) {
    var oField = eval("document."+oForm.name+"."+sFieldName);
    oField.value = trim(oField.value);

    if ((oField.value.length>0) || (bRequired==true)) {
        if (isNumeric(oField.value)==true) {
            if (((parseInt(oField.value))>=(parseInt(iMin))) &&
                ((parseInt(oField.value))<=(parseInt(iMax))) ) {
                return (true);
            } else {
                alert ("Please enter a value from "+iMin+" to "+iMax+" in the \""+sLabel+"\" field.\n");
                oField.focus();
                oField.select();
                return (false);
            }
        } else {
            alert ("Please enter numeric values in the \""+sLabel+"\" field.\n");
            oField.focus();
            oField.select();
            return (false);
        }
    } else {
        return(true);
    }
}
/*******************************************************************/
function DecimalNumberValidation(oForm,sFieldName,sLabel,iMinLength,iMaxLength,bRequired) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  oField.value = trim(oField.value);

  if (TextValidation(oForm,sFieldName,sLabel,iMinLength,iMaxLength,bRequired)==true) {
    if (!(isNaN(oField.value))) {
      return (true);
    }
    else {
      alert ("Please enter numeric values in the \""+sLabel+"\" field.\n");
      oField.focus();
      oField.select();
      return (false);
    }
  }
  else {
    return (false);
  }
}
/*******************************************************************/
function isNumeric(strng) {
  var illegalChars = /[^0-9]/;
  if (illegalChars.test(strng)) {
      return (false);
  }
  else {
    return (true);
  }
}
/*******************************************************************/
function YearValidation(oForm,sFieldName,sLabel,iMinYear,iMaxYear,bRequired) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  oField.value = trim(oField.value);

  if (NumberValidation(oForm,sFieldName,sLabel,4,4,true)==true) {
    if (oField.value<=iMinYear) {
      alert ("Please enter a value more then "+iMinYear+" in the \""+sLabel+"\" field.\n");
      oField.focus();
      oField.select();
      return (false);
    }
    if (oField.value>iMaxYear) {
      alert ("Please enter a value less then "+iMaxYear+" in the \""+sLabel+"\" field.\n");
      oField.focus();
      oField.select();
      return (false);
    }
    return (true);
  }
}
/*******************************************************************/
function DropDownValidate(oForm,sFieldName,sLabel) {
    var oField = eval("document."+oForm.name+"."+sFieldName);
    oField.value = trim(oField.value);
    if (oField.value==0) {
        alert ("Please select a \""+sLabel+"\".\n");
        oField.focus();
        return (false);
    }
    return(true);
}

/*******************************************************************/
// function to count the number of words in the textarea
function wordCounter(field,cntfield,maxlimit) {
    var NumOfWords
    NumOfWords=CountWords(field);
    if ((NumOfWords)<=maxlimit) {
        cntfield.value=maxlimit-NumOfWords
    } else {
        field.value = field.value.substring(0, (field.value.length-1));
    }
}
/*******************************************************************/
function CountWords (this_field) {
    var fullStr = this_field.value + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length -1;
    if (fullStr.length <2) {
        word_count = 0;
    }
    return word_count;
}
/*******************************************************************/
function textCounter(field,cntfield,maxlimit) {
    if (field.value.length > maxlimit) {
        // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    } else {
        // otherwise, update 'characters left' counter
        cntfield.value = maxlimit - field.value.length;
    }
}
/*******************************************************************/
function GetRadioButtonValue(oForm,sFieldName) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  var strRadioButtonValue = "";

  for (i=0; i<oField.length; i++) {
    if (oField[i].checked) {
      strRadioButtonValue = oField[i].value;
      break;
    }
  }
  return strRadioButtonValue;
}
/*******************************************************************/
function RadioButtonValidation(oForm,sFieldName,sLabel) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  var strRadioButtonValue = GetRadioButtonValue(oForm,sFieldName);

  if (strRadioButtonValue.length==0) {
      alert ("Please select the \""+sLabel+"\" field.\n");
      oField[0].focus();
      return (false);
  }
  return (true);
}
/*******************************************************************/
function GetCheckBoxValue(oForm,sFieldName) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  var strCheckBoxValue = "";
  
  if (!(oField.length))
  {
    if (oField.checked) {
            strCheckBoxValue += oField.value + ",";
      }
  }
  else
  {
    for (i=0; i<oField.length; i++) {
      if (oField[i].checked) {
        strCheckBoxValue += oField[i].value + ",";
      }
    }  
  }
  
  if (strCheckBoxValue.length>0) {
    if (strCheckBoxValue.substr((strCheckBoxValue.length-1),(strCheckBoxValue.length-1))==",") {
      strCheckBoxValue = strCheckBoxValue.substr(0,(strCheckBoxValue.length-1));
    }
  }
  return strCheckBoxValue;
}
/*******************************************************************/
function CheckBoxValidation(oForm,sFieldName,sLabel) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  var strCheckBoxValue = GetCheckBoxValue(oForm,sFieldName); 
  if (strCheckBoxValue.length==0) {
    
    alert(setCheckBoxAlertDisplay(sLabel));
    
    try
    {
      oField[0].focus();  
    }
    catch(exception)
    {
      oField.focus();
    }
    
    return (false);
  }
    
  return true;
}
/*******************************************************************/
function setCheckBoxAlertDisplay(sLabel)
{
  var illegalChars = /^.+\]$/;
      
  if (sLabel.match(illegalChars)) 
  {
     sLabel = sLabel.substring(1,(sLabel.length - 1))
     return sLabel+"\n";
  }
  else
  {
    return "Please select a check box in the \""+sLabel+"\" field.\n";
  }
}
/*******************************************************************/
function GetDropdownValue(oForm,sFieldName) {

  var oField = eval("document."+oForm.name+"."+sFieldName);
  return oField.options[oField.selectedIndex].value;

}
/*******************************************************************/
function GetTextValue(oForm,sFieldName) {

  var oField = eval("document."+oForm.name+"."+sFieldName);
  return oField.value;

}
/*******************************************************************/
function y2k(number) {
  return (number < 1000) ? number + 1900 : number;
}
/*******************************************************************/
function isDate(strDate,oForm,sFieldName) {
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = strDate.match(datePat); // is the format ok?
  
  if (matchArray == null)
  {
    alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
    return false;
  }
  
  var monthName = new
Array('','January','February','March','April','May','June','July','August','September','October','November','December');
  var monthShortName = new
Array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  //var datePat = /^(\d{1,2})(\/|-)((\d{1,2})|(jan)|(feb)|(mar)|(apr)|(may)|(jun)|(jul)|(aug)|(sep)|(oct)|(nov)|(dec))(\/|-)(\d{4})$/i;
  //var matchArray = strDate.match(datePat); // is the format ok?
  var pattern = /\/|-/;
  var dateArray = strDate.split(pattern);

  if (dateArray == null) {
    alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
    return false;
  }

  if (dateArray.length != 3)
  {
    alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
    return false;
  }

  strDay   = dateArray[0]; // parse date into variables
  strMonth = dateArray[1];
  strYear  = dateArray[2];
  
  
// alert('strDay: '+strDay+' ('+strDay.length+')\nstrMonth: '+strMonth+'('+strMonth.length+')\nstrYear: '+strYear+' ('+strYear.length+')');

  intDay = parseInt(strDay, 10);
  intMonth = parseInt(strMonth, 10);
  intYear = parseInt(strYear, 10);

// alert('intDay: '+intDay+'\nintMonth: '+intMonth+'\nintYear: '+intYear);

  if (intDay < 1 || intDay > 31 || isNaN(strDay)) {
    alert("Day must be between 1 and 31.");
    return false;
  }

  if(isNaN(intMonth))
  {
    var booMonthValid = false;
    for(i=0; i<monthShortName.length; i++)
    {
      if ( strMonth.toLowerCase()==monthShortName[i].toLowerCase() )
      {
        intMonth = i;
        booMonthValid = true;
      }
    }

    if (!booMonthValid)
    {// check month range
      alert("Month must be between Jan and Dec.");
      return false;
    }
  }
  else
  {
    if (intMonth < 1 || intMonth > 12)
    { // check month range
      alert("Month must be between 1 and 12.");
      return false;
    }
  }

  if (isNaN(strYear)) {
    alert("Invalid Year Format.");
    return false;
  }

// alert('intDay: '+intDay+'\nintMonth: '+intMonth+'\nintYear: '+intYear);

  if ((intMonth==4 || intMonth==6 || intMonth==9 || intMonth==11) &&
intDay==31)
  {
    alert("Month of "+monthName[intMonth]+" doesn't have 31 days!")
    return false;
  }

  if (intMonth == 2)
  { // check for february 29th
    var isleap = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400
== 0));
    if (intDay > 29 || (intDay==29 && !isleap)) {
      alert("February " + intYear + " doesn't have " + intDay + " days!");
      return false;
    }
  }
  
  var oDay   = eval("document."+oForm.name+".d_"+sFieldName);
  var oMonth = eval("document."+oForm.name+".m_"+sFieldName);
  var oYear  = eval("document."+oForm.name+".y_"+sFieldName);

  oDay.value = intDay
  oMonth.value = intMonth
  oYear.value = intYear

//alert('isDate: true');
  return true; // date is valid
}

/*******************************************************************/
function setDate(strDate,oForm,sFieldName) {
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = strDate.match(datePat); // is the format ok?
    
  var oDay   = eval("document."+oForm.name+".d_"+sFieldName);
  var oMonth = eval("document."+oForm.name+".m_"+sFieldName);
  var oYear  = eval("document."+oForm.name+".y_"+sFieldName);
  
  oDay.value   = matchArray[1]; // parse date into variables
  oMonth.value = matchArray[3];
  oYear.value  = matchArray[5];
}
/*******************************************************************/
function DateValidation(oForm,sFieldName,sLabel,sSetFieldName,bRequired) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  oField.value = trim(oField.value);

  if ((oField.value.length==0) && (bRequired==true)) {
    alert ("Please enter a value for the \""+sLabel+"\" field.\n [dd-mm-yyyy]");
    oField.focus();
    oField.select();
    return (false);
  }
  else {
    if (oField.value.length>0) {
      if (!isDate(oField.value, oForm, sSetFieldName)) {
        oField.focus();
        oField.select();
        return (false);
      }
      //else {
      //  if (sSetFieldName.length>0) {
      //    setDate(oField.value, oForm, sSetFieldName);
      //  }
      //}
    }
  }
  return (true);
}
/*******************************************************************/