$(document).ready(function() { 

	_size = 'five';

	fiveInchArray = Array(0);
	sixInchArray = Array(0);

	// 5 inch prices
	fiveInchArray[0] = 39;
	fiveInchArray[1] = 5;
	fiveInchArray[2] = 25;
	fiveInchArray[3] = 50;
	fiveInchArray[4] = 25;
						
	// 6 inch prices
	sixInchArray[0] = 44;
	sixInchArray[1] = 7;
	sixInchArray[2] = 25;
	sixInchArray[3] = 50;
	sixInchArray[4] = 25;
	
	arrPrices = Array(0);
	arrPrices = CopyArray(eval(_size + 'InchArray'));
})

	function loadPrices(size){
		  _size = size;
		  updateLabels(eval(_size + 'InchArray'));
		  priceCalculator();
	}
	

	function updateLabels(arr){
		  $('#meterslabel').fadeOut('medium', function(){
		  	$('#meterslabel').html('@ $' + arr[0] + ' per metre').fadeIn('medium');
		  });
		  $('#bracketslabel').fadeOut('medium', function(){
		  	$('#bracketslabel').html('@ $' + arr[1] + ' per bracket').fadeIn('medium');
		  });
		  $('#endcapslabel').fadeOut('medium', function(){
		  	$('#endcapslabel').html('@ $' + arr[2] + ' each').fadeIn('medium');
		   });	
		  $('#holeslabel').fadeOut('medium', function(){
		  	$('#holeslabel').html('@ $' + arr[3] + ' each').fadeIn('medium');
		   });
		  $('#cornerslabel').fadeOut('medium', function(){
		  	$('#cornerslabel').html('@ $' + arr[4] + ' each').fadeIn('medium');
		   });	
	}
	
	function validateForm() {
		var d = document;
		var errMsg = "";
		var regex = /^(\d{1,8})$/;
		var regexDecimal = /^[0-9\.]{1,8}$/;
		
		if(!regexDecimal.test(d.forms['priceForm']['metres'].value) || 
			!regex.test(d.forms['priceForm']['brackets'].value) || 
			!regex.test(d.forms['priceForm']['endcaps'].value) ||
			!regex.test(d.forms['priceForm']['corners'].value) ||
			!regex.test(d.forms['priceForm']['holes'].value)) {
			d.forms['priceForm']['total'].value = 0;
			return false;
		}
		else {
			return true;
		}
	}
	
	function priceCalculator() {
	    var d = document;
		var totalValue = 0;
		var metreRate = eval(_size + 'InchArray')[0];
		var bracketRate = eval(_size + 'InchArray')[1];
		var endcapRate = eval(_size + 'InchArray')[2];
		var holesRate = eval(_size + 'InchArray')[3];
		var cornersRate = eval(_size + 'InchArray')[4];
		
		if(validateForm()) {
			totalValue = 	(parseFloat(d.forms['priceForm']['metres'].value) * metreRate)
							+ (parseFloat(d.forms['priceForm']['brackets'].value) * bracketRate)
							+ (parseFloat(d.forms['priceForm']['endcaps'].value) * endcapRate)
							+ (parseFloat(d.forms['priceForm']['holes'].value) * holesRate)
							+ (parseFloat(d.forms['priceForm']['corners'].value) * cornersRate);
		
		    // Display the total rounded to two decimal places
		    d.forms['priceForm']['total'].value = round_decimals(totalValue, 2);
		 }
		 else {
		 	alert('Please enter only numbers in the form fields (decimals are only allowed in the metre box)');
		 	return false;
		 }
	}
	
	function round_decimals(original_number, decimals) {
	    var result1 = original_number * Math.pow(10, decimals)
	    var result2 = Math.round(result1)
	    var result3 = result2 / Math.pow(10, decimals)
	    return pad_with_zeros(result3, decimals)
	}
	
	function pad_with_zeros(rounded_value, decimal_places) {
	
	    // Convert the number to a string
	    var value_string = rounded_value.toString()
	    
	    // Locate the decimal point
	    var decimal_location = value_string.indexOf(".")
	
	    // Is there a decimal point?
	    if (decimal_location == -1) {
	        
	        // If no, then all decimal places will be padded with 0s
	        decimal_part_length = 0
	        
	        // If decimal_places is greater than zero, tack on a decimal point
	        value_string += decimal_places > 0 ? "." : ""
	    }
	    else {
	
	        // If yes, then only the extra decimal places will be padded with 0s
	        decimal_part_length = value_string.length - decimal_location - 1
	    }
	    
	    // Calculate the number of decimal places that need to be padded with 0s
	    var pad_total = decimal_places - decimal_part_length
	    
	    if (pad_total > 0) {
	        
	        // Pad the string with 0s
	        for (var counter = 1; counter <= pad_total; counter++) 
	            value_string += "0"
	        }
	    return value_string
	} 
	
	function CopyArray(a)
	{
	  //Create a new array with the old ones length
	  var n = new Array(a.length);
	
	  //Just copy all the values from the old to the new
	  for(var i = 0; i < a.length; i++)
	    n[i] = a[i];
	  
	  return n;
	}


	