// THIS FUNCTION IS REQUIRED BY WINDOW POPUP FUNCTION BELOW
function pop(url) {
	newwindow=window.open(url,'name','height=350,width=350');
	return false; }

// OPENS LINKS WITH CLASS AS 'popup' IN NEW WINDOW
function popups() {
	var x = document.getElementsByTagName('a');
	for (var i=0;i<x.length;i++) {
		if (x[i].className == 'popup') {
			x[i].onclick = function () {
				return pop(this.href); }
			x[i].title += ' (Opens in new window)'; } } }

// AUTO-SELECTS QTY INPUTS
function autoSelect() {
	var y = document.getElementsByTagName('input');
	for (var i=0;i<y.length;i++) {
		if (y[i].className == 'qty') {
			y[i].onfocus = function () {
				this.select(); } } } }

function closePopup() {
	if (document.getElementById('back_to_products')) {
		var z=document.getElementById('back_to_products');
		z.onclick=function() {
			window.close();
			} } }

// UPDATE DELIVERY ADDRESS
// IF UPDATE DELIVERY BUTTON EXISTS ADD THE BUTTON
function addDelButton() {
	if (document.getElementById('del_button')) {
		document.getElementById('del_button').innerHTML='<label for="same_delivery" id="same_delivery_label">This is also my delivery address</label><input type="checkbox" value="1" id="same_delivery" name="same_delivery" class="check" />';
		} }
// FUNCTION TO UPDATE DELIVERY DETAILS
function updateDelAddress() {
	if ((document.getElementById('same_delivery_label'))&&(document.getElementById('same_delivery'))) {
		var m=document.getElementById('same_delivery');
		var n=document.getElementById('same_delivery_label');
		updateDel=function() {
			if (document.getElementById('same_delivery').checked==true) {
				document.getElementById('deliveryaddress').value=document.getElementById('address').value;
				document.getElementById('deliveryaddress2').value=document.getElementById('address2').value;
				document.getElementById('deliveryaddress3').value=document.getElementById('address3').value;
				document.getElementById('deliverycity').value=document.getElementById('city').value;
				document.getElementById('deliverypostcode').value=document.getElementById('postcode').value; } }
		m.onchange=updateDel;
		m.onclick=updateDel;
		n.onchange=updateDel;
		n.onclick=updateDel;
		} }

// SEARCH FORM FUNCTIONS
function searchForm() {
	// CHECK SEARCH BOX EXISTS
	if (document.getElementById('searchtext')) {
		// INITIAL SEARCH VALUE
		var initialSearch='Type what you need in here';
		// RETRIEVE START SEARCH VALUE
		var defaultSearch=document.getElementById('searchtext').value;
		// RETRIEVE SEARCH TYPE
		var defaultType=document.getElementById('searchtype').value;
		// ON FOCUS - REMOVE START TEXT
		document.getElementById('searchtext').onfocus=function() {
			if (document.getElementById('searchtext').value==initialSearch) {
				document.getElementById('searchtext').value='';
				return false; } } 
		// REPLACE START TEXT ON BLUR IF BLANK
		document.getElementById('searchtext').onblur=function() {
			if (document.getElementById('searchtext').value=='') {
				document.getElementById('searchtext').value=defaultSearch;
				return false; } }
		// DON'T ALLOW SEARCH FORM TO BE SUBMITTED IF NO VALUE ENTERED OR (DEFAULT VALUE ENTERED AND TYPE NOT CHANGED)
		if (document.getElementById('searchform')) {
			document.getElementById('searchform').onsubmit=function() {
				if (((document.getElementById('searchtext').value==defaultSearch)&&(document.getElementById('searchtype').value==defaultType))||(document.getElementById('searchtext').value=='')||(document.getElementById('searchtext').value==initialSearch)) {
					var alert_extra1='';
					var alert_extra2='';
					if (document.getElementById('searchtext').value!=initialSearch) {
						alert_extra1=' new';
						alert_extra2=' or type'; }
					alert("Please enter a" + alert_extra1 + " search term" + alert_extra2 + " before clicking 'Find'.                    ");
					return false; } } } } }

// VAT NO BOX FUNCTION
function vatBox() {
	// CHECK SEARCH BOX EXISTS
	if (document.getElementById('vat_no')) {
		// INITIAL VALUE
		var initialVatNo=document.getElementById('vat_no').value;
		// ON FOCUS - REMOVE START TEXT
		document.getElementById('vat_no').onfocus=function() {
			if (document.getElementById('vat_no').value==initialVatNo) {
				document.getElementById('vat_no').value='';
				return false; } } 
		// REPLACE START TEXT ON BLUR IF BLANK
		document.getElementById('vat_no').onblur=function() {
			if (document.getElementById('vat_no').value=='') {
				document.getElementById('vat_no').value=initialVatNo;
				return false; } } } }

// SEND EPDQ FORM AUTOMATICALLY
function sendEpdq() {
	if (document.getElementById('send_epdq_form')) {
		document.getElementById('send_epdq_form').submit(); } }

// THIS CALLS ALL FUNCTIONS THAT NEED TO RUN AT ONCE
function doFunctions() {
	popups();
	closePopup();
	autoSelect();
	searchForm();
	vatBox();
	addDelButton();
	updateDelAddress();
	sendEpdq();
	}


// MENU FUNCTIONS FOR THE MAINTENANCE KITS AND FUSERS PAGE
function closeSwitches(switches) {	
	for (i=0;i<switches.length;i++) {
		document.getElementById(switches[i]).style.display='none';
		}
	}

// OPENS/CLOSES MENU LISTS
function switchMenu(id) {
	var span_id=id+'_span';
	if (document.getElementById(id).style.display=='none') {
		document.getElementById(id).style.display='block';
		document.getElementById(span_id).innerText='-'; }
	else {
		document.getElementById(id).style.display='none';
		document.getElementById(span_id).innerText='+'; }
	}


// START FUNCTIONS WHEN WINDOW HAS LOADED
window.onload=doFunctions;
