/* Travelers Scripts */ 

/* Variables */

//Array of Traveler Groups
var travGroups = new Array();

//---------
//ShowAges
//shows the ages dropdwon list
//---------
function ShowAges(selChildren, options) {
	//console.group('Function: ShowAges');
	//console.info('Parameters:\nselChilren: ' + selChildren + '\noptions: ' + options);
	//console.dir(options);
	//Get number of children selected
	var childSelect = checkElement(selChildren);
	var childVal = childSelect.options[childSelect.selectedIndex].value;
	
	var prefix = (hasProp(options, 'children_prefix')) ? options.children_prefix : "";
	var child;
	//Reset Children ages display (hide all)
    HideAll(selChildren, prefix);

    //Display child age selection boxes based on number of children selected
    for(i=1 ; i <= childVal; i++) {	
			child = document.getElementById(prefix + 'child' + i);
			child.style.visibility="visible";
			if (i > 3) {
				child.style.display = 'block';
			}
    }
    //Run Xtra Function
    if (hasProp(options, 'children_function')) {
			//console.log('Running Xtra Function: ' + options.children_function);
			//console.log('Option Type: ' + (typeof(options.children_function)));
			if (typeof(options.children_function) == 'function')
				options.children_function();
			if (typeof(options.children_function) == 'string') {
				var test = options.children_function;
				eval(options['children_function'] + '()');
			}
	}
	//console.groupEnd();
}

//---------
//HideAll
//hide the ages dropdwon list
//---------
function HideAll(selChildren, divPrefix) {
	//console.group('Function: HideAll()');
	//console.info('Parameters\nselChildren: %s\ndivPrefix: %s', selChildren, divPrefix);
	prefix = (divPrefix) ? divPrefix : "";
	selChildren = checkElement(selChildren);
	var numChildren = (selChildren && selChildren.id) ? selChildren.selectedIndex : 0;
	//console.log('Selected Children: ' + numChildren);
	//console.group('Children Selection Iteration');
  for(i = numChildren + 1 ; i <= 6; i++) {
  	//console.log('Element: %s', (prefix + 'child' + i));
		child = document.getElementById(prefix + 'child' + i);
		////console.log(child.id);
		if(!child)
			continue;
		child.style.visibility = "hidden";
		if(i > 3) {
			child.style.display = "none";
		}
  }
  //console.groupEnd();
  //console.groupEnd();
}

//Sets Related Travelers to watch as group
/*Arguments
 *adults: ID of adults select
 *children: ID of children select
 *seniors: ID of seniors select
 *button: ID of form submit button 
 *errDiv: ID of error box 
 *options: object containing options 
 */					
function setTravelers(adults, children, seniors, button, errDiv, options) {
	//console.group('Function: setTravelers');
	//console.info('Parameters: \nadults: ' + adults + '\nchildren: ' + children + '\nseniors: ' + seniors + '\nbutton: ' + button + '\nerrDiv: ' + errDiv + '\noptions: ' + options);
	if(options)
		//console.dir(options);	
	//console.log('Setting new traveler group');
	travGroups.push({
					travelers: [checkElement(adults), checkElement(children), checkElement(seniors)],
					button: checkElement(button),
					errDiv: checkElement(errDiv)
					});
	//Get Group ID
	var travID = (travGroups.length - 1);
	//console.log('Traveler Group ID: ' + travID);
	//Set onChange event for elements
	for(var x = 0; x < travGroups[travID].travelers.length; x++) {
		if (!travGroups[travID].travelers[x])
			continue;
		//Add onchange Event to all Travelers (verify that at least one traveler is selected)
		addEvent(travGroups[travID].travelers[x], 'change', function() { checkTravelers(travID); });
		//Add onchange event to Children selection (show children ages selectors)
		//console.log('Children: ' + children);
		if (travGroups[travID].travelers[x].id == children) {
			//console.log('Adding ShowAges to ' + travGroups[travID].travelers[x].id);
			addEvent(travGroups[travID].travelers[x], 'change', function() { ShowAges(children, options); setTF(); });
		}
		//console.log(travGroups[travID].travelers[x].id + ' onChange Event:\n' + travGroups[travID].travelers[x].onchange);
	}
	//console.groupEnd();
}

//Checks traveler group to make sure the selection values are valid
function checkTravelers(groupID) {
	console.group('Function: checkTravelers');
	console.info('Parameters:\ngroupID: ' + groupID);
	//Get total number of selected travelers
	var travNum = 0;
	console.group('Looping through Traveler Group ' + groupID);
	for(var x = 0; x < travGroups[groupID].travelers.length; x++) {
		travNum += travGroups[groupID].travelers[x].selectedIndex;
		console.log('Element: ' + travGroups[groupID].travelers[x].id + '\nIndex: ' + travGroups[groupID].travelers[x].selectedIndex + '\nSelected Travelers: ' + travNum);
	}
	console.groupEnd();
	//If No travelers in group are selected, display error message and disable submit button
	if (travNum == 0) {
		console.log('Displaying Error/Disabling Button');
		showIt(travGroups[groupID].errDiv, 'block');
		travGroups[groupID].button.disabled = true;
	} else {
		console.log('Hiding Error/Enabling Button');
		hideIt(travGroups[groupID].errDiv, 'none');
		travGroups[groupID].button.disabled = false;
	}
	console.groupEnd();
}

