function popUpImage() {
	var popUpWin = '';
	var popUpImageRegExp = new RegExp("(^|\\s)popUpImage(\\s|$)");
	var a = document.getElementsByTagName('a');
	var area = document.getElementsByTagName('area');
	function setPopUp(linksArray) {
		for (var i=0;i<linksArray.length;i++) {
			if(popUpImageRegExp.test(linksArray[i].className)) {
				linksArray[i].number = i;
				linksArray[i].image = new Image();
				linksArray[i].image.src = linksArray[i].href;
				linksArray[i].title = linksArray[i].title;
				linksArray[i].onclick = function() {
					winSrc = linksArray[this.number].image.src;
					winTitle = linksArray[this.number].title;
					winWidth = linksArray[this.number].image.width;
					winHeight = linksArray[this.number].image.height;
					winAtt = 'width='+winWidth+'px,height='+winHeight+'px';
					if(!popUpWin.closed && popUpWin.location) { popUpWin.close(); }
					popUpWin = window.open('', 'popUpWin', winAtt);
					popUpWin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>' + winTitle + '</title><body style="margin: 0; padding: 0;"><div><img src="' + winSrc + '" width="' + winWidth + '" height="' + winHeight + '" alt=""></div></body>');
					popUpWin.document.close();
					popUpWin.focus();
					return false;
				}
			}
		}
	}
	setPopUp(a);
	setPopUp(area);
}

function MSpopuplink() {
  var pathRegExp = new RegExp("^.*:\/\/.*");
  var domainRegExp = new RegExp("(^.*:\/\/)" + document.domain + "(.*)", "i");
  var classRegExp = new RegExp("(^|\\s)popuplink(\\s|$)");
  var e = document.getElementsByTagName('body')[0];
  if(e) {
    var a = e.getElementsByTagName('a');
    for(var i=0; i<a.length; i++){
    if((pathRegExp.test(a[i].getAttribute('href')) && !domainRegExp.test(a[i].getAttribute('href'))) || classRegExp.test(a[i].className)) {
        if(!classRegExp.test(a[i].className)) {
          a[i].className += a[i].className?' popuplink':'popuplink';
        }
        a[i].title += a[i].title?' (opens in a new window)':'Opens in a new window';
        a[i].onclick = function() {
          newWin = window.open(this.href,'');
          newWin.focus();
          return false;
        }

      }
    }
  }
}

function setCountry() {
	countryObj = document.getElementById('country_choice');
	if(countryObj) {
		for(var i=0; i<countryObj.length; i++) {
			if(countryObj.options[i].value == 'United Kingdom') {
				countryObj.selectedIndex = i;
				break;
			}
		}
	}
}

function hideCircleWidth() {
	if(document.getElementById('lawncoverage_shape')) {
		document.getElementById('lawncoverage_shape').onchange = function() {
			if(document.getElementById('lawncoverage_shape')[document.getElementById('lawncoverage_shape').selectedIndex].value == 'circle') {
				document.getElementById('lawncoverage_setlength').style.display = 'none';
				document.getElementById('lawncoverage_setlength').style.display = 'none';
			} else {
				document.getElementById('lawncoverage_setlength').style.display = 'block';
			}
		}
        }
	if(document.getElementById('landscapecoverage_shape')) {
		document.getElementById('landscapecoverage_shape').onchange = function() {
			if(document.getElementById('landscapecoverage_shape')[document.getElementById('landscapecoverage_shape').selectedIndex].value == 'circle') {
				document.getElementById('landscapecoverage_setlength').style.display = 'none';
			} else {
				document.getElementById('landscapecoverage_setlength').style.display = 'block';
			}
		}
        }
	if(document.getElementById('playgroundcoverage_shape')) {
		document.getElementById('playgroundcoverage_shape').onchange = function() {
			if(document.getElementById('playgroundcoverage_shape')[document.getElementById('playgroundcoverage_shape').selectedIndex].value == 'circle') {
				document.getElementById('playgroundcoverage_setlength').style.display = 'none';
			} else {
				document.getElementById('playgroundcoverage_setlength').style.display = 'block';
			}
		}
        }
}

function calculateArea(shape, width, length) {
	switch (shape) {
		case 'rectangle':
			return Math.ceil(width * length);
			break;
		case 'triangle':
			return Math.ceil(width * length / 2);
			break;
		case 'circle':
			return Math.ceil(Math.PI * Math.pow((width / 2), 2));
			break;
		default:
			return '';
	}
}

