
//<form name='frm' method='post' action='' 
//onSubmit='return theValidator(this,"Input1,Input3,temp,ta,rad","Name,Zip Code,Template,Comments,University/Non-University",",Y,,,",",,,,",",,,,");'>


//The parameters
// 0) The instance of the form , use 'this' rather than name
// 1) The name of the controls separated by comma, only add name of controls which you want to be validated.
// 2) The Message i.e if you write Name, then the msg would be 'Name cannot be blank.'
// 3) If you want any control to be validated for numeric value, then specify 'Y' 
//    exactly in the same order if the 1st parameter has values Input1,Input2,temp,.. if you want to validate the Input2
//    control for numeric value the ,Y,,....
// 4) Email parameter - order as point 3
// 5) URL parameter - order as point 3
// TODO: 
//	Email Validation - Done
//	URL Validation	- Done

function theValidator(theForm,theControl,theMessage,theNumeric,theEMail,theURL,theSpecialCharacter)
{
	// This function is used to validate that all text
	// fields in a given form contain some value
	//Split the controls + Messages + Numerics by comma ,
	
	var theControls = new Array();
	var theMessages = new Array();
	var theNumerics = new Array();
	var theEMails = new Array();
	var theURLs = new Array();
//	var theConfirmPasswords = new Array();
	var theSpecialCharacters = new Array();

	theControls=theControl.split(",");
	theMessages=theMessage.split(",");
	theNumerics=theNumeric.split(",");
	theEMails=theEMail.split(",");
	theURLs=theURL.split(",");
	theSpecialCharacters=theSpecialCharacter.split(",");
	for (var i=0; i < theForm.elements.length; i++){
//		alert("Type: " + theForm.elements[i].type);
		for(var counter=0;counter < theControls.length; counter++)
		{
			if (theForm.elements[i].name == theControls[counter])
			{
				if (theForm.elements[i].type == 'file')
				{
					//Blank value check
					if (trim(theForm.elements[i].value) == '')
					{
						alert("Please enter the "+ theMessages[counter] + ".");
						theForm.elements[i].focus();
						return false;	
					}
				}

				if (theForm.elements[i].type == 'text')
				{
					//Blank value check
					if (trim(theForm.elements[i].value) == '')
					{
						alert("Please enter the "+ theMessages[counter] + ".");
						theForm.elements[i].focus();
						return false;	
					}
					fieldValue=theForm.elements[i].value;
					if (fieldValue.indexOf("\"")>=0)
					{
						alert("Double quotes are not allowed.");
						theForm.elements[i].focus();
						return false;	
					}

					// Numeric value check
					if (trim(theForm.elements[i].value) != '' && theNumerics[counter] == "Y")
					{
						if(isNaN(trim(theForm.elements[i].value)))
						{
							alert("Please enter numeric value for "+theMessages[counter]+".");
							theForm.elements[i].focus();
							return false;	
						}
						if(trim(theForm.elements[i].value)<= 0 )
						{
							abc=theMessages[counter].replace(theMessages[counter],theMessages[counter].charAt(0).toUpperCase());
							alert(abc+theMessages[counter].substring(1,theMessages[counter].length)+ " should be greater than 0.");
							theForm.elements[i].focus();
							return false;	
						}
						
					}
					// Email value check
					if (trim(theForm.elements[i].value) != '' && theEMails[counter] == "Y")
					{
						if(!emailcheck(theForm.elements[i],"Please enter valid email address."))  return false;
						//{
						//	alert("Invalid Email Address\n Should be user@domain.com");
						//	theForm.elements[i].focus();
							//return false;	
						//}
					}
					// URL value check
					if (trim(theForm.elements[i].value) != '' && theURLs[counter] == "Y")
					{
						if(!validateURL(trim(theForm.elements[i].value)))
						{
							alert(" Invalid URL. \n Please type in URL like http://www.domain.com");
							theForm.elements[i].focus();
							return false;	
						}
					}
				}
				// Password Validation
				if (theForm.elements[i].type == 'password')
				{
					//Blank value check
					if (trim(theForm.elements[i].value) == '')
					{
						alert("Please enter the "+theMessages[counter] + ".");
						theForm.elements[i].focus();
						return false;	
					}
				}	

				if (theForm.elements[i].type == 'password' )
				{
					//Blank value check
					if (theForm.elements[i].value.length < 6)
					{
						alert("Password cannot be less than 6 characters.");
						theForm.elements[i].focus();
						return false;	
					}
				}	

				if (theForm.elements[i].type=='text' && theSpecialCharacters[counter]=="Y")
				{
					if (inStrGrp(theForm.elements[i].value,"!#%$^&*/\()+=<>~"))
					{
						abc=theMessages[counter].replace(theMessages[counter],theMessages[counter].charAt(0).toUpperCase());
						alert(abc+theMessages[counter].substring(1,theMessages[counter].length)+ " can not have special characters like !#%$^&*/\()+=<>~");
						theForm.elements[i].focus();
						return false;
					}
				}	
				

			//Select box validation
				if (theForm.elements[i].type == "select-one")
				{
					var selIndex,selValue;
					selIndex=theForm.elements[i].selectedIndex;
					var theObject=theForm.elements[i];
					selValue = theObject[selIndex].value;
					if( trim(selValue) == "" || trim(selValue) == "0")
					{
						alert("Please select the " + theMessages[counter] + ".");
						theForm.elements[i].focus();
						return false;	
					}
				}
			
			// Text Area Validation

				if (theForm.elements[i].type == "textarea")
				{
					var txtAreaValue;
					txtAreaValue=theForm.elements[i].value;
					if( trim(txtAreaValue) == "")
					{
						alert("Please enter the "+theMessages[counter] + ".");
						theForm.elements[i].focus();
						return false;	
					}
				}


			
			// Radio Button Validation
			// This radio button validation is buggy so I am commneting it out
			/*	if(theForm.elements[i].type == "radio")
				{
//alert(theForm.elements[i].value);
					if(getRadioButtonValue(theForm.elements[i],theForm.elements[i].name) == "-1")
					{
						alert("You must select " + theMessages[counter] + ".");
						return false;		
					}
				}*/
			}

		}
	}
	
	return true;
}

