/* Preload images
---------------------------------------------------------------------------------------------*/
var go_buttonOff = new Image();go_buttonOff.src = '../../images/brett/button_search_go.gif';
var go_buttonOn = new Image();go_buttonOn.src = '../../images/brett/button_search_go_hover.gif';

var ibnClearOff = new Image();ibnClearOff.src = '../../images/brett/button_clear.gif';
var ibnClearOn = new Image();ibnClearOn.src = '../../images/brett/button_clear_hover.gif';

var iFormClearOff = new Image();iFormClearOff.src = '../../images/brett/button_clear.gif';
var iFormClearOn = new Image();iFormClearOn.src = '../../images/brett/button_clear_hover.gif';

var iFormSubmitOff = new Image();iFormSubmitOff.src = '../../images/brett/button_submit.gif';
var iFormSubmitOn = new Image();iFormSubmitOn.src = '../../images/brett/button_submit_hover.gif';

var ibnSubmitOff = new Image();ibnSubmitOff.src = '../../images/brett/button_submit.gif';
var ibnSubmitOn = new Image();ibnSubmitOn.src = '../../images/brett/button_submit_hover.gif';

var ibnSubmitTypeOff = new Image();ibnSubmitTypeOff.src = '../../images/brett/button_findnow.gif';
var ibnSubmitTypeOn = new Image();ibnSubmitTypeOn.src = '../../images/brett/button_findnow_hover.gif';

var ibnFindNowOff = new Image();ibnFindNowOff.src = '../../images/brett/button_findnow.gif';
var ibnFindNowOn = new Image();ibnFindNowOn.src = '../../images/brett/button_findnow_hover.gif';

var ibnSubmitBrandOff = new Image();ibnSubmitBrandOff.src = '../../images/brett/button_findnow.gif';
var ibnSubmitBrandOn = new Image();ibnSubmitBrandOn.src = '../../images/brett/button_findnow_hover.gif';

var iProdFindGoOff = new Image();iProdFindGoOff.src = '../../images/brett/go_prodfinder.gif';
var iProdFindGoOn = new Image();iProdFindGoOn.src = '../../images/brett/go_prodfinder_hover.gif';

/* var alphaOff = new Image(); alphaOff.src = 'images/driveways_paving.gif'; */
/* var alphaOn = new Image(); alphaOn.src = 'images/go_prodfinder_hover.gif'; */

/* Direct image swaps i.e. swap the source of the image being moused-over
---------------------------------------------------------------------------------------------*/
function iSwapOn(el) {
	document.getElementById(el.id).src = eval(el.id + 'On').src;
}

function iSwapOff(el) {
	document.getElementById(el.id).src = eval(el.id + 'Off').src;
}

/* Indirect image swaps i.e. swap an image saperate from the element being moused-over
---------------------------------------------------------------------------------------------*/
function iSetSwapOn(el, iTarget) {
	var idStr = el.title.toLowerCase();
	idStr = replaceAll(idStr);
	document.getElementById(iTarget).src = eval(idStr + 'On').src; //eval(idStr + 'Off').src;
}

function iSetSwapOff(el, iTarget) {
	var idStr = el.title.toLowerCase();
	idStr = replaceAll(idStr);
	document.getElementById(iTarget).src = eval(idStr + 'Off').src; //eval(idStr + 'Off').src;
}

