//**********************************************************************************
// These scripts iterate through the various form objects and performs verification 
// routines based on the object's properties. These properties are set in the onSubmit()
// method of the calling form. When an object fails verification the source property of 
// an image next to the object changes, and the new source image shows the failure.  
//
// In this variation the objects are verified individually using the onBlur method of each 
// object, and then all objects are verified using the onSubmit method of the form.
//
//
// Important things to know - the names of the properties that can be set in the 
// verify function, and the names of the images. An image next a form field named 
// xyz will be named img_xyz. The calling form that goes with this code is named calljs1.html
//
// The images need to be the same size, and there needs to be a blank image. Also, all 
// images need to be floating gifs.
//
//**********************************************************************************

// GLOBAL VARIABLES AND PROPERTIES

var errorcnt = 0;


//**********************************************************************************

//

// popup(formobj,len, nam) FUNCTION	

//

// This function allows for additional options to be added to a select object. 

// Accepts formobj - the name of the calling form 

//         len - the "length", or number of options in the select obbject

//         nam - the name of the select object	

	
function MM_preloadImages() { //v2.0
  if (document.images) {
    var imgFiles = MM_preloadImages.arguments;
    if (document.preloadArray==null) document.preloadArray = new Array();
    var i = document.preloadArray.length;
    with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){
      preloadArray[i] = new Image;
      preloadArray[i++].src = imgFiles[j];
  } }
}

function popup(formobj,len, nam)

           {

		          

			         

               

               var newvalue = prompt("Value to be added"," ");//ask user to input a new value that isn't on the drop down

                   if (newvalue > " ") //if the user didn't cancel

		   {	     newoption = new Option("", "", false, true); //create an option

					 formobj[nam].options[len] = newoption; // add the option to the list of options of the calling select

				     formobj[nam].options[len].text = newvalue;//set text = value input

					 formobj[nam].options[len].value = newvalue;}//set value = to value input

			       

				    

            }

	

		

//**********************************************************************************

//

// isblank(s) FUNCTION

//

// A utility function that returns true if a string contains only whitespace characters.



	function isblank(s)	{

    		for(var k = 0; k < s.length; k++)  {

        		var c = s.charAt(k);

        		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;

					          					}

    			return true;

			             }



//**********************************************************************************

//

// chkEmail(emailStr) FUNCTION

//

// Accepts a value that should be a valid email address

// This function checks that the email address and domain are valid



 

	function chkEmail(emailStr) {


		var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;// pattern of a valid email address

		var mailStr = emailStr.value;// Variable to hold the value of the text object we passed

		var matchArray = mailStr.match(emailPat); // comparison between the pattern and the input value



		// if the input doesn't match the valid email pattern

		
		if (matchArray == null) {

				image_name = 'img_' + emailStr.name;
				document[image_name].src = 'http://www.megashow.com/javascript/images/email.gif';
				errorcnt=errorcnt+1;	

								}

		// make sure the IP address domain is valid

		

		else {
		
						

			var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);

			if (IPArray != null) {

				for (var m=1;m<=4;m++)  {

					if (IPArray[m]>255) {

						image_name = 'img_' + emailStr.name;
						document[image_name].src = 'http://www.megashow.com/javascript/images/email.gif';
						errorcnt=errorcnt+1;	

      							  }

   								   		}

										}

			}

								   }						 



//**********************************************************************************

//

// chkNum(nm) FUNCTION

//

// Accepts a value that should be a valid numeric value

// This function checks that the value is a number, and can also check to see if the number is 

// within a certain range





	function chkNum(nm) {



		

		var num = parseFloat(nm.value);// holds numeric value of the input
		
	

	// Is value numeric?

	

		if 	(isNaN(num) 							||

			((nm.min != null) && (num < nm.min)) 	||

			((nm.max != null) && (num > nm.max)))   {

			image_name = 'img_' + nm.name;
			document[image_name].src = 'http://www.megashow.com/javascript/images/number.gif';
			errorcnt=errorcnt+1;	

	

			
			
			

													}		

							}