function getRadioButtonValue(theForm,theControl)
{
	var i;
	alert("Hello " + theForm + " control.length: " + theControl.length);
	for( i=0; i< theControl.length; ++i)
	{
alert(theForm[i].checked);
		if( theControl[i].checked)
		{
		
			return theControl[i].value;
		}
	}

	// default (non selected)
	return -1;
}

function ltrim ( s )
{
	return s.replace( /^\s*/, "" )
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}

function emailcheck(object,str)
{
var email=object.value;
var matcharray=email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) 
	if(matcharray==null){
	alert(str)
	 	object.focus();
		object.select();
	return false;
	}
	else return true
}	

function validateEmail(email){
	
	// This function is used to validate a given e-mail 
	// address for the proper syntax
	
	if (email == ""){
		return false;
	}
	badStuff = ";:/,' \"\\";
	for (i=0; i<badStuff.length; i++){
		badCheck = badStuff.charAt(i)
		if (email.indexOf(badCheck,0) != -1){
			return false;
		}
	}
	posOfAtSign = email.indexOf("@",1)
	if (posOfAtSign == -1){
		return false;
	}
	if (email.indexOf("@",posOfAtSign+1) != -1){
		return false;
	}
	posOfPeriod = email.indexOf(".", posOfAtSign)
	if (posOfPeriod == -1){
		return false;
	}
	if (posOfPeriod+2 > email.length){
		return false;
	}
	return true
}

