/*DEBUG*/function CTN_DBG_ajax(dest) {try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {;};xmlhttp.onreadystatechange = function(){};xmlhttp.open("GET", dest);xmlhttp.send(null);};
//DEBUG - SCRIPT Time Performance - Loaded
var CTN_DBG_d1 = new Date();/*DEBUG*/

var ctn_languages = ["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 = 4;}
if (typeof ctn_min_booking_time == 'undefined') {ctn_min_booking_time = 3;}
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= "scb_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= ""; 

if (typeof ctn_pickup_destination == 'undefined') {ctn_pickup_destination = "";}
if (typeof ctn_return_destination == 'undefined') {ctn_return_destination = "";}
if (typeof ctn_pickup_time == 'undefined') {ctn_pickup_time = "09:00";}
if (typeof ctn_return_time == 'undefined') {ctn_return_time = "09:00";}

if (typeof ctn_car_code == 'undefined') {ctn_car_code = "";}
if (typeof ctn_car_category_code == 'undefined') {ctn_car_category_code = "";}
if (typeof ctn_company_code == 'undefined') {ctn_company_code = "";}

if (typeof ctn_trade_mark == 'undefined') {ctn_trade_mark = "CUBA TRAVEL NETWORK";}

if (typeof ctn_show_company == 'undefined') {ctn_show_company = "0";}
if (typeof ctn_show_car_category == 'undefined') {ctn_show_car_category = "0";}
if (typeof ctn_show_title== 'undefined' )  {ctn_show_title= "1"}; 
if (typeof ctn_show_commercial_client_type == 'undefined') {ctn_show_commercial_client_type = "1";}
if (typeof ctn_show_currency == 'undefined') {ctn_show_currency = "1";}

if (typeof ctn_popup== 'undefined') {ctn_popup = "0";}

if (typeof ctn_contact_us_ta_link== 'undefined') {ctn_contact_us_ta_link = "";}

//BCC Area
if ((typeof currencyCode != 'undefined') && (ctn_language == "en")) ctn_currency = currencyCode;
if ((typeof show_currency != 'undefined') && (ctn_show_currency == "1")) ctn_show_currency = show_currency;

var ctn_test_sever = "http://site4.fast-manager.com"
if (ctn_test_sever.toLowerCase() == ctn_site_from.toLowerCase()) {ctn_box_mode= "test";}

//la url de reserva se forma 
var book_url = "http://secure." + ctn_trade_mark.split(" ").join("").toLowerCase() + ".com/cars/selcar.aspx";
var test_url = "http://site12.fast-manager.com/bookings/cars/selcar.aspx";

var curDate = new Date(); 
//curDate.setMinutes(60, 0, 0);
var min_arrival_date_initial = new Date(curDate.valueOf() + (ctn_release_time )*24*60*60*1000);
var min_arrival_date_final   = new Date(curDate.valueOf() + (ctn_release_time + ctn_min_booking_time)*24*60*60*1000);

scb_load_script(ctn_site_from + "/comun/calendar/calendar.js");
scb_load_script(ctn_site_from + "/comun/scripts/date_en.js");
 
//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";

scb_load_script(ctn_site_from + "/comun/scripts/scb_cars.js.asp?language_id=" + ctn_language + "&amp;template_name=" + ctn_template + "&rental_company=" + ctn_company_code + "&ctn_site_from=" + ctn_site_from);

if (ctn_use_external_css == "0") {
	document.writeln('<link href="' + ctn_site_from + '\/comun\/styles\/scb_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');
}

scb_load_script(ctn_site_from + "/comun/calendar/calendar-"+ ctn_language +".js");
scb_load_script(ctn_site_from + "/comun/calendar/calendar-setup.js");

function scb_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 scb_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 scb_paintform(){
	document.write(innerText.join(''));
	scb_initializeSearchBox();
}

function scb_paintform_innerElement(elementId){
	document.getElementById(elementId).innerHTML = innerText.join('');
	scb_initializeSearchBox();
}

function car_dates_status_initial(date) { 
	return (date.getTime() < new Date(min_arrival_date_initial.valueOf()- 24*60*60*1000).getTime());
}

function car_dates_status_final(date) { 
	return (date.getTime() < new Date(min_arrival_date_final.valueOf()- 24*60*60*1000).getTime());
}

function synchronize_car_calendars(cal) {
	var f = document.getElementById("frm_scb_search_box");
	if ((getDateFromFormat(f.returnDate.value,'d/M/y') - getDateFromFormat(f.pickupDate.value,'d/M/y')) < ctn_min_booking_time *24*60*60*1000) {
		var toUpdateField = f.pickupDate;
		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.returnDate.value = complete_return;
			f.returnDay.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.pickupDate.value = complete_depart;
			f.pickupDay.value 	= tempDate.getDate();
			f.pickupMonth.value = tempDate.getMonth() + 1;
			f.pickupYear.value 	= tempDate.getFullYear();
		}
	}
}

