// JavaScript Document

//***************************************
// File		:	Validation.js
// Author	: 	A.Ansari, Programmer, Emantras Interactive Technologies Pvt, Ltd
// Email	:  	ansari@emantras.com
// Date		:   April 25, 2006
// Comments	: 	The code for the Validation,  was originally 
//				written by A.Ansari, Programmer, Emantras Interactive Technologies Pvt, Ltd
//				for the specific project "Simulation International"
// ****************************************


//function for login screen starts here
	
	function validate(theForm){
		
		if (trim(theForm.txtuname.value) == 0){						//User Name Validation checking the  value is not null 
			alert("Please enter UserName");							//Alert Message to enter the user name
			theForm.txtuname.focus();								//Focus the user name field
			return false; 											//return the value false back to the function in the page
		}
		
		if (trim(theForm.txtpwd.value) == 0){						//Password Validation for checking the value is not null 
			alert("Please enter Password");							//Alert Message to enter the Password
			theForm.txtpwd.focus();									//Focus the Password field
			return false; 											//return the value false back to the function in the page
		}
		
		return true; 												//Return the value true if the coditons are false
	}

//function for login screen ends here



//function for Contactusform

function validateContact(theForm){
	
	
	//Validation for FirstName 
			if (trim(theForm.txtfname.value) == 0){
				alert("Please enter First Name");
				theForm.txtfname.focus();
				return false; 
			}
			else if( alphabets(trim(theForm.txtfname.value)) == false ){
				alert("First Name: Enter only alphabets");
				theForm.txtfname.focus();
				return false; 
			}
			
			//Validation LastName
			if (trim(theForm.txtlname.value) == 0){
				alert("Please enter Last Name");
				theForm.txtlname.focus();
				return false; 
			}
			else if( alphabets(trim(theForm.txtlname.value)) == false ){
				alert("Last Name: Enter only alphabets");
				theForm.txtlname.focus();
				return false; 
			}
			
			//Validation for Email ID
			if (trim(theForm.txtemail.value) == 0){
				alert("Please enter valid Email Id");
				theForm.txtemail.focus();
				return false; 
			}
			else if( isEmail(trim(theForm.txtemail.value)) == false ){
				alert("Invalid Email Id");
				theForm.txtemail.focus();
				return false; 
			}
			
			
			//Validation for Phone number
			
			var Phone=theForm.txtphone
			
			if ((Phone.value!=null)||(Phone.value!="")){
				if (checkInternationalPhone(Phone.value)==false){
					alert("Please Enter a Valid Phone Number")
					//Phone.value=""
					Phone.focus()
					return false
				}
			}
			
			
			
			//Validation for Company
			
			if (trim(theForm.txtcompany.value) == 0 && trim(theForm.txtother.value) == 0){
				alert("Please enter Company / Organization or If other, please specify");
				theForm.txtcompany.focus();
				return false; 
			}
			
			
			//Validation for Country
			
			strCountry = theForm.Cbocountry.value
			
			
			if (strCountry == ""){
				alert("Please Select a Country");
				theForm.Cbocountry.focus();
				return false; 
			}
			
			//Validation for Area of Interest
			
			strArea = theForm.cboArea.value
			
			if (strArea == ""){
				alert("Please Select a Area of Interest");
				theForm.cboArea.focus();
				return false; 
			}
			
			//Validation for Comments
			
			if (trim(theForm.txtComment.value) == 0){
				alert("Please enter your Comments");
				theForm.txtlname.focus();
				return false; 
			}
			
			return true;
	

}


//FunctionEnds here


