// configure drop downs
function configure(type)
{
	var section = $("#frm_section").html();

	if(type == 'partnumber')
	{
		var partnumber = $("#fpart").val();
		var catid = $("#catid").val();
		
		$.ajax({
			type: "post",
			url: "/ajax/configure_partnumber.php",
			cache: false,
			data: "&partnumber="+partnumber+"&inline=yes&catid="+catid+"&section="+section,
			success:function(data)
					{	
					
						$("#searchFilterResults").html(data);	
					}
		});
	} else
	{	
		var width = $("#fwidth").val();
		var height = $("#fheight").val();
		var depth = $("#fdepth").val();
		var iprating = $("#frating").val();
		var material = $("#fmaterial").val();
		var catid = $("#catid").val();
		
		$.ajax({
			type: "post",
			url: "/ajax/configure.php",
			cache: false,
			data: "&width="+width+"&height="+height+"&depth="+depth+"&rating="+iprating+"&material="+material+"&inline=yes&catid="+catid+"&section="+section,
			success:function(data)
					{	
					
						$("#searchFilterResults").html(data);	
					}
		});
	}
}

function configure_it(type)
{
	var section = $("#frm_section").html();

	if(type == 'partnumber')
	{
		var partnumber = $("#fpart").val();
		var catid = $("#catid").val();
		
		$.ajax({
			type: "post",
			url: "/ajax/configure_partnumber_it.php",
			cache: false,
			data: "&partnumber="+partnumber+"&inline=yes&catid="+catid+"&section="+section,
			success:function(data)
					{	
					
						$("#searchFilterResults").html(data);	
					}
		});
	} else
	{	
		var width = $("#fwidth").val();
		var height = $("#fheight").val();
		var depth = $("#fdepth").val();
		var iprating = $("#frating").val();
		var material = $("#fmaterial").val();
		var catid = $("#catid").val();
		
		$.ajax({
			type: "post",
			url: "/ajax/configure_it.php",
			cache: false,
			data: "&width="+width+"&height="+height+"&depth="+depth+"&rating="+iprating+"&material="+material+"&inline=yes&catid="+catid+"&section="+section,
			success:function(data)
					{	
					
						$("#searchFilterResults").html(data);	
					}
		});
	}
}

function configure_cooling(type)
{
	var section = $("#frm_section").html();
	
	var width = $("#fwidth").val();
	var height = $("#fheight").val();
	var depth = $("#fdepth").val();
	var noracks = $("#fnoracks").val();
	var kwcab = $("#fkwcab").val();
	var material = $("#fmaterial").val();
	var cooling = $("#cooling").val();	
	
	$.ajax({
		type: "post",
		url: "/ajax/configure_cooling.php",
		cache: false,
		data: "&width="+width+"&height="+height+"&depth="+depth+"&kwcab="+kwcab+"&material="+material+"&noracks="+noracks+"&cooling="+cooling+"&inline=yes&section="+section,
		success:function(data) {		
			$("#searchFilterResults").html(data);	
		}
	});	
	
	coolingDisplay();
}

function coolingDisplay()
{
	if($('#fnoracks').val() > 0) $('#init-hide-1').fadeIn();
	else $('#init-hide-1').fadeOut();
	
	if(($('#fnoracks').val() > 0) && $('#fkwcab').val() > 0) $('.init-hide-2').fadeIn();
	else $('.init-hide-2').fadeOut();
}

function configure_ups(type)
{
	var section = $("#frm_section").html();
	
	var redundancy = $("#fredundancy").val();
	var power = $("#fpower").val();
	var autonomy = $("#fautonomy").val();	
	
	$.ajax({
		type: "post",
		url: "/ajax/configure_ups.php",
		cache: false,
		data: "&redundancy="+redundancy+"&power="+power+"&autonomy="+autonomy+"&inline=yes&section="+section,
		success:function(data) {		
			$("#searchFilterResults").html(data);	
		}
	});	
}

