﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		$("#History").addClass('hoverDisable');
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		$("#History").removeClass('hoverDisable');
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top":168,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	    jQuery("#backgroundPopup").height(jQuery(document).height());
    jQuery("#backgroundPopup").width(jQuery(document).width());
	
}


//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){ 
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	/*
	 * Datepicker doesn't work on the form, no idea why so another method ensues
	 
	$('#datepicker-id').datepicker({
		onSelect: function(dateText, inst){
				var days = [
					'sun',
					'mon',
					'tue',
					'wed',
					'thu',
					'fri',
					'sat'
				];
			 	dt = $(this).datepicker('getDate');
			 	var day = days[dt.getDay()];
			 	$('#dayinput').val(day);
			 	if(day=="sat" || day=="sun")
				    {
						$('#divtime').hide();
						$('#divtime_fri').hide();
						
						$('#divtime_sat').show();
				    }
					else if(day=="fri")
				    {
						$('#divtime').hide();
						$('#divtime_sat').hide();
						$('#divtime_fri').show();
					}
					else
					{
						$('#divtime_sat').hide();
						$('#divtime_fri').hide();
						$('#divtime').show();
					}
			 }

	}); 
	*/
	$('#datepicker-id').blur(
		function(){
			var val = $(this).val();
			if(validateDate(val))
				timeSelectSwitch(new Date(val));
			
		}
	).keyup(
		function(event){
			if(event.keyCode != 8){
				var v = $(this).val();
				if (v.match(/^\d{2}$/) !== null) {
			        $(this).val(v + '/');
			    } else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
			        $(this).val(v + '/');
			    }
		    }
    	}
	);
	
	$("#reservForm").submit(function(){
		var flag = true;
		$("#reservForm input").removeClass('error');
		var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
		
		if(!validateDate($("#datepicker-id").val())){
			$("#datepicker-id").addClass('error');
			flag = false;
		}
		
		if($("#first_name").val()==''){
			$('#first_name').addClass('error');
			flag = false;
		}
		
		if($("#last_name").val()==''){
			$('#last_name').addClass('error');
			flag = false;
		}
		
		if($("#email").val()=='' || !email.test($("#email").val())){
			$('#email').addClass('error');
			flag = false;
		}
		
		else if($("#email_confirm").val() != $('#email').val()){
			$('#email_confirm').addClass('error');
			flag = false;
		}
		if(flag){
			vals = $('#reservForm').serialize();
			$('#spinner').show();
			$('#resFormContainer').hide();
			$.post(def_path+'ajax/vegasreserve', vals, vegas_response);
		}
		else {
			$(".submitStatus").text('Please fill-up required field');
		}
		return false;	// form submitted via ajax so never actually submit it
	});
	
	$('#respSwap').click(
		function(){
			$('#respMessageContainer').hide();
			$('#resFormContainer').show();
		}
	);

});

function vegas_response(resp){
	$('#spinner').hide();
	if(resp == 'ok'){
		$('#respMessage').html('<p>Thank you, your reservation request has been sent</p>');
		$('#respMessageContainer').show();
		$('#tryagain').hide();
	} else {
		$('#respMessage').html(resp);
		$('#respMessageContainer').show();
		$('#tryagain').show();
	}
}

function timeSelectSwitch(dt){
	var days = [
					'sun',
					'mon',
					'tue',
					'wed',
					'thu',
					'fri',
					'sat'
				];
	var day = days[dt.getDay()];
	if(day=="sat" || day=="sun")
	    {
			$('#divtime').hide();
			$('#divtime_fri').hide();
			
			$('#divtime_sat').show();
	    }
		else if(day=="fri")
	    {
			$('#divtime').hide();
			$('#divtime_sat').hide();
			$('#divtime_fri').show();
		}
		else
		{
			$('#divtime_sat').hide();
			$('#divtime_fri').hide();
			$('#divtime').show();
		}
	$('#dayinput').val(day);
}

function validateDate(str){
	var parts = str.split('/');
	if(parts.length == 3){
		if(parseInt(parts[0]) <= 12 && parseInt(parts[0]) > 0
			&& parseInt(parts[1]) > 0 && parseInt(parts[1]) <= 31
			&& parseInt(parts[2]) > 2010)
			return true;
	}
	return false;
}
