// JavaScript Document
//initialize the date picker
$(function() {
	$("#error_box").hide();
	$("#message_box").hide();
	
	$("#event_date").datepicker({minDate: 0, maxDate: '+12M'});


	/* 
	* The function which will do the suggest box
	*/
	/*
	//do the auto suggest
	$("#school").keyup(function(){
		
		var str = jQuery.trim( $(this).val() );
		if (str.length == 0)
			{
			// hide the suggestion box
			$('#suggestions').hide();
			
			} else {
				
				// get data to our php processing page and if there is a return greater than zero
				// show the suggestions box
				$.get("school_suggest.php",{str:str}, function(data){
					
					if (jQuery.trim(data) != 'error'){
														
						if (data.length > 0) {
							$('#suggestions').show();
							$('#autoSuggestionsList').html(data);
							}
					
					} else {
						$('#suggestions').hide();
						}
					
					});	
				}
	
	});
	*/
	
	
	
	/* 
	* function to submit the form
	* and carry out the validation of the 
	* submited form
	*/
	$("#BookingForm").submit(function(){
			
		//validation for the following fields
		var error = "";
				
		//client
		var name = $("#name").val();
		if ( name == "") 
			{
			error += 'Please enter your Name.<br />'; 
			setMsgText('name');
			} else if ( !isValid_alphabets(name) ) {
					error += "Invalid character in Name.<br />"; 
					setMsgText('name');
			} else {
				unsetMsgText('name');				
				}
		
		var address = $("#address").val();
		if ( address == "")
			{
			error += "Please enter your address.<br />";	
			setMsgText('address');	
			} else {
				unsetMsgText('address');
				}
		
		var phone = $("#phone").val();		
		var mobile = $("#mobile").val();		
		if ( (phone =='') && (mobile == '') )
			{
			error +="Please enter either Phone or Mobile Number.<br />"; 	
			setMsgText('phone');	
			} else {
				unsetMsgText('phone');					
				}
		
		
		var email = $("#email").val();	
		if (email =='') {
			error += "Please enter your Email.<br />";
			setMsgText('email');				
			} else if (!isValidEmail(email)) { 
					error += "Invalid email!<br />"; 
					setMsgText('email');						
			} else {
				unsetMsgText('email');										
				}
		
	
		
		//venue
		var v_address = $("#venue").val();
		if ( v_address == "")
			{
			error += "Please enter Venue Address.<br />";	
			setMsgText('venue');	
			} else {
				unsetMsgText('venue');
				}		
		
		var v_zip = $("#venue_postcode").val();
		if (v_zip == "")
			{
			error += "Please enter Venue Post Code.<br />";	
			setMsgText('venue_postcode');	
			} else {
				unsetMsgText('venue_postcode');
				}		
		
		
		var v_phone = $("#venue_phone").val();
		if (v_phone == "")
			{
			error += "Please enter Venue Contact Number.<br />";	
			setMsgText('venue_phone');	
			} else {
				unsetMsgText('venue_phone');
				}			
		
			
		//Event details
		var e_date = $("#event_date").val();
		if (e_date == '')
			{
			error += "Please Select Party Date.<br />";	
			setMsgText('event_date');	
			} else {
				unsetMsgText('event_date');
				}					
				
		var e_name = $("#party_owner").val();
		if (e_name == '')
			{
			error += "Please enter Whose Party is this?<br />";	
			setMsgText('party_owner');	
			} else if ( !isValid_alphabets(e_name) ) {
					error += "Invalid character in Name.<br />"; 
					setMsgText('party_owner');
			} else {
				unsetMsgText('party_owner');				
				}						


		
		var e_no = $("#children_count").val();
		if (e_no =='')
			{
			error += "Please enter No. of Children in the Party.<br />";	
			setMsgText('children_count');							
			} else {
				unsetMsgText('children_count');											
				}
		
		var party_type = $("#party_type").val();
		if (party_type == '0')
			{
			error += "Select Type of Party.<br />";	
			setMsgText('children_count');					
			} else {
				unsetMsgText('children_count');									
				}
		
		//checkboxes & radio
		if( $('input[name="terms"]:checked').size() == 0) 
			{
			error += "You need to agree to our Terms of Service";
			$("#TOS").css('backgroundColor','#FFECEC');
			} else {
				$("#TOS").css('backgroundColor','#FFF');
				}

		
		//CHECK THE payment options
		var found = null;	
		var objval = $('input[name="payment"]');
		for (var x=0; x < objval.length; x++)
			{
			if (objval[x].checked)
				{
				found = objval[x].value;
				} 
			}
			
		if (found == null)
			{
			error +="Please Choose a Payment Option.<br />";
			$("#PAY").css('backgroundColor','#FFECEC');
			} else {
				$("#PAY").css('backgroundColor','#FFF');
			  }
	
		if ( error ) {
				$("#error_box").html( error ).show();
				return false;
			}
		
	});
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
});


