var hingeColours = new Array();
hingeColours[0]="white.png";       // argument to control array's size)
hingeColours[1]="pearl.png";
hingeColours[2]="bisque.png";
hingeColours[3]="brightbrass.png";
hingeColours[4]="antiquebrass.png";
hingeColours[5]="steel.png";
hingeColours[6]="black.png";

var hingeNames = new Array();
hingeNames[0]="White";       // argument to control array's size)
hingeNames[1]="Pearl";
hingeNames[2]="Bisque";
hingeNames[3]="Bright Brass";
hingeNames[4]="Antique Brass";
hingeNames[5]="Stainless Steel";
hingeNames[6]="Black";


var current_page = 0;
var animating = false;
var hiding_left_arrow = false;
var hiding_right_arrow = false;

//this var is set by a script tag mid-html
var custom_finish_id =-10;
var colour_picker_showing = false;

var shutter_panel_showing = true;
var quote_panel_showing = false;
var number_openings = 3;
var shutter_details = new Object();
var openings = new Array();
var costMP = 1;


function page_load()
{
	change_finish_id(finish_id);
	set_hinge_colour(selected_hinge_id);
}

//pulls an image using form values
function get_image()
{
	if (finish_id < 0)
	{
		finish_id = 1;
	}
	if (size_style_id < 0)
	{
		size_style_id = $('#style_size input:radio:checked').val();
	}
	$.post("/products/get_variant_image_url", 
				  {product_id:product_id, finish_value_id: finish_id, size_style_value_id:size_style_id },
				   process,
				   "json" );
}

//displays the shutter image
function process(data)
{
	shutter_details.image = data.image_url;
	$('div#shutterpanel').css('background-image', 'url(/media/images/large/'+data.image_url+')');
}

//when finish changes, get new finish and update image
function change_finish_id(id)
{//does the finish exist? revert to first if not
	if ($('#Finish'+id).length == 0)
	{
		id = $('#colourslider li:first').attr('id').substr(6);
	}
	
	$('#finishname').empty();
	$('#finishname').text($('#Finish'+id).attr('class'));
	$('#customised #shutter-colour').empty;
	$('#customised #shutter-colour').text($('#Finish'+id).attr('class'));
	$('.colourgroup .ticked').remove();
	
	
	
	$('<span class="ticked">&nbsp;</span>').appendTo('#Finish'+id);
	finish_id = id;
	get_image();
	var colour_name = $('#Finish'+id+' img').attr('alt');
	$('.tiltrod').css('background-image', 'url(/skin/purelyshutters/images/customise/rods/'+colour_name+'.png)');
	shutter_details.rod_img = colour_name+'.png';
	
	if (id == custom_finish_id)
	{
		show_colour_picker();
		$('#custom-colour-info').text(' (#ffffff)');
	}
	else
	{
		hide_colour_picker();
		$('#custom-colour-info').empty();
	}
}

//when size/style changes, update image
function update_style_size(id, value)
{
	size_style_id=id;
	
	if (value == "47mm")
	{
		//if hidden rods is selected, we need to disable it
		if ($('input#hidden').is(':checked')) {
			$('input#hidden').attr('checked', false);
			$('input#centre').attr('checked', true);
			set_rod_position('centre');
		}
		$('input#hidden').parent().css('display', 'none');
		$('#incompatible').slideDown();
	}
	else
	{
		//undisable it. Bit wierd, but meh.
		$('input#hidden').parent().css('display', 'inline');
		$('#incompatible').slideUp();
	}
	
	$('#customised #shutter-info').text(value);
	shutter_details.style = value;
	get_image();
}

//when hinge colour changes, update image
function set_hinge_colour(id)
{
	$('#hingename').empty();
	$('#hingename').text(hingeNames[id]);
	$('#customised #hinge-colour').empty();
	$('#customised #hinge-colour').text(hingeNames[id]);
	shutter_details.hinge = hingeNames[id];
	shutter_details.hinge_img = hingeColours[id];
	$('#hingecolour .ticked').remove();
	$('<span class="ticked">&nbsp;</span>').appendTo('#Hinge'+id);
	$('div.hingesleft').css('background-image', 'url(/skin/purelyshutters/images/customise/hinges/'+hingeColours[id]+')');
	$('div.hingesright').css('background-image', 'url(/skin/purelyshutters/images/customise/hinges/'+hingeColours[id]+')');
}

