// JavaScript Document


//header scripts
function menu_over(button) {
	button.style.backgroundPosition="0px -31px";
	}
function menu_out(button) {
	button.style.backgroundPosition="0px 0px";
	}
function button_over(obj, move) {
	obj.style.backgroundPosition=move;
	}
function button_out(obj) {
	obj.style.backgroundPosition="0px 0px";
	}
function button_width(width){
	menu_button.style.width=width;
	}
function show_menu(obj_name){
	obj_name.style.visibility = 'visible';
	}
function close_menu(obj_name) {
	obj_name.style.visibility = 'hidden';
	}
function menu_right(){
	drop_menu7.style.left = "-50px";
	}
function banner_link(){
	header_banner.style.cursor = 'pointer';
	}

/*		
function show_sub_drop(menu_obj, drop_pos) {
	//menu_odj is the ID of the catagory
	//drop_pos is where the menu item is in the list i.e 5th place down

	//close the current menu if there is one open
	if(selected_menu != "") {
		var menu_item = document.getElementById(selected_menu);
		menu_item.style.backgroundColor = "";
	}
	if(open_sub != "") {
		var sub_drop = document.getElementById(open_sub);
		sub_drop.style.visibility = 'hidden';
	}
	
	//set variables
	var position = 19;
	var sub_drop = document.getElementById("sub_drop"+menu_obj);
	var drop_link = document.getElementById("drop_link"+drop_pos);

	drop_link.style.backgroundColor = '#CECEFF';

	position = position * (drop_pos);
	open_sub = "sub_drop"+menu_obj;
	selected_menu = "drop_link"+drop_pos;

	sub_drop.style.top = position;
	sub_drop.style.visibility = "visible";
	
}

*/




//add / remove qty 
function add_search(qty_box){
	qty_box.value++;
	}
function minus_search(qty_box){
	if (qty_box.value < 2) {
		qty_box.value = 1;
	} else {
	qty_box.value--;
	}
	}
	
function add_basket(qty){
	qty.value++;
	//qty_update.submit();
	}
function minus_basket(qty){
	if (qty.value < 2) {
		qty.value = 1;
		//qty_update.submit();
	} else {
	qty.value--;
	//qty_update.submit();
	}
	}


//image opening
function open_image(src) {
	image_src = 'product_images/'+src;
	window.open(image_src, 'Image', 'width=510,height=510,location=no');
}


//validate new account form
function validate_form() {
	var f_name = document.new_acc.f_name.value;
	var l_name = document.new_acc.l_name.value;
	var email = document.new_acc.email.value;
	var f_pass = document.new_acc.f_pass.value;
	var s_pass = document.new_acc.s_pass.value;

	if(f_name=="" || l_name=="") {
		alert("Please enter your first name and last name.");
		return false;
	}
	
	if(email=="") {
		alert("Please enter your email address.");
		return false;
	}
	
	if (email != "") {
   		var x = email;
   		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  		if (!filter.test(x)) {
			alert("Please enter a valid email address");
			return false;
		}
	}
	
	if(f_pass=="" || s_pass=="") {
		alert("Please fill out both password fields");
		return false;
	}
	
	if(f_pass != s_pass) {
		alert("Your password confirmation does not match your password. Please re-enter your password");
		return false;
	}
	return true;
}


//submit new account form
function submit_form() {
	if(validate_form()) {
		document.new_acc.submit();
	}
}


//submit login
function submit_login() {
	document.login_form.submit();
}


//validate new address
function validate_address() {
	var add_name = document.add_addr.addr_name.value;
	var line = document.add_addr.line1.value;
	var county = document.add_addr.county.value;
	var postcode = document.add_addr.postcode.value;
	
	if(add_name == "" || line == "" || county == "" || postcode == "") {
		alert('Please fill out all mandatory fields');
		return false
	} else {
		return true;
	}
}


//submit new_addr
function submit_new_addr() {
	if(validate_address()) {
		document.add_addr.submit();
	}
}


//validate edit address
function validate_edit() {
	var add_name = document.edit_addr.addr_name.value;
	var line = document.edit_addr.line1.value;
	var county = document.edit_addr.county.value;
	var postcode = document.edit_addr.postcode.value;
	
	if(add_name == "" || line == "" || county == "" || postcode == "") {
		alert('Please fill out all mandatory fields');
		return false;
	} else {
		return true;
	}
}


//submit edit_addr
function submit_edit_addr(){
	if(validate_edit()) {
		document.edit_addr.submit();
	}
}


//validate details change
function validate_details(){
	var f_name = document.details_edit.f_name.value;
	var l_name = document.details_edit.l_name.value;
	var email = document.details_edit.email.value;
	
	if(f_name == "" || l_name == "" || email == "") {
		alert('Please fill out all mandatory fields');
		return false;
	}
	
	if (email != "") {
   		var x = email;
   		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  		if (!filter.test(x)) {
			alert("Please enter a valid email address");
			return false;
		}
	}
	return true;
}


//submit details_edit
function submit_details_edit() {
	if(validate_details()) {
		document.details_edit.submit();
	}
}


