// JavaScript Document
// JavaScript Document
// form functions
function auto_fill(frm_name, frm_value){
	document.contact[frm_name].value = frm_value;
}
/* validation of the downloads form */
// JavaScript Document
var warning_color = "#FFCCCC";
var normal_color = "#FFFFFF";

// 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("FNAME","Your First Name") 
txt_name[1] = new Array("LNAME","Your Last Name") 

// fields for sending information by mail
var info_mail = new Array()
info_mail[0] = new Array("MAILING_ADDRESS","Your Mailing Address")
info_mail[1] = new Array("CITY", "City")
info_mail[2] = new Array("ZIP", "Zip Code")

// fields for sending information by phone
var info_phone = new Array()
info_phone[0] = new Array("WORK_NUMBER", "Work Number")
info_phone[1] = new Array("HOME_NUMBER", "Home Number")

// turn all the fields to the default white color
function clear_fields(){
	for (x=0; x < document.contact.length; x++){
		document.contact[x].style.backgroundColor = normal_color;
	};
	
//	document.contact["EMAIL_NEWSLETTER"].style.backgroundColor = "#EFEBD7";
}
// check for blank text fields
function missing_content(arr_name){
	// check the regular text fields for empty content
	var j;
	var error_ct=0; // a counter variable for the errors;
	var missing_empty = "";

	arr_name = eval(arr_name);
	
	// [01] CHECK THE TEXT FIELDS FROM THE ARRAY ABOVE
	for (j=0; j<arr_name.length; j++){
		if (document.contact[arr_name[j][0]].value == "") {
			missing_empty+= arr_name[j][1] + "\n";
			document.contact[arr_name[j][0]].style.backgroundColor = warning_color;
		} else {
			document.contact[arr_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)
}

// check the type of information the user is opting for
function ckbox_type_info(){
	//alert (document.contact["INFORMATION"].checked);
	document.contact["want_information"].value = "false";
	
	/*
	var c_value = "";
	for (var i=0; i < document.contact["INFORMATION[]"].length; i++)
   		{
   			if (document.contact["INFORMATION[]"][i].checked){
					document.contact["want_information"].value = "true";
      		}
   		}
		*/
	if (document.contact["INFORMATION"].checked){
		document.contact["want_information"].value = "true";
	}
}


// check the checkboxes to indicate how to send the information
function ckbox_value(ckbox_name) {
	var c_value = "";
	var info_missing = "";
	document.contact["send_information"].value = "false"; // this says that the user doesn't want any information sent to them
	for (var i=0; i < document.contact[ckbox_name].length; i++) {
		if (document.contact[ckbox_name][i].checked)
      		{
      			c_value = document.contact[ckbox_name][i].value;
				
				// the user wants information sent to them
				document.contact["send_information"].value = "true";
				
				// how the user wants the information sent to them, we now validate the fields for the option.
				if (c_value == "MAIL"){
					info_missing += missing_content("info_mail");
				}
				
				if (c_value == "PHONE"){
					info_missing += missing_content("info_phone");
				}
      		}
   		}
	//alert (info_missing);
	
	return info_missing;
}


function validate(){
	// turn all the form fields back to their default white color
	clear_fields();
	
	var missing = "";

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

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

/*
	// see what type of information the user is looking for, if any
	ckbox_type_info();
	
	// ck the methods of how to send the information
	var send_how = ckbox_value("SEND_INFO[]");
	if (send_how != ""){
		missing += send_how;
	}
	
	
	//alert ("want: " + document.contact["want_information"].value + " send: " + document.contact["send_information"].value);
	// run some validation about wanting and sending the information
	if (document.contact["want_information"].value == "true" && document.contact["send_information"].value == "false"){
		missing += "Please indicate how we should send the information to you (via email, mail or phone)\n";
	};
	
	if (document.contact["want_information"].value == "false" && document.contact["send_information"].value == "true"){
		missing += "Please indicate what type of information to send to you.\n";
	};
*/
	
	// FINALE: is anything missing?
	if (missing != ""){
		missing_hdr = "The Following Information is Required:\n";
		alert (missing_hdr + missing);
		return false;
	} else {	
		return true;
	}
}