//when rod position changes, update image
function set_rod_position(position)
{
	var capital_letter = position.substr(0,1);
	$('#customised #rod-position').empty();
	$('#customised #rod-position').text(capital_letter.toUpperCase() + position.substr(1));
	shutter_details.rod = capital_letter.toUpperCase() + position.substr(1);
	switch (position)
	{
		case "left":
			$('div.tiltrod').css('margin-left', '-45px');
			$('#custom_rods').css('margin-left', '-45px');
			$('div.tiltrod').css('display', 'block');
			$('#custom_rods').css('display', 'block');
			break;
		case "right":
			if(typeof document.body.style.maxHeight === "undefined")
			{
				//IT'S IE6!
				$('div.tiltrod').css('margin-left', '22px');
				$('#custom_rods').css('margin-left', '22px');
			}
			else
			{
				$('div.tiltrod').css('margin-left', '45px');
				$('#custom_rods').css('margin-left', '45px');
			}
			$('div.tiltrod').css('display', 'block');
			break;
		case "centre":
			$('div.tiltrod').css('margin-left', '0px');
			$('#custom_rods').css('margin-left', '0px');
			$('div.tiltrod').css('display', 'block');
			$('#custom_rods').css('display', 'block');
			break;
		case "hidden":
			$('div.tiltrod').css('display', 'none');
			$('#custom_rods').css('display', 'none');
			break;
	}
}

//moves the 'finish' slider and resets display of arrows, if necessary
function prev_image()
{
	if (animating)
		return;
	
	var num_pages = Math.ceil(number_finishes/6);
	//not first page
	if (current_page != 0)
	{
		animating = true;
		$('#colourslider').animate( {marginLeft: '+=300px'}, 350, $.easing.easeOutExpo(),function() {animating = false;});
		current_page--;
		//dec page
		//if first page
		if (current_page == 0)
		{
			hide_left_arrow();
		}
		if (hiding_right_arrow)
		{
			show_right_arrow();
		}
	}
	/* 
	else
	{
		//looping code, unnecessary
		var end_point = num_pages*-300;
		$('#colourslider').animate( {marginLeft: '+=300px'}, 200, $.easing.easeOutExpo(), function() {  $('#colourslider').css('margin-left', end_point+'px');
																											 function() {animating = false;});
																									  		 });
		current_page = num_pages-1;
	}*/
}
	
//moves the 'finish' slider and resets display of arrows, if necessary
function next_image()
{
	if (animating)
		return;
	
	var num_pages = Math.ceil(number_finishes/6);
	//if not last page
	if (current_page != (num_pages-1))
	{	
		animating = true;
		$('#colourslider').animate( {marginLeft: '-=300px'}, 350, $.easing.easeOutExpo(),function() {animating = false;});
		current_page++;
		//inc page
		//if last page
		if (current_page == (num_pages-1))
		{
			hide_right_arrow();
		}
		//show the left arrow if not visible
		if (hiding_left_arrow)
		{
			show_left_arrow();
		}
	}
	/*
	else
	{
		//looping code, unnecessary
		$('#colourslider').animate( {marginLeft: '-=300px'}, 200, $.easing.easeOutExpo(), function() {  $('#colourslider').css('margin-left', '300px');
																									         $('#colourslider').animate( {marginLeft: '-=300px'},
																											 200,
																											 $.easing.easeOutExpo(),
																											 function() {animating = false;});
																										   });
		current_page = 0;
	}*/
}

//changes display of 'finish' slider left arrow
function hide_left_arrow()
{
	$('#shuttercolour .previous').css('visibility', 'hidden');
	//$('#colourholder').css('margin-left', '33px');
	hiding_left_arrow = true;
}