/* Replace all occurances of a character in string
---------------------------------------------------------------------------------------------*/
function replaceAll(str) {
	return str.replace(/-| |'|&/g, '');
}

/* Used on the "Literature - Brochures" page
---------------------------------------------------------------------------------------------*/
function checkRole(elm, ebroc) {
	var frm = document.forms['frmBrochure'];
	if (frm.regBusinessTypes.selectedIndex == 0) {
		alert('Please select your role to download the brochure.');
	}
	else {
		frm.businesstype.value = frm.regBusinessTypes.selectedIndex;  // May want to check if the value of brochure is null or empty.
		frm.businesstypeother.value = frm.strBusinessTypeOther.value;  // May want to check if the value of brochure is null or empty.
		frm.brochure.value = ebroc;  // May want to check if the value of brochure is null or empty.
		frm.submit()
		// Start action to submit form and download selected brochure.
		// alert('Submit to download brochure (' + frm.brochure.value + ') and save role selection.');
	}
}

/* Used on the "Literature - Ordering" page - displays other text field
---------------------------------------------------------------------------------------------*/
function showExtra(theCombo, eid, fid) {
	//get a reference to the control
	var myText = document.getElementById(eid);
	//alert(eid)

	//now check to what the new selection is
	if (theCombo.options[theCombo.selectedIndex].value == fid) {
		myText.style.display = "";
		//alert(theCombo.options[theCombo.selectedIndex].value)
	}
	else {
		myText.value = "";
		myText.style.display = "none";
	}
}

/* Find x position of element
---------------------------------------------------------------------------------------------*/
function getPageOffsetLeft(el) {
  var x;
  // Return the x coordinate of an element relative to the page.
  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);
  return x;
}

/* Find y position of element
---------------------------------------------------------------------------------------------*/
function getPageOffsetTop(el) {
  var y;
  // Return the x coordinate of an element relative to the page.
  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);
  return y;
}	

/* Animate element height
---------------------------------------------------------------------------------------------*/
function animateHeight(id, size) {
  el = document.getElementById(id);
  var h_size1 = el.offsetHeight;
  var step = 3;
  if (isMac) {
    step = 10; 
  }
  if (h_size1 < size) {
    if ((size - el.offsetHeight) < step) {
      step = (size - el.offsetHeight);
    }
    el.style.height = h_size1 + step +"px";
    setTimeout("animateHeight('" + id + "','" + size + "')", 0);
  }
}
  
/* Show elements
---------------------------------------------------------------------------------------------*/
function doToggleOn(src, pop) {
 var animate = true;
 if (loaded) {
   src = document.getElementById(src);
   elm = pop.replace('drop','');
   var x = getPageOffsetLeft(src);
   var y = getPageOffsetTop(src) + src.offsetHeight;
   obj = eval('obj' + elm);
   obj.style.left = x + 'px';
   obj.style.top = y + 'px';
   obj.style.visibility = 'visible';
   if (animate) {
     obj.style.height = '0px';
     obj.style.overflow = 'hidden';
      setInterval("animateHeight('"+pop+"', obj.scrollHeight)", 10)
    }  
  }  
}
	
/* Hide elements
---------------------------------------------------------------------------------------------*/
function doToggleOff(src, pop) {
  if (loaded) {
	  src = document.getElementById(src);
    pop = pop.replace('drop','');
    obj = eval('obj' + pop);
    obj.style.visibility = 'hidden';
  }  
}
  
/* To run onload of page
---------------------------------------------------------------------------------------------*/
var loaded = false;
var isMac = (navigator.userAgent.indexOf('Mac') != -1);
window.onload = function() {
	
  if (document.getElementById('dropAggregates') != null) {
 		objAggregates = document.getElementById('dropAggregates');
  } 
  if (document.getElementById('dropConcrete') != null) {
 		objConcrete = document.getElementById('dropConcrete');
  } 
  if (document.getElementById('dropConstruction') != null) {
  	objConstruction = document.getElementById('dropConstruction');
  } 
  if (document.getElementById('dropLandscape') != null) {
 		objLandscape = document.getElementById('dropLandscape');
  } 
  loaded = true;
}

