﻿function getTemplate(template_name, texts)
{
var template
if (template_name == "so_featured_table") 
	template = ["<div id=\"tiplayer\" style=\"visibility:hidden;position:absolute;z-index:1000;top:-100;\"></div>",
"<form action=\"\" method=\"get\" name=\"frm_sfb_search_box\" id=\"frm_sfb_search_box\">",
"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"tbl_sfb_search_box\" >",
"<tr>",
"<td id=\"sfb_button_flight\" class=\"radio\"> <input name=\"sfb_rg_product_type\" id=\"sfb_rg_flight\" type=\"radio\" value=\"flight\" checked /> ##flights_inter##&nbsp;&nbsp;</td>",
"<td id=\"sfb_button_domestic_flight\" class=\"radio\"> <input name=\"sfb_rg_product_type\" id=\"sfb_rg_domestic_flight\" type=\"radio\" value=\"domestic_flight\" /> ##flights_domes##&nbsp;&nbsp;</td>",
"</tr>",
"<tr >",
"<td id=\"sfb_cell_out\" colspan=\"2\" valign=\"top\" >",
"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">",
"<tr>",
"<td width=\"35%\" align=\"right\">##depart##</td>",
"<td width=\"65%\" align=\"left\"><select name=\"cbx_flg_departure_city\" id=\"cbx_flg_departure_city\" class=\"route\"></select>&nbsp;<input id=\"txt_flg_departure_city_title\" value=\"\" name=\"txt_flg_departure_city_title\" class=\"code\"></td>",
"</tr>",
"<tr>",
"<td align=\"right\">##arrival##</td>",
"<td align=\"left\"><select name=\"cbx_flg_arrival_city\"  id=\"cbx_flg_arrival_city\" class=\"route\"></select>&nbsp;<input id=\"txt_flg_arrival_city_title\" value=\"\" name=\"txt_flg_arrival_city_title\" class=\"code\"></td>",
"</tr>",
"<tr>",
"<td align=\"right\">##flg_type## </td>",
"<td align=\"left\"><label name=\"lb_flg_round\" id=\"lb_flg_round\" style=\"cursor:pointer\"><input name=\"rg_flg_type\" type=\"radio\" id=\"rg_flg_type\" value=\"R\" checked> ##flg_type_round##</label>",
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
"<label name=\"lb_flg_one\" id=\"lb_flg_one\" style=\"cursor:pointer\"><input type=\"radio\" id=\"rg_flg_type\" name=\"rg_flg_type\" value=\"O\"> ##flg_type_one##</label></td>",
"</tr>",
"<tr>",
"<td align=\"right\">##dep_date##</td>",
"<td align=\"left\"><input id=\"txt_flg_departure_date\" class=\"time\" value=\"\" name=\"txt_flg_departure_date\" readonly=\"1\">",
" <img id=\"img_flg_departure_date\" title=\"##msg_date##\"/>",
" <i>##date##</i>",
"<input id=\"departureDate\" value=\"\" name=\"departureDate\" type=\"hidden\"><input id=\"departureMonth\" value=\"\" name=\"departureMonth\" type=\"hidden\"><input id=\"departureYear\" value=\"\" name=\"departureYear\" type=\"hidden\"></td>",
"</tr>",
"<tr id=\"sfb_tr_flg_return_date\">",
"<td align=\"right\">##ret_date##</td>",
"<td align=\"left\"><input id=\"txt_flg_return_date\" class=\"time\" value=\"\" name=\"txt_flg_return_date\" readonly=\"1\">",
" <img id=\"img_flg_return_date\" title=\"##msg_date##\"  />",
" <i>##date##</i>",
"<input id=\"returnDate\" value=\"\" name=\"returnDate\" type=\"hidden\"><input id=\"returnMonth\" value=\"\" name=\"returnMonth\" type=\"hidden\"><input id=\"returnYear\" value=\"\" name=\"returnYear\" type=\"hidden\"></td>",
"</tr>",
"<tr>",
"<td align=\"right\">##class##</td>",
"<td align=\"left\"><select name=\"cbx_flg_class\"  id=\"cbx_flg_class\">",
"<option value=\"\">##any##</option><option value=\"E\">##economy##</option><option value=\"B\">##business##</option><option value=\"F\">##first##</option></select></td>",
"</tr>",
"<tr>",
"<td align=\"center\" colspan=\"2\">##adults##&nbsp;",
"<select name=\"cbx_flg_number_of_adults\" id=\"cbx_flg_number_of_adults\">",
"<option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option></select>",
" <a class=\"help\" onMouseOver=\"sfb_help(\'##adults##\',\'##help_adults##\');\">[?]</a>",
"&nbsp;##children##&nbsp;",
"<select name=\"cbx_flg_number_of_children\"  id=\"cbx_flg_number_of_children\">",
"<option value=\"0\">0</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option></select>",
" <a class=\"help\" onMouseOver=\"sfb_help(\'##children##\',\'##help_children##\');\">[?]</a>",
"&nbsp;<label >##infants##</label><select name=\"cbx_flg_number_of_infants\"  id=\"cbx_flg_number_of_infants\">",
"<option value=\"0\">0</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option></select>",
" <a class=\"help\" onMouseOver=\"sfb_help(\'##infants##\',\'##help_infants##\');\">[?]</a></td>",
"</tr>",
"<tr id=\"sfb_tr_curr\"><td align=\"right\">##curr##</td><td align=\"left\"><select name=\"cbxCurr\" id=\"cbxCurr\"></select></td></tr>",
"<tr id=\"sfb_tr_office\"><td align=\"right\">##country##</td><td align=\"left\"><select name=\"cbx_client_ip\" id=\"cbx_client_ip\"></select></td></tr>",
"<tr>",
"<td ></td>",
"<td align=\"left\"><input name=\"btn_search_flight\" id=\"btn_search_flight\" type=\"button\" class=\"button\" value=\"##button_label##\"></td>",
"</tr>",
"</table>",
"</td></tr></table>",
"</form><div style=\"visibility:hidden;display:none;\"><form action=\"\" method=\"get\" id=\"frm_sfb_search_box_info\" name=\"frm_sfb_search_box_info\" style=\"visibility:none\" target=\"_blank\"></form></div>"];
else if (template_name == "sfb_table_vertical") 
	template = ["<div id=\"tiplayer\" style=\"visibility:hidden;position:absolute;z-index:1000;top:-100;\"></div>",
"<form action=\"\" method=\"get\" name=\"frm_sfb_search_box\" id=\"frm_sfb_search_box\">",
"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"tbl_sfb_search_box\" >",
"<tr>",
"<td id=\"sfb_button_flight\" class=\"radio\"> <input name=\"sfb_rg_product_type\" id=\"sfb_rg_flight\" type=\"radio\" value=\"flight\" checked /> ##flights_inter##&nbsp;&nbsp;</td>",
"<td id=\"sfb_button_domestic_flight\" class=\"radio\"> <input name=\"sfb_rg_product_type\" id=\"sfb_rg_domestic_flight\" type=\"radio\" value=\"domestic_flight\" /> ##flights_domes##&nbsp;&nbsp;</td>",
"</tr>",
"<tr >",
"<td id=\"sfb_cell_out\" colspan=\"2\" valign=\"top\" >",
"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">",
"<tr>",
"<td align=\"center\">##depart##</td>",
"</tr>",
"<tr>",
"<td align=\"center\"><select name=\"cbx_flg_departure_city\" id=\"cbx_flg_departure_city\" class=\"route\"></select>&nbsp;<input id=\"txt_flg_departure_city_title\" value=\"\" name=\"txt_flg_departure_city_title\" class=\"code\"></td>",
"</tr>",
"<tr>",
"<td align=\"center\">##arrival##</td>",
"</tr>",
"<tr>",
"<td align=\"center\"><select name=\"cbx_flg_arrival_city\"  id=\"cbx_flg_arrival_city\" class=\"route\"></select>&nbsp;<input id=\"txt_flg_arrival_city_title\" value=\"\" name=\"txt_flg_arrival_city_title\" class=\"code\"></td>",
"</tr>",
"<tr>",
"<td align=\"center\">##flg_type## </td>",
"</tr>",
"<tr>",
"<td align=\"center\"><label name=\"lb_flg_round\" id=\"lb_flg_round\" style=\"cursor:pointer\"><input name=\"rg_flg_type\" type=\"radio\" id=\"rg_flg_type\" value=\"R\" checked> ##flg_type_round##</label>",
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
"<label name=\"lb_flg_one\" id=\"lb_flg_one\" style=\"cursor:pointer\"><input type=\"radio\" id=\"rg_flg_type\" name=\"rg_flg_type\" value=\"O\"> ##flg_type_one##</label></td>",
"</tr>",
"<tr>",
"<td align=\"center\">##dep_date##<br \/><input id=\"txt_flg_departure_date\" class=\"time\" value=\"\" name=\"txt_flg_departure_date\" readonly=\"1\">",
" <img id=\"img_flg_departure_date\" title=\"##msg_date##\"/>",
" <i>##date##</i>",
"<input id=\"departureDate\" value=\"\" name=\"departureDate\" type=\"hidden\"><input id=\"departureMonth\" value=\"\" name=\"departureMonth\" type=\"hidden\"><input id=\"departureYear\" value=\"\" name=\"departureYear\" type=\"hidden\"></td>",
"</tr>",
"<tr id=\"sfb_tr_flg_return_date\">",
"<td align=\"center\">##ret_date##<br \/><input id=\"txt_flg_return_date\" class=\"time\" value=\"\" name=\"txt_flg_return_date\" readonly=\"1\">",
" <img id=\"img_flg_return_date\" title=\"##msg_date##\"  />",
" <i>##date##</i>",
"<input id=\"returnDate\" value=\"\" name=\"returnDate\" type=\"hidden\"><input id=\"returnMonth\" value=\"\" name=\"returnMonth\" type=\"hidden\"><input id=\"returnYear\" value=\"\" name=\"returnYear\" type=\"hidden\"></td>",
"</tr>",
"<tr>",
"<td align=\"center\">##class## <select name=\"cbx_flg_class\"  id=\"cbx_flg_class\">",
"<option value=\"\">##any##</option><option value=\"E\">##economy##</option><option value=\"B\">##business##</option><option value=\"F\">##first##</option></select></td>",
"</tr>",
"<tr>",
"<td align=\"center\" >##adults##&nbsp;",
"<select name=\"cbx_flg_number_of_adults\" id=\"cbx_flg_number_of_adults\">",
"<option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option></select>",
" <a class=\"help\" onMouseOver=\"sfb_help(\'##adults##\',\'##help_adults##\');\">[?]</a>",
" ##children##&nbsp;",
"<select name=\"cbx_flg_number_of_children\"  id=\"cbx_flg_number_of_children\">",
"<option value=\"0\">0</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option></select>",
" <a class=\"help\" onMouseOver=\"sfb_help(\'##children##\',\'##help_children##\');\">[?]</a>",
" ##infants## <select name=\"cbx_flg_number_of_infants\"  id=\"cbx_flg_number_of_infants\">",
"<option value=\"0\">0</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option></select>",
" <a class=\"help\" onMouseOver=\"sfb_help(\'##infants##\',\'##help_infants##\');\">[?]</a></td>",
"</tr>",
"<tr id=\"sfb_tr_curr\">",
"<td align=\"center\">##curr## <select name=\"cbxCurr\" id=\"cbxCurr\"></select></td>",
"</tr>",
"<tr id=\"sfb_tr_office\"><td align=\"right\">##country##</td><td align=\"left\"><select name=\"cbx_client_ip\" id=\"cbx_client_ip\"></select></td>",
"</tr>",
"<tr>",
"<td align=\"center\"><input name=\"btn_search_flight\" id=\"btn_search_flight\" type=\"button\" class=\"button\" value=\"##button_label##\"></td>",
"</tr>",
"</table>",
"</td></tr></table>",
"</form><div style=\"visibility:hidden;display:none;\"><form action=\"\" method=\"get\" id=\"frm_sfb_search_box_info\" name=\"frm_sfb_search_box_info\" style=\"visibility:none\" target=\"_blank\"></form></div>"];
	var templateText = template.join("");
	for (var i = 0; i < texts.length; i++) 
		while (templateText.indexOf(texts[i][0]) != -1)
			templateText = templateText.replace(texts[i][0], texts[i][1]);
	return (templateText);
}	
	