//changes display of 'finish' slider right arrow
function hide_right_arrow()
{
	$('#shuttercolour .next').css('visibility', 'hidden');
	hiding_right_arrow = true;
}

function show_left_arrow()
{
	$('#shuttercolour .previous').css('visibility', 'visible');
	//$('#colourholder').css('margin-left', '0px');
	hiding_left_arrow = false;
}

function show_right_arrow()
{
	$('#shuttercolour .next').css('visibility', 'visible');
	hiding_right_arrow = false;
}

function go_to_product()
{
	url = jQuery('select.full').val();
	var getvars = '';
	getvars = getvars + "style="+$('#style_size input:checked').val();
	if ($('#rodposition').length != 0)
	{
		getvars = getvars + "&rod="+$('#rodposition input:checked').attr('id');
	}
	getvars = getvars + "&finish="+$('#colourslider span.ticked').parent().attr('id');
	getvars = getvars + "&hinge="+$('#hingecolour span.ticked').parents('li').attr('id');

	location = "/shutters/"+url+"?"+getvars;
}

function show_colour_picker()
{
	if (!colour_picker_showing)
	{
		$('#left_custom_rod').css('display', 'block');
		$('#right_custom_rod').css('display', 'block');
		$('#shuttercolour').animate( {height: '240px'}, 350, $.easing.easeOutExpo());
		colour_picker_showing = true;
	}
}

function hide_colour_picker()
{
	if (colour_picker_showing)
	{
		$('#left_custom_rod').css('display', 'none');
		$('#right_custom_rod').css('display', 'none');
		$('#shuttercolour').animate( {height: '55px'}, 350, $.easing.easeOutExpo());
		colour_picker_showing = false;
	}
}

function toggle_shutter_panel()
{
	if (shutter_panel_showing)
	{
		$('#customisepanel .title').removeClass('on');
		$('#customisepanel .title').addClass('off');
		$('#customisingcontainer').slideUp();
		shutter_panel_showing = false;	
	}
	else
	{
		$('#customisepanel .title').removeClass('off');
		$('#customisepanel .title').addClass('on');
		$('#customisingcontainer').slideDown();
		shutter_panel_showing = true;		
	}
}

function toggle_quote_panel()
{
	if (quote_panel_showing)
	{
		$('#formpanel .title').removeClass('on');
		$('#formpanel .title').addClass('off');
		$('#quotecontainer').slideUp();
		
		quote_panel_showing = false;
	}
	else
	{
		$('#formpanel .title').removeClass('off');
		$('#formpanel .title').addClass('on');
		$('#quotecontainer').slideDown();
		quote_panel_showing = true;
	}
}

function togglePanels()
{
	toggle_shutter_panel();
	toggle_quote_panel();
	lock_quote_buttons();
}

function removePanel(panel_id)
{
	lock_quote_buttons();
	if (number_openings > 1)
	{
		panel_id.parent().animate({height: '0px'}, 140,function()
		{
			panel_id.parent().remove();
			$('#formpanel div.opening_form label').each(function(index){$(this).text("Opening " + (index+1) + ":");})
			number_openings--;
		});
	}
}

function addPanel()
{
	lock_quote_buttons();
	if (number_openings < 20)
	{
		number_openings++;
		$('#formpanel #measurements .add').remove();
		$('#formpanel #measurements').append(
			'<div class="opening_form created" style="display:none;">'+
			'<label>Opening '+number_openings+':</label>'+                       
			'<span class="inputfield">'+
			'<input type="text" class="text opening-x" name="openings" value="Width" onblur="clear_value(this, false);" onclick="clear_value(this, true);"/>'+
			'</span>'+
			'<span class="by">x</span>'+
			'<span class="inputfield">'+
			'<input type="text" class="text opening-y" name="openings" value="Height" onblur="clear_value(this, false);" onclick="clear_value(this, true);"/>'+
			'</span>'+
			'</span><a href="#" class="remove" onclick="removePanel($(this)); return false;">Remove</a></div>'+
			
			'<a class="add" href="#" onclick="addPanel(); return false;">Add another opening</a>'
		);
		$('#formpanel .created').slideDown(140);
		$('.created').removeClass('created');
	}
}

