//-------------------------------------- General Functions ----------------------------------------------------//

function getObj(nm) {
	if (document.getElementById) {
		return document.getElementById(nm);
	} else if (document.all) {
		return document.all[nm];
	} else {
		return eval("document." + nm);
	}
}

function change_bg(id, color) {
	getObj('prodrow' + id).style.backgroundColor = '#' + color;
}

function checkEmail(email)
{
	email = email.toLowerCase(); 
	if (email.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/)) {
		return true;
	} else {
		return false;
	}
}

//-------------------------------------- Accounts Functions ------------------------------------------------------//

function arena_4_ecommerce_validsignup() {
	valid = 1;
	message = "You have not completed all the required fields, please check:\n";
	formName = document.forms["account_signup"];
	//alert(formName.elements['signup_firstname'].value)
	if (formName.elements['signup_firstname'].value == "") {
		valid = 0;
		message += "First Name\n";
		getObj("signup_firstname").style.border = "#aa0000 solid 1px";
	} else {
		getObj("signup_firstname").style.border = "#A5ACB2 solid 1px";
	}

	if (formName.elements["signup_lastname"].value == "") {
		valid = 0;
		message += "Last Name\n";
		getObj("signup_lastname").style.border = "#aa0000 solid 1px";
	} else {
		getObj("signup_lastname").style.border = "#A5ACB2 solid 1px";
	}
	
	if (checkEmail(formName.elements["signup_email"].value) == false) {
		valid = 0;
		message += "E-mail Address\n";
		getObj("signup_email").style.border = "#aa0000 solid 1px";
	} else {
		getObj("signup_email").style.border = "#A5ACB2 solid 1px";
	}

	if (formName.elements["signup_password"].value == "") {
		valid = 0;
		message += "Password\n";
		getObj("signup_password").style.border = "#aa0000 solid 1px";
	} else {
		getObj("signup_password").style.border = "#A5ACB2 solid 1px";
	}

	if (formName.elements["signup_password_confirm"].value == "") {
		valid = 0;
		message += "Password Confirmation\n";
		getObj("signup_password_confirm").style.border = "#aa0000 solid 1px";
	} else {
		getObj("signup_password_confirm").style.border = "#A5ACB2 solid 1px";
	}
	
	if (formName.elements["signup_password_confirm"].value != formName.elements["signup_password"].value) {
		valid = 0;
		message += "Passwords do not Match\n";
	}
	
	if (valid == 0) {
		alert(message);
	} else {
		formName.submit();
	}
}
//------------------------------------------------- Account Details -------------------------------------------------------//

function arena_4_ecommerce_validaccsave() {
valid = 1;
	message = "You have not completed all the required fields, please check:\n";
	formName = document.forms["update_accdetails"];
	//alert(formName.elements['signup_firstname'].value)
	if (formName.elements['signup_firstname'].value == "") {
		valid = 0;
		message += "First Name\n";
		getObj("signup_firstname").style.border = "#FF0000 solid 1px";
	} else {
		getObj("signup_firstname").style.border = "#A5ACB2 solid 1px";
	}

	if (formName.elements["signup_lastname"].value == "") {
		valid = 0;
		message += "Last Name\n";
		getObj("signup_lastname").style.border = "#FF0000 solid 1px";
	} else {
		getObj("signup_lastname").style.border = "#A5ACB2 solid 1px";
	}
	
	if (formName.elements["signup_email"].value == "") {
		valid = 0;
		message += "E-mail Address\n";
		getObj("signup_email").style.border = "#FF0000 solid 1px";
	} else {
		getObj("signup_email").style.border = "#A5ACB2 solid 1px";
	}

	if (formName.elements["signup_password"].value == "") {
		valid = 0;
		message += "Password\n";
		getObj("signup_password").style.border = "#FF0000 solid 1px";
	} else {
		getObj("signup_password").style.border = "#A5ACB2 solid 1px";
	}

	if (formName.elements["signup_password_confirm"].value == "") {
		valid = 0;
		message += "Password Confirmation\n";
		getObj("signup_password_confirm").style.border = "#FF0000 solid 1px";
	} else {
		getObj("signup_password_confirm").style.border = "#A5ACB2 solid 1px";
	}
	
	if (formName.elements["signup_password_confirm"].value != formName.elements["signup_password"].value) {
		valid = 0;
		message += "Passwords do not Match\n";
	}
	
	if (valid == 0) {
		alert(message);
	} else {
		formName.submit();
	}
}


