/**
For forms
*/
function showP1()
{
	document.getElementById("p2").style.display = "none";
	document.getElementById("p1").style.display = "";
	
}

function showP2()
{
	document.getElementById("p1").style.display = "none";
	document.getElementById("p2").style.display = "";
	
}

var win = null;
function mail(mypage,myname,w,h){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,resizable=yes'
	win = window.open(mypage,myname,settings);
}

function common__swapOffers() {
	/*
	new Ajax.Request('/home/ajax_ticker', {
		onSuccess:common__swapOffersCB,
		method:"get"
	});*/
	var request = new Request({url:'/home/ajax_ticker.htm', method:"get", onSuccess:common__swapOffersCB});
	request.send();
}

function common__swapOffersCB(text, xml) {
	//var xml = transport.responseXML;
	var items = xml.getElementsByTagName("item");
	new slowly(2000, 60).fade("offersContainer1", function() {common__swapOffer("offersContainer1", items[0], null); });
	new slowly(2000, 60).fade("offersContainer2", function() {
		common__swapOffer("offersContainer2", items[1], function() { 
			setTimeout("common__swapOffers();", 6000);													 
		}); 
	});
}

function common__swapOffer(elemID, xml, func) {
	var elem = document.getElementById(elemID);
	var nameDiv = elem.getElementsByTagName("div")[0];
	var nameA = nameDiv.getElementsByTagName("a")[0];
	var descDiv = elem.getElementsByTagName("div")[1];
	nameA.innerHTML = xml.getElementsByTagName("name")[0].firstChild.data;
	descDiv.innerHTML = xml.getElementsByTagName("desc")[0].firstChild.data;
	
	if(xml.getElementsByTagName("type")[0].firstChild.data == "offer") {
		nameA.href = "/home/offer/"+xml.getElementsByTagName("id")[0].firstChild.data+".htm"	
	}
	else {
		nameA.href = "/events/view/"+xml.getElementsByTagName("id")[0].firstChild.data+".htm"
	}
	new slowly(2000, 60).fadeIn(elemID, func);
}

var common__bookingFormRatesPopulated = false;

function common__populateBookingFormRates() {
	//Don't run more than once!
	if(common__bookingFormRatesPopulated) return;
	common__bookingFormRatesPopulated = true;
	/*
	new Ajax.Request('/home/ajax_booking_form_rates.htm', {
		method:"get",
		onSuccess:common__populateBookingFormRatesCB
	});*/
	var request = new Request({url:'/home/ajax_booking_form_rates.htm', method:"get", onSuccess:common__populateBookingFormRatesCB});
	request.send();
}

function common__populateBookingFormRatesCB(text, xml) {
	//var xml = transport.responseXML;
	var statusElem = xml.getElementsByTagName("status")[0];
	if(statusElem.firstChild == null) {
		alert("No or empty status tag found");	
		return;
	}
	
	if(statusElem.firstChild.data != "true") {
		alert(statusElem.firstChild.data);	
		return;
	}
	
	var rates = xml.getElementsByTagName("rate");
	var rateSelect = document.bookingForm.txtRateSelected;
	//Remove existing.
	var options = rateSelect.getElementsByTagName("option");
	while(options.length > 0) {
		options[0].parentNode.removeChild(options[0]);	
		options = rateSelect.getElementsByTagName("option");
	}
	
	//Put in new.
	for(var i = 0; i < rates.length; i++) {
		//<option value="0">TEST Rate</option>
		var newOption = document.createElement("option");
		newOption.appendChild(document.createTextNode(rates[i].getAttribute("name")));
		newOption.value = rates[i].getAttribute("value");
		rateSelect.appendChild(newOption);		
	}
}

function common__bookingFormProcess() {
	var nightsInput = document.getElementById("nights");
	var endDate = new Date();
	endDate.setFullYear(document.bookingForm.CheckInYear.value, new Number(document.bookingForm.CheckInMonth.value) - 1, document.bookingForm.CheckInDay.value);
	endDate.setDate(endDate.getDate() + new Number(nightsInput.value));
	
	var outDay = new String(endDate.getUTCDate());
	if(outDay.length < 2) outDay = "0"+outDay;
	var outMonth = new String((endDate.getUTCMonth()+1));
	if(outMonth.length < 2) outMonth = "0"+outMonth;
	
	document.bookingForm.CheckOutDay.value 		= outDay;
	document.bookingForm.CheckOutMonth.value 	= outMonth;
	document.bookingForm.CheckOutYear.value 	= endDate.getFullYear();
	
	return true;
}
