/* **********************************************************
   Module: EnterMyPC - Validations
   Created By : Nitin Sakhare
   Description: A page containing validation functions to be used.
  *********************************************************** */
// Validates maxlength 

function ValidateLength(poControl, psLength, psName)
{
	lsLength = poControl.value.length
	if (lsLength > psLength)
		{
			alert("Please enter a value less than or equal to " + psLength + " for " + psName + " field.");
			poControl.focus();
			return false;
		}
	return true;
}
// For Validation of blank field and field with only spaces
function ValidateNull(poControl, psName)
{
	var str=" ";
	var count=0;
	var string;
	var maxLength;

	string=poControl.value
	if(string=="")
		{
			alert("Please enter some value. " + psName + " cannot be blank.");
			poControl.focus();
			return false;
		}

	maxLength=string.length;
	while(count < maxLength)
	{
		if(string==str)
		{
			alert("Please enter some value. " + psName + " cannot have all blanks.")
			poControl.value="";
			poControl.focus();
			return false;
		}
		else
		{
			str+=" ";
			count++;
		}
	}
	return true;
}



// For Validation of Number
function IsNumber(poControl, psName)
{
	if (!isFinite(poControl.value))
		{
			window.alert("Please enter Number for " + psName + " field.");
			poControl.value = "";
			poControl.focus();
			return false;
		}
	return true;
}


//For Validation of Email Fields
function IsEmail(poControl, psName)
{
	var count;
	var charcount;

	charcount=0;
	email=poControl.value.toLowerCase();

	for(count=0; count<email.length; count++)
	{
		if(email.charAt(count)=='@')
		{
			if(count==0)
			{
				//alert("First character cannot be '@' for " + psName);
				//poControl.focus();
				return "First character cannot be '@' for " + psName;
			}
			else
			{
				//Check atleast one character is present after the @
				charcount+=1;
				if(charcount>1)
				{
					//more than one @ present
					//alert("There should be only one '@' for " + psName);
					//poControl.focus();
					//return false;
					return "There should be only one '@' for " + psName;
				}
				else
				{
					if(email.charAt(count+1)=="")
					{
						//alert("There should be atleast one character after '@' for " + psName);
						//poControl.focus();
						//return false;
						return "There should be atleast one character after '@' for " + psName;
					}
				}
			}
		}
	}
	if (charcount < 1 )
	{
		//alert("There should be atleast one '@' character for " + psName);
		//poControl.focus();
		//return false;	
		return "There should be atleast one '@' character for " + psName;	
	}
	return '';
}

//For Validation of Alphabetic Fields
function IsAlphabet(poControl, psName)
{	
	arg=poControl.value.toLowerCase();
	for(i=0; i<arg.length; i++)
		if(arg.substring(i,i+1)>"z" || arg.substring(i,i+1)<"a")
			if(arg.substring(i,i+1)!=" ")
			{
				alert("Invalid value for field " + psName);
				poControl.focus();
				return false;
			}
	return true;
}

//For validation of webmaster change password
function validatepassword()
{
//	alert ("submit!");
//check if old password is blank
if (frmchangepassword.txtoldpassword.value == "")
{
alert("You must enter your old password!");
frmchangepassword.txtoldpassword.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var checkStr = frmchangepassword.txtoldpassword.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}

if (!allValid)
{
alert("Please enter only letter and numeric characters in the password field.");
frmchangepassword.txtoldpassword.focus();
return (false);
}
// check to see if the field is blank
if (frmchangepassword.txtnewpassword.value == "")
{
alert("You must enter a password!");
frmchangepassword.txtnewpassword.focus();
return (false);
}

// require at least 3 characters be entered
if (frmchangepassword.txtnewpassword.value.length < 8)
{
alert("Please enter at least 8 characters in the password field.");
frmchangepassword.txtnewpassword.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
checkStr = frmchangepassword.txtnewpassword.value;
allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the password field.");
frmchangepassword.txtnewpassword.focus();
return (false);
}

if (frmchangepassword.txtnewpassword.value!=frmchangepassword.txtnewpassword1.value)
{
alert("Please enter identical values when confirming your password!");
frmchangepassword.txtnewpassword1.focus();
return (false);
}

frmchangepassword.pwdchkval.value="valok";
return (true);
}
//For showing disclaimer
function ShowDisclaimer()
{
	window.open("disclaimer.htm");
}


