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

//check email feild
function checkemail(strng, xVar){
var stmnt = "";
var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
		   stmnt = xVar+" email address is not valid.\n";
	}
	
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   stmnt += xVar+" 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 office phone number contains illegal characters.\n";
	}
	
	if (!(stripped.length == 10)) {
	stmnt += "The office 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;
	
	
}

//########### Validate Form
function fValidate(){	

	var error = "";
	var path = document.tdpfrm;	

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

loopPop(path.media_support, path.media_support_hidden);												//Populate the hidden field for emailing ** Not Required

error += checkfeild(path.last_name.value, "Please enter your Last Name.\n"); 						//Last Name
error += checkfeild(path.first_name.value, "Please enter your First Name.\n"); 						//First Name
error += checkfeild(path.media_org.value, "Please enter your Media Orginization.\n"); 				//Media Orginization
error += checkfeild(path.address.value, "Please enter your Postal Address.\n"); 					//Postal Address
error += checkfeild(path.city.value, "Please enter your City.\n"); 									//City
error += checkfeild(path.prov.value, "Please enter your Province / State.\n"); 						//Province / State
error += checkfeild(path.postal.value, "Please enter your Postal Code/Zip.\n"); 					//Postal Code/Zip
error += checkPhone(path.tel.value); 																//Phone (office)
error += checkemail(path.email.value, "Your"); 														//Email

error += checkfeild(path.assoc1.value, "Please enter your Associate's Name # 1.\n"); 				//Associate's Names # 1

error += checkradio(path.hotel, "Please select yes or no regarding accommodations.");				//Hotel

error += checkfeild(path.arrival_date.value, "Please enter an Arrival Date.\n"); 					//Arrival Date
error += checkfeild(path.departure_date.value, "Please enter an Departure Date.\n"); 				//Departure Date

error += checkradio(path.conact_prefference, "Please select a contact preference.");				//Contect Preference

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

	if(error != ""){
		alert(error);
	}else{
		path.action = "email/mediaform.email.php";
		path.submit();	
	}	
	
}
