function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function inDepthShow() {
	var fieldset = document.getElementById('inDepth');
	if(fieldset) {
		var display = fieldset.style.display;
		var button = document.getElementById('inDepthLink');
		if(display == "none") {
			fieldset.style.display = "block";
			if(button) {
				button.style.display = "none";
			}
		}
		if(display == "block") {
			fieldset.style.display = "none";
			if(button) {
				button.style.display = "block";
			}
		}
	}
}

function validateContactForm(thisForm) {
	var msg = "";
	var selected = thisForm.contact_type.value;
	if(selected == "") {
		msg += "- " + lbl_contact_type + "\n";
	}
	if (selected == "company") {
		if (thisForm.contact_company.value == ""){
			msg += "- " + lbl_contact_company + "\n";
		}
		if(thisForm.contact_function.value == "") {
			msg += "- " + lbl_contact_function + "\n";
		}
	}
	if(selected == "press") {
		if(thisForm.contact_media.value == "") {
			msg += "- " + lbl_contact_media + "\n";
		}
	}
	if (thisForm.contact_name.value == ""){
		msg += "- " + lbl_contact_name + "\n";
	}
	if (thisForm.contact_country.value == ""){
		msg += "- " + lbl_contact_country + "\n";
	}
	if (thisForm.contact_tel.value == ""){
		msg += "- " + lbl_contact_tel + "\n";
	}
	if (thisForm.contact_email.value == ""){
		msg += "- " + lbl_contact_mail + "\n";
	}
	if (thisForm.contact_message.value == ""){
		msg += "- " + lbl_contact_message + "\n";
	}
	if (msg != ""){
		msg = lbl_contact_fill_in +  ": \n" + msg;
		alert(msg);
		return false;
	}
	if(!checkemail(thisForm.contact_email.value)){
		alert (lbl_contact_check_mail);
		return false;
	}
	return true;
}

function validatePressForm(thisForm) {
	
	var zoneForm = document.zoneForm;

	var selected_zone = zoneForm.press_zone_select.value;
	if(selected_zone) {
		var msg = "";
		if(selected_zone == "press") {
			if(thisForm.press_media.value == "") {
				msg += "- " + lbl_contact_media + "\n";
			}
			if(thisForm.press_media_name.value == "") {
				msg += "- " + lbl_press_media_name + "\n";
			}
		}
		if(selected_zone == "customer") {
			if (thisForm.press_company.value == ""){
				msg += "- " + lbl_contact_company + "\n";
			}
			if(thisForm.press_function.value == "") {
				msg += "- " + lbl_contact_function + "\n";
			}
			if(!thisForm.press_sales_rep_no.checked) {
				if(thisForm.press_sales_rep.value == "") {
					msg += "- " + lbl_press_sales_rep + "\n";
				}
			}
		}
	
		if (thisForm.press_name.value == ""){
			msg += "- " + lbl_contact_name + "\n";
		}
		if (thisForm.press_country.value == ""){
			msg += "- " + lbl_contact_country + "\n";
		}
		if (thisForm.press_tel.value == ""){
			msg += "- " + lbl_contact_tel + "\n";
		}
		if (thisForm.press_email.value == ""){
			msg += "- " + lbl_contact_mail + "\n";
		}
		if (thisForm.press_password.value == ""){
			msg += "- " + lbl_press_password + "\n";
		}
		if (thisForm.press_password2.value == ""){
			msg += "- " + lbl_press_password_confirm + "\n";
		}
		if (thisForm.checkcaptcha.value == ""){
			msg += "- " + lbl_enter_capcha + "\n";
		}
		if (msg != ""){
			msg = lbl_contact_fill_in +  ": \n" + msg;
			alert(msg);
			return false;
		}
		if(!checkemail(thisForm.press_email.value)){
			alert (lbl_contact_check_mail);
			return false;
		}
		if(!validatePressPwd(thisForm)) {
			return false;
		}
		return true;
	}else {
		return false;
	}
}

function validatePressPwd(thisForm) {
	var msg = "";
	var pwd1 = thisForm.press_password.value;
	var pwd2 = thisForm.press_password2.value;
	if(pwd1 != pwd2) {
		msg += lbl_press_password_mismatch;
	}
	if(msg != "") {
		msg = "Error: \n" + msg;
		alert(msg);
		return false;
	}
	return true;
}

function showLogin() {
	var form = document.getElementById('loginForm');
	if(form) {
		var display = form.style.display;
		var sidebar = document.getElementById('sidebar');
		if(display == "block") {
			form.style.display = "none";
			if(sidebar) {
				sidebar.className = "";
			}
		}
		if(display == "none") {
			form.style.display = "block";
			if(sidebar) {
				sidebar.className = "width_loginOpen";
			}
		}
	}
}

