function calculateTiles(form, outputdiv, tile_width, tile_height, quantity, alt){
	// Alt for the alternative tile page - different output
	if (alt == undefined){
		alt=0;
	}
	$('#'+outputdiv).load("/default/betterbathrooms/tile_calculator.php?tile_width="+tile_width+"&tile_height="+tile_height+"&quantity="+quantity+"&alt="+alt+"&"+$('#'+form).serialize(), {}, function(){});
        var res = document.getElementById(outputdiv);
	res.style.display = '';
	return false;
}

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}

function splashDiv(div)
{
	var el = document.getElementById(div);
	el.style.display = 'block';
	el.style.marginTop = '0px';
	el.style.marginLeft = 'auto';
	var bl = document.getElementById('blanket');
	bl.style.display = 'block';
}

function closeDiv(div)
{
	var el = document.getElementById(div);
	el.style.display = 'none';
	var bl = document.getElementById('blanket');
	bl.style.display = 'none';
}

function calculateTotalCost(box_price,total_boxes){
	return formatAsMoney(box_price * total_boxes);
}


/* These two functions are probably redundant but leaving them here for now in case not having them breaks anything */
function calculatem2ToBoxes(m2,w,h,n){
	boxes = m2 / w*h*n;
	return Math.ceil(boxes/10);
}


function calculateBoxesTom2(boxes,w,h,n){
	boxes = w*h/n*boxes;
	return Math.ceil(boxes/100);
}
/** *** **/


function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

$(document).ready(function(){
        $('.m2toboxes_m2').keyup(function(){m2convert(this)});
        $('.boxestom2_boxes').keyup(function(){boxesconvert(this)});
        });

/* Tile calculator */
function m2convert(val){
        $('.m2toboxes_boxes, .boxestom2_boxes').val( m2toboxes( $(val).val() ) );
        $('#calculator_total').text ( '£'+formatAsMoney(parseFloat($('#subTotalValue1').html()) * $('.m2toboxes_boxes').val()) );
        $('#calculator_quant').val( $('.m2toboxes_boxes').val() );
        $('.m2toboxes_m2, .boxestom2_m2').val($(val).val());
}

function boxesconvert(val){
        $('.boxestom2_m2, .m2toboxes_m2').val( boxestom2( $(val).val() ) );
        $('#calculator_total').text ( '£'+formatAsMoney(parseFloat($('#subTotalValue1').html()) * $(val).val()) );
        $('#calculator_quant').val( $(val).val() );
        $('.boxestom2_boxes, .m2toboxes_boxes').val($(val).val());
}

function m2toboxes(m2){
        var w = $('#width').val();
        var h = $('#height').val();
        var n = $('#tpp').val();
        if (w && h){
                var tpp = (w*h*n)/10000;
        } else {
                var tpp = 1;
        }
        return Math.ceil(m2 / tpp);
}

function boxestom2(m2){
        var w = $('#width').val();
        var h = $('#height').val();
        var n = $('#tpp').val();

        if (w && h){
                var tpp = (w*h*n)/10000;
        } else {
                var tpp = 1;
        }
        return Math.floor(m2 * tpp);
}

//Get the coverage of a box (used to work out the cost of a tile per m2)
function getCoverage(){
        var w = $('#width').val();
        var h = $('#height').val();
        var n = $('#tpp').val();

	var c = (w * h * n)/10000;
	return c;
}

//Get the cost of the tile per m2
function getPricePerm2(){
	var ppm2 = $('#orig_cost').val() / getCoverage();
	return ppm2;
}

//Update m2 values on site
function updateppm2(extracost, vatrate){
	var ppm2 = (parseFloat($('#orig_cost').val()) + (parseFloat(extracost) * parseFloat(vatrate))) / getCoverage();
	$('.ppm').text(formatAsMoney(ppm2));
}

