

	function valid_required(field)
		{
        	 if(field=="")          
		         {
        			 return false;
		         }
		
			 return true;
		}

	function ValidPhone(aphone)
		{
			// declare valid variable as a string with all valid characters (digits from 0 to 9 )
		    var valid = "0123456789";

			//if phone field is empty - display a warning and return false
	         if(aphone=="")
		         {
					 alert ("Contact Number is a required field.");
			         return false;
		         }
		         
			//if number of character in phone field is not equal 10 - display warning and return false
	         if(aphone.length < 10)
		         {
				    alert("Invalid phone number! Please try again.");
			         return false;
		         }

	        //check each character entered in the phone field
		     for (var i=0; i < aphone.length; i++)
		         {
			         //put in temp variable each character, one at a time.
			
			         temp = "" + aphone.substring(i, i+1);
			
				    //check index of a phone character in the "valid" variable.
				    // if temp contains a character which is not in "valid" variable,
				    //then valid.indexOf(temp) will be -1, otherwise it may be 0.1.2.3.4.5.6.7.8 or 9

			         if (valid.indexOf(temp) == "-1")
				         {
						    alert("Invalid characters in your phone. Please try again.");
					          return false;
				         }
			    }

		    //if all conditions are passed, then return true
			    return true;
		}

	function ValidDate(datefield)
		{
			//if field is empty display - warning and return false
			
	         if(datefield=="")
    		     {
				    alert("Please specify date of birth. Please enter date dd/mm/yyyy!");
			         return false;
		         }

		    //declare valid variable with all valid characters: digits from 0 to 9 and backslash
		         var valid = "0123456789/";
	
		    //declare variable for counting number of slashes

		         var slashcount = 0;
			//checking date length. If it not equals 10 - display warning and return false

		         if (datefield.length!=10)
			         {
						alert("Invalid date! The correct date format is like 'dd/mm/yyyy'. Please try again.");
				         return false;
			         }

		    //check each character in the date field, one at a time

        		 for (var i=0; i < datefield.length; i++)
			         {
				         temp = "" + datefield.substring(i, i+1);
				         //if character is backslash- count it
				         if (temp == "/")
					         slashcount++;
					    //if character in temp does not exist in valid character string, display warning	
				         if (valid.indexOf(temp) == "-1")
					         {
						         alert("Invalid characters in your date. Please try again.");
						         return false;
					         }
					}
					    //if number of slashes not equals 2 display warning
				         if (slashcount != 2)
				         {
							    alert("Invalid date! The correct date format is like 'dd/mm/yyyy'. Please try again.");
						         return false;
				         }
					
					    //check position of slashes in date string. It should be 2 and 5
					    // Because the first character position is 0 not one

				         if((datefield.charAt(2)!= '/')||( datefield.charAt(5) != '/'))
					         {
								    alert("Invalid date! The correct date format is like 'dd/mm/yyyy'. Please try again.");
							         return false;
					         }
				          
						 if(!checkDate(datefield))
						  {
						     alert("No such date exists!! Please enter a valid and existing date.")
						     return false;
						   }  
						  						  
			    return true;
		}

	function checkDate(datefield)
	 	{
	 			var day = parseInt(datefield.substr(0,2));
	 			var month = parseInt(datefield.substr(3,2));
	 			var year = parseInt(datefield.substr(6));
	 			
	 			if(month < 0 || month > 12)
	 			 {
	 			 	return false;
	 			 }	
	 			if(year % 4 == 0)
	 			 {
	 			   	if(month == 2)
	 			   		{
	 			   			if(day >= 1 && day <= 29)
	 			   			 return true;
	 			   		}
	 			   		
					else 
					 {
					 		if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 12 || month == 10)
					 		 {
					 		 		if( day >= 1 && day <= 31)
					 		 		 return true;
					 		 }
							
							else 
							 {
							 	if(day >=1 && day <= 30)				 		 
							 		return true;
							 }
					}
				}	
				
				else
					{
	 			   	if(month == 2)
	 			   		{
	 			   			if(day >= 1 && day <= 28)
	 			   			 return true;
	 			   		}
	 			   		
					else 
					 {
					 		if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 12 || month == 10)
					 		 {
					 		 		if( day >= 1 && day <= 31)
					 		 		 return true;
					 		 }
							
							else 
							 {
							 	if(day >=1 && day <= 30)				 		 
							 		return true;
							 }
					  }
			 	 }		
			 	 		
			return false;	
 		}
			  	 
	 			
	 	
	 	
	function EmailValid(aemail)
		{
			    //if email field is empty - warning displays and return false
		         if(aemail=="")
		         {
					    alert ("E-Mail is a required field. Please specify an e-mail ID.");
			    	     return false;
		         }
		         //get email string length
        
				 len = aemail.length;

		         //check position of @ character.
		         //it may be anywhere but not in the beginning or at the end of string.
		
		         if((aemail.charAt(1)=='@')||(aemail.charAt(1)=='.'))
				    {
				       alert ("Invalid email. Please enter email like myemail@mysite.com");
			           return false;
			        }
			           
		         if((aemail.charAt(len-2)=='@')||(aemail.charAt(len-2)=='.'))
				   {
				     alert ("Invalid email. Please enter email like myemail@mysite.com");
			         return false;
			       } 
			        
			    //count number of @ occurrences and number of dot occurrences
			     count=0;
		         dotcount=0;

			    for (i=0; i< aemail.length; i++)
				    {
				         if(aemail.charAt(i)=='@')
				         count++;
				
				         
          			}

			    //if @ or dot occurs not once display warning and return false
		         if((count !=1))
			         {
					    alert ("Invalid email. Please enter email like myemail@mysite.com");
				         return false;
				    }

			    return true;
		}

	function validEducation()
		 {
	 		var x = document.getElementById("eduQual");
	 		
	 		if(x.selectedIndex > 0 && x.selectedIndex < 6)
	 		 {
	 		   
	 		   return true;
	 		 }  
	 		 
	 		else
	 		 return false;
		 }
	
	function validCountry()
		 {
	 		var x = document.getElementById("resCountry");
	 		
	 		if(x.selectedIndex >= 0 && x.selectedIndex < 63)
	 		 {
	 		   
	 		   return true;
	 		 }  
	 		 
	 		else
	 		 return false;
		 }
		 
	 		 
	function validButton(thisForm) 
		{
		// validate myradiobuttons
			var myOption = -1;
			var i;
			
			for (i=(thisForm.empStatus.length)-1; i > -1; i--) 
			{
				if (thisForm.empStatus[i].checked) 
					{
						myOption = i; 
						i = -1;
					}
			}
		
			if (myOption == -1) 
			{
				return false;
			}

			return true;	
		
		}
 		  
 	function validMailAdd()
 	 {
 	 	var x = document.registerForm;
 	 	
		if(!valid_required(x.resAddr.value) && !valid_required(x.resAddr.value))
		 	{	
		 		alert("Mailing address must contain atleast one line of address.");	  
				 return false;
			}	 
		
		if(!valid_required(x.resCity.value))	  
			{
				alert("City/Town of residence not specified. Please enter correctly.");		
				 return false;
		 }
		if(!valid_required(x.resPostCode.value))	  
  		 {
  		 		alert("Postal Code is a must. Please enter a valid postal code.");
  		 		return false;
  		 }		
 
		if(x.resPostCode.length < 3)
		 {
		 		alert("Invalid postal code. Please enter a valid postal code.");
		 		return false;
		 }
		 
		return true;
	}			 		
 
	function checkYear(year)	 
		{
			if(!valid_required(year))
			 {
			    alert("Please enter the year in which you left school.");
			    return false;
			 }   
			 
			if(year.length < 4 || year < 1968)
			 {
			 	alert("Invalid school leaving year. Please enter correctly.");
			 	return false;
			 }
			 
			return true;
		}		
 
 	function validStream()
		 {
	 		var x = document.getElementById("stream");
	 		
	 		if(x.selectedIndex > 0 && x.selectedIndex < 4)
	 		 {
	 		   return true;
	 		 }  
	 		 
	 		else
	 		 return false;
		 }
		 
	function valForm()
		{

			//call valid_required function to check if field is not empty for first name
			
		        if(!valid_required(document.registerForm.firstName.value))
			         {
					    alert("First name is a required field!")
				         return false;
			         }

			//call valid_required function to check if field is not empty for last name
            	 if(!valid_required(document.registerForm.lastName.value))
			        {
					    alert("Last name is a required field!")
				         return false;
			         }

		    //call ValidDate function to validate date field value          
			    if(!ValidDate(document.registerForm.dateOfBirth.value))
		         return false;

				if(!checkYear(document.registerForm.gradYear.value))
					{
					  return false;
					}  
					
			//call validEducation() to validate educational qualification field
				if(!validStream())
				 {
				 	alert("Please select a Class XII stream.")
				 	return false;
				 }

				if(!validEducation())
				 {
				 	alert("Please select your highest educational qualification.")
				 	return false;
				 }
				
				if(!validCountry())
				 {
				 	alert("Please select your current country of residence.")
				 	return false;
				 }
 	
				if(!validButton(document.registerForm))
				 {
				 	alert("Please select your employment status.");
				 	return false;
				 }
				 	 	
				if(!valid_required(document.registerForm.resCountry.value))
			         {
					    alert("Please select your current country of residence.")
				         return false;
			         }
					
			         
			    if(!validMailAdd())
			         {
					 
				         return false;
			         }
		    // call ValidPhone function to validate phone field value
		         if(!ValidPhone(document.registerForm.contactNo.value))
		    	     {
			    	     return false;
			         }
     
 	 	
			//call Emailvalid function to validate email entered by user
		         if(!EmailValid(document.registerForm.email.value))
			         {
				         return false;
			         }

			         
			// if everything passed - return true
				return true; 
		}


