// 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 phone number contains illegal characters.\n";
	}
	
	if (!(stripped.length == 10)) {
	stmnt += "The 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.sdsfrm;

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

error += checkfeild(path.name.value, "Please enter your name.\n"); 									//Name
//error += checkfeild(path.title.value, "Please enter your title.\n"); 								//Title
//error += checkfeild(path.company.value, "Please enter your company name.\n"); 					//Company
error += checkPhone(path.phone.value); 																//Phone
error += checkemail(path.email.value); 																//Email


// validate the mailing address if it has been selected
if(path.delivery[1].checked){
	
	error += checkfeild(path.address.value, "Please enter your address.\n"); 						//Address
	error += checkfeild(path.city.value, "Please enter your city.\n"); 								//City
	error += checkfeild(path.province.value, "Please enter your province/state.\n"); 				//Province
	error += checkfeild(path.country.value, "Please enter your country.\n"); 						//Country
	error += checkfeild(path.postal.value, "Please enter your postal/zip code.\n"); 				//Postal
	
}


loopPop(path.industry, path.industry_hidden);

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

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



//############# Function to dynamically display address content ##############
function fAddress(){
	path = document.sdsfrm;
	
	if(path.delivery[1].checked){
	
		document.getElementById('addressTbl').style.display = 'block';
	
	}else{
		
		document.getElementById('addressTbl').style.display = 'none';
		
	}
	
}