function validateLoginForm(thisForm) {
	var msg = "";
	
	if(thisForm.username.value == "") {
		msg += "- " + lbl_press_username + "\n";
	}
	if(thisForm.password.value == "") {
		msg += "- " + lbl_press_password + "\n";
	}
	if (msg != ""){
		msg = lbl_contact_fill_in +  ": \n" + msg;
		alert(msg);
		return false;
	}
	return true;
}

function validateForumLoginForm(thisForm) {
	var msg = "";
	
	if(thisForm.forum_username.value == "") {
		msg += "- " + lbl_press_username + "\n";
	}
	if(thisForm.forum_password.value == "") {
		msg += "- " + lbl_press_password + "\n";
	}
	if (msg != ""){
		msg = lbl_contact_fill_in +  ": \n" + msg;
		alert(msg);
		return false;
	}
	return true;
}

function validateForgotPwd(thisForm) {
	var msg = "";
	
	if(thisForm.press_email.value == "") {
		msg += "- " + lbl_press_username + "\n";
	}
	if (msg != ""){
		msg = lbl_contact_fill_in +  ": \n" + msg;
		alert(msg);
		return false;
	}
	return true;
}

function validateForumRegForm(thisForm) {
	var msg = "";
	
	if (thisForm.forum_name.value == ""){
		msg += "- " + lbl_contact_name + "\n";
	}
	if (thisForm.forum_email.value == ""){
		msg += "- " + lbl_contact_mail + "\n";
	}
	if (thisForm.forum_gender.value == ""){
		msg += "- " + lbl_forum_gender + "\n";
	}
	if (thisForm.forum_country.value == ""){
		msg += "- " + lbl_contact_country + "\n";
	}
	if (thisForm.forum_password.value == ""){
		msg += "- " + lbl_press_password + "\n";
	}
	if (thisForm.forum_password2.value == ""){
		msg += "- " + lbl_press_password_confirm + "\n";
	}
	if (msg != ""){
		msg = lbl_contact_fill_in +  ": \n" + msg;
		alert(msg);
		return false;
	}
	if(!checkemail(thisForm.forum_email.value)){
		alert (lbl_contact_check_mail);
		return false;
	}
	if(!validateForumPwd(thisForm)) {
		return false;
	}
	return true;
}

function validateForumPwd(thisForm) {
	var msg = "";
	var pwd1 = thisForm.forum_password.value;
	var pwd2 = thisForm.forum_password2.value;
	if(pwd1 != pwd2) {
		msg += lbl_press_password_mismatch;
	}
	if(msg != "") {
		alert(msg);
		return false;
	}
	return true;
}

function checkemail(mail){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(mail)) return true; else return false;
}

function showhideContactForm() {
	document.getElementById("company").style.display = "none";
	document.getElementById("website").style.display = "none";
	document.getElementById("function").style.display = "none";
	document.getElementById("media").style.display = "none";
	var selected = this.document.contactForm.contact_type.options[this.document.contactForm.contact_type.selectedIndex].value;
	if(selected == "company") {
		document.getElementById("company").style.display = "block";
		document.getElementById("website").style.display = "block";
		document.getElementById("function").style.display = "block";
	}
	if(selected == "press"){
		document.getElementById("website").style.display = "block";
		document.getElementById("media").style.display = "block";
	}
}

function doChangeZone() {
	var select = document.getElementById('press_zone_select');
	if(select) {
		var selected = select.options[select.selectedIndex].value;
		var form = document.pressForm;
		var field = form.press_zone;
		var div = document.getElementById('form');
		var press_holder = document.getElementById('press_holder');
		var customer_holder = document.getElementById('customer_holder');
		if(press_holder && customer_holder) {
			if(selected == "") {
				div.style.display = "none";
			}else {
				if(selected == "press") {
					press_holder.style.display = "block";
					customer_holder.style.display = "none";
					field.value = "press";
					div.style.display = "block";
				}
				if(selected == "customer") {
					press_holder.style.display = "none";
					customer_holder.style.display = "block";
					field.value = "customer";
					div.style.display = "block";
				}
			}
		}
	}
}

function quote(id) {
	var post = document.getElementById('text' + id).innerHTML;
	var poster = document.getElementById('user' + id).innerHTML;
	var box = document.getElementById('forum_reply');
	if(box) {
		box.innerHTML = '[QUOTE=' + poster + ']' + post + '[/QUOTE]';
	}
}

function popup(url, width, height) {
	theWidth = width;
	theHeight = height;
	theTop = Math.ceil((screen.height-theHeight*1.5)/2);
	theLeft = Math.ceil((screen.width-theWidth)/2);
	winopts = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,copyhistory=0,width="+theWidth+",height="+theHeight+",top="+theTop+",left="+theLeft;
	smallwindow=window.open(url,"Popup",winopts);
	smallwindow.focus();
}