function arena_4_ecommerce_validaccaddress() {
valid = 1;
	message = "You have not completed all the required fields, please check:\n";
	formName = document.forms["accountaddress_details"];

	if (TrimString(formName.elements['billing_address1'].value) == "") {
		valid = 0;
		message += "Line 1\n";
		getObj("billing_address1").style.border = "#FF0000 solid 1px";
	} else {
		getObj("billing_address1").style.border = "#A5ACB2 solid 1px";
	}
	
	if (TrimString(formName.elements['billing_address2'].value) == "") {
		valid = 0;
		message += "Line 2\n";
		getObj("billing_address2").style.border = "#FF0000 solid 1px";
	} else {
		getObj("billing_address2").style.border = "#A5ACB2 solid 1px";
	}
	
	//if (formName.elements['billing_address3'].value == "") {
	//	valid = 0;
	//	message += "Billing Line 3\n";
	//	getObj("billing_address3").style.border = "#FF0000 solid 1px";
	//} else {
	//	getObj("billing_address3").style.border = "#A5ACB2 solid 1px";
	//}
	
	if (TrimString(formName.elements['billing_postcode'].value) == "") {
		valid = 0;
		message += "Postcode\n";
		getObj("billing_postcode").style.border = "#FF0000 solid 1px";
	} else {
		getObj("billing_postcode").style.border = "#A5ACB2 solid 1px";
	}
	
	if (TrimString(formName.elements['billing_town'].value) == "") {
		valid = 0;
		message += "Town\n";
		getObj("billing_town").style.border = "#FF0000 solid 1px";
	} else {
		getObj("billing_town").style.border = "#A5ACB2 solid 1px";
	}
	
	if (TrimString(formName.elements['billing_country'].value) == "") {
		valid = 0;
		message += "Country\n";
		getObj("billing_country").style.color = "#FF0000";
	} else {
		getObj("billing_country").style.color = "#000000";
	}
	
		
	if (formName.elements['day_telephone'].value == "") {
		valid = 0;
		message += "Daytime Telephone\n";
		getObj("day_telephone").style.border = "#aa0000 solid 1px";
	} else {
		getObj("day_telephone").style.border = "#A5ACB2 solid 1px";
	}
	
	if (valid == 0) {
		alert(message);
	} else {
		formName.submit();
	}
	
}

function arena_4_ecommerce_validaccaddresssave() {
valid = 1;
	message = "You have not completed all the required fields, please check:\n";
	formName = document.forms["accountaddress_details"];

	if (formName.elements['billing_address1'].value == "") {
		valid = 0;
		message += "Billing Line 1\n";
		getObj("billing_address1").style.border = "#aa0000 solid 1px";
	} else {
		getObj("billing_address1").style.border = "#A5ACB2 solid 1px";
	}
	
	if (formName.elements['billing_address2'].value == "") {
		valid = 0;
		message += "Billing Line 2\n";
		getObj("billing_address2").style.border = "#aa0000 solid 1px";
	} else {
		getObj("billing_address2").style.border = "#A5ACB2 solid 1px";
	}
	
//	if (formName.elements['billing_address3'].value == "") {
	//	valid = 0;
	//	message += "Billing Line 3\n";
	//	getObj("billing_address3").style.border = "#aa0000 solid 1px";
	//} else {
	//	getObj("billing_address3").style.border = "#A5ACB2 solid 1px";
	//}
	
	if (formName.elements['billing_postcode'].value == "") {
		valid = 0;
		message += "Billing Postcode\n";
		getObj("billing_postcode").style.border = "#aa0000 solid 1px";
	} else {
		getObj("billing_postcode").style.border = "#A5ACB2 solid 1px";
	}
	
	
	if (formName.elements['billing_town'].value == "") {
		valid = 0;
		message += "Billing Town\n";
		getObj("billing_town").style.border = "#aa0000 solid 1px";
	} else {
		getObj("billing_town").style.border = "#A5ACB2 solid 1px";
	}
	
	if (formName.elements['billing_country'].value == "") {
		valid = 0;
		message += "Billing Country\n";
		getObj("billing_country").style.color = "#aa0000";
	} else {
		getObj("billing_country").style.color = "#000000";
	}
	
	if (formName.elements['day_telephone'].value == "") {
		valid = 0;
		message += "Daytime Telephone\n";
		getObj("day_telephone").style.border = "#aa0000 solid 1px";
	} else {
		getObj("day_telephone").style.border = "#A5ACB2 solid 1px";
	}

	if (valid == 0) {
		alert(message);
	} else {
		formName.submit();
	}
	
}