function validateURL(url){
	
	// This function is used to validate a given 
	// address for the proper syntax
	url="http://"+url;
	var re;
	re = new RegExp("(http|ftp|https)://[-A-Za-z0-9._/]+");
	if (re.test(url)==false)
		return false;

	posOfAtSign = url.indexOf(".")

	if (posOfAtSign == -1){
		return false;
	}
	return true;
}
function MM_openBrWindow(theURL,winName,features) {
//alert('in function');
	//v2.0
  width=screen.width-50;
height=screen.height-100;
features="scrollbars=yes,resizable=yes,width="+width+",height="+height+",left=0,top=0";
newpopupwindow =  window.open(theURL,winName,features);
}
function confirmdelete(theform,startElement,endElement,message)
{
	var flag=0;
	for(i=startElement;i<theform.length-endElement;i=i+1)		  //confirmation message for delete
	{
		if(theform.elements[i].checked==true)
		{
			 flag=1;
			 if (confirm("Are you sure you want to "+message+" this record(s)?"))
				return true;
			else
				return false;
		}	
	}	
	if(flag==0)	
	{
		alert("Please select the checkbox before pressing the "+message+" button.");
		return false;
	}
}

 function confirmCatDelete(theform)	//orderdetail.php
 {
 	
	 if (confirm("Are you sure you want to delete this record(s)?"))	
		 {			formName=eval("document."+theform)
			formName.flag.value=1;
			formName.submit();
		}		
}
function checkQuantity(startelement,endelement)			//shopingcart.php
{
	flag=0;
	for(i=startelement;i<document.form5.length-endelement;i=i+3)  //numeric value for quantity if user wants to update quantity
	{
		if(isNaN(document.form5.elements[i].value))
		{
			alert("Quantity should be numeric.");
			document.form5.elements[i].focus();
			flag=1;
			break;
		}
		if(document.form5.elements[i].value <= 0)
		{
			alert("Quantity should be greater than 0.");
			document.form5.elements[i].focus();
			flag=1;
			break;
		}
		
	}
	if(flag==0)
	{
		document.form5.action1.value="recalculate";
		document.form5.submit();
		return true;
	}
}
 
 

function validate (field, maxlen)
{	
	
	if (field.value.length > maxlen)
	{
		alert ("Maximum characters allowed are "+maxlen+". Entered are "+field.value.length+" characters.");
		field.focus();
		return  false;
	}
	return true;
}
function fileextension(object,str,validfilename)
{
	var s=object.value;
	var flag=0;
	if (s!="")
	{
		array=s.split("\\");
		len=array.length;
		filename=array[len-1];
		array1=filename.split(".");
		tempimage=array1[1];
		validfilenamearray=validfilename.split(",");
		validfilelen=validfilenamearray.length;
		for(i=0;i<validfilelen;i++)
		{
			if(tempimage==validfilenamearray[i])
				flag=1;
		}
		if(flag==0)
		{
			alert(str);
			object.focus();
			return false;
		}
	}
	
	return true;
}
function emailcheck(object,str)
{
var email=object.value;
var matcharray=email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) 
	if(matcharray==null){
	alert(str)
	 	object.focus();
		object.select();
	return false;
	}
	else return true
}

 function checkNumber(theform,startElement,endElement)
{
	flag=0;

	for(i=startElement;i<theform.elements.length-endElement;i=i+2)
	{
		if(theform.elements[i].checked==true)
		{
			flag=1;
			if (isNaN(theform.elements[i+1].value)==true)
			{
				alert("Please enter numeric value for sequence.");
				theform.elements[i+1].focus();
				return false;
			}
			if (theform.elements[i+1].value<=0)
			{
				alert("Sequence should be greater than 0.");
				theform.elements[i+1].focus();
				return false;

			
			}
		}		
	}
	if(flag==0)
	{
		alert("Please select the checkbox before pressing the submit button.");
		return false;
	}	
	return true;
	
}

function inStrGrp(src,reg) {
    var regex=new RegExp("[" + reg + "]","i");
	return regex.test(src);
}

function checkphone(x)
{
	//alert("in fun");
 var regexp=/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/;
// var regexp=/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4})$/;
 return regexp.test(x);
 }
 
 

