/* Copyright (C) Abarthisti 2008-2010 */

if (top!=self) top.location.href=self.location.href;

function pop(img, width, height) {
	var window_name = 'new_wnd';
	var resizable = 1;
	var scroll = 0;
	var leftPos = 0;
	var topPos  = 0;

	if(screen) {
		leftPos = (screen.width/2) - (width/2);
		topPos  = (screen.height/2) - (height/2);
	}

	var new_window = window.open('/pop.html?'+img, window_name,'height='+height+',width='+width+',status=0,toolbar=0,resizable='+resizable+',scrollbars='+scroll+',menubar=0,location=0,directories=0,left='+leftPos+',top='+topPos+'');
}
function popup(page, width, height) {
	var window_name = 'new_wnd';
	var resizable = 1;
	var scroll = 0;
	var leftPos = 0;
	var topPos  = 0;

	if(screen) {
		leftPos = (screen.width/2) - (width/2);
		topPos  = (screen.height/2) - (height/2);
	}

	var new_window = window.open(page, window_name,'height='+height+',width='+width+',status=0,toolbar=0,resizable='+resizable+',scrollbars='+scroll+',menubar=0,location=0,directories=0,left='+leftPos+',top='+topPos+'');
}
function showCar(stockid, defaultimg, width, height) {
	var window_name = 'new_wnd';
	var resizable = 1;
	var scroll = 0;
	var leftPos = 0;
	var topPos  = 0;

	if(screen) {
		leftPos = (screen.width/2) - (width/2);
		topPos  = (screen.height/2) - (height/2);
	}

	var new_window = window.open('/find-a-car/stockpop.php?id='+stockid+'&s='+defaultimg, window_name,'height='+height+',width='+width+',status=0,toolbar=0,resizable='+resizable+',scrollbars='+scroll+',menubar=0,location=0,directories=0,left='+leftPos+',top='+topPos+'');
}
function formatCurrency(num) {
	var objRegExp = new RegExp('(-?\[0-9]+)([0-9]{3})');
	num = num.toString().replace(/\,/g,"");
	num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;
	//num.toString().indexOf(".") == -1 ? num += ".00" : void 0;
	while(/\.\d{0,1}$/.test(num)) num += "0";
	while(objRegExp.test(num)) num = num.toString().replace(objRegExp,'$1,$2');
	return num;
}
function calc() {
	var fields = document.getElementById('fields').value.split(',');
	var total  = parseFloat(document.getElementById('baseprice').value);
	
	for(var i=0; i<fields.length; i++) {
		if(document.getElementById(fields[i]).checked)
			total = total + parseFloat(document.getElementById(fields[i]).value);
	}
	
	document.getElementById('total').innerHTML = "&pound;" + formatCurrency(total);
}
function calc2() {
	var fields = document.getElementById('fields').value.split(',');
	var total  = parseFloat(document.getElementById('baseprice').value);
	
	for(var i=0; i<fields.length; i++) {
		if(document.getElementById(fields[i]).checked)
			total = total + parseFloat(document.getElementById(fields[i]).value);
	}
	
	document.getElementById('total').innerHTML = "&pound;" + formatCurrency2(total);
}
/* DID
function eventcalc() {
	var total = 0;

	if(document.getElementById('spectator').checked) {
		document.getElementById('moredrivers').selectedIndex = 0;
		document.getElementById('instruction').selectedIndex = 0;
		document.getElementById('moredrivers').disabled = true;
		document.getElementById('instruction').disabled = true;
	} else {
		document.getElementById('moredrivers').disabled = false;
		document.getElementById('instruction').disabled = false;
	}

	if(document.getElementById('driver').checked) total = total + 99;
	if(document.getElementById('spectator').checked) total = total + 20;

	total = total + (document.getElementById('moredrivers').options[document.getElementById('moredrivers').selectedIndex].value * 39);
	total = total + (document.getElementById('guests').options[document.getElementById('guests').selectedIndex].value * 20);
	total = total + (document.getElementById('instruction').options[document.getElementById('instruction').selectedIndex].value * 15);
	total = total + (document.getElementById('paxrides').options[document.getElementById('paxrides').selectedIndex].value * 10);

	document.getElementById('totaltext').innerHTML = "&pound;" + total;
	document.getElementById('totalfield').value = total;
} */
function eventcalc() {
	var paypal = 0;
	var total = 0;

	total = total + (document.getElementById('adults').options[document.getElementById('adults').selectedIndex].value * 9.3);
	total = total + (document.getElementById('children').options[document.getElementById('children').selectedIndex].value * 4.6);

	paypal = ((total + 0.20) / (1-(3.4/100)) - total);
	total = total + paypal;

	if(document.getElementById('ntmember').checked) {
		total = 0;
		paypal = 0;
	}

	document.getElementById('totaltext').innerHTML = "&pound;" + formatCurrency2(total);
	document.getElementById('paypaltext').innerHTML = "&pound;" + formatCurrency2(paypal);
	document.getElementById('totalfield').value = total;
	document.getElementById('paypalfield').value = paypal;
}
function formatCurrency2(num) {
	var objRegExp = new RegExp('(-?\[0-9]+)([0-9]{3})');
	num = num.toString().replace(/\,/g,"");
	num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;
	num.toString().indexOf(".") == -1 ? num += ".00" : void 0;
	while(/\.\d{0,1}$/.test(num)) num += "0";
	while(objRegExp.test(num)) num = num.toString().replace(objRegExp,'$1,$2');
	return num;
}
var IsIE = false;
var IsIE7 = false;