//-----------------------------------------------------Checkout Postage functions -----------------------------------------//

function setzero_postage (line_id, cart_lines) {
	var postage_amount = document.forms["cartline" + line_id].item_postage_value.value;
	var delivery_type = document.forms["cartline" + line_id].products_deliver.value;
	var actual_postage = document.forms["cartline" + line_id].item_actual_postage_value.value;
	//alert(delivery_type)
	
	if (delivery_type == "1") {
		document.forms["cartline" + line_id].item_postage_value.value = actual_postage;
	} else {
		document.forms["cartline" + line_id].item_postage_value.value="0.00";
		document.forms["cartline" + line_id].item_postage_surcharge.value="0.00";
	}
	var calcpostage = calculate_postage(cart_lines, 0);
}
//
function calculate_postage(cart_lines, zonepercent) {
//
var thispostage = 0.00;
var lastpostage = 0.00;
var maxpostage = 0.00;
var surcharge = 0.00;
var totalpostage = 0.00;
var maxprodsurcharge = 0.00;
	for (var i=1; i<=cart_lines; i++) {
		var thispostage = document.forms["cartline" + i].item_postage_value.value;
		var delivery_type = document.forms["cartline" + i].products_deliver.value;
		var surcharge = document.forms["cartline" + i].item_surcharge.value;
		var quantity = parseInt(document.forms["cartline" + i].products_Quantity.value);
		if (delivery_type == "1") {
			document.forms["cartline" + i].item_postage_value.value = document.forms["cartline" + i].item_actual_postage_value.value
		}
		//
		//alert(surcharge)
		if (thispostage > lastpostage) {
			maxpostage = thispostage;
			maxprodsurcharge = surcharge;
		}
		lastpostage = thispostage;
		if (surcharge != 0.00) {
			var surchargetotal = (surcharge * quantity) - surcharge;
			var totalpostage = parseFloat(surchargetotal) + parseFloat(maxpostage);
			//alert(surchargetotal)
		} else {
			var totalpostage = maxpostage
		}
	}
	
document.forms["master_form"].order_delivery_cost.value = totalpostage;
//alert(totalpostage)
}
//
//
function verify_deliveryoptions(cart_lines) {
	//
	var checkpostageset = calculate_postage(cart_lines, 0);
	//
	var overall_valid = "true";
	var valid = 0;
	var total_true = cart_lines;
	for (var i=1; i<=cart_lines; i++) {
		var delivery_selected = document.forms["cartline" + i].products_deliver.value;
		//
		if (delivery_selected != "") {
			document.forms["cartline" + i].products_deliver.value = delivery_selected;
			document.forms["cartline" + i].products_deliver.style.color = "#000000";
			var valid = valid + 1;
		} else {
			var valid = valid + 0;
			document.forms["cartline" + i].products_deliver.style.color = "#aa0000";
		}
		
	}
	//alert(valid)
		if (valid != cart_lines) {
			alert("You have not declared all your delivery/collection options");
		} else {
			var max_postage = calculate_postage(cart_lines, 0);
			
			//getObj("delivery_cost_display").style.display = "block"
			var message = "Your total delivery charge is £" + document.forms["master_form"].order_delivery_cost.value + " If you wish to accept this and continue the checkout process please click OK, if you wish to alter your delivery/collection options please click Cancel"
			//alert(message)
			//
			if (document.forms["master_form"].order_delivery_cost.value == "0") {
				var message = "All items have been set to collection OR their is no delivery charge for your selected item(s). If you wish to accept this and continue the checkout process please click OK, if you wish to cancel this process please click Cancel"
			}
			//
			if (confirm(message)) {
			//
			var completecheck = complete_checkout(cart_lines);
			}
		}
}
//
//
function complete_checkout(cart_lines) {
//
	var order_id = document.forms["cartline1"].order_id.value;
	var maximum_delivery = document.forms["master_form"].order_delivery_cost.value
	//var staff_repcode = document.forms["master_form"].staff_repcode.value
	//
	if (maximum_delivery == "") {
		var maximum_delivery = 0.00
	}
	//
	var redirect = "checkout_saveoptions.asp?orderid=" + order_id + "&state_sessionid=" + document.forms["master_form"].state_sessionid.value + "&maxdel=" + maximum_delivery
	//
	for (var i=1; i<=cart_lines; i++) {
	var delivery_selected = document.forms["cartline" + i].products_deliver.value;
	//
	var redirect = redirect + "&l=" + i + "_" + delivery_selected
	}
	document.location.href = redirect
//
}
//
//---------------------------------------------------------- Various ------------------------------------------------------//