function sfb_help(t,m){
  var Text=[t,m];
  var Style=["#FFFFFF","#005EA6","","","Verdana, Arial, Helvetica, sans-serif",,"black","#E2F2FE","","","Verdana, Arial, Helvetica, sans-serif",,,,2,"#005EA6",2,24,0.5,0,2,"gray",,0,20,20];
  stm(Text,Style);  
}

function sfb_innerElement(elementId){
	document.getElementById(elementId).innerHTML =  getTemplate(ctn_template,_ft);;
	sfb_initializeSearchBox();
	setBox();
}

function sfb_load_script(src_script) {
	var scripts_loaded;
	var found = false;
	scripts_loaded = document.getElementsByTagName("script");
	for (var i= 0; i < scripts_loaded.length && !found ; i++) {
		if (scripts_loaded[i].src.slice(scripts_loaded[i].src.lastIndexOf("/")) == src_script.slice(src_script.lastIndexOf("/"))) {
			found = true;
		}
	}
	if (!found){ document.writeln('<script type="text\/javascript" src="' + src_script + '"><\/script>\r\n');}
}

var ctn_languages = new Array("en", "fr", "de", "es", "it", "nl");
var ctn_currencies_default = new Array("CAD","EUR","GBP","JPY","USD");

if (typeof ctn_color_dark== 'undefined' ) { ctn_color_dark= "#000000"; }
if (typeof ctn_color_medium== 'undefined' ) { ctn_color_medium= "#097864";}
if (typeof ctn_color_clear== 'undefined' ) { ctn_color_clear= "#ffffff";}
if (typeof ctn_affiliate_id== 'undefined' ) { ctn_affiliate_id= "";}
if (typeof currencyCode== 'undefined' )  {currencyCode= "USD";}
if (typeof show_currencies == 'undefined') {show_currencies = true;}
if (typeof ctn_currencies == 'undefined') {ctn_currencies = ctn_currencies_default;}
if (typeof ctn_language == 'undefined') {ctn_language = "en";}
if (typeof ctn_site_from== 'undefined' ) { ctn_site_from= "http://www.fast-manager.com";}
if (typeof ctn_site_name== 'undefined' ) { ctn_site_name= "";}
if (typeof ctn_use_external_css== 'undefined' ) { ctn_use_external_css= "0";}
if (typeof ctn_template== 'undefined' ) { ctn_template= "so_featured_table";}
if (typeof ctn_width== 'undefined' ) { ctn_width= "100%"; }
if (typeof showOfficeID == 'undefined') { showOfficeID = 0;} 
if (typeof ctn_release_time == 'undefined') {ctn_release_time = 5;}
if (typeof ctn_departure_city == 'undefined') {ctn_departure_city = "";}
if (typeof ctn_arrival_city == 'undefined') {ctn_arrival_city = "";}
if (typeof ctn_show_city_code == 'undefined') {ctn_show_city_code = 0;}
if (typeof ctn_group_id == 'undefined') {ctn_group_id = 'International';}

