
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,gnm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { gnm=args[i+1]; test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") { 
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+gnm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+gnm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num)
			  errors+='- '+gnm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+gnm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function show_form_info() {
	document.getElementById('display_none').style.display='block';
}

function show_address() {
	document.getElementById('address_select').style.display='block';
    document.getElementById('account_select').style.display='none';
}

function hide_address() {
	document.getElementById('address_select').style.display='none';
    document.getElementById('account_select').style.display='block';
}
function selectAddress (tg) {
	if (tg == 1) {
		document.getElementById('display_account_address').className = ""
		document.getElementById('no_specify_address').style.display = 'block';
		document.getElementById('specify_address').style.display = 'none';
	}
	else {
		document.getElementById('display_account_address').className = "deselect_this"
		document.getElementById('no_specify_address').style.display = 'none';
		document.getElementById('specify_address').style.display = 'block';
	}
}

function selectBuyer(pass) {
		var togglethis = document.getElementById(pass);
		var togglethisp = document.getElementById("p_" + pass);

		if (togglethis.value == "business") {
			togglethisp.className = "selectradio";
			document.getElementById("p_radio_personal").className = "";
			document.getElementById("company_select").style.display = "block";
		}
		else {
			togglethisp.className = "selectradio";
			document.getElementById("p_radio_business").className = "";
			document.getElementById("company_select").style.display = "none";
			document.getElementById("company").value = "";
		}
	}
	function amIcheckd(myval) {
		var checkThis = document.getElementById(myval);
		if (checkThis.checked == true) {
			document.getElementById("company_select_req").style.display = "block";
		}
		else {
			document.getElementById("company_select_req").style.display = "none";
			document.getElementById("vatnumber").value = "";
			
		}
	}
	
	function check13B(tickbox_id, number_id)
	{
		var tickbox = document.getElementById(tickbox_id);
		var number = document.getElementById(number_id);
				
		if (tickbox.checked == false)
			return true;
		else if (number.value == '')
		{
			alert('Please ensure you have supplied a 13B number.');
			return false;
		}
		
		return true;
	}
	
	/*Run this function when the page loads*/
