/* calendar */
// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
	cal.hide();			// hide the calendar

	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar.  Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	} else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1900, 2070);	// min/max year allowed
		calendar.create();		// create a popup calendar
	}
	calendar.sel = el;		// inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field

	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}

function setupPage() {
  menuHeight = $('container').clientHeight - 1150;
  if(menuHeight > 0) { 
    $('scaling_menu').style.height = menuHeight+"px";
  }

}

var preload_list = new Array("6-reasons-bottom.gif", "guaranteed.gif", "disappointment.gif", "safety.gif", "budget.gif", "hiddencosts.gif",
	"itinerary.gif");
var path_to_images = "/images/";

if (document.images) {
	var image_list = new Array();
	for (var preload_counter=0; preload_counter<preload_list.length; preload_counter++) {
  	image_list[preload_counter] = new Image(); 
		image_list[preload_counter].src = path_to_images + preload_list[preload_counter];	
  }
}

var swapTimer;

var restore_delay = 5200; 
function swapImage(num) {
	if (swapTimer) clearTimeout(swapTimer);
	if (document.images) {
		document.swap.src = image_list[num].src;
	}
}

function restoreImg() {
	swapTimer = setTimeout("swapImage("+0+")",restore_delay);
}

function MM_callJS(jsStr) { 
  return eval(jsStr)
}

function selectStoredAddress(obj, address_type)
{
	address_id = obj.options[obj.selectedIndex].value;

	if(address_id != "")
	{
		input_elements = document.getElementsByTagName("input");
		for(i=0;i<input_elements.length;i++)
		{
			if(input_elements[i].type == "hidden")
			{
				input_name = input_elements[i].name;
				matches = input_name.match("^address_([0-9]+)_([A-Za-z0-9_]+)$");
				
				if(matches)
				{
					if(matches.length > 0)
					{
						if(address_id == matches[1])
						{
							if(matches[2] == "region_id")
							{
								// select box
								opts = document.getElementById("addresses["+address_type+"]["+matches[2]+"]").options;
								for(i2=0;i2<opts.length;i2++)
								{
									if(opts[i2].value = input_elements[i].value)
									{
										opts.selectedIndex = i2;
									}
								}
							}
							else
							{
								// standard text box
								document.getElementById("addresses["+address_type+"]["+matches[2]+"]").value = input_elements[i].value;
							}
						}
					}
				}
			}
		}
	}
}

function setBillingAsDelivery(obj)
{
	if(obj.checked)
	{
		input_elements = document.getElementsByTagName("input");
		for(i=0;i<input_elements.length;i++)
		{
			if(input_elements[i].type == "text")
			{
				
				input_name = input_elements[i].name;
				
				matches = input_name.match(/([A-Za-z0-9_]+)/g);
				if(matches)
				{
					if(matches[1] == "delivery")
					{
						if(document.getElementById("addresses[billing]["+matches[2]+"]"))
						{
							document.getElementById("addresses[billing]["+matches[2]+"]").value = input_elements[i].value;
						}
					}
				}
			}
		}
		
		// select box
		delivery_region_opts = document.getElementById("addresses[delivery][country_id]").options;
		delivery_id = delivery_region_opts[delivery_region_opts.selectedIndex].value;
		
		setAddressCountry("billing", delivery_id);
	}
	else
	{
		input_elements = document.getElementsByTagName("input");
		for(i=0;i<input_elements.length;i++)
		{
			if(input_elements[i].type == "text")
			{
				input_name = input_elements[i].name;
				if(input_name.match("^addresses\[billing\]\[[A-Za-z0-9_]+\]$"))
				{
					input_elements[i].value = "";
				}
			}
		}
		billing_region_opts = document.getElementById("addresses[billing][country_id]").options;
		billing_region_opts.selectedIndex = 0;
	}
}

function setAddressCountry(type, region_id)
{
	region_opts = document.getElementById("addresses["+type+"][country_id]").options;
	
	for(i=0;i<region_opts.length;i++)
	{
		if(region_opts[i].value == region_id)
		{
			region_opts.selectedIndex = i;
		}
	}
}


function dropdownlist_item_select(obj) {
  if (obj.options[obj.selectedIndex].style.color == 'silver') {
    for (var i = obj.length - 1; i >= 0; i--) { 
      if (obj.options[i].defaultSelected == true || i == 0) { obj.options[i].selected=true; break; }
    }
    alert('This option is greyed out and is not available under the current search criterea');

  }
    return false;
}

function submitFormTo(form, action) {
  document.forms[form].action = action;
  document.forms[form].submit();
}


