/*
FUNCTIONS.JS
Common functions throught the site
*/

/* Form tools */
function getParentForm(obj) {
	while (obj.nodeName != "FORM") {
		obj = obj.parentNode;
	}

	return obj;
}

function collateFormData(form) {
	submission = "";
	
	for (i = 0; i < form.elements.length; i++) {
		thisItem = form.elements.item(i);
		thisData = "";
		
		if (thisItem.type == "select-multiple") {
			multiples = "";
			for (x = 0; x < thisItem.options.length; x++) {
				if (thisItem.options[x].selected) { multiples += thisItem.name + "=" + thisItem.options[x].value + "&" }
			}
			thisData = multiples.replace(/.$/,'');
		}
		else if (thisItem.type == "checkbox" || thisItem.type == "radio") {
			if (thisItem.checked) { thisData = thisItem.name + "=" + thisItem.value }
		}
		else {
			thisData = thisItem.name + "=" + thisItem.value;
		}
		
		
		prepend = (i > 0) ? "&" : "";
		if (thisData != "") {
			submission += (prepend + thisData);
		}
	}
	
	return submission;
}

function setFormFlag(obj, element, value) {
	getParentForm(obj).elements[element].value = value;
}

function validateForm(form) {
	validated = true;
	
	switch(form.id) {
		case "checkout_details":
			if (form['psn_you'].value == "") {
				msg       = "You must enter your name.\n\nPlease enter this information before you continue.";
				validated = false;
				form['psn_you'].focus();
			}
			else if (form['psn_address'].value == "") {
				msg       = "You must enter your address.\n\nPlease enter this information before you continue.";
				validated = false;
				form['psn_address'].focus();
			}
			else if (form['psn_postcode'].value == "") {
				msg       = "You must enter your postcode.\n\nPlease enter this information before you continue.";
				validated = false;
				form['psn_postcode'].focus();
			}
			else if (form['psn_telno'].value == "") {
				msg       = "You must enter a contact telephone number.\n\nPlease enter this information before you continue.";
				validated = false;
				form['psn_telno'].focus();
			}
			else if (form['psn_email'].value == "") {
				msg       = "You must enter your e-mail address.\n\nPlease enter this information before you continue.";
				validated = false;
				form['psn_email'].focus();
			}
			else if (!validateEmail(form['psn_email'].value)) {
				msg       = "That's not a proper e-mail address.\n\nplease check you've not mistyped it.";
				validated = false;
				form['psn_email'].focus();
			}
			else if (form['use_shipping'].value != 0) {
				if (form['shp_you'].value == "") {
					msg       = "You must enter the shipping name.\n\nPlease enter this information before you continue.";
					validated = false;
					form['shp_you'].focus();
				}
				else if (form['shp_address'].value == "") {
					msg       = "You must enter the shipping address.\n\nPlease enter this information before you continue.";
					validated = false;
					form['shp_address'].focus();
				}
				else if (form['shp_postcode'].value == "") {
					msg       = "You must enter the shipping postcode.\n\nPlease enter this information before you continue.";
					validated = false;
					form['shp_postcode'].focus();
				}
				else if (form['shp_telno'].value == "") {
					msg       = "You must enter a contact telephone number.\n\nPlease enter this information before you continue.";
					validated = false;
					form['shp_telno'].focus();
				}
				else if (form['shp_email'].value == "") {
					msg       = "You must enter the e-mail address.\n\nPlease enter this information before you continue.";
					validated = false;
					form['shp_email'].focus();
				}
				else if (!validateEmail(form['shp_email'].value)) {
					msg       = "That's not a proper e-mail address.\n\nplease check you've not mistyped it.";
					validated = false;
					form['shp_email'].focus();
				}
			}
			break;
		
		case "checkout_confirm":
			if (form['accept'].checked == false) {
				msg       = "You must read and accept the Terms & Conditions.";
				validated = false;
				form['accept'].focus();
			}
			
			break;
			
		case "contact_panel":
			if (form['you'].value == "") {
				msg       = "You must enter your name.\n\nPlease tell us who you are before continuing.";
				validated = false;
				form['you'].focus();
			}
			else if (form['email'].value == "") {
				msg       = "You must enter your e-mail address.\n\nPlease supply one before continuing.";
				validated = false;
				form['email'].focus();
			}
			else if (!validateEmail(form['email'].value)) {
				msg       = "That's not a proper e-mail address.\n\nplease check you've not mistyped it.";
				validated = false;
				form['email'].focus();
			}
			else if (form['msg'].value == "") {
				msg       = "You did actually say anything!\n\nPlease write your message before continuing.";
				validated = false;
				form['msg'].focus();
			}
			break;
	}

	if (!validated) { alert(msg) }
	
	return validated;
}

function validateEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	return reg.test(email);
}


/* Pop-up windows */
function smallPopup(a) {
	chrome = "menubar=no,resizable=no,scrollbars=yes,width=650,height=700";
	var popup = window.open(a, "details", chrome);
	
	(popup) ? popup.focus() : alert("Your pop-up blocker is preventing the information window from opening. Please disable your pop-up blocker or add this site to your trusted list");
	
	return false;
}

function externalLink(a) {
	var popup = window.open(a, "external");
	
	(popup) ? popup.focus() : alert("Your pop-up blocker is preventing a new browser window from opening. Please disable your pop-up blocker or add this site to your trusted list");
	
	return false;
}



/*
function toggleAdvanced(linkObj) {
	panel  = document.getElementById("adv_options");
	
	panel.style.display != "block" ? panel.style.display = "block" : panel.style.display = "none";
	panel.style.display == "block" ? linkObj.innerHTML = "Hide advanced options" : linkObj.innerHTML = "Advanced options";
	
	return false;
}




function updateMainPage(a) {
	self.opener.location = a;
	self.close();
	return false;
}

function toggleLevels(ref) {
	if (ref == undefined) { ref = document.getElementById("type") }
	
	var show = (ref.value == "nvq") ? "visible" : "hidden";
	
	document.getElementById("levels").style.visibility = show;
}



*/