// Micro Booking Engine UI functions

var MBE = {
	tabDetails: [
		{formId: 'formwrapper_1', tabId: 'tabLink1', offImage: '/stc/uusi_etusivu/tab2_package_off.gif', onImage: '/stc/uusi_etusivu/tab2_package.gif'},
		{formId: 'formwrapper_2', tabId: 'tabLink2', offImage: '/stc/uusi_etusivu/tab2_flight.gif', onImage: '/stc/uusi_etusivu/tab2_flight_on.gif'},
		{formId: 'formwrapper_3', tabId: 'tabLink3', offImage: '/stc/uusi_etusivu/tab2_hotel.gif', onImage: '/stc/uusi_etusivu/tab2_hotel_on.gif'}
	],
	
	activateTab: function(clickSrc, tabNum){
		/*if(tabNum == 3)
		{
			var iframe = document.getElementById('hotelzonIframe');
			if(iframe.src == '')
				iframe.src = 'http://book.hotelzon.com/portallogin?loginname=kaleva_leisure&password=kaleva_leisure&domain=kt';
		}*/

		for(var i = 0; i < 3; i++)
		{
			var otherImg = document.getElementById(this.tabDetails[i].tabId).getElementsByTagName('img')[0];
			otherImg.src = this.tabDetails[i].offImage;
			document.getElementById(this.tabDetails[i].formId).style.display = "none";
		}
		var img = clickSrc.getElementsByTagName('img')[0];
		img.src = this.tabDetails[tabNum - 1].onImage;
		document.getElementById(this.tabDetails[tabNum - 1].formId).style.display = "block";

/*
		var otherIdx = tabNum % 2;
		var img = clickSrc.getElementsByTagName('img')[0];
		var otherImg = document.getElementById(this.tabDetails[otherIdx].tabId).getElementsByTagName('img')[0];
		
		img.src = this.tabDetails[tabNum - 1].onImage;
		otherImg.src = this.tabDetails[otherIdx].offImage;
		
		document.getElementById(this.tabDetails[otherIdx].formId).style.display = "none";
		document.getElementById(this.tabDetails[tabNum - 1].formId).style.display = "block";
*/
	}
};





// Micro Booking Engine datepicker helper class
function DateHandler(){}
DateHandler.prototype.helperTxtIds = {ddate: "helperStartDate", adate: "helperEndDate"}; // Helper text field ids (datepicker updates these fields)
DateHandler.prototype.finalTxtIds = {ddate: "frm1_B_DATE_1", adate: "frm1_B_DATE_2"}; // Ids of the fields that hold the final departure and arrival dates and times
DateHandler.prototype.ddIds = {ddate: "L_PV", dmonth: "L_KK", adate: "T_PV", amonth: "T_KK"}; // Drop down ids
DateHandler.prototype.datepicker = null; // Datepicker object
DateHandler.prototype.paddedDates = false; // Do the date drop down values have leading zeros?

// Helper function to left pad numbers with leading zeros and convert them to strings
DateHandler.prototype.numToStr = function(num, len){
	var testLen = (typeof(len) != 'undefined') ? len : 2;
	var numStr = ("" + num).replace(/\s/g, "");
	
	while(numStr.length < testLen){
		numStr = "0" + numStr;
	}
	
	return numStr;
}

// Helper function to strip leading zeros from strings
DateHandler.prototype.strToNum = function(str){
	while(str.length > 1 && str.charAt(0) == '0'){
		str = str.substr(1);
	}
	
	return str;
}

// Apply dates to a) drop downs and b) the final date fields
DateHandler.prototype.applyDates = function(){
	var date1Parts = document.getElementById(this.helperTxtIds.ddate).value.split('.');
	var date2Parts = document.getElementById(this.helperTxtIds.adate).value.split('.');
	
	var combined = date1Parts.concat(date2Parts);
	var errors = 0;
	
	for(var i = 0; i < combined.length; i++){
		var value = this.numToStr(combined[i]);
		var result = value.match(/^\d{2,4}$/);
		if(!result || result == null)
			errors++;
	}
	
	this.updateDropdowns();
	
	document.getElementById(this.finalTxtIds.ddate).value = date1Parts[2] + "" + this.numToStr(parseInt(date1Parts[1]), 2) + "" + this.numToStr(date1Parts[0]) + "0000";
	document.getElementById(this.finalTxtIds.adate).value = date2Parts[2] + "" + this.numToStr(parseInt(date2Parts[1]), 2) + "" + this.numToStr(date2Parts[0]) + "0000";
}

