/*
PROGRAM ID: subValidation.js
DESCRIPTION: Javascript functions to validate user input.
AUTHOR: Simon Chan
DATE: 20/07/2001
VERSION NO: 1.0 
PARAMETERS: NA
RETURN: NA
COMMENTS: 
---------------------------------------------------------------------------------------
AUTHOR: Carolyn Cheng  
DATE: 21/10/2003
VERSION NO: 1.1
CHANGES: added validate() function
---------------------------------------------------------------------------------------
AUTHOR: lifeng
DATE: 08/05/04
VERSION NO: 1.2
CHANGES: added isUserNPassword() function
	added isSourceOfInfo() function
---------------------------------------------------------------------------------------
AUTHOR		: Jun Yong
DATE		: 23-12-2008
VERSION NO	: 1.3
CR NO		: D-UORE-2008-027
CHANGES		: Changed the "Company Registration Number" to "Unique Entity Number (UEN)".
---------------------------------------------------------------------------------------
*/

<!--
function isInteger(theData){
  if (isNaN(parseInt(theData))) return false
  else  return true
}
function isLetterNotSpace(theChar){
  //Blank space is considered a character
  var charArray = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
                            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
                            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 
                            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
                            'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
                            's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
  for (j = 0; j < charArray.length; j++)
    if (theChar == charArray[j])  return true
  return false
}
function isLetter(theChar){
  //Blank space is considered a character
  var charArray = new Array(' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
                            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
                            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 
                            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
                            'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
                            's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
  for (j = 0; j < charArray.length; j++)
    if (theChar == charArray[j])  return true
  return false
}
function isDigit(theDigit){
  var digitArray = new Array('0','1','2','3','4','5','6','7','8','9')
  for (j = 0; j < digitArray.length; j++)
  if (theDigit == digitArray[j])  return true
  return false
}
function isAlphaNumeric(theChar){
  if ( !isDigit(theChar) )
    if ( !isLetter(theChar) ) return false
  return true
} 
function isAlphaNumericNotSpace(theChar){
  if ( !isDigit(theChar) )
    if ( !isLetterNotSpace(theChar) ) return false
  return true
}
function isMaxLength(aString, maxLength){
	if (aString.length <= maxLength)  return true
   else return false
}
function isMinLength(aString, minLength){
	if (aString.length >= minLength)  return true
   else return false
}
function isLength(aString, aLength){
   if (isMaxLength(aString, aLength) && isMinLength(aString, aLength))  return true
   else return false
}
function trim(strText) { 
  // this will get rid of leading spaces 
  while (strText.substring(0,1) == ' ') 
    strText = strText.substring(1, strText.length);
  // this will get rid of trailing spaces 
  while (strText.substring(strText.length-1,strText.length) == ' ')
    strText = strText.substring(0, strText.length-1);
  return strText;
} 
function isAcctType(obj){ 
  for (var idx=0; idx<obj.length; idx++){
    if (obj[idx].checked)  
      return true;
  }
  errmsg = errmsg + 'Missing account type.\n';  
  return false;
}
function isUsername(str){
  if (str.length == 0){
    errmsg = errmsg + 'Missing username.\n';  
    return false;
  }
  else{
    for (var i = 0; i < str.length; i++)
    {
      var theChar = str.charAt(i)
      //is it not a character or a digit?
      if ( !isAlphaNumericNotSpace(theChar) ){
        errmsg = errmsg + 'Invalid username. Only alphabets/digits are allowed.\n';  
        return false;          
      }
    }
    if (!isMinLength(str, 8)){
      errmsg = errmsg + 'Invalid username. Minimum 8 characters required.\n';  
      return false;            
    }
    else
      return true;
  }
}
function isPassword(str1, str2){
  if (str1.length == 0){
    errmsg = errmsg + 'Missing password.\n';  
    return false;
  }
  else{
    if (!isMinLength(str1, 8)){
      errmsg = errmsg + 'Invalid password. Minimum 8 characters required.\n';  
      return false            
    }
    else{
      if (str1 == str2)      
        return true;
      else{
        errmsg = errmsg + 'Passwords do not match.\n';  
        return false;
      }
    }
  }
}

function isUserNPassword(str1,str2)
{
	if (str1 == str2)   
	{   
		errmsg = errmsg + 'Please ensure that your userid and password are different.\n';  
		return false;
	}
	else
		return true;
}

function isGivenName(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing given name.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i)
      if ( !isLetter(theChar) ) {
        if ( !( (theChar == '-') || (theChar == ',') ) ){
          errmsg = errmsg + 'Invalid given name. Only alpahbets/spaces are allowed.\n';  
          return false            
        }
      } 
    }
    return true;
  }
}
function isFamilyName(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing family name.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i)
      if ( !isLetter(theChar) ) {
        if ( !( (theChar == '-') || (theChar == ',') ) ){
          errmsg = errmsg + 'Invalid family name. Only alpahbets/spaces are allowed.\n';  
          return false            
        }
      } 
    }
    return true;
  }
}
function isNric(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing identity number.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i)
      if ( !isAlphaNumeric(theChar) ) {
        if ( !((theChar == '-') ) ){
          errmsg = errmsg + 'Invalid identity number. Only alpahbets/digits/spaces are allowed.\n';  
          return false            
        }
      } 
    }
    return true;
  }
}
function isMailAddress(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing mailing address.\n';  
    return false;
  }
  else{
    if (!isMaxLength(str, 250)){
      errmsg = errmsg + 'Invalid mailing address. Exceeded maximun of 250 characters.\n';  
      return false            
    }
    else
    {
      var theChar;
      for (var i = 0; i < str.length; i++){
        theChar = str.charAt(i);
        if ( !isAlphaNumeric(theChar) ) {
          // '%0D%0A' -- newline 
          if ( !((escape(theChar) == '%0D' ) || (escape(theChar) == '%0A' ) || (theChar == '/' ) || (theChar == "\\" ) || (theChar == '(' ) || (theChar == ')' ) || (theChar == '#' ) || (theChar == '.' ) || (theChar == ',' ) || (theChar == "'" ) || (theChar == '-' ) ) ){
            errmsg = errmsg + 'Invalid mailing address. Only alpahbets/digits/spaces are allowed.\n';  
            return false            
          }
        } 
      }
      return true;
    }
  }
}
function isPostalCode(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing postal code.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i);
      if ( !isAlphaNumeric(theChar) ) {
        if ( !( (theChar == '-') ) ){
          errmsg = errmsg + 'Invalid postal code. Only alpahbets/digits/spaces are allowed.\n';  
          return false            
        }
      } 
    }
    return true;
  }
}
function isSourceOfInfo(str){ 
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing Source Of Info.\n';  
    return false;
  }
	  return true;
}

