var package = true;

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

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

$('.package').click(function(){
	$(this).each(function(){
		if ($(this).attr('checked') == true)
		{
			package = true;
		}
	});
	
	$('#code_accepted').fadeOut('slow', function(){
		$('#accept_code').fadeIn();
		$('select[name=product] option').show();
	});
	
	support = hosting = management = 0;
	
	$('.add_cont').slideUp('normal');
	$('.add_field').html('');
	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();
});

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

check_purchase = function(){
	if ( package && terms_of_use && agree_refound && agree_req )
	{
		$('#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').fadeOut('normal', function(){
		$(this).html('$'+out).fadeIn('normal')
	});
}

/* 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(){
	var s_plan_id = $('input[name=support_plan]:checked').val();
	support = ($(this).attr('checked') == true) ? support_plans[s_plan_id]['amount'] : 0;
	
	var month = support_plans[s_plan_id]['period'] > 1 ? 'months' : 'month';
	if ( $(this).attr('checked') )
	{
		$('#support_container').slideDown('slow');
		$('#support_price_field').html('(<span class="red"><b>$'+support_plans[s_plan_id]['amount']+' USD</b></span> per <b>'+support_plans[s_plan_id]['period']+'</b> '+month+')').show();
	}
	else
	{
		$('#support_container').slideUp('slow');
		$('#support_price_field').hide();
	}
	
	recount_total();
});

$('#support_container input[type=radio]').click(function(){
	var month = support_plans[$(this).val()]['period'] > 1 ? 'months' : 'month';
	$('#support_price_field').html('(<span class="red"><b>$'+support_plans[$(this).val()]['amount']+' USD</b></span> per <b>'+support_plans[$(this).val()]['period']+'</b> '+month+')').show();
	support = ($(this).attr('checked') == true) ? support_plans[$(this).val()]['amount'] : 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(){
	var h_plan_id = $('input[name=hosting_plan]:checked').val();
	hosting = ($(this).attr('checked') == true) ? hosting_plans[h_plan_id]['amount'] : 0;

	var month = hosting_plans[h_plan_id]['period'] > 1 ? 'months' : 'month';
	if ( $(this).attr('checked') )
	{
		$('#hosting_container').slideDown('slow');
		$('#hosting_price_field').html('(<span class="red"><b>$'+hosting_plans[h_plan_id]['amount']+' USD</b></span> per <b>'+hosting_plans[h_plan_id]['period']+'</b> '+month+')').show();
	}
	else
	{
		$('#hosting_container').slideUp('slow');
		$('#hosting_price_field').hide();
	}

	recount_total();
});

$('#hosting_container input[type=radio]').click(function(){
	var month = hosting_plans[$(this).val()]['period'] > 1 ? 'months' : 'month';
	$('#hosting_price_field').html('(<span class="red"><b>$'+hosting_plans[$(this).val()]['amount']+' USD</b></span> per <b>'+hosting_plans[$(this).val()]['period']+'</b> '+month+')').show();
	hosting = ($(this).attr('checked') == true) ? hosting_plans[$(this).val()]['amount'] : 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(){
	var m_plan_id = $('input[name=management_plan]:checked').val();
	management = ($(this).attr('checked') == true) ? management_plans[m_plan_id]['amount'] : 0;
	
	var month = management_plans[m_plan_id]['period'] > 1 ? 'months' : 'month';
	if ( $(this).attr('checked') )
	{
		$('#management_container').slideDown('slow');
		$('#management_price_field').html('(<span class="red"><b>$'+management_plans[m_plan_id]['amount']+' USD</b></span> per <b>'+management_plans[m_plan_id]['period']+'</b> '+month+')').show();
	}
	else
	{
		$('#management_container').slideUp('slow');
		$('#management_price_field').hide();
	}
	
	recount_total();
});

$('#management_container input[type=radio]').click(function(){
	var month = management_plans[$(this).val()]['period'] > 1 ? 'months' : 'month';
	$('#management_price_field').html('(<span class="red"><b>$'+management_plans[$(this).val()]['amount']+' USD</b></span> per <b>'+management_plans[$(this).val()]['period']+'</b> '+month+')').show();
	management = ($(this).attr('checked') == true) ? management_plans[$(this).val()]['amount'] : 0;
	recount_total();
});

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