function openEmenta() {
	var curHora = $('#iHoraReserva').val();
	var curEmenta = $('#iEmentaUpdate').val();
	tb_show(JS_LANG.ESCOLHA_A_CARTA,'/iframe_ementa.php?lang='+myLang+'&curHora='+curHora+'&ementaUpdate='+curEmenta+'&KeepThis=true&TB_iframe=true&height=509&width=500',false);	
}


function openPlan() {
	var curData = $('#iDataReserva').val();
	var curHora = $('#iHoraReserva').val();
	var curPax = $('#iPessoas').val();
	var iMesas = $('#iMesas').val();
	$('#PlantaFlash').html('&nbsp;');
	$('#PlantaFlash').flash({ src: '/flash/swf/reservas.1.2.swf', wmode:'transparent', flashvars: {baseDir:'/', lang:myLang, dataReserva:curData, horaReserva:curHora, pessoasReserva:curPax, mesasSel:iMesas}, width:430, height:520 }, { version: 8 });	
	tb_show(JS_LANG.ESCOLHA_A_MESA,'#TB_inline?&height=550&width=430&inlineId=flashContent',false);	
}

function swfAlertMessage(sMsgId, sLang) {
	sMsg = '';
	switch(sLang) {
	case('EN'):
	default:
		if		(sMsgId=='nao_pode_seleccionar_mais_mesas') sMsg = JS_LANG.NAO_PODE_ESCOLHER_MAIS_MESAS;
		else if (sMsgId=='nao_chega_ao_minimo_da_mesa') 	sMsg = JS_LANG.NAO_CHEGA_AO_MINIMO_DA_MESA;
	}
	alert(sMsg);
}
function swfUpdateMesas(sMesasCSV){
	if(sMesasCSV=='null'){
		showOkIcon(2,false);
		$('#divMesasSeleccionadas').html('');
		$('#iMesas').val('');
	} else{
		showOkIcon(2,true);
		sMesasCSV = sMesasCSV.substring(0, sMesasCSV.length-1);	//remove a , final
		if(sMesasCSV.indexOf(',')>=0) $('#divMesasSeleccionadas').html('<b>'+ JS_LANG.MESAS_SELECCIONADAS+':</b><br />' + sMesasCSV);
		else $('#divMesasSeleccionadas').html('<b>'+ JS_LANG.MESA_SELECCIONADA +':</b> ' + sMesasCSV);
		$('#iMesas').val(sMesasCSV);
	}
}
function swfClosePlanta() {	tb_remove(); }




function validateStep1(){
	var erMail = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	var erTelemovel = new RegExp(/^\s*\d+\s*$/);
	 
	if((!$('#Nome').val()) || (!$('#Apelido').val()) || (!$('#Email').val()) || (!$('#Telemovel').val()) || (!$('#iDataReserva').val()) || (!$('#iPessoas').val()) || (!$('#iHoraReserva').val())) {
		alert(JS_LANG.CAMPOS_OBRIGATORIOS);	
		return false;
	} else if(!erMail.test($('#Email').val())) {
		alert(JS_LANG.EMAIL_VALIDO);
		$('#Email').focus();
		return false;
	} else if(!erTelemovel.test($('#Telemovel').val())) {
		alert(JS_LANG.TELEMOVEL_VALIDO);
		$('#Telemovel').focus();
		return false;
	} else
		return true;
}

function validateStep2(){
	 if(!$('#iMesas').val()) {
		 alert(JS_LANG.ESCOLHA_A_MESA);
		 return false;
	 }
	 return true;
}

