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 UsernameValidation(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) {
    var usernameFilter=/[\W_]/;
      if (usernameFilter.test(oField.value)) {
        alert ("Only alphanumeric characters are allow in \""+sLabel+"\" field.\n");
        oField.focus();
        oField.select();
        return (false);
      }
      else
      {
        return (true);
      }
  }
  else {
    return (false);
  }
}


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 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 != undefined) 
  {
    for (i=0; i<oField.length; i++) 
    {
      if (oField[i].checked) 
      {
        strCheckBoxValue += oField[i].value + ",";
      }
    }
  }
  else
  {
    if (oField.checked)
    {
      strCheckBoxValue += oField.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 ("Please select a check box in the \""+sLabel+"\" field.\n");
      
      if (isArray(oField))
        oField[0].focus();  
      else
        oField.focus();
      
      return (false);
  }
  return (true);
}
*/

function CheckBoxValidation2(oForm,sFieldName,sLabel) {
  var oField = eval("document."+oForm.name+"."+sFieldName);
  
  var strCheckBoxValue = GetCheckBoxValue(oForm,sFieldName); 
  
  if (strCheckBoxValue.length==0) {
      alert ("You need to read and accepted the Terms & Conditions before proceeding");
      
    try
    {
      oField[0].focus();  
    }
    catch(exception)
    {
      oField.focus();
    }
    return (false);
  }
  return (true);
}

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) {
  var monthName = new Array('','January','February','March','April','May','June','July','August','September','October','November','December');
  var datePat = /^(\d{2})(\/|-)(\d{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;
  }

  day   = matchArray[1]; // parse date into variables
  month = matchArray[3];
  year  = matchArray[5];

  if (day < 1 || day > 31) {
    alert("Day must be between 1 and 31.");
    return false;
  }

  if (month < 1 || month > 12) { // check month range
    alert("Month must be between 1 and 12.");
    return false;
  }

  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    alert("Month of "+monthName[month]+" doesn't have 31 days!")
    return false;
  }

  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
      alert("February " + year + " doesn't have " + day + " days!");
      return false;
    }
  }
  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)) {
        oField.focus();
        oField.select();
        return (false);
      }
      else {
        if (sSetFieldName.length>0) {
          setDate(oField.value, oForm, sSetFieldName);
        }
      }
    }
  }
  return (true);
}