function NewWindow(mypage, myname, w, h, scroll) {
	
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+''
	win = window.open(mypage, myname, winprops)
	
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function NewWindow2(mypage, myname, w, h, scroll) {

	if (ukcarlinecheck() == true){
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+''
		win = window.open(mypage, myname, winprops)
		if (parseInt(navigator.appVersion) >= 4) {
			win.window.focus();
		}
	}

}


function NewWindow3(mypage, myname, w, h, scroll) {

	if (innoviacheck() == true){
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+''
		win = window.open(mypage, myname, winprops)
		if (parseInt(navigator.appVersion) >= 4) {
			win.window.focus();
		}
	}

}

function NewWindow4(mypage, myname, w, h, scroll) {

	if (aircraftCheck() == true){
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		
		winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+''
		win = window.open(mypage, myname, winprops)
		
		if (parseInt(navigator.appVersion) >= 4) {
			win.window.focus();
		}
	}

}

function NewWindow5(mypage, myname, w, h, scroll) {

	if (fuelcalccheck() == true){
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		
		winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+''
		win = window.open(mypage, myname, winprops)
		
		if (parseInt(navigator.appVersion) >= 4) {
			win.window.focus();
		}
	}

}


function changeModel() {
	var url = "/inc/getmodels.php?mkid=" + $('mkid').value;
	new Ajax(url, {method: 'get', update: $('lmdl')}).request();
}

function changeTab(div) {
	// Hide All
	$('overview').style.display = 'none';
	$('cont').style.display = 'none';
	$('stats').style.display = 'none';
	
	if ($('overviewtab')) $('overviewtab').className = '';
	if ($('conttab')) $('conttab').className = '';
	if ($('statstab')) $('statstab').className = '';
	
	$(div).style.display = '';
	$(div+'tab').className = 'on';
}

function swapNewsEvent(div) {
	// Hide All
	$('news').style.display = 'none';
	$('events').style.display = 'none';
	
	$('newstab').className = '';
	$('eventstab').className = '';
	
	$(div).style.display = '';
	$(div+'tab').className = 'on';
}

function inputClick(id, defval) {
	val = $(id).value;
	if (val == defval) {
		$(id).value = '';
	}
}

function inputBlur(id, defval) {
	val = $(id).value;
	if (val == '') {
		$(id).value = defval;
	}
}

/* External Calc */
function calcPrice(cur,fixedprice) {
	var pprice = $('pprice').value;
	if (!fixedprice) {
		var offset = $('offset').value;
		var totprice = pprice * offset;
	} else {
		var totprice = pprice;
	}
	var tot = totprice;
	
	totprice = parseFloat(totprice);
	totprice = roundNumber(totprice,2);  //returns 2 d.p.

	// Update Per offset price
	$('indtot').innerHTML = cur+totprice;
	
	tot = parseFloat($('num').value * totprice);
	tot = roundNumber(tot,2);  //returns 2 d.p.
	
	// Update Total Offset Price	
	$('pricetot').innerHTML = cur+tot;
	/*$('offset').value = $('num').value;*/
}

function updateProjPrice(cur) {
	var valarr = $('pid').value.split('|'); 
	var price = valarr[1];
	$('pprice').value = price;
	
	calcPrice(cur,false);
}


function roundNumber(number,decimals) {
	var newString;// The new rounded number
	decimals = Number(decimals);
	if (decimals < 1) {
		newString = (Math.round(number)).toString();
	} else {
		var numString = number.toString();
		if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
			numString += ".";// give it one at the end
		}
		var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
		var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
		var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
		if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
			if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
				while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
					if (d1 != ".") {
						cutoff -= 1;
						d1 = Number(numString.substring(cutoff,cutoff+1));
					} else {
						cutoff -= 1;
					}
				}
			}
			d1 += 1;
		} 
		newString = numString.substring(0,cutoff) + d1.toString();
	}
	if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
		newString += ".";
	}
	var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
	for(var i=0;i<decimals-decs;i++) newString += "0";
	//var newNumber = Number(newString);// make it a number if you like
	return newString; // Output the result to the form field (change for your purposes)
}

function swapRoadTravel(div) {
	// Hide All
	$('compcar').style.display = 'none';
	$('staffcar').style.display = 'none';
	
	$('compcartab').className = '';
	$('staffcartab').className = '';
	
	$(div).style.display = '';
	$(div+'tab').className = 'on';
}

function conf(Message) {
	if (confirm(Message)) {
		return(true);
	}	
	return(false);
}