function clearBox(input) {
	if(input.value=input.defaultValue) {
		input.value = '';	
	}
}

function resetBox(input) {
	if(!input.value) {
		input.value = input.defaultValue;	
	}
}

/*
	addQty
	used in the accessory popup. transfers the quantity in to the main quantity box on the page and closes the popup
*/
function addQty(id)
{
	var quantity = $("#popqty"+id).val();
	if(quantity > 0)
	{	/*
		$(".lightbox2").each(function()
   	    {
			$(this).fadeOut();
			$(".fader").fadeOut();
		});		*/
		$('#popup'+id).fadeOut();
		$(".fader").fadeOut();
		
		$("#fqty"+id).val(quantity);
		return false;		
	} else
	{	
		alert("Please enter a valid number for the quantity");
		return false;
	}	
}

/*
	deleteAccessory
	delete an accessory from the final "build quote" step
*/
function deleteAccessory(id)
{
	var conf = confirm("Are you sure you want to delete this accessory?");
	if(conf)
	{
		$.ajax({
			type: "post",
			url: "/ajax/deleteAccessory.php",
			cache: false,
			data: "&id="+id,
			success:function(data)
					{	
						if(data == 'success')
						{
							$("#a-"+id).fadeOut();
						}
					}
		});	
		
	}
	return false;
}

/*
	deleteQuote
	delete a quote from the final "build quote" step
*/
function deleteQuote(id, configtype)
{
	var conf = confirm("Are you sure you want to delete this configuration?");
	if(conf)
	{
		$.ajax({
			type: "post",
			url: "/ajax/deleteQuote.php",
			cache: false,
			data: "&id="+id,
			success:function(data)
					{	
						if(data == 'success')
						{
							$("#quote-"+id).slideUp('normal', function() {
								$(this).remove();	
								
								if(configtype == 'open')
								{								
									var openconfig = parseInt($(".openconfig").html());
									openconfig--;
									$(".openconfig").html(openconfig);
								} else
								{
									var savedconfig = parseInt($(".savedconfig").html());
									savedconfig--;
									$(".savedconfig").html(savedconfig);	
								}															
								checkConfigurations();
						   });
						}
					}
		});
		setTimeout("fadeMessage('<h2>Deleted!</h2>Your Configuration has been deleted!')", 650);
	}
	return false;
}

function deleteAllQuotes(quotes, configtype)
{
	if(quotes)
	{
		var quoteArray = new Array();
		quoteArray = quotes.split(',');		
		
		if(confirm("Are you sure you wish to delete these configurations?"))
		{
			$.ajax({
				type: "post",
				url: "/ajax/deleteQuote.php",
				cache: false,
				data: "&quotes="+quotes,
				success:function(data)
				{
					if(data == 'success')
					{
						var id;
						var i;
						
						for(i=0; i<=quoteArray.length; i++)
						{
							id = quoteArray[i];
							$("#quote-"+id).slideUp('normal', function() {
								$(this).remove();	
								
								if(configtype == 'open')
								{								
									var openconfig = parseInt($(".openconfig").html());
									openconfig--;
									$(".openconfig").html(openconfig);
								} else
								{
									var savedconfig = parseInt($(".savedconfig").html());
									savedconfig--;
									$(".savedconfig").html(savedconfig);	
								}															
								checkConfigurations();
						   });
						}
						
						document.getElementById('removeall').style.display = 'none';
					}
				}
			});
			
			setTimeout("fadeMessage('<h2>Deleted!</h2>Your Configurations have been deleted!')", 650);
		}
	}
	
	return false;
}

// check all the configurations check for quote inclusion
function checkIncs()
{
	var ids = new Array();
	var i = 0;
	$(".incQuote").each(function() {				 						 
		if($(this).is(":checked"))
		{
			ids[i] = $(this).val();
			i++;
		}
	});	
	
	var configurations = ids.join('-');
	$(".configurations").val(configurations);
}