function enlarge_photo(photo_id, productname, catid) {
	var newurl1 = "enlarge.asp?photo="+ photo_id + "&pname="+ productname + "&cid=" + catid;
	var photopage=window.open(newurl1, "Enlarged_Photo", "width=600,height=500,locationbar=0,menubar=0,personalbar=0,status=0,scrollbars=0,resizable=0");
}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function create_notify(product_id, acc_id, catid) {
	var notifypage=window.open("create_notify.asp?product_id=" + product_id + "&acc=" + acc_id + "&categories_id=" + catid, "Product_Notification", "width=500,height=350,locationbar=0,menubar=0,personalbar=0,status=0,scrollbars=0,resizable=0");
}

function check_notify() {
	valid = 1;
	message = "You have not completed all the required fields, please check:\n";
	formName = document.forms['create_notify'];
	//
	if (formName.elements["customer_name"].value == "") {
		valid = 0;
		message += "Name\n";
		getObj("customer_name").style.border = "#aa0000 solid 1px";
	} else {
		getObj("customer_name").style.border = "#A5ACB2 solid 1px";
	}
	
	if (formName.elements["customer_email"].value == "") {
		valid = 0;
		message += "Email\n";
		getObj("customer_email").style.border = "#aa0000 solid 1px";
	} else {
			if (checkEmail(formName.elements["customer_email"].value) == false) {
			valid = 0;
			message += "The e-mail address isn't valid\n";
			getObj("customer_email").style.border = "#aa0000 solid 1px";
		} else {
			getObj("customer_email").style.border = "#A5ACB2 solid 1px";
		}
	}
	
	if (valid == 0) {
		alert(message);
	} else {
		formName.submit();
	}
}


function arena_4_ecommerce_validcompetitionform() {
	valid = 1;
	message = "You have not completed all the required fields, please check:\n";
	formName = document.forms['competitionentry'];

	if (formName.elements["answer"].value == "") {
		valid = 0;
		message += "Answer\n";
		getObj("answer").style.border = "#aa0000 solid 1px";
	} else {
		getObj("answer").style.border = "#A5ACB2 solid 1px";
	}

	if (formName.elements["email_addy"].value == "") {
		valid = 0;
		message += "Email Address\n";
		getObj("email_addy").style.border = "#aa0000 solid 1px";
	} else {
		if (checkEmail(formName.elements["email_addy"].value) == false) {
			valid = 0;
			message += "The e-mail address isn't valid\n";
			getObj("email_addy").style.border = "#aa0000 solid 1px";
		} else {
			getObj("email_addy").style.border = "#A5ACB2 solid 1px";
		}
	}

	if (formName.elements["name"].value == "") {
		valid = 0;
		message += "Name\n";
		getObj("name").style.border = "#aa0000 solid 1px";
	} else {
		getObj("name").style.border = "#A5ACB2 solid 1px";
	}
	
	if (valid == 0) {
		alert(message);
	} else {
		formName.submit();
	}
}