

//<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 < 2)

					{

						alert("Password cannot be less than 2 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) { //v2.0

  window.open(theURL,winName,features);

}

function confirmdelete(startElement,endElement,message)

{

	var flag=0;

	for(i=startElement;i<document.form1.length-endElement;i=i+1)		  //confirmation message for delete

	{

		if(document.form1.elements[i].checked==true)

		{

			 flag=1;

			 if (confirm("Are you sure you want to "+message+" this record(s)?"))

			{

				document.form1.submit();

			 }

		}	

	}	

	if(flag==0)	

	{

		alert("Please select the checkbox before pressing the "+message+" button.");

		

	}

}



 function confirmCatDelete(theform)	//orderdetail.php

 {

 	

	 if (confirm("Are you sure you want to delete this record(s)?"))	

		 {		

		   formName=eval(theform);

			//theform.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 checkOut(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="checkout";

		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)

{

	var s=object.value;

	if (s!="")

	{

		array=s.split("\\");

		len=array.length;

		filename=array[len-1];

		array1=filename.split(".");

		tempimage=array1[1];

		if(tempimage!="jpg" && tempimage!="jpeg" && tempimage!="gif" && tempimage!="GIF" && tempimage!="JPG" && tempimage!="JPEG")

		{

			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)

{

 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)";



/*'--- ---------------------------------------------------------

'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;

	

}

function addfav(){



if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {



var url="http://www.proacousticsusa.com";

var title="Proacoustics";



window.external.AddFavorite(url,title);



}

else {

var msg = "Don't forget to bookmark us!";

if(navigator.appName == "Netscape") msg += "  (CTRL-D)";

document.write(msg);

}

}

function checkCC()

{

	 if(document.formcc.cname.value.length==0)

	{

		alert("Please enter the name!");

		 document.formcc.cname.focus();

		return false;

	 

	 }

	  if(document.formcc.cardNum.value.length==0)

	{

		alert("Please enter the credit card number!");

		 document.formcc.cardNum.focus();

		return false;

	 

	 }

	  if(document.formcc.cardExp.value.length==0)

	{

		alert("Please enter value for card expiration field!");

		 document.formcc.cardExp.focus();

		return false;

	 

	 }

	if(isNaN(document.formcc.cardNum.value))

	{

			alert("Please enter numeric value for credit card!")

			document.formcc.cardNum.focus();

			return false;	

	}

	if(isNaN(document.formcc.cardExp.value))

	{

			alert("Please enter numeric value for card expiration field!")

			document.formcc.cardExp.focus();

			return false;	

	}

	 if(document.formcc.cvv2.value.length==0)

	{

		alert("Please enter the card security code!");

		 document.formcc.cvv2.focus();

		return false;

	 

	 }

	  if(document.formcc.street.value.length==0)

	{

		alert("Please enter the address!");

		 document.formcc.street.focus();

		return false;

	 

	 }

	 if(document.formcc.zip.value.length==0)

	{

		alert("Please enter the zip!");

		 document.formcc.zip.focus();

		return false;

	 

	 }

	

	   return true;



}



function chck_box_selected(frm,elm){

	elm=elm+'[]';

	//alert(elm);

	myCheckboxArray=frm[elm];

	if(myCheckboxArray.value){

		if(myCheckboxArray.checked==false) {

			alert("You must select at least one checkbox to continue"); 

			return false; 

		}else

			return true;

	}else{

	sel = -1; 

		for (i=0; i<myCheckboxArray.length; i++) { 

			if (myCheckboxArray[i].checked) { 

			sel = i; 

			} 

		} 

		if ((sel == -1)&&(i!=0)) { 

			alert("You must select at least one checkbox to continue"); 

			return false; 

		} else

			return true;

	}	

}



var usd_gbp=0.550015;

var usd_eur=0.792501;

 function f_getcookieval(offset) {

         var endstr = document.cookie.indexOf (";", offset);

         if (endstr == -1) endstr = document.cookie.length;

         return unescape(document.cookie.substring(offset, endstr));

 }

 

 function f_getcookie(name) {

         var arg = name + "=";

         var alen = arg.length;

         var clen = document.cookie.length;

         var i = 0;

         while (i < clen) {

                 var j = i + alen;

                 if (document.cookie.substring(i, j) == arg) return f_getcookieval (j);

                 i = document.cookie.indexOf(" ", i) + 1;

                 if (i == 0) break;

         }

         return null;

 }

 

 function f_setcookie (name, value, expires) {

	 		expires=30;

			var today = new Date();

 		var expire = new Date();

 		 nDays=30;

 		expire.setTime(today.getTime() + 3600000*24*nDays);

         document.cookie = name + "=" + escape (value) + ";expires="+expire.toGMTString()+";path=/;domain=.my-domain";

		 //alert(expires.toGMTString());

		 //alert(name+'  '+value);

		  window.location.reload(false); 



 }

 function roundNumber(rnum) {

	var rlength = 2; // The number of decimal places to round to

	if (rnum > 8191 && rnum < 10485) {

		rnum = rnum-5000;

		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);

		//newnumber = newnumber+5000;

	} else {

		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);

	}

	return newnumber;

}

function show_currency(amount){

	var curr=f_getcookie('currency');

	var symbol='$';

	if(curr=='USD'||curr=='')

		amount=parseFloat(amount);

	else if(curr=='GBP'){

		amount=parseFloat(amount)* parseFloat(usd_gbp);

		symbol='&pound;';

	}else if(curr=='EUR'){

		amount=parseFloat(amount)* parseFloat(usd_eur);	

		symbol='&#8364;';

	}

		

	amount=roundNumber(amount);

	document.write(symbol+' '+amount);

}
