// JavaScript Document

//ALLOWS EXTERNAL LINKS TO BE OPENED IN A NEW WINDOW WITHOUT THE USE OF TARGET ATTRIBUTE
//LOOPS THROUGH LINKS ON PAGE FINDS EXTERNAL LINKS AND ADDS TARGET//
function externalLinks() {
	//SETS DOMAIN OF SITE
	var domainName=document.domain;
	
	//FIND ALL LINKS ON THE CURRENT PAGE
	var externalLinks=document.getElementsByTagName("a");
	//LOOP THROUGH LINKS ARRAY
	for(var i=0; i<externalLinks.length; i++) {
		//GETS CONTENT OF 'HREF' ATTRIBUTE ON CLICKED LINK
		var attribute=externalLinks[i].getAttribute("href");
		
		//GET VALUE http IN 'HREF' **FOR MOZILLA&&
		var contains_http=attribute.indexOf("http");
		
		//GET VALUE domainName **FOR IE**
		var contains_domain=attribute.indexOf(domainName);
		
		//DOES CONTAIN AN 'http' OR DOES NOT CONTAIN domainName
		if(contains_http>-1 && contains_domain==-1) {
			//SET NEW ATTRIBUTE ON LINK
			externalLinks[i].setAttribute("target", "_blank");
		}
		
	}
}

//VALIDATE FORMS
function validateForm(formName) {
	var emptyFields=0;
	
	//CHECK FORM FOR REQUIRED INPUTS
	var inputs = document.forms[formName].getElementsByTagName("input");
	var texareas = document.forms[formName].getElementsByTagName("textarea");
	var selects = document.forms[formName].getElementsByTagName("select");
	
	var errors = "";

	//LOOP THROUGH INPUTS
	for(var i=0; i<inputs.length; i++) {
			//FIND THE CLASS NAME
			if(inputs[i].className=="mand") {
				//IS THE MANDATORY FIELD EMPTY
				if((inputs[i].value=="")||(inputs[i].value=='0.00')) {
					//ADD EMPTY FIELD NAME TO VAR
					emptyFields++;
					inputs[i].style.border="1px solid #cb0000";
				}
			}
	}
	
	//LOOP THROUGH TEXTAREAS
	for(var i=0; i<texareas.length; i++) {
			//FIND THE CLASS NAME
			if(texareas[i].className=="mand") {
				//IS THE MANDATORY FIELD EMPTY
				if(texareas[i].value=="") {
					//ADD EMPTY FIELD NAME TO VAR
					emptyFields++;
					texareas[i].style.border="1px solid #cb0000";
				}
			}
	}
	
	//LOOP THROUGH SELECTS
	for(var i=0; i<selects.length; i++) {
			//FIND THE CLASS NAME
			if(selects[i].className=="mand") {
				//IS THE MANDATORY FIELD EMPTY
				if(selects[i].value=="0") {
					//ADD EMPTY FIELD NAME TO VAR
					emptyFields++;
					selects[i].style.border="1px solid #cb0000";
				}
			}
	}
	
	if(document.getElementById("email") && document.getElementById("verify_email")){
		if(document.getElementById("email").value != document.getElementById("verify_email").value){
			errors = "Please ensure email fields match";
		}
	}

	if(document.getElementById("email").value != ''){
		var x = document.getElementById("email").value;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(x)){}else{errors+="Please ensure email address is valid";}
	}

	//ALERT EMPTY FIELDS
	if(emptyFields || errors) {
		if(emptyFields && errors){
			alert(errors+"\nPlease ensure all mandatory fields are filled in");
		}
		else if(emptyFields){
			alert("Please ensure all mandatory fields are filled in");
		}
		else if(errors){
			alert(errors);
		}
		return false;
	}else{
		return true;
	}
}


// EXPAND MENU
function expandMenu(listName) {
	var stay = document.getElementById('stay').value;
	var control=document.getElementById(listName);
	var listElements=control.getElementsByTagName("li");
	var idname = "";
	
	for(var i=0; i<listElements.length; i++) {
		//EXPAND MENU
		listElements[i].onmouseover=function() {
				var uls=this.getElementsByTagName("ul");
				for(var n=0; n<uls.length; n++) {
					if(n==0) {
						uls[n].style.display="block";
						var idname = uls[n].getAttribute("id");
						if(idname !== stay) { 
							document.getElementById(stay).style.display = "none";
						}
					}
					
				}
			}
		//CLOSE MENU	
		listElements[i].onmouseout=function() {
			var uls=this.getElementsByTagName("ul");
				for(var n=0; n<uls.length; n++) {
					if(n==0) {
						uls[n].style.display="none";
						document.getElementById(stay).style.display = "block";
					}
					
				}
		}
	}
}

function goto(shortcountry, base){
var whereto = document.getElementById('product_listing').options[document.getElementById('product_listing').selectedIndex].value;
window.location.href= base+shortcountry + '/product_more_info/boots/x/' + whereto;
}

function goto2(shortcountry, base){
var whereto = document.getElementById('product_listing').options[document.getElementById('product_listing').selectedIndex].value;
alert(base);
alert(base+'/'+shortcountry + '/product_more_info/boots/x/' + whereto);
}

//EXECUTES ONLOAD FUNCTIONS
function loadfunctions() {
	externalLinks();
}

//LOADS IN MULTIPLE FUNCTIONS ONLOAD
window.onload=loadfunctions;
