﻿
function IsValidPin(userid, password, confirmpassword) {
    
    //var txtUser = document.getElementById(userid);
    var txtPass = document.getElementById(password);
    var txtConfirm = document.getElementById(confirmpassword);
    // Check if empty password
    if (txtPass.value == "") {
        ErrorMsg = PinNotEmpty;
        txtPass.focus();
        return false;
    }

    // Check if password length is less than 8 characters
    if (txtPass.value.length < 6) {
        ErrorMsg = PasswordMin6Char;
        txtPass.focus();
        return false;
    }

    // Check if password length is more than 8 characters
    if (txtPass.value.length > 16) {
        ErrorMsg = PinMax16Char;
        txtPass.focus();
        return false;
    }

    // Check if the passwords entered are match.
    if (txtPass.value != txtConfirm.value) {
        ErrorMsg = PinNotMatch;
        txtConfirm.focus();
        return false;
    }

    // Check if password has any whitespace character
    if (isWhiteSpace(txtPass.value)) {
        ErrorMsg = PinNoSpaces;
        txtPass.focus();
        return false;
    }
    // Check if password contains any control character
    if (isControlChr(txtPass.value)) {
        ErrorMsg = PinWithoutCtrlChar;
        txtPass.focus();
        return false;
    }

//    // Check if password is aplhanumeric
//    if (!IsNumeric(txtPass.value)) {
//        ErrorMsg = PasswordNumeric;
//        txtPass.focus();
//        return false;
//    }
    
    // Check if password is sorted numerically in normal and reverse order
    var sPasswordCompare = "012345678909876543210";
    if (isSorted(txtPass.value, sPasswordCompare)) {
        ErrorMsg = PinNotSortedNumeric;
        txtPass.focus();
        return false;
    }
    //ErrorMsg = "";
    return true;
}

function IsValidPassword(userid, password, confirmpassword) {
    var txtUser = document.getElementById(userid);
    var txtPass = document.getElementById(password);
    var txtConfirm = document.getElementById(confirmpassword);

    // Check if empty password
    if (txtPass.value == "") {
        ErrorMsg = PassNotEmpty;
        txtPass.focus();
        return false;
    }

    // Check if password length is less than 8 characters
    if (txtPass.value.length < 8) {
        ErrorMsg = PasswordMin8Char;
        txtPass.focus();
        return false;
    }

    // Check if password length is more than 8 characters
    if (txtPass.value.length > 16) {
        ErrorMsg = PasswordMax16Char;
        txtPass.focus();
        return false;
    }

    // Check if the passwords entered are match.
    if (txtPass.value != txtConfirm.value) {
        ErrorMsg = PasswordNotMatch;
        txtConfirm.focus();
        return false;
    }

    // Check if password is the same as UserID
    if (txtPass.value == txtUser.value) {
        ErrorMsg = PasswordNotSameUserID;
        txtPass.focus();
        return false;
    }

    // Check if password has any whitespace character
    if (isWhiteSpace(txtPass.value)) {
        ErrorMsg = PasswordNoSpaces;
        txtPass.focus();
        return false;
    }
    // Check if password contains any control character
    if (isControlChr(txtPass.value)) {
        ErrorMsg = PassWithoutCtrlChar;
        txtPass.focus();
        return false;
    }

    // Check if password is a part of UserID
    if ((txtUser.value.indexOf(txtPass.value) != -1) || (txtPass.value.indexOf(txtUser.value) != -1)) {
        ErrorMsg = PasswordNotPartUserID;
        txtPass.focus();
        return false;
    }

    // Check if password is sorted alphabetically in normal and reverse order
    var sPasswordCompare = "ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA";
    if (isSorted(txtPass.value, sPasswordCompare)) {
        ErrorMsg = PasswordNotSortedAlphab;
        txtPass.focus();
        return false;
    }

    // Check if password is sorted numerically in normal and reverse order
    var sPasswordCompare = "012345678909876543210";
    if (isSorted(txtPass.value, sPasswordCompare)) {
        ErrorMsg = PasswordNotSortedNumeric;
        txtPass.focus();
        return false;
    }

    // Check if password contains more than 2 consecutive repeated characters/numerlas
    if (isRepeated(txtPass.value)) {
        ErrorMsg = PasswordNo2RepeatedChar;
        txtPass.focus();
        return false;
    }

    // Check if password is aplhanumeric
    if (!isAlphaNumeric(txtPass.value)) {
        ErrorMsg = PasswordAlphanumeric;
        txtPass.focus();
        return false;
    }

    return true;
}

// isWhiteSpace
function isWhiteSpace(sText) {
  // whitespace characters
  var whiteSpace = " \t\n\r";
  var sChar;
  var i;

  // Search through string's characters one by one
  // until we find a whitespace character.
  // When we do, return true;
  for (i = 0; i < sText.length; i++) {
    // Check that current character isn't whitespace.
    sChar = sText.charAt(i);
    if (whiteSpace.indexOf(sChar) != -1) return true;
  }
  
  return false;
}

// isControlChr
function isControlChr(sText) {
  var ctrlChar = "()/|?,;:'~<>\\+=.[]{}";
  var sChar;
  var i;
  
  for(i = 0; i < sText.length; i++) 
  {
    sChar = sText.charAt(i);
    if (ctrlChar.indexOf(sChar) !=-1) 
		return true;
  }

  return false;
}

// isSorted
function isSorted(sText, sCompareVar) {
  var bSorted = false;
  var sChar;
  var i;
  var n;

  if (IsNumeric(sCompareVar)) n = 4;
  else n = 3;
  
  for(i = 0; i < sText.length && bSorted==false; i++) {
    if (sText.substr(i).length >=n) {
      sChar =sText.substr(i,n);
      if (sCompareVar.toLowerCase().indexOf(sChar.toLowerCase()) !=-1) bSorted=true;
    }
  }

  return bSorted;
}

// 2 consecutive repeated characters/numerlas
function isRepeated(sText) {
  var code;
  var nCount=1;

  code = sText.charAt(0);
  for (i = 1; i < sText.length; i++) {
    sChar = sText.charAt(i);
    if (code==sChar) {
      nCount = nCount+1;
    }else{
		code = sChar;
		nCount = 1;
	}
	if (nCount==3) return true;

  }	
  return false;
}

// isAlphaNumeric
function isAlphaNumeric(sText) {
  var bAlphaNum =false;
  var sChar;
  var sAlpha="ABCDEFGHIJKLMNOPQRSTUVWXYZأبتثجحخدذرزسشصضطظعغفقكلمنهـويا";
  var sNum ="0123456789";

  //Convert sText To uppercase to make comparisons easier.
	sText = sText.toUpperCase();
	
	// Check if only alphabetic then return false
  for(i = 0; i < sText.length && bAlphaNum ==false; i++) {
  	sChar = sText.charAt(i);
    if (sAlpha.indexOf(sChar) !=-1) bAlphaNum=true;
  }
  
  if (bAlphaNum==false) return bAlphaNum;

	bAlphaNum=false;
  for(i = 0; i < sText.length && bAlphaNum ==false; i++) {
    sChar = sText.charAt(i);
    if (sNum.indexOf(sChar) !=-1) bAlphaNum=true;
	}

  return bAlphaNum;
}

// IsNumeric
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


