/*
 *	JavaScript Object for Client side Validations like Date,NotNull...
 *	Written By				: Satyanarayana Modukuri , Rama Chandra Batchu.
 *	On Date					: 8-Feb-2001.
 *	Last Modification On	: 19-May-2003 10:20 AM.
*/


/*	
 *	This object includes -
 *	1. Array to Hold Validations.
 *	2. Method to add Validations to Array.
 *	3. Method to validate the Conditions which are in the Array.
*/

/*	This Function will works like a constructor for Validation Object */

function RSI_validation()
{
	this.arrValidations=new Array();
	this.add=addValidation ;
	this.validate = validate ;
	this.dateComparision = dateComparision;	 
}

/*  For adding Validations to the Array 
 *  itemName	:	Name of the Item to which we are going to add Validation
 *  constraint	:	Name of the Validation (Eg: NotNull,Number...).
 *	errMsg		:	Message which has to be displayed when the validation has failed.
*/
function addValidation(itemName,constraint,errMsg)
{
	this.arrValidations[this.arrValidations.length] = new Array(itemName,constraint,errMsg) ;
}

/*	Method for Checking the Validations which are in the Array -
	numForm		:	Number of the Form to Be Validated.
	blnSubmit	:	Boolean Value whether the form has to be submitted after all Validations.
*/

var validationResult=1 ;

function validate(numForm,blnSubmit)
{	
	validationResult = 1;

	/* For Knowing whether the validation is passed or Not */

	var arrElements=new Array() ;	
	
	/* For holding the validation which is in the Validations Array */   
	for(var validationNo=0;validationNo< this.arrValidations.length ;validationNo++)
	{
		arrElements=this.arrValidations[validationNo];

		/*Checking for phone validation 
		if (validationResult==1 && arrElements[1] == "phoneValidation")
		{
			var phonevalid = phoneValidation(numForm,arrElements[2]);
			if(!phonevalid)
			{
					validationResult=0;
					break;
			}
		}*/

		/* Checking for Not Null Validation */
		if (validationResult==1 && arrElements[1] == "NotNull" && document.forms[numForm].elements[arrElements[0]].value == "" )
		{
				alert(arrElements[2]);
				document.forms[numForm].elements[arrElements[0]].value="";
				document.forms[numForm].elements[arrElements[0]].focus();
				validationResult=0;
				break;
		}

		/* Checking for isblank Validation */
		if(validationResult==1 && arrElements[1] == "NoBlank" &&  isblank(document.forms[numForm].elements[arrElements[0]].value) == true)
		{
			alert(arrElements[2]);
			document.forms[numForm].elements[arrElements[0]].value="";
			document.forms[numForm].elements[arrElements[0]].focus();
			validationResult=0;
			break;
		}

		/* Checking for Numeric Value */
		if (validationResult==1 && arrElements[1]=="Number" && isNaN(document.forms[numForm].elements[arrElements[0]].value) == true)
		{
			alert(arrElements[2]);
			document.forms[numForm].elements[arrElements[0]].value="";
			document.forms[numForm].elements[arrElements[0]].focus();
			validationResult=0;
			break;
		}

		/* Validating the Character input */
		if (validationResult==1 && arrElements[1]=="String")
		{
			var str=document.forms[numForm].elements[arrElements[0]].value ;
			if(str.indexOf("\"") != -1)
			{
					alert("Enter Valid String");
					document.forms[numForm].elements[arrElements[0]].value="";
					document.forms[numForm].elements[arrElements[0]].focus();
					validationResult=0;
					break;
			}
			for(var charNo=0; charNo<(str.length); charNo++)
			{
				if ( isNaN(str.charAt(charNo) ) == false && validationResult==1 && str.charAt(charNo)!=" ")
				{
					  alert(arrElements[2]);
					  document.forms[numForm].elements[arrElements[0]].value="";
					  document.forms[numForm].elements[arrElements[0]].focus();
					  validationResult=0;
					  break;
				}
			} // End of For.
			if(validationResult==0)
				break;
		}

		/* Test for the SPL Char */
        if (validationResult==1 && arrElements[1]=="NoSplChar" )
		{
			var resrtictedCharecters =  new Array(":",";","\"","'","?","*","~","@","`","!","#","$","%","^","&","(",")","+","=","\\","|","<",">","?","/","{","}","[","]","-");
            var getValue = document.forms[numForm].elements[arrElements[0]].value ;

            if((getValue.indexOf(",") != -1 || getValue.indexOf(".") != -1) && arrElements[0]!="txtco_org")
            {
				alert(arrElements[2]);
				document.forms[numForm].elements[arrElements[0]].value="";
				document.forms[numForm].elements[arrElements[0]].focus();
				validationResult=0;
                break;
            }
            else
            {
				for(var check =0;check<resrtictedCharecters.length;check++)
              	{
					if(getValue.indexOf(resrtictedCharecters[check]) != -1)
					{
						check =  resrtictedCharecters.length;
						alert(arrElements[2]);
						document.forms[numForm].elements[arrElements[0]].value="";
						document.forms[numForm].elements[arrElements[0]].focus();
						validationResult=0;
						break;
                   	}
                }
            }
            if(validationResult==0)
				break;
		}

		/* Age Validations */
		if (validationResult==1 && arrElements[1]=="Age" )
		{
			if( isNaN(document.forms[numForm].elements[arrElements[0]].value) == true || (document.forms[numForm].elements[arrElements[0]].value)>120  || (document.forms[numForm].elements[arrElements[0]].value)<1)
			{
					document.forms[numForm].elements[arrElements[0]].focus();
					alert(arrElements[2]);
					validationResult=0;
					break;
			}
		}
		
		if(validationResult==1 && arrElements[2]=="Please enter EmailID")
		{
			var result = emailValidation("txtEmailID","resumeForm");
			if(result==false)
			{
				document.resumeForm.txtEmailID.value="";
				document.resumeForm.txtEmailID.focus();
				validationResult=0;
				return false;
			}

			result = checkMailId(document.resumeForm.txtEmailID.value);
			if(result==false)
			{
				document.resumeForm.txtEmailID.value="";
				document.resumeForm.txtEmailID.focus();
				validationResult=0;
				break;
			}
		}
	} // End of For.

	/* If no errors found and the user wants to submit the form, it will get submitted without any validation alert */
	if (validationResult==1 && blnSubmit==true)
	{
		//document.forms[numForm].btnSubmit.="false"
		document.forms[numForm].submit();
	}

}  // End of function validate().