function get_quotes()
{
	$('#validation_fail').css('display', 'none');
	$('#formpanel span.inputfield').css('background', 'none repeat scroll 0 0 #E5E9EE');
	//we know the product id
	//is it fitted
	var hidden_rods = ($('#rodposition input:checked').attr('id') == "hidden");
	var units = $('#unitoflength input:checked').attr('id');
	openings = new Array();
	var numOpenings = 0;
	//used for checking extra cost if custom
	var sColour = $('#shuttercolour span.ticked').parent().attr('class');
	$('#formpanel div.opening_form input').each(function(index){
		var currentOpening = new String();
		currentOpening =  $(this).attr("value");
		openings[numOpenings] = currentOpening;
		numOpenings++;
	});
	
	//for email image gen, we need to send a lot of vars
	$.post("/get_quote", {units: units, colour:sColour, openings: JSON.stringify(openings), hidden_rods:hidden_rods},
				   show_quote,
				   "json" );
}

function show_quote(data)
{
	
	costMP = 1;
	if (data.success)
	{
		
		var id = 0;
		//find the result we're looking for
		shutter_details.other_prices = new Array();		
		if(typeof product_id !== 'undefined')
		{		
			$('#delivery input#supply').attr('checked', false);
			$('#delivery input#fitted').attr('checked', true);
			$('#quote_results #compared').empty();
			for (i in data.prices)
			{
				//we're on a product page, thus having a prod id
				if (data.prices[i].product_id == product_id)
				{
					$('#quote_results div#result span.codd').html('&pound;' + data.prices[i].price);
					shutter_details.price = data.prices[i].price;
				}
				else
				{
					var theClass = (id%2 == 0) ? 'odd' : 'even';
					$('#quote_results #compared').append('<span class="comparison title t'+theClass+'">'+data.prices[i].product_name + ' ' + data.prices[i].category_name +'</span>' +
																   '<span class="comparison cost c'+theClass+'">&pound;'+data.prices[i].price+'</span>');
					shutter_details.other_prices[id] = data.prices[i];
					id++;
				}
			}
			$('#formpanel #nextstep').css('display', 'none');
		}
		else
		{
			$('#formpanel #results_title').addClass('on');
			$('#formpanel #measure_title').removeClass('on');
			$('#formpanel #options').slideUp();	
			$('#quote_results #costperrange').empty();
			for (i in data.prices)
			{
				$('#quote_results #costperrange').append('<div class="range">'+
														'<a class="customiselink" style="height: auto; width: auto; background:none; margin: 0px;" title="Personalise the '+data.prices[i].product_name+' Range" href="/shutters/view/'+data.prices[i].product_info.product_fname+'">'+
														 '<img src="/media/images/product_default/'+data.prices[i].product_info.image_url+'" alt="Range"></a>' +
														 '<p>'+data.prices[i].product_name + ' ' + data.prices[i].category_name +
														 '<span class="material">'+data.prices[i].product_info.value_name+'</span>'+
														 '<span class="rangecost">&pound;'+data.prices[i].price+'</span>'+
														 '</p><a class="customiselink" title="Personalise the '+data.prices[i].product_name+' Range" href="/shutters/view/'+data.prices[i].product_info.product_fname+'">'+
														 '<span>Personalise this range</span></a></div>');
				shutter_details.other_prices[id] = data.prices[i];
				id++;
			}
			if ($('#delivery input#supply').attr('checked'))
			{
				change_delivery('supply');
			}
			$('#formpanel #quote_results').slideDown();
			$('#quote_results #costperrange').append('<br style="clear:both;"/>');
			
		}

		if ($('#delivery input:checked').attr('id') == "fitted")
		{
			$('#quote_results #compared').append('<p class="costtagline">* Prices include discounts, VAT and all fitting costs.</P>');
		}
		else
		{
			$('#quote_results #compared').append('<p class="costtagline">* Prices include discounts and VAT.</P>');
		}
		//$('#quote_results').css('display', 'block');
		$('#quote_results').slideDown();	
		
		
	}
	else
	{
		$('#validation_fail').css('display', 'block');
		$('#formpanel span.inputfield').each(function(index){
			if (!data.valid_values[index])
			{
				$(this).css('background', 'none repeat scroll 0 0 #9F066C');
			}
		});
	}
}