function validalphanumeric(poControl, psName)
{
	// allow ONLY alphanumeric keys, no symbols or punctuation
	// this can be altered for any "checkOK" string you desire
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = poControl.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		{	if (ch == checkOK.charAt(j))
				break;
			if (ch == " ")
				break;
		}
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
	
	}
	if (!allValid)
	{
		alert("Please enter only letter and numeric characters in the " + psName);
		poControl.focus();
		return (false);
	}
	return true;
}

function IsAlphabetonly(poControl, psName)
{	var i;
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var checkStr = poControl.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
	
	}
	if (!allValid)
	{
		alert("Please enter only letters in field " + psName);
		poControl.focus();
		return (false);
	}
	return true;
}

function validparam(poControl,psName)
{	var i;
	var arg=poControl.value.toLowerCase();
	for(i=0; i<arg.length; i++)
		if(arg.charAt(i)=="'" || arg.charAt(i)=="\"")
			{
				alert("Remove single code OR double code from " + psName);
				poControl.focus();
				return false;
			}
			
	return true;
}

function IsValidDate(poDateControl, psName) 
{ 
var monthalphaarray=new Array ("###","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
var montharray=new Array ("01","02","03","04","05","06","07","08","09","10","11","12"); 
var dayarray=new Array ("31","28","31","30","31","30","31","31","30","31","30","31"); 
var validationflag=1; 
var leapyrflag=0; 
var DateString=new String(poDateControl.value); 
var len=DateString.length; 

if(len == 0) 
	return true;


if(len > 10 ) 
{ 
alert("Invalid Date value for " + psName); 
poDateControl.focus(); 
return false; 
} 

var char1=DateString.charAt(2); 
var char2=DateString.charAt(5); 
//var dayvalue=DateString.substring(0,2);  for dd-mm-yyyy
//var monthvalue=DateString.substring(3,5); for dd-mm-yyyy
var monthvalue=DateString.substring(0,2); 
var dayvalue=DateString.substring(3,5); 

var yearvalue=DateString.substring(6,10); 

if (((parseInt(monthvalue,10) > 0) && (parseInt(monthvalue,10) < 13))) 
validationflag=1; 
else 
validationflag=0; 

if (((parseInt(dayvalue,10) < 1) || (parseInt(dayvalue,10) > 31))) 
validationflag=0; 

if (((parseInt(yearvalue,10) < 1) || (parseInt(yearvalue,10) >2100))) 
validationflag=0 ; 

if ((monthvalue.length !=2)||(dayvalue.length !=2)||(yearvalue.length !=4)) 
validationflag=0; 

if ( (parseInt(yearvalue,10)%4==0 && parseInt(yearvalue,10)%100 !=0) || (parseInt(yearvalue,10)%400==0) ) 
{ 
leapyrflag=1; 
if(monthvalue=="02") 
if(parseInt(dayvalue,10) > 29) 
validationflag=0; 
} 
else 
{ 
if(monthvalue=="02") 
if(parseInt(dayvalue,10) > 28) 
validationflag=0; 
} 

if (validationflag==0) 
{ 
alert("Invalid Date value for " + psName + "\n\n" + "Valid Date Format: MM/DD/YYYY"); 
poDateControl.focus(); 
return false; 
} 
else 
{ 
for(var j=0;j<12;j=j+1) 
{ 
if(monthvalue==montharray[j]) 
if(dayvalue>dayarray[j]) 
{ 
if(leapyrflag==1) 
{ 
if(monthvalue!="02") 
{ 
alert("Date Entered, " + dayvalue + " Exceeds Maximum No of Days for the Entered Month, " + monthalphaarray[parseInt(monthvalue,10)] + " for " + psName); 
return false; 
} 
} 
else 
{ 
alert("Date Entered, " + dayvalue + " Exceeds Maximum No of Days for the Entered Month, " + monthalphaarray[parseInt(monthvalue,10)] + " for " + psName); 
return false; 
} 
} 
} 
} 
return true; 
} 



