function payment(issued){
	if (issued=='UK'){
		if (document.nochexUK.delivery_fullname.value==""){
			alert ("Please enter full name");;
		}
		else if (document.nochexUK.delivery_address.value==""){
			alert ("Please enter delivery address");
		}
		else if (document.nochexUK.delivery_postcode.value==""){
			alert ("Please enter delivery post code");
		}
		else if (document.nochexUK.customer_phone_number.value==""){
			alert ("Please enter contact phone number");
		}
		else {
			alert('You will now be taken to our payment provider Nochex for secure payment by credit/debit card.');
			document.nochexUK.submit();
		}
	}
	else if (issued=='UKnoAddress'){
		alert('You will now be taken to our payment provider Nochex for secure payment by credit/debit card.');
		document.nochexUK.submit();
	}
	else if (issued=='NonUKnoAddress'){
		alert('You will now be taken to our payment provider PayPal for secure payment by credit/debit card.\n\nPlease note that in order to make a payment, you do NOT need to have a PayPal account.\n\n- Simply select payment by credit/debit card.');
		document.nochexNonUK.submit();
	}
	else {
		if (document.nochexNonUK.delivery_fullname.value==""){
			alert ("Please enter full name");;
		}
		else if (document.nochexNonUK.delivery_address.value==""){
			alert ("Please enter delivery address");
		}
		else if (document.nochexNonUK.delivery_postcode.value==""){
			alert ("Please enter delivery post code");
		}
		else if (document.nochexNonUK.customer_phone_number.value==""){
			alert ("Please enter contact phone number");
		}
		else {
			alert('You will now be taken to our payment provider PayPal for secure payment by credit/debit card.\n\nPlease note that in order to make a payment, you do NOT need to have a PayPal account.\n\n- Simply select payment by credit/debit card.');
			document.nochexNonUK.submit();
		}
	}
}	

function updatetotal(deliveryCost, code){
	var thetotal = document.getElementById('total').innerHTML;
	// replace comma... e.g. make 1,200.50 -> 1200.50
	// to fix in future: order 5x£200 of an item and will display £1,000 on top of cart and £1000 at bottom of cart. could be neater...
	thetotal = thetotal.replace(",", "");
	thetotal = thetotal - 0;
	deliveryCost = deliveryCost - 0;
	var countitems = document.form_cart.numberOfItems.value;
	countitems = countitems - 0;
	thetotal = deliveryCost * countitems + thetotal - document.form_cart.delivery_temp.value;
	thetotal = thetotal.toFixed(2);
	document.getElementById('total').innerHTML = thetotal;
	var newdeliverycost = deliveryCost * countitems;
	newdeliverycost = newdeliverycost.toFixed(2);
	document.form_cart.delivery_temp.value = newdeliverycost;
	document.getElementById('deliverycharge').innerHTML = newdeliverycost;
	document.form_cart.delivery_code.value = code;
	document.nochex.delivery_code.value = code;
}

function uncheckRadio() {
 var choice = document.form_cart.delivery;
 for (i = 0; i < choice.length; i++) {
  if ( choice[i].checked = true ) 
   choice[i].checked = false; 
 }
}

function deliveryto(location){
	if (location=='uk'){
		document.getElementById('world').style.display = 'none';
		document.getElementById('nonuk').style.display = 'block';
		document.getElementById('deliverycharge').innerHTML = '0.00';
		document.getElementById('radiouk').checked = true;
		updatetotal('0.00', '0')
	}
	else {
		document.getElementById('world').style.display = 'block';
		document.getElementById('nonuk').style.display = 'none';
		if (location=='eu'){
			document.getElementById('radioeu').checked = true;
			updatetotal('9.95', '1')
		}
		else if (location=='rest'){
			alert('Please contact us to obtain delivery cost before placing an order\n\n(select "Contact us" on the top of the page)');
			uncheckRadio();
		}
		else {
		}
	}
}