/* -------------------------------------------------------------------------------------------------------------------------------------------- */
// setup machining js
function setupMachining()
{
	$(document).ready(function() {
		// setup any delete rows
		$(".deleteRow").live('click', function() {
			var parent = $(this).parents("div").hasClass("holes");			
			$(this).parents(".row").slideUp('fast', function() {
												// remove from dom
												$(this).remove();

												// check for any button changes
												if(parent)
												{
													checkHoles();
												} else
												{
													checkCutouts();
												}
											});
			return false;
		});
		
		$(".last").live("keydown", function(e) {
			var keyCode = e.which;

			if(keyCode == 9)
			{
				var parent = $(this).parents("div").hasClass("holes");
				if(parent)
				{
					addHole();
					$(".holes").children(".row:last").find("input:first").focus();
					return false;
				} else
				{
					addCutout();
					$(".cutouts").children(".row:last").find("input:first").focus();
					return false;					
				}
			}		
		});
		
		// setup the add hole rows
		$(".add-hole").live('click', function(e) {									
			addHole();
			return false;
		});
		
		// setup the add click rows
		$(".add-cutout").live('click', function(e) {												
			addCutout();
			return false;
		});
	});
}

function addHole()
{
	// add
	var newRow = '<div class="row" style="display: none;"><div class="bg"><span class="indent">Holes Diameter &empty;</span><input type="text" name="diametre[]" class="text-form-machining indent first-item" /><span class="mm">mm</span> <span>Enter Qty</span><input type="text" name="hole_quantity[]" class="text-form-machining last" /><a href="" class="deleteRow"><img src="/images/bg-delete-blue.gif" alt="Delete Hole" /></a></div><a href="#" class="add-more add-hole" style="display: none;"><img src="/images/machining_add_more.png" alt="Add Hole" /></a></div>';
	$(".holes").append(newRow);	
	$(".holes").children(".row:last").slideDown('fast', function() {													 
		// check and move add button if neccessary
		checkHoles();															 
	});
}

function addCutout()
{
	var newRow = '<div class="row" style="display: none;"><div class="bg"><span>Width</span><input type="text" name="width[]" class="text-form-machining first-item" /><span class="mm">mm</span> <span>Height</span><input type="text" name="height[]" class="text-form-machining" /><span class="mm">mm</span> <span>Enter Qty</span><input type="text" name="cutout_quantity[]" class="text-form-machining last" /><a href="" class="deleteRow"><img src="/images/bg-delete-blue.gif" alt="Delete Hole" /></a></div><a href="#" class="add-more add-cutout" style="display: none;"><img src="/images/machining_add_more.png" alt="Add Cutout" /></a></div>';
	$(".cutouts").append(newRow);
	$(".cutouts").children(".row:last").slideDown('fast', function() { 
		// check and move add button if neccessary
		checkCutouts();
	});		
}

function checkHoles()
{
	var numChilds = $(".holes").children().length;
	if(numChilds > 0)
	{
		if($(".holes").children(".row:last").hasClass("active-hole") == false)
		{
			$(".add-hole").fadeOut('fast').parents(".row").removeClass('active-hole');
			$(".holes").children(".row:last").addClass('active-hole').children(".add-hole").fadeIn('fast');
		}
	} else
	{
		$(".hole-head").children(".add-hole").fadeIn('fast');
	}
}

function checkCutouts()
{
	var numChilds = $(".cutouts").children().length;		
	
	if(numChilds > 0)
	{
		if($(".cutouts").children(".row:last").hasClass("active-cutout") == false)
		{
			$(".add-cutout").fadeOut('fast').parents(".row").removeClass('active-cutout');
			$(".cutouts").children(".row:last").addClass('active-cutout').children(".add-cutout").fadeIn('fast');
		}
	} else
	{
		$(".cutout-head").children(".add-cutout").fadeIn('fast');
	}
}

