// error handling, can be switched off
// window.onerror = showError;

function showError( e, f, l ){
	window.alert("Please report the following error to info@auburn.co.uk.\nFile: " + f + "\nLine: " + l + "\nError:" + e);
	return true;	
}

// variables
var customerServiceEmail = "info@yourwebsite.co.uk";
var customerServicePhone = "01926 736 796";
var siteShortURL = "yourwebsite.co.uk";

// custom function placeholders
function customInit() { }
function customTextOnly() { }
function customVisual() { }
function customMetrics() { }
function customValidate() { }

// quotation

function getQuote() {
	var valid = false;
	var net = 0;
	var netPremium = 0;
	var total, days, totalPremium, daysPremium;
	
	var inputCrates = document.getElementById('crates').value;
	var inputDays = document.getElementById('days').value;
	var inputWeeks = document.getElementById('weeks').value;
	var inputFrom = document.getElementById('from').value;
	var inputTo = document.getElementById('to').value;
	
	try {

		crates = parseFloat(inputCrates);

		if (crates == 0) {
			valid = false;
		} else if (inputDays != '') {
			days = parseFloat(inputDays);								
			valid = true;
		} else if (inputWeeks != '') {
			days = parseFloat(inputWeeks) * 7;
			valid = true;
		} else if (inputFrom != '') {
			var pattern = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;									
			
			var matchGroups = inputFrom.match(pattern);
			
			year = parseFloat(matchGroups[1]);
			month = parseFloat(matchGroups[2]) - 1;
			day = parseFloat(matchGroups[3]);
			
			var from = new Date(year,month,day);
			
			matchGroups = inputTo.match(pattern);
			
			year = parseFloat(matchGroups[1]);
			month = parseFloat(matchGroups[2]) - 1;
			day = parseFloat(matchGroups[3]);									
			
			var to = new Date(year,month,day);									
			
			days = (to - from + 1) / (1000 * 60 * 60 * 24);
			days = days.toFixed(0)
			
			valid = true;	
		
		} else {
			valid = false;
		}
		
		net = crates * days;
		net = net.toFixed(2);
		total = net * 1.175;
		total = total.toFixed(2);

		netPremium = crates * days * 1.3;
		netPremium = netPremium.toFixed(2);
		totalPremium = netPremium * 1.175;
		totalPremium = totalPremium.toFixed(2);
	
		if (net == 'NaN') {
			valid = false;
		}
	
	} catch(e) { 
		valid = false;
	}

	if (valid == true && net > 0) {
		document.getElementById('quote').style.display = 'block';
		document.getElementById('period').innerHTML = crates + ' crates for ' + days + ' days';	

		document.getElementById('net').innerHTML = '£' + net;
		document.getElementById('total').innerHTML = '£' + total;
		document.getElementById('netPremium').innerHTML = '£' + netPremium;
		document.getElementById('totalPremium').innerHTML = '£' + totalPremium;							
	} else {
		window.alert('You must specify the number of crates and valid length of time in either days, weeks or two dates in the format 2005-12-24.');
	}
	
	return false;
}