//validate payment details form
function validate_payment_details() {
	var addr_1 = document.payment_form.line1.value;
	var county = document.payment_form.county.value;
	var postcode = document.payment_form.postcode.value;
	var card_type = document.payment_form.card_type.value;
	var card_name = document.payment_form.card_name.value;
	var card_number = document.payment_form.card_number.value;
	var exp_month = document.payment_form.exp_month.value;
	var exp_year = document.payment_form.exp_year.value;
	var cv2 = document.payment_form.cv2.value;
	var check_date = new Date();
	
	if(addr_1 == "" || county == "" || postcode == "" || card_type == "" || card_name == "" || card_number == "" || cv2 == "") {
		alert("Please ensure all mandatory fields are completed");
		return false;
	}
	
	if(isNaN(card_number)) {
		alert("Please enter only numbers in the card number field");
		return false;
	}
	
	if(card_number.length < 12 || card_number.length > 20) {
		alert("Your card number must be between 12 and 20 digits long");
		return false;
	}
	
	if(exp_year == check_date.getFullYear()) {
		if((exp_month-1) < check_date.getMonth()) {
			alert("Your card has expired. Please use another card");
			return false;
		}
	}
	
	if(isNaN(cv2)) {
		alert("Please enter only numbers in the CV2 field");
		return false;
	}
	
	if(cv2.length < 3 || cv2.length > 4) {
		alert("Your CV2 number should be 3 or 4 digits long");
		return false;
	}

	return true;
}
	

//submit payment form
function submit_payment() {
	if(validate_payment_details()) {
		document.payment_form.submit();
	}
}


//submit reference form
function submit_ref() {
	document.cust_ref.submit();
}


//submit delivery address form
function submit_del() {
	document.delivery_address.submit();
}


//page number hilight
function page_number(obj) {
	obj.style.backgroundColor = '#E6E6FF';
	obj.style.border = '1px solid #999999';
}


//reset page number highlight
function page_number_out(obj) {
	obj.style.backgroundColor = '#FFFFFF';
	obj.style.border = '1px solid #FFFFFF';
}


//check if the basket is empty
function basket_check(total) {
	if(total == 0.00) {
		alert("Your basket is empty. Please add items to your basket before procceeding to the checkout.");
	} else {
		window.location = "https://www.carspares.co.uk/checkout.php?page=del";
	}
}

//check if the user wants to empty their basket
function empty_confirm() {
	var result = confirm("Are you sure you want to remove all items from your basket?");
	if(result) {
		redirect('basket.php?action=empty');
	}
}

//open new window function
function createWindow(cUrl,cName,cFeatures) {
var xWin = window.open(cUrl,cName,cFeatures)
}


//validate enquiry form
function validate_enquiry() {
	var your_name = document.getElementById("your_name").value;
	var email = document.getElementById("email").value;
	var reg = document.getElementById("reg").value;
	var details = document.getElementById("details").value;
	var enquiry = document.getElementById("enquiry").value;
	
	if(your_name == "" || email == "" || enquiry == "") {
		alert("Please fill out all mandatory fields");
		return false;
	}
	
	if (email != "") {
   		var x = email;
   		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  		if (!filter.test(x)) {
			alert("Please enter a valid email address");
			return false;
		}
	}
	return true;
}


//send enquiry
function send_enquiry(){
	if(validate_enquiry()) {
		document.enquiry_form.submit();
		alert('Thank you. Your enquiry has been sent and is being delt with.');
	}
}


function set_billing() {
	if(document.getElementById("billing_set").checked == true) {
		
		var get_address = document.getElementById("billing_set").value;
		var postcode = document.getElementById("set_postcode").value;
		var billing_address = get_address.split("^");
		
		switch (billing_address.length) {
			case 2:
				document.getElementById("line1").value = billing_address[0];
				document.getElementById("county").value = billing_address[1];
				document.getElementById("postcode").value = postcode;
			break;
			
			case 3:
				document.getElementById("line1").value = billing_address[0];
				document.getElementById("line2").value = billing_address[1];
				document.getElementById("county").value = billing_address[2];
				document.getElementById("postcode").value = postcode;
			break;
			
			case 4:
				document.getElementById("line1").value = billing_address[0];
				document.getElementById("line2").value = billing_address[1];
				document.getElementById("line3").value = billing_address[2];
				document.getElementById("county").value = billing_address[3];
				document.getElementById("postcode").value = postcode;
			break;
		}
	} else {
			document.getElementById("line1").value = "";
			document.getElementById("line2").value = "";
			document.getElementById("line3").value = "";
			document.getElementById("county").value = "";
			document.getElementById("postcode").value = "";	
	}
}



function redirect(url){
	if(url != "") {
		window.location = url;
	}
}


function check_return() {
	var reason = document.getElementById("reason").value;
	var comments = document.getElementById("comments").value;

	if(reason == "select") {
		alert("Please select a reason for returning this item");
		
	} else if(reason == "faulty" && comments == "") {
			alert("Please specify a brief description of the fault");
	} else {
		document.returns.submit();
	}
}