function isCountry(str){ 
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing country.\n';  
    return false;
  }
  return true;
}
function isBusiness(str){ 
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing business.\n';  
    return false;
  }
  return true;
}
function isProfession(str){ 
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing profession.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i)
      if ( !isAlphaNumeric(theChar) ) {
        errmsg = errmsg + 'Invalid profession. Only alpahbets/digits are allowed.\n';  
        return false            
      } 
    }
    return true;
  }
}
function isEmail(str) {
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing email address.\n';  
    return false;
  }
  else{
    if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
      return true;
    else{
      errmsg = errmsg + 'Invalid email address. Allowed format is user@domain.com\n';  
      return false;
    }
  }
}
function isPhone(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing phone number.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i);
      if ( !isDigit(theChar) ) {
        if ( !((theChar == ' ') || (theChar == '(') || (theChar == ')') || (theChar == '-')) ){
          errmsg = errmsg + 'Invalid phone number. Only digits/spaces are allowed.\n';  
          return false            
        }
      } 
    }
    return true;
  }
}
function isFax(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing fax number.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i);
      if ( !isDigit(theChar) ) {
        if ( !((theChar == ' ') || (theChar == '(') || (theChar == ')') || (theChar == '-')) ){
          errmsg = errmsg + 'Invalid fax number. Only digits/spaces are allowed.\n';  
          return false            
        }
      } 
    }
    return true;
  }
}
function isCompany(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing company name.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i)
      if ( !isAlphaNumeric(theChar) ) {
        if ( !((theChar == '.' ) || (theChar == ',' ) || (theChar == "'" ) || (theChar == '-' ) ) ){
          errmsg = errmsg + 'Invalid company name. Only alpahbets/digits/spaces are allowed.\n';  
          return false            
        }
      } 
    }
    return true;
  }
}
function isRoc(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    //errmsg = errmsg + 'Missing roc number.\n';
	errmsg = errmsg + 'Missing UEN number.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i)
      if ( !isAlphaNumeric(theChar) ) {
        //errmsg = errmsg + 'Invalid roc number. Only alpahbets/digits are allowed.\n';  
		errmsg = errmsg + 'Invalid UEN number. Only alpahbets/digits are allowed.\n';
        return false            
      } 
    }
    return true;
  }
}
function isOfficeAddress(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing office address.\n';  
    return false;
  }
  else{
    if (!isMaxLength(str, 250)){
      errmsg = errmsg + 'Invalid office address. Exceeded maximun of 250 characters.\n';  
      return false            
    }
    else
    {
      var theChar;
      for (var i = 0; i < str.length; i++){
        theChar = str.charAt(i);
        if ( !isAlphaNumeric(theChar) ) {
          // '%0D%0A' -- newline 
          if ( !((escape(theChar) == '%0D' ) || (escape(theChar) == '%0A' ) || (theChar == '/' ) || (theChar == "\\" ) || (theChar == '(' ) || (theChar == ')' ) || (theChar == '#' ) || (theChar == '.' ) || (theChar == ',' ) || (theChar == "'" ) || (theChar == '-' ) ) ){
            errmsg = errmsg + 'Invalid office address. Only alpahbets/digits/spaces are allowed.\n';  
            return false            
          }
        } 
      }
      return true;
    }
  }
}
function isDesignation(str){
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing designation.\n';  
    return false;
  }
  else{
    var theChar;
    for (var i = 0; i < str.length; i++){
      theChar = str.charAt(i)
      if ( !isAlphaNumeric(theChar) ) {
        errmsg = errmsg + 'Invalid designation. Only alpahbets/digits are allowed.\n';  
        return false            
      } 
    }
    return true;
  }
}
function isNumAcct(str){ 
  var tmpStr = trim(str);
  if (tmpStr.length == 0){
    errmsg = errmsg + 'Missing number of account(s).\n';  
    return false;
  }
  return true;
}
function isProduct(obj){ 
  for (var idx=0; idx<obj.length; idx++){
    if (obj[idx].checked)  
      return true;
  }
  errmsg = errmsg + 'Missing subscription plan.\n';  
  return false;
}