function tos(){
	var delivery_val = 99;
	for (var i=0; i < document.form_cart.delivery.length; i++){
		if (document.form_cart.delivery[i].checked){
			delivery_val = document.form_cart.delivery[i].value;
		}
	}
	if (document.tosform){
		if (delivery_val == 99){
	        	alert('Please select delivery type (UK or Non-UK)');
			breakvar = 1;
		}
		else if (document.tosform.copyright.checked==false){
	        	alert('You must agree to the terms and conditions before proceeding to checkout.');
			breakvar = 1;
		}
		else {
			//alert('You will now be taken to our payment provider Nochex for secure payment by credit/debit card.');
			document.nochex.submit();
		}
	}
	else {
		if (delivery_val == 99){
	        	alert('Please select delivery type (UK or Non-UK)');
			breakvar = 1;
		}
		else {
			//alert('You will now be taken to our payment provider Nochex for secure payment by credit/debit card.');
			document.nochex.submit();
		}
	}
}

function aff_tos(){
	if (document.signupform.tos.checked==false){
        	alert('You must agree to the terms and conditions before you can submit the application.');
	}
	else {
		if (aff_ValidateEmail()==false){
			document.signupform.submit();
		}
	}
}

function echeck(str, type) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
			if (type=="paypal"){
				alert("Invalid PayPal E-mail address")
				return false
			}
			else {
				alert("Invalid E-mail address")
				return false
			}
		}
		// @ does not exist OR is 1st character OR is last character
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr - 1){
			if (type=="paypal"){
				alert("Invalid PayPal E-mail address")
				return false
			}
			else {
				alert("Invalid E-mail address")
				return false
			}
		}
		// . does not exist OR is 1st character OR is last character
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr - 1){
			if (type=="paypal"){
				alert("Invalid PayPal E-mail address")
				return false
			}
			else {
				alert("Invalid E-mail address")
				return false
			}
		}
		// more than one @
		 if (str.indexOf(at,(lat+1))!=-1){
			if (type=="paypal"){
				alert("Invalid PayPal E-mail address")
				return false
			}
			else {
				alert("Invalid E-mail address")
				return false
			}
		 }
		// cannot have . just before OR after @
		// is that true? leave out until verified...
		//if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//    alert("Invalid E-mail address")
		//    return false
		//}
		// no . at least two character steps to right of @
		 if (str.indexOf(dot,(lat+2))==-1){
			if (type=="paypal"){
				alert("Invalid PayPal E-mail address")
				return false
			}
			else {
				alert("Invalid E-mail address")
				return false
			}
		 }
		// contains a space
		 if (str.indexOf(" ")!=-1){
			if (type=="paypal"){
				alert("Invalid PayPal E-mail address")
				return false
			}
			else {
				alert("Invalid E-mail address")
				return false
			}
		 }

 		 return true					
	}

function ValidateEmail(inputemail){
	// is undefined if is using es
	if (inputemail == undefined){
		var emailID=document.emailform.email_address;
	}
	else {
		var emailID=inputemail;
	}
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your E-mail address");
		emailID.focus();
		return true;
	}
	if (echeck(emailID.value, '')==false){
		//emailID.value=""
		emailID.focus();
		return true;
	}
	return false;
 }

function aff_ValidateEmail(){
	var emailID=document.signupform.aemail
	var emailIDpp=document.signupform.apaypalemail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your E-mail address")
		emailID.focus()
		return true
	}
	if (echeck(emailID.value, '')==false){
		//emailID.value=""
		emailID.focus()
		return true
	}
	if ((emailIDpp.value==null)||(emailIDpp.value=="")){
		alert("Please enter your PayPal E-mail address")
		emailIDpp.focus()
		return true
	}
	if (echeck(emailIDpp.value, 'paypal')==false){
		//emailIDpp.value=""
		emailIDpp.focus()
		return true
	}
	return false
 }

function urldecode (str) {
    // http://kevin.vanzonneveld.net
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function (search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
    return ret;
}