function isblank(str)
{
	/*	Checking all the characters in the string whether it contains empty space,
 		or new line character or tab.If it contains,then it returns true else false.*/

	//var charcount=0;
	//alert("String :"+str+"-----length"+str.length);
	for(var increment=0;increment<str.length;increment++)
	{
		var character = str.charAt(increment);
		//alert("Character :"+character);
		if(increment==0 && character==' ')
			return true;
			//charcount++;
		//alert("Character count :"+charcount);
	}
	//alert(charcount==str.length);
	if(increment==str.length)
		return false;
}

/*-----------------------------------------------------------------
Method 	: dateComparision()
Purpose   : This function is used to compare 2 dates in the format MM-DD-YYYY
------------------------------------------------------------------*/
function dateComparision(firstDate,secondDate)
{
		firstMonth   = firstDate.substring(0,2);
		firstDay = firstDate.substring(3,5);
		firstYear = firstDate.substring(6);

		secondMonth   = secondDate.substring(0,2);
		secondDay = secondDate.substring(3,5);
		secondYear = secondDate.substring(6);

		firstDate = ""+firstYear+firstMonth+firstDay;
		secondDate = ""+secondYear+secondMonth+secondDay;
		if(firstDate > secondDate)
		{
				return true;
		}
		else
		{
				return false;
		}
}

