var CO2locations = new Array();
var company = '';
function addJavascript(jsname, pos) {
	var th = document.getElementsByTagName(pos)[0];
	var s = document.createElement('script');
	s.setAttribute('type', 'text/javascript');
	s.setAttribute('src', jsname);
	th.appendChild(s);
}
function getCities(countryID, nodeID, selCityID) {
	document.getElementById('AJAXbusy').style.visibility = 'visible';
	var url = baseURL
			+ '/modules/GreenCertTransport/travel/getcities/countryID/'
			+ countryID + '/nodeID/' + nodeID + '/selCityID/' + selCityID + '/co/' + company;
	var request = new Request.JSON( {
		url :url,
		onComplete : function(jsonObj) {
			citiesArrived(jsonObj);
		}
	}).send();

}
function citiesArrived(jsonObj) {
	document.getElementById('AJAXbusy').style.visibility = 'hidden';
	var cities = jsonObj.cities;
	var selCityID = jsonObj.selCityID;
	var cityNode = document.getElementById('CO2ci' + jsonObj.nodeID);
	cityNode.options.length = 0;
	var selNodeID = 0;
	for ( var i = 0; i < cities.length; ++i) {
		var city = cities[i];
		cityNode.options[cityNode.options.length] = new Option(city.city,
				city.id);
		if (!selNodeID && selCityID == city.id) {
			selNodeID = cityNode.options.length - 1;
		}
	}
	if (selNodeID) {
		cityNode.options.selectedIndex = selNodeID;
	}
}

function addLocation(countryID, cityID) {
	var locationCounter = CO2locations.length;
	var newNode = document.createElement('div');
	newNode.id = 'CO2location' + locationCounter;
	var countrySel = document.createElement('select');
	countrySel.id = 'CO2co' + locationCounter;
	countrySel.style.marginRight = '10px';
	countrySel.style.marginBottom = '10px';
	// countrySel.style.color = '#6ca524';
	countrySel.style.fontFamily = 'arial';
	countrySel.style.fontSize = '9pt';
	selID = 0;
	for ( var i in countries) {
		var newOption = new Option(countries[i][1], countries[i][0]);
		countrySel.options[countrySel.options.length] = newOption;
		if (!selID && countryID == countries[i][0]) {
			selID = countrySel.options.length - 1;
		}
	}
	countrySel.onchange = function() {
		getCities(this.options[this.options.selectedIndex].value, this.id
				.substring(5), cityID);
	};
	newNode.appendChild(countrySel);
	var citySel = document.createElement('select');
	citySel.id = 'CO2ci' + locationCounter;
	citySel.style.marginRight = '10px';
	citySel.style.marginBottom = '10px';
	// citySel.style.color = '#6ca524';
	citySel.style.fontFamily = 'arial';
	citySel.style.fontSize = '9pt';
	newNode.appendChild(citySel);
	if (countryID) {
		countrySel.options.selectedIndex = selID; // 50; //Denmark
		countrySel.onchange();
	}
	switch (CO2locations.length) {
	case 0:
		document.getElementById('CO2depart').appendChild(newNode);
		break;
	case 1:
		document.getElementById('CO2dest').appendChild(newNode);
		break;
	default:
		var removeNode = document.createElement('span');
		removeNode.id = 'CO2r' + locationCounter;
		removeNode.innerHTML = '<a style="text-decoration:none; color: #999999;" href="#" onclick="removeLocation(' + locationCounter + ')">&times;</a>';
		newNode.appendChild(removeNode);
		document.getElementById('CO2stopovers').appendChild(newNode);
	}
	CO2locations[CO2locations.length] = locationCounter;

}

function removeLocation(rID) {
	var rnode = document.getElementById('CO2location' + rID);
	if (rnode) {
		document.getElementById('CO2stopovers').removeChild(rnode);
	}
	for ( var i in CO2locations) {
		if (CO2locations[i] == rID) {
			CO2locations.splice(i, 1);
			break;
		}
	}
}

function initCO2(country1, city1, country2, city2, co) {
	company = co;
	var el = document.getElementById('CO2calc');
	el.style.fontFamily = 'arial';
	el.style.fontSize = '10pt';
	el.style.width = '500px';
	getLocSel(country1, city1, country2, city2);

}

function getLocSel(country1, city1, country2, city2) {
	if (country1 == null) {
		country1 = 0;
	}
	if (city1 == null) {
		city1 = 0;
	}
	if (country2 == null) {
		country2 = 0;
	}
	if (city2 == null) {
		city2 = 0;
	}

	var req = new Request.HTML( {
		url :baseURL + '/modules/GreenCertTransport/travel/locsel/country1/'
				+ country1 + '/city1/' + city1 + '/country2/' + country2
				+ '/city2/' + city2 + '/co/' + company,
		evalScripts :false,
		onSuccess : function(responseTree, responseElements, responseHTML,
				responseJavaScript) {
			locSelArrived(responseTree);
			var js = responseJavaScript;
			eval(js);
		}
	}).send();
}

function locSelArrived(content) {
	// Clear the text currently inside the results div.
	$('CO2calc').set('text', '');
	// Inject the new DOM elements into the results div.
	$('CO2calc').adopt(content);
}

function getCO2() {
	var isReturn = document.getElementById('CO2returnid').checked;
	var URL = baseURL + '/modules/GreenCertTransport/travel/calcco2/?co=' + company + '&';
	var el = document.getElementById('CO2ci0');
	if (!el.options.length) {
		alert('Du mangler at vælge en by!');
		return;
	}

	URL += "ciID[]=" + el.options[el.options.selectedIndex].value + '&';
	for ( var i = 2; i < CO2locations.length; ++i) {
		var el = document.getElementById('CO2ci' + CO2locations[i]);
		if (!el.options.length) {
			alert('Du mangler at vælge en by!');
			return;
		}
		URL += "ciID[]=" + el.options[el.options.selectedIndex].value + '&';
	}
	var el = document.getElementById('CO2ci1');
	if (!el.options.length) {
		alert('Du mangler at vælge en by!');
		return;
	}

	URL += "ciID[]=" + el.options[el.options.selectedIndex].value + '&';
	URL += 'isReturn=' + (isReturn + 0);
	var req = new Request.HTML( {
		url :URL,
		onSuccess : function(html) {
			co2Arrived(html);
		}
	}).send();
}

function co2Arrived(content) {
	// Clear the text currently inside the results div.
	$('CO2calc').set('text', '');
	// Inject the new DOM elements into the results div.
	$('CO2calc').adopt(content);
}

function openChild(file, windowname, width, height) {
	childWindow = open(
			file,
			windowname,
			'width='
					+ width
					+ ',height='
					+ height
					+ ', toolbar = no, location = no, directories = no, status = no, mnubar = no, scrollbars = yes, resizable = yes');
	if (childWindow == null) {
		alert('It seems that you have a popup-blocker installed. Please allow popups for this site');
		return null;
	}
	if (childWindow.opener == null) {
		childWindow.opener = self;
	}
	if (childWindow.focus) {
		childWindow.focus();
	}
	return childWindow;
}

function confirmBuy(url) {
	openChild(
			baseURL + '/modules/GreenCertTransport/travel/confirmbuy/?' + url,
			'CO2payment', 550, 700);

	/*
	 * var req = new Request.HTML( { url :baseURL +
	 * '/modules/GreenCertTransport/travel/confirmbuy/?' + url, onSuccess :
	 * function(html) { co2Arrived(html); } }).send();
	 */

}