//se busca si la moneda definida está dentro del arreglo, si no se establece EUR
for(var i = 0; i < ctn_currencies.length; i ++) if(currencyCode == ctn_currencies[i]) break;
currencyCode = (i < ctn_currencies.length) ? ctn_currencies[i] : "EUR";

//se busca si el idioma definido está dentro del areglo, si no se establece EN
for(var i = 0; i < ctn_languages.length; i ++) if(ctn_language == ctn_languages[i]) break;
ctn_language = (i < ctn_languages.length) ? ctn_languages[i] : "en";

if (ctn_use_external_css == "0") {
	document.writeln('<link href="' + ctn_site_from + '\/comun\/styles\/sbf_search_box_cbb.css" rel="stylesheet" type="text\/css">\r\n');
	document.writeln('<link href="' + ctn_site_from + '\/comun\/calendar\/calendar-win2k-cold-1.css" rel="stylesheet" type="text\/css">\r\n');
}

sfb_load_script(ctn_site_from + "\/comun\/calendar\/calendar.js");
sfb_load_script(ctn_site_from + "\/comun\/calendar\/calendar-"+ ctn_language +".js");
sfb_load_script(ctn_site_from + "\/comun\/calendar\/calendar-setup.js");
sfb_load_script(ctn_site_from + "\/comun\/scripts\/date_en.js");
sfb_load_script(ctn_site_from + "\/comun\/scripts\/main15.js");
var rnd_p = Math.random();
sfb_load_script(ctn_site_from + "\/comun\/data\/searchbox_flg_cbb\/sfb_jsdata_cbb_" + ctn_language + ".js?p=" + rnd_p);

