/************************************************************************
Validation 
************************************************************************

--------GUIDELINES FOR USAGE OF FUNCTIONS ----------------------
set the id of form elements as directed here and 
call the CheckEntireForm(objForm) in the form submit evet
i.e. <form onSubmit = "return CheckEntireForm(MyFormName)" ... >

RQ-Y/N REQUIRED(SHOULD NOT BE BLANK)
NM-Y/N NAME
NU-Y/N NUMERIC
FN-Y/N FLOAT NUMERIC
NUR-Y/N NUMERIC RANGE
NURMX-n MAX NUMERIC RANGE
NURMN-n MIN NUMERIC RANGE
NRC-Y/N CHECK NUMEIC RANGE
PH-Y/N PHONE
EM-Y/N EMAIL
CMX-n CONTROL HAS n AS MAX RANGE
CMN-n CONTROL HAS n AS MIN RANGE
CRC - Y/N CHECK CONTROL LENGTH RANGE
LBC-Y/N- CONTROL IS A LIST BOX CONTROL AND CHECK THAT A SELECTON IS MADE
MSS - XYZ... MESSAGE TO THROW IF VALIDATION FAILED
SPA - Y/N space is not allowed
FUP - Y/N if a file upload comp
TFP - P/C p=picture ; c = cv

A SAMPLE ID IS SHOWN HERE
RQ-Y|NU-Y|CMN-3|CMX-8|MSS-EMPLYOEE_INCOME_LEVEL
this field is a required field, it can only accept
numeric data and can contain only 3 to 8 char length number
it is EMPLYOEE INCOME LEVEL (notice underscore as space is not allowwd)
***********************************************************************************
*/
//Global constants for message throwing
var MESS_BLANK = new String("Please specify a value for the field: ")
var MESS_RANGE = new String("Please specify a value in the valid range for the field: ")
var MESS_NUMERIC = new String("Please specify a valid numeric value for the field: ")
var MESS_NAME = new String("Please specify a valid name (without spaces) for the field: ")
var MESS_NUMERIC_RANGE = new String("Please specify a numeric value in the given range for the field: ")
var MESS_PHONE = new String("Please specify a valid phone number in the format xxx-xxx-xxxxxxxx for the field: ")
var MESS_EMAIL = new String("Please specify a valid Email address (without spaces e.g. youremail@domain.com) for the field: ")
var MESS_CHAR_DATA = new String("Please specify a value with the specified min/max length for the field: ")
var MESS_LIST = new String("Please select an option from the list : ")
var MESS_START =new String("PLEASE FILL THIS FORM AS SPECIFIED HERE :\n--------------------------------------------------------------------")
var MESS_FUP=new String("The file you have selected for upload is not a valid file for the field: ")
var MESS_STRICTNUMERIC = new String("Please enter a whole number for the field : ")
var MESS_PASSWORD = new String("Please enter a valid value (Alphabets,numbers, _ are allowed) for the field :")
var MESS_SPC = new String("Please specify a value for the field,Special charactor not allowed: ")
var MESS_CMBFS = new String("Please Select Any Item: ")
//x


//Function to search  through an entire select list and highlight a specific option value based on passed arguement ObjList = Name of select list
function SelectListOption(ObjList,StrOptionValue)
{
	for(i=0;i<ObjList.length;i++)
	{
		if (ObjList.options[i].text==StrOptionValue)
		{
			ObjList.options[i].selected="1";
			return true;
		}
	}
	return false;		
}

//Function to check / uncheck checkbox form control
function SelectCheckBox(ObjCheckBox,boolCheck)
{
	if(boolCheck){
		ObjCheckBox.checked=true;
		return true;
		}
	else
	{
		ObjCheckBox.checked=false;
		return false;
	}
}

//get ascii char code of arg
function GetAsciiCode(argChar)
{
	var s = new String(argChar);
	return parseInt(s.charCodeAt(0));
}