var errmsg;
function validateIndividualDetails(){
  errmsg = 'Error(s) were found on the Subscription form\n\n';
  with (document.frmSubDetails){
    var proceed = true;
    if (!isGivenName(GivenName.value))  proceed = false;
    if (!isFamilyName(FamilyName.value))  proceed = false;
    if (!isNric(Nric.value))  proceed = false;
    if (!isMailAddress(MailAddress.value))  proceed = false;
		if (!isUserNPassword(Username.value, Password.value))  proceed = false;
		
    //if (!isPostalCode(PostalCode.value))  proceed = false;
    if (!isSourceOfInfo(SourceOfInfo.options[SourceOfInfo.selectedIndex].value))  proceed = false;
    if (!isCountry(Country.options[Country.selectedIndex].value))  proceed = false;
    if (trim(Profession.value).length > 0 && !isProfession(Profession.value))  proceed = false;
    if (!isEmail(Email.value))  proceed = false;
    if (!isPhone(Phone.value))  proceed = false;
    //if (trim(Fax.value).length > 0 && !isFax(Fax.value))  proceed = false;
   // if (!isNumAcct(NumberOfAcct.options[NumberOfAcct.selectedIndex].value))  proceed = false;
    //if (!isProduct(Product))  proceed = false;
    if (!isUsername(Username.value))  proceed = false;
    if (!isPassword(Password.value, ConfirmPassword.value))   proceed = false;
    if (!proceed)
      alert(errmsg);
		return proceed;
  }
}

