/*
//
// Reposition the line stretches to accomodate for browser shape changes
function lineStretch(){
	var height = window.innerHeight;
	
	if (height < 633){
		alert(document.getElementById('leftLineStretchTD').style['vertical-align']);
		
		//document.getElementById('leftLineStretchTD').style['vertical-align'] = "top";
	}
	
	
	setTimeout(lineStretch,4000);	
}
*/


//
// Display the name of a tile in the given target element
function displayTileName(img, targetElement){
	if(document.getElementById(targetElement)){
	
	var obj = document.getElementById(targetElement).firstChild;
	
	obj.nodeValue = img.alt;
	}
	
	//sIFR.redraw();
}


//
// Go back to displaying the type name in the target element
function restoreTypeName(oldTypeName,targetElement){
	
	if(document.getElementById(targetElement)){
	var obj = document.getElementById(targetElement).firstChild;
	
	obj.nodeValue = oldTypeName;
	}
	
	//sIFR.redraw();
}


//
// Show the min left column - done after page has loaded to avoid old scrollbar showing
function enableScrolling(){
	if (document.getElementById('mainContainerLeftColumn'))
	{
		document.getElementById('mainContainerLeftColumn').style.overflow = "auto";
	}
}


//
// Show the min left column - done after page has loaded to avoid old scrollbar showing
function disableScrolling(){
	document.getElementById('mainContainerLeftColumn').style.overflow = "hidden";
}


//
// Do a rollOver
function rollOver(path, img){
	img.src = "images/" + path + "-over.gif";
}


//
// Do a rollOut
function rollOut(path, img){
	img.src = "images/" + path + ".gif";
}


//
// Do a tile rollover
function tileRollOver(tile){
	tile.className = "materialThumbnailOver";	
}


//
// Do a tile rollover
function tileRollOut(tile){
	tile.className = "materialThumbnail";	
}











/*
 *
 * AUTO QUOTE
 * AUTO QUOTE
 * AUTO QUOTE
 * AUTO QUOTE
 * AUTO QUOTE
 * AUTO QUOTE
 *
 */