//	Valor inicial para las fechas de salida y arribo
var sfb_flg_curDate = new Date(); 
var sfb_min_flg_departure_date 	= new Date(sfb_flg_curDate.valueOf() + (ctn_release_time)*24*60*60*1000);
var sfb_min_flg_return_date		= new Date(sfb_flg_curDate.valueOf() + (ctn_release_time+1)*24*60*60*1000);

var TipId="tiplayer"; // should be the same as <div> tag's id
var FiltersEnabled = 1;
//	canSee contiene el estilo correcto para ocultar un elemento en dependencia del navegador en uso
if(navigator.appName.indexOf("Microsoft") > -1){
	var canSee = 'block'
} 
else {
	var canSee = 'table-row';
}


function sfb_populateSelect(selectId, x, y,e)
{
	document.getElementById(selectId).innerHTML = "";
	var prevValue = String("");
	for (var i=0;i<x.length;i=i+1+e)
		if (x[i] != prevValue){
			var optionElement = document.createElement('OPTION');
			optionElement.setAttribute('value', x[i]);
			var theData = document.createTextNode(x[i+e]);
			if (x[i] == y) optionElement.setAttribute('selected', 'true');
			optionElement.appendChild(theData);
			document.getElementById(selectId).appendChild(optionElement);
			prevValue = x[i];
		}
}

function sfb_populate_citySelect(selectId, x, y, d)
{
	document.getElementById(selectId).innerHTML = "";
	
	// Poniendo Select a destination como primer item del select
	var optionElementSelect = document.createElement('OPTION');
	optionElementSelect.setAttribute('value', '');
	if (d == '') optionElementSelect.setAttribute('selected', 'true');
	optionElementSelect.innerHTML = option_select;//" -- select destination --"; 
	document.getElementById(selectId).appendChild(optionElementSelect);		
	
	var prevValue = String("");
	for (var i=0; i < x.length; i++)
		if (x[i][4][0] == y) {
			if (prevValue != x[i][3]) {
				prevValue = x[i][3];
				var optionGroup = document.createElement('OPTGROUP');
				optionGroup.setAttribute('label', x[i][3]);
				document.getElementById(selectId).appendChild(optionGroup);
			}
			var optionElement = document.createElement('OPTION');
			optionElement.setAttribute('value', x[i][0]);
			var theData = document.createTextNode( x[i][2]);
			if ((x[i][0] == d) || (x[i][1] == d)) optionElement.setAttribute('selected', 'true');
			optionElement.appendChild(theData);
			optionGroup.appendChild(optionElement);
		}
}