// Select the drop down option that has the given value
DateHandler.prototype.selectOptionByValue = function(selEmt, value){
/*if(value % 1 == 0 && value.length == 12)
{
//	value = value.substring(0, 6);
}
//if(selEmt.id == 'L_KK2') alert("L_KK2=" + value);
//if(selEmt.id == 'L_KK2') alert("T_KK2=" + value);
*/
	for(var i = 0; i < selEmt.options.length; i++){
		if(selEmt.options[i].value == "" + value)
			selEmt.options[i].selected = true;
		else
			selEmt.options[i].selected = false;
	}
}

// Update drop downs to match the helper field values
DateHandler.prototype.updateDropdowns = function(){			
	var date1Parts = document.getElementById(this.helperTxtIds.ddate).value.split('.');
	var date2Parts = document.getElementById(this.helperTxtIds.adate).value.split('.');
	
	var testValues1 = {month: (date1Parts[2] + "" + this.numToStr(date1Parts[1], 2)), date: this.strToNum(date1Parts[0])};
	var testValues2 = {month: (date2Parts[2] + "" + this.numToStr(date2Parts[1], 2)), date: this.strToNum(date2Parts[0])};
	
	var dDDs = {date: document.getElementById(this.ddIds.ddate), month: document.getElementById(this.ddIds.dmonth)};
	var aDDs = {date: document.getElementById(this.ddIds.adate), month: document.getElementById(this.ddIds.amonth)};
	
	this.selectOptionByValue(dDDs.date, testValues1.date);
	this.selectOptionByValue(dDDs.month, testValues1.month);
	this.selectOptionByValue(aDDs.date, testValues2.date);
	this.selectOptionByValue(aDDs.month, testValues2.month);
}

DateHandler.prototype.monthNameToNum = function(name){
	if(name == 'tammikuu')		return '01';
	else if(name == 'helmikuu')	return '02';
	else if(name == 'maaliskuu')	return '03';
	else if(name == 'huhtikuu')	return '04';
	else if(name == 'toukokuu')	return '05';
	else if(name == 'kesäkuu')	return '06';
	else if(name == 'heinäkuu')	return '07';
	else if(name == 'elokuu')	return '08';
	else if(name == 'syyskuu')	return '09';
	else if(name == 'lokakuu')	return '10';
	else if(name == 'marraskuu')	return '11';
	else if(name == 'joulukuu')	return '12';
	return name;
}
// Update a) helper text fields, b) final date fields, c) datepicker calendars and d) dropdowns
DateHandler.prototype.updateTextFields = function(){
	var dDDs = {date: document.getElementById(this.ddIds.ddate), month: document.getElementById(this.ddIds.dmonth)};
	var aDDs = {date: document.getElementById(this.ddIds.adate), month: document.getElementById(this.ddIds.amonth)};

	var dV1 = dDDs.date.options[dDDs.date.selectedIndex].value;
	var aV1 = aDDs.date.options[aDDs.date.selectedIndex].value;
	var dV2 = dDDs.month.options[dDDs.month.selectedIndex].value;
	var aV2 = aDDs.month.options[aDDs.month.selectedIndex].value;
	var dV3 = dDDs.month.options[dDDs.month.selectedIndex].value;
	var aV3 = aDDs.month.options[aDDs.month.selectedIndex].value;
	/*if(aV2.indexOf(" ") > 0)
	{
		var tmp = aV2.split(" ");
		aV2 = this.monthNameToNum(tmp[0]);
		aV3 = this.monthNameToNum(tmp[1]);
	}
	if(dV2.indexOf(" ") > 0)
	{
		var tmp = dV2.split(" ");
		dV2 = this.monthNameToNum(tmp[0]);
		dV3 = this.monthNameToNum(tmp[1]);
	}*/
	dV2 = dV2.substr(4,2);
	aV2 = aV2.substr(4,2);
	dV3 = dV3.substr(0,4);
	aV3 = aV3.substr(0,4);
/*	if(aV2 == '')	aV2 = '01';
	if(dV2 == '')	dV2 = '01';
alert(aV1 + "." + aV2 + "." + aV3 + "\n\n" + dV1 + "." + dV2 + "." + dV3);
*/
	document.getElementById(this.helperTxtIds.ddate).value = this.numToStr(dV1) + "." + this.numToStr(dV2, 2) + "." + dV3;
	document.getElementById(this.helperTxtIds.adate).value = this.numToStr(aV1) + "." + this.numToStr(aV2, 2) + "." + aV3;
	
	document.getElementById(this.finalTxtIds.ddate).value = dV3 + "" + this.numToStr(dV2, 2) + "" + this.numToStr(dV1) + "0000";
	document.getElementById(this.finalTxtIds.adate).value = aV3 + "" + this.numToStr(aV2, 2) + "" + this.numToStr(aV1) + "0000";
	
	// Uncool: explicitly update datepickers to use the new dates
	for(var i = 0; i < this.datepicker.calendars.length; i++){
		this.datepicker.changed(this.datepicker.calendars[i]);
	}
	
	this.updateDropdowns();
}