/* Literature Order Form
---------------------------------------------------------------------------------------------*/
function validateLiteratureOrderForm() {
	var blValid = true;
	var strError = 'Please ensure the following form fields are completed before proceeding:\n\n';

	var strusrname        = document.frmLitOrder.usrname.value;
	var strBusinessTypes  = document.frmLitOrder.regBusinessTypes.value;
	var strusraddress1    = document.frmLitOrder.usraddress1.value;
	var strusrPostcode    = document.frmLitOrder.usrPostcode.value;
	var strregCountries   = document.frmLitOrder.regCountries[document.frmLitOrder.regCountries.selectedIndex].value;
	var strusrtelephone   = document.frmLitOrder.usrtelephone.value;
	var strusremail       = document.frmLitOrder.usremail.value;
	var blBrochureChecked = oneChecked(document.frmLitOrder.selBrochure);

	if (strusrname == '') {
		strError = strError + "Please enter your name\n";
		blValid = false;
	}
	if (strBusinessTypes == '-1') {
		strError = strError + "Please select a business type\n";
		blValid = false;
	}
	if (strusraddress1 == '') {
		strError = strError + "Please enter your address\n";
		blValid = false;
	}
	if (strusrPostcode == '') {
		strError = strError + "Please enter your postcode\n";
		blValid = false;
	}
	if (strregCountries == '-1') {
		strError = strError + "Please enter your country\n";
		blValid = false;
	}
	if (strusrtelephone == '') {
		strError = strError + "Please enter your telephone\n";
		blValid = false;
	}
	if ((strusremail == '') || (!echeck(strusremail))) {
		strError = strError + "Please enter your email\n";
		blValid = false;
	}
	if (!blBrochureChecked) {
		strError = strError + "Please select at least one of the available brochures\n";
		blValid = false;
	}


//RETURN TO FORM
	if (blValid) {
		return (true); }
	else {
		window.alert (strError);
		return (false); }	
}

/* Contact us Form
---------------------------------------------------------------------------------------------*/
function validateContactUsOrderForm() {
	var blValid = true;
	var strError = 'Please ensure the following form fields are completed before proceeding:\n\n';

	var strusrname        = document.frmContactUs.usrname.value;
	var strusraddress1    = document.frmContactUs.usraddress1.value;
	var strusrPostcode    = document.frmContactUs.usrPostcode.value;
	var strregCountries   = document.frmContactUs.regCountries[document.frmContactUs.regCountries.selectedIndex].value;
	var strusrtelephone   = document.frmContactUs.usrtelephone.value;
	var strusremail       = document.frmContactUs.usremail.value;
	var blBaseChecked     = oneChecked(document.frmContactUs.idEnquiryType);

	if (strusrname == '') {
		strError = strError + "Please enter your name\n";
		blValid = false;
	}
	if (strusraddress1 == '') {
		strError = strError + "Please enter your address\n";
		blValid = false;
	}
	if (strusrPostcode == '') {
		strError = strError + "Please enter your postcode\n";
		blValid = false;
	}
	if (strregCountries == '-1') {
		strError = strError + "Please enter your country\n";
		blValid = false;
	}
	if (strusrtelephone == '') {
		strError = strError + "Please enter your telephone\n";
		blValid = false;
	}
	if ((strusremail == '') || (!echeck(strusremail))) {
		strError = strError + "Please enter your email\n";
		blValid = false;
	}
	if (!blBaseChecked) {
		strError = strError + "Please indicate the nature of your enquiry\n";
		blValid = false;
	}


//RETURN TO FORM
	if (blValid) {
		return (true); }
	else {
		window.alert (strError);
		return (false); }	
}

/* Stockist finder
---------------------------------------------------------------------------------------------*/
function clearStockistFinderForm() {
	var objProduct        = document.frmStockistFinder.product;
	var strPostcode       = document.frmStockistFinder.strPostcode;
	var strTown           = document.frmStockistFinder.strTown;
	var strStockistName   = document.frmStockistFinder.strStockistName;
	
	for (i = 0; i < objProduct.length; i++) {
		objProduct[i].checked = false;
	}
	strPostcode.value = "";
	strTown.value = "";
	strStockistName.value = "";
}