//	Funcion para crear los campos que se van a enviar a la página de SFB
function sfb_populatePhantomForm(formId, arrIdVal)
{
	for (var i=0;i<arrIdVal.length;i=i+2)
	{
		var optionElement = document.createElement('INPUT');
		optionElement.setAttribute('id', arrIdVal[i]);
		optionElement.setAttribute('name', arrIdVal[i]);
		optionElement.setAttribute('value', arrIdVal[i+1]);
		document.getElementById(formId).appendChild(optionElement);
	}
}

//	Funcion para alternar entre vuelos hacia el Caribe y regionales
function sfb_show_product_type(product_type)
{
	if (product_type=='flight'){
	    ctn_group_id = 'International';
		sfb_populate_citySelect("cbx_flg_departure_city", _c, ctn_group_id, ctn_departure_city)
		if (ctn_use_external_css == "0") {
			document.getElementById("sfb_button_flight").style.backgroundColor  = ctn_color_dark;
			document.getElementById("sfb_button_flight").style.border  = "1px solid " + ctn_color_dark;
			document.getElementById("sfb_button_domestic_flight").style.backgroundColor  = ctn_color_medium;
			document.getElementById("sfb_button_domestic_flight").style.border  = "1px solid " + ctn_color_medium;
		}
		document.getElementById('sfb_rg_flight').checked = true;
	}
	if (product_type=='domestic_flight'){
		ctn_group_id = 'Domestic';
		sfb_populate_citySelect("cbx_flg_departure_city", _c, ctn_group_id, ctn_departure_city)
		if (ctn_use_external_css == "0") {
			document.getElementById("sfb_button_flight").style.backgroundColor  = ctn_color_medium;
			document.getElementById("sfb_button_flight").style.border  = "1px solid " + ctn_color_medium;
			document.getElementById("sfb_button_domestic_flight").style.backgroundColor  = ctn_color_dark;
			document.getElementById("sfb_button_domestic_flight").style.border  = "1px solid " + ctn_color_dark;
		}
		document.getElementById('sfb_rg_domestic_flight').checked = true;
	}
}


//	Funciones del primer paso de la busqueda de disponibilidad de SCT tal cual
function sfb_flg_departure_date_status(date) { 
	return (date.getTime() < new Date(sfb_min_flg_departure_date.valueOf() - 24*60*60*1000).getTime());
}

function sfb_flg_arrival_date_status(date) { 
	return (date.getTime() < new Date(sfb_min_flg_departure_date.valueOf()).getTime());
}

function sfb_synchronize_flg_calendars(cal) {
	if (compareDates(document.frm_sfb_search_box.txt_flg_return_date.value,'d/M/y',document.frm_sfb_search_box.txt_flg_departure_date.value,'d/M/y') == 0) {
		var toUpdateField = document.frm_sfb_search_box.txt_flg_departure_date;
		if (toUpdateField == cal.params.inputField) {
			tempDate = new Date(cal.date.getTime() + 1*24*60*60*1000);
			var complete_return = tempDate.getDate() + "/";
			if (tempDate.getDate()<10) 
			   complete_return = "0" + complete_return;
			if ((tempDate.getMonth() + 1)<10)
			    complete_return  = complete_return + "0"  + (tempDate.getMonth() + 1) + "/";
			else
			    complete_return  = complete_return + (tempDate.getMonth() + 1) + "/";
			complete_return  = complete_return + tempDate.getFullYear();
			document.frm_sfb_search_box.txt_flg_return_date.value = complete_return;
			document.frm_sfb_search_box.returnDate.value  = tempDate.getDate();
			document.frm_sfb_search_box.returnMonth.value = tempDate.getMonth() + 1;
			document.frm_sfb_search_box.returnYear.value  = tempDate.getFullYear();
		} else {
			tempDate = new Date(cal.date.getTime() - 1*24*60*60*1000);
			var complete_depart = tempDate.getDate() + "/";
			if (tempDate.getDate()<10) 
			   complete_depart = "0" + complete_depart;
			if ((tempDate.getMonth() + 1)<10)
			    complete_depart  = complete_depart + "0"  + (tempDate.getMonth() + 1) + "/";
			else
			    complete_depart  = complete_depart + (tempDate.getMonth() + 1) + "/";
			complete_depart  = complete_depart + tempDate.getFullYear();
			document.frm_sfb_search_box.txt_flg_departure_date.value = complete_depart;
			document.frm_sfb_search_box.departureDate.value 	= tempDate.getDate();
			document.frm_sfb_search_box.departureMonth.value = tempDate.getMonth() + 1;
			document.frm_sfb_search_box.departureYear.value 	= tempDate.getFullYear();
		}
	}
}