$(document).ready(function(){
	
	/********************************************************************************************************/
	/**************************************Paypal Telephone Number Popup*************************************/
	/********************************************************************************************************/
	$(".telephone_input").attr("value","")
	$(".paypal_popup_tel_error_message").remove();

	$("form[name=paypal] input").click(function(){

		$(".telephone_input").attr("value","")
		$(".paypal_popup_tel_error_message").remove();
			
		$("#paypal_telephone_container:hidden").fadeIn(100,function(){$(".telephone_input").focus()});
		
		$("#paypal_telephone_submit_form").submit(function(){
			$(".paypal_popup_tel_error_message").remove();
			var user_tel_input = $(".telephone_input").attr("value");
			var user_tel_input_trimmed = user_tel_input.replace(/\s/g,"");
			
			if (isNaN(user_tel_input_trimmed) || user_tel_input_trimmed.length < 4) {
				$("<strong class=\"paypal_popup_tel_error_message\">Please enter a valid phone number</strong>").appendTo(".paypal_telephone_input_container");					
				$(".telephone_input").focus()
				user_tel_input = null;
				user_tel_input_trimmed = null;
				return false;
			} 
			else {
				$(".paypal_popup_tel_error_message").remove();
				return true;
			}
		});

		$("#exit_tel_form_button").click(function(){
			$("#paypal_telephone_container").fadeOut(100,function(){
				$(".telephone_input").attr("value","")
				$(".paypal_popup_tel_error_message").remove();
			});	
			return false;
		});
		return false;
	})
	
	/********************************************************************************************************/
	/**********************************************Basket update*********************************************/
	/********************************************************************************************************/
	/*When an "Add to basket", "Add", or recently viewed "Buy now" button is clicked*/
	$(".buy").click(function() {
		/*Create an array of all the elements in the form and set it to all the fields in the form*/
		var formElements = $(this).parents("form:first").serialize() + "&formProductID=" + $(this).attr("name");
		/*Store the id of the submit button to show the "Basket updated!" message under it on the product list pages*/
		var productUpdated = $(this).attr("name");
		
		var currentURL = window.location.pathname;
		var currentPage = currentURL.substring(currentURL.lastIndexOf('/') + 1).toLowerCase();
	
		if(currentPage !== "viewbasket.asp") {
			/*Call the update basket include page to update with the values entered (formElements)*/
			$.ajax({ type: "POST", url: "includes/updateBasket.asp", data: formElements, success: function(basketContents) {  
				/*Update the basket section in the header with the updated basket contents*/
				$("#compactBasketContainer").html(basketContents);
				/*Call compactBasketScrollPosition to position the header basket popup*/
				compactBasketScrollPosition();
				/*Call compactBasketItemDisplay to only show the 4 newest items added to the basket*/
				compactBasketItemDisplay();
				/*Show the basket popup then hide it after 3 seconds*/
				$("#compactBasketPopup").slideDown().delay(3000).slideUp();
				/*When the mouse cursor entered and leaves the header basket container. Hover intent is used to provide a delay ensuring that the popup is not shown if the user quickly runs the cursor over the container*/
				$("#compactBasketContainer").hover(
					function () {
						/*When the basket container is hovered*/
						/*Call compactBasketScrollPosition to position the header basket popup*/
						compactBasketScrollPosition();
						/*Show the basket popup*/
						$("#compactBasketPopup").stop(true, true).slideDown();
					},
					function () {
						/*When the basket container is not hovered*/
						/*Hide the basket popup*/
						$("#compactBasketPopup").slideUp();
					}
				);
				
				/*Reset the quantity box to 1*/
				$("#REF" + productUpdated).val(1);
			}}); 
			/*Return false to stop the button from submitting the form*/
			return false;
		}
	});
	
	/********************************************************************************************************/
	/*******************************************Header basket popup******************************************/
	/********************************************************************************************************/
	/*When the mouse cursor entered and leaves the header basket container. Hover intent is used to provide a delay ensuring that the popup is not shown if the user quickly runs the cursor over the container*/
	$("#basketContainer").hover(
  		function () {
			/*When the basket container is hovered*/
			/*Call compactBasketScrollPosition to position the header basket popup*/
			compactBasketScrollPosition();
			/*Show the basket popup*/
			$("#compactBasketPopup").stop(true, true).slideDown();
  		},
  		function () {
			/*When the basket container is not hovered*/
			/*Hide the basket popup*/
			$("#compactBasketPopup").slideUp();
  		}
	);
	
	/********************************************************************************************************/
	/******************************************Basket scroll position****************************************/
	/********************************************************************************************************/
	/*Ensures that the basket popup is displayed at the top of the screen if the basket button is not in view and under it if it is*/
	function compactBasketScrollPosition() {
		/*If the position of the popup is set to fixed then the browser supports fixed positioning (not IE6)*/
		if ($("#compactBasketPopup").css("position") == "fixed") {
			/*The position of the bottom of the basket button (the top position of the element plus its height)*/
			var basketContainerPosition = $("#compactBasketContainer").offset().top + $("#compactBasketContainer").height() + $("#compactBasketCheckoutButton").height();
			/*If the basket container is not on screen*/
			if ($(window).scrollTop() > basketContainerPosition) {
				/*Set the popup to be at the top of the window*/
				$("#compactBasketPopup").css("top", "0");
			/*If the basket container is on screen*/
			} else {
				/*Ensure the popup is positioned below the basket container*/
				$("#compactBasketPopup").css("top", basketContainerPosition - $(window).scrollTop());
			}
			/*The position of the basket when scrolled*/
			$(window).scroll(function () { 
				/*If the basket container is not on screen*/
				if ($(window).scrollTop() > basketContainerPosition) {
					/*Set the popup to be at the top of the window*/
					$("#compactBasketPopup").css("top", "0");
				/*If the basket container is on screen*/
				} else {
					/*Ensure the popup is positioned below the basket container*/
					$("#compactBasketPopup").css("top", basketContainerPosition - $(window).scrollTop());
				}
			});
		}
	}	
	/*Call compactBasketItemDisplay when the page loads to only show the 4 newest items added to the basket*/
	compactBasketItemDisplay();
	
	/********************************************************************************************************/
	/*******************************************Basket item display******************************************/
	/********************************************************************************************************/	
	/*If more than 4 items are in the basket this will ensure that only the latest items added are shown*/
	function compactBasketItemDisplay() {
		/*Find out how many list items are in compactBasketPopupList*/
		var compactBasketPopupItemTotal = $("#compactBasketPopupList .compactBasketPopupListItem").size();	
	
		/*If the total number of items in the basket is greater than 4*/
		if (compactBasketPopupItemTotal > 4) {
			/*Show the last 4 products added, hides all list items before the fourth one*/
			$(".compactBasketPopupListItem:lt(" + (compactBasketPopupItemTotal - 4) + ")").hide();
		}
	}	
	
	/********************************************************************************************************/
	/******************************************Printer wizard update*****************************************/
	/********************************************************************************************************/	
	function updatePrinterWizard(selectName) {
		$("select[name='"+selectName+"']").removeAttr("onchange");
		//When the printer wizard select boxes are changed
		$("select[name='"+selectName+"']").change(function() {
			/*Create an array of all the elements in the form*/
			var formElements = formElements = $(this).parents("form:first").serialize();
	
			/*Call the printer wizard include page to update with the values entered (formElements)*/
			$.ajax({ type: "POST", url: "includes/printerWizardUpdate.asp", data: formElements, success: function(printerWizardContents) {  
				var $holder = $("<div/>").html(printerWizardContents);
				if ($("select[name='group_selection']").attr("disabled") == true) {
					$("#printerWizardFamilyItem").hide().html($("#printerWizardFamilyItem", $holder).html()).fadeIn("slow");
				} else {
					$("#printerWizardFamilyItem").html($("#printerWizardFamilyItem", $holder).html());
				}
				$("#printerWizardModelItem").hide().html($("#printerWizardModelItem", $holder).html()).fadeIn("slow");
				$("select[name='group_selection'], select[name='printer_selection']").uniform(); 
				$("select[name='group_selection']").removeAttr("onchange");
				updatePrinterWizard("group_selection");
			}}); 
		});
	}
	updatePrinterWizard("manu_selection");
});
