/* ======================================================================
DESC: This script is used to validate the login page which takes the user id, password

PLATFORMS: Windows/Linux

USAGE NOTES: 
====================================================================== */

/* ======================================================================
FUNCTION: 	formValidate

INPUT:		It calls several other functions like isUid,isPassword
				
RETURNS:		This function returns true if all the inputs in the form are valid or returns false

DESC:			This function is called before submitting the form whose true/false 
				would let the form from being submitted or not.				
====================================================================== */

/* ======================================================================
FUNCTION: 	isUid

INPUT:		It reads the value from the form's user id text field 
				
RETURNS:		This function returns true if the user id is valid with respect to specs or returns false

DESC:			This function checks the userid to be a minimum of 4 chars & max of 15 chars.
				No special symbols are also allowed except for '_'.Can have alphabets & numbers.
====================================================================== */

/* ======================================================================
FUNCTION: 	isPassword

INPUT:		It reads the value from the form's user password & confrirm password fields 
				
RETURNS:		This function returns true if the user id password is valid with respect to specs or returns false

DESC:			This function checks the password to be a minimum of 4 chars & max of 50 chars.
				No spaces are also allowed.Can have alphabets & numbers. Cannot be same as user id.
====================================================================== */

//This function checks the validity of the necessary functions
function Validate()
{
	if(isUid() && isPassWord())
	{
		return true;
	}
	else
	{
		return false;
	}
}

//This function checks the validity of userid
function isUid ()
{
 var bool_uidvalid=true;
 var txt_uid=Trim(document.frm_login_info.uname.value);

 //This is to check if the user id has been provided with any value or not
 if (txt_uid=="")
 {
	bool_uidvalid=false;
    alert ("User Id must not be blank...");
 }
 
 //This is to check if length of user id is min 4 chars
 else if (txt_uid.length<4)
 {
    bool_uidvalid=false;
    alert ("User Id must have minimum 4 characters....");
 }

 //This is to check if length of user id is more than max 15 chars
 else if (txt_uid.length>15)
 {
    bool_uidvalid=false;
    alert ("User Id must not exceed 15 characters...");
 }
 
 //This is to check if the first charecter of the user id given is an alphabet or not
 if(bool_uidvalid==true)
 {
   var ch=txt_uid.substring(0,1);
   if( (ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) )  
   {
 	  alert ("First letter of User Id must be an alphabet");
 	  bool_uidvalid=false;	  
   }
 }
 //This is to check whether all the charecters in the user id are valid charecters
  if(bool_uidvalid==true){
 for (var int_i = 1; int_i < txt_uid.length; int_i++) 
 {
		var ch = txt_uid.substring(int_i, int_i + 1);
		if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_')) 
		{
			alert("User Id field accepts A-Z, a-z, 0-9, '_'");
 			bool_uidvalid=false;
			break
		}
  }}
  //This is to focus on the user id text field if any criterion has been violated
  if (bool_uidvalid==false) {
  document.frm_login_info.uname.select();
  document.frm_login_info.uname.focus();
  }
  //Return the status of the user id validity
  return bool_uidvalid;
}

//This function checks the validity of password
function isPassWord() 
{
var txt_pwd=document.frm_login_info.upass.value;
var bool_pwdvalid=true;

//Checks whether the password is empty
if ( txt_pwd=="" ) 
{
    alert ("Password must not be blank...");
    bool_pwdvalid=false;
}
//Checks whether the password length is less than 6 chars
else if (txt_pwd.length < 6 ) 
{
    alert ("Password must have minimum 6 characters...");
    bool_pwdvalid=false;
}
//Checks whether the password length exceeds 50 chars
else if (txt_pwd.length > 50)
{
    alert ("Password must not exceed 50 characters...");
    bool_pwdvalid=false; 
}
//Checks whether the password is same as user id
else if (txt_pwd==document.frm_login_info.uname.value)
{
    alert ("Password must not be same as User Id...");
    bool_pwdvalid=false; 
}

//Checks if the password has any spaces in it
if ( bool_pwdvalid==true ) 
{
   for(var int_i=0;int_i<txt_pwd.length;int_i++)
   {
     var ch=txt_pwd.substring(int_i,int_i+1)
     if ( ch==" " ) 
     {
     	alert ("Password must not contain space...");
       	bool_pwdvalid=false;
	    break;
     }
	}
}
//If the password is not satisfying all criterion then the focus is set on the password field
if ( bool_pwdvalid==false )
{
   document.frm_login_info.upass.select()
   document.frm_login_info.upass.focus()
}
//Return the status of the password validity
return bool_pwdvalid
}