function sfb_flg_departure_date_changed(calendar) {
	if (calendar.dateClicked) {
		document.frm_sfb_search_box.departureYear.value  	= calendar.date.getFullYear();
		document.frm_sfb_search_box.departureMonth.value 	= calendar.date.getMonth() + 1;
		document.frm_sfb_search_box.departureDate.value   	= calendar.date.getDate();
		sfb_synchronize_flg_calendars(calendar);
	}
}

function sfb_flg_return_date_changed(calendar) {
	if (calendar.dateClicked) {
		document.frm_sfb_search_box.returnYear.value  	= calendar.date.getFullYear();
		document.frm_sfb_search_box.returnMonth.value 	= calendar.date.getMonth() + 1;
		document.frm_sfb_search_box.returnDate.value   	= calendar.date.getDate();
		sfb_synchronize_flg_calendars(calendar);
	}
}

function sfb_change_flg_type(flight_type) {
	if (flight_type=='return_flight'){
		document.frm_sfb_search_box.rg_flg_type[0].checked				= true;
		document.getElementById('sfb_tr_flg_return_date').style.display 	= canSee;
	}
	else{
		document.frm_sfb_search_box.rg_flg_type[1].checked				= true;
		document.getElementById('sfb_tr_flg_return_date').style.display 	= "none";
	}
}


//	Funcion que toma todos los valores del formulario de busqueda y rellena los del formulario de enviar informacion 
//	Envia solo los campos requeridos por SFB
function sfb_searchFlight() {
	f_i = document.frm_sfb_search_box_info;
	f = document.frm_sfb_search_box;
	
	if (ctn_show_city_code == 1 && f.txt_flg_departure_city_title.value.replace(" ","").length == 3)
	   f_i.DeptAirPort.value = f.txt_flg_departure_city_title.value;
	else   
	   f_i.DeptAirPort.value = f.cbx_flg_departure_city.value;
	   
	if (ctn_show_city_code == 1 && f.txt_flg_arrival_city_title.value.replace(" ","").length == 3)
	   f_i.DestAirPort.value = f.txt_flg_arrival_city_title.value;
	else   
	   f_i.DestAirPort.value = f.cbx_flg_arrival_city.value;
	   		
	if (f.cbx_flg_departure_city.value == "") {alert(msg_departure_city);f.cbx_flg_departure_city.focus();return (false);}
	
	if (f.cbx_flg_arrival_city.value == "") {alert(msg_arrival_city);f.cbx_flg_arrival_city.focus();return (false);}	

	if (f_i.DeptAirPort.value == f_i.DestAirPort.value ) {alert(err_txt[0]);return (false);};
	
    f_i.departureDate.value = f.departureDate.value;
	f_i.departureMonth.value = f.departureMonth.value;
	f_i.departureYear.value = f.departureYear.value;
	f_i.Class.value = f.cbx_flg_class.value;
	if (f.rg_flg_type[0].checked){
		f_i.OR.value = "R";

		f_i.returnDate.value = f.returnDate.value;
		f_i.returnMonth.value = f.returnMonth.value;
		f_i.returnYear.value = f.returnYear.value;
	}
	else {
		f_i.OR.value = "O";

		f_i.returnDate.value = "";
		f_i.returnMonth.value = "";
		f_i.returnYear.value = "";
    }
    if (f.sfb_rg_product_type[0].checked) {
        f_i.FlightType.value = "International";
    }
    else {
        f_i.FlightType.value = "Domestic";
    }
	f_i.Adults.value = f.cbx_flg_number_of_adults.value;
	f_i.Children.value = f.cbx_flg_number_of_children.value;
	f_i.Infants.value = f.cbx_flg_number_of_infants.value;

    f_i.CurrCode.value = f.cbxCurr.value;

    f_i.refSite.value = window.location.href;
	
	//frmAction = "http://secure.caribbeantravelnetwork.com/get_availability.aspx";
	frmAction = "http://secure.caribbeantravelnetwork.com/flights/get_availability.aspx";

    f_i.action = frmAction;
	f_i.submit();
}

