function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

function showHide(targetName) {
	if( document.getElementById ) {
		target = document.getElementById(targetName);
	} else if( document.all ) {
		target = document.all[targetName];
	}
	
	if( target ) {
		if( target.style.display == "none" ) {
			target.style.display = "inline";
		} else {
			target.style.display = "none";
		}
	}
}

function guaranteehide() {
	if( document.getElementById ) {
	
		if(document.getElementById("bargain").style.display == "none") {
			document.getElementById("bargain").style.display = "inline";
			document.getElementById("bargain2").style.display = "none";
		} else {
			document.getElementById("bargain").style.display = "none";
			document.getElementById("bargain2").style.display = "inline";
		}
		
	} else if( document.all ) {
	
		if(document.all["bargain"].style.display == "none") {
			document.all["bargain"].style.display = "inline";
			document.all["bargain2"].style.display = "none";
		} else {
			document.all["bargain"].style.display = "none";
			document.all["bargain2"].style.display = "inline";
		}
		
	}
}


function MM_jumpMenu(targ,selObj,restore){
  eval(targ+".location='https://www.whybuynew.co.uk/"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


function shippingarea(){
	if(document.getElementById("region").value != '') { //selObj.options[selObj.selectedIndex].value
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		thetime = '&thetime='+new Date().getTime();
		liveSearchReq.open('GET',document.getElementById("region").value+thetime,false);
		liveSearchReq.send(null);
	
		if (liveSearchReq.readyState == 4) {
			var res = document.getElementById("resback");
			res.innerHTML = liveSearchReq.responseText;
		}
	} else {
		alert('Chosen option is blank')
	}
}

/* split into 2 functions so that it does the scroll before it shows/hides the divs */
function fixright()
{
	window.scrollTo(0,0);
	updatingcart();
}

function updatingcart()
{
	document.getElementById('rightcol').style.visibility = 'hidden';
	document.getElementById('rightcol').style.height = 0;
	document.getElementById('updatingcart').style.visibility = 'visible';
}


function getcarddetails(region,country){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	liveSearchReq.open("GET","carddetails.html?region="+region+"&country="+country,false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("entercarddetails");
		res.innerHTML = liveSearchReq.responseText;
		var  res3 = document.getElementById("hideable");
		res3.innerHTML = '';
		var  res4 = document.getElementById("hideable2");
		res4.innerHTML = '';
		var  res5 = document.getElementById("hideable3");
		res5.innerHTML = '';
		window.scrollTo(0,0);
	}
}

function fillindeliveryaddress(toggle){
	if (toggle =="on") {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
	
		liveSearchReq.open("GET","carddelivery.html?thearea=nulltrigger",false);
		liveSearchReq.send(null);
		
		if (liveSearchReq.readyState == 4) {
			var  res = document.getElementById("addresscheckbox");
			res.innerHTML = liveSearchReq.responseText;
			
			// copy form fields
			/*
			document.forms.main_form.title.value = document.forms.main_form.bill_title.value;
			document.forms.main_form.Firstname.value = document.forms.main_form.bill_Firstname.value;
			document.forms.main_form.Surname.value = document.forms.main_form.bill_Surname.value;
			document.forms.main_form.Address1.value = document.forms.main_form.bill_Address1.value;
			document.forms.main_form.Address2.value = document.forms.main_form.bill_Address2.value;
			document.forms.main_form.Town.value = document.forms.main_form.bill_Town.value;
			document.forms.main_form.County.value = document.forms.main_form.bill_County.value;
			document.forms.main_form.Postcode.value = document.forms.main_form.bill_Postcode.value;
			document.forms.main_form.Tel.value = document.forms.main_form.bill_Tel.value;
			document.forms.main_form.Country.value = document.forms.main_form.bill_Country.value;
			document.forms.main_form.Email.value = document.forms.main_form.bill_Email.value;
			*/
		}
	} else {
		var  res = document.getElementById("addresscheckbox");
		res.innerHTML = '';
	}
}


function makesend(thestring){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	liveSearchReq.open("GET","sendprotx.html?vars=true&"+thestring,false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("entercarddetails");
		res.innerHTML = liveSearchReq.responseText;
	}
}


function showsecurityinfo(){
	window.open('securityinfo.html','security','resizable=yes,width=400,height=230,scrollbars=yes');
}


function showHide(targetName) {
	if( document.getElementById ) { // NS6+
		target = document.getElementById(targetName);
	} else if( document.all ) { // IE4+
		target = document.all[targetName];
	}
	
	if( target ) {
		if( target.style.display == "none" ) {
			target.style.display = "inline";
		} else {
			target.style.display = "none";
		}
	}
} 


function sendtovalidate() {
	var  res = document.getElementById("entercarddetails-errors");
	res.innerHTML = '<img src="images/processing.gif">';
	window.setTimeout('actuallysend()',1750);
}

function actuallysend() {
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	var thestring = ''
	
	try {
		if (document.getElementById('del_day')) {
			var thestring = "&del_day="+document.getElementById('del_day').value
		}
	} catch (err) {}
	

	if (document.getElementById('Title')) {
		var thestring = thestring+"&title="+document.getElementById('Title').value
	} else {
		var thestring = thestring+"&title="+document.getElementById('bill_title').value
	}
	
	if (document.getElementById('Firstname')) {
		var thestring = thestring+"&firstname="+document.getElementById('Firstname').value
	} else {
		var thestring = thestring+"&firstname="+document.getElementById('bill_Firstname').value
	}
	
	if (document.getElementById('Surname')) {
		var thestring = thestring+"&surname="+document.getElementById('Surname').value
	} else {
		var thestring = thestring+"&surname="+document.getElementById('bill_Surname').value
	}
			
	if (document.getElementById('Address1')) {
		var thestring = thestring+"&address1="+document.getElementById('Address1').value
	} else {
		var thestring = thestring+"&address1="+document.getElementById('bill_Address1').value
	}
	
	if (document.getElementById('Address2')) {
		var thestring = thestring+"&address2="+document.getElementById('Address2').value
	} else {
		var thestring = thestring+"&address2="+document.getElementById('bill_Address2').value
	}		

	if (document.getElementById('Town')) {
		var thestring = thestring+"&town="+document.getElementById('Town').value
	} else {
		var thestring = thestring+"&town="+document.getElementById('bill_Town').value
	}

	if (document.getElementById('County')) {
		var thestring = thestring+"&county="+document.getElementById('County').value
	} else {
		var thestring = thestring+"&county="+document.getElementById('bill_County').value
	}

	if (document.getElementById('Postcode')) {
		var thestring = thestring+"&postcode="+document.getElementById('Postcode').value
	} else {
		var thestring = thestring+"&postcode="+document.getElementById('bill_Postcode').value
	}

	if (document.getElementById('Country')) {
		var thestring = thestring+"&country="+document.getElementById('Country').value
	} else {
		var thestring = thestring+"&country="+document.getElementById('bill_Country').value
	}

	if (document.getElementById('Tel')) {
		var thestring = thestring+"&tel="+document.getElementById('Tel').value
	} else {
		var thestring = thestring+"&tel="+document.getElementById('bill_Tel').value
	}

	if (document.getElementById('Mobile')) {
		var thestring = thestring+"&mobile="+document.getElementById('Mobile').value
	} else {
		var thestring = thestring+"&mobile="
	}
	
	if (document.getElementById('Email')) {
		var thestring = thestring+"&email="+document.getElementById('Email').value
	} else {
		var thestring = thestring+"&email="+document.getElementById('bill_Email').value
	}

	
	//liveSearchReq.open("GET","http://10.0.1.32/clients/whybuynew_protx/validate_overrule.html?name_on_card="
	liveSearchReq.open("GET","https://www.whybuynew.co.uk/validate_overrule.html?name_on_card="+document.getElementById('name_on_card').value
		+"&card_type="+document.getElementById('card_type').value
		+"&card_num="+document.getElementById('card_num').value
		+"&start_month="+document.getElementById('start_month').value
		+"&start_year="+document.getElementById('start_year').value
		+"&expiry_month="+document.getElementById('expiry_month').value
		+"&expiry_year="+document.getElementById('expiry_year').value
		+"&card_issue="+document.getElementById('card_issue').value
		+"&cvv2="+document.getElementById('cvv2').value
		+"&thearea="+document.getElementById('thearea').value
		+"&shipping_instructions="+document.getElementById('shipping_instructions').value
		+"&bill_title="+document.getElementById('bill_title').value
		+"&bill_firstname="+document.getElementById('bill_Firstname').value
		+"&bill_surname="+document.getElementById('bill_Surname').value
		+"&bill_address1="+document.getElementById('bill_Address1').value
		+"&bill_address2="+document.getElementById('bill_Address2').value
		+"&bill_town="+document.getElementById('bill_Town').value
		+"&bill_county="+document.getElementById('bill_County').value
		+"&bill_postcode="+document.getElementById('bill_Postcode').value
		+"&bill_country="+document.getElementById('bill_Country').value
		+"&bill_tel="+document.getElementById('bill_Tel').value
		+"&bill_email="+document.getElementById('bill_Email').value	
		+thestring,false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("entercarddetails-errors");
		res.innerHTML = liveSearchReq.responseText;
	}
}


/* currency functions */
function curr_change(){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	liveSearchReq.open("GET","curr_change.htm",false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		location.reload(true);
	}
}



/* OMG make javascript functions returned by ajax parse-able by adding them to the DOM! */
function parseScript(_source) {
	var source = _source;
	var scripts = new Array();
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
			eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
		}
	}
	// Return the cleaned source
	return source;
}


function gotopaypal() {
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	liveSearchReq.open("GET","express.html?thearea="+document.getElementById('thearea').value,false);
	liveSearchReq.send(null);
	
	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("entercarddetails");
		res.innerHTML = parseScript(liveSearchReq.responseText);
	}
}