/*--------------------------------------------------------------------------------
Method 	: resetForm(formName)
Purpose   : This Function resets all the form fields.form name should be passed as parameter
---------------------------------------------------------------------------------*/

function resetForm(formName)
{				
		document.forms[formName].elements[0].focus(); 
		document.forms[formName].reset(); 		
		return false;
}

					
/*-----------------------------------------------------------------
Method 	: emailValidation()
Purpose   : This Function Checks whether the entered email
					address is valid or not
------------------------------------------------------------------*/

function emailValidation(fieldName,formName)
{
	
		var email = document.forms[formName].elements[fieldName].value;		
		if(email!="")
		{			    
				if(email.indexOf(" ")!=-1)
				{
					//document.forms[formName].elements[fieldName].value="";
					alert("Blank spaces are not allowed for email address");
					//document.forms[formName].elements[fieldName].select();
					document.forms[formName].elements[fieldName].value="";
					document.forms[formName].elements[fieldName].focus();
					return false;
				}
				//email = email.replace(/\s/g,"");
				var emailLength=email.length;
				var atLength=email.indexOf("@");
				var dotLength=email.lastIndexOf(".");
				var resrtictedCharecters =  new Array("~","!","#","\$","%","^","&","*","\,","(",")","+","=","\\","|","<",">","\,","?","/","{","}","[","]");
				for(var check =0;check<resrtictedCharecters.length;check++)
				{
						if(email.indexOf(resrtictedCharecters[check])!=-1)
						{
							alert("Please enter vaild email address");
							document.forms[formName].elements[fieldName].value="";
							document.forms[formName].elements[fieldName].focus();
							return false;
						}
				}			

				if(emailLength==atLength || emailLength==dotLength+1 || atLength==dotLength-1 || atLength==-1 || dotLength==-1 || dotLength<atLength || atLength=="0")
				{
					alert("Please enter vaild email address");
					document.forms[formName].elements[fieldName].value="";
					document.forms[formName].elements[fieldName].focus();
					return false;
				}
		}		
		else
		{
			return false;
		}
		return true;
}


/*-----------------------------------------------------------------
Method 	: checkMailId()
Purpose   : This Function Checks whether the entered email
					address is valid or not
------------------------------------------------------------------*/

function checkMailId(mailids)
{
	
	var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro',
								'.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al',
								'.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.az','.ba','.bb',
								'.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs',
								'.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci',
								'.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de',
								'.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.eh','.er','.es','.et',
								'.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh',
								'.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy',
								'.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io',
								'.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
								'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk',
								'.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg','.mh','.mk',
								'.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw',
								'.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np',
								'.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm',
								'.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rw','.ru','.sa',
								'.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn',
								'.so','.sr','.st','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj',
								'.tk','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug',
								'.uk','.um','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu',
								'.ws','.wf','.ye','.yt','.yu','.za','.zm','.zw'); 
	var mai = mailids;
	var val = true;

	var dot = mai.lastIndexOf(".");
	var ext = mai.substring(dot,mai.length);
	//alert(ext);
	var at = mai.indexOf("@");
	if(dot > 5 && at >1)
	{
		for(var i=0; i<arr.length; i++)
		{
			if(ext == arr[i])
			{
				val = true;
				break;
			} 
			else
			{
				val = false;
			}
		}
		if(val == false)
		{
			alert("Your maild "+mai+" is not corrrrect");
			return false;
		}
	}
	else
	{
		alert("Your maild "+mai+" is not correct");
		return false;
	}

	return true;
}



