/* JS used in bestilling's views */

/* onClick tidspunkt radiobuttons*/
/*
 param radioButton - Simply use "this"
 param date        - Date as a string, ex "Mandag 1. januar 2007 kl: 12:00
 param ticketType  - "Full flex" or "Lavpris"
 param price       - Ex 199
 param direction   - "departure" or "return"

 "is_returning" should be set as a global to indicate retour.
 */
function tidspunktUpdateRight(radioButton, date, ticketType, price, direction) {
	// Change background color to red on tr element
	// - First remove color from all tr elements
	var table = radioButton.parentNode.parentNode.parentNode.parentNode;
	var length = table.rows.length;

	for ( var i = 0; i < length; i++) {
		tr = table.rows[i];
		tr.className = "";
	}

	tr = radioButton.parentNode.parentNode;
	tr.className = "selecteddatered";

	// Show right column
	rightDiv = document.getElementById('right-content');
	rightDiv.className = "";
	// - Show return table?
	if (is_returning) {
		returnDiv = document.getElementById('right-return-div');
		returnDiv.className = "";
	}

	var tableHead = document.getElementById('right-' + direction);
	// Delete rows
	length = tableHead.rows.length;
	for ( var i = 0; i < length; i++) {
		tableHead.deleteRow(0);
	}

	// Build table
	// - Date
	var rowDate = document.createElement('tr');
	var td = document.createElement('td');
	td.setAttribute('colspan', 2);
	td.appendChild(document.createTextNode(date));
	rowDate.appendChild(td);
	tableHead.appendChild(rowDate);

	// - Ticket type and Price
	// - Type
	rowTicket = document.createElement('tr');
	td = document.createElement('td');
	td.setAttribute('width', '70%');
	td.appendChild(document.createTextNode(ticketType));

	// - Price
	var strPrice = addDotToPrice(price);

	td2 = document.createElement('td');
	td2.setAttribute('id', 'right-' + direction + '-cost');
	td2.className = "price";
	td2.appendChild(document.createTextNode(strPrice + ',-'));
	rowTicket.appendChild(td);
	rowTicket.appendChild(td2);
	tableHead.appendChild(rowTicket);

	// - Total price
	var totalcostTd = document.getElementById('right-totalcost');
	var opposite;
	if (direction == "departure") {
		opposite = "return";
	} else {
		opposite = "departure";
	}
	var oppositeCost = document.getElementById('right-' + opposite + '-cost');
	var strOppositeCost = removeDotFromPrice(oppositeCost.innerHTML);

	var strTotalCost = new String(parseInt(strOppositeCost) + parseInt(price));
	// - Add a . to the price if its over 3 digits
	if (strTotalCost.length > 3) {
		var l = strTotalCost.length;
		strTotalCost = strTotalCost.substr(0, l - 3) + '.'
				+ strTotalCost.substr(l - 3);
	}

	totalcostTd.innerHTML = '';
	totalcostTd.innerHTML = strTotalCost + ',-';
}

// - 1200 will become 1.200
function addDotToPrice(price) {
	var strPrice = new String(price);
	if (strPrice.length > 3) {
		var l = strPrice.length;
		strPrice = strPrice.substr(0, l - 3) + '.' + strPrice.substr(l - 3);
		return strPrice;
	} else {
		return strPrice;
	}
}

// - 1.200,- will become 1200,-
function removeDotFromPrice(price) {
	var strPrice = new String(price);
	if (strPrice.length > 3) {
		var priceArr = new Array();
		priceArr = strPrice.split('.');
		strPrice = '';

		for ( var i = 0; i < priceArr.length; i++) {
			if (priceArr[i] != '.')
				;
			{
				strPrice = strPrice + priceArr[i];
			}
		}
	}
	return strPrice;
}

/* onChange luggage dropdowns */
/*
 * param radioButton - Simply use "this" param date - Date as a string, ex
 * "Mandag 1. januar 2007 kl: 12:00 param ticketType - "Full flex" or "Lavpris"
 * param price - Ex 199 param direction - "departure" or "return"
 */

function luggageUpdateRight(dropdown, direction) {
	var kolli = dropdown.value;
	var luggagecost = document.getElementById('right-' + direction
			+ '-luggage-cost');
	var opposite;
	// add_price is GLOBAL and is set inline in reisende.tpl
	luggagecost.innerHTML = '';
	luggagecost.innerHTML = addDotToPrice(parseInt(dropdown.value * add_price))
			+ ',-';

	if (direction == "departure") {
		opposite = "return";
	} else {
		opposite = "departure";
	}
	var oppositeLuggageCost = document.getElementById('right-' + opposite
			+ '-luggage-cost');
	totalLuggageCost = parseInt(removeDotFromPrice(oppositeLuggageCost.innerHTML))
			+ parseInt(dropdown.value * add_price);

	var totaltcostTd = document.getElementById('right-totalcost');
	var strTotalCost = new String(base_price + totalLuggageCost
			+ getCurrentSMSPrice());

	// - Add a . to the price if its over 3 digits
	if (strTotalCost.length > 3) {
		var l = strTotalCost.length;
		strTotalCost = strTotalCost.substr(0, l - 3) + '.'
				+ strTotalCost.substr(l - 3);
	}

	totaltcostTd.innerHTML = '';
	totaltcostTd.innerHTML = strTotalCost + ',-';
}

function getTotalPrice() {
	var e = document.getElementById('right-totalcost');

	if (e !== null) {
		// remove dots and trailing ",-" from price
		var len = e.innerHTML.length;
		return parseInt(removeDotFromPrice(e.innerHTML.substr(0, len - 2)));
	}

	return 0;
}

function setTotalPrice(price_int) {
	var e = document.getElementById('right-totalcost');
	if (e !== null) {
		e.innerHTML = '';
		e.innerHTML = addDotToPrice(price_int) + ',-';
	}
}

// Checks whether SMS is enabled.
function getCurrentSMSPrice() {
	var elem = document.getElementById('right-departure-sms-cost-div');
	var enabled = (elem.className.trim() != "hide");
	return enabled ? sms_price : 0;
}

function smsUpdateRight(enabled) {
	var elem = document.getElementById('right-departure-sms-cost-div');

	if (elem !== null) {
		// sms_price is global and set in reisende.tpl
		var totalPrice = getTotalPrice();
		if (enabled) {
			elem.className = "";
			setTotalPrice(totalPrice + sms_price);
		} else {
			elem.className = "hide";
			setTotalPrice(totalPrice - sms_price);
		}
	}
}

// Called by the wc checkbox in "reisende"
// - show can either be true or false
function changeWcTravellor(travellor, show) {
	var wcRow = document.getElementById('wc-' + travellor);

	if (show) {
		wcRow.className = "";
		wc_travellors++;
	} else {
		wcRow.className = "hide";
		wc_travellors--;
	}

	// Hightlight span#make-contact
	var makeContant = document.getElementById('make-contact');
	if (wc_travellors > 0) {
		makeContant.className = "bold";
	} else {
		makeContant.className = '';
	}
}