function clear_value(obj,isClick)
{
	if (isClick)
	{
		if(obj.value == 'Width' ||
			obj.value == 'Height') 
		{
			obj.value='';
		}
	}
	else
	{
		if(obj.value.length == 0)
		{
			if ($(obj).hasClass('opening-x'))
				obj.value='Width';
			else
				obj.value='Height';
			
		}
	}	
	lock_quote_buttons();
}

function change_delivery(delivery)
{
	costMP = 1; //1x cost for fitted shutters
	if (delivery == 'supply')
	{
		costMP = 0.8; //20% discount for supply only
	}
	
	var id = 0;
	if(typeof shutter_details.price !== 'undefined')
	{
		$('#quote_results #compared').empty();
		var price = (shutter_details.price*costMP);
		
		$('#quote_results div#result span.codd').html('&pound; ' + price.toFixed(2));
		for (i in shutter_details.other_prices)
		{
			price = (shutter_details.other_prices[i].price*costMP);
			var theClass = (id%2 == 0) ? 'odd' : 'even';
			$('#quote_results #compared').append('<span class="comparison title t'+theClass+'">'+shutter_details.other_prices[i].product_name
															+ ' ' + shutter_details.other_prices[i].category_name +'</span>' +
														   '<span class="comparison cost c'+theClass+'">&pound; '+price.toFixed(2)+'</span>');
			id++;
		}
	}
	else
	{
		$('#quote_results #costperrange').empty();
		for (i in shutter_details.other_prices)
		{
			var price = (shutter_details.other_prices[i].price*costMP);
			$('#quote_results #costperrange').append('<div class="range"><img src="/media/images/product_default/'+shutter_details.other_prices[i].product_info.image_url+'" alt="Range">' +
													 '<p>'+shutter_details.other_prices[i].product_name + ' ' + shutter_details.other_prices[i].category_name +
													 '<span class="material">'+shutter_details.other_prices[i].product_info.value_name+'</span>'+
													 '<span class="rangecost">&pound; '+price.toFixed(2)+'</span>'+
													 '</p><a class="customiselink" title="Personalise the '+shutter_details.other_prices[i].product_name+' Range" href="/products/view/'+					
													 shutter_details.other_prices[i].product_info.product_fname+'"><span>Personalise this range</span></a></div>');
			id++;
		}
		$('#quote_results #costperrange').append('<br style="clear:both;"/>');
	}

	if ($('#delivery input:checked').attr('id') == "fitted")
	{
		$('#quote_results #compared').append('<p class="costtagline">* Prices include discounts, VAT and all fitting costs.</P>');
	}
	else
	{
		$('#quote_results #compared').append('<p class="costtagline">* Prices include discounts and VAT.</P>');
	}	
}

//if the user changes a value, they need to update the quote before submitting
function lock_quote_buttons()
{
	if ($('#quote_results').css('display') != 'none')
	{
		$('#quote_results').slideUp();
		$('#formpanel #nextstep').css('display', 'block');	
	}
}

function show_save_form()
{
	$('#quote_results #submitquote').css('display', 'none');
	//$('#quote_results #submitquote').css('height', '0px');
	$('#quote_results #savequote').slideDown();
	$('a.submitquote').removeClass('submitquoteon');
	$('a.savequote').addClass('savequoteon');
}

function show_submit_form()
{
	$('#quote_results #savequote').css('display', 'none');
	//$('#quote_results #savequote').css('height', '0px');
	$('#quote_results #submitquote').slideDown();
	$('a.savequote').removeClass('savequoteon');
	$('a.submitquote').addClass('submitquoteon');
}