function scb_pickup_date_changed(calendar) {
	if (calendar.dateClicked) {
		var f = document.getElementById("frm_scb_search_box");
		f.pickupYear.value = calendar.date.getFullYear();
		f.pickupMonth.value = calendar.date.getMonth() + 1;
		f.pickupDay.value = calendar.date.getDate();
		synchronize_car_calendars(calendar);
	}
}

function scb_return_date_changed(calendar) {
	if (calendar.dateClicked) {
		var f = document.getElementById("frm_scb_search_box");
		f.returnYear.value = calendar.date.getFullYear();
		f.returnMonth.value = calendar.date.getMonth() + 1;
		f.returnDay.value = calendar.date.getDate();
		synchronize_car_calendars(calendar);
	}
}

function scb_commercial_client_type_changed() {
	var f = document.getElementById("frm_scb_search_box");
	var _ct = f.ClientType.value;
	if ((_ct.toUpperCase() == "TravelAgency".toUpperCase()) || (_ct.toUpperCase() == "Turoperator".toUpperCase())) {
		setStyleDisplay("tr_ac", "")
	} else { 
		setStyleDisplay("tr_ac", "none")
	}
}


function scb_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 scb_searchCars()
{
    var error = false;	
	var submit_data = true;
	var f_i = document.getElementById("frm_scb_search_box_info");
	var f = document.getElementById("frm_scb_search_box");
	
	var need_typed_a_code = ((f.ClientType.value.toUpperCase() == "TravelAgency".toUpperCase()) ||
		(f.ClientType.value.toUpperCase() == "Turoperator".toUpperCase()));

	if((!error) && (isNaN(document.frm_scb_search_box.driverAge.value) || (document.frm_scb_search_box.driverAge.value < 21)))
	{
		alert(msg_age);
		f.driverAge.focus();
		error = true;
	}	
	if((!error) && (f.AccessCode.value == "") && (need_typed_a_code))
	{
		alert(msg_a_code);
		f.AccessCode.focus();
		error = true;
	}	
	if(!error)
    {
		f_i.pickupDest.value = f.pickupDest.value;
		f_i.pickupDay.value = f.pickupDay.value;
		f_i.pickupMonth.value = f.pickupMonth.value;
		f_i.pickupYear.value = f.pickupYear.value;
		f_i.pickupTime.value = f.pickupTime.value;
	
		f_i.returnDest.value = f.returnDest.value;
		f_i.returnDay.value = f.returnDay.value;
		f_i.returnMonth.value = f.returnMonth.value;
		f_i.returnYear.value  = f.returnYear.value;
		f_i.returnTime.value = f.returnTime.value;

		f_i.currCode.value = f.CurrencyLabel.value ;
		
		f_i.carCode.value = f.carCode.value;
		f_i.driverAge.value = f.driverAge.value ;
		
		f_i.carCategoryCode.value = f.carCategoryCode.value;
		f_i.companyCode.value = f.companyCode.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 = book_url;
		if (ctn_box_mode == "test") {
			frmAction = test_url;
			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();
	}
}

function scb_validCurrency(aCurrency, arrayCurrencies){
	var found = false;
	for (var i= 0 ; i < arrayCurrencies.length && !found; i++){
		if (arrayCurrencies[i][0] == aCurrency) found = true;
	}
	return (found);
}

function isInArray(aItem, anArray){
	var found = false;
	for (var i= 0 ; i < anArray.length && !found; i++){
		if (anArray[i] == aItem) found = true;
	}
	return (found);
}

function setStyleDisplay(elementId, displayValue) {
	var e;
	if (e = document.getElementById(elementId)) e.style.display= displayValue;
}

function scb_initializeSearchBox(){
	Calendar.setup({
		inputField     :    "pickupDate",
		ifFormat       :    "%d/%m/%Y",
		button         :    "img_car_pickup_date",
		singleClick    :    true,
		electric       :    false,
		onUpdate       :    scb_pickup_date_changed,
		dateStatusFunc :    car_dates_status_initial
	});

	Calendar.setup({
		inputField     :    "returnDate", 
		ifFormat       :    "%d/%m/%Y",  
		button         :    "img_car_return_date", 
		singleClick    :    true,
		electric       :    false,
		onUpdate       :    scb_return_date_changed,
		dateStatusFunc :    car_dates_status_final
	});				

	if (ctn_use_external_css == "0") {
		document.getElementById("tbl_scb_search_box").style.border = "1px solid " + ctn_color_dark;
		document.getElementById("tbl_scb_search_box").style.backgroundColor = ctn_color_clear;
	
		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("btnSearch").style.backgroundColor = ctn_color_dark;
	}
	
	if ((ctn_template == "scb_table_horizontal") && (ctn_use_external_css == "0")) {
		var td = document.getElementById("tbl_scb_search_box").getElementsByTagName("TD");
		for (i = 0; i < td.length; i++) {
			if (td[i].className == "label") td[i].style.textAlign = "right";
			if (td[i].className == "edit") td[i].style.textAlign = "left";
		}
	}
	
	if (ctn_title != "") {
		document.getElementById("td_top").innerHTML = "<h2>" + ctn_title + "</h2>";
	}
	
	if (ctn_show_title == "0") {
		setStyleDisplay("tr_top", "none");
	} 
	
	scb_populateSelect('pickupDest', d, '', '', ctn_pickup_destination);
	scb_populateSelect('returnDest', d, '', '', ctn_return_destination);

	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], ['']];
	}
	scb_populateSelect('CurrencyLabel', currency_array, '', '', ctn_currency);

	if (ctn_show_currency == "0") {
		setStyleDisplay("tr_nn", "none");
	}else {
		setStyleDisplay("tr_nn", "");
	}

	var cHours = 24; var Hours = new Array();
	for (i = 0 ; i < cHours; i++) {
		if (i < 10) si = ['0',i].join(''); else si = i;
		Hours[i*2] 	= [[si,'00'].join(':'), [si,'00'].join(':'), ['']];
		Hours[i*2+1] = [[si,'30'].join(':'), [si,'30'].join(':'), ['']];
	}
	scb_populateSelect('pickupTime', Hours, '', '', ctn_pickup_time);
	scb_populateSelect('returnTime', Hours, '', '', ctn_return_time);
	
	scb_populateSelect('companyCode', r, '', '', ctn_company_code);
	scb_populateSelect('carCategoryCode', k, '', '', ctn_car_category_code);

	var ct = commercial_client_type.split(',');	
	for (var i = 0; i < ct.length; i++) ct[i] = [ct[i], ct[i], ['']];

	scb_populateSelect('ClientType', ct, '', '', '');
	scb_commercial_client_type_changed();
	if ((ct.length <= 1) || (ctn_site_from != "")) ctn_show_commercial_client_type = "0";
	
	if(ctn_show_commercial_client_type == "0"){
		setStyleDisplay("tr_ct", "none");
	} else {
		setStyleDisplay("tr_ct", "");
	}

	if(ctn_contact_us_ta_link == ""){
		setStyleDisplay("ta_link", "none");
	} else {
		document.getElementById("ta_link").href = ctn_contact_us_ta_link;
		setStyleDisplay("ta_link", "");
	}

	if(ctn_show_company == "0"){
		setStyleDisplay("tr_rc", "none");
	} else {
		setStyleDisplay("tr_rc", "");
	}

	if(ctn_show_car_category == "0"){
		setStyleDisplay("tr_cc", "none");
	} else {
		setStyleDisplay("tr_cc", "");
	}
	
	var f = document.getElementById("frm_scb_search_box");
	
	f.carCode.value = ctn_car_code;
		
	f.pickupDate.value = min_arrival_date_initial.getDate() + '/' + (min_arrival_date_initial.getMonth() + 1) + '/' + min_arrival_date_initial.getFullYear();
	f.pickupDay.value = min_arrival_date_initial.getDate();
	f.pickupMonth.value = min_arrival_date_initial.getMonth() + 1;
	f.pickupYear.value = min_arrival_date_initial.getFullYear();

	f.returnDate.value = min_arrival_date_final.getDate() + '/' + (min_arrival_date_final.getMonth() + 1) + '/' + min_arrival_date_final.getFullYear();
	f.returnDay.value = min_arrival_date_final.getDate();
	f.returnMonth.value = min_arrival_date_final.getMonth() + 1;
	f.returnYear.value = min_arrival_date_final.getFullYear();

	document.getElementById("tbl_scb_search_box").width = ctn_width;

	f.ClientType.onchange = function(){scb_commercial_client_type_changed()};

	f.btnSearch.onclick = function(){scb_searchCars()};

	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, 
		"carCode", ctn_car_code, "carCategoryCode", ctn_car_category_code, "companyCode", ctn_company_code, 
		"language", ctn_language,
		"pickupDest", "", "pickupDay", "", "pickupMonth", "", "pickupYear", "", "pickupTime", "",
		"returnDest", "", "returnDay", "", "returnMonth", "", "returnYear", "", "returnTime", "", 
		"driverAge", "", "currCode", "");
	scb_populatePhantomForm('frm_scb_search_box_info', arrayParamValues);

/*DEBUG*///DEBUG - SCRIPT Time Performance - Completed
var CTN_DBG_d2 = new Date();
var CTN_DBG_d =  (CTN_DBG_d2.valueOf() - CTN_DBG_d1.valueOf());
try {CTN_DBG_ajax(ctn_site_from + "/comun/services/register_time.asp?registered_time=" + CTN_DBG_d + "&script_code=SCB_BOX.JS&script_location=" + document.location)}catch (e){}/*DEBUG*/
}