function busEnergyType() {
	if ($('etype1').checked) {
		$('etknown').style.display = 'block';
		$('etsize').style.display = 'none';
	} else if ($('etype2').checked) {
		$('etknown').style.display = 'none';
		$('etsize').style.display = 'block';
	}
}

/* created for events calculator on federation */
function showinternational(){
	if($('prox').value == 1){	
		$('intcon').style.display = 'none';
	} else {
		$('intcon').style.display = 'block';
	}
}


function showlocal(){
	if($('totalvisitors').value > 0){
		$('localtype').style.display = 'block';
		$('overallcount2').value = '100';
	} else {
		$('localtype').style.display = 'none';
		$('local').value = '';
		$('regional').value = '';
		$('national').value = '';
	}
}

function showoversea(){
	if($('oversea').value > 0){
		$('flighttype2').style.display = 'block';
		$('overallcount').value = '100';
	} else {
		$('flighttype2').style.display = 'none';
		$('shorthaul').value = '';
		$('mediumhaul').value = '';
		$('longhaul').value = '';
	}
}

function countperc(haulbox){
	var tot = 100;
	var subtot = 0;
	
	var shorthaul = 0;
	var mediumhaul = 0;
	var longhaul = 0;
	
	if ($('shorthaul').value.length > 0) { shorthaul = cleanNum($('shorthaul').value); $('shorthaul').value = shorthaul; }
	if ($('mediumhaul').value.length > 0) { mediumhaul = cleanNum($('mediumhaul').value); $('mediumhaul').value = mediumhaul; }
	if ($('longhaul').value.length > 0) { longhaul = cleanNum($('longhaul').value); $('longhaul').value = longhaul; }


	//alert(shaul + "|" + mhaul + "|" + lhaul);
	subtot = shorthaul + mediumhaul + longhaul;

	//alert(subtot);

	/*
	if(overalltot > 0){
		$('overallcount').value = maxlimit - overalltot;
	} else {
		$('overallcount').value = maxlimit - overalltot;
	} */

	if (subtot > tot) {
		var slength = $(haulbox).value.length;
		
		if ($('eventerror').style.display == 'none') {
			$('eventerror').style.display = 'block';
		}

		$(haulbox).value = $(haulbox).value.substring(0, slength - 1);

	} else {
		
		if ($('eventerror').style.display == 'block') {
			$('eventerror').style.display = 'none';
		}
		$('overallcount').value = tot - subtot;
		
		
	}

}




function countperc2(haulbox){
	var tot = 100;
	var subtot = 0;
	
	var local = 0;
	var regional = 0;
	var national = 0;
	
	if ($('local').value.length > 0) { local = cleanNum($('local').value); $('local').value = local; }
	if ($('regional').value.length > 0) { regional = cleanNum($('regional').value); $('regional').value = regional; }
	if ($('national').value.length > 0) { national = cleanNum($('national').value); $('national').value = national; }

	//alert(shaul + "|" + mhaul + "|" + lhaul);
	subtot = local + regional + national;

	//alert(subtot);

	/*
	if(overalltot > 0){
		$('overallcount').value = maxlimit - overalltot;
	} else {
		$('overallcount').value = maxlimit - overalltot;
	} */

	if (subtot > tot) {
		var slength = $(haulbox).value.length;
		
		if ($('eventerror2').style.display == 'none') {
			$('eventerror2').style.display = 'block';
		}

		$(haulbox).value = $(haulbox).value.substring(0, slength - 1);

	} else {
	
		if ($('eventerror2').style.display == 'block') {
			$('eventerror2').style.display = 'none';
		}
		$('overallcount2').value = tot - subtot;
	
	}

}

function cleanNum(num) {
	if (isNaN(num)) {
		var num = new String(num); 
    num = num.replace(/[^0-9]/g, ''); 
		if (num.length == 0) num = 0;
	}
	return parseFloat(num);
}


function cleanDec(id) {	
	var num = document.getElementById(id).value;
	num = num.replace(/[^0-9^.]/g, ''); 
	document.getElementById(id).value = num;
}


function cleanOffsetNum(num) {
	if (isNaN(num)) {
		var num = new String(num); 
    num = num.replace(/[^0-9]/g, ''); 
	}
	if (num.length == 0) num = 0;
	return parseFloat(num);
}

function showstuff(boxid){
	if($(boxid).style.display == "block"){
		$(boxid).style.display = "none";
	} else {
		$(boxid).style.display = "block";
	}
}