//Function for User Registration (validation1)

		function validate1(theForm){
		
			//Validation for FirstName 
			if (trim(theForm.txtfname.value) == 0){
				alert("Please enter First Name");
				theForm.txtfname.focus();
				return false; 
			}
			else if( alphabets(trim(theForm.txtfname.value)) == false ){
				alert("First Name: Enter only alphabets");
				theForm.txtfname.focus();
				return false; 
			}
			
			//Validation LastName
			if (trim(theForm.txtlname.value) == 0){
				alert("Please enter Last Name");
				theForm.txtlname.focus();
				return false; 
			}
			else if( alphabets(trim(theForm.txtlname.value)) == false ){
				alert("Last Name: Enter only alphabets");
				theForm.txtlname.focus();
				return false; 
			}
		
			//Validation for UserName
			if (trim(theForm.txtuname.value) == 0){
				alert("Please enter UserName");
				theForm.txtuname.focus();
				return false; 
			}
			
			//Validation for Email ID
			if (trim(theForm.txtemail.value) == 0){
				alert("Please enter valid Email Id");
				theForm.txtemail.focus();
				return false; 
			}
			else if( isEmail(trim(theForm.txtemail.value)) == false ){
				alert("Invalid Email Id");
				theForm.txtemail.focus();
				return false; 
			}
			
			//Validation for Display Name
			
			if (trim(theForm.txtdisplayname.value) == 0){
				alert("Please enter Display Name");
				theForm.txtdisplayname.focus();
				return false; 
			}
			else if( alphaNumeric(trim(theForm.txtdisplayname.value)) == false ){
				alert("Display Name: Enter only alphanumeric word and including (_ and -)");
				theForm.txtdisplayname.focus();
				return false; 
			}
			
			//Date of birth Validation
			if (trim(theForm.txtmon.value) == 0)
					{
					alert("Please enter birth Day field");
					theForm.txtmon.focus();
					return false; 
					}
				else if( IsNumeric(trim(theForm.txtmon.value) )== false )
					{
					alert("Birth Day field: Enter only numeric value");
					theForm.txtmon.focus();
					return false; 
					}
					
			if (trim(theForm.txtmon.value) < 1 || trim(theForm.txtmon.value) > 12)
			{
					alert("Invalid birth Day field value");
					theForm.txtmon.focus();
					return false; 
			}
			
			if (theForm.txtmon.value.length != 2 )
			{
					alert("Invalid birth Day field format (mm)");
					theForm.txtmon.focus();
					return false; 
			}
		//Date Vallue
			if (trim(theForm.txtdat.value) == 0){
				alert("Please enter birth Day field");
				theForm.txtdat.focus();
				return false; 
			}
			else if( IsNumeric(trim(theForm.txtdat.value) )== false ){
				alert("Birth Day field: Enter only numeric value");
				theForm.txtdat.focus();
				return false; 
			}
				
				if (trim(theForm.txtdat.value) < 1 || trim(theForm.txtdat.value) > 31){
					alert("Invalid birth Day value");
					theForm.txtdat.focus();
					return false; 
				}
			
			if (theForm.txtdat.value.length != 2 )
			{
					alert("Invalid birth Day field format (dd)");
					theForm.txtdat.focus();
					return false; 
			}	
		
			//Year Value
					if (trim(theForm.txtyy.value) == 0)
					{
					alert("Please enter birth Day field");
					theForm.txtyy.focus();
					return false; 
					}
				else if( IsNumeric(trim(theForm.txtyy.value) )== false )
					{
					alert("birth Day field: Enter only numeric value");
					theForm.txtyy.focus();
					return false; 
					}
				
					if (trim(theForm.txtyy.value) < 1900 )
					{
						alert("Invalid value");
					theForm.txtyy.focus();
					return false; 
			}
			
			if (theForm.txtyy.value.length != 4 )
			{
					alert("Invalid birth Day format (yyyy)");
					theForm.txtyy.focus();
					return false; 
			}
			
			
			//City / Town Validation
			
			if (trim(theForm.txtcity.value) != 0){
				if( alphabets1(trim(theForm.txtcity.value)) == false ){
					alert("City / Town: Enter alphabets only");
					theForm.txtcity.focus();
					return false; 
				}
			}
			
			//Region / state validation
			if (trim(theForm.txtstate.value) != 0){
				if( alphabets1(trim(theForm.txtstate.value)) == false ){
					alert("Region / state: Enter alphabets only");
					theForm.txtstate.focus();
					return false; 
				}
			}
			
			//Validation for zip or pin code
			var zip = trim(theForm.txtzip.value);
			if (trim(theForm.txtzip.value) != 0){
				if( IsNumeric(trim(theForm.txtzip.value) )== false ){
					alert("Zip / Postal code: Enter only Numerics");
					theForm.txtzip.focus();
					return false; 
				}
				
				if (zip.length < 5 ){
					alert("Invalid Zip / Postal code");
					theForm.txtzip.focus();
					return false; 
				}
			}
			
			//Validation for Country
			
			if (trim(theForm.txtcountry.value) != 0){
				if( alphabets1(trim(theForm.txtcountry.value)) == false){
					alert("Country: Enter only alphabets");
					theForm.txtcountry.focus();
					return false; 
				}
			}
			
			//Validation for Phone number
			
			var Phone=theForm.txtphone
			
			if ((Phone.value!=null)||(Phone.value!="")){
				if (checkInternationalPhone(Phone.value)==false){
					alert("Please Enter a Valid Phone Number")
					Phone.value=""
					Phone.focus()
					return false
				}
			}
			return true;  
		}

//Validation for regiter user end here

//Function for News and Wat's hot starts here (Validation2)
	
	function validate2(theForm){
	
			var head;	
		head=theForm.txtheading.value;
		
		if (head=='')
		{
			alert('Enter Heading')
			theForm.txtheading.focus();
			return false;
		}
		
		var content;
		content=theForm.txtcontent.value;
		
		if (content=='')
		{
			alert('Enter Content')
			theForm.txtcontent.focus();
			return false;
		}
	}
	

//Function for news and wat's hot ends here