function submit_quote()
{
	shutter_details.size = $('#unitoflength input:checked').attr('id');
	shutter_details.finish = $('#shutter-colour').text();
	
	//URL BUILDING
	url = jQuery('select.full').val();
	var getvars = '';
	getvars = getvars + "style="+$('#style_size input:checked').val();
	if ($('#rodposition').length != 0)
	{
		getvars = getvars + "&rod="+$('#rodposition input:checked').attr('id');
	}
	getvars = getvars + "&finish="+$('#colourslider span.ticked').parent().attr('id');
	getvars = getvars + "&hinge="+$('#hingecolour span.ticked').parents('li').attr('id');	
	shutter_details.url = "/shutters/"+url+"?"+getvars;
	//URL DONE
	
	if (shutter_details.finish == "Custom")
	{
		shutter_details.finish = shutter_details.finish + $('#custom-colour-info').text();
	}
	$('#submitquote .sending').slideDown();
	var email_address = ($('#submitquote #submitemail').attr('value') == 'Please enter your email address') ? '' : $('#submitquote #submitemail').attr('value') ;
	var name = ($('#submitquote #name').attr('value') == 'Please enter your name') ? '' : $('#submitquote #name').attr('value') ;
	var address = ($('#submitquote textarea#address').val() == 'Please enter your address') ? '' : $('#submitquote #address').val() ;
	var phone = ($('#submitquote #phone').attr('value') == 'Please enter your phone number') ? '' : $('#submitquote #phone').attr('value');
	//this is the only one we want to ignore if not set
	var comments = ($('#submitquote textarea#comment').val() == 'Please add any comments or queries you have') ? '' : $('#submitquote #comment').val();
	var shutter_dupe = shutter_details;
	//store price
	var tempPrices = new Array();
	var tempPrice;
	for (i in shutter_dupe.other_prices)
	{
		tempPrices[i] = shutter_dupe.other_prices[i].price;
		shutter_dupe.other_prices[i].price*=costMP;
		shutter_dupe.other_prices[i].price = shutter_dupe.other_prices[i].price.toFixed(2);
	}
	if(typeof shutter_details.price !== 'undefined')
	{
		tempPrice = shutter_dupe.price;
		shutter_dupe.price *= costMP;
		shutter_dupe.price = shutter_dupe.price.toFixed(2);
	}
	
	var heardfrom = $('#submitquote #heardfrom').attr('value') ;
		
	var fitted = (costMP == 1);
	$.post("/submit_quote", 
				  {name:name, address:address, phone:phone, heardfrom:heardfrom, email_address:email_address, comments:comments, openings:JSON.stringify(openings),
				   fitted:fitted, shutter:JSON.stringify(shutter_dupe)}, submit_process, "json" );
				   
	//bug fixing, we now need to UNDO the price changes we just made
	for (i in shutter_dupe.other_prices)
	{
		shutter_dupe.other_prices[i].price = tempPrices[i];
	}
	if(typeof shutter_details.price !== 'undefined')
	{
		shutter_dupe.price = tempPrice;
	}
}

function submit_process(data)
{
	$('#submitquote .save-error').css('display', 'none');
	$('#submitquote .sending').stop(true, true);
	$('#submitquote .sending').css('display', 'none');
	if (data.success == true)
	{
		$('#submitquote .save-error').css('display', 'none');
		$('#submitquote .inputfield').css('background', 'none repeat scroll 0 0 #E5E9EE');
		
		$('#submitquote .form_data').css('display', 'none');
		$('#submitquote .form_thanks').slideDown(); //.css('display', 'block');
		_gaq.push(['_trackPageview', 'survey_request_complete.php']);
	}
	else
	{	
		$('#submitquote .save-error').empty();
		$('#submitquote .inputfield').css('background', 'none repeat scroll 0 0 #E5E9EE');
		if (data.error_name != '')
		{
			$('#submitquote .save-error').append(data.error_name);
			$('#submitquote #namefield').css('background', 'none repeat scroll 0 0 #9F066C');
		}
		if (data.error_email != '')
		{
			$('#submitquote .save-error').append(data.error_email);
			$('#submitquote #emailfield').css('background', 'none repeat scroll 0 0 #9F066C');
		}
		if (data.error_phone != '')
		{
			$('#submitquote .save-error').append(data.error_phone);
			$('#submitquote #phonefield').css('background', 'none repeat scroll 0 0 #9F066C');
		}
		if (data.error_address != '')
		{
			$('#submitquote .save-error').append(data.error_address);
			$('#submitquote #addressfield').css('background', 'none repeat scroll 0 0 #9F066C');
		}
		$('#submitquote .save-error').slideDown();	
	}
}