/*
function CheckTravelers(adults, children, seniors, button, errDiv) {
	adults = checkElement(adults);
	children = checkElement(children);
	seniors = checkElement(seniors);
	if (errDiv == undefined)
		errDiv = 'SelectPerson';
	errDiv = checkElement(errDiv);
	button = checkElement(button);
	//Make sure all elements exist
	if (adults != undefined && children != undefined && seniors != undefined && button != undefined && errDiv != undefined) {
		if ((adults.selectedIndex + children.selectedIndex + seniors.selectedIndex) != 0) {
			errDiv.style.visibility = 'hidden';
			button.disabled = false;
		} else {
			errDiv.style.visibility = 'visible';
			button.disabled = true;
		}
	}
}
*/

//Display Popup Traveler form
function popTF(clicked, el) {
	var pos = getPos(clicked);
	//Adjust Y position
	pos[1] = pos[1] + 15;
	//Use default DIV if none is defined
	if (el == undefined)
		el = 'TravelerForm';
	el = checkElement(el);
	//Remove from DOM
	el.parentNode.removeChild(el);
	var iframe = checkElement('TF_Frame');
	iframe.parentNode.removeChild(iframe);
	//Add to Content DIV
	var content = checkElement('content');
	//Add IFrame (for IE Select Element Issue)
	
	//iframe.setAttribute("src", "javascript:'<html></html>';");
	//iframe.setAttribute("scrolling", "no");
	//iframe.setAttribute("frameborder", "no");
	//iframe.id = "TFrame";
	
	content.appendChild(iframe);
	content.appendChild(el);
	setPos(iframe, pos);
	setPos(el, pos);
	//Display form
	showIt(iframe, 'block');
	showIt(el, 'block');
	
	//Connect Form to iFrame
	iframe['formEl'] = el;
	
	setTF(iframe);
}

//Sets Dimensions of iFrame for Traveler Form
function setTF(iframe, el) {
	if (typeof iframe == 'undefined')
		iframe = checkElement('TF_Frame');
	if (typeof el == 'undefined') {
		if (hasProp(iframe, 'formEl'))
			el = iframe.formEl;
		else
			return false;
	}
	
	el = checkElement(el);
		
	//Get Element's Dimensions
	var elH = el.offsetHeight;
	var elW = el.offsetWidth;
	//Match Dimensions of positioned element
	iframe.style.height = elH;
	iframe.style.width = elW;
}

function hideTF() {
	//Hide IFrame
	hideIt('TF_Frame', 'none');
	hideIt('TravelerForm', 'none');
}

//Populates city field based on selected country
function fillCities (stCmbcounties ,stCmbCities ,sttxtAirport) {
	var cmbCounties = document.getElementById(stCmbcounties);
	var cmbCities = document.getElementById(stCmbCities);
	var txtAirport = document.getElementById(sttxtAirport);

 	var selectedCountry = cmbCounties.options[cmbCounties.selectedIndex].value
 	while (cmbCities.options.length > 0 ) {
    cmbCities.options[cmbCities.options.length - 1] = null;
	}
	cmbCities.options.add(new Option ("-" ,"" ))
	if (cmbCounties.selectedIndex != 0) {
	  for (var i=0; i < Cities[selectedCountry].length; i++) {
	       cmbCities.options.add(new Option(Cities[selectedCountry][i], Cities[selectedCountry][i]))
		}
	}
  if (txtAirport.style.display == 'none')
     txtAirport.value="";
}

//Event handler for City selection
function selectCity (stCmbCities, sttxtAirport) {
    var txtAirport = document.getElementById (sttxtAirport);
    var cmbCities = document.getElementById (stCmbCities);
   if (cmbCities.options != null)
   {
     if (cmbCities.options.length > 0 )
        txtAirport.value = cmbCities.options[cmbCities.selectedIndex].value;
      else
       txtAirport.value = ""
    }
    else
     txtAirport.value = ""
}

//Setup function for destination form fields
function setupDestinations(country, city, airport) {
	//conFunc(arguments);
	//Setup Events
		addEvent(country, 'change', function() {
		fillCities(country, city, airport);
	});
	addEvent(city, 'change', function() {
		selectCity(city, airport);
	});
	//console.groupEnd(); //Function group
}
