var ticketHandler = {

	cacheTimeOut 		: 60000, 	// 60 seconds before reload of timeslot
	currentYearMonth 	: '', 		// format: YYYY-mm
	arrTimeSlotDays 	: [],		// hold days that have timeslots, index by currentYearMonth
	timeSlots 			: [],		// hold timeSlots dictionary, index by currentYearMonth
	dateTimeLastFetch 	: [],		// when was the last fetch. used to check cache age
	onSubmitValue		: false,	//
	maxNumTickets		: 25,		//
	ticketTypes			: [],
	isLoadingDatePicker	: false,
	strings				: [],
	
	init : function(){
		$("body").bind("url_preloaded", function(e){
			appUtils.log('ticket fetched preloaded event:' + e.module);
			appUtils.log(e);
			if(e.module == 'tickets'){
				$('div#ticketLayer > div#ticketContent').html(e.returnData.html);
				ticketHandler.renderTicketForm(e.returnData.vars);
			}
		});
		$("body").bind("url_loaded", function(e){
			appUtils.log('ticket fetched loaded event:' + e.module);
			if(e.module == 'tickets'){
				/* Force html append if layer is still empty */
				if ($('div#ticketLayer > div#ticketContent').html()==="") {
					$('div#ticketLayer > div#ticketContent').html(e.returnData.html);
					ticketHandler.renderTicketForm(e.returnData.vars);
				}
				/* *** */
				ticketHandler.showTicketForm();
			}
		});
		
		$('#buyTicketButton').click(function(){
			if($('#ticketLayer').hasClass('opened')){
				ticketHandler.hideTicketForm();
			} else {
				urlHandler.load('tickets/', false);
			}
		});
		
		$('#closeTicketLayer').click(function(){
			ticketHandler.hideTicketForm();
		});
					
	},
	/* returns [true] or [false] for jquery-ui datepicker widget if date has timeslots - is selectable */
	timeSlotDays : function(date){
		var key = date.getFullYear() + '-' + appUtils.padDigits((date.getMonth()+1), 2) + '-' + appUtils.padDigits(date.getDate(), 2);
		
		if(ticketHandler.arrTimeSlotDays[ticketHandler.currentYearMonth]){
			if($.inArray(key, ticketHandler.arrTimeSlotDays[ticketHandler.currentYearMonth]) >= 0){
				return [true];
			}
		}
		return [false];
	},
	
	init_feedbackform : function () {

		/* Bind events to submit button/formOnSubmit */
		$('#submitFormTicketFeedback').unbind();
		$('#submitFormTicketFeedback').bind('click', ticketHandler.postFormTicketFeedback);
		$('#ticketFeedback').show();
		$('#formTicketFeedback').show();
		$('#formTicketFeedback').attr('action', 'javascript:void(0)'); //this fails silently
		$('#formTicketFeedback').get(0).setAttribute('action', 'javascript:void(0)'); //this works
		$('#formTicketFeedback').attr("onsubmit", "$('#submitFormTicketFeedback').trigger('click');"); //this fails silently
		$('#formTicketFeedback').get(0).setAttribute("onsubmit", "$('#submitFormTicketFeedback').trigger('click');"); //this works
		
		/* Temporary init function to display ticket feedback form */
		$('#buyTicketButton').click(function(){
			if($('#ticketLayer').hasClass('opened')){
				/* Hide form */
				ticketHandler.hideTicketForm();
			} else {
				/* First clean form */
				$('#formTicketFeedback').show();
				$('#msgTicketFeedback .error').hide();
				$('#msgTicketFeedback .ok').hide();
				$('#formTicketFeedback input[type=text]').attr('value', '');
				/* Show form */
				ticketHandler.showTicketForm(275);
			}
		});
		
		$('#closeTicketLayer').click(function(){
			ticketHandler.hideTicketForm();
		});
	},

	/* show the ticket form. */
	showTicketForm : function(bar_height){
		bar_height	= (parseInt(bar_height)>0)	?	parseInt(bar_height)	:	404;
		
		$( "#ticketLayer" ).addClass('opened');
		$( "#ticketLayer" ).animate({height: bar_height+'px'}, 500);
		//$('#ticketLayer').show();
		$('#ticketLayer .ticketSteps').show();
	},
	
	hideTicketForm : function(){
		$( "#ticketLayer" ).removeClass('opened');
		$( "#ticketLayer" ).animate({height: '59px'}, 500);
		$('#ticketLayer .ticketSteps').hide();
	},
	
	/* fetch the timeslots for the given yearmonth
	 * check if existing data is within the cacheTimeout- if so, only update the currentYearMonth
	 * otherwise, fetch and update
	*/
	fetchTimeSlots : function(yearMonth){
		
		var load = false;
		
		if(ticketHandler.dateTimeLastFetch[yearMonth]){
			
			if((new Date()).getTime() - ticketHandler.dateTimeLastFetch[yearMonth].getTime() > ticketHandler.cacheTimeOut){
				load = true;
			}
			
		} else {
			load = true;
		}
		if(load){
			//appUtils.loadJSON('tickets/timeslots/' + yearMonth, {}, ticketHandler.updateTimeSlots);
			ticketHandler.toggleDatepickerLoader(true);
			appUtils.loadJSON('tickets/timeslots/' + yearMonth, {}, function(data){
				ticketHandler.updateTimeSlots(data);
				ticketHandler.toggleDatepickerLoader(false);
			});
		} else {
			//ticketHandler.updateTimeSlots(false);
			ticketHandler.currentYearMonth = yearMonth;
			$( "#datepicker" ).datepicker( "refresh" );
			appUtils.log('no need to load');
			ticketHandler.toggleDatepickerLoader(false);
		}
	},

	toggleDatepickerLoader: function (status) {
		if (status) {
			$("#datepicker_loader").fadeIn(200, function(){
				ticketHandler.isLoadingDatePicker	= true;
				$("#datepicker_loader").show();
			});
		} else {
			$("#datepicker_loader").fadeOut(200, function(){
				ticketHandler.isLoadingDatePicker	= false;
				$("#datepicker_loader").hide();
			});
		}
	},
	
	fetchDatepickerStartDate: function () {
		
		var curTimeStamp	= Math.round((new Date()).getTime() / 1000);
		var timeCeil		= 1333249200;
		var returnDate		= new Date();
		
		//If we are before 2011-03-01
		if (curTimeStamp<timeCeil) {
			returnDate.setTime( timeCeil * 1000 );
		}
		
		return returnDate;
	},
	
	/* after load of form html, fetch first timeslots and render form elements and add listeners */
	renderTicketForm : function(vars){
		appUtils.log('renderTicketForm, vars:');
		appUtils.log(vars);
		ticketHandler.strings = vars.strings;
		ticketHandler.ticketTypes = vars.ticketTypes;
		ticketHandler.fetchTimeSlots(vars.currentYearMonth);
		
		$( "#datepicker" ).datepicker({ 
			maxDate: '+6m', 
			minDate: ticketHandler.fetchDatepickerStartDate(), 
			dateFormat: 'yy-mm-dd',
			firstDay: 1,
			/* regional: 'nl', 'en' */
			monthNames:appGlobals.LANG_MONTH_NAMES,
			monthNamesShort:appGlobals.LANG_MONTH_NAMES_SHORT,
			dayNames:appGlobals.LANG_DAY_NAMES,
			dayNamesShort:appGlobals.LANG_DAY_NAMES_SHORT,
			dayNamesMin:appGlobals.LANG_DAY_NAMES_MIN,
			
			constrainInput: true, beforeShowDay: ticketHandler.timeSlotDays,
			onSelect: function(dateText, inst) {
				appUtils.log(dateText);
				
				$('#timeSlotInput').attr('value', '');
				ticketHandler.resetSelectedTimeSlots();
				
				$('.timeSlotsOfDay').hide();
				$('#timeSlotsOfDay_' + dateText).show();
				
				ticketHandler.updateTicketForm();
			},
			onChangeMonthYear: function(year, month, inst) {
				//
				appUtils.log('onChange ' + month);
				ticketHandler.fetchTimeSlots(year + '-' + month);
			}
		});
		
		$( "#datepicker" ).datepicker( "show" );
		//
		$('#timeSlots .timeSlotsOfDay .timeSlot.available').live('click', ticketHandler.selectTimeSlot);
		
		$('#ticketChoice .ticketType').change(ticketHandler.updateTicketForm);
		
		$('.subTicketForm .navigbar .button_next').click(function(){
			var $elt = $(this);
			// going next, only if the button is enabled
			if($elt.hasClass('enabled')){
				// the parent of parent is the container of the sub-view
				$subView = $elt.parent().parent();
				// reservation call in specific subform
				
				if($subView.attr('id') == 'dateChoice'){
					// make reservation call and advance if success, otherwise show message
					
					ticketHandler.postReservation();
					
				} else {
					
					ticketHandler.advanceProgressBar($subView);

					ticketHandler.updateTicketForm();
				}
				
			}
		});
		$('.subTicketForm .navigbar .button_back').click(function(){
			var $elt = $(this);
			$subView = $elt.parent().parent();
			
			$('#step_' + $subView.attr('id')).removeClass('active');
			$('#step_' + $subView.attr('id')).removeClass('current');
			
			var $prevView = $subView.hide().prev().show(); 
			$('#step_' + $prevView.attr('id')).addClass('current');
			
			ticketHandler.updateTicketForm();
		});
		
		$('#ticketChoice .ticketRow .ticketTypePlus').click(function(){
			var $elt = $(this);
			//appUtils.log($elt);
			var id = $elt.attr('id').split('_')[1];
			var $eltValue = $('#ticketType_' + id);
			var value = ticketHandler.parseTicketNum($eltValue);
			value++;
			if(value > ticketHandler.maxNumTickets){
				//value = ticketHandler.maxNumTickets;
			}
			
			/* Set value in order to execute proper validation in countTotalTickets */ 
			$eltValue.val(value);
			
			/* Validate numbers */
			if(ticketHandler.countTotalTickets()){
				ticketHandler.updateTicketForm();	
			} else {
				if (value>1) {
					$eltValue.val(value-1);
				} else {
					$eltValue.val(0);
				}
			}
		});
		$('#ticketChoice .ticketRow .ticketTypeMinus').click(function(){
			var $elt = $(this);
			//appUtils.log($elt);
			var id = $elt.attr('id').split('_')[1];
			var $eltValue = $('#ticketType_' + id);
			var value = ticketHandler.parseTicketNum($eltValue);
			value--;
			if(value < 0){
				value = 0;
			}
			
			/* Set value in order to execute proper validation in countTotalTickets */ 
			$eltValue.val(value);
			
			/* Validate numbers */
			if(ticketHandler.countTotalTickets()){
				ticketHandler.updateTicketForm();	
			} else {
				$eltValue.val(value+1);
			}
		});
		
		$('input.ticketType').bind('change', function(){
			
			var $elt = $(this);
			var id = $elt.attr('id').split('_')[1];
			var $eltValue = $('#ticketType_' + id);
			
			if(ticketHandler.countTotalTickets()){
				ticketHandler.updateTicketForm();	
			} else {
				$eltValue.val(0);
			}
			
		});
		
		$('#orderConfirmation .validateEmailRow .editText').click(function(){
			$('#orderConfirmation').hide();
			$('#nameAndEmail').show();
		});
		
		/* Attach listener to isTermsAgreed checkbod */
		$('#toggleIsTermsAgreed').bind('change', function (ev) {
			if ($('#toggleIsTermsAgreed').attr('checked')) {
				$('#isTermsAgreed').attr('value', 1);
			} else {
				$('#isTermsAgreed').attr('value', 0);
			}
			ticketHandler.updateTicketForm();
		});	
		
		$('#firstName').bind('change', function(ev){
			ticketHandler.updateTicketForm();
		});
		$('#familyName').bind('change', function(ev){
			ticketHandler.updateTicketForm();
		});
		$('#emailAddress').bind('change', function(ev){
			ticketHandler.updateTicketForm();
		});
		
		/* Attach listener to form submit button */
		$("#payButton").click(function(){
			ticketHandler.postTicketForm();
		});
		
		$('#paymentOption').bind('change', function(){
			ticketHandler.updateTicketForm();
		});
		
		/* Real-time first name validation */
		$('#firstName').bind('keydown', function(){
			var firstName	= $('#firstName').val();
			ticketHandler.setNameAndEmailFieldStatus($('#firstName'), firstName.length > 1, true);
		});
		
		$('#familyName').bind('keydown', function(){
			var familyName	= $('#familyName').val();
			ticketHandler.setNameAndEmailFieldStatus($('#familyName'), familyName.length > 1, true);
		});

		/* Real-time e-mail validation */
		$('#emailAddress').bind('keydown', function(){
			var email	= $('#emailAddress').val();
			if(email.length > 0 && appUtils.checkEmail(email)){
				$('#ticketViewEmail').html(email);
				$('#validateEmail').html(email);
				ticketHandler.setNameAndEmailFieldStatus($('#emailAddress'), true, true);
			} else {
				ticketHandler.setNameAndEmailFieldStatus($('#emailAddress'), false, true);
			}
		});
		
	},
	
	advanceProgressBar : function($subView){
		
		$('#ticketBarPageWidth .barItem').removeClass('current');
		
		var $nextView = $subView.hide().next();
		
		$('#step_' + $nextView.attr('id')).addClass('active');
		$('#step_' + $nextView.attr('id')).addClass('current');
		
		$nextView.show();
		
	},
	
	
	
	parseTicketNum : function($elt){
		var value = parseInt($elt.attr('value'));
		if(isNaN(value)){
			value = 0;
		}
		if(value < 0){
			value = 0;
		}
		if(value > ticketHandler.maxNumTickets){
			//value = ticketHandler.maxNumTickets;
		}
		return value;
	},
	
	/* update the timeslots variables for the current 'yearmonth' */
	updateTimeSlotsVariables : function(vars){
		appUtils.log('updating for ' + vars.currentYearMonth);
		appUtils.log(vars);
		
		ticketHandler.currentYearMonth = vars.currentYearMonth;
		
		ticketHandler.dateTimeLastFetch[ticketHandler.currentYearMonth] = new Date();
		
		//appUtils.log(vars);
		ticketHandler.arrTimeSlotDays[ticketHandler.currentYearMonth] = [];
		
		$.each(vars.timeSlots, function(i, item){
			if(item.enabled){
				//appUtils.log(item);
				ticketHandler.arrTimeSlotDays[ticketHandler.currentYearMonth].push(item.date);
			}
		});
		
		ticketHandler.timeSlots[ticketHandler.currentYearMonth] = vars.timeSlots;
	},
	
	// callback of preloading extra timeslots
	updateTimeSlots : function(data){
		appUtils.log('updateTimeSlots');
		appUtils.log(data);
		ticketHandler.updateTimeSlotsVariables(data);
		
		$yearMonth = $('#yearMonth_' + data.currentYearMonth);
		$yearMonth.remove();
		
		var html = '<div id="yearMonth_' + data.currentYearMonth + '">';
		$.each(data.timeSlots, function(i, daySlots){
			//appUtils.log(daySlots);
			html += '<div class="timeSlotsOfDay" id="timeSlotsOfDay_' + daySlots.date + '" style="display:none">';
			if(daySlots.enabled){
				var bCheck = true;
				html += '<div class="timeSlotTypeHeader">' + data.strings.morning + '</div>';
				$.each(daySlots.slots, function(j, slot){
					//appUtils.log("====>");
					//appUtils.log(slot);
					if(bCheck && slot.type == 'afternoon'){
						bCheck = false;
						html += '<div class="timeSlotTypeHeader">' + data.strings.afternoon + '</div>';
					}
					html += '<div class="timeSlot ';
					if(slot.tt){ // .length?
						html += ' available ';
					}
					html += '" rel="' + daySlots.date + ', ' + slot.time + '">' + slot.time + '</div>';	
				});
			}
			html += '</div>';
		});
		html += '</div>';
		
		$('#timeSlots').append(html);
		
		$( "#datepicker" ).datepicker( "refresh" );
	},
	
	resetSelectedTimeSlots : function(){
		$('#timeSlots .timeSlotsOfDay .timeSlot').toggleClass('selected', false);
	},
	
	selectTimeSlot : function(){
		var $elt = $(this);
		appUtils.log($elt.attr('rel'));
		
		ticketHandler.resetSelectedTimeSlots();
		$elt.addClass('selected');
		
		ticketHandler.updateTicketForm();
	},
	
	postTicketForm : function(){
		
		
		/* Update form values */
		if (ticketHandler.updateTicketForm()) {
			
			ticketHandler.onSubmitValue = true;
			
			/* DEBUG ###########################################*/
			//$('#calculatedPrice').attr('value', '15.75');
			
			/* Submit form */
			//appUtils.log($('#ticketForm').serialize());
			$('#ticketForm').submit();
			
		} else {
			appUtils.log("First agree to T&C!");
			ticketHandler.onSubmitValue = false;
		}
		
	},
	
	postReservation: function (){
		
		//$('#ticketForm').serialize();
		$.post("/tickets/reservation/", $('#ticketForm').serialize(), function(data){
			
			if(data.status){
				$('#dateChoice').hide();
				$('#nameAndEmail').show();
				
				$('#ticketBarPageWidth .barItem').removeClass('current');
				
				$('#step_nameAndEmail').addClass('active');
				$('#step_nameAndEmail').addClass('current');
				
				ticketHandler.updateTicketForm();
			} else {
				
				alert(ticketHandler.strings.no_reservation);
			}
		});
		
	},
	
	countTotalTickets : function(){
		var numTicketTypes		= new Array();
		var retStatus			= true;
		var numAdults			= 0;
		var numChildren			= 0;
		
		numTicketTypes[0]		= 0;
		
		/* Process selected ticket numbers */
		$('#ticketChoice input.ticketType').each(function(i, elt){
			var $elt		= $(elt);
			var val			= ticketHandler.parseTicketNum($elt);
			var typeID		= appUtils.extractNameID(elt.id);
			
			numTicketTypes[typeID]	= (numTicketTypes[typeID])	?	numTicketTypes[typeID]	:	0;
			
			if(val > 0){
				numTicketTypes[0] 		+= val;
				numTicketTypes[typeID]	+= val;
				switch (parseInt(typeID)) {
					case 2:
					case 3:
						numChildren	+= val;
					break;
					default:
						numAdults	+= val;
					break;
				}
			}
		});
		
		/* Check ticket type "Children": Children under 5yrs (typeID=2,3) require 1/3 Adult (typeID=1) */
		if (numChildren>0) {
			if (numAdults>0 && (numChildren/numAdults<=3)) {
				//Ok
			} else {
				//False
				alert(ticketHandler.strings.childs_adult);
				return false;
			}
		}
		
		/* Check if total number of tickets does not exceed max_tickets */
		if(numTicketTypes[0] > ticketHandler.maxNumTickets){
			alert(ticketHandler.strings.max_tickets);
			return false;
		}
		
		return true;
	},
	
	/*
	 * check if the form is valid (ready to post)
	 * update the ticket preview 
	 */
	updateTicketForm : function () {
		
		var isFormValid	= true;
		ticketHandler.onSubmitValue = false;
		
		/* Assign timeSlotInput value */
		$selSlot = $('#timeSlots .timeSlotsOfDay .timeSlot.selected');
		$('#timeSlotInput').attr('value', $selSlot.attr('rel'));
		
		/* clean ticket feedback */
		$('#dateOfVisit').html('');
		$('#timeOfVisit').html('');
		
		/* clean input */
		var arrTickets = [];
		var totalTickets = 0;
		$('#ticketChoice input.ticketType').each(function(i, elt){
			var $elt = $(elt);
			var val = ticketHandler.parseTicketNum($elt);
			if(val > 0){
				ticketType = $elt.attr('id').split('_')[1];
				arrTickets[ticketType] ={'label' : ticketHandler.getLabelForTicketType(ticketType), 'num' : val};
				totalTickets += val;
			}
			$elt.val(val);
		});
		
		$('#ticketChoice .button_next').toggleClass('enabled', (totalTickets > 0 && totalTickets <= ticketHandler.maxNumTickets));
		
		/* counters and price and list of tickets */
		
		var totalPrice = 0;
		var ticketsAvailable = 0;
		
		if($selSlot.get(0)){
			
			var timeSlot = $selSlot.attr('rel');
			var ticketTypesPricing = [], found = false;
			// loop though timeslots of period, match and set the ticket-types tt to ticketTypesPricing
			$.each(ticketHandler.timeSlots[ticketHandler.currentYearMonth], function(i, day){
				if(timeSlot.indexOf(day.date) == 0){
					$.each(day.slots, function(j, slot){
						if(timeSlot.indexOf(day.date + ', ' + slot.time) == 0){
							
							$('#ticketViewDateOfVisit').html(appUtils.parseDateString(day.date));
							$('#ticketViewTimeOfVisit').html(' ' + slot.time);
							ticketTypesPricing = slot.tt;
							found = true;
							
							ticketsAvailable = slot.ta; 
							
							
						}
					});
				}
			});
			
			// the selected timeslot was found
			if(found){
				if(totalTickets > ticketsAvailable){
					alert(ticketHandler.strings.no_reservation);
					$('#timeSlots .timeSlotsOfDay .timeSlot.available.selected').removeClass('selected');
					
					$('#dateChoice .button_next').toggleClass('enabled', false);
					return false;
				}
				/* Assign calculatedPrice value */
				//appUtils.log("tt pricing: ");
				//appUtils.log(ticketTypesPricing);
				
				$('#ticketChoice input.ticketType').each(function(i, elt){
					$inp = $(elt);
					var ticketType, price = 0, numTickets = 0;
					
					numTickets = ticketHandler.parseTicketNum($inp);
					
					if(numTickets > 0){
						
						// find the price for the selected ticketType
						ticketType = $inp.attr('id').split('_')[1];
						for(type in ticketTypesPricing){
							if(type == ticketType){
								price = ticketTypesPricing[type];
						
								arrTickets[ticketType].price = price * numTickets;
								
								break;
							}
						}
						
						totalPrice += (price * numTickets);
					}
				});
				
				$('#dateChoice .button_next').toggleClass('enabled', (totalTickets > 0));
				
				$('#calculatedPrice').attr('value', totalPrice.toFixed(2));
				
			}
		}
		
		var sHtml = '';
		for(type in arrTickets){
			
			sHtml += '<div class="ticketSummaryRow"><div class="ticketName">';
			if(arrTickets[type].num){
				sHtml += '<span class="num">' + arrTickets[type].num + ' x</span>';
			}
			sHtml += appUtils.stripTags(arrTickets[type].label) + '</div>';
			if(arrTickets[type].price){
				sHtml += '<div class="ticketPrice">&euro; ' + arrTickets[type].price + '</div>';
			}
			sHtml += '</div>';
			//+ ' = ' +  + '<br/>';
		}
		
		$('#ticketItemsSummary').html(sHtml);
		
		$('#ticketViewTotalPrice').html('&euro; ' + totalPrice);
		
		/* name and e-mail */
		$('#nameAndEmail .button_next').toggleClass('enabled', ticketHandler.updateNameAndEmail());
		
		/* payment selection */
		$('#paymentChoice .button_next').toggleClass('enabled', $('#paymentOption').val().length > 0);
		
		/* Check isTermsAgreed */
		if ($('#isTermsAgreed').attr('value') == "1") {
			//checkbox 'toggleIsTermsAgreed' controls the value of 'isTermsAgreed'
			
		} else {
			isFormValid		= false;
		}
		$('#payButton').toggleClass('enabled', isFormValid);

		return isFormValid;
		
	},
	
	updateNameAndEmail : function(){
		
		var name = $('#firstName').val();
		var familyName = $('#familyName').val();
		var email = $('#emailAddress').val();
		var isOk = true;
		
		// $('#nameAndEmail').is(':visible')
		var showErr = name.length > 1 || familyName.length > 1 || email.length > 0;
		if(name.length == 0){
			isOk = false;
		}
		if(familyName.length == 0){
			isOk = false;
		}
		
		ticketHandler.setNameAndEmailFieldStatus($('#firstName'), name.length > 1, showErr);
		ticketHandler.setNameAndEmailFieldStatus($('#familyName'), familyName.length > 1, showErr);
		
		$('#ticketViewName').html(name + ' ' + familyName + '&nbsp;');
		
		if(email.length > 0 && appUtils.checkEmail(email)){
			$('#ticketViewEmail').html(email);
			$('#validateEmail').html(email);
			ticketHandler.setNameAndEmailFieldStatus($('#emailAddress'), true, showErr);
			
			
			/*
			$('#emailAddress').parent().parent().toggleClass('error', false);
			if($('#emailAddress').attr('value')){
				$('#emailAddress').parent().parent().toggleClass('ok', true);
			}
			*/
		} else {
			ticketHandler.setNameAndEmailFieldStatus($('#emailAddress'), false, showErr);
			isOk = false;
			/*
			if($('#emailAddress').attr('value')){
				$('#emailAddress').parent().parent().toggleClass('error', true);
			}
			$('#emailAddress').parent().parent().toggleClass('ok', false);
			*/
		}
		return isOk;
	},
	
	setNameAndEmailFieldStatus : function($field, bOk, bShow){
		
		if(bShow){
			$field.parent().parent().toggleClass('error', bOk ? false : true);
			$field.parent().parent().toggleClass('ok', bOk);
		} else {
			$field.parent().parent().toggleClass('error', false);
			$field.parent().parent().toggleClass('ok', false);
		}
	},
	
	getLabelForTicketType : function(ticketType){
		return $('label[for=ticketType_' + ticketType + ']').html();
	},
	
	postFormTicketFeedback : function () {
		//$.post('/emailform/', {'name' : 'Ammar', 'email' : 'a.tijani@do.com', 'csrfmiddlewaretoken' : $("input[name='csrfmiddlewaretoken']").val()});
		$.post('/emailform/', $('#formTicketFeedback').serialize(), function(result){
			appUtils.log("===> POST RESULT <===");
			appUtils.log(result);
			appUtils.log("=====================");
			//try {
				if (isset(result.status) && result.status) {
					$('#formTicketFeedback').fadeOut(function(){
						$('#msgTicketFeedback .error').hide();
						$('#msgTicketFeedback .ok').fadeIn();
					});
				} else {
					$('#msgTicketFeedback .ok').hide();
					$('#msgTicketFeedback .error').fadeIn();
				}
			//} catch (e) {}
		});
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
};