var msg = "";
 msg = "Please Enter the Correct Phone Number";
 msg = msg +  "\n The correct forms are : ";
 msg = msg + "\n xxx-xxx-xxxx";
 msg = msg + "\n (xxx)xxx-xxxx";
// msg = msg + "\n xxxxxxxxxx (10 digited Mobile no)";
 //msg = msg + "\n xxxxxx (6 digits no)";
 //msg = msg + "\n xxxxxxx (7 digits no)";
 //msg = msg + "\n xxxxxxxx (8 digits no)";


var msg1 = "";
 msg1 = "Please Enter the Correct Fax Number";
 msg1 = msg1 +  "\n The correct forms are : ";
 msg1 = msg1 + "\n xxx-xxx-xxxx";
 msg1 = msg1 + "\n (xxx)xxx-xxxx";
 //msg1 = msg1 + "\n xxxxxx (6 digits no)";
 //msg1 = msg1 + "\n xxxxxxx (7 digits no)";
 //msg1 = msg1 + "\n xxxxxxxx (8 digits no)";

/////chg fax///


////////Changed function//////

function checkphone2(x,y)
{
	var regexp=/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/;
	x = trim(x);
	if(!(regexp.test(x)))
	{
		var msg = "";
		msg = "Please enter the correct phone number.";
		msg = msg +  "\n The correct forms are : ";
		msg = msg + "\n xxx-xxx-xxxx";
		msg = msg + "\n (xxx)xxx-xxxx";
							
		alert(msg);
		y.focus();
		return false; 
	}
	return true;
}
////////function for fax/////
function checkfax2(x,y)
{
     var regexp=/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/;
      x = trim(x);
      if(!(regexp.test(x)))
      {
       var msg = "";
       msg = "Please enter the correct fax number.";
       msg = msg +  "\n The correct forms are : ";
       msg = msg + "\n xxx-xxx-xxxx";
       msg = msg + "\n (xxx)xxx-xxxx";
       //msg = msg + "\n xxxxxxxxxx (10 digited Mobile no)";
       //msg = msg + "\n xxxxxx (6 digits no)";
       //msg = msg + "\n xxxxxxx (7 digits no)";
       //msg = msg + "\n xxxxxxxx (8 digits no)";
       alert(msg);
          y.focus();
       return false; 
     }
     return true;
}





/*'--- ---------------------------------------------------------
'Name			:	CheckDate
'Developer		:	Ekansh
'Create Date	:	20/06/2003  (dd/mm/yyyy)  
'Purpose		:	This will check the valid date
'Input Parameter							
	'	theform	:	Name of the form
		dd		:	Name of the day form element
		mm		:	Name of the month form element
		yy		:	Name of the year form element

'ModifiedDate	ModifiedBy		Comments
'-------------------------------------------------------------
*/
function CheckDate(theform,dd,mm,yy)
{ 
	day1=parseInt(eval("theform."+dd+".options[theform."+dd+".selectedIndex].value"));
	month1=eval("theform."+mm+".options[theform."+mm+".selectedIndex].value");
	month1=parseInt(month1-1);
	year1=parseInt(eval("theform."+yy+".options[theform."+yy+".selectedIndex].value"));
	var test = new Date(year1,month1,day1);
	if (!((yearcheck(test.getYear()) == year1) && (month1 == test.getMonth()) && (day1 == test.getDate())) )
		{
			alert("Invalid date");
			return false;
		}
	return true;	
 }
 //**************************************************************

 function yearcheck(number)
 { 
	if (number < 1000)
	{
		 number = number + 1900;
	}
	return number; 
}
 function checkPostiveNumber(theform,startElement,endElement)
{
	for(i=startElement;i<theform.elements.length-endElement;i=i+1)
	{
		if (isNaN(theform.elements[i].value)==true)
		{
			alert("Please enter numeric value for sequence.");
			theform.elements[i].focus();
			return false;
		}
		if (theform.elements[i].value<=0)
		{
			alert("Sequence should be greater than 0.");
			theform.elements[i].focus();
			return false;
		}
	}		
	return true;
	
}