//	Función para inicialiar los valores del trozo de HTML
//	NOTA: Es obligatorio llamarla al final del HTML
function sfb_initializeSearchBox(){
   
	Calendar.setup({
	inputField     :    "txt_flg_return_date",     
	ifFormat       :    "%d/%m/%Y",     
	button         :    "img_flg_return_date",  
	align          :    "B1",           
	singleClick    :    true,
	electric       :    false,
	onUpdate       :    sfb_flg_return_date_changed,
	dateStatusFunc :    sfb_flg_arrival_date_status	
	});

	Calendar.setup({
	inputField     :    "txt_flg_departure_date",     
	ifFormat       :    "%d/%m/%Y",     
	button         :    "img_flg_departure_date", 
	align          :    "B1",          
	singleClick    :    true,
	electric       :    false,
	onUpdate       :    sfb_flg_departure_date_changed,
	dateStatusFunc :    sfb_flg_departure_date_status	
	});

    var init_depart = sfb_min_flg_departure_date.getDate() + "/";

	if (sfb_min_flg_departure_date.getDate()<10) 
	   init_depart = "0" + init_depart;
	if ((sfb_min_flg_departure_date.getMonth() + 1)<10)
		init_depart  = init_depart + "0"  + (sfb_min_flg_departure_date.getMonth() + 1) + "/";
	else
		init_depart  = init_depart + (sfb_min_flg_departure_date.getMonth() + 1) + "/";
	init_depart  = init_depart + sfb_min_flg_departure_date.getFullYear();

	document.frm_sfb_search_box.txt_flg_departure_date.value = init_depart;
	document.frm_sfb_search_box.departureDate.value  = sfb_min_flg_departure_date.getDate();
	document.frm_sfb_search_box.departureMonth.value  = (sfb_min_flg_departure_date.getMonth() + 1);
	document.frm_sfb_search_box.departureYear.value  =  sfb_min_flg_departure_date.getFullYear();
	
    var init_return = sfb_min_flg_return_date.getDate() + "/";

	if (sfb_min_flg_return_date.getDate()<10) 
	   init_return = "0" + init_return;
	if ((sfb_min_flg_return_date.getMonth() + 1)<10)
		init_return  = init_return + "0"  + (sfb_min_flg_return_date.getMonth() + 1) + "/";
	else
		init_return  = init_return + (sfb_min_flg_return_date.getMonth() + 1) + "/";
	init_return  = init_return + sfb_min_flg_return_date.getFullYear();

	document.frm_sfb_search_box.txt_flg_return_date.value = init_return;
	document.frm_sfb_search_box.returnDate.value  = sfb_min_flg_return_date.getDate();
	document.frm_sfb_search_box.returnMonth.value  = (sfb_min_flg_return_date.getMonth() + 1);
	document.frm_sfb_search_box.returnYear.value  =  sfb_min_flg_return_date.getFullYear();

    var searchflights_box
	searchflights_box = document.getElementById("tbl_sfb_search_box");
	elements = searchflights_box.getElementsByTagName("A");
	for (var i=0; i < elements.length; i++) {
	    if (elements[i].className == "help") {
		    elements[i].href = "#";
			elements[i].onmouseout = function(){htm()};
		}
	}
	
	if (show_currencies == true) 
	   document.getElementById('sfb_tr_curr').style.display 	= canSee;
	else
	   document.getElementById('sfb_tr_curr').style.display 	= "none";
	   
	if (showOfficeID == 1) 
	   document.getElementById('sfb_tr_office').style.display 	= canSee;
	else
	   document.getElementById('sfb_tr_office').style.display 	= "none";
	   
	if (ctn_show_city_code == 0) {
	   document.getElementById('txt_flg_departure_city_title').style.display 	= "none";
	   document.getElementById('txt_flg_arrival_city_title').style.display 	= "none";
	}
	
	document.getElementById("tbl_sfb_search_box").width = ctn_width;
	if (ctn_use_external_css == "0") {
		document.getElementById("img_flg_departure_date").style.border= "1px solid " + ctn_color_dark;
		document.getElementById("img_flg_return_date").style.border= "1px solid " + ctn_color_dark;
		var sfb_cell_out;
		if (sfb_cell_out = document.getElementById("sfb_cell_out")) {
			sfb_cell_out.style.border  = "1px solid " + ctn_color_dark;
			sfb_cell_out.style.backgroundColor  = ctn_color_clear;
		}
		document.getElementById("btn_search_flight").style.border  = "1px solid " + ctn_color_dark;
		document.getElementById("btn_search_flight").style.backgroundColor  =  ctn_color_dark;
	}
	document.getElementById('txt_flg_departure_city_title').maxLength = "3";
	document.getElementById('txt_flg_arrival_city_title').maxLength = "3";
	document.getElementById('txt_flg_departure_city_title').style.width = "3em"
	document.getElementById('txt_flg_arrival_city_title').style.width = "3em"
	
	document.getElementById('img_flg_departure_date').src = ctn_site_from + "\/comun\/calendar/img.gif";
	document.getElementById('img_flg_return_date').src = ctn_site_from + "\/comun\/calendar/img.gif";

	//Arreglo de campos ocultos con los datos que se envían a SFB //window.reseller_id == null
	var arrayParamValues = new Array("commercial_client_code", typeof commercial_client_code != 'undefined' ? commercial_client_code : "",
	    "point_of_sale", typeof point_of_sale != 'undefined' ? point_of_sale : "",
	    "commercial_client_type", typeof commercial_client_type != 'undefined' ? commercial_client_type : "",
	    "reseller_id", typeof reseller_id != 'undefined' ? reseller_id : "", "ctn_site_name", ctn_site_name,
		"DeptAirPort", "", "DestAirPort", "",
		"Class", "",	"OR", "",
		"departureDate", "", "departureMonth", "", "departureYear","", 
		"returnDate", "", "returnMonth" ,"", "returnYear", "",
		"Adults", "", "Children", "", "Infants", "", "CurrCode", "", "ClientCountryCode", "", "refSite", "", "FlightType", "", "language", ctn_language);
	
	sfb_populateSelect('cbxCurr',ctn_currencies, currencyCode, 0);
	sfb_populatePhantomForm('frm_sfb_search_box_info', arrayParamValues);
	
	found = false;
	if ((ctn_departure_city != '') && (ctn_arrival_city != '')){
		for (var i=0; ((i < _c.length) && (!found)); i++) {
			if  (_c[i][0].toLowerCase() == ctn_arrival_city.toLowerCase() || 
			_c[i][1].toLowerCase() == ctn_arrival_city.toLowerCase()) {
				sfb_show_product_type((_c[i][4][0] == 'Domestic' ) ? 'domestic_flight' : 'flight');
				found = true;
			}
		}
		if (!found) sfb_show_product_type((ctn_group_id == 'Domestic' ) ? 'domestic_flight' : 'flight');
	} else {sfb_show_product_type((ctn_group_id == 'Domestic' ) ? 'domestic_flight' : 'flight');}

	sfb_populate_citySelect("cbx_flg_arrival_city", _c, 'Domestic', ctn_arrival_city)
	
	document.getElementById("sfb_button_flight").onclick = function(){sfb_show_product_type('flight')};
	document.getElementById("sfb_button_domestic_flight").onclick = function(){sfb_show_product_type('domestic_flight')};
	document.getElementById("lb_flg_round").onclick = function(){sfb_change_flg_type('return_flight')};
	document.getElementById("lb_flg_one").onclick = function(){sfb_change_flg_type('one_way_flight')};
	document.getElementById("btn_search_flight").onclick = function(){sfb_searchFlight()};
}


