addLoadEvent(init_mailinglist);

function init_mailinglist() {
	var frmMailinglist = document.getElementById("mailinglist_form");
	if(frmMailinglist) {
		frmMailinglist.onsubmit = validateMailinglist;
	}
}

function validateMailinglist() {
	var theForm = document.getElementById("mailinglist_form");

	var error = 0;
	var msg = "";

	if(theForm.company.value=="") {
		msg += "\n- " + label_mailing_company;
		error++;
	}
	if(theForm.city.value=="") {
		msg += "\n- " + label_mailing_city;
		error++;
	}
	if(theForm.country.value=="") {
		msg += "\n- " + label_mailing_country;
		error++;
	}
	if(theForm.title.selectedIndex==0) {
		msg += "\n- " + label_mailing_title;
		error++;
	}
	if(theForm.last_name.value=="") {
		msg += "\n- " + label_mailing_last_name;
		error++;
	}
	if(theForm.email.value=="") {
		msg += "\n- " + label_mailing_email;
		error++;
	}
	if((theForm.how_did_you_hear0.selectedIndex==0)
	|| (theForm.how_did_you_hear0.selectedIndex==1 && theForm.how_did_you_hear1.selectedIndex==0)
	|| (theForm.how_did_you_hear0.selectedIndex==2 && theForm.how_did_you_hear2.selectedIndex==0)
	) {
		msg += "\n- " + label_mailing_how_did_you_hear;
		error++;
	}
	if((theForm.how_did_you_hear0.selectedIndex==1 && theForm.how_did_you_hear1.selectedIndex==3 && theForm.how_did_you_hear_other.value=="")
	|| (theForm.how_did_you_hear0.selectedIndex==2 && theForm.how_did_you_hear2.selectedIndex!=3 && theForm.how_did_you_hear_other.value=="")
	) {
		msg += "\n- " + label_mailing_how_did_you_hear + " > " + lbl_specify;
		error++;
	}
	
	if (error) {
		alert(label_mailing_required_fields + msg);
		return false;
	}
	if (!validateEmail(theForm.email.value)) {
		alert(label_mailing_valid_email);
		return false;
	}
	theForm.x.value = "abc123";
	return true;
}
function validateEmail(email) {
	
	if (email != null && email != "") {
		a = email.lastIndexOf("@");
		b = email.lastIndexOf(".");
		c = email.indexOf(":");
		d = email.indexOf("/");
		e = email.substring(0,a);
		f = e.indexOf("@");
		g = email.substring(a+1,email.length);
		h = g.indexOf("[");
		i = g.indexOf("]");
		j = g.indexOf("<");
		k = g.indexOf(">");
		l = email.substring(a+1,b);
		m = email.substring(b+1,email.length);
		n = email.substring(0,a);
		p = email.indexOf("'");
		q = email.indexOf("\"");
		o = 0;
		if (a > b) {o++};
		if (c != -1) {o++};
		if (d != -1) {o++};
		if (f != -1) {o++};
		if (h != -1) {o++};
		if (i != -1) {o++};
		if (j != -1) {o++};
		if (k != -1) {o++};
		if (l.length < 2) {o++};
		if (m.length < 2) {o++};
		if (n.length < 1) {o++};
		if (p != -1) {o++};
		if (q != -1) {o++};
		if (o != 0) {
			return false;
		}
	}
	return true;
}
function click_all() {
	var theForm = document.mailinglist_form;
	
	if(theForm.all_information.checked) {
		theForm.general_information.checked = true;
		theForm.following_information.checked = true;
		theForm.tradeshow_information.checked = true;
		theForm.all_products.checked = true;
		for (i = 0; i<theForm.elements.length; ++ i) {	
			if (theForm.elements[i].name.substring(0,11)=="product_ids") {
				theForm.elements[i].checked=true;
			}
		}
	}
	for (i = 0; i<theForm.elements.length; ++ i) {	
		if (theForm.elements[i].name.substring(0,11)=="product_ids") {
			theForm.elements[i].checked=true;
		}
	}
}
function click_all_products() {
	var theForm = document.mailinglist_form;
	
	if(theForm.all_products.checked) {
	
		for (i = 0; i<theForm.elements.length; ++ i) {	
			if (theForm.elements[i].name.substring(0,11)=="product_ids") {
				theForm.elements[i].checked=true;
			}
		}
	}
}
function click_one(the_one) {
	var theForm = document.mailinglist_form;
	
	if(!the_one.checked) {
		theForm.all_information.checked = false;
		theForm.all_products.checked = false;
	}
}
function how1(dropdown) {
	if(dropdown.selectedIndex==0) {
		document.getElementById("how_did_you_hear1").style.display="none";
		document.getElementById("how_did_you_hear2").style.display="none";
		document.getElementById("how_specify").style.display="none";
	}
	if(dropdown.selectedIndex==1) {
		document.getElementById("how_did_you_hear1").style.display="inline";
		document.getElementById("how_did_you_hear2").style.display="none";
		document.getElementById("how_specify").style.display="none";
	}
	if(dropdown.selectedIndex==2) {
		document.getElementById("how_did_you_hear1").style.display="none";
		document.getElementById("how_did_you_hear2").style.display="inline";
		document.getElementById("how_specify").style.display="none";
	}
	document.getElementById("how_did_you_hear1").selectedIndex==0;
	document.getElementById("how_did_you_hear2").selectedIndex==0;
	document.getElementById("how_did_you_hear_other").value="";
}
function how2(dropdown2) {
	var dropdown1 = document.getElementById("how_did_you_hear0");
	
	if(dropdown1.selectedIndex==1) { // search engine
		if(dropdown2.selectedIndex==3) { // other search engine
			document.getElementById("how_specify").style.display="block";
		} else {
			document.getElementById("how_specify").style.display="none";
		}
	}
	document.getElementById("how_did_you_hear_other").value="";
}
function how3(dropdown3) {
	var dropdown1 = document.getElementById("how_did_you_hear0");
	
	if(dropdown1.selectedIndex==2) { // other
		if(dropdown3.selectedIndex==3) { // direct mail
			document.getElementById("how_specify").style.display="none";
		} else {
			document.getElementById("how_specify").style.display="block";
		}
	}
	document.getElementById("how_did_you_hear_other").value="";
}