function showStep1(bShow) {
if(bShow)  {
	$("#oneTitle, #oneField, #oneButton").fadeTo('fast',1);
	$("#oneField input").removeAttr('readonly');
	$("#oneField select").removeAttr('disabled');
	$('#oneButton').css('visibility','visible');
	$('input#iDataReserva').dpSetDisabled(false);
} else {
	$("#oneTitle").fadeTo('slow',0.8);
	$("#oneField, #oneButton").fadeTo('slow',0.5);
	$("#oneField input").attr('readonly','readonly');
	$("#oneField select").attr('disabled','disabled');
	$('#oneButton').css('visibility','hidden');
	$('input#iDataReserva').dpSetDisabled(true);
}
}  
function showStep2(bShow) {
if(bShow)  {
	// validate form step 1
	$("#twoTitle, #twoField, #twoButton").fadeTo('fast',1);
	$("#twoField textarea").removeAttr('disabled');
	$("#twoField #iPlantaIcon").bind('click',openPlan);
	$("#twoField #iPlantaIcon").css({cursor:'hand'});
	$('#twoButton').css('visibility','visible') ;
} else {
	$("#twoTitle").fadeTo('slow',0.8);
	$("#twoField, #twoButton").fadeTo('slow',0.5);
	$("#twoField textarea").attr('disabled','disabled');
	$("#twoField #iPlantaIcon").unbind('click');
	$("#twoField #iPlantaIcon").css({cursor:'default'});
	$('#twoButton').css('visibility','hidden') ;
}
}  
function showStep3(bShow) {
if(bShow)  {
	// validate form step 1
	$("#threeTitle, #threeField, #threeButton").fadeTo('fast',1);
	$("#threeField textarea, #threeField input").removeAttr('disabled');
	$("#threeField #iEmentaIcon").bind('click',openEmenta);
	$("#threeField #iEmentaIcon").css({cursor:'hand'});
	$('#threeButton').css('visibility','visible');
} else {
	$("#threeTitle").fadeTo('slow',0.8);
	$("#threeField, #threeButton").fadeTo('slow',0.5);
	$("#threeField textarea, #threeField input").attr('disabled','disabled');
	$("#threeField #iEmentaIcon").unbind('click');
	$("#threeField #iEmentaIcon").css({cursor:'default'});
	$('#threeButton').css('visibility','hidden') ;
}
}

function checkEmenta() {
	if($('#iEmenta').val()!='') {
		showOkIcon(3,true);
	} else {
		showOkIcon(3,false);	
	}
}
//steps
function gotoStep(iStep){	  
  $(".Hold").removeClass("Hold");
  switch(iStep) {
	  case(1):  showStep1(true);	  showStep2(false);	  showStep3(false);	break;
	  case(2): 	if(validateStep1())	{	showOkIcon(1,true); showStep1(false);	showStep2(true);	showStep3(false); } break;
	  case(3): 	if(validateStep2())	{	showOkIcon(1,true); showOkIcon(2,true); showStep1(false);	showStep2(false);	showStep3(true); }	break;
  }
}

function showOkIcon(iStep, bShow) {
   switch(iStep) {
	   case(1): if(bShow) $('#oneTitle .ok').fadeIn('slow'); else $('#oneTitle .ok').fadeOut('fast');	break;
	   case(2): if(bShow) $('#twoTitle .ok').fadeIn('slow'); else $('#twoTitle .ok').fadeOut('fast');	break;
	   case(3): if(bShow) $('#threeTitle .ok').fadeIn('slow'); else $('#threeTitle .ok').fadeOut('fast');	break;
   }
}

function resetMesas() {
  showOkIcon(2,false);
  $('#divMesasSeleccionadas').html('');
  $('#iMesas').val('');
}

function resetEmenta() {
  showOkIcon(3,false);
  $('#iEmenta').val('');
  $('#iEmentaUpdate').val('');
}

function finalizar(){
if(validateStep1() && validateStep2()) {
	$("#oneField input").removeAttr('readonly');
	$("#oneField input").removeAttr('disabled');
	$("#oneField select").removeAttr('disabled');
	$("#twoField textarea").removeAttr('disabled');	  
	$("#iJsValid").val("1");
	$("#formReserva").submit();
} 
}

function submitReserva() {
 
}

$(document).ready(function(){
  showStep1(true);
  showStep2(false);
  showStep3(false);

	$.dpText = JS_LANG.CAL_TEXT_OBJECT;

	$('input#iDataReserva').datePicker(
		{
			startDate:'<?=date("Y-m-d", mktime(0, 0, 0, date("m")  , date("d"), date("Y")))?>', 
			endDate:'<?=date("Y-m-d", mktime(0, 0, 0, date("m")+1  , date("d"), date("Y")))?>'
		}
	);
	$('input#iDataReserva').focus(function(){$(this).dpDisplay();});
	$('input#iDataReserva').bind('change',
		function(){

		}
	);

});  