function new_quote()
{
	$('#submitquote .form_data').slideDown(); //.css('display', 'block');
	$('#submitquote .form_thanks').css('display', 'none');
}

function save_quote()
{
	shutter_details.size = $('#unitoflength input:checked').attr('id');
	shutter_details.finish = $('#shutter-colour').text();
	if (shutter_details.finish == "Custom")
	{
		shutter_details.finish = shutter_details.finish + $('#custom-colour-info').text();
	}
	
	//URL BUILDING
	url = jQuery('select.full').val();
	var getvars = '';
	getvars = getvars + "style="+$('#style_size input:checked').val();
	if ($('#rodposition').length != 0)
	{
		getvars = getvars + "&rod="+$('#rodposition input:checked').attr('id');
	}
	getvars = getvars + "&finish="+$('#colourslider span.ticked').parent().attr('id');
	getvars = getvars + "&hinge="+$('#hingecolour span.ticked').parents('li').attr('id');	
	shutter_details.url = "/shutters/"+url+"?"+getvars;
	//URL DONE
	
	$('#savequote .sending').slideDown();
	var email_address = ($('#savequote #sendemail').attr('value') == 'Please enter your email address') ? '' : $('#savequote #sendemail').attr('value');
	var shutter_dupe = shutter_details;
	
	var tempPrices = new Array();
	var tempPrice;
	for (i in shutter_dupe.other_prices)
	{
		tempPrices[i] = shutter_dupe.other_prices[i].price;
		shutter_dupe.other_prices[i].price*=costMP;
		shutter_dupe.other_prices[i].price = shutter_dupe.other_prices[i].price.toFixed(2);
	}
	if(typeof shutter_details.price !== 'undefined')
	{
		tempPrice = shutter_dupe.price;
		shutter_dupe.price *= costMP;
		shutter_dupe.price = shutter_dupe.price.toFixed(2);
	}
	var fitted = (costMP == 1);
	$.post("/save_quote", {email_address:email_address, openings:JSON.stringify(openings), fitted:fitted, shutter:JSON.stringify(shutter_dupe)}, save_process, "json" );
	
	//bug fixing, we now need to UNDO the price changes we just made
	for (i in shutter_dupe.other_prices)
	{
		shutter_dupe.other_prices[i].price = tempPrices[i];
	}
	if(typeof shutter_details.price !== 'undefined')
	{
		shutter_dupe.price = tempPrice;
	}
}


function save_process(data)
{
	$('#savequote .sending').stop(true, true);
	$('#savequote .sending').css('display', 'none');
	if (data.success == true)
	{
		$('#savequote .save-error').css('display', 'none');
		$('#savequote .inputfield').css('background', 'none repeat scroll 0 0 #E5E9EE');
		
		$('#savequote .form_data').css('display', 'none');
		$('#savequote .form_thanks').slideDown(); //.css('display', 'block');
		_gaq.push(['_trackPageview', 'quote_save_complete.php']);
	}
	else
	{
		$('#savequote .save-error').slideDown();
		$('#savequote .inputfield').css('background', 'none repeat scroll 0 0 #9F066C');
	}
}

function new_save()
{
	$('#savequote .form_data').slideDown(); //('display', 'block');
	$('#savequote .form_thanks').css('display', 'none');
}
