/* **********************************************************
   Module: EnterMyPC - Validations
   Created By : Nitin Sakhare
   Description: A page containing validation functions to be used.
  *********************************************************** */
// Validates maxlength 

function replacestring(arg,rstr)
{
      	if(arg.indexOf(rstr) >= 0)
        {
          arg = arg.replace(rstr,"");
          arg = replacestring(arg,rstr);
        }
        return arg;
}



function ValidateLength1(poControl, psLength, psName)
{
	str= replacestring(TrimSpace(poControl.value),",");

        lsLength = str.length
	if (lsLength > psLength)
		{
			alert("Please enter a value less than or equal to " + psLength + " for " + psName + " field.");
			poControl.focus();
			return false;
		}
	return true;
}


function IsNumber1(poControl, psName)
{
	
           if (!isFinite(replacestring(TrimSpace(poControl.value),",")))
		{
			window.alert("Please enter Number for " + psName + " field.");
			poControl.value = "";
			poControl.focus();
			return false;
		}
	return true;
}

function ValidateLength(poControl, psLength, psName)
{
	lsLength = TrimSpace(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;
}


function ValidateLengthmsg(poControl, psLength,psName)
{
	lsLength = TrimSpace(poControl.value).length
	string=TrimSpace(poControl.value)
	if(string=="")
		{
			alert(psName);
			poControl.focus();
			return false;
		}

		
        else if (lsLength > psLength)
		{
			alert(psName);
			poControl.focus();
			return false;
		}
        else if (!isFinite(TrimSpace(poControl.value)))
		{
			alert(psName);
			poControl.value = "";
			poControl.focus();
			return false;
		}

	else
             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=TrimSpace(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(TrimSpace(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 false;
			}
			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;
				}
				else
				{
					if(email.charAt(count+1)=="")
					{
						alert("There should be atleast one character after '@' for " + psName);
						poControl.focus();
						return false;
					}
				}
			}
		}
	}
	if (charcount < 1 )
	{
		alert("There should be atleast one '@' character for " + psName);
		poControl.focus();
		return false;		
	}
	return true;
}

//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; 
} 




function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1)
{
return"";
}

TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);

if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


// function to remove &nbsp;

function TrimSpace(str)
{

  return str.replace(/\u00a0/g,'');
}



function Validatefield(poControl,psLength, psName,startrange,endrange,blankfield)
{  
  //blankfield =true (if null is allowed )    
 var fieldvalue ;
     fieldvalue =  poControl.value;
     fieldvalue =TrimSpace(fieldvalue);
  
 var lsLength =fieldvalue.length;
 
 if (blankfield == true) 
    msg=",or leave a blank";
 else
    msg="";
 
 if (lsLength != 0 )
 {
     if (!isFinite(fieldvalue))
     {
	window.alert("Please enter a value between " + startrange +" and " + endrange + msg +" for " + psName + " field.");
	if (poControl.disabled == false)
        {	
           poControl.value="";
	   poControl.focus();
        }
	   return false;
	}     
     else if (lsLength > psLength)
     {
	   window.alert("Please enter a value between " + startrange +" and " + endrange + msg +" for " + psName + " field.");
	   if (poControl.disabled == false)
           {	
            poControl.value="";
	    poControl.focus();
           }
  	   return false;
     }
     else if ((fieldvalue < startrange) || (fieldvalue > endrange))
     {
           window.alert("Please enter a value between " + startrange +" and " + endrange + msg +" for " + psName + " field.");
           if (poControl.disabled == false)
           {	
                poControl.value="";
		poControl.focus();
           }
		return false;
     }
  }
  else
  {
      if (blankfield == false) 
      {

      window.alert("Please enter a value between " + startrange +" and " + endrange + msg +" for " + psName + " field.");
       poControl.focus();
       return false;   
      }
  }
}