/* Stockist finder
---------------------------------------------------------------------------------------------*/
function validateStockistFinderForm() {
	var blValid = true;
	var strError = 'Please ensure the following form fields are completed before proceeding:\n\n';

	var strPostcode       = document.frmStockistFinder.strPostcode.value;
	var strTown           = document.frmStockistFinder.strTown.value;
	var strStockistName   = document.frmStockistFinder.strStockistName.value;
	var blServiceChecked  = oneChecked(document.frmStockistFinder.product);

	if ((strTown == '') && (strPostcode == '')) {
		strError = strError + "Please enter either a postcode or town\n";
		blValid = false;
	}
	if ((strTown != '') && (strPostcode != '')) {
		strError = strError + "Please enter either a postcode OR town\n";
		blValid = false;
	}
	if (!blServiceChecked) {
		strError = strError + "Please select at least one of the available products\n";
		blValid = false;
	}

//RETURN TO FORM
	if (blValid) {
		return (true); }
	else {
		window.alert (strError);
		return (false); }	
}


/* Contractor finder
---------------------------------------------------------------------------------------------*/
function validateContractorFinderForm() {
	var blValid = true;
	var strError = 'Please ensure the following form fields are completed before proceeding:\n\n';

	var strTown          = document.frmContractorFinder.strTown.value;
	var strPostcode      = document.frmContractorFinder.strPostcode.value;
	var blServiceChecked = oneChecked(document.frmContractorFinder.idService);

	if ((strTown == '') && (strPostcode == '')) {
		strError = strError + "Please enter either a postcode or town\n";
		blValid = false;
	}
	if ((strTown != '') && (strPostcode != '')) {
		strError = strError + "Please enter either a postcode OR town\n";
		blValid = false;
	}
	if (!blServiceChecked) {
		strError = strError + "Please select at least one of the available services\n";
		blValid = false;
	}

//RETURN TO FORM
	if (blValid) {
		return (true); }
	else {
		window.alert (strError);
		return (false); }	
}

function clearContractorFinderForm() {
	var objService        = document.frmContractorFinder.idService;
	var strPostcode       = document.frmContractorFinder.strPostcode;
	var strTown           = document.frmContractorFinder.strTown;
	var strContractorName = document.frmContractorFinder.strContractorName;
	
	for (i = 0; i < objService.length; i++) 
	{
		objService[i].checked = false;
	}
	strPostcode.value = "";
	strTown.value = "";
	strContractorName.value = "";
}

function oneChecked(group){
	var blAtLeastOneClicked = false;
	if (group.length){
		for (i = 0; i < group.length; i++) 
		{
			if (group[i].checked == true)
			{
				blAtLeastOneClicked = true;
			}
		}
	}
	else
	{
		if (group.checked){
			blAtLeastOneClicked = true; }
		else {
			blAtLeastOneClicked = false; }
	}
	return blAtLeastOneClicked;
}

function echeck(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	if (lat == (lstr - 1)) { return false; }
	if (ldot == (lstr - 1)) { return false; }
	if (str.indexOf(at) == -1) { return false;}
	if ((str.indexOf(at) == -1) || (str.indexOf(at) == 0) || (str.indexOf(at) == lstr)) { return false; }
	if ((str.indexOf(dot) == -1) || (str.indexOf(dot) == 0) || (str.indexOf(dot) == lstr)){ return false; }
	if (str.indexOf(at, (lat+1)) != -1){ return false; }
	if ((str.substring(lat-1, lat) == dot) || (str.substring(lat+1, lat+2) == dot)){ return false; }
	if (str.indexOf(dot, (lat+2)) == -1){ return false; }
	if (str.indexOf(" ") != -1){ return false; }
	return true;
}

function doReset() {
	var bConfirm = confirm('Are you sure you want to clear all fields on this form?');
	if(bConfirm == true) {
		document.location.href = document.location.href;
	}
}
