function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
}

function countryChange() {
	var countrySelect = document.getElementById('countrySelect');
	var i = countrySelect.selectedIndex;
	if (i > -1) { mapOver(countrySelect.options[i].value); }
}

function preloadLandingImages() {
	if (document.images) {
		document.gb = newImage("images/WEEE_over.png");
		document.ie = newImage("images/recyc_over.png.png");
	}
}

function preloadMaps() {
	if (document.images) {
		document.at = newImage("images/maps/at.png");
		document.be = newImage("images/maps/be.png");
		document.cz = newImage("images/maps/cz.png");
		document.dk = newImage("images/maps/dk.png");
		document.fi = newImage("images/maps/fi.png");
		document.fr = newImage("images/maps/fr.png");
		document.de = newImage("images/maps/de.png");
		document.ie = newImage("images/maps/ie.png");
		document.it = newImage("images/maps/it.png");
		document.lu = newImage("images/maps/lu.png");
		document.nl = newImage("images/maps/nl.png");
		document.pl = newImage("images/maps/pl.png");
		document.pt = newImage("images/maps/pt.png");
		document.sk = newImage("images/maps/sk.png");
		document.es = newImage("images/maps/es.png");
		document.se = newImage("images/maps/se.png");
		document.uk = newImage("images/maps/uk.png");
	}
}

function newImage(src) {
	if (document.images) {
		rslt = new Image();
		rslt.src = src;
		return rslt;
	}
}

function mapOver(country) {
	var euMapImage = document.getElementById('euMapImage');
	euMapImage.src = "images/maps/"+country+".png";
	document.getElementById(country+'_opt').selected = true;
}

function isValidEmail(strEmail) {
  var emailRegEx = /(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$)/i;
  return (emailRegEx.test(strEmail));
}

function isValidProdNum(pNumberCheck) {
	
	if (pNumberCheck == '') return false;
	
	/* Split given numbers into array */
	pNumberCheck = pNumberCheck.split(',');
	
	/* Strip spaces and transform to uppercase */
	for (i=0; i < pNumberCheck.length; i++)
		pNumberCheck[i] = pNumberCheck[i].replace(/[ \t]/g, '').toUpperCase();
	for (i=0; i < pNumber.length; i++)
		pNumber[i] = pNumber[i].replace(/[ \t]/g, '').toUpperCase();
	
	/* Check if any of the given numbers are in the list */
	for (i=0; i < pNumber.length; i++)
		for (j=0; j < pNumber.length; j++)
			if (pNumber[i] == pNumberCheck[j])
				return true;
	
	return false;
}

function validateLabelRequest() {
	
	var errors = "";
	
	/* Name */
	if (document.getElementById("name").value == "")
		errors += "\n- You must enter your name";
	
	/* Company */
	if (document.getElementById("company").value == "")
		errors += "\n- You must enter your company name";
	
	/* e-mail */
	if (!isValidEmail(document.getElementById("email").value))
		errors += "\n- You must enter a valid email address";
	
	/* Product number */
	if (!isValidProdNum(document.getElementById("pnumber").value))
		errors += "\n- You must enter a valid product model number";
	
	/* Product type */
	if (!(document.getElementById("product_new").checked || 
				(document.getElementById("product_historic") && document.getElementById("product_historic").checked)))
		errors += "\n- You must select which products are being sent for recycling";
	
	/* Recycle reason */
	if (!(document.getElementById("reason_replace").checked || 
				document.getElementById("reason_anotherreplace").checked || 
				document.getElementById("reason_notused").checked || 
				document.getElementById("reason_other").checked))
		errors += "\n- You must select the reason the products are being recycled";
	
	/* Performance */
	if (!(document.getElementById("performance_exceeded").checked || 
				document.getElementById("performance_met").checked || 
				document.getElementById("performance_notmet").checked))
		errors += "\n- You must select the performance level of the products";
		
	/* Satisfaction */
	if (!(document.getElementById("satisfaction_very").checked || 
				document.getElementById("satisfaction_neutral").checked || 
				document.getElementById("satisfaction_not").checked))
		errors += "\n- You must select how satisfied you were with the service provided";
		
	/* Reseller name */
	if (document.getElementById("reseller_name").value == "")
		errors += "\n- You must enter the name of the reseller";
	
	/* Reseller location */
	if (document.getElementById("reseller_location").value == "")
		errors += "\n- You must enter the reseller town/city";
	
	/* Errors? */
	if (errors != "") {
		alert("Please correct the following errors with your submission:"+errors);
		return false;
	} else {
		return true;
	}
}