/*-----------------------------------------------------------------
Method 	: phone_faxValidation(formName)
Purpose   : This Function is used for phone and fax validations
------------------------------------------------------------------*/
 function phone_faxValidation(formName)
{
		var phoneCount = 0,phoneValue = 0;
		var faxCount=0 , faxValue =0;
		for(var count=0;count<phoneArray.length-2;count++)
		{
			 phoneCount = phoneCount +document.forms[formName].elements[phoneArray[count]].value.length;
			 phoneValue = phoneValue + document.forms[formName].elements[phoneArray[count]].value;
			 faxCount = faxCount+ document.forms[formName].elements[faxArray[count]].value.length;
			 faxValue = faxValue+ document.forms[formName].elements[faxArray[count]].value;  			 
		}		
		if((phoneCount!=0 && phoneCount != 10) || (phoneValue<=0 && phoneCount != 0))
		{
			document.forms[formName].elements[phoneArray[0]].focus();
			alert("Please enter valid phone number");
			return false;
		}			
		if((document.forms[formName].elements[phoneArray[3]].value.length !=0)&&(document.forms[formName].elements[phoneArray[3]].value.length <3))
		{
			document.forms[formName].elements[phoneArray[3]].focus();
			alert("Please enter valid extension number");
			return false;
		}
		if((faxCount !=0 && faxCount <13) || (faxValue <= 0 && faxCount!=0))
		{
			document.forms[formName].elements[faxArray[0]].focus();
			alert("Please enter valid fax number");
			return false;
		}            
		return true;
}

function phoneValidation(formName,arrayName)
{
		var phoneCount = 0,phoneValue = 0;		
		var spaceCheck = false;
		for(var count=0;count<arrayName.length-2;count++)
		{
			if((document.forms[formName].elements[arrayName[count]].value).indexOf(" ") == -1)
			{
				 phoneCount = phoneCount +document.forms[formName].elements[arrayName[count]].value.length;			 
				 phoneValue = phoneValue + document.forms[formName].elements[arrayName[count]].value;			 
			}
			else
			{
				 phoneCount = phoneCount +document.forms[formName].elements[arrayName[count]].value.length;			 
				 phoneValue = phoneValue + document.forms[formName].elements[arrayName[count]].value;			 
				 spaceCheck = true;
			}
		}		
		if((phoneCount!=0 && phoneCount != 10) || (phoneValue<=0 && phoneCount != 0))
		{			
			document.forms[formName].elements[arrayName[0]].focus();
			alert("Please enter valid phone number");
			return false;
		}					            
		if(spaceCheck)
		{
			document.forms[formName].elements[arrayName[0]].focus();
			alert("blank spaces not allowed for phone number");
			spaceCheck = false;
			return false;
		}
		if(isNaN(phoneValue))
		{
				document.forms[formName].elements[arrayName[0]].focus();
				alert("Charecters not allowed for phone number");
				return false;
		}
		/*if((document.forms[formName].elements[arrayName[3]].value.length !=0)&&(document.forms[formName].elements[arrayName[3]].value.length <3))
		{
			document.forms[formName].elements[arrayName[3]].focus();
			alert("Please enter valid extension number");
			return false;
		}*/
		if((document.forms[formName].elements[arrayName[3]].value).indexOf(" ")!=-1)
		{
					spaceCheck = true;
		}
		if(spaceCheck)
		{
				document.forms[formName].elements[arrayName[3]].focus();
				alert("Blank spaces not allowed for extension");
				spaceCheck = false;
				return false;
		}
		if(isNaN(document.forms[formName].elements[arrayName[3]].value))
		{
				document.forms[formName].elements[arrayName[3]].focus();
				alert("Charecters not allowed for extension");
				return false;
		}
				return true;
}