//populate the field
//with the clicked value
function fill(thisValue) {
	$('#school').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}


//do ajax to get the values and totals 
//for all the payment fields

//1. Change of Party Type
function getPartyValue(id){

	var partyValue = $("#"+id).val();
	var price;
	var totalPrice;
	if (partyValue != 0)
		{
		if (partyValue == 1) { price = 300; }
		else if (partyValue == 2) { price = 250; }		
		else if (partyValue == 3) { price = 220; }		
		} else {
			price = 0;
			alert("Please select Party Type");
			}
		
	//display the price in the price box
	$("#party_type_value").val(""+ price +"");
return false;
}

//2. On click of the checkbox
//there are two states here
//first when the checkbox is checked
//second when the checkbox is unchecked
function get_optionals(id,unit_price){
	
	
	//get the state of the checkbox after this click
	var checkStatus = $('#'+id).is(':checked'); 
	if (checkStatus)
		{
		//when checked
		//a) enable qty box
		//b) set qty value to 1
		//c) calculate value as qty * price
		//d) display the price
		var price_value = 1 * unit_price; 
		$("#"+id+"_qty").attr('disabled','').val( '1' );
		$("#"+id+"_value").val( ''+ price_value +'' );		
		} else {
			//when not checked
			//a) disable qty box
			//b) set qty value to 0
			//d) display the price to 0		
			$("#"+id+"_qty").attr('disabled','disabled').val( '0' );
			$("#"+id+"_value").val( ' 0 ' );			
			}
	
	checkTotal();
	return false;

}

//3. on Change in the value of the qty field
function getPrice(id,unit_price){

	var ret = id.split("_");
	var nId = ret[0]+"_"+ret[1];


	var qty = $("#"+id).val();
	var nValue = (qty*unit_price).toFixed(2);
	$("#"+nId+"_value").val(""+ nValue +"" );
	
	checkTotal();
	return false;
};

//calculate the total price
function checkTotal(){
	var party_value = $("#party_type_value").val();
	
	//get total count of check boxes
	var optCount = ($("#option_count").val() - 1);

	var total = eval(party_value);
	for (var x=1; x <= optCount; x++)
		{
		var t = "optional_0"+ x +"_value";
		var optValue = $("#"+t).val();
		
		total += eval(optValue);
		
		}

	//var total = eval(party_value)+eval(optValue);
	
	$("#total_value").val( ""+ total.toFixed(2) +"");
	return false;
};


/* function to get the time phase based on hour selection */
function getTimePhase(id){
	
	var hr = $("#"+id).val();
	
	//time band is  10 am - 6  pm
	//thefore if hr is > 6 it is bound to be am
	//else it is bound for pm
	
	if ( ( hr > 6 ) && ( hr != 12) ) {
			//this is AM
			$("#event_phase").val( 'AM' );
		} else {
			// this is PM
			$("#event_phase").val( 'PM' );	
			}
		
	return false;
}


function remove_notice(){
	
	$('#message_box').animate({opacity: 1.0}, 2000)
			.fadeOut('slow', function() {
				$(this).hide();
			});
	
	
	$('#error_box').animate({opacity: 1.0}, 2000)
			.fadeOut('slow', function() {
					$(this).hide();
					});

};


//function to display 
//other details input box for 
//the referrer select field
function checkReferrer(val){
	
	val = jQuery.trim(val);
	var nVal = val.split('-');
	
	if (nVal[1] == '1')
		{
			$("#txtOther").show();
		} else {
			$("#txtOther").hide();
			}
	return false;
}