var errorstr = "";

		function check(form1) {
			//alert("hai");

			var theForm = document.forms['form1'];
			//alert("the form name is "+theForm);
			
			
			if(document.getElementById("emailid")!=null && document.getElementById("confemailid")!=null)
						{
			      var email=document.getElementById("emailid").value;
			       var confirmemail=document.getElementById("confemailid").value;
			            //alert(email);
						if(email!=confirmemail)
						{
						errorstr += "email and confirm mail doesnt match\n";
			
						}
			}


                        //alert("before calling CheckBoxGroupCheck()");

			CheckBoxGroupCheck1();
			//alert("after calling CheckBoxGroupCheck()");

			//alert("No of elements is "+theForm.elements.length);

			for (i = 0; i < theForm.elements.length; i++) {
				//alert("Element Type: " + theForm.elements[i].type);
				//alert("Element Name: " + theForm.elements[i].name);
				//alert("Id: " + theForm.elements[i].id);
				//alert("class name = "+theForm.elements[i].className);
				
						
				var misc = Validator(theForm.elements[i].className,
						theForm.elements[i].name, theForm.elements[i].value,
						theForm.elements[i].id);
						//alert("value = "+theForm.elements[i].value);
						
						
				}
				
					
//alert("final error str is "+errorstr);
//alert(errorstr=="");

  if(errorstr=="")
  {
  //alert("hi");
  return true
  }
  else
  {
  alert(errorstr);
  errorstr = "";
  return false;
  }
			}

		function Validator(cname, iname, ival, id) {
		
		var theForm = document.forms['form1'];
		
			//alert("in function Validator");
			//alert("class name is "+cname+"element name is "+iname+" ival is "+ival);
			
			//alert("in Validator b4 checking for CheckBoxSelect");
			
			var pos = cname.indexOf("CheckBoxSelect");
			var opt=0;
			
			//alert("pos is "+pos);
			
					if (pos != -1)
					{
						//alert("1111111111");
						//alert(theForm.elements[i].checked);
						//alert(opt);
						
						if(!theForm.elements[i].checked && !opt)
						{
							//alert("2222222");
							errorstr+=theForm.elements[i].id+" should be checked \n";
							opt=1;
			
						}
						//alert("33333333");
			
					}
			
			
			//alert(errorstr);
			//alert("@@@@@");
			var pos = cname.indexOf("NullCheck");
			//alert("pos for NullCheck is "+pos);

			if (pos != -1) {
				//alert("calling NullCheck function");
				//alert(document.getElementsByName(iname)[0].value);

				if (!document.getElementsByName(iname)[0].value) {
					errorstr += id + " cannot be null \n";
				}//end of inner-if
				else {
				
				var retstr = trim1(document.getElementsByName(iname)[0].value);
								if (retstr.length == 0) {
									errorstr += id + " cannot have only spaces \n";
									}
					pos = cname.indexOf("checkForInvalidCharacters");
					//alert("pos for checkForInvalidCharacters is "+pos);

					if (pos != -1) {
						//alert("calling checkForInvalidCharacters function");
						var ret = checkForInvalidCharacters1(ival);

						//alert(ret);

						if (!ret) {
							errorstr += id + " cannot have Invalid Characters \n";
							
						}
					
					}

					pos = cname.indexOf("checkForNumbers");
					//alert("pos for checkForNumbers is "+pos);

					if (pos != -1) {
						//alert("calling checkForNumbers function");

						var ret = checkForNumbers1(ival);

						//alert(ret);

						if (!ret) {
							errorstr += id + " cannot have Numbers \n";
						}
					}

					pos = cname.indexOf("validateUSPhone");

					//alert("pos for validateUSPhone is "+pos);

					if (pos != -1) {
						//alert("calling validateUSPhone function");
						var ret = validateUSPhone1(ival);
						//alert("FINAL RETURN VALUE is "+ret);

						if (!ret) {
							//errorstr+=id +" is not Valid \n";
						}

					}

					pos = cname.indexOf("ValidateEmail");

					//alert("pos for ValidateEmail is "+pos);

					if (pos != -1) {
						//alert("calling ValidateEmail function");
						var ret = ValidateEmail1(ival);
						//alert("FINAL RETURN VALUE is "+ret);

						if (!ret) {
							//errorstr+=id +" is not Valid \n";
						}

					}

					pos = cname.indexOf("ValidateCardNumber");
					{
						if (pos != -1) {
							//alert("calling ValidateCardNumber function");
							var ret = ValidateCardNumber1(ival);
							//alert("FINAL RETURN VALUE is "+ret);

							if (!ret) {
								//errorstr+=id +" is not Valid \n";
							}

						}

					}

					pos = cname.indexOf("ValidateSecuriyCode");

					//alert("pos for ValidateSecuriyCode is " + pos);

					if (pos != -1) {
						//alert("calling ValidateSecuriyCode function");
						var ret = ValidateSecuriyCode1(ival);
						//alert("FINAL RETURN VALUE is "+ret);

						if (!ret) {
							//errorstr+=id +" is not Valid \n";
						}

					}
					
					pos = cname.indexOf("ValidateNumbers");
					
					//alert("pos for ValidateNumbers is " + pos);
					if (pos != -1) {
					//alert("calling ValidateNumbers function");
					var ret = ValidateNumbers1(ival);
					//alert("FINAL RETURN VALUE is "+ret);
					if (!ret) {
					errorstr+="Please enter only Number for "+ id+ "\n";
						  }
					
					}

					pos = cname.indexOf("ValidateZipCode");

					//alert("pos for ValidateZipCode is " + pos);

					if (pos != -1) {
						//alert("calling ValidateSecuriyCode function");
						var ret = ValidateZipCode1(ival);
						//alert("FINAL RETURN VALUE is "+ret);

						if (!ret) {
							//errorstr+=id +" is not Valid \n";

						}
    				

					}
					
					
			}//end of else part

		}//end of if loop

	}//end of function Validator

		function checkForInvalidCharacters1(strInput) {
			var invalidChars = "?&=";
			for ( var i = 0; i < invalidChars.length; i++) {
				strBadChar = invalidChars.charAt(i); //get next invalid character
				if (strInput.indexOf(strBadChar, 0) > -1)
					return 0; //if there is one in Email
			}
			return 1;
			//returns 0 if bad char is present
		}

		function checkForNumbers1(strInput) {
			var invalidChars = "1234567890";
			for ( var i = 0; i < invalidChars.length; i++) {
				strBadChar = invalidChars.charAt(i);
				if (strInput.indexOf(strBadChar, 0) > -1)
					return 0;
			}
			return 1;

			// returns 0 if it has numbers
		}

		function validateUSPhone1(theFieldValue) {

			//alert("in ValidateUSPhone function with value "+theFieldValue);

			var phoneNumberDelimiters = '()- ';

			var normalizedPhone = stripCharsInBag1(theFieldValue,
					phoneNumberDelimiters);
			//alert("normalizedPhone is "+normalizedPhone);

			//alert("checking if the Phone-Number is normalised");
			if (!isUSPhoneNumber1(normalizedPhone)) {
				//alert("Not normalised and hence returning 0");
				return 0;
			} else {
				//alert("Normalised and hence returning 1");
				return 1;
			}
		}

		function isUSPhoneNumber1(s) {
			//alert("in isUSPhoneNumber function with "+s);

			//alert("returning "+ (isInteger(s) && s.length == digitsInUSPhoneNumber)+" from isUSPhoneNumber function");

			var digitsInUSPhoneNumber = 10;

			return (isInteger1(s) && s.length == digitsInUSPhoneNumber);
		}

		//removes all characters which appear in string bag from string s
		function stripCharsInBag1(s, bag) {
			//alert("in stripCharsInBag function with "+s+" and "+bag);
			var i;
			var returnString = '';
			//search through strings characters one by one
			//if character is not in bag, append to return string
			for (i = 0; i < s.length; i++) {
				//check that current character isnt whitespace
				var c = s.charAt(i);
				if (bag.indexOf(c) == -1)
					returnString += c;
			}
			//alert("returning "+returnString+" from stripCharsInBag function");
			return returnString;
		}

		function isInteger1(s) {
			//alert("in isInteger function with "+s);
			var i;
			for (i = 0; i < s.length; i++) {
				//check that current character is number.
				var c = s.charAt(i);
				if (!isDigit1(c)) {
					//alert("returning false1 in isInteger");
					errorstr += "Phone Number must contain only digits . \n";
					return false;
				}
			}
			var digitsInUSPhoneNumber = 10;

			if (s.length != digitsInUSPhoneNumber) {
				//alert("returning false2 in isInteger");
				errorstr += "Phone Number should be of 10 digits. \n";
				return false;
			}

			//all characters are numbers
			//alert("returning true in isInteger");
			return true;
		}

		//check for empty string
		function isEmpty1(s) {
			return ((s == null) || (s.length == 0))
		}

		//returns true if character c is a digit
		//(0 .. 9)
		function isDigit1(c) {
			return ((c >= '0') && (c <= '9'))
		}
		function ValidateEmail1(strInput) {
			if (strInput.indexOf("@") == -1 && strInput != "") {
				errorstr += "Email should contain @ sign. \n";
				return 0;
			}

			if (strInput.indexOf("@") == 0) {
				errorstr += "Please verify that your email address is valid.\n";
				return 0;
			}

			if (strInput.indexOf(" ") >= 0 && strInput.value != "") {
				errorstr += "Please verify that your email address does not contain any spaces.\n";
				return 0;

			}
			var apos=strInput.indexOf("@");
			 var dotpos=strInput.lastIndexOf(".");
			 if (dotpos-apos<2)
			  	 {
			   	 errorstr += "Please verify that your email address is valid.\n";
			   	 return 0;
			  					
  				}
			
			
			if ((strInput.indexOf('@', 0) == -1 || strInput.indexOf('.', 0) == -1)
					&& strInput != "") {
				errorstr += "Please verify that your email address is valid.\n";
				return 0;

			}

			return 1;
		}
		var validNum = "1234567890";
		var vmax = 16;

		function ValidateCardNumber1(number) {
			for ( var i = 0; i <= number.length; i++) {
				if (validNum.indexOf(number.charAt(i)) == -1) {

					errorstr += "Use Only Numbers for your credit card number. \n";
					return 0;

				}
			}

			if ((number.length != vmax) && (number.length > 0)) {
				errorstr += "Credit Card Number must be 16 characters.\n";
				return 0;

			}
			return 1;

		}
		function ValidateSecuriyCode1(number) {
	     	if (number.length < 3) {
			 //alert("here");
			  //alert(number.length);
				errorstr += "Card Security Code must be at least 3 characters.\n";
				return 0;
			}

			for ( var i = 0; i <= number.length; i++) {
				if (validNum.indexOf(number.charAt(i)) == -1) {
					errorstr += "Please use only numbers for your Card Security Code.\n";
					return 0;

				}

			}

			return 1;

		}

		function ValidateZipCode1(code)
		{

			if (code.length < 5) {
				errorstr += "Your Zip Code must be five digits long.\n";
				}

			for ( var i = 0; i <= code.length; i++) {
				if (validNum.indexOf(code.charAt(i)) == -1) {
					errorstr += "Please use only numbers for Zip Code.\n";
					return 0;
				}
			}

			return 1;
		}
		
		
		function trim1(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ValidateNumbers1(input)
{
var validNum = "1234567890";
for ( var i = 0; i <= input.length; i++) {
	if(validNum.indexOf(input.charAt(i)) == -1) {
		return 0;

				}
			}
			return 1;
			
}



function CheckBoxGroupCheck1()
{

	var theForm = document.forms['form1'];
	var opt=0;

	for (i = 0; i < theForm.elements.length; i++) {
		//alert(i+" "+theForm.elements[i].type+" "+theForm.elements[i].id);

		//alert((theForm.elements[i].type=="checkbox") + " and " + (theForm.elements[i].id!="") + " and "+ (theForm.elements[i].className!="CheckBoxSelect") );
				
		if( (theForm.elements[i].type=="checkbox")  && (theForm.elements[i].id!="") && (theForm.elements[i].className!="CheckBoxSelect"))
		{
			//alert("found checkbox");
			var checkboxid=theForm.elements[i].id;
			var reqdid=checkboxid;
			//alert("checkbox id is "+checkboxid);

			var isChecked=0;

			for (j = 0; j < theForm.elements.length; j++) {

				if(theForm.elements[j].type=="checkbox" && theForm.elements[j].id==checkboxid)
				{
					//alert(theForm.elements[j].checked);

					if(theForm.elements[j].checked)
					{
						//alert("HURRAY");
						isChecked=1;
					}
				}

			}

			//alert("isChecked is "+isChecked);
			if(!isChecked && !opt)
			{
				opt=reqdid;
				errorstr+="Please select atleast one CheckBox \n"
			}

		}
	}

}