function selHomeNews(num) {
	$('homenews1').removeClass('ncurrent');
	$('homenews2').removeClass('ncurrent');
	$('homenews3').removeClass('ncurrent');
	
	$('homenews'+num).addClass('ncurrent');
	
	$('homenewssel1').style.display = 'none';
	$('homenewssel2').style.display = 'none';
	$('homenewssel3').style.display = 'none';
	
	$('homenewssel'+num).style.display = 'block';	
}

function disrow(urow) {
	/* function for ukcarline calc */
	var urow = parseInt(urow);
	var rrow = urow + 1;

	$('row' + rrow).style.display = 'block';
	$('show' + urow).style.display = 'none';
}

function innoviacheck() {
	var err = 0;
	
	if ($('clientname').value == "") {
		alert("Please enter the company name.");
		err = 1;
		return(false);
	}
	
	if ($('email').value == "") {
		alert("Please enter an email address.");
		err = 1;
		return(false);
	}
	
	if ($('num').value == "") {
		alert("Please enter the number of rolls.");
		err = 1;
		return(false);
	}
	
	if (err == 0) {
		return(true);
	} else {
		return(false);
	}
	
}	

function aircraftCheck() {
	/*aircraft calculator error checking on ecertificate preview */
	var err = 0;

	if ($('fname').value == ""){	
		alert("Please enter the company name.");
		err = 1;
		return(false);
	}
	if ($('cdate').value == ""){	
		alert("Please enter the date of charter.");
		err = 1;
		return(false);
	}
	if ($('croute').value == ""){	
		alert("Please enter the charter route.");
		err = 1;
		return(false);
	}
	
	if(err==0){
		return(true);	
	}
	
}

function ukcarlinecheck() {
	/*uk carline error checking on ecertificate preview */
	var err = 0;

	if ($('companyname').value == ""){	
		alert("Please enter the company name.");
		err = 1;
		return(false);
	}
	
	if ($('email').value == ""){	
		alert("Please enter an email address.");
		err = 1;
		return(false);
	}
	
	if ($('dist').value == "") {
		alert("Please enter the distance travelled.");
		err = 1;
		return(false);
	}
	
	if ($('fact').value == "0") {
		alert("Please select a co2 factor.");
		err = 1;
		return(false);
	}
	
	if ($('vreg').value == "") {
		alert("Please enter a vehicle registration number.");
		err = 1;
		return(false);
	}
	
	if (err == 0) {
		return(true);
	} else {
		return(false);
	}
}

function fuelcalccheck() {
	/*uk carline error checking on ecertificate preview */
	var err = 0;
	
	if ($('fname').value == ""){	
		alert("Please enter the Company Name.");
		err = 1;
		return(false);
	}
	
	
	if (err == 0) {
		return(true);
	} else {
		return(false);
	}
}

function stovefilter(field) {
    if (field == 'sregion') {
        $('scountry').style.display = 'none';
        $('sregion').style.display = 'block';
				$('sproject').style.display = 'none';
    }else if (field == 'scountry') {
        $('scountry').style.display = 'block';
        $('sregion').style.display = 'none';
				$('sproject').style.display = 'none';
		}else if (field == 'sproject') {
        $('sproject').style.display = 'block';
        $('sregion').style.display = 'none';
				$('scountry').style.display = 'none';
    }else{
        $('scountry').style.display = 'none';
        $('sregion').style.display = 'none';    
				$('sproject').style.display = 'none';
    }
    $('filtergo').style.display = 'block';
}

function ksdisp(field) {
    if (field == '1') {
        $('kitchensurvey').style.display = 'block';
    }else if (field == '0') {
        $('kitchensurvey').style.display = 'none';
    }else{
        $('kitchensurvey').style.display = 'none';
        $('kitchensurvey').style.display = 'none';    
    }
		$('filtergo').style.display = 'block';
}

function navigation(navName) {	
	if ($('nav-' + navName).style.display == 'block') {
		$('nav-' + navName).style.display = 'none';
		$$('img.' + navName).each(function(img) {
			var src = img.getProperty('src');
			var extension = src.substring(src.lastIndexOf('.'),src.length);
			var imgname = src.substring(src.lastIndexOf('-'),src.length);
			img.setProperty('src',src.replace(imgname, extension));
		});
	}else{
		$('nav-' + navName).style.display = 'block';
		$$('img.' + navName).each(function(img) {
			var src = img.getProperty('src');
			var extension = src.substring(src.lastIndexOf('.'),src.length);
			img.setProperty('src',src.replace(extension,'-minus' + extension));
			//img.setProperty('src',src); });
		});
	}
	

}