//
// Restrict the values a user can enter in a field
var phone = "()- 0123456789";
var numb = "0123456789.";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
function res(t,v){
	var w = "";
	for (i=0; i < t.value.length; i++) {
		x = t.value.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
	t.value = w;
}



//
// Validate the AutoQuote form
function validateQuote()
{
	var error = '';
	
	if (document.getElementById('specMaterial').value == '0')
	{
		error = 'Please choose a material to quote';
	}
	else
	if (getMaterialSize() == '')
	{
		error = 'Please choose a material size';
	}
	else
	if (getPieces().length == 0)
	{
		error = 'Please enter some pieces for your worktop';
	}
	else
	if (getTemplateAndFitting() == 0)
	{
		error = 'Please choose a template and fitting option';
	}
	
	
	if (error != '')
	{
		alert(error);
		return false;
	}
	
	return true;
}




//
// Calculate the quote amount
function calculateQuote()
{
	
	
	/*
	 *
	 * CONSTANTS
	 * CONSTANTS
	 * CONSTANTS
	 * CONSTANTS
	 *
	 */
	 
	// Factors
	var MU_MATERIAL = 2;
	var MU_WORK = 2;
	
	// Costs of various cuts
	var COST_LINEAR_METER_CUT = 7.5;
	var COST_CUT_BEVELLED = 10;
	var COST_CUT_HALF_ROUNDED = 30;
	var COST_CUT_BULL_NOSED = 40;
	var COST_CUT_OGEE = 50;

	// Costs of various extras
	var COST_TAP_HOLE = 10;
	var COST_ROUNDED_CORNER = 25;
	var COST_UNDERMOUNTED_SINK = 45;
	var COST_OVERMOUNTED_SINK = 20;
	var COST_CUT_OUT = 20;
	var COST_DRAINER_GROOVE = 40;	
		
	// Cost of templating and fitting
	var COST_TEMPLATING_UP_TO_4_PIECES = 150;	
	var COST_TEMPLATING_5_PIECES_AND_OVER = 180;
	
	var COST_FITTING = 
	{
		London:400,
		Surrey:400,
		Kent:400,
		E_Sussex:400,
		W_Sussex:450,
		Essex:400,
		Hertfordshire:450,
		Buckinghamshire:500,
		Oxfordshire:500,
		Berkshire:500,
		Hampshire:500,
		None:0
	}
	 
	 
	 
	
	/*
	 *
	 * VALIDATION
	 * VALIDATION
	 * VALIDATION
	 * VALIDATION
	 *
	 */
	if (!validateQuote()) return false;
	
	
	
	
	
	/*
	 *
	 * GET VALUES
	 * GET VALUES
	 * GET VALUES
	 * GET VALUES
	 *
	 */
	
	
	//
	// Get the material data
	var id = document.getElementById('specMaterial').value;
	var material = getMaterial(id);
	
	
	//
	// Get the material size
	var size = getMaterialSize();
	
	
	//
	// Get the material price
	var price = getMaterialPrice(material, size);
	
	
	//
	// Get an array of the pieces in the worktop
	var pieces = getPieces();
	
	
	//
	// Get a hash of the edges for the worktop
	var edges = getEdges();
	
	
	//
	// Get an array of the upstands for the worktop
	var upstands = getUpstands();
	
	
	//
	// Get a hash of the extras for the worktop
	var extras = getExtras();
	
	
	//
	// Get the template and fitting value
	var templateAndFitting = getTemplateAndFitting();
	
	
	
	
	
	/*
	 *
	 * CALCULATION
	 * CALCULATION
	 * CALCULATION
	 * CALCULATION
	 *
	 */
	 
	 
	 //
	 // Base cost
	 var costBase = price * MU_MATERIAL;
	 var runningCost = 0;
	 
	// alert("costBase: " + costBase);
	
	 
	 
	 //
	 // Area and side cost
	 var area = 0;
	 var sides = 0;
	 
	 for (var i = 0; i < pieces.length; i++)
	 {
		 area += pieces[i].length * pieces[i].width;
		 sides += pieces[i].length * 2 + pieces[i].width * 2;
	 }
	 
	 var costArea = costBase * area * MU_WORK;
	 var costSides = sides * COST_LINEAR_METER_CUT * MU_WORK;
	 
	// alert("costArea: " + costArea);
	// alert("costSides: " + costSides);
	
	runningCost += costArea;
	runningCost += costSides;
	//alert("runningCost: " + runningCost);
	 
	 
	 //
	 // Edges cost
	 var costEdges = 0;	 
	 costEdges += edges.bevelled * COST_CUT_BEVELLED * MU_WORK;
	 costEdges += edges.bullNosed * COST_CUT_BULL_NOSED * MU_WORK;
	 costEdges += edges.halfRounded * COST_CUT_HALF_ROUNDED * MU_WORK;
	 costEdges += edges.ogee * COST_CUT_OGEE * MU_WORK;
	 
	// alert("costEdges: " + costEdges);
	
	runningCost += costEdges;
	//alert("runningCost: " + runningCost);
	 
	 
	 //
	 // Upstands cost
	 area = 0;
	 
	 for (i = 0; i < upstands.length; i++)
	 {
		 area += upstands[i].length * upstands[i].width;
	 }
	 
	 var costUpstands = costBase * area;
	 
	// alert("costUpstands: " + costUpstands);
	
	runningCost += costUpstands;
	//alert("runningCost: " + runningCost);
	 
	 
	 //
	 // Extras cost
	 var costExtras = 0;
	 
	 costExtras += extras.roundedCorners * COST_ROUNDED_CORNER * MU_WORK;
	// alert("costExtras->roundedCorners: " + (extras.roundedCorners * COST_ROUNDED_CORNER * MU_WORK));
	 
	 costExtras += extras.tapHoles * COST_TAP_HOLE * MU_WORK;
	// alert("costExtras->tapHoles: " + (extras.tapHoles * COST_TAP_HOLE * MU_WORK));
	 
	 costExtras += extras.cutOuts * COST_CUT_OUT * MU_WORK;
	// alert("costExtras->cutOuts: " + (extras.cutOuts * COST_CUT_OUT * MU_WORK));
	 
	 costExtras += extras.undermountedSinks * COST_UNDERMOUNTED_SINK * MU_WORK;
	// alert("costExtras->undermountedSinks: " + (extras.undermountedSinks * COST_UNDERMOUNTED_SINK * MU_WORK));
	 
	 costExtras += extras.overmountedSinks * COST_OVERMOUNTED_SINK * MU_WORK;
	// alert("costExtras->overmountedSinks: " + (extras.overmountedSinks * COST_OVERMOUNTED_SINK * MU_WORK));
	 
	 costExtras += extras.drainerGrooves * COST_DRAINER_GROOVE * MU_WORK;
	// alert("costExtras->drainerGrooves: " + (extras.drainerGrooves * COST_DRAINER_GROOVE * MU_WORK));
	
	 //alert("costExtras: " + costExtras);
	
	runningCost += costExtras;
	//alert("runningCost: " + runningCost);
	 
	 
	 //
	 // Templating cost
	 var costTemplating = 0;
	 
	 if (pieces.length <= 4)
	 {
		 costTemplating += COST_TEMPLATING_UP_TO_4_PIECES;
	 }
	 else
	 {
		 costTemplating += COST_TEMPLATING_5_PIECES_AND_OVER;		 
	 }
	
	runningCost += costTemplating;
	//alert("runningCost: " + runningCost);
	 
	 
	 
	 //
	 // Fitting cost
	 var costFitting = COST_FITTING[templateAndFitting];		
	
	runningCost += costFitting;
	//alert("runningCost: " + runningCost);
	 
	 
	 //
	 // Total cost
	 var totalCost = costArea + costSides + costEdges + costUpstands + costExtras + costTemplating + costFitting;
	 var totalCostRounded = Math.round(totalCost * 100) / 100;
	 
	 var totalCostSegments = (totalCostRounded + '').split('.');
	 
	 if (totalCostSegments[1] != undefined && totalCostSegments[1].length < 2)
	 {
		 totalCostSegments[1] = totalCostSegments[1] + '0';
	 }
	 
	 var totalCostString = totalCostSegments.join('.');
	 
	 
	 
	 //
	 // Display
	 document.getElementById('quoteField').value = '\xA3' + totalCostString + ' + VAT';

	 document.getElementById('discountField').value = totalCost * .70;
	 
	 document.getElementById('autoQuoteDiscountSWF').show(document.getElementById('discountField').value);
	 
	
	 document.doneQuote = true;
	 
	 
	
	
	 // Send the email
     sendQuoteEmail();
	 
	
}



// Send the email with the quote details
function sendQuoteEmail(){
	
	var dataString = $('#autoQuoteForm').serialize();
	//alert (dataString);return false;
	
	dataString += '&buttonSubmit=true&';
	
	$.ajax({
		type: "POST",
		url: "auto-quote-send.php",
		data: dataString,
		success: function() {
		}
	});
	return false;
}



//
// Display the correct swatch for the mateiral which was been selected from the passed drop down list
function displayMaterialSwatch(menu)
{
	var swatchElement = document.getElementById('materialSwatch');
	
	if (menu.value == '0')
	{
		//swatchElement.style.display = 'none';
		swatchElement.src = 'images/white.gif';		
	}
	else
	{
		var material = getMaterial(menu.value);	
		
		if (material.uniqueURL != '')
		{			
			swatchElement.style.display = '';
			swatchElement.src = 'images/materials/' + material.type + '-thumbnails/' + material.uniqueURL + '.jpg';		
		}
		else
		{
			swatchElement.src = 'images/white.gif';	
			swatchElement.style.display = '';			
		}
	}
}





//
// Check the currently selected material is available in the given size
function checkSizeAvailable(radioButton, size)
{
	var menu = document.getElementById('specMaterial');
	var error = '';
	
	if (menu.value == '0')
	{
		error = 'Please select a material before choosing the size you need.';
	}
	else
	{
		var material = getMaterial(menu.value);	
		
		if (material['price'+size] == '' || material['price'+size] == 0)
		{			
			error = 'This material is not available in ' + size;
		}
	}
	
	if (error != '')
	{
		alert(error);
		radioButton.checked = false;
		return false;
	}
	else
	{
		return true;
	}
}




//
// Get a material's value object from the materialData array
function getMaterial(id)
{
	return materialData[id];
}





//
// Get the material size by checking the size options
function getMaterialSize()
{	
	if (document.getElementById('specSize20mm').checked)
	{
		return '20mm';
	}
	else
	if (document.getElementById('specSize30mm').checked)
	{
		return '30mm';
	}	
	
	return '';
}





//
// Get the material price, given the material and the size required
function getMaterialPrice(material, size)
{	
	switch(size)
	{
		case '20mm':
			return material.price20mm;	
		case '30mm':
			return material.price30mm;	
	}	
}



//
// Return an array of the pieces in the worktop
function getPieces()
{
	var MAX_PIECES = 7;
	var pieces = [];
	
	for (var i = 0; i < MAX_PIECES; i++)
	{		
		var length = document.getElementById('specPiecesLength' + i).value;
		var width = document.getElementById('specPiecesWidth' + i).value;
		
		if (length != '' && width != '' && length != 0 && width != 0)
		{
			pieces.push({ length:length, width:width });
		}
	}
	
	return pieces;
}



//
// Return an hash of the edges required
function getEdges()
{
	var edges = {};
	
	var edgeFields = ['specEdgesBevelled', 'specEdgesBullNosed', 'specEdgesHalfRounded', 'specEdgesOgee'];
	var edgeNames = ['bevelled', 'bullNosed', 'halfRounded', 'ogee'];
	
	for (var i = 0; i < edgeFields.length; i++)
	{
		var value = document.getElementById(edgeFields[i]).value;
		
		var edgeName = edgeNames[i];
		
		if (value != '')
		{
			edges[edgeName] = value;
		}
		else
		{
			edges[edgeName] = 0;
		}
	}
	
	return edges;
}



//
// Return an array of the upstands for the worktop
function getUpstands()
{
	var MAX_UPSTANDS = 5;
	var upstands = [];
	
	for (var i = 0; i < MAX_UPSTANDS; i++)
	{		
		var length = document.getElementById('specUpstandsLength' + i).value;
		var width = document.getElementById('specUpstandsWidth' + i).value;
		
		if (length != '' && width != '' && length != 0 && width != 0)
		{
			upstands.push({ length:length, width:width });
		}
	}
	
	return upstands;
}



//
// Return a hash of various extra options
function getExtras()
{
	var extras = {};
	
	var extraFields = ['specExtraNumRoundedCorners', 'specExtraNumTapHoles', 'specExtraNumCutOuts', 'specExtraNumUndermountedSinks', 'specExtraNumOvermountedSinks', 'specExtraNumDrainerGrooves'];
	var extraNames = ['roundedCorners', 'tapHoles', 'cutOuts', 'undermountedSinks', 'overmountedSinks', 'drainerGrooves'];
	
	for (var i = 0; i < extraFields.length; i++)
	{
		var value = document.getElementById(extraFields[i]).value;
		
		var extraName = extraNames[i];
		
		if (value != '')
		{
			extras[extraName] = value;
		}
		else
		{
			extras[extraName] = 0;
		}
	}
	
	return extras;
}



//
// Return the template and fitting value
function getTemplateAndFitting()
{
	var value = document.getElementById('templateAndFitting').value;
	
	return value;
}





/**
 * Validate the submit bit of the AutoQuote form
 */
function validateSubmit()
{
	var error = '';
	
	if (document.doneQuote == undefined)
	{
		error = 'You must complete a quote before submitting';
	}
	
	
	if (error != '')
	{
		alert(error);
		return false
	}
	
	return true;
	
}


/*
 * Make sure an email address is valid
 */
function checkEmail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}







/**
 * Validate the Get A Quote form
 */
function validateGetAQuote()
{
	var error = '';
		
	if (error != '')
	{
		alert(error);
		return false
	}
	
	return true;
	
}


