
var MAX_RESERVATIONS_COUNT = 0;
var RESERVATION_ALREADY_RETRIVED = '';
var MAX_RESERVATIONS_REACHED = '';
var NO_GUEST_SELECTED = '';
var DIFFERENT_GATEWAY = '';
var WHEN_ONLY_CHILD ='';
var currResId = '';

function checkReservations(x){

	var reservationId = document.getElementById("reservationId");
	
	if (document.getElementById(reservationId))
	{
		alert(RESERVATION_ALREADY_RETRIVED);
		return false;
	}
	
	var reservationsCount = document.getElementById("noOfReservations");
	
	if (reservationsCount && parseInt(reservationsCount.value) >= MAX_RESERVATIONS_COUNT)
	{
		alert(MAX_RESERVATIONS_REACHED+ "  ---"+reservationsCount.value+"----"+MAX_RESERVATIONS_COUNT);
		return false;
	}
	return validateReservationSearchForm(x);
}

function checkGateways() {
    var gateways = document.getElementsByName("gateway");
    var checkboxStatus = false;
    var selectedGateway;
    var selectedAge;
    var reservationId;
    
    if ($('guestSubmitBtn').className == 'Button')
	{    
		window.scrollTo(0,0);
	    // Checking atleast one checkbox is selected
	    for (var x=0; x<gateways.length; x++)
		{
			if (gateways[x].checked) {
				checkboxStatus = true;
				var allInfo = gateways[x].value.split('_');
				selectedGateway = allInfo[2];
				break;
			}
		}
	    
	    if (!checkboxStatus){
	    	alert(NO_GUEST_SELECTED);
	    	return void(0);
	    }
	    
	    var selectedGuestsCounter = 0;
	    
	   
	    if(checkboxStatus)
	    {
	    
	    	// Checking for the Same gateway for all the selected guests
	        for (var x=0; x<gateways.length; x++)
	    	{
	    		if (gateways[x].checked){
	    		
		    		selectedGuestsCounter++;
	    			var allOtherInfo = gateways[x].value.split('_');
	    			if(selectedGateway != allOtherInfo[2]){
	    				alert(DIFFERENT_GATEWAY);
						return void(0);
	    			}
	       		}
	    	}
	    	
	    	
	    	// checking for only child/infant guest travelling among all guests in all reservations
	    	var onlyChild = true;
			for (var x=0; x<gateways.length; x++)
	    	{
	    		if (gateways[x].checked){
	    			
	    			var allOtherInfo = gateways[x].value.split('_');
	  				currResId = allOtherInfo[0];
	  				var currAge = $(gateways[x].value).value;
	    			if(currAge == 'Adult'){
		    			onlyChild = false;
	    			}
	    			if(currAge == 'select') {
	    				alert(AGE_RANGE_SELECTION_ERROR+' '+currResId);
	    				return void(0);
	    			}
	       		}
	    	}
	    	
	    	if (onlyChild) {
	    		alert(WHEN_ONLY_CHILD);
	    		return void(0);
	    	}
	    	
	
			// checking for the child or infant alone travelling without adult guest per reservation
/*	        for (var x=0; x<gateways.length; x++)
	    	{
	    		if (gateways[x].checked){
	    			
	    			var allOtherInfo = gateways[x].value.split('_');
	  				currResId = allOtherInfo[0];
	//  				var currAge = $(gateways[x].get('value')).get('value');
	  				var currAge = $(gateways[x].value).value;
	    			if(currAge == 'Infant' || currAge == 'Child'){
	    				if(!checkAdultExistance(currResId, gateways)) {
	    					alert(WHEN_ONLY_CHILD+' '+currResId);
							return void(0);
	    				}
	    			}
	    			if(currAge == 'select') {
	    				alert(AGE_RANGE_SELECTION_ERROR+' '+currResId);
	    				return void(0);
	    			}
	       		}
	    	}*/
	    }
	
		if (selectedGuestsCounter <=8)
		{
			document.forms[1].method="POST";
			document.forms[1].submit();
		}
		else {
			alert("Eight is the maximum number of guests allowed at one time.  Please review your information and try again");
		}
	}
	else {
		return void(0);
	}
		
}

function checkAdultExistance(reservationId,  gateways) {

	var adultExistance = false;

    for (var x=0; x<gateways.length; x++)
    {
    	if (gateways[x].checked){
    	var reservationIdLength = reservationId.length+1;
    	
    		if(gateways[x].value.substring(0,reservationIdLength) == reservationId+"_"){
	    		var allOtherInfo = gateways[x].value.split('_');
  				var currAge = $(gateways[x].value).value;
	    		if (currAge == 'Adult') {
	    			adultExistance = true;
	    		}
    		}
    	}
    }
    return adultExistance;
}
function alert(msg) {
  writeMessage(msg, "error");
}

function showReqDiv() {

	var allTabs = document.getElementsByName('flightType');

	for (var i=0; i < allTabs.length; i++)
	{
		var tab = document.getElementById(""+allTabs[i].value);
		if (allTabs[i].checked)
			tab.style.visibility = 'visible';
		else
			tab.style.visibility = 'hidden';
	}
}

function updateSeatValue(seatHolder,seat)
{
	$(seatHolder).innerHTML=seat;
	
	var ajaxOptions = {
			method: "get",
			onComplete: function(txt) {
			}
	}	
	// load the content for the popup
	new Ajax('/protected/choiceair/updateSeat.do?'+JSESSIONID+'&seatHolder='+seatHolder+'&seat='+seat, ajaxOptions).request();
}

function displayInprogress() {
	$('OuterBody').setStyle('display','none');
	$('InProgress').setStyle('display','');
}
function submitForm(formIndex) {
	displayInprogress();
	document.forms[formIndex].submit();
}