
// JavaScript Document
/* validation of the downloads form */
// JavaScript Document
var warning_color = "#FFCCCC";
var normal_color = "#EEEEEE";

// Create new main array. Good for empty text fields
var txt_name = new Array() 
// Form field names is listed first followed by a descriptive name to be show in the js pop up if left blank
txt_name[0] = new Array("NAME","Your Name") 
txt_name[1] = new Array("AFFILIATION","Your Affiliation (what school or business)") 

var drop = new Array()
drop[0] = new Array("INTERESTS", "Topics of Interests")

// function for the value of the dropdown field
function drop_valid(drop_name) { // this will only work when the first value is set to: ""
	var d;
	var missing_drop = "";
	
	for (d=0; d<drop_name.length; d++){
		if (document.download[drop_name[d][0]].selectedIndex == ""){
			missing_drop+= drop_name[d][1] + "\n";
			document.download[drop_name[d][0]].style.backgroundColor = warning_color;
		} else {
			document.download[drop_name[d][0]].style.backgroundColor = normal_color;
		}
	}
	return missing_drop;
}


// check for blank text fields
function missing_content(){
	// check the regular text fields for empty content
	var j;
	var error_ct=0; // a counter variable for the errors;
	var missing_empty = "";


	// [01] CHECK THE TEXT FIELDS FROM THE ARRAY ABOVE
	for (j=0; j<txt_name.length; j++){
		if (document.download[txt_name[j][0]].value == "") {
			missing_empty+= txt_name[j][1] + "\n";
			document.download[txt_name[j][0]].style.backgroundColor = warning_color;
		} else {
			document.download[txt_name[j][0]].style.backgroundColor = normal_color;
		}
	}
	return missing_empty;
}

// email validation
function checkEmail(myForm) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm)){
		return (true)
	}
	return (false)
}

function validate(){
	var missing = "";

	// ck for blank fields
	missing = missing_content();

	// ck and validate the email address
	email = checkEmail (document.download["EMAIL"].value);
	
	if (email == false){
		missing+= "Invalid Email Address\n";
		document.download["EMAIL"].style.backgroundColor = warning_color;
	} else {
		document.download["EMAIL"].style.backgroundColor = normal_color;
	}

	// ck the drop down box
	drop_ck = drop_valid(drop);
	missing+= drop_ck;

	// FINALE: is anything missing?
	if (missing != ""){
		missing_hdr = "The Following Information is Required:\n";
		alert (missing_hdr + missing);
		return false;
	} else {	
		return true;
	}
}