// 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'}
	],
	
	activateTab: function(clickSrc, tabNum){
		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(date1Parts[1]) + "" + this.numToStr(date1Parts[0]) + "0000";
	document.getElementById(this.finalTxtIds.adate).value = date2Parts[2] + "" + this.numToStr(date2Parts[1]) + "" + this.numToStr(date2Parts[0]) + "0000";
}

// Select the drop down option that has the given value
DateHandler.prototype.selectOptionByValue = function(selEmt, 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])), date: this.strToNum(date1Parts[0])};
	var testValues2 = {month: (date2Parts[2] + this.numToStr(date2Parts[1])), 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);
}

// 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)};

	document.getElementById(this.helperTxtIds.ddate).value = this.numToStr(dDDs.date.options[dDDs.date.selectedIndex].value) + "." + this.numToStr(dDDs.month.options[dDDs.month.selectedIndex].value.substr(4,2)) + "." + dDDs.month.options[dDDs.month.selectedIndex].value.substr(0,4);
	document.getElementById(this.helperTxtIds.adate).value = this.numToStr(aDDs.date.options[aDDs.date.selectedIndex].value) + "." + this.numToStr(aDDs.month.options[aDDs.month.selectedIndex].value.substr(4,2)) + "." + aDDs.month.options[aDDs.month.selectedIndex].value.substr(0,4);
	
	document.getElementById(this.finalTxtIds.ddate).value = dDDs.month.options[dDDs.month.selectedIndex].value.substr(0,4) + "" + dDDs.month.options[dDDs.month.selectedIndex].value.substr(4,2) + "" + this.numToStr(dDDs.date.options[dDDs.date.selectedIndex].value) + "0000";
	document.getElementById(this.finalTxtIds.adate).value = aDDs.month.options[aDDs.month.selectedIndex].value.substr(0,4) + "" + aDDs.month.options[aDDs.month.selectedIndex].value.substr(4,2) + "" + this.numToStr(aDDs.date.options[aDDs.date.selectedIndex].value) + "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();
}