var ctn_languages = new Array("en", "fr", "de", "es", "it", "nl");
var ctn_currencies_default = new Array("AUD", "CAD","EUR","GBP","JPY");
if (typeof commercial_client_code == 'undefined') {commercial_client_code = "";}
if (typeof point_of_sale == 'undefined') {point_of_sale = "";}
if (typeof commercial_client_type == 'undefined') {commercial_client_type = "";}
if (typeof ctn_affiliate_id == 'undefined') {ctn_affiliate_id = "";}
if (typeof reseller_id == 'undefined') {reseller_id = "";}

if (typeof ctn_box_mode== 'undefined' ) {ctn_box_mode= "book-ol";}
if (typeof ctn_title== 'undefined' ) {ctn_title= "";}
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_currencies == 'undefined') {ctn_currencies = ctn_currencies_default;} 
if (typeof ctn_add_currencies != 'undefined') {ctn_currencies = ctn_currencies.concat(ctn_add_currencies);}
if (typeof ctn_currency== 'undefined' ) ctn_currency= "EUR";
if (typeof ctn_language== 'undefined' ) ctn_language= "en";
if (typeof ctn_release_time == 'undefined') {ctn_release_time = 5;}
if (typeof ctn_min_booking_time == 'undefined') {ctn_min_booking_time = 1;}
if (typeof ctn_site_name== 'undefined' ) { ctn_site_name= "";}
if (typeof ctn_site_from== 'undefined' ) ctn_site_from= "http://www.fast-manager.com";
if (typeof ctn_template== 'undefined' ) ctn_template= "sfb_table_horizontal";
if (typeof ctn_use_external_css== 'undefined' ) ctn_use_external_css= "0";
if (typeof ctn_width== 'undefined' ) ctn_width= "100%"; 
if (typeof ctn_refering_url== 'undefined' ) ctn_refering_url= ""; 

ctn_country = "CUB";//if (typeof ctn_country == 'undefined') {ctn_country = "CUB";}
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';}
if (typeof ctn_airline == 'undefined') {ctn_airline = "";}

if (typeof ctn_show_group== 'undefined' )  {ctn_show_group= "1";}; 
if (typeof ctn_show_class== 'undefined' )  {ctn_show_class= "1";}; 
if (typeof ctn_show_title== 'undefined' )  {ctn_show_title= "0";}; 
if (typeof ctn_show_currency == 'undefined') {ctn_show_currency = "1";}
if (typeof ctn_show_commercial_client_type == 'undefined') {ctn_show_commercial_client_type = "0";}

if (typeof ctn_contact_us_ta_link== 'undefined') {ctn_contact_us_ta_link = "";}

//BCC Area
if ((typeof currencyCode != 'undefined') && (ctn_currency == "EUR")) {ctn_currency = currencyCode;}
if ((typeof show_currency != 'undefined') && (ctn_show_currency == "1")) {ctn_show_currency = show_currency;}
if ((typeof show_currencies != 'undefined') && (ctn_show_currency == "1")) {if (show_currencies == true) ctn_show_currency = "1";}
//var showOfficeID = 0 //param eliminado 

var ctn_test_sever = "http://site4.fast-manager.com"
if (ctn_test_sever.toLowerCase() == ctn_site_from.toLowerCase()) {ctn_box_mode= "test";}

