/* 
Simple JQuery menu.
HTML structure to use:

Notes: 

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

function initMenus() {
	$('ul.menu ul').hide();
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
		}
	);
	
	/*
		Slides open the correct nav based on url for country you are in.
			If not available, defaults to Caribbean
	*/
	var url = window.location.href;
	var region = url.substring(url.indexOf('/vacations/') + 11);
	region = region.substring(0, region.indexOf('/'));
	var thespot = $('#' + region);
	if(thespot.html()) {
		thespot.slideDown('normal');
	} else {
		thespot = $('#caribbean');
		if(thespot) {
			thespot.slideDown('normal');
		}
	}
}


function tiCheck() {
	var strLeavDate;
	var strRetDate;
	var strPsngrCount;
	var strAdult1;
	var strChild1;
	var strAdult2;
	var strChild2;
	var strRoomCount;
	var strSelDepart;
	var strSelDest;
	var noOfDays;
		
	strSelDepart = document.SearchFormTIC.selFrom[document.SearchFormTIC.selFrom.selectedIndex].value;
	strSelDest= document.SearchFormTIC.selTo[document.SearchFormTIC.selTo.selectedIndex].value;
	
		
    // Set date to yyyy format is yy entered
    document.getElementById('txtLeave').value = set4DigitYear(document.getElementById('txtLeave').value);
    document.getElementById('txtReturn').value = set4DigitYear(document.getElementById('txtReturn').value);
	

	strLeavDate = document.SearchFormTIC.txtLeave.value;
	strRetDate = document.SearchFormTIC.txtReturn.value;

        strRoomCount = document.SearchFormTIC.NoRooms[document.SearchFormTIC.NoRooms.selectedIndex].value;

	strAdult1 = document.SearchFormTIC.NoAdultsRoom1[document.SearchFormTIC.NoAdultsRoom1.selectedIndex].value;
	strChild1 = document.SearchFormTIC.NoChldRm1[document.SearchFormTIC.NoChldRm1.selectedIndex].value;
   	
	if(strSelDepart == "- Select a Departure City -") { 
		document.SearchFormTIC.selFrom.focus();
		return false;
	}
	else if(document.SearchFormTIC.selTo[0].selected == true){
		document.SearchFormTIC.selTo.focus();
		return false;
	}
	else if(strSelDepart == strSelDest){
		document.SearchFormTIC.selFrom.focus();
		return false;
	}
    
	chkLeavDtInd = chkDt(strLeavDate,"txtLeave");
	chkRetDtInd = chkDt(strRetDate,"txtReturn");

	
	if(chkLeavDtInd == false){
			document.SearchFormTIC.txtLeave.select();
			return false;
	}

	if(chkRetDtInd == false){
			document.SearchFormTIC.txtReturn.select();
			return false;
			
	}

	else {
		
		noOfDays = Math.ceil((chkRetDtInd.getTime() - chkLeavDtInd.getTime())/(1000*60*60*24));

	//added as a part of change request

		if(noOfDays == 0){
			strRetEarlierLeav = true;
			document.SearchFormTIC.txtReturn.select();
			return false;
			
		}

	//end added

		if(noOfDays < 0){
			strRetEarlierLeav = true;
			document.SearchFormTIC.txtReturn.select();
			return false;
			
		}
	}
	if(strRoomCount > 1){

		strAdult2 = document.SearchFormTIC.NoAdultsRoom2[document.SearchFormTIC.NoAdultsRoom2.selectedIndex].value;
		strChild2 = document.SearchFormTIC.NoChldRm2[document.SearchFormTIC.NoChldRm2.selectedIndex].value;

		strPsngrCount =parseInt(strAdult1) + parseInt(strChild1) +parseInt(strAdult2) + parseInt(strChild2);
		if(strPsngrCount> 7){
			document.SearchFormTIC.NoAdultsRoom1.focus();
			return false;
		}
		else{
		   if((checkChildAges(strChild1,1) == false)||(checkChildAges(strChild2,2) == false)){
				return false;
		   }
		
		}
	}
	else if(strRoomCount == 1){
        strPsngrCount =parseInt(strAdult1) + parseInt(strChild1);
		if(strPsngrCount  > 7){
			document.SearchFormTIC.NoAdultsRoom1.focus();
			return false;
		}
		else{
            if(checkChildAges(strChild1,1) == false){
			return false;
			}
		}
	}

    //date format bug fix
    document.SearchFormTIC.txtLeave.value = chkDate(chkLeavDtInd,0);
    document.SearchFormTIC.txtReturn.value = chkDate(chkRetDtInd,0);

		if(document.SearchFormTIC.hotelSel[1].checked == true){
		document.SearchFormTIC.hr_button.value = "Y"; 
	}
	else{
		document.SearchFormTIC.hr_button.value = "N"; 
	}
	
	 //Added for Hotel Specific Requirement
    if(document.SearchFormTIC.hotelSel[1].checked == true) {
    	
    	var selbox	= document.getElementById("hotelList");
		var len		= selbox.options.length;
		var flag 	= 0;
		for(var i=0; i < len; i++) {
			
			if(selbox.options[i].selected == true) {
				flag = 1;
				break;
			}
		}
		if(flag == 0) {
			document.SearchFormTIC.hotelList.focus();
			return false;
		}
    }
	// Added for Carrier Specific Requirement
	//if(document.SearchFormTIC.carrierSel[1].checked == true){
	//	document.SearchFormTIC.cr_button.value = "Y";
	//}else{
	//	document.SearchFormTIC.cr_button.value = "N";
	//}
	if(document.SearchFormTIC.carrierSel[1].checked == true){
		var selBox = document.getElementById("carrierList");
		var len = selBox.options.length;
		var flag = 0;
		for(var i=0;i < len;i++){
			if(selBox.options[i].selected == true){
				flag = 1;
				break;
			}
		}
		if(flag == 0){
			document.SearchFormTIC.carrierList.focus();
			return false;
		}
	}

   return true;
 }