function validateIndividualDetails_prem(){
  errmsg = 'Error(s) were found on the Subscription form\n\n';
  with (document.frmSubDetails){
    var proceed = true;
    if (!isGivenName(GivenName.value))  proceed = false;
    if (!isFamilyName(FamilyName.value))  proceed = false;
    if (!isNric(Nric.value))  proceed = false;
    if (!isMailAddress(MailAddress.value))  proceed = false;
		if (!isUserNPassword(Username.value, Password.value))  proceed = false;
		
    //if (!isPostalCode(PostalCode.value))  proceed = false;
    if (!isCountry(Country.options[Country.selectedIndex].value))  proceed = false;
    if (trim(Profession.value).length > 0 && !isProfession(Profession.value))  proceed = false;
    if (!isEmail(Email.value))  proceed = false;
    if (!isPhone(Phone.value))  proceed = false;
    if (trim(Fax.value).length > 0 && !isFax(Fax.value))  proceed = false;
   	if (!isNumAcct(NumberOfAcct.options[NumberOfAcct.selectedIndex].value))  proceed = false;
    //if (!isProduct(Product))  proceed = false;
    if (!isUsername(Username.value))  proceed = false;
    if (!isPassword(Password.value, ConfirmPassword.value))   proceed = false;
    if (!proceed)
      alert(errmsg);
		return proceed;
  }
}
function validateCorporateDetails(){
  errmsg = 'Error(s) were found on the Subscription form\n\n';
  with (document.frmSubDetails){
    var proceed = true;
    if (!isCompany(Company.value))   proceed = false;
    if (!isRoc(Roc.value))   proceed = false;
    if (!isOfficeAddress(OfficeAddress.value))  proceed = false;
    if (trim(MailAddress.value).length > 0 && !isMailAddress(MailAddress.value))  proceed = false;
    //if (!isPostalCode(PostalCode.value))  proceed = false;
    if (!isCountry(Country.options[Country.selectedIndex].value))  proceed = false;
    if (!isGivenName(GivenName.value))  proceed = false;
    if (!isFamilyName(FamilyName.value))  proceed = false;
    if (!isDesignation(Designation.value))  proceed = false;
    //if (!isProfession(Profession.options[Profession.selectedIndex].value))  proceed = false;
    if (trim(Profession.value).length > 0 && !isProfession(Profession.value))  proceed = false;
    if (!isEmail(Email.value))  proceed = false;
    if (!isPhone(Phone.value))  proceed = false;
    if (!isFax(Fax.value))  proceed = false;
    if (!isNumAcct(NumberOfAcct.options[NumberOfAcct.selectedIndex].value))  proceed = false;
    //if (!isProduct(Product))  proceed = false;
    if (!isUsername(Username.value))  proceed = false;
    if (!isPassword(Password.value, ConfirmPassword.value))   proceed = false;
		if (!isUserNPassword(Username.value, Password.value))  proceed = false;
    if (!proceed)
      alert(errmsg);
    return proceed;
  }
}
function validateAcct(){
  errmsg = 'Error(s) were found on the Subscription form\n\n';
  
  with (document.frmAcctType){
    var proceed = true;
    if (!isAcctType(acctType))  proceed = false;
    if (!proceed)
      alert(errmsg);
    return proceed;
  }
}

function validate(){
  errmsg = 'Error(s) were found on the Subscription form\n\n';
  
   var proceed = true;
   if (!isAcctType(document.frmAcctType.plan))  proceed = false;
   
    if (!proceed)
      alert(errmsg);
    else{
		if (document.frmAcctType.plan[0].checked){
			document.frmAcctType.action = "adhoc/adhoc_registration.cfm";
		}else
			document.frmAcctType.action = "registration.cfm";
		
		document.frmAcctType.submit();	
	}

}
function validateProducts(){
  errmsg = 'Error(s) were found on the Subscription form\n\n';
  with (document.frmSubDetails){
    var proceed = true;
    if (!isNumAcct(NumberOfAcct.options[NumberOfAcct.selectedIndex].value))  proceed = false;
    //if (!isProduct(Product))  proceed = false;
    if (!proceed)
      alert(errmsg);
    return proceed;
  }
}
function validateRenewal(){
  errmsg = 'Error(s) were found on the Renewal Subscription form\n\n';
  
  with (document.frmRenewal){
    var proceed = true;
    //if (!isAcctType(acctType))  proceed = false;
    if (!isUsername(Username.value))  proceed = false;
    if (!isPassword(Password.value, Password.value))   proceed = false;
    if (!isEmail(Email.value))  proceed = false;
    if (!proceed)
      alert(errmsg);
    return proceed;
  }
}
function validatePaymentMode(){
  errmsg = "Error(s) were found in the Subscription Payment Method form\n\n";
  
  for (var idx=0; idx<document.frmPaymentMode.payment.length; idx++){
    if (document.frmPaymentMode.payment[idx].checked && document.frmPaymentMode[idx].value != 'ibank')
      return true;
    else if (document.frmPaymentMode[idx].checked && document.frmPaymentMode[idx].value == 'ibank'){
      for (var inneridx=1; inneridx<document.frmPaymentMode.bank.length; inneridx++){
        if (document.frmPaymentMode.bank[inneridx].selected)
          return true;
      }
      errmsg = errmsg + "Please select a bank to proceed."
      alert(errmsg);
      return false;
    }
  }
  errmsg = errmsg + "Please select a payment mode to proceed."
  alert(errmsg);
  return false;
}
//-->
