// Catering javascript file
// update menu , submit menu
// 

function tf_limitInput(t,num,abc,em,ph,uname,des){

    // allowed 
        var Vphone      = "+()- 0123456789";
        var VnumbDes        = "0123456789.";
        var Vnumb       = "0123456789";
        var Valpha      = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
        var VUalpha         = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
        var Valphanum   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789";
        var Vemail      = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.@-_~";   
        var v = Vnumb;
    // triggers
        if (num == 1) { v = Vnumb; }
        if (num == 0 && abc == 1) { v = Valpha; }
        if (num == 1 && abc == 1) { v = Valphanum; }
        if (em == 1) { v = Vemail; }
        if (ph == 1) { v = Vphone }
        if (uname == 1) { v = VUalpha }
        if (des == 1) { v = VnumbDes }
        
    var w = "";
    for (i=0; i < t.value.length; i++) {
        x = t.value.charAt(i);
        if (v.indexOf(x,0) != -1){
            w += x;
        }
    }
    t.value = w;
} 

/* Return value in money formated format ##.## */
function formatCurrency(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 = "0" + 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);
}



var menu = {};

// price, # of people 1-15, 16-30
// IMPORTANT: Must enter two qty!
// ID number : [price1,price2]

menu.items = {
    1 : [38.00,76.00],
    2 : [39.00,78.00],
    3 : [48.00,96.00],
    4 : [43.00,85.00],
    5 : [46.00,92.00],
    6 : [28.00,56.00],
    7 : [28.00,56.00],
    8 : [29.00,58.00],
    9 : [14.00,26.00],       
    10 :[23.00,45.00],
    11 :[42.00,58.00],    
    12 :[12.75,58.00]   
};

// menu checkboxes
menu.cb = $('#catering input:checkbox');

var tf_num_people = $('#tf_num_ppl').val();
$('#tf_num_ppl').bind('keyup', function(){
    tf_limitInput(this,1);
    tf_num_people = parseInt($(this).val());
     menu.update();
    
});

menu.date_set = false; // check if user has set a date?

menu.update = function (obj, id_name){
	 
	 var total_items = 13;
	 
	 var total_checked = 0;
	 
	 for(i = 1; i<total_items;i++)
	 {
	   if (menu.items[i]){
	   	
	   
	 	   var itm = menu.items[i];
	 	   var price_1 = itm[0]; // 12-15 ppl
           var price_2 = itm[1]; // 16-30 ppl
            var txt_p_n = 'txt_p_'+i;
            
            // num people found
            if (tf_num_people >=1 ){
                var t_price = 0;
   
                // 1-15 people
                if (tf_num_people < 16){
                    t_price = price_1;
                }

                // 16 - 30 people
                if (tf_num_people > 15 && tf_num_people < 31){
                    //t_price = price_2;
					 t_price = price_1*2;
                }
                
                // more then 30 people (31-N)
                if (tf_num_people > 30){
                    
                    // each 15 people
                    power = Math.floor( (tf_num_people-1) / 15)
                    
                    // next 15 qty price diff.
                    // example 1 : $38 - $76 ....... [ 1-15 - 16-30 people]
                    // example 2 : $42 - $58 ....... [ 1-15 - 16-30 people]
                   // price_more =  Math.floor( price_2 - price_1);
                    
                    // base price, pluse next 15qty price
                    // example 1 : $38 + ($38 * 2) ....... [ 31-45 people ]
                    // example 2 : $42 + ($16 * 2) ....... [ 31-45 people ] 
                    // t_price = price_1 +  (price_more * power);
                    
					t_price = price_1 + (price_1 * power);;
                }   
               
			  
				 if ( $('[name=id_p_'+ i +']:checked').length){
					 total_checked+=t_price;
				 }
				 
           $('#'+txt_p_n).html( '$' + formatCurrency(t_price) );
	 }
	   }
	 
	 }
	 
	 $('.menu_total_of_checked').html( '$' + formatCurrency(total_checked) ); 
 
};

$('document').ready(function(){ 
	// Menu checkbox click updates price:
	menu.cb.click(function(){
		menu.update();
	});



// Submit button, validate
$('#m_s').bind('submit', function(){
    
    var input_fields = $('input.req,#tf_st,#tf_num_ppl'); // req. inputs 
    
    // trigger validate passed or failed
    var validate_fail = false;
    
    // any empty inputs fields, add style to input to highlight
    input_fields.removeClass('req_missed').each(function(){
        if ($.trim($(this).val()) == ''){
            $(this).addClass('req_missed');
            validate_fail = true;
        }
    });
    
    // check date was set
    // Start date is  
    //if (!menu.date_set){    	
    //	alert('Please make sure you select date and time');
   // 	$('#tf_dt').focus();
   // 	return false;
   // }
if ( $('input.req,#tf_st,#tf_num_ppl').filter('.req_missed').length){
  // $('#menu_area').show('shake', 100);
   $('input.req,#tf_st,#tf_num_ppl').filter('.req_missed:eq(0)').focus().blur().show('pulsate','',150);;
   
}

// Menu checkbox 1 or more?
if (!$('#tbl_menu_list input:checkbox').filter(':checked').length){
    validate_fail = true;
	alert('Please checkbox one or more from menu list');
	return false;
}
														

// number of people:
    var num_ppl = $('#tf_num_ppl').val();
    
     if (num_ppl){
     	num_ppl = parseInt(num_ppl);
     	if (num_ppl < 0){
     		$('#tf_num_ppl').focus();
     		alert('Please number of people attending event (1 or more), then try again.');     		
     		return false;
     	}
     }
         
    if (validate_fail){
        $('#req_missed_toggle').show();
        return false;
    }
    
    $('#req_missed_toggle').hide();
    return ture;    
    
});


// date time,  picker
/*$('#tf_dt').datetime({
					 			 
                                    userLang    : 'en',
                                    americanMode: true
                                });*/
	$("#tf_dt").datepicker();


	$('#tbl_menu_list tr')
	.hover(function(){
										  $(this).addClass('hover');
										  }, 
										  function(){ 
										   $(this).removeClass('hover');
										  })
	.click(function(){
										var cb = $(this).find('input[type=checkbox]:eq(0)');
										if (!cb[0].checked){
											cb.attr('checked', 'checked');											 
										} else {
											cb.removeAttr('checked');
										}
							menu.update();
										  });
	
	$('#tbl_menu_list tr input[type=checkbox]').click(function(){
					var cb = $(this);
										if (!cb[0].checked){
												cb.attr('checked', 'checked');
										} else {
												cb.removeAttr('checked');
										}
	});
	
$('#tf_dtime').timeEntry().change(function() { 
    //  var log = $('#log'); 
    // log.val(log.val() + ($('#defaultEntry').val() || 'blank') + '\n'); 
});

jQuery("#menu_area input").each(function(){
	jQuery(this).watermark( $(this).attr('title' ));
});
 
setTimeout(function(){ jQuery('.slideUp').hide().slideDown(1500, function(){ 
												   $('#tf_num_ppl').focus(); 
												   });  
												   }, 1500);


$('#tf_num_ppl').trigger('keyup');

});