function faxValidation(formName,arrayName)
{
		/*var faxCount = 0,faxValue = 0;		
		for(var count=0;count<arrayName.length-2;count++)
		{
			 faxCount = faxCount +document.forms[formName].elements[arrayName[count]].value.length;
			 faxValue = faxValue + document.forms[formName].elements[arrayName[count]].value;			 
		}
		if((faxCount!=0 && faxCount != 10) || (faxValue<=0 && faxCount != 0))
		{
			document.forms[formName].elements[arrayName[0]].focus();
			alert("Please enter valid fax number");
			return false;
		}					 
		if((document.forms[formName].elements[arrayName[3]].value.length !=0)&&(document.forms[formName].elements[arrayName[3]].value.length <3))
		{
			document.forms[formName].elements[arrayName[3]].focus();
			alert("Please enter valid extension number");
			return false;
		}
		return true;*/
		var faxCount = 0,faxValue = 0;		
		var spaceCheck = false;
		for(var count=0;count<arrayName.length-2;count++)
		{
			if((document.forms[formName].elements[arrayName[count]].value).indexOf(" ") == -1)
			{
				 faxCount = faxCount +document.forms[formName].elements[arrayName[count]].value.length;			 
				 faxValue = faxValue + document.forms[formName].elements[arrayName[count]].value;			 
			}
			else
			{
				 faxCount = faxCount +document.forms[formName].elements[arrayName[count]].value.length;			 
				 faxValue = faxValue + document.forms[formName].elements[arrayName[count]].value;			 
				 spaceCheck = true;
			}
		}		
		if((faxCount!=0 && faxCount != 10) || (faxValue<=0 && faxCount != 0))
		{			
			document.forms[formName].elements[arrayName[0]].focus();
			alert("Please enter valid fax number");
			return false;
		}					            
		if(spaceCheck)
		{
			document.forms[formName].elements[arrayName[0]].focus();
			alert("Blank spaces not allowed for fax number");
			spaceCheck = false;
			return false;
		}
		if(isNaN(faxValue))
		{
				document.forms[formName].elements[arrayName[0]].focus();
				alert("Charecters not allowed for fax number");
				return false;
		}
		if((document.forms[formName].elements[arrayName[3]].value).indexOf(" ")!=-1)
		{
					spaceCheck = true;
		}
		if(spaceCheck)
		{
				document.forms[formName].elements[arrayName[3]].focus();
				alert("Blank spaces not allowed for extension");
				spaceCheck = false;
				return false;
		}
		if(isNaN(document.forms[formName].elements[arrayName[3]].value))
		{
				document.forms[formName].elements[arrayName[3]].focus();
				alert("Charecters not allowed for extension");
				return false;
		}
				return true;
}

function checkSocialSecurity(formName,arrayName)
{
		var socialCount = 0,socialValue = 0,spaceCheck = false;		
		for(var count=0;count<arrayName.length-1;count++)
		{
			if((document.forms[formName].elements[arrayName[count]].value).indexOf(" ") == -1)
			{
				 socialCount = socialCount +document.forms[formName].elements[arrayName[count]].value.length;
				 socialValue = socialValue + document.forms[formName].elements[arrayName[count]].value;			 
			}
			else
			{
				socialCount = socialCount +document.forms[formName].elements[arrayName[count]].value.length;
				socialValue = socialValue + document.forms[formName].elements[arrayName[count]].value;			 	
				spaceCheck = true;			
			}
		}		
		if((socialCount!=0 && socialCount != 9) || (socialValue<=0 && socialCount != 0))
		{
			document.forms[formName].elements[arrayName[0]].focus();
			alert("Please enter valid social security number");
			return false;
		}
		if(spaceCheck)
		{
				document.forms[formName].elements[arrayName[0]].focus();
				alert("Blank spaces not allowed for social security number");
				spaceChek = false;
				return false;
		}
		if(isNaN(socialValue))
		{
				document.forms[formName].elements[arrayName[0]].focus();
				alert("Charecters not allowed for social security number");
				return false;
		}
		return true;
}

/**
		****************************************************************************************
		* @Type             :    function
		* @Name            :    selectAllRecords()
		* @param           :    Nothing
		* @purpose        :    this funciton is  called to select all the check boxes when
									   user selects the main delete check box
		* @ Return Type :    boolean
		****************************************************************************************
*/