/* -------------------------------------------------------------------------------------------------------------------------------------------- */

function saveConfiguration(id) 
{
	var obj =$("#quote-"+id).find(".form-resume");
	var id = obj.children(".qrpid").val();

	$.ajax({
		type: "post",
		url: "/ajax/saveQuote.php",
		cache: false,
		data: "&id="+id,
		success:function(data)
				{	
					if(data == 'success')
					{
						$("#quote-"+id).slideUp('normal', function() {
							$(this).remove();			   
							var openconfig = parseInt($(".openconfig").html());
							openconfig--;
							$(".openconfig").html(openconfig);
							var savedconfig = parseInt($(".savedconfig").html());
							savedconfig++;
							$(".savedconfig").html(savedconfig);
							checkConfigurations();
						});
					}
				}
	});
	setTimeout("fadeMessage('<h2>Saved!</h2>Your Configuration has been saved!')", 650);
	return false;								  
}
function checkConfigurations()
{
	var count = $(".m").length;
	if(count == 0)
	{
		$("#form-holder").fadeOut();
		$("#no-quote-msg").fadeIn();
	}
}

function saveQuoteDetails()
{
	var ids = $(".configurations").val();
	var idArray = ids.split('-');
	
	for(i=0;i<idArray.length;i++)
	{
		if(idArray[i] == 'undefined')
		{
			continue;
		}		
		var id = idArray[i];
		var obj = $("#quote-"+id).find(".form-resume");
		var quantity = obj.find(".mainqty").val();
		var accessories = new Array();
				
		obj.find(".a-qty").each(function() {			 
			var array = new Array();
			var aid = $(this).attr('id').split('-');
			
			array[0] = aid[1];
			array[1] = $(this).val();
			if(obj.find("#fitted-"+aid[1]).is(":checked"))
			{
				array[2] = 1;
			} else
			{
				array[2] = 0;
			}		
			accessories[i] = array.join("||");
		});
		var accessories = accessories.join("//");		

		$.ajax({
			async: false,
			type: "post",
			url: "/ajax/saveQuoteDetails.php",
			cache: false,
			data: "&id="+id+"&quantity="+quantity+"&accessories="+accessories
		});	
	}
	return true;	
}

function updateQuoteDetails(id, configtype)
{
	var obj = $("#quote-"+id).find(".form-resume");
	var quantity = obj.find(".mainqty").val();
	var accessories = new Array();
		
	obj.find(".a-qty").each(function(i, e) {			 
		var array = new Array();
		var aid = $(this).attr('id').split('-');
		
		array[0] = aid[1];
		array[1] = $(this).val();
		if(obj.find("#fitted-"+aid[1]).is(":checked"))
		{
			array[2] = 1;
		} else
		{
			array[2] = 0;
		}		
		accessories[i] = array.join("||");
	});
	var accessories = accessories.join("//");		
	
	$.ajax({
		async: false,
		type: "post",
		url: "/ajax/saveQuoteDetails.php",
		cache: false,
		data: "&id="+id+"&quantity="+quantity+"&accessories="+accessories+"&return=quote",
		success: function(data) {
			var totals = data.split('-');
			
			$("#box1-"+id).html(totals[0]);
			$("#box2-"+id).html(totals[1]);
			$("#box3-"+id).html(totals[2]);
			$("#box4-"+id).html(totals[3]);
			$("#totalunit-"+id).html("&pound;"+totals[4]);			
			$("#discount-"+id).html("&pound;"+totals[5]);	
			$("#totalnet-"+id).html("&pound;"+totals[6]);
		}
	});		

	setTimeout("fadeMessage('<h2>Updated!</h2>Your Configuration has been updated!')", 650);

	return false;
}

function toggleRightSub(id)
{
	$('.rim-'+id).slideToggle();
}