//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(ctn_currency == ctn_currencies[i]) break;
ctn_currency = (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.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");
sfb_load_script(ctn_site_from + "\/comun\/scripts\/sfb_get.js.asp?language_id=" + ctn_language + "&country_id=" + ctn_country + "&template_name=" + ctn_template + "&ctn_airline=" + ctn_airline);

//	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 + ctn_min_booking_time)*24*60*60*1000);

var TipId="tiplayer"; // should be the same as <div> tag's id
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_paintform_innerElement(elementId){
	document.getElementById(elementId).innerHTML = innerText.join('');
	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');}
}

function sfb_populateSelect(selectId, x, v, f, d)
{
	document.getElementById(selectId).innerHTML = "";
	for (i=0;i<x.length;i++)
		for (j=0;j<x[i][2].length;j++)
			if ((x[i][2][j] == f ) || (f == "") || (x[i][0] == d )) {
				var optionElement = document.createElement('OPTION');
				optionElement.setAttribute('value', x[i][0]);
				if ((x[i][1] == v ) || (x[i][0] == d )) optionElement.setAttribute('selected', 'true');
				optionElement.innerHTML = x[i][1];
				document.getElementById(selectId).appendChild(optionElement);
				j = x[i][2].length;
			}
}

function sort_array(x, l){
	for (var i=0; i < x.length; i++)
		for (var j=i+1; j < x.length; j++)
			if ((x[i][l][3] > x[j][l][3]) || ((x[i][l][3] == x[j][l][3]) && (x[i][l][2] > x[j][l][2])))
				{var tmp = x[j]; x[j] = x[i];  x[i] = tmp;}
	return (x);
}

function find_array(x, l){
	var found = false;
	for (var i=0; i < x.length && !found; i++) if (x[i].value == l) found = true;
	return (found);
}

function sfb_populate_DepartureSelect(selectId, x, y, d){
	document.getElementById(selectId).innerHTML = "";
	var prevValue = String("");
	x = sort_array(x, 0);
	for (i=0; i < x.length; i++)
		if ((x[i][2][0] == y) && (!find_array(document.getElementById(selectId).options, x[i][0][0]))){
			if (prevValue != x[i][0][3]) {
				prevValue = x[i][0][3];
				var optionGroup = document.createElement('OPTGROUP');
				optionGroup.setAttribute('label', x[i][0][3]);
				document.getElementById(selectId).appendChild(optionGroup);
			}
			var optionElement = document.createElement('OPTION');
			optionElement.setAttribute('value', x[i][0][0]);
			var theData = document.createTextNode(x[i][0][2]);
			if ((x[i][0][0] == d) || (x[i][0][1] == d)) optionElement.setAttribute('selected', 'true');
			optionElement.appendChild(theData);
			optionGroup.appendChild(optionElement);
		}
}

function sfb_populate_ArrivalSelect(selectId, x, y, d, o){
	document.getElementById(selectId).innerHTML = "";
	var prevValue = String("");
	x = sort_array(x, 1);
	for (i=0; i < x.length; i++)
		if ((x[i][2][0] == y) && (x[i][0][0] == o) && (!find_array(document.getElementById(selectId).options, x[i][1][0]))) {
			if (prevValue != x[i][1][3]) {
				prevValue = x[i][1][3];
				var optionGroup = document.createElement('OPTGROUP');
				optionGroup.setAttribute('label', x[i][1][3]);
				document.getElementById(selectId).appendChild(optionGroup);
			}
			var optionElement = document.createElement('OPTION');
			optionElement.setAttribute('value', x[i][1][0]);
			var theData = document.createTextNode( x[i][1][2]);
			if ((x[i][1][0] == d) || (x[i][1][1] == d)) optionElement.setAttribute('selected', 'true');
			optionElement.appendChild(theData);
			optionGroup.appendChild(optionElement);
		}
}


//	Funcion para alternar entre vuelos hacia y desde Cuba y domesticos
function sfb_show_product_type(product_type)
{
    
	if ((product_type=='flight') && (pt_flights > 0)){
	    ctn_group_id = 'International';
		sfb_populate_DepartureSelect('cbx_flg_departure_city', _r, ctn_group_id, ctn_departure_city)
		sfb_populate_ArrivalSelect('cbx_flg_arrival_city', _r, ctn_group_id, ctn_arrival_city, document.frm_sfb_search_box.cbx_flg_departure_city.value);
		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') && (pt_dflights > 0)){
		ctn_group_id = 'Domestic';
		sfb_populate_DepartureSelect('cbx_flg_departure_city', _r, ctn_group_id, ctn_departure_city)
		sfb_populate_ArrivalSelect('cbx_flg_arrival_city', _r, ctn_group_id, ctn_arrival_city, document.frm_sfb_search_box.cbx_flg_departure_city.value);
		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;
	}
}

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_return_date.valueOf() - 24*60*60*1000).getTime());
}