function lawncoverage() {
	coverageperkilo = 1;
	var shape = document.getElementById('lawncoverage_shape')[document.getElementById('lawncoverage_shape').selectedIndex].value;
	var width = document.getElementById('lawncoverage_width').value;
	var length = document.getElementById('lawncoverage_length').value;
	if((!isNaN(width) && !isNaN(length) && width != '' && length != '') || (shape == 'circle' && !isNaN(width) && width != '')) {
		document.getElementById('lawncoverage_gardensize').value = calculateArea(shape, width, length);
	}
	gardensize = document.getElementById('lawncoverage_gardensize').value;
	if(!isNaN(gardensize) && gardensize != '') {
		kilosrequired = Math.ceil(gardensize/coverageperkilo);
		if(kilosrequired < 500) {
			bags = Math.ceil(gardensize/(coverageperkilo * 10));
			postfix = '';
			if(bags != 1) {
				postfix = 's';
			}
			document.getElementById('lawncoverage_result').innerHTML = 'You require <strong>' + bags + ' x 10 kilo bag' + postfix + '</strong>';
		} else {
			bulkbags = Math.floor(gardensize/(coverageperkilo * 500));
			bulkpostfix = '';
			if(bulkbags != 1) {
				bulkpostfix = 's';
			}
			document.getElementById('lawncoverage_result').innerHTML = 'You require <strong>' + bulkbags + ' x 500 kilo bag' + bulkpostfix + '</strong>';
			bags = Math.ceil(gardensize/(coverageperkilo * 10)) - (bulkbags * 50);
			postfix = '';
			if(bags != 1) {
				postfix = 's';
			}
			if(bags > 0) {
				document.getElementById('lawncoverage_result').innerHTML += ' plus <strong>' + bags + ' x 10 kilo bag' + postfix + '</strong>';
			}
		}
	} else {
		document.getElementById('lawncoverage_result').innerHTML = '<strong>You must enter a number.</strong>';
	}
	return false;
}

function landscapecoverage() {
	coverageperkilo = 0.075;
	var shape = document.getElementById('landscapecoverage_shape')[document.getElementById('landscapecoverage_shape').selectedIndex].value;
	var width = document.getElementById('landscapecoverage_width').value;
	var length = document.getElementById('landscapecoverage_length').value;
	if((!isNaN(width) && !isNaN(length) && width != '' && length != '') || (shape == 'circle' && !isNaN(width) && width != '')) {
		document.getElementById('landscapecoverage_gardensize').value = calculateArea(shape, width, length);
	}
	gardensize = document.getElementById('landscapecoverage_gardensize').value;
	if(!isNaN(gardensize) && gardensize != '') {
		kilosrequired = Math.ceil(gardensize/coverageperkilo);
		if(kilosrequired < 500) {
			bags = Math.ceil(gardensize/(coverageperkilo * 10));
			postfix = '';
			if(bags != 1) {
				postfix = 's';
			}
			document.getElementById('landscapecoverage_result').innerHTML = 'You require <strong>' + bags + ' x 10 kilo bag' + postfix + '</strong>';
		} else {
			bulkbags = Math.floor(gardensize/(coverageperkilo * 500));
			bulkpostfix = '';
			if(bulkbags != 1) {
				bulkpostfix = 's';
			}
			document.getElementById('landscapecoverage_result').innerHTML = 'You require <strong>' + bulkbags + ' x 500 kilo bag' + bulkpostfix + '</strong>';
			bags = Math.ceil(gardensize/(coverageperkilo * 10)) - (bulkbags * 50);
			postfix = '';
			if(bags != 1) {
				postfix = 's';
			}
			if(bags > 0) {
				document.getElementById('landscapecoverage_result').innerHTML += ' plus <strong>' + bags + ' x 10 kilo bag' + postfix + '</strong>';
			}
		}
	} else {
		document.getElementById('landscapecoverage_result').innerHTML = '<strong>You must enter a number.</strong>';
	}
	return false;
}

