$(document).ready(function(){

	// Call price summary slider
	if($("#price_summary").length){
		$("#price_summary").stickyfloat({duration: 400});
	}
	
	// Form value checker
	var max_mailboxes = 100,
		total_mailboxes = $("#buy_process input#mailboxes"),
		form_error = 0,
		mailbox_cost = 0,
		total_cost = 0;
		
		
	// Disabled button when page is first visited
	$("#buy_process input[type=submit]").addClass("disabled").attr("disabled", "disabled");	
		
	// Runs check_input_value function each time a text input box loses focus	
	$("#buy_process input[type=text].prod_opt_qty").bind({
	  keyup: function(){
	  	$("#price_summary .product_totals").delay(500).show(function(){
	  		check_input_values();
	  		calculate_cost();
	  	})	  	
	  }
	});	
	
	// Set core product value
	$("#buy_process input:radio[name=product]").click(function(){
		cost = $(this).siblings(".core_product").val();
		$("#buy_process #cost_core_product").val(cost);
		$("#buy_process #cost_core_product_text").text(cost);
		calculate_cost();
	});
	
	// Set Advanced filtering upgrade option cost
	$("#buy_process input:radio[name=adv_upgrade]").click(function(){
		cost = $(this).siblings(".cost").val();
		$("#buy_process #cost_core_adv_filtering").val(cost);
		calculate_cost();
	});
	
	function calculate_cost(){
		$("#buy_process .prod_opt_row input[type=hidden].cost").each(function(){
			if(total_mailboxes.val() > 0){
				$this = $(this);
				cost = parseFloat($this.val());
				qty = StripNonNumeric(parseInt($this.siblings("input[type=text].prod_opt_qty").val()));
				total_cost += (qty * cost);
			}
		});
		$("#price_summary #monthly_cost").text(FormatAsCurrency(total_cost));
		$("#price_summary #yearly_cost").text(FormatAsCurrency((total_cost*12)));
		total_cost = 0;
	}
	
	// Loops over all text inputs and first checks to see whether the mailbox limit hasn't been exceeded in the main #mailboxes field
	// then checks to see whether all other values are less than this, otherwise it shows a message box.
	 
	function check_input_values(){
		$("#buy_process input[type=text]").not("#basic_exchange").each(function(){
			$this = $(this);
			if($this.prop("id") == "mailboxes"){
				if(parseInt($this.val()) > max_mailboxes){
					item_error = 1;
					form_error = 1;
				}
				else{
					item_error = 0;
				}	
			}
			else{
				if(parseInt($this.val()) > total_mailboxes.val()){
					item_error = 1;
					form_error = 1;
				}
				else{
					item_error = 0;
				}
			}
			if(item_error){
				$this.parent(".prod_opt").addClass("error");
			}
			else{
				$this.parent(".prod_opt").removeClass("error");
			}			
		});

		if(form_error){
			$("#buy_process input[type=submit]").addClass("disabled").attr("disabled", "disabled");	
			$("#price_summary #summary_prod_msg").addClass("error");
		}
		else{
			$("#buy_process input[type=submit]").removeClass("disabled").removeAttr("disabled");
			$("#price_summary #summary_prod_msg").removeClass("error");
		}
		form_error = 0;
	}
	
	//Original:  Cyanide_7 (leo7278@hotmail.com)
	//Web Site:  http://www7.ewebcity.com/cyanide7
	
	//This script and many more are available free online at
	//The JavaScript Source!! http://javascript.internet.com
	
	//Begin
	function FormatAsCurrency(num) {
		  num = num.toString().replace(/\$|\,/g, '')
		  if (isNaN(num)) num = "0";
		  sign = (num == (num = Math.abs(num)));
		  num = Math.floor(num * 100 + 0.50000000001);
		  cents = num % 100;
		  num = Math.floor(num / 100).toString();
		  if (cents < 10) cents = '';
		  else cents = '.' + cents;
		
		  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3))
		  }
		
		  return (((sign) ? '' : '-') + '£' + num + cents);
	}
	//End
	
	// Created by: Justin Barlow | http://www.netlobo.com/
	// This script downloaded from www.JavaScriptBank.com
	function StripNonNumeric(str){
	  str += '';
	  var rgx = /^\d|\.|-$/;
	  var out = '';
	  for( var i = 0; i < str.length; i++ ){
	    if( rgx.test( str.charAt(i) ) ){
	      if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
	             ( str.charAt(i) == '-' && out.length != 0 ) ) ){
	        out += str.charAt(i);
	      }
	    }
	  }
	  return out;
	}
});