//check if arg is pure numeric and a +ive whole number 
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;
   
}
//check valid password
//check if arg is pure numeric and a +ive whole number 
function IsPassword(sText)
{
   var ValidChars = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789_";
   var ret=true;
   var Char;
 
   for (i = 0; i < sText.length && ret == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         ret= false;
         }
   }
   return ret;
}
//check if arg is pure numeric and a +ive whole number 
function IsStrictNumeric(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;
   
}
//func for char checking 
function IsValid(argValue,argValidChars)
{
//alert(argValue);
//alert(argValidChars);

   var ValidChars = argValidChars;
   var ret=false;
   var Char;
 
   for (i = 0; i < argValue.length && ret == false; i++) 
      { 
      Char = argValue.charAt(i); 
      //alert(Char);
      if (ValidChars.indexOf(Char) != -1) 
         {
         //alert('INsideif');
         ret = true;
         }
      }
   return ret;
}
//check if arg is  numeric and a +ive decimal number
function IsFloatOrNumeric(argStr)
{
	var s = new String(argStr);
	var cnt=0;
	len = s.length;
	if (IsBlank(argStr)) { return false;}
	for(i=0;i<len;i++)
	{
		intCh = GetAsciiCode(s.charAt(i));
		
		if (intCh==46) //there can't be more than one decimal
		{
			cnt++;
			if(cnt>1)
				return false;
		}
		
		if (intCh==46&&(i==0||i==len-1))//first char and last char cannot be decimal
		{
			 return false;
		}
		
		if(!((intCh>=48&&intCh<=57)||(intCh==46)))
		{
			return false;
		}
	}
	return true;
}

//check if a string contains whitespaces or nothing ...
function IsBlank(argStr)
{
	var s = new String(argStr);
	var cnt=0;
	len = s.length;
	
	for(i=0;i<len;i++)
	{
		intCh = GetAsciiCode(s.charAt(i));
		if(intCh==32)
		{
			cnt++;
		}
	}
	if ((cnt==len)||(cnt==0&&len==0))
	{
	
		return true;
	}
	return false;
}

function IsSpace(argStr)
{
	var s = new String(argStr);
	var cnt=0;
	len = s.length;
	
	for(i=0;i<len;i++)
	{
		intCh = GetAsciiCode(s.charAt(i));
		if(intCh==32)
		{
			cnt++;
		}
	}
	if ((cnt==len)||(cnt==0&&len==0))
	{
	
		return true;
	}
	return false;
}

//check if a string contains special characters
function IsSpecialCharacter(argStr)
{
	var s = new String(argStr);
	var cnt=0;
	len = s.length;
	if (IsBlank(argStr)) { return false;}
	
	for(i=0;i<len;i++)
	{
		intCh = GetAsciiCode(s.charAt(i));
		alert(intCh.toString());
		if ((intCh >= 0 && intCh <= 31) || (intCh >= 127 && intCh <= 255))
		{
		    return true;
		}
	}
	return false;
}

//check if a string abides to a min , max range rule for an input field
function IsLengthBetweenRange(argStr,MIN,MAX)
{
	var s = new String(argStr)
	len = s.length;
	if (len<MIN||len>MAX)
	{
		return false;
	}
	return true;
}

//check valid international phone number xxx-xxx-xxxxxxxx
function IsValidInternationalPhoneFaxNumber(argStr)
{
	var s = new String(argStr);
	var cnt=0;
	len = s.length;
	
	if (IsThereAnyWhiteSpace(argStr)==true)
	{
		return false;
	}
		
	if (!(s.indexOf("-")>=0))
	{
		return false;
	}
		
	arr = s.split("-")
	
	if (arr.length!=3)
	{
		return false;
	}
	
	var countrycode=new String(arr[0]);
	var statecode=new String(arr[1]);
	var localcode=new String(arr[2]);
	
	if (IsNumeric(countrycode)==false||IsNumeric(statecode)==false||IsNumeric(localcode)==false)
	{
		return false;
	}
	
	if (IsLengthBetweenRange(countrycode,2,3)==false||IsLengthBetweenRange(statecode,2,3)==false||IsLengthBetweenRange(localcode,4,8)==false)
	{
		return false;
	}
	
	return true;
}