function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    //alert(name);
    if (results == null)
        return "";
    else
       // alert(name + ':' + results[1]);
        return results[1];
}


function sfb_paintform(){
document.writeln('<div id="sfb_div"></div>\r\n');
    sfb_innerElement("sfb_div");
}

function sfb_paintform_innerElement(i) {sfb_innerElement(i);};

function setBox() {
    if (gup("DeptAirPort") != "") {
        if (gup("FlightType") != "") {
            ctn_group_id = gup("FlightType");
            if (gup("FlightType") == "International") {
                sfb_show_product_type('flight');
            }
            else {
                sfb_show_product_type('domestic_flight');
            }
        }
        document.frm_sfb_search_box.cbx_flg_departure_city.value = gup("DeptAirPort");
        document.frm_sfb_search_box.departureDate.value = gup("departureDate");
        document.frm_sfb_search_box.departureMonth.value = gup("departureMonth");
        document.frm_sfb_search_box.departureYear.value = gup("departureYear");
        document.frm_sfb_search_box.txt_flg_departure_date.value = gup("departureDate") + "/" + gup("departureMonth") + "/" + gup("departureYear");
        //document.frm_sfb_search_box.txt_flg_return_date.value = gup("returnDate") + "/" + gup("returnMonth") + "/" + gup("returnYear");
        document.frm_sfb_search_box.cbx_flg_class.value = gup("Class");
        if (gup("OR") != "") {
            if (gup("OR") == "R") {
                document.frm_sfb_search_box.returnDate.value = gup("returnDate");
                document.frm_sfb_search_box.returnMonth.value = gup("returnMonth");
                document.frm_sfb_search_box.returnYear.value = gup("returnYear");
                document.frm_sfb_search_box.txt_flg_return_date.value = gup("returnDate") + "/" + gup("returnMonth") + "/" + gup("returnYear");
                document.frm_sfb_search_box.rg_flg_type[0].checked = true;
                sfb_change_flg_type('return_flight');
            }
            else {
                document.frm_sfb_search_box.rg_flg_type[1].checked = true;
                sfb_change_flg_type('one_way_flight');
            }
        }
        document.frm_sfb_search_box.cbx_flg_number_of_adults.value = gup("Adults");
        document.frm_sfb_search_box.cbx_flg_number_of_children.value = gup("Children");
        document.frm_sfb_search_box.cbx_flg_number_of_infants.value = gup("Infants");

        document.frm_sfb_search_box.cbxCurr.value = gup("CurrCode");
        
        document.frm_sfb_search_box.cbx_flg_arrival_city.value = gup("DestAirPort");
    }
}
