var vatRate = 0;

window.onload=getVAT; 

xx='aa';

function validateform()
{	
	if(document.getElementById('price').value!='' || document.getElementById('price_bch').value!='' || document.getElementById('price_pch').value !='' )
	{ }
	else
	{
	alert('Please enter a price or monthly lease cost'); 
	return false
	}
	
	if(document.getElementById('cfr_draft').checked)
	{ return confirm('This vehicle is currently marked as "Hide from website!" If you proceed, it will not appear in any search results.\n\nProceed anyway?'); }
	
	return true
}

function deleteSelected()
{
	// get a list of the selected items
	var xx = countSelected();
	if(xx=='') { alert('Please select one or more items before proceeding.'); return false }
	mx = confirm("Do you really want to delete the records that have been ticked?");
	if(mx==true) 
	{
		document.location = "/clients/index.cfm?fnc=myDeals&bulkdelete="+xx;
	}
}


function processing()
{
	document.getElementById("processingmessage").style.display = "block";
	document.getElementById("processingform").style.display = "none";
	return false;
}

function commercialEnable(type)
{
	$("#durationdeposit_1").attr("disabled","disabled");
	$("#durationdeposit_2").attr("disabled","disabled");
	$("#duration").attr("disabled","disabled");
	$("#maintained").attr("disabled","disabled");
	$("#bchprice").attr("disabled","disabled");
	$("#mpa").attr("disabled","disabled");
	$("#price").attr("disabled","disabled");
	$("#rrp").attr("disabled","disabled");
	switch(type)
	{
		case 1:
			$("#durationdeposit_1").removeAttr("disabled");
			$("#durationdeposit_2").removeAttr("disabled");
			$("#duration").removeAttr("disabled");
			$("#maintained").removeAttr("disabled");
			$("#bchprice").removeAttr("disabled");
			$("#mpa").removeAttr("disabled");
			$("#mpa").focus();
			break;
		case 2:
			$("#price").removeAttr("disabled");
			$("#rrp").removeAttr("disabled");
			$("#rrp").focus();
			break;
		case 3:
			$("#price").removeAttr("disabled");
			$("#price").focus();
			break;
	}
}



function custRound(x,places) {
	return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}

function getVAT() { 
	if(document.getElementById('vat_rate'))
	{
	vatRate = document.getElementById('vat_rate').value;
	}
	else
	{
	vatRate = 0;
	}
}

function calcVat(sourceField,destinationField)
{
	if (!sourceField) { sourceField = 'bchprice'; destinationField = 'pchprice' }
	var bch = $("#"+sourceField).getValue();
	pchPrice=custRound(bch * vatRate,2);
	$("#"+destinationField).setValue(pchPrice);
	$("#"+sourceField).setValue(custRound(bch,2));
	alert("PCH set to - "+pchPrice);
}

function reinstateSelected()
{
	// get a list of the selected items
	var xx = countSelected();
	if(xx=='') { alert('Please select one or more items before proceeding.'); return false }
	mx = confirm("Do you really want to reinstate the records that have been ticked?\n\nAny expiry dates will be removed and the deal will become active again. NOTE: It will not perform a boost on the deal(s).");
	if(mx==true) 
	{
		document.location = "/clients/index.cfm?fnc=myDeals&bulkreinstate="+xx;
	}
}

function selectAllDeals(thestate)
{
	for(var i=0; i < document.theForm.selbox.length; i++)
	{
		document.theForm.selbox[i].checked = thestate;
	}
	return true
}

function countSelected()
{
	
	var total="";
	var totalSel=0;
	if(document.theForm.selbox.length)
		{
		for(var i=0; i < document.theForm.selbox.length; i++)
			{
			if(document.theForm.selbox[i].checked)
			total += document.theForm.selbox[i].value + ","
			totalSel += 1;
			}
		}
		else
		{
			if(document.theForm.selbox.checked)
			{
				total = document.theForm.selbox.value;
				totalSel = 1;
			}
		}
	
	alert(total);

	return total;
}