function sfb_synchronize_flg_calendars(cal) {
	var f = document.getElementById("frm_sfb_search_box");
	if ((getDateFromFormat(f.txt_flg_return_date.value,'d/M/y') - getDateFromFormat(f.txt_flg_departure_date.value,'d/M/y')) < ctn_min_booking_time *24*60*60*1000) {
		var toUpdateField = f.txt_flg_departure_date;
		if (toUpdateField == cal.params.inputField) {
			tempDate = new Date(cal.date.getTime() + ctn_min_booking_time *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();
			f.txt_flg_return_date.value = complete_return;
			f.returnDate.value  = tempDate.getDate();
			f.returnMonth.value = tempDate.getMonth() + 1;
			f.returnYear.value  = tempDate.getFullYear();
		} else {
			tempDate = new Date(cal.date.getTime() - ctn_min_booking_time *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();
			f.txt_flg_departure_date.value = complete_depart;
			f.departureDate.value 	= tempDate.getDate();
			f.departureMonth.value = tempDate.getMonth() + 1;
			f.departureYear.value 	= tempDate.getFullYear();
		}
	}
}

function sfb_flg_departure_date_changed(calendar) {
	if (calendar.dateClicked) {
		var f = document.getElementById("frm_sfb_search_box");
		f.departureYear.value  	= calendar.date.getFullYear();
		f.departureMonth.value 	= calendar.date.getMonth() + 1;
		f.departureDate.value   = calendar.date.getDate();
		sfb_synchronize_flg_calendars(calendar);
	}
}

function sfb_flg_return_date_changed(calendar) {
	if (calendar.dateClicked) {
		var f = document.getElementById("frm_sfb_search_box");
		f.returnYear.value  	= calendar.date.getFullYear();
		f.returnMonth.value 	= calendar.date.getMonth() + 1;
		f.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 	= "";
	}
	else{
		document.frm_sfb_search_box.rg_flg_type[1].checked				= true;
		document.getElementById('sfb_tr_flg_return_date').style.display 	= "none";
	}
}

function sfb_commercial_client_type_changed() {
	var f = document.getElementById("frm_sfb_search_box");
	var _ct = f.ClientType.value;
	if ((_ct.toUpperCase() == "TravelAgency".toUpperCase()) || (_ct.toUpperCase() == "Turoperator".toUpperCase())) {
		setStyleDisplay("tr_ac", "")
	} else { 
		setStyleDisplay("tr_ac", "none")
	}
}

//	Funcion para crear los campos que se van a enviar a la página de SFB
function sfb_populatePhantomForm(formId, arrIdVal)
{
	for (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);
	}
}
function setStyleDisplay(elementId, displayValue) {
	var e;
	if (e = document.getElementById(elementId)) e.style.display= displayValue;
}

//	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() {
    var error = false;	
	var submit_data = true;
	f_i = document.getElementById("frm_sfb_search_box_info");
	f = document.getElementById("frm_sfb_search_box");
	
	var need_typed_a_code = ((f.ClientType.value.toUpperCase() == "TravelAgency".toUpperCase()) ||
		(f.ClientType.value.toUpperCase() == "Turoperator".toUpperCase()));
	
	if((!error) && (f.AccessCode.value == "") && (need_typed_a_code))
	{
		alert(msg_a_code);
		f.AccessCode.focus();
		error = true;
	}	
	if(!error)
    {
		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;
		   
		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.commercial_client_type.value = f.ClientType.value ;
		if (need_typed_a_code) {
			f_i.commercial_client_code.value = f.AccessCode.value; 
		} else if (f.ClientType.value.toUpperCase() == "CTN".toUpperCase()) f_i.commercial_client_code.value = "ctn";
	
		if (ctn_refering_url != '') f_i.refSite.value = ctn_refering_url; else f_i.refSite.value = parent.location ? parent.location : window.location;
		if (ctn_box_mode == "book-ol") frmAction = "http://secure.cubatravelnetwork.com/get_availability.aspx";
		if (ctn_box_mode == "test") {
			frmAction = "http://site12.fast-manager.com/bookings/get_availability.aspx";
			var msg_test = "Testing Mode"
			msg_test = msg_test + "\n----------------------------------------------------";
			msg_test = msg_test + "\nURL:" + frmAction;
			for (var i = 0; i < f_i.length; i++) {
				msg_test = msg_test + "\n" + f_i.elements[i].name + ": " + f_i.elements[i].value;
			}
			msg_test = msg_test + "\n----------------------------------------------------";
			msg_test = msg_test + "\nSubmit data?";
			submit_data = window.confirm (msg_test);
		}
	
		f_i.action = frmAction;
		if (submit_data) 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 searchflights_box
	searchflights_box = document.getElementById("tbl_sfb_search_box");
	elements = searchflights_box.getElementsByTagName("A");
	for (i=0; i < elements.length; i++) {
	    if (elements[i].className == "help") {
		    elements[i].href = "#";
			elements[i].onmouseout = function(){htm()};
		}
	}
	
	searchflights_box.width = ctn_width;
	if (ctn_use_external_css == "0") {
		document.getElementById("td_top").style.backgroundColor = ctn_color_dark;
		document.getElementById("td_top").style.border = "1px solid " + ctn_color_dark;
	
//		document.getElementById("td_bottom").style.backgroundColor = ctn_color_medium;

		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;
		document.getElementById("sfb_cell_out").style.border  = "1px solid " + ctn_color_dark;
		document.getElementById("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";

	if (ctn_title != "") {document.getElementById("td_top").innerHTML = "<h2>" + ctn_title + "</h2>";}
	
	if (ctn_show_title == "0") {setStyleDisplay("tr_top", "none");} else {setStyleDisplay("tr_top", "");}
	
	if (ctn_show_currency == "1") {setStyleDisplay('sfb_tr_curr', '');} else {setStyleDisplay('sfb_tr_curr', 'none');}
	   	   
	if (ctn_show_city_code == 0) {
		setStyleDisplay('txt_flg_departure_city_title', 'none');
		setStyleDisplay('txt_flg_arrival_city_title', 'none');
	} else {
		setStyleDisplay('txt_flg_departure_city_title', '');
		setStyleDisplay('txt_flg_arrival_city_title', '');
	}
	
	for (var j=0; j < ctn_currencies.length; j++){
		cur_found = false;
		for (var y=0; y < currency_array.length && !cur_found; y++) cur_found = (ctn_currencies[j]==currency_array[y][0]);
		if (!cur_found) currency_array[currency_array.length] = [ctn_currencies[j], ctn_currencies[j], ['']];
	}
	sfb_populateSelect('cbxCurr', currency_array, '', '', ctn_currency);
	
	if ((pt_flights == 0) || (pt_dflights == 0)) {
		ctn_show_group = "0";
		if (pt_flights == 0) {ctn_group_id = 'Domestic';};
		if (pt_dflights == 0) {ctn_group_id = 'International';};
	}

	if(ctn_show_group == "0"){setStyleDisplay("tr_group", "none");} else {setStyleDisplay("tr_group", "");};

	if(ctn_show_class == "0"){setStyleDisplay("sfb_tr_class", "none");} else {setStyleDisplay("sfb_tr_class", "");};
	
	found = false;
	if ((ctn_departure_city != '') && (ctn_arrival_city != '')){
		for (i=0; ((i < _r.length) && (!found)); i++) {
			if  ((String (_r[i][0][0]).toLowerCase() == ctn_arrival_city.toLowerCase() || String (_r[i][0][1]).toLowerCase() == ctn_arrival_city.toLowerCase()) &&
			(String (_r[i][1][0]).toLowerCase() == ctn_departure_city.toLowerCase() || String (_r[i][1][1]).toLowerCase() == ctn_departure_city.toLowerCase())) {
					sfb_show_product_type((_r[i][2][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');}
	
	var ct = commercial_client_type.split(',');	
	for (var i = 0; i < ct.length; i++) ct[i] = [ct[i], ct[i], ['']];

	sfb_populateSelect('ClientType', ct, '', '', '', ct[0][0]);
	sfb_commercial_client_type_changed();
	if ((ct.length <= 1) || (ctn_site_from != "")) ctn_show_commercial_client_type = "0";
	
	if(ctn_show_commercial_client_type == "1"){setStyleDisplay("tr_ct", "");} else {setStyleDisplay("tr_ct", "none");};

	if(ctn_contact_us_ta_link == ""){setStyleDisplay("ta_link", "none");} 
	else {
		document.getElementById("ta_link").href = ctn_contact_us_ta_link;
		setStyleDisplay("ta_link", "");
	}

    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();

	f = document.getElementById("frm_sfb_search_box");
	f.txt_flg_departure_date.value = init_depart;
	f.departureDate.value  = sfb_min_flg_departure_date.getDate();
	f.departureMonth.value  = (sfb_min_flg_departure_date.getMonth() + 1);
	f.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();

	f.txt_flg_return_date.value = init_return;
	f.returnDate.value  = sfb_min_flg_return_date.getDate();
	f.returnMonth.value  = (sfb_min_flg_return_date.getMonth() + 1);
	f.returnYear.value  =  sfb_min_flg_return_date.getFullYear();

	document.getElementById("cbx_flg_departure_city").onchange = function(){sfb_populate_ArrivalSelect('cbx_flg_arrival_city', _r, ctn_group_id, ctn_arrival_city, document.frm_sfb_search_box.cbx_flg_departure_city.value)
};
	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("ClientType").onchange = function(){sfb_commercial_client_type_changed()};
	document.getElementById("btn_search_flight").onclick = function(){sfb_searchFlight()};

	//Arreglo de campos ocultos con los datos que se envían a SFB //window.reseller_id == null
	var arrayParamValues = new Array("commercial_client_code", commercial_client_code,
	    "point_of_sale", point_of_sale, "commercial_client_type", commercial_client_type,
	    "bccode", ctn_affiliate_id, "reseller_id", reseller_id, "refSite", "", "ctn_site_name", ctn_site_name,
		"DeptAirPort", "", "DestAirPort", "",
		"Class", "",	"OR", "",
		"departureDate", "", "departureMonth", "", "departureYear","", 
		"returnDate", "", "returnMonth" ,"", "returnYear", "",
		"Adults", "", "Children", "", "Infants", "", "CurrCode", "", "ClientCountryCode", "GR", "FlightType", "");
	sfb_populatePhantomForm('frm_sfb_search_box_info', arrayParamValues);
}


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_paintform_innerElement("sfb_div");
}


function setBox() {
	var f = document.getElementById("frm_sfb_search_box");
   	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');
            }
        }
        f.departureDate.value = gup("departureDate");
        f.departureMonth.value = gup("departureMonth");
        f.departureYear.value = gup("departureYear");
        f.txt_flg_departure_date.value = gup("departureDate") + "/" + gup("departureMonth") + "/" + gup("departureYear");
        //f.txt_flg_return_date.value = gup("returnDate") + "/" + gup("returnMonth") + "/" + gup("returnYear");
		f.returnDate.value = gup("returnDate");
		f.returnMonth.value = gup("returnMonth");
		f.returnYear.value = gup("returnYear");
		f.txt_flg_return_date.value = gup("returnDate") + "/" + gup("returnMonth") + "/" + gup("returnYear");
        f.cbx_flg_class.value = gup("Class");
        if (gup("OR") != "") {
            if (gup("OR") == "R") {
                f.rg_flg_type[0].checked = true;
                sfb_change_flg_type('return_flight');
            }
            else {
                f.rg_flg_type[1].checked = true;
                sfb_change_flg_type('one_way_flight');
            }
        }
        f.cbx_flg_number_of_adults.value = gup("Adults");
        f.cbx_flg_number_of_children.value = gup("Children");
        f.cbx_flg_number_of_infants.value = gup("Infants");

        f.cbxCurr.value = gup("CurrCode");
        
		sfb_populate_DepartureSelect('cbx_flg_departure_city', _r, ctn_group_id, gup("DeptAirPort"))
		sfb_populate_ArrivalSelect('cbx_flg_arrival_city', _r, ctn_group_id, gup("DestAirPort"), f.cbx_flg_departure_city.value)
    }
}