var package = true;

var support = 0;
var hosting = 0;
var management = 0;

var terms_of_use = false;
var agree_refound = false;
var start_price = false;

$('.package').click(function(){
	$(this).each(function(){
		if ($(this).attr('checked') == true)
		{
			package = true;
		}
	});
	check_purchase();
});

$('#agree_terms').click(function(){
	if ($(this).attr('checked') == true)
	{
		terms_of_use = true;
	}
	else
	{
		terms_of_use = false;
	}
	check_purchase();
});

$('#agree_refound').click(function(){
	if ($(this).attr('checked') == true)
	{
		agree_refound = true;
	}
	else
	{
		agree_refound = false;
	}
	check_purchase();
});

check_purchase = function(){
	if ( package && terms_of_use && agree_refound )
	{
		$('#purchase_button').attr('disabled', false);
	}
	else
	{
		$('#purchase_button').attr('disabled', true);
	}
}

recount_total = function(package){

	if ( package == 'hosted_package' )
	{
		var out = total + management;
	}
	else if( package == 'software_package' )
	{
		var out = total + support + hosting;
	}
	else
	{
		var out = total + support + hosting + management;
	}
	$('#total').html('$'+out);
}

/* services operations */

// select package
$('#hosted_package').click(function(){
	total = start_price = prices['hosted_package']; //new yeat
	management = 0;
	recount_total('hosted_package');
	
	$('#ny_bonus').attr('checked', false);
	$('#management_tr').removeClass('disabled');
	$('#support_tr').addClass('disabled');
	$('#hosting_tr').addClass('disabled');
	
	$('#management_extra').attr('disabled', false);
	$('#hosting_extra').attr('disabled', true).attr('checked', false);
	$('#support_extra').attr('disabled', true).attr('checked', false);
	$('#management_count').attr('disabled', false).removeClass('disabled');
	$('#hosting_count').attr('disabled', true).addClass('disabled').val('');
	$('#support_count').attr('disabled', true).addClass('disabled').val('');
});
$('#software_package').click(function(){
	total = start_price = prices['software_package'];
	support = hosting = 0;
	recount_total('software_package');
	
	$('#ny_bonus').attr('checked', false);
	$('#management_tr').addClass('disabled');
	$('#support_tr').removeClass('disabled');
	$('#hosting_tr').removeClass('disabled');
	
	$('#management_extra').attr('disabled', true).attr('checked', false);
	$('#hosting_extra').attr('disabled', false);
	$('#support_extra').attr('disabled', false);
	$('#management_count').attr('disabled', true).addClass('disabled').val('');
	$('#hosting_count').attr('disabled', false).removeClass('disabled');
	$('#support_count').attr('disabled', false).removeClass('disabled');
});

// bonuses
start_price = total;
$('#ny_bonus').click(function(){
	if ($(this).attr('checked') == true)
	{
		total = total - ((total/100)*25);
		recount_total();
	}
	else
	{
		total = start_price;
		recount_total();
	}
});

// support control
$('#support_count').keyup(function(){
	support = $(this).val() * prices['support'];
	
	if ( $('#support_extra').attr('checked') == true )
	{
		recount_total();
	}
});

$('#support_extra').click(function(){
	support = ($(this).attr('checked') == true) ? $('#support_count').val() * prices['support'] : 0;

	recount_total();
});

// hosting control
$('#hosting_count').keyup(function(){
	hosting = $(this).val() * prices['hosting'];
	
	if ( $('#hosting_extra').attr('checked') == true )
	{
		recount_total();
	}
});

$('#hosting_extra').click(function(){
	hosting = ($(this).attr('checked') == true) ? $('#hosting_count').val() * prices['hosting'] : 0;

	recount_total();
});

// management control
$('#management_count').keyup(function(){
	management = $(this).val() * prices['management'];
	
	if ( $('#management_extra').attr('checked') == true )
	{
		recount_total();
	}
});

$('#management_extra').click(function(){
	management = ($(this).attr('checked') == true) ? $('#management_count').val() * prices['management'] : 0;
	
	recount_total();
});


$(document).ready(function(){
	$("input.numeric").numeric();
});