function promptAbort(func)
{
mx=confirm('Any changes made will be lost !\n\nDo you want to abort now?');
if(mx) { document.location = '/clients/index.cfm?fnc='+func; }
}

function save()
{ hideFields(); }

function showFields()
{
$("thetable").style.display = 'block';
$("saving").style.display = 'none';
}
function hideFields()
{
$("thetable").style.display = 'none';
$("saving").style.display = 'block';
}




/* --------------- */


function viewVehicle(vid)
{ popup('/lister/viewVehicle.cfm?vid='+vid, 790, 550, 0, 0, 1, 1, 0, 0, 'info'); }

function deleteVehicle(vid)
{ popup('/lister/deleteVehicle.cfm?vid='+vid, 420, 300, 0, 0, 1, 1, 0, 0, 'DELETE'); }

function selRow(myObj) {
	myObj.style.backgroundColor="#fbe9c4";
}

function unSelRow(myObj) {
	myObj.style.backgroundColor=myObj.bgColor;
}

function popup(URL, width, height, left, top, scrollbars, statusbar, resizable, modal, windowName, toolbar, menubar)
{
if (!windowName) { 	random_num = (Math.round((Math.random()*99)+1)); windowName = random_num};
if (navigator.appName != "Netscape") {
	if (left == 0) {left = (screen.availWidth/2) - (width/2);}
	if (top == 0) {top = (screen.availHeight/2) - (height/2);}
}
if (modal == 1)
	{
	var features = 'dialogwidth='+width+'px;dialogheight='+height+'px;left='+left+'px;top='+top+'px;toolbar='+toolbar+';status='+statusbar+';menuBar='+menubar+';scrollBars='+scrollbars+';resizable='+resizable
	window.showModalDialog(URL, null, features)
	}
	else
	{
	try { 
		var nw=window.open(URL, windowName, 'width='+width+',height='+height+',left='+left+',top='+top+',toolbar='+toolbar+',status='+statusbar+',menuBar='+menubar+',scrollBars='+scrollbars+',resizable='+resizable);
		}
		catch(e)
		{ alert('Problem opening window '+windowName+ '. Check popups are allowed for this site.'); }
	nw.focus();
	}
}

function windowWidthPercent(percentage) {
	// Will return a percentage value 1-100 as a physical width in pixels
	ns4 = (document.layers)? true:false;
	ie4 = (document.all)? true:false;
	t =  (ns4)? window.innerWidth-16 : document.body.offsetWidth-20;
	t =  (ns4)? window.innerWidth-16 : screen.width;
	t = t * percentage / 100;
	return t;
}	

function windowHeightPercent(percentage) {
	// Will return a percentage value 1-100 as a physical height in pixels
	ns4 = (document.layers)? true:false;
	ie4 = (document.all)? true:false;
	t = (ns4)? window.innerHeight : document.body.offsetHeight;
	t = (ns4)? window.innerHeight : screen.height;
	t = t * percentage / 100;
	return t;
}	

function windowWidth() {
	// Try to obtain the clients screen width
	ns4 = (document.layers)? true:false;
	ie4 = (document.all)? true:false;
	t =  (ns4)? window.innerWidth-16 : document.body.offsetWidth-20;
	t =  (ns4)? window.innerWidth-16 : screen.width;
	return t;
}	

function windowHeight() {
	// Try to obtain the clients screen width
	ns4 = (document.layers)? true:false;
	ie4 = (document.all)? true:false;
	t = (ns4)? window.innerHeight : document.body.offsetHeight;
	t = (ns4)? window.innerHeight : screen.height;
	return t;
}	
	
function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function showUploader(number)
{
	document.getElementById('photoupload'+number).style.display = 'block';
	document.getElementById('photogallery').style.display = 'none';	
}
function showGallery(number)
{
	document.getElementById('photoupload'+number).style.display = 'none';
	document.getElementById('photogallery').style.display = 'block';	
}