//**********************************************************************************

//

// chkZip(zip) FUNCTION

//

// Accepts a value that should be a valid zip code

// This function checks to see that the value is either 5 numeric values long, or 

// 5 numeric values, followed by a (-), followed by 4 more numeric values

 

	function  chkZip(zip)   {
	
		

		var sw = "off";// prevents the error message from printing more than once

		var sw2 = "off"; //  also prevents the error message from printing more than once

		var valid = "0123456789-"; //these are the valid characters in a zip code

		var hyphencount = 0; // this initializes our hyphen counter

	



		//check the length of the string, if it's not 5 or 10 add line to error message

		

			if (zip.value.length!=5 && zip.value.length!=10) {

				image_name = 'img_' + zip.name;
			    document[image_name].src = 'http://www.megashow.com/javascript/images/zip.gif';
				errorcnt=errorcnt+1;		
						}

		// check the characters in the string to see if they are in our valid variable (0-9, -)

		// count the number of hyphens, and determine the position of the hyphen

		// add to the error message if the input is found to be invalid



			else {

				for (var j=0; j < zip.value.length; j++) {

				temp = "" + zip.value.substring(j, j+1);





				if (temp == "-") hyphencount++;

					if ((valid.indexOf(temp) == "-1") && (sw == "off")) {

						image_name = 'img_' + zip.name;
			            document[image_name].src = 'http://www.megashow.com/javascript/images/zip.gif';	
						errorcnt=errorcnt+1;	
						sw = "on";

												}

					if ((hyphencount > 1) || ((zip.value.length==10) && (""+zip.value.charAt(5)!="-")) && (sw2 == "off")) {

						image_name = 'img_' + zip.name;
			            document[image_name].src = 'http://www.megashow.com/javascript/images/zip.gif';	
						errorcnt=errorcnt+1;	
						
						sw2 = "on";

							

   							}

						}

					}

				}





//**********************************************************************************

//

// chkDate(aDate) FUNCTION

//				

// Accepts a value that should be a valid date in MM/DD/YYYY or MM-DD-YYYY format

// Checks that the month is (1-12), that the day is the right number of days for the month,

// including leap years, that the year is 4 numberic values long, and if lowYear and highYear

// properties were defined for the text, can compare the year to the allowed range of values.

//



	function chkDate(aDate){ 

			

		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;// the pattern for a valid date

		var dateStr = aDate.value; // holds the passed value

		var matchArray = dateStr.match(datePat); // is the format ok?

	
		

	// check to see if the input matches the allowed format. Add to errors if it doesn't



		if (matchArray == null) {

		       	image_name = 'img_' + aDate.name;
				document[image_name].src = 'http://www.megashow.com/javascript/images/date.gif';
				errorcnt=errorcnt+1;	
				
					}

		else						{



	// parse date into variables



			month = matchArray[1]; 

			day = matchArray[3];

			year = matchArray[4];





	// check month range



			if (month < 1 || month > 12)  { 

				 image_name = 'img_' + aDate.name;
				 document[image_name].src = 'http://www.megashow.com/javascript/images/date.gif';
				 errorcnt=errorcnt+1;	
						       			   }

 

	// check day range	



			if (day < 1 || day > 31)       {

				image_name = 'img_' + aDate.name;
				document[image_name].src = 'http://www.megashow.com/javascript/images/date.gif';
				errorcnt=errorcnt+1;	
											}

										

			

	// check 30 day long months		

			

			if ((month==4 || month==6 || month==9 || month==11) && day==31) {

				 image_name = 'img_' + aDate.name;
				 document[image_name].src = 'http://www.megashow.com/javascript/images/date.gif';
				 errorcnt=errorcnt+1;	
																			}

	// check for february

			

			if (month == 2) { 

						var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

						if (day>29 || (day==29 && !isleap)) {

							 image_name = 'img_' + aDate.name;
							 document[image_name].src = 'http://www.megashow.com/javascript/images/date.gif';
							
															}

	

							}



	// compare year to lowYear and highYear properties

			

			if (aDate.lowYear < 1000 || aDate.lowYear == null) {aDate.lowYear = 1000;}

			if (aDate.highYear >9999 || aDate.highYear == null) {aDate.highYear = 9999;} 

					

			if (year < aDate.lowYear || year > aDate.highYear) {

			    image_name = 'img_' + aDate.name;
				document[image_name].src = 'http://www.megashow.com/javascript/images/date.gif'; 		
				errorcnt=errorcnt+1;	

																}

					

							

				

								 	}

						   }



//**********************************************************************************

//

// verify(f) FUNCTION

//	

// Accepts a form 

// Called by a form by the onBlur method of various form objects. Iterates through the objects on the form, 

// calling various verification functions based of the object type. 

			

				

	function verify(f) {   //open function
	
	// these are just properties for the example. The calling form is named myform.


		document.myForm.Bride_Name.required = true;
		document.myForm.from_email.required = true;
		document.myForm.Bride_Address.required = true;
		document.myForm.Bride_City.required = true;
		document.myForm.Bride_State.required = true;
		document.myForm.Bride_Zip.required = true;
		document.myForm.Groom_Name.required = true;
		document.myForm.Groom_Address.required = true;
		document.myForm.Groom_City.required = true;
		document.myForm.Groom_State.required = true;
		document.myForm.Wedding_Date.required = true;
		document.myForm.Wedding_Date.validate = 'date';
		document.myForm.Groom_Zip.required = true;
		document.myForm.Howheard.required = true;
			
				
	// e is a variable that stores the properties of the elements

		
	

		var e = f; 
		
		image_name = 'img_' + e.name;
		
		if (document[image_name] != null){
		document[image_name].src = 'http://www.megashow.com/javascript/images/blank.gif'; }
	

	// strip lead and trailing spaces from text values

	

		if ((e.type == "text")||(e.type == "textarea")||(e.type == "password")) {

			while(''+e.value.charAt(0)==' ')

				e.value=e.value.substring(1,e.value.length); // trim leading spaces

			while(''+e.value.charAt

				(e.value.length-1)==' ')e.value=e.value.substring(0,e.value.length-1);//trim trailing spaces

			                                            }

	// test to see if the element is text or text area, and if it is required and it is blank, send an error message

	

		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && e.required)  {

	

	    if ((e.value == null) || (e.value == "") || isblank(e.value)) {

		       	image_name = 'img_' + e.name;
				document[image_name].src = 'http://www.megashow.com/javascript/images/required.gif';
				errorcnt = errorcnt+1;
				
                                             }
				
					   

																			}

																																				   

	// if the object is a select, test to see if it is required that a selection is made      															}

				

		if ((e.type == "select-one") && (e.selectedIndex == "0") && e.required) {

					image_name = 'img_' + e.name;
					document[image_name].src = 'http://www.megashow.com/javascript/images/list.gif';
					errorcnt=errorcnt+1;														}

																

	// test validate. run appropriate validation function is it is called for

	

		if (e.validate == "zip" && e.value.length != 0){chkZip(e)};

		if (e.validate == "date" && e.value.length != 0) {chkDate(e)};

		if (e.validate == "mail" && e.value.length != 0) {chkEmail(e)};		

		if (e.validate == "numeric" && e.value.length != 0) {chkNum(e)};		

				

									  }		
//**********************************************************************************

//

// recheck(fname) FUNCTION

//	

// Accepts a form 

// Called by a form by the onSubmit method. Iterates through the objects on the form, 

// calling the verify functions based of the object type. 

	   function recheck(fname){
									  
		errorcnt=0;
	
		
	//iterate through each form item and verify that it's valid
		
		for(var j = 0; j < fname.length; j++){ //start for loop
		
		// item is a variable that stores the properties of the elements
		
		verify(fname.elements[j]);
		
		    
			
			
			
			continue; } // close for loop
			if(errorcnt != 0){
				
				
				return false;}
				
				else
				{return true;}
		
		
			
			} //close function
		
		