//check if arg contains no white spaces
function IsThereAnyWhiteSpace(argStr)
{
	var s = new String(argStr);
	var cnt=0;
	len = s.length;
	if (IsBlank(argStr))
	{
	return false;
	}
	
	if(s.indexOf(" ")>=0)
	{
		return true;
	}
	return false;
}
//check if number is between given range
function CheckNumericRange(argStr,MIN,MAX)
{
	if (IsNumeric(argStr)==true)
	{
		var n = new Number(argStr);
		
		if (n<MIN||n>MAX)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}
function IsValidName(argStr)
{
	var s = new String(argStr);
	var cnt=0;
	len = s.length;
	
	for(i=0;i<len;i++)
	{
		intCh = GetAsciiCode(s.charAt(i));
		if(!((intCh>=65&&intCh<=90)||(intCh>=97&&intCh<=122)||(intCh==46)))
		{
			return false;
		}	
	}
	return true;
}
function IsValidEmail(argStr)
{
	var s = new String(argStr)
	
	if(IsBlank(s)==true) {return false;}
	if(IsThereAnyWhiteSpace(s)==true) { return false;}
	if(IsValid(s,"qwertyuiopasdfghjklzxcvbnm0123456789-_@.QWERTYUIOPASDFGHJKLZXCVBNM")==false) return false;
	if(s.indexOf('@')<1) { return false;}
	
	var dotCount;
	var id = new String();
	var dom = new String();
	
	arr=s.split('@')
	
	id  = arr[0];
	dom = arr[1];
	
	
	
	if (id.length<1) { return false;}
	if (dom.length<4) { return false;}
	if (dom.indexOf('.')<1) { return false;}
	return true;
}
function IsListBoxSelected(objListBox)
{
	
	/*var strText = new String(objListBox.options[objListBox.selectedIndex].text)
	if (strText.toUpperCase()=='SELECT') return false;*/
	
	if (objListBox.options[objListBox.selectedIndex].value==0)
	{
		return false;
	}
	return true;
}
//resolve extra data passed with a control to the form and get name - value pairs 
//for Example HERE CONTROL_DATA = RQ-Y|mss-NAME, MKEY LIKE 'RQ'
function GetCommand(CONTROL_DATA,MKEY)
{	
	arr = CONTROL_DATA.split("|")
	for(i=0;i<arr.length;i++)
	{
		arr2=arr[i].split("-");
		if (arr2[0]==MKEY)
		{
			return arr2[1];
		}
	}
}
/*fn :: restrict the user to select only gif,jpe,png files for photo uploading */
function IsValidFile(argStr,type)
{
	var fileExt=new Array();
	if(type.toUpperCase()=='P')//picture
	{
		fileExt[0]="GIF";
		fileExt[1]="JPEG";
		fileExt[2]="PNG";
		fileExt[3]="JPG";
	}
	if(type.toUpperCase()=='C')//cv
	{
		fileExt[0]="TXT";
		fileExt[1]="DOC";
		fileExt[2]="XLS";
		fileExt[3]="ZIP";
		fileExt[4]="HTM";
		fileExt[5]="HTML";
	}

	dt=argStr;
	if(dt.length>0)
	{
		dotpos = dt.lastIndexOf(".");
		ext = dt.substr(dotpos+1);
		for(i=0;i<fileExt.length;i++)
		{
			if (ext.toUpperCase()==fileExt[i])
			{
				return true;				
			}
		}
	return false;
	}
	else
	{
		//if no file is selected then return true as there is nothin to check
		return true;
	}
	return false;
}
//***********************************************************************************************
//This function takes the form obj as arg and processes all it's elements by the ids passed to it
//***********************************************************************************************

function CheckEntireForm(objForm)
{
    
	var intFormElements=new Number();		//number of form elements
	var obj;								//pointer to particular object of form
	var i;									//loop var
	var COMMAND_STRING = new String();		//command string of form control
	var FINAL_MESSAGE = new String();		//message to throw if validation fails
	
	
	var RQ;									//required field
	var NM;									//name field
	var NU;									//numeric fiels
	var FN;									//float numeric
	var NUR;								//numeric range
	var NURMX;								//if nur then max numeric range
	var NURMN;								//if nur then min numeric range
	var PH;									//phone field
	var EM;									//email field
	
	var CMX;								//field max length
	var CMN;								//field min length
	var LBC;								//control is a list box select
	var MSS_CONTROL;						//mss associated with a control(message)
	var SPA;								//spaces not allowed
	
	var NRC;
	var CRC;
	var FUP;
	var TFP;
	var STN;								//STRICT NUMERIC NO DECIMALS
	var PWD;								//password field
	var SPC;								//checking for special char
		var CMBFS;								//checking for COMBO FIRST VALUE...
	
	var YES="Y";							//constants for the ease of distinguishing
	var NO="N";								//constants for the ease of distinguishing
	
	var CONTROL_VALUE;
	var m;
	
	//get number of form elements
	intFormElements= objForm.elements.length;
	
	//seperate messages for all elements
	var MESSAGE_ARRAY = new Array(intFormElements)
	//show focus on the first alert 
	var flag=0;
	//loop through all elements
	for(i=0;i<intFormElements;i++)
	{
		obj = objForm.elements[i]
				
		COMMAND_STRING = obj.lang;
		//alert(COMMAND_STRING);
		COMMAND_STRING = COMMAND_STRING.toUpperCase();
		
		//get custom validation
	
		RQ			= GetCommand(COMMAND_STRING,"RQ");
		NM			= GetCommand(COMMAND_STRING,"NM");
		NU			= GetCommand(COMMAND_STRING,"NU");
		FN			= GetCommand(COMMAND_STRING,"FN");
		NUR			= GetCommand(COMMAND_STRING,"NUR");
		NURMX		= GetCommand(COMMAND_STRING,"NURMX");
		NURMN		= GetCommand(COMMAND_STRING,"NURMN");
		PH			= GetCommand(COMMAND_STRING,"PH");
		EM			= GetCommand(COMMAND_STRING,"EM");
		CMX			= GetCommand(COMMAND_STRING,"CMX");
		CMN			= GetCommand(COMMAND_STRING,"CMN");
		LBC			= GetCommand(COMMAND_STRING,"LBC");
		MSS_CONTROL = GetCommand(COMMAND_STRING,"MSS");
		SPA			= GetCommand(COMMAND_STRING,"SPA");
		NRC			= GetCommand(COMMAND_STRING,"NRC");
		CRC			= GetCommand(COMMAND_STRING,"CRC");
		FUP			= GetCommand(COMMAND_STRING,"FUP")
		TFP			= GetCommand(COMMAND_STRING,"TFP")
		STN			= GetCommand(COMMAND_STRING,"STN")
		PWD			= GetCommand(COMMAND_STRING,"PWD")
		SPC			= GetCommand(COMMAND_STRING,"SPC")
		CMBFS       = GetCommand(COMMAND_STRING,"CMBFS")
		CONTROL_VALUE = obj.value
		MESSAGE_ARRAY[i]=""
		
		//COMBO FIRST VALUE..
		if (CMBFS==YES)
		{
			if (CONTROL_VALUE=="-First-")
			{ 
				MESSAGE_ARRAY[i] = MESS_CMBFS+MSS_CONTROL
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}

		
		//SPECIAL CHAR
		if (SPC==NO)
		{
			if (IsValid(CONTROL_VALUE,"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789_")==false)
			{
				MESSAGE_ARRAY[i] = MESS_SPC+MSS_CONTROL
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
	
		
		//check required
		if (RQ==YES)
		{
			if (IsBlank(CONTROL_VALUE)==true)
			{
				MESSAGE_ARRAY[i] = MESS_BLANK+MSS_CONTROL
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
	
		//check name
		if (NM==YES)
		{ 
			if (IsValidName(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0 && IsBlank(CONTROL_VALUE)==false)
			{
				MESSAGE_ARRAY[i] = MESS_NAME+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check strict numeric
		if (STN==YES)
		{
			if (IsStrictNumeric(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0)
			{
				MESSAGE_ARRAY[i] = MESS_STRICTNUMERIC+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check numeric
		if (NU==YES)
		{
			if (IsNumeric(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0)
			{
				MESSAGE_ARRAY[i] = MESS_NUMERIC+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check float numeric
		if (FN==YES)
		{
			if (IsFloatOrNumeric(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0 && IsBlank(CONTROL_VALUE)==false)
			{
				MESSAGE_ARRAY[i] = MESS_NUMERIC+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check password
		if (PWD==YES)
		{
			if (IsPassword(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0)
			{
				MESSAGE_ARRAY[i] = MESS_PASSWORD+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check numeric with range
		if (NUR==YES)
		{
			if(STN==YES)
			{
				if (IsStrictNumeric(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0)
				{
					MESSAGE_ARRAY[i] = MESS_NUMERIC+MSS_CONTROL;
					if (flag!=1){
				obj.focus();
				flag=1;
				}
				}		
			}
			else
			{
				if (IsNumeric(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0)
				{
					MESSAGE_ARRAY[i] = MESS_NUMERIC+MSS_CONTROL;
					if (flag!=1){
				obj.focus();
				flag=1;
				}
				}
			}
		}
		
		if (NRC==YES&&MESSAGE_ARRAY[i].length==0)
		{
		
			if (CheckNumericRange(CONTROL_VALUE,NURMN,NURMX)==false&&MESSAGE_ARRAY[i].length==0)
			{
				MESSAGE_ARRAY[i] = MESS_NUMERIC_RANGE+MSS_CONTROL+"[Range: "+NURMN+" to "+NURMX+ " ]";
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		
		//check phone number
		if (PH==YES&&MESSAGE_ARRAY[i].length==0)
		{   
			if (IsValidInternationalPhoneFaxNumber(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0 && IsBlank(CONTROL_VALUE)==false)
			{
				MESSAGE_ARRAY[i] = MESS_PHONE+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//IsBlank
			if (SPA==YES&&MESSAGE_ARRAY[i].length==0)
		{
		
			if (IsSpace(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0)
			{
				MESSAGE_ARRAY[i] = MESS_BLANK+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check email
		if (EM==YES&&MESSAGE_ARRAY[i].length==0)
		{
		 
			if (IsValidEmail(CONTROL_VALUE)==false&&MESSAGE_ARRAY[i].length==0 && IsBlank(CONTROL_VALUE)==false)
			{
				MESSAGE_ARRAY[i] = MESS_EMAIL+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check length for character data
		if (CRC==YES&&MESSAGE_ARRAY[i].length==0)
		{
			if (IsLengthBetweenRange(CONTROL_VALUE,parseInt(CMN),parseInt(CMX))==false&&MESSAGE_ARRAY[i].length==0 && IsBlank(CONTROL_VALUE)==false)
			{
				MESSAGE_ARRAY[i] = MESS_CHAR_DATA+MSS_CONTROL+ " Range ("+CMN + " to " + CMX+ " )";
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check if value  from a list is selectd
		if (LBC==YES&&MESSAGE_ARRAY[i].length==0)
		{
			if(IsListBoxSelected(obj)==false&&MESSAGE_ARRAY[i].length==0 && IsBlank(CONTROL_VALUE)==false)
			{
				MESSAGE_ARRAY[i] = MESS_LIST+MSS_CONTROL;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		//check file upload is havin a valid file upload
		
		if (FUP==YES&&MESSAGE_ARRAY[i].length==0)
		{
			if (IsValidFile(CONTROL_VALUE,TFP)==false)
			{
				if (TFP=='P') { var e = new String("GIF,JPG,PNG")}
				if (TFP=='C') { var e = new String("TXT,DOC,HTM,HTML,ZIP,XLS")}
				MESSAGE_ARRAY[i] = MESS_FUP+MSS_CONTROL+" Valid file formats are : " + e;
				if (flag!=1){
				obj.focus();
				flag=1;
				}
			}
		}
		
	}
	
	//end checking form controls
	m="";
	for(i=0;i<intFormElements;i++)
	{
		m+=MESSAGE_ARRAY[i];
	}
	
	if(m!="")
	{
		FINAL_MESSAGE+=MESS_START + "\n\n"
		
		for(i=0;i<intFormElements;i++)
		{
			if (MESSAGE_ARRAY[i]!="")
			{
				//FINAL_MESSAGE+="("+parseInt(i+1)+") "+MESSAGE_ARRAY[i]+"\n";
				FINAL_MESSAGE+=MESSAGE_ARRAY[i]+"\n";
			}
		}		
		alert(FINAL_MESSAGE);				
		return false;
	}
	else
	{	    
		return true;
	}
}
	


function SubmitClick(varform)
{

if(CheckEntireForm(varform)==true)
{
    if(confirm('Are you Sure'))
    {
    //document.form1.submit();
    return true;
    }
    else
    {
    return false;
    }
}
else
{
return false;
}
}
function CancelClick()
{
return(confirm('Are you Sure'));

}
	
function ShowConfirmation()
{
return(confirm('Are you Sure'));
}

function openShowGenWindow(varFormID,varControlString,varCondition)
{
//alert(getPath());
 window.open(getPath() + '/Resources/ShowGen/ShowGen.aspx?FormGenID='+ varFormID +'&ControlString=' + varControlString + '&condition=' + varCondition + '','Select','width=500,height=325,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
}

function getPath()
{
var path =document.location;
var str = path.toString();
var posFirstSlash = str.indexOf('/',8);
posFirstSlash = str.indexOf('/',posFirstSlash+1);
return(str.substring(0,posFirstSlash));
}
//Change the background color of controls on got focus    BY: - Pankaj Kumar Pandey
function ChangeColor(){
    element=event.srcElement;
   alert(element.name);
    for(i=0;i<form1.elements.length;i++){
      if(form1.elements(i).id==element.id){
           form1.elements(i).style.backgroundColor='LightSkyBlue';
           }
      else{
           form1.elements(i).style.backgroundColor='White';
       }
            
    }
}

var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = 1200
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        OpenModalWindowFinalForLock()
    }
    else
    {
        //self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}
//Timer Start
InitializeTimer()
//Calling Lock After given time 
function OpenModalWindowFinalForLock()
{
    var h = null;
    h = window.open(getPath() + '/MODULES/USERMAINTENANCE/frmLock.aspx', '_Blank', "width=400,height=200,menubar=no,status=no,location=no,left=350,top=275,toolbar=no,scrollbars=no,close=no");    
	
	window.onfocus=function()
       {
           if(h && !h.closed)
   		{
		    h.focus();
		}
       }

	window.onclick=function()
       {
           if(h && !h.closed)
   		{
		    h.focus();
		}
       }
	window.document.onclick=function()
       {
           if(h && !h.closed)
   		{
		    h.focus();
		}
       }

	window.document.onactivate=function()
       {
           if(h && !h.closed)
   		{
		    h.focus();
		}
       }


}	
function UCValidateAll(TextId){
    var TextIdf = eval(TextId);
    if(!IsNumeric(TextIdf.value)){
        alert("Enter Numric Value..");
        TextIdf.focus();
        return false;
    }
    else
    return true;
}


function array(n) {
  for(i=0;i<n;i++) this[i]=0;
  this.length=n;
}

/* Some basic logical functions had to be rewritten because of a bug in
 * Javascript.. Just try to compute 0xffffffff >> 4 with it..
 * Of course, these functions are slower than the original would be, but
 * at least, they work!
 */

function integer(n) { return n%(0xffffffff+1); }

function shr(a,b) {
  a=integer(a);
  b=integer(b);
  if (a-0x80000000>=0) {
    a=a%0x80000000;
    a>>=b;
    a+=0x40000000>>(b-1);
  } else
    a>>=b;
  return a;
}

function shl1(a) {
  a=a%0x80000000;
  if (a&0x40000000==0x40000000)
  {
    a-=0x40000000;  
    a*=2;
    a+=0x80000000;
  } else
    a*=2;
  return a;
}

function shl(a,b) {
  a=integer(a);
  b=integer(b);
  for (var i=0;i<b;i++) a=shl1(a);
  return a;
}

function and(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return ((t1&t2)+0x80000000);
    else
      return (t1&b);
  else
    if (t2>=0)
      return (a&t2);
    else
      return (a&b);  
}

function or(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return ((t1|t2)+0x80000000);
    else
      return ((t1|b)+0x80000000);
  else
    if (t2>=0)
      return ((a|t2)+0x80000000);
    else
      return (a|b);  
}

function xor(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return (t1^t2);
    else
      return ((t1^b)+0x80000000);
  else
    if (t2>=0)
      return ((a^t2)+0x80000000);
    else
      return (a^b);  
}

function not(a) {
  a=integer(a);
  return (0xffffffff-a);
}

/* Here begin the real algorithm */

    var state = new array(4); 
    var count = new array(2);
	count[0] = 0;
	count[1] = 0;                     
    var buffer = new array(64); 
    var transformBuffer = new array(16); 
    var digestBits = new array(16);

    var S11 = 7;
    var S12 = 12;
    var S13 = 17;
    var S14 = 22;
    var S21 = 5;
    var S22 = 9;
    var S23 = 14;
    var S24 = 20;
    var S31 = 4;
    var S32 = 11;
    var S33 = 16;
    var S34 = 23;
    var S41 = 6;
    var S42 = 10;
    var S43 = 15;
    var S44 = 21;

    function F(x,y,z) {
	return or(and(x,y),and(not(x),z));
    }

    function G(x,y,z) {
	return or(and(x,z),and(y,not(z)));
    }

    function H(x,y,z) {
	return xor(xor(x,y),z);
    }

    function I(x,y,z) {
	return xor(y ,or(x , not(z)));
    }

    function rotateLeft(a,n) {
	return or(shl(a, n),(shr(a,(32 - n))));
    }

    function FF(a,b,c,d,x,s,ac) {
        a = a+F(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function GG(a,b,c,d,x,s,ac) {
	a = a+G(b, c, d) +x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function HH(a,b,c,d,x,s,ac) {
	a = a+H(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function II(a,b,c,d,x,s,ac) {
	a = a+I(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function transform(buf,offset) { 
	var a=0, b=0, c=0, d=0; 
	var x = transformBuffer;
	
	a = state[0];
	b = state[1];
	c = state[2];
	d = state[3];
	
	for (i = 0; i < 16; i++) {
	    x[i] = and(buf[i*4+offset],0xff);
	    for (j = 1; j < 4; j++) {
		x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
	    }
	}

	/* Round 1 */
	a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
	d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
	c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
	b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
	a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
	d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
	c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
	b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
	a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
	d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
	c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
	b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
	a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
	d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
	c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
	b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */

	/* Round 2 */
	a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
	d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
	c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
	b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
	a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
	d = GG ( d, a, b, c, x[10], S22,  0x2441453); /* 22 */
	c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
	b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
	a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
	d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
	c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
	b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
	a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
	d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
	c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
	b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */

	/* Round 3 */
	a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
	d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
	c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
	b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
	a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
	d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
	c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
	b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
	a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
	d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
	c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
	b = HH ( b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
	a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
	d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
	c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
	b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */

	/* Round 4 */
	a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
	d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
	c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
	b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
	a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
	d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
	c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
	b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
	a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
	d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
	c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
	b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
	a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
	d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
	c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
	b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */

	state[0] +=a;
	state[1] +=b;
	state[2] +=c;
	state[3] +=d;

    }

    function init() {
	count[0]=count[1] = 0;
	state[0] = 0x67452301;
	state[1] = 0xefcdab89;
	state[2] = 0x98badcfe;
	state[3] = 0x10325476;
	for (i = 0; i < digestBits.length; i++)
	    digestBits[i] = 0;
    }

    function update(b) { 
	var index,i;
	
	index = and(shr(count[0],3) , 0x3f);
	if (count[0]<0xffffffff-7) 
	  count[0] += 8;
        else {
	  count[1]++;
	  count[0]-=0xffffffff+1;
          count[0]+=8;
        }
	buffer[index] = and(b,0xff);
	if (index  >= 63) {
	    transform(buffer, 0);
	}
    }

    function finish() {
	var bits = new array(8);
	var	padding; 
	var	i=0, index=0, padLen=0;

	for (i = 0; i < 4; i++) {
	    bits[i] = and(shr(count[0],(i * 8)), 0xff);
	}
        for (i = 0; i < 4; i++) {
	    bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
	}
	index = and(shr(count[0], 3) ,0x3f);
	padLen = (index < 56) ? (56 - index) : (120 - index);
	padding = new array(64); 
	padding[0] = 0x80;
        for (i=0;i<padLen;i++)
	  update(padding[i]);
        for (i=0;i<8;i++) 
	  update(bits[i]);

	for (i = 0; i < 4; i++) {
	    for (j = 0; j < 4; j++) {
		digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
	    }
	} 
    }

/* End of the MD5 algorithm */

function hexa(n) {
 var hexa_h = "0123456789abcdef";
 var hexa_c=""; 
 var hexa_m=n;
 for (hexa_i=0;hexa_i<8;hexa_i++) {
   hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
   hexa_m=Math.floor(hexa_m/16);
 }
 return hexa_c;
}


var ascii="01234567890123456789012345678901" +
          " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
          "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

function MD5(entree) 
{
 var l,s,k,ka,kb,kc,kd;

 init();
 for (k=0;k<entree.length;k++) {
   l=entree.charAt(k);
   update(ascii.lastIndexOf(l));
 }
 finish();
 ka=kb=kc=kd=0;
 for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
 for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
 for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
 for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
 s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
 return s; 
}


