// JavaScript Document
//check a text feild for content
function checkfeild(strng, stmnt){
	if(strng == ""){
			return stmnt;
	}else{
			return "";
	}
}


//check email feild
function checkemail(strng){
var stmnt = "";
var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
		   stmnt = "Please enter a valid email address.\n";
	}
	
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   stmnt += "Email address contains illegal characters.\n";	
	}
	
	if(stmnt != ""){
		return stmnt;
	}else{
			return "";
	}
}


//check phon number
function checkPhone(strng){
	var stmnt = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   stmnt += "The home phone number contains illegal characters.\n";
	}
	
	if (!(stripped.length == 10)) {
	stmnt += "The home phone number is the wrong length. Make sure you included an area code.\n";
	}
	
		if(stmnt != ""){
		return stmnt;
		}else{
		return "";
		}
	
}

//check radio buttons
function checkradio(path_radioname, stmnt){
	
	function checkRadio(checkvalue) {
	var errorr = "";
		if (!(checkvalue)) {
		   errorr = stmnt+"\n";
		}
	return errorr;    
	}
	
for (i=0, n=path_radioname.length; i<n; i++) {
	  if (path_radioname[i].checked) {
		 var checkvalue = path_radioname[i].value;
		 break;
	  }
}	
return checkRadio(checkvalue);
}

//function to loop and populate a designated feild with all checked answers from a specified checkbox group
function loopPop(targetInput, targetOutput){
	
	var vArray = new Array();
	var counter = 0;

			for (i=0, n=targetInput.length; i<n; i++) {
			  if (targetInput[i].checked) {
				vArray[counter] = targetInput[i].value;
				counter++;
			  }
			}
			
		

targetOutput.value = vArray;
	
	
}


//function to check the whole form
function check_whole_form(){	
	
var error = "";
var path = document.mpfrm;

// Validation #####################################################################

error += checkfeild(path.name.value, "Please enter your name.\n"); 									//Name
error += checkemail(path.email.value); 																//Email

error += (path.birth_year.value == "Year"?"Please select your birth year.\n":""); 					//Date of Birth Year
error += (path.birth_month.value == "Month"?"Please select your birth month.\n":""); 				//Date of Birth Month
error += (path.birth_day.value == "Day"?"Please select your birth day.\n":""); 						//Date of Birth Day

error += checkfeild(path.weight.value, "Please enter your weight.\n"); 								//Weight
error += checkPhone(path.phone.value); 																//Phone

error += (path.dep_year.value == "Year"?"Please select your departure year.\n":""); 				//Date of Departure Year
error += (path.dep_month.value == "Month"?"Please select your departure month.\n":""); 				//Date of Departure Month
error += (path.dep_day.value == "Day"?"Please select your departure day.\n":""); 					//Date of Departure Day

loopPop(path.accomidation, path.accom_hidden);														//populate the hidden accomidation feild
error += checkfeild(path.accom_hidden.value, "Please select an accommodation type.\n"); 						//Type of Accomidation

error += checkfeild(path.countries.value, "Please enter the countries you are visiting.\n"); 		//Which Countries
error += checkradio(path.preg, "Please select if you are pregnant."); 							//Pregnant
error += checkradio(path.breast, "Please select if you are breast feeding."); 					//Breast Feeding



//#####################################################################

	
	if(error != ""){
		alert(error);
	}else{
		path.submit();
	}
	
}