///////// charset="UTF-8"
/////////
/////////
/////////
/////////
//var root = '/cake/';
//var j = jQuery.noConflict();
var total_ = 0;

function person_day(i){
//		jQuery('body').append('<div style="font-size:300%;">class: '+jQuery('#person'+i+'-class').find('input').eq(0).attr('checked')+'</div>');
	if(!isNaN(i)){
		person_age_ = '';
		if(jQuery('#person'+i+'-age').val() == undefined){
		}else{
			if(!isNaN(toNumber(jQuery('#person'+i+'-age').val()))){
				person_age_ =1;
			}else{
				jQuery('#person'+i+'-age').val('');
			}
		}
		if(person_age_ != ''){
			jQuery('#person'+i+'-day1').removeAttr('disabled');
			jQuery('#person'+i+'-day1').removeClass('disabled');
			jQuery('#person'+i+'-day2').removeAttr('disabled');
			jQuery('#person'+i+'-day2').removeClass('disabled');
			jQuery('#person'+i+'-class').find('input').eq(0).removeAttr('disabled');
			jQuery('#person'+i+'-class').find('input').eq(0).removeClass('disabled');
		}else{
			jQuery('#person'+i+'-day1').val('');
			jQuery('#person'+i+'-lunch1').val('');
			jQuery('#person'+i+'-day2').val('');
			jQuery('#person'+i+'-lunch2').val('');
//			jQuery('#person'+i+'-class').find('input').eq(0).attr({
//				'checked': false
//			});
			jQuery('#person'+i+'-day1').attr({
				'disabled': 'disabled'
			});
			jQuery('#person'+i+'-day1').addClass('disabled');
			jQuery('#person'+i+'-lunch1').attr({
				'disabled': 'disabled'
			});
			jQuery('#person'+i+'-lunch1').addClass('disabled');
			jQuery('#person'+i+'-day2').attr({
				'disabled': 'disabled'
			});
			jQuery('#person'+i+'-day2').addClass('disabled');
			jQuery('#person'+i+'-lunch2').attr({
				'disabled': 'disabled'
			});
			jQuery('#person'+i+'-lunch2').addClass('disabled');
			jQuery('#person'+i+'-class').find('input').eq(0).attr({
				'disabled': 'disabled',
				'checked': false
			});
			jQuery('#person'+i+'-class').find('input').eq(0).addClass('disabled');
		}


		person_class_ = '';
//		if((jQuery('#person'+i+'-class').val() == undefined) || (jQuery('#person'+i+'-class').val() == '')){
//		}else{
//			person_class_ =1;
//		}
		if(jQuery('#person'+i+'-class').find('input').eq(0).attr('checked')){
			person_class_ =1;
		}
		person_day1_ = '';
		if(jQuery('#person'+i+'-day1').val() == undefined){
		}else{
			if(person_age_ == 1){
				person_day1_ =jQuery('#person'+i+'-day1').val();
			}
		}
		person_day2_ = '';
		if(jQuery('#person'+i+'-day2').val() == undefined){
		}else{
			if(person_age_ == 1){
				person_day2_ =jQuery('#person'+i+'-day2').val();
			}
		}
		if(person_day1_ == 'A'){
			total_ += 1500;
			if(person_class_ == 1){
				total_ -= 500;
			}
		}else if(person_day1_ == 'B'){
			total_ += 1000;
			if(person_class_ == 1){
				total_ -= 500;
			}
		}else{
			total_ += 0;
		}
		if(person_day2_ == 'C'){
			total_ += 1500;
			if(person_class_ == 1){
				total_ -= 500;
			}
		}else if(person_day2_ == 'D'){
			total_ += 1000;
			if(person_class_ == 1){
				total_ -= 500;
			}
		}else{
			total_ += 0;
		}
		if((person_day1_ != '') 
			&& (person_day2_ != '')){
			total_ -= 500;
		}
		if(person_day1_ != ''){
			jQuery('#person'+i+'-lunch1').removeAttr('disabled');
			jQuery('#person'+i+'-lunch1').removeClass('disabled');
		}else{
			jQuery('#person'+i+'-lunch1').val('');
			jQuery('#person'+i+'-lunch1').attr({
				'disabled': 'disabled'
			});
			jQuery('#person'+i+'-lunch1').addClass('disabled');
		}
		if(person_day2_ != ''){
			jQuery('#person'+i+'-lunch2').removeAttr('disabled');
			jQuery('#person'+i+'-lunch2').removeClass('disabled');
		}else{
			jQuery('#person'+i+'-lunch2').val('');
			jQuery('#person'+i+'-lunch2').attr({
				'disabled': 'disabled'
			});
			jQuery('#person'+i+'-lunch2').addClass('disabled');
		}
	}
}
function person_lunch(i){
	if(!isNaN(i)){
		if(jQuery('#person'+i+'-lunch1').val() == undefined){
			person_lunch1_ = '';
		}else{
			person_lunch1_ =jQuery('#person'+i+'-lunch1').val();
		}
		if(jQuery('#person'+i+'-lunch2').val() == undefined){
			person_lunch2_ = '';
		}else{
			person_lunch2_ =jQuery('#person'+i+'-lunch2').val();
		}
		if(person_lunch1_ == 'a'){
			total_ += 750;
		}else if(person_lunch1_ == 'b'){
			total_ += 500;
		}else{
			total_ += 0;
		}
		if(person_lunch2_ == 'a'){
			total_ += 750;
		}else if(person_lunch2_ == 'b'){
			total_ += 500;
		}else{
			total_ += 0;
		}
	}
}


function calc_payment(){
	total_ = 0;
	jQuery('#total-payment').attr({
		'disabled': 'disabled'
	});
	for(i=1;i<5;i++){
		person_day(i);
		person_lunch(i);
	}
//	jQuery('#form-1 *:disabled').css({
//		'border': ' solid 1px #ccc',
//		'background': '#ddd'
//	});
	jQuery('#total-payment').val(numberF(total_));
	jQuery('#total-payment-hidden').val(numberF(total_));
}
jQuery(document).ready(function(){
//	jQuery('body').append('<div style="font-size:300%;">booooooooooooooooooooooooo</div>');
	jQuery('#submit-form-1').click(function(){
		jQuery('#total-payment').removeAttr('disabled');	
	});
//	jQuery('#total-payment').after('<input type="hidden" name="total-payment-hidden" value="" />');
//	jQuery('#total-payment').css({
//		'border': ' solid 1px #ddd',
//		'background': '#eee'
//	});
	jQuery('.calc').change(function(){
		calc_payment();
	});
	calc_payment();
});



function toNumber(ns) {
	ns += '';n = ns;
	var rgx = /(\d)(\D)/;
	while (rgx.test(n)) {
		n = n.replace(rgx,'$1');
	}
	return parseFloat(n);
}
function numberF(ns) {
	ns += '';
	x = ns.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


