//-----------------------------------------------------------
/*
Program name :- chicpercook.js
Author :- Bharathi PV
Created Date :- 15th Sep 2006
Last Modified by :-Bharathi PV
Last Modified date :- 11th Oct 2006
Purpose: This program is for numeric and Email validatons
*/
//------------------------------------------------
//Validation function for Email
/*function EmailVal(email)
{
	var re1=true;
	apos=email.indexOf("@");
	dotpos=email.lastIndexOf(".");
	laststr=email.substring(dotpos+1,email.length+1);
	lenlstr=laststr.length;
	if (apos<1||dotpos-apos<2||lenlstr<1)
	  re1=false;
	return re1;
}
//-------------------------------------------------------------------------
*/
/*
	Parameter : string email  
    RETURN: Boolean value TRUE/FALSE
*/
//---Email validation function------------------------------------------
function EmailVal (emailStr) 
{
var strValidChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var strValidUserChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890._&";
var strValidDomainChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.@";
/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

var matchArray=emailStr.match(emailPat);

if (matchArray==null) 
{

	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */
	return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic (0-9)(a-z)(A-Z)(._&) present
var strChar=user.charAt(0);
if(strValidChars.indexOf(strChar) == -1)
{
	return false;
}
if(chkNumericAndChars(user,strValidUserChars)==false)
{
	return false;
}

if(chkNumericAndChars(domain,strValidDomainChars)==false)
{
	return false;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) 
{
	if (domArr[i].search(atomPat)==-1)
	{
		return false;
	}
	if(isNaN(domArr[i])==false)
	{
		return false;
	}
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) 
{
	return false;
}

// Make sure there's a host name preceding the domain.

if (len<2)
{
	return false;
}

// If we've gotten this far, everything's valid!
	return true;
}
//-----------------------------------------------------------------------

//--------------validation for numeric and char entry----------------------------------------
/*
parameters:value,cahracterset
return bool value
*/
function chkNumericAndChars(numb,allowChars)
 {
	var strValidChars =allowChars;
	var strChar;
	var blnres=true;
	var flag=0;
	for(i=0;i<numb.length;i++)
	{
	  strChar = numb.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
	  {
		blnres=false;	  
		break;
	  }
	}
	return blnres;
  }
//---------Validation for Numerics entry--------------------
function chkNumeric(numb)
{
	var strValidChars = "0123456789.-";
    var strChar;
	var blnres=true;
	var flag=0;
	for(i=0;i<numb.length;i++)
	{
	  strChar = numb.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
	  {
		blnres=false;	  
		break;
	  }
	}
	return blnres;
}
//------------------------------------------------------------

//-------Login form Validation----------------------
function loginOnSubVal(f)
{
	//var f=document.frmLogin;
	var txtPw1=f.txtPw.value;
	//alert(txtPw1);
	f.username.value=f.txtUserId.value;
	f.password.value=f.txtPw.value;
	//alert(f.txtUserId.value);
	//alert(f.username.value);
	if(f.txtUserId.value=="")
	{
		alert("Enter User Id");
		f.txtUserId.focus();
		return false;
	}
	else if(f.txtPw.value.length==0)
	{
		alert("Enter Password");
		f.txtPw.focus();
		return false;
	}
	else if(f.txtPw.value.length!=0 && txtPw1.length<6)
	{
			alert("Password length should be minimum 6 characters");
			f.txtPw.focus();
			return false;
		
	}
	else if(f.txtUserId.value!="")
	{
		if(f.chkpercookie.checked==true)
		{
			writePersistentCookie('UserId',f.txtUserId.value);
			writePersistentCookie('Psw',f.txtPw.value);
		}
		return true;
	}
	else
	{
		return true;
	}
  }
  //----------------------------------------------------------------------------------------------------------

  //---------Registration form validation------------------------------------------
function regOnSubVal(f)
{
	  //var f=document.frmReg;
	 //alert(f.txtFirstName);
	 //var txtPwLen=f.txtPw.value.length;
	 var username=f.txtusername.value;
	 var firstname=f.txtFirstName.value;
	 var lastname=f.txtLastName.value;
	 var pw1=f.txtPw.value;
     var pw2=f.txtRePw.value;
	 if(username=="")
     {
			alert("Enter User Name");
			return false;
	 }
	 else if(username.length<4)
	 {
		 alert("Please Enter Minimum 4 Characters for Username");
		 return false;
	 }
	 else if(pw1.length=="0")
	 {
		alert("Enter Password");
		//alert(f.txtPw.value.length);
		f.txtPw.focus();
		return false;
	 }
	 else if(pw1.length!=0 && pw1.length<=5)
	 {
			alert("Please Enter Minimum 6 Characters for Password");
			f.txtPw.focus();
			return false;
		
	 }
	 else if(pw2.value=="")
	 {
		alert("Please Retype the Password");
		f.txtRePw.focus();
		return false;
	 }
	 else if(firstname=="")
	 {
		alert("Enter Firstname");
		f.txtFirstName.focus();
		return false;
	 }
	 else if(firstname.length<4)
	 {
		alert("Plz Enter Minimum 4 Characters for Firstname");
		f.txtFirstName.focus();
		return false;
	 }
	 else if((firstname!="" && isNaN(firstname)==false) || (firstname!="" && isNaN(firstname.charAt(0))==false))
	 {
		alert("Please Enter Valid Firstname");
		f.txtFirstName.focus();
		return false;
	 }
	 else if(lastname=="")
	 {
		alert("Enter Lastname");
		f.txtLastName.focus();
		return false;
	 }
	 else if(lastname.length<4)
	 {
		alert("Please Enter Minimum 4 Characters for Lastname");
		f.txtLastName.focus();
		return false;
	 }
	 else if((lastname!="" && isNaN(lastname)==false) || (lastname!="" && isNaN(lastname.charAt(0))==false))
	 {
		alert("Plz Enter Valid Lastname");
		f.txtLastName.focus();
		return false;
	 }
	 else if(f.txtEmail.value=="")
	 {
		alert("Please Enter Email Id");
		f.txtEmail.focus();
		return false;
	 }
	 else if(f.selYear.value=="Year" || f.selYear.value=="")
	 {
		alert("Please Select Birth Year");
		f.selYear.focus();
		return false;
	 }
	 else if(f.selMonth.value=="Month" || f.selMonth.value=="")
	 {
		alert("Please Select Birth Month");
		f.selMonth.focus();
		return false;
	 }
	 else if(f.selDay.value=="Day" || f.selDay.value=="")
	 {
		alert("Please Select Birth Day");
		f.selDay.focus();
		return false;
	 }
     else if(f.selGender[0].checked==false && f.selGender[1].checked==false)
	 {
		alert("Please Select Gender");
		//f.selGender.focus();
		return false;
	 }
	 else if(f.txtAdd1.value=="" || f.txtAdd1.value=="ADDRESS")
	 {
		alert("Please Enter Address");
		f.txtAdd1.focus();
		return false;
	 }
	 else if(f.txtAdd1.value.length>60)
	 {
		alert("Please Enter Maximum 60 characters for Address");
		f.txtAdd1.focus();
		return false;
	 }
	 else if(f.txtCity.value=="")
	 {
		alert("Please Enter City");
		f.txtCity.focus();
		return false;
	 }
	 else if((f.txtCity.value!="" && isNaN(f.txtCity.value)==false) || (f.txtCity.value!="" && isNaN(f.txtCity.value.charAt(0))==false))
	 {
		alert("Please Enter valid City");
		f.txtCity.focus();
		return false;
	 }
	 else if(f.selState.value=="")
	 {
		alert("Please Select State");
		f.selState.focus();
		return false;
	 }
	 else if((f.selState.value!="" && isNaN(f.selState.value)==false) || (f.selState.value!="" && isNaN(f.selState.value.charAt(0))==false))
	 {
		alert("Please Enter valid State");
		f.selState.focus();
		return false;
	 }
	 else if(f.selCountry.value=="")
	 {
		alert("Please Select Country");
		f.selCountry.focus();
		return false;
	 }
	 else if(f.txtZip.value=="")
	 {
		alert("Please Enter Postal Zipcode");
		f.txtZip.focus();
		return false;
    }
    else
    {}
	// (User Name, Password, Password Confirm, First Name and Last Name) to characters containing only 0-9, a-z, A-Z and underscore?

	 var chkuser=chkNumericAndChars(username,"0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	 if(chkuser==false)
	 {
		 alert("Please Enter Valid Username\n Use only 0-9,a-z,A-Z and Underscore");
		 return chkuser;
	 }
	 var chkpw1=chkNumericAndChars(pw1,"0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	 if(chkpw1==false)
	 {
		 alert("Please Enter Valid Password \n Use only 0-9,a-z,A-Z and Underscore");
		 return chkpw1;
 	 }
	 var chkfname=chkNumericAndChars(firstname,"0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	 if(chkfname==false)
	{
		alert("Please Enter Valid Firstname \n Use only 0-9,a-z,A-Z and Underscore");
		return chkfname;
	}
	var chklname=chkNumericAndChars(lastname,"0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	if(chklname==false)
	{
		alert("Please Enter Valid Lastname \n Use only 0-9,a-z,A-Z and Underscore");
		return chklname;
	}

	 if(f.txtEmail.value!="")
	 {
		var re=EmailVal(f.txtEmail.value);
		if(re==false)
		{
			alert("Pleasse Enter Valid Mail Id");
			f.txtEmail.focus();
			return re;
		}
	 }
	 if(pw1!=pw2)
	 {
	 	alert("Password Entries are Not Matched");
		f.txtPw.value="";
		f.txtRePw.value="";
		return false;
	}
	if(f.txtZip.value!="")		
	{
		var zip=f.txtZip.value
		if(zip.length<5)
		{
			alert("Enter Valid Zip code ");
			return false;
		}
	    else
		{
			var rz=chkNumeric(zip);  
			if(rz==false)
			{
				alert("Enter Valid Zip code");
				f.txtZip.focus();
				return rz;
			}
			else
			{
				if(f.txtSecurity_code.value=="")
				{
					 alert("Enter The Security Code in Text Box");
					 return false;
				}
			}
		}
	}
	var agr=f.chkAgr.checked;
	if(agr==false)
	{
		alert("If You Want To Continue Registration Please Agree Terms And Services");
		return false;
	}
	else
	{
		//writePersistentCookie("selyear",f.selYear.value);
		//writePersistentCookie("selday",f.selDay.value);
		return true;
	}

					
 }
 //------------------------------------------------------------------------------------------------

 //-----retrieve password form validation------------------------------------------------------
 function rpwsubval(f)
 {
	if(f.txtusername.value=="")
	{
		alert("Please Enter Username");
		return false
	}
	else 
	{
		return true;
	}
 }
 //------------------------------------------------------------------------------------------------


//----Edit profile validation-----------------------------------------------------------------
function editprofileOnSubVal(f)
{
	 var firstname=f.txtFirstName.value;
	 var lastname=f.txtLastName.value;
	 if(firstname=="")
	 {
		alert("Enter Firstname");
		f.txtFirstName.focus();
		return false;
	 }
	 else if(firstname.length<4)
	 {
		alert("Plz Enter Minimum 4 Characters for Firstname");
		f.txtFirstName.focus();
		return false;
	 }
	 else if((firstname!="" && isNaN(firstname)==false) || (firstname!="" && isNaN(firstname.charAt(0))==false))
	 {
		alert("Please Enter Valid Firstname");
		f.txtFirstName.focus();
		return false;
	 }
	 else if(lastname=="")
	 {
		alert("Enter Lastname");
		f.txtLastName.focus();
		return false;
	 }
	 else if(lastname.length<4)
	 {
		alert("Please Enter Minimum 4 Characters for Lastname");
		f.txtLastName.focus();
		return false;
	 }
	 else if((lastname!="" && isNaN(lastname)==false) || (lastname!="" && isNaN(lastname.charAt(0))==false))
	 {
		alert("Plz Enter Valid Lastname");
		f.txtLastName.focus();
		return false;
	 }
	 else if(f.txtEmail.value=="")
	 {
		alert("Please Enter Email Id");
		f.txtEmail.focus();
		return false;
	 }
	 else if(f.selYear.value=="Year" || f.selYear.value=="")
	 {
		alert("Please Select Birth Year");
		f.selYear.focus();
		return false;
	 }
	 else if(f.selMonth.value=="Month" || f.selMonth.value=="")
	 {
		alert("Please Select Birth Month");
		f.selMonth.focus();
		return false;
	 }
	 else if(f.selDay.value=="Day" || f.selDay.value=="")
	 {
		alert("Please Select Birth Day");
		f.selDay.focus();
		return false;
	 }
     else if(f.selGender[0].checked==false && f.selGender[1].checked==false)
	 {
		alert("Please Select Gender");
		//f.selGender.focus();
		return false;
	 }
	 else if(f.txtAdd1.value=="" || f.txtAdd1.value=="ADDRESS")
	 {
		alert("Please Enter Address");
		f.txtAdd1.focus();
		return false;
	 }
	 else if(f.txtAdd1.value.length>60)
	 {
		alert("Please Enter Maximum 60 characters for Address");
		f.txtAdd1.focus();
		return false;
	 }
	 else if(f.txtCity.value=="")
	 {
		alert("Please Enter City");
		f.txtCity.focus();
		return false;
	 }
	 else if((f.txtCity.value!="" && isNaN(f.txtCity.value)==false) || (f.txtCity.value!="" && isNaN(f.txtCity.value.charAt(0))==false))
	 {
		alert("Please Enter valid City");
		f.txtCity.focus();
		return false;
	 }
	 else if(f.selState.value=="")
	 {
		alert("Please Select State");
		f.selState.focus();
		return false;
	 }
	 else if((f.selState.value!="" && isNaN(f.selState.value)==false) || (f.selState.value!="" && isNaN(f.selState.value.charAt(0))==false))
	 {
		alert("Please Enter valid State");
		f.selState.focus();
		return false;
	 }
	 else if(f.selCountry.value=="")
	 {
		alert("Please Select Country");
		f.selCountry.focus();
		return false;
	 }
	 else if(f.txtZip.value=="")
	 {
		alert("Please Enter Postal Zipcode");
		f.txtZip.focus();
		return false;
    }
    else
    {}
	// (User Name, Password, Password Confirm, First Name and Last Name) to characters containing only 0-9, a-z, A-Z and underscore?

	var chkfname=chkNumericAndChars(firstname,"0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	if(chkfname==false)
	{
		alert("Please Enter Valid Firstname \n Use only 0-9,a-z,A-Z and Underscore");
		return chkfname;
	}
	var chklname=chkNumericAndChars(lastname,"0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	if(chklname==false)
	{
		alert("Please Enter Valid Lastname \n Use only 0-9,a-z,A-Z and Underscore");
		return chklname;
	}

	 if(f.txtEmail.value!="")
	 {
		var re=EmailVal(f.txtEmail.value);
		if(re==false)
		{
			alert("Pleasse Enter Valid Mail Id");
			f.txtEmail.focus();
			return re;
		}
	 }
	if(f.txtZip.value!="")		
	{
		var zip=f.txtZip.value
		if(zip.length<5)
		{
			alert("Enter Valid Zip code ");
			return false;
		}
	    else
		{
			var rz=chkNumeric(zip);  
			if(rz==false)
			{
				alert("Enter Valid Zip code");
				f.txtZip.focus();
				return rz;
			}
			else
			{
				if(f.txtSecurity_code.value=="")
				{
					 alert("Enter The Security Code in Text Box");
					 return false;
				}
				else
				{
					return true;
				}
			}
		}
	}
	else
	{
		return true;
	}
	
}
 //--------------------------------------------------------------------------------------------

 //----------------------------------------------------------------------------------------
 function userreportValid(f)
{
	//alert(f);
	//alert(f.fromDate.value);
	//alert(f.date[0].checked);
	//alert(frm.date.checked);
	if (f.date[0].checked==false)
	{
		//alert(f.fromDate.value);
		var eventStartDate=f.fromDate.value;
		var eventEndDate=f.toDate.value;
		//alert(eventStartDate);
		if(eventStartDate == "")
		{
			alert('Plz Enter From Date');
			f.fromDate.focus();
			return false;
		}
		else if(eventEndDate == "")
		{
			alert('Plz Enter To Date');
			f.toDate.focus();
			return false;
		}
		else if(eventStartDate!="" && eventEndDate!="")
		{ 
			 var StartDate=eventStartDate.split("-");
			 var EndDate=eventEndDate.split("-");
			  evstartdate=StartDate[2];
			  evstartmonth=StartDate[1];
			  evstartyear=StartDate[0];
			  evenddate=EndDate[2];
			  evendmonth=EndDate[1];
			  evendyear=EndDate[0];
			
			   if(!(
				  (
				  (parseInt(evendyear)==parseInt(evstartyear)) && (parseInt(evstartmonth,10)==parseInt(evendmonth,10)) && (parseInt(evstartdate,10)<parseInt(evenddate,10))
				  ) 
				  ||
				  (
				  (parseInt(evstartyear)==parseInt(evendyear)) && (parseInt(evstartmonth,10)<parseInt(evendmonth,10))
				  ) 
				  || 
				  (parseInt(evstartyear)<parseInt(evendyear))))
				{ 
					alert('To Date should be Greater than From Date');
					f.toDate.focus();
				    return false;	 
				}
		 }
		 else
		 {
			 return true;
		 }
	}
	else
	{
		return true;
	}
}
 //----------------------------------------------------------------------------------------