function playgroundcoverage() {
	coverageperkilo = document.getElementById('playgroundcoverage_equipmentheight')[document.getElementById('playgroundcoverage_equipmentheight').selectedIndex].value;
	var shape = document.getElementById('playgroundcoverage_shape')[document.getElementById('playgroundcoverage_shape').selectedIndex].value;
	var width = document.getElementById('playgroundcoverage_width').value;
	var length = document.getElementById('playgroundcoverage_length').value;
	if((!isNaN(width) && !isNaN(length) && width != '' && length != '') || (shape == 'circle' && !isNaN(width) && width != '')) {
		document.getElementById('playgroundcoverage_gardensize').value = calculateArea(shape, width, length);
	}
	gardensize = document.getElementById('playgroundcoverage_gardensize').value;
	if(!isNaN(gardensize) && gardensize != '') {
		kilosrequired = Math.ceil(gardensize/coverageperkilo);
		if(kilosrequired < 500) {
			bags = Math.ceil(gardensize/(coverageperkilo * 10));
			postfix = '';
			if(bags != 1) {
				postfix = 's';
			}
			document.getElementById('playgroundcoverage_result').innerHTML = 'You require <strong>' + bags + ' x 10 kilo bag' + postfix + '</strong>';
		} else {
			bulkbags = Math.floor(gardensize/(coverageperkilo * 500));
			bulkpostfix = '';
			if(bulkbags != 1) {
				bulkpostfix = 's';
			}
			document.getElementById('playgroundcoverage_result').innerHTML = 'You require <strong>' + bulkbags + ' x 500 kilo bag' + bulkpostfix + '</strong>';
			bags = Math.ceil(gardensize/(coverageperkilo * 10)) - (bulkbags * 50);
			postfix = '';
			if(bags != 1) {
				postfix = 's';
			}
			if(bags > 0) {
				document.getElementById('playgroundcoverage_result').innerHTML += ' plus <strong>' + bags + ' x 10 kilo bag' + postfix + '</strong>';
			}
		}
	} else {
		document.getElementById('playgroundcoverage_result').innerHTML = '<strong>You must enter a number.</strong>';
	}
	return false;
}

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    // alert('Handler could not be attached');
    return false;
  }
}

addEvent(window, 'load', popUpImage, false);
addEvent(window, 'load', MSpopuplink, false);
addEvent(window, 'load', setCountry, false);
addEvent(window, 'load', hideCircleWidth, false);





var checkOptions = new Array();
var rowArray = new Array();

function dispOptions(optionForm, originalProdName, optionName, optionTitle, optionValues, optionDelimiter) {

	var optionOutput = '';

	if(!checkOptions[optionForm]) {
		checkOptions[optionForm] = new Array();
	}

	if(optionTitle != '' && optionTitle != ' ' && optionValues != '' && optionValues != ' ') {

		var optionArray = optionValues.split(optionDelimiter);

		if(optionArray.length > 0) {
			optionOutput += '<p><label for="' + optionName + '">' + optionTitle + '</label>';
			optionOutput += '<select name="' + optionName + '" id="' + optionName + '">\n';
			optionOutput += '<option value="' + optionTitle + '">Please choose an option</option>\n';

			for(var i=0; i<optionArray.length; i++) {
				optionOutput += '<option value="' + optionArray[i] + '">' + optionArray[i] + '</option>\n';
			}

			optionOutput += '</select></p>\n';

			checkOptions[optionForm][checkOptions[optionForm].length] = optionName;
		} else {
			optionOutput += '<tr><td><b>' + optionTitle + ':</b></td><td align="right">' + optionArray[0] + '</td></tr>\n';
			document[optionForm][originalProdName].value = document[optionForm][originalProdName].value + '; ' + optionTitle + ': ' + optionArray[0];
		}

	}

	return(optionOutput);

}

function buyProduct(productForm) {

	var prodcodeAdditional = '';
	var prodnameAdditional = '';
	var errorMsg = '';

	for(var i=0; i<checkOptions[productForm].length; i++) {
		thisForm = document[productForm][checkOptions[productForm][i]];

		if(thisForm.selectedIndex > 0) {
			prodcodeAdditional += '_' + thisForm.selectedIndex;
			prodnameAdditional += '; ' + thisForm.options[0].value;
			prodnameAdditional += ': ' + thisForm.options[thisForm.selectedIndex].value;
		} else {
			errorMsg += '\n          You must select a ' + thisForm.options[0].value + ' option;';
		}
	}
	totalBought = 0;
	for(var i=0; i<rowArray.length; i++) {
		prodCodeOriginal = rowArray[i] + 'prodcode_original';
		prodCode = rowArray[i] + 'prodcode';
		prodNameOriginal = rowArray[i] + 'prodname_original';
		prodName = rowArray[i] + 'prodname';
		document[productForm][prodCode].value = document[productForm][prodCodeOriginal].value + prodcodeAdditional;
		document[productForm][prodName].value = document[productForm][prodNameOriginal].value + prodnameAdditional;

		prodMin = rowArray[i] + 'min';
		prodQty  = rowArray[i] + 'qty';
		if(parseInt(document[productForm][prodQty].value) > 0) {
			totalBought += parseInt(document[productForm][prodQty].value);
			if(parseInt(document[productForm][prodQty].value) < parseInt(document[productForm][prodMin].value)) {
				errorMsg += '\n          You must buy a minimum of ' + document[productForm][prodMin].value + ' of ' + document[productForm][prodName].value + ';';
			}
		}
	}
	if(totalBought == 0) {
		errorMsg += '\n          You must enter the quantity of this product you would like to buy;';
	}

	if(errorMsg == '') {
		return true;
	} else {
		alert('Before you buy this product:\n' + errorMsg);
		return false;
	}

}