function DisplayMainCover() {
	// Setup the DIV that will be used to cover.
	var cover = document.getElementById('cover');
	var relElement = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	cover.style.width = Math.max( relElement.scrollWidth, relElement.clientWidth, document.scrollWidth || 0 ) - 1 + 'px';
	cover.style.height = Math.max( relElement.scrollHeight, relElement.clientHeight, document.scrollHeight || 0 ) - 1 + 'px';

	var obj = document.getElementById('covercontent');
	obj.style.top = "-99999px"; //  Move the image off the page so it doesn't flash when it re-appears
	obj.style.left = "-999999px";

	cover.style.display = 'block';
	document.getElementById('coverhorizon').style.display = 'block';

	var pageSize = getPageSize();
	var scrollPos = getScrollPos();
	var emSize = getElementSize(obj);
	var x = Math.round((pageSize.width - emSize.width) / 2 + scrollPos.scrollX);
	var y = Math.round((pageSize.height - emSize.height) / 2 + scrollPos.scrollY);
	if (obj.offsetLeft) x -= obj.offsetLeft;
	if (obj.offsetTop) y -= obj.offsetTop;
	if (obj.style.left) x += parseInt(obj.style.left);
	if (obj.style.top) y += parseInt(obj.style.top);
	x -= 10; // this nearly centers it due to scrollbars
	obj.style.top = y + 'px';
	obj.style.left = x + 'px';
}
function getPageSize() {
	return {
		width: window.innerWidth || (document.documentElement && document.documentElement.clientWidth) || document.body.clientWidth,
		height: window.innerHeight || (document.documentElement && document.documentElement.clientHeight) || document.body.clientHeight
	};
}
function getScrollPos() {
	var docElem = document.documentElement;
	return {
		scrollX: document.body.scrollLeft || window.pageXOffset || (docElem && docElem.scrollLeft),
		scrollY: document.body.scrollTop || window.pageYOffset || (docElem && docElem.scrollTop)
	};
}
function getElementSize(obj) {
	return {
		width: obj.offsetWidth || obj.style.pixelWidth,
		height: obj.offsetHeight || obj.style.pixelHeight
	};
}
function closeImage() {
	document.getElementById('cover').style.display = 'none';
	document.getElementById('coverhorizon').style.display = 'none';
}
function zoomImage(img) {
	document.getElementById('coverimage').src = img;
	DisplayMainCover();
}
function clickAllColours() {
	var args = clickAllColours.arguments;
	var allCheck = document.getElementById('allcolours').checked;
	for(var i=0;i<args.length;i++)
		document.getElementById('colour'+args[i]).checked = allCheck;
}
function checkAllColours() {
	var args = checkAllColours.arguments;
	var allChecked = true;
	for(var i=0;i<args.length;i++)
		if(document.getElementById('colour'+args[i]).checked==false) {
			allChecked = false;
			break;
		}
	document.getElementById('allcolours').checked = allChecked;
}