//Function for contactus valiation starts here

	function validate3(theForm){
		
		if (trim(theForm.txtemail.value) == 0){
			alert("Please enter valid Email Id");
			theForm.txtemail.focus();
			return false; 
		}
		else if( isEmail(trim(theForm.txtemail.value)) == false ){
			alert("Invalid Email Id");
			theForm.txtemail.focus();
			return false; 
		}
		
		var head;	
		head=theForm.txtheading.value;
		
		if (head=='')
		{
			alert('Enter subject')
			theForm.txtheading.focus();
			return false;
		}
		
		var content;
		content=theForm.txtcontent.value;
		
		if (content=='')
		{
			alert('Enter message')
			theForm.txtcontent.focus();
			return false;
		}
	}
	

//Function for contactus validation ends here


//Function Nesletter starts here

	function validate4(theForm){
		
		var head;	
		head = theForm.txtheading.value;
		
		if (head==''){
			alert('Enter Name')
			theForm.txtheading.focus();
			return false;
		}
		
		
		if (trim(theForm.txtemail.value) == 0){
			alert("Please enter valid Email Id");
			theForm.txtemail.focus();
			return false; 
		}
		else if( isEmail(trim(theForm.txtemail.value)) == false ){
			alert("Invalid Email Id");
			theForm.txtemail.focus();
			return false; 
		}
		
		
		
	}

//Function Newsletter end here


//Function for alphabets starts here

		function alphabets(sText){
		   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		   var IsNumber=true;
		   var Char; 
		   for (i = 0; i < sText.length && IsNumber == true; i++){ 
			  Char = sText.charAt(i); 
			  if (ValidChars.indexOf(Char) == -1){
				 IsNumber = false;
			  }
		   }
		   return IsNumber;   
		 }
		 
//Function for alphabets end here

//Function for alphabets1 (including space) starts here

		function alphabets1(sText){
		   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		   var IsNumber=true;
		   var Char; 
		   for (i = 0; i < sText.length && IsNumber == true; i++){ 
			  Char = sText.charAt(i); 
			  if (ValidChars.indexOf(Char) == -1){
				 IsNumber = false;
			  }
		   }
		   return IsNumber;   
		 }
		 
//Function for alphabets (including space) end here

//Function for Email Id starts here
	function isEmail(email) {
		invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";
		// Check for invalid characters as defined above
		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i);
			if (email.indexOf(badChar,0) > -1) {
				return false;
			}
		}
		lengthOfEmail = email.length;
		if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
			return false;
		}
		Pos = email.indexOf("@",1);
		if (email.charAt(Pos + 1) == ".") {
			return false;
		}
		while ((Pos < lengthOfEmail) && ( Pos != -1)) {
			Pos = email.indexOf(".",Pos);
			if (email.charAt(Pos + 1) == ".") {
				return false;
			}
			if (Pos != -1) {
				Pos++;
			}
		}
		// There must be at least one @ symbol
		atPos = email.indexOf("@",1);
		if (atPos == -1) {
			return false;
		}
		// But only ONE @ symbol
		if (email.indexOf("@",atPos+1) != -1) {
			return false;
		}
		// Also check for at least one period after the @ symbol
		periodPos = email.indexOf(".",atPos);
		if (periodPos == -1) {
			return false;
		}
		if (periodPos+3 > email.length) {
			return false;
		}
		return true;
}


//Function for Email Id ends here

//Function for Numerics starts here
	function IsNumeric(sText){
	   var ValidChars = "0123456789";
	   var IsNumber=true;
	   var Char; 
	   for (i = 0; i < sText.length && IsNumber == true; i++){ 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1){
			 IsNumber = false;
		  }
	   }
	   return IsNumber;   
	}
//Function for Numerics ends here


//Function for Alpha Numeric Strats here

	function alphaNumeric(sText){
		var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-";
		var IsNumber=true;
		var Char; 
		for (i = 0; i < sText.length && IsNumber == true; i++){ 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1){
			IsNumber = false;
		  }
		}
	   return IsNumber;   
	}

//Function for Alphanumeric ends here


//Function for trim the values starts here

	function trim(s) {
		while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
		}
		 return s;		
	}
//Function for trim the values ends here


//Function for Phone Validation starts here

		// Declaring required variables
		var digits = "0123456789";
		// non-digit characters which are allowed in phone numbers
		var phoneNumberDelimiters = "()- ";
		// characters which are allowed in international phone numbers
		// (a leading + is OK)
		var validWorldPhoneChars = phoneNumberDelimiters + "+";
		// Minimum no of digits in an international phone no.
		var minDigitsInIPhoneNumber = 10;
		function isInteger(s)
		{   var i;
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character is number.
				var c = s.charAt(i);
				if (((c < "0") || (c > "9"))) return false;
			}
			// All characters are numbers.
			return true;
		}
		function stripCharsInBag(s, bag)
		{   var i;
			var returnString = "";
			// Search through string's characters one by one.
			// If character is not in bag, append to returnString.
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character isn't whitespace.
				var c = s.charAt(i);
				if (bag.indexOf(c) == -1) returnString += c;
			}
			return returnString;
		}
		function checkInternationalPhone(strPhone){
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
		}
//Function for Phone Validation ends here