var Mojo={
	init:function(){
		//Link Button IE fix
		$("a button").click(function(){ window.location = $(this).parent('a')[0].href; return false;});
		
		//Change Credit Card Type
		$("select[name='credit_card_type']").change(function(){
			var option = $("select[name='credit_card_type'] option:selected");
			var card = '';
			$("label[for='credit_card_number'] span.input input").each(function(){
				card+=$(this).val();
			});
			if (option.val() == "Amex"){
				$("label[for='credit_card_number'] span.input").html(
					'<input type="text" maxlength="15" size="15" name="credit_card_number[]" value="'+card.substr(0,15)+'"/> '
//					'<input type="text" maxlength="4" size="2" name="credit_card_number[]" value="'+card.substr(0,4)+'"/> '+
//					'<input type="text" maxlength="6" size="4" name="credit_card_number[]" value="'+card.substr(4,6)+'"/> '+
//					'<input type="text" maxlength="5" size="3" name="credit_card_number[]" value="'+card.substr(10,5)+'"/>'
				);
				var cvv2 = $("label[for='cvv2_number'] span.input input").val();
				$("label[for='cvv2_number'] span.input").html('<input class="cardVNo" type="text" size="3" maxlength="4" name="cvv2_number" value="'+cvv2+'" />');
			}else{
				$("label[for='credit_card_number'] span.input").html(
					'<input type="text" maxlength="16" size="16" name="credit_card_number[]" value="'+card+'"/> '
//					'<input type="text" maxlength="4" size="2" name="credit_card_number[]" value="'+card.substr(0,4)+'"/> '+
//					'<input type="text" maxlength="4" size="2" name="credit_card_number[]" value="'+card.substr(4,4)+'"/> '+
//					'<input type="text" maxlength="4" size="2" name="credit_card_number[]" value="'+card.substr(8,4)+'"/> '+
//					'<input type="text" maxlength="4" size="2" name="credit_card_number[]" value="'+card.substr(12,4)+'"/>'
				);
				var cvv2 = $("label[for='cvv2_number'] span.input input").val().substr(0,3);
				$("label[for='cvv2_number'] span.input").html('<input class="cardVNo" type="text" size="3" maxlength="4" name="cvv2_number" value="'+cvv2+'" />');
			}
		});
		//Change Credit Card Number
		$("label[for='credit_card_number'] span.input input").live('keyup',function(event){
			if (String.fromCharCode(event.which) >= '0' && String.fromCharCode(event.which) <= '9') {
				var x = $(this).val();
				if (x.length == $(this).attr('maxlength')) {				
					$(this).next('input').val('').focus();
				}
			}
		});
	}
	
};
var Triple_Your_Calls={
	total:function(){
		var nr_agents=Number($('#agents_number option:selected').val());
		var total_montly_fee = Triple_Your_Calls._subtotalAgents() + 
			Triple_Your_Calls._subtotalOption1() + 
			Triple_Your_Calls._subtotalOption2() + 
			Triple_Your_Calls._subtotalAdmin()+
			Triple_Your_Calls._subtotalNumbers();
		$('#total_monthly_fee').html('$'+total_montly_fee);
		var price_monthly = $('#price_per_login_monthly');
		if (price_monthly.length)
			total_montly_fee = Triple_Your_Calls._subpriceAgents()+ 
				Triple_Your_Calls._subtotalOption1() + 
				Triple_Your_Calls._subtotalOption2() + 
				Triple_Your_Calls._subtotalAdmin()+
			Triple_Your_Calls._subtotalNumbers();
		$('#total_first_month_fee').html('$'+(total_montly_fee + Triple_Your_Calls._subtotalLeadStore()));
	},
	
	montly_fee:function(){
		$('#agents_number_fee').html('$'+Triple_Your_Calls._subtotalAgents());
		$('#digital_call_recording_fee').html('$'+Triple_Your_Calls._subtotalOption1());
		$('#administrator_fee').html('$'+Triple_Your_Calls._subtotalAdmin());
		$('#call_me_feature_fee').html('$'+Triple_Your_Calls._subtotalOption2());
		$('#lead_store_access_fee').html('$'+Triple_Your_Calls._subtotalLeadStore());
		if ($('#nr_numbers_fee').length)
			$('#nr_numbers_fee').html('$'+Triple_Your_Calls._subtotalNumbers());
		Triple_Your_Calls.total();
		$('#agents_number').change(function(event){
			$('#agents_number_fee').html('$'+Triple_Your_Calls._subtotalAgents());
			$('#digital_call_recording_fee').html('$'+Triple_Your_Calls._subtotalOption1());
			$('#call_me_feature_fee').html('$'+Triple_Your_Calls._subtotalOption2());
			Triple_Your_Calls.total();
		});
		if ($('#nr_numbers').length)
			$('#nr_numbers').change(function(event){
				$('#nr_numbers_fee').html('$'+Triple_Your_Calls._subtotalNumbers());
				Triple_Your_Calls.total();
			});
		
		$('#digital_call_recording').click(function(event){
			$('#digital_call_recording_fee').html('$'+Triple_Your_Calls._subtotalOption1());
			Triple_Your_Calls.total();
		});
		$('#administrator').click(function(event){
			$('#administrator_fee').html('$'+Triple_Your_Calls._subtotalAdmin());
			Triple_Your_Calls.total();
		});
		$('#call_me_feature').click(function(event){
			$('#call_me_feature_fee').html('$'+Triple_Your_Calls._subtotalOption2());
			Triple_Your_Calls.total();
		});
		$('#lead_store_access').click(function(event){
			$('#lead_store_access_fee').html('$'+Triple_Your_Calls._subtotalLeadStore());
			Triple_Your_Calls.total();
		});
	},
	_subtotalAgents:function(){
		var nr = $('#agents_number option:selected').val();
		var price_monthly = $('#price_per_login_monthly');
		//var price_monthly = $('#price_per_login');
		if (price_monthly.length==1){
			return nr*Number(price_monthly.val());
		}
		var discount = Number($('input[name=discount]').val());
		if (!discount) discount = 0;
		if (nr < 5 ) return nr*150-nr*discount; 
		if (nr < 10 ) return nr*125-nr*discount;
		return nr*95-nr*discount;
	},
	_subpriceAgents:function(){
		var nr = $('#agents_number option:selected').val();
		var price_monthly = $('#price_per_login_monthly');
		if (price_monthly.length==1){
			return nr*Number(price_monthly.val());
		}
		var discount = Number($('input[name=discount]').val());
		if (!discount) discount = 0;
		if (nr < 5 ) return nr*150-nr*discount; 
		if (nr < 10 ) return nr*125-nr*discount;
		return nr*95-nr*discount;
	},
	_subtotalNumbers:function(){
		if ($('#nr_numbers').length==0)return 0;
		var nr = $('#nr_numbers option:selected').val();
		return (nr-1)*19; 
	},
	_subtotalOption1:function(){
		var nr = $('#agents_number option:selected').val();
		var ignore = $('#ignore_call_recording');
		if (ignore.length!=0&&1*ignore.val()==1)
			return 0;
		return (($('#digital_call_recording').attr('checked'))?nr*25:0);
	},
	_subtotalAdmin:function(){
		return (($('#administrator').attr('checked'))?49:0);
	},
	_subtotalOption2:function(){
		var nr = $('#agents_number option:selected').val();
		return (($('#call_me_feature').attr('checked'))?nr*25:0);
	},
	_subtotalLeadStore:function(){
		return (($('#lead_store_access').attr('checked'))?199:0);
	},
	Form_Validation:function(container, url, f){
		$(container + " .error").html('');
		$(container + " input, "+container+" select, "+container+" textarea").ajaxFileUpload({
		    'url':url,
		    data:{},
		    dataType:'json',
		    success: function(resp, status){
				if (!resp.success){
					$.each(resp.errors, function(k,v){
						$(container + " label[for='"+k+"'] .error").html(v);
		    		});
		    	}
		    	f(resp);
		    }
		});
		//
	},
	_timesClicked :0,
	Send_form:function(container, url){
		$(container+" button[type='submit']").live('click',function(event){
			event.preventDefault();
			if (Mojo._timesClicked>0)return;
			Mojo._timesClicked++;
			var _this = $(this);
			Triple_Your_Calls.Form_Validation(container, url, function(resp){
				if(resp.success){ 
					$(container+" form").append('<input type = "submit" id = "submit" value = "1" name = "'+_this.attr('name')+'" style = "visibility:hidden; height:0; width:0;" />');
					$('#submit').click();
					_this.remove();
					Mojo._timesClicked=0;
				}else{Mojo._timesClicked=0;}
			});
		});
	}
};
(function($){
	$.tip = function($config){
		$('.tooltip').live('mouseover',function(event){
			if ($(this).next('.tip').html()!="") $(this).next('.tip').show();
		}).live('mouseout',function(event){
			$(this).next('.tip').hide();
		}).live('click',function(event){
			event.preventDefault();
		});
	};
})(jQuery);
$(function() {
	Triple_Your_Calls.montly_fee();
	Triple_Your_Calls.Send_form(".order-confirmation", 'sign-up/json-validate-credit-card-form');
	$.tip();
});