function selectAllRecords(formName,checkName,allChk)
{
		var checkLength = document.forms[formName].elements[checkName].length;
		if(document.forms[formName].elements[allChk].checked)
		{
				if(confirm("Do you want to select all records for deletion?"))
				{                        
					if(checkLength==null)
					{
								document.forms[formName].elements[checkName].checked=true;
					}
					else
					{
							for(var count=0;count<checkLength;count++)
							{
								document.forms[formName].elements[checkName][count].checked = true;
							}
					 }       
				}    
				else
				{
					document.forms[formName].elements[allChk].checked=false;
				}
		}
		else
		{
				for(var count=0;count<checkLength;count++)
				{
					document.forms[formName].elements[checkName][count].checked = false;
				}
				if(checkLength==null)
				{
						document.forms[formName].elements[checkName].checked=false;
				}
				document.forms[formName].elements[allChk].checked =false;
		}
}

		/**
		****************************************************************************************
		* @Type             :    function
		* @Name            :    checkAll()
		* @param           :    Nothing
		* @purpose        :    This function is used to check the status of check box
									   user selects the main delete check box
		* @ Return Type :    boolean
		****************************************************************************************
		*/
			
		function checkAll(formName,checkName,allChk)
		{				
				var checkBoxLength = document.forms[formName].elements[checkName].length;
				var check=0;								
				if(checkBoxLength==null)
				{
						if(document.forms[formName].elements[checkName].checked)
						{
							document.forms[formName].elements[allChk].checked =true;
						}
						else
						{
							document.forms[formName].elements[allChk].checked =false;
						}
				 }				 
				 for(var count=0;count<checkBoxLength;count++)
				 {	
						if(document.forms[formName].elements[checkName][count].checked)
						{
								check++;					
								if(check==checkBoxLength)
								{
									document.forms[formName].elements[allChk].checked =true;
								}
						}
						else
						{
								document.forms[formName].elements[allChk].checked =false;
						}
				 }
		 }
		
		
		
		/**
		****************************************************************************************
		* @Type             :    function
		* @Name             :    deleteRecords()
		* @param            :    Nothing
		* @purpose          :    this funciton is  called when user press the submit button
		 @ Return Type      :    boolean
		****************************************************************************************
		*/

		function deleteRecords(formName,checkName,bottomFrame,deleteFile,topFrame,topFile,allChk)
		{
			var checkBoxLength = document.forms[formName].elements[checkName].length;
			var checkStatus=false;
			if(checkBoxLength==null)
			 {
					if(document.forms[formName].elements[checkName].checked)
					{
						checkStatus=true;
					}    
			 }

			for(var count=0;count<checkBoxLength;count++)
			{						
				if(document.forms[formName].elements[checkName][count].checked)
				{
					checkStatus =  true;
				}
			}
			if(checkStatus)
			{
					if(confirm("Do you want to delete  the selected record(s) ?"))
					{								
								document.forms[formName].target=bottomFrame;
								document.forms[formName].action=deleteFile;
								document.forms[formName].submit();				
								document.forms[formName].action=topFile;
								document.forms[formName].target=topFrame;
					 }
					 else
					{
						 for(var count=0;count<checkBoxLength;count++)
						 {						
								if(document.forms[formName].elements[checkName][count].checked && checkBoxLength!=null)
								{
									document.forms[formName].elements[checkName][count].checked=false;
									document.forms[formName].elements[allChk].checked=false;
								}
						 }							 								
						 if(checkBoxLength==null)
						{							    
								document.forms[formName].elements[checkName].checked=false;
								document.forms[formName].elements[allChk].checked=false;
						}
							return false; 
					 }
			}
			else
			{
					alert("Please select record(s) to delete");
					return false;
			}
	 }
	 
	 
	 
	 
	 function trim(txtObj)
	{
		// trim leading spaces
		while(''+txtObj.value.charAt(0)==' ')
			txtObj.value=txtObj.value.substring(1,txtObj.value.length);		
			
		// trim trailing spaces
		while(''+txtObj.value.charAt(txtObj.value.length-1)==' ')
			txtObj.value=txtObj.value.substring(0,txtObj.value.length-1);
			
		return 	txtObj.value;
	}

