var DayNDU = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var onDay  = 86400000;

function countryChange()
{
	var options = ChangeLocationsAccordingToCountry(countryLocationResort.XMLDocument, bookToVinetFrm.slcCountry.value);

	// remove all existing options, except the first one (select Location);
	for (var optionsCounter = 1; optionsCounter < bookToVinetFrm.slcLocation.options.length; )
	{
		bookToVinetFrm.slcLocation.options.remove(optionsCounter);
	}
	// remove all existing options, except the first one (select Resort);
	for (var optionsCounter = 1; optionsCounter < bookToVinetFrm.slcResorts.options.length; )
	{
		bookToVinetFrm.slcResorts.options.remove(optionsCounter);
	}
	// insert new location options 
	for (var optionsCounter = 0; optionsCounter < options.length; ++optionsCounter)
	{
		var option = document.createElement("option");
		option.value = options[optionsCounter].locationCode;
		option.text = options[optionsCounter].locationName;
		option.cityCode = options[optionsCounter].cityCode;
		bookToVinetFrm.slcLocation.add(option);
	}
	//alert(slcLocation.outerHTML);
		
}	// countryChange
	
function locationChange()
{
	//alert("locationChange " + slcResorts[slcResorts.selectedIndex].value);
	var options = ChangeResortsAccordingToLocation(countryLocationResort.XMLDocument, bookToVinetFrm.slcLocation.value);

	// remove all existing options, except the first one (select Resort);
	for (var optionsCounter = 1; optionsCounter < bookToVinetFrm.slcResorts.options.length; ++optionsCounter)
	{
		bookToVinetFrm.slcResorts.options.remove(optionsCounter);
	}
	// insert new options
	for (var optionsCounter = 0; optionsCounter < options.length; ++optionsCounter)
	{
		var option = document.createElement("option");
		option.value = options[optionsCounter].resortCode;
		option.text = options[optionsCounter].resortName;
		bookToVinetFrm.slcResorts.add(option);
	}
		
}	// locationChange
	
function setAdditionalSlc()
{
		
	var slcLocElm = document.all["slcLocation"];
	var slcCountryElm = document.all["slcCountry"];
			
	for (var locL=0; locL<slcLocElm.options.length; locL++) {
		if (String(slcLocElm.options[locL].innerText).toUpperCase() == String(slcCountryElm[slcCountryElm.selectedIndex].innerText).toUpperCase()) {
			slcLocElm.options[locL].selected = true;
		}
	}
			
	locationChange();		
}

function ChangeLocationsAccordingToCountry(xml, countryCode)
{
	var options = new Array();
	var locations = xml.selectNodes("CountryLocationResortsInfo/Country[@code='" + countryCode + "']/Location");		
	if (locations)
	{
		var numOfLocations = locations.length;
		
		for (var locationCounter = 0; locationCounter < numOfLocations; ++locationCounter)
		{
			var option = new Object();
			var location = locations[locationCounter];
			var locationCode = location.getAttribute("code");
			var locationName = location.getAttribute("name");
			var cityCode	 = location.getAttribute("cityCode");
			option.locationCode = unescape(locationCode);
			option.locationName = unescape(locationName);
			option.cityCode		= unescape(cityCode);

			options.push(option)
			
		}
	}	
	return options;
		
}	
	
function ChangeResortsAccordingToLocation(xml, locationCode)
{
	var resortOptions = new Array();
	var resorts = xml.selectNodes("CountryLocationResortsInfo/Country/Location[@code='" + locationCode + "']/Resort");		
	var numOfResorts = resorts.length;

	for (var resortsCounter = 0; resortsCounter < numOfResorts; ++resortsCounter)
	{
		var option = new Object();
		var resort = resorts[resortsCounter];
		var resortCode = resort.getAttribute("code");
		var resortName = resort.getAttribute("name");
		option.resortCode = unescape(resortCode);
		option.resortName = unescape(resortName);
		
		resortOptions.push(option)
				
	}
	return resortOptions;
		
}	

function submitForm()
{
	bookToVinetFrm.dateFrom.value = bookToVinetFrm.txtCheckin.value;
	bookToVinetFrm.dateTo.value   = bookToVinetFrm.txtCheckout.value;		
	bookToVinetFrm.resort.value   = bookToVinetFrm.slcResorts.value;
	bookToVinetFrm.Location.value = ((bookToVinetFrm.slcResorts.value == "0")?bookToVinetFrm.slcLocation.value:bookToVinetFrm.slcResorts.value);
	bookToVinetFrm.cityCode.value = bookToVinetFrm.slcLocation.options[bookToVinetFrm.slcLocation.selectedIndex].cityCode;
	bookToVinetFrm.unitPax.value  = resBoxUnitPax();
			
	bookToVinetFrm.selected_country.value = bookToVinetFrm.slcCountry.value;
	bookToVinetFrm.selected_location.value = bookToVinetFrm.slcLocation.value;
	bookToVinetFrm.selected_resort.value = bookToVinetFrm.slcResorts.value;
	bookToVinetFrm.origDepDate.value = bookToVinetFrm.txtCheckin.value;		
					
	bookToVinetFrm.submit();
}
	
function resBoxUnitPax()
{
	var untPax = "ad" + bookToVinetFrm.adultPaxNumberSlc.value + "ch";
	var numOfChildren = parseInt(bookToVinetFrm.childPaxNumberSlc.value,10);
	if (numOfChildren == 0)
		untPax += "0";
	else {
		for(var chCounter = 0; chCounter < numOfChildren; ++chCounter)
			untPax += document.all["child_age_" + chCounter].value + "^";
		untPax = String(untPax).substr(0,String(untPax).length-1);
	}
	untPax += "in0";
		
	return untPax;
}
	
function window_onload()
{
	var from = new Date();
		
	from.setDate(from.getDate()+7);	
	from.toFormat = "m-d-yyyy";
	from.DatePart = 1;	
		
	document.all["txtCheckin"].value  = from.dateObject2StringNDU();
	document.all["numberNightSlc"].value = "7";
	setCheckout("txtCheckin","txtCheckout");
	
	countryChange();
	setAdditionalSlc();
	
}	
	
function chAgeOnChange()
{
	var chNum = parseInt(document.all["childPaxNumberSlc"].value,10);
		
	var chAge0 = document.all["child_age_0"];
	var chAge1 = document.all["child_age_1"];
	var chAge2 = document.all["child_age_2"];
		
	chAge0.style.display = "none";
	chAge1.style.display = "none";
	chAge2.style.display = "none";
		
	if (chNum > 0) {
		document.all["tblChAges"].style.display = "block";
		document.all["tdChAges"].style.display  = "block";			
	}
	else {
		document.all["tblChAges"].style.display = "none";
		document.all["tdChAges"].style.display  = "none";
	}
		
	if (chNum > 2) {	
		chAge0.style.display = "block";
		chAge1.style.display = "block";
		chAge2.style.display = "block";
	}
	if (chNum > 1) {	
		chAge0.style.display = "block";
		chAge1.style.display = "block";
	}
	if (chNum > 0)	
		chAge0.style.display = "block";
		
}
	
function quickPackCalendar(elmName)
{	
	var posLeft = getRealLeft(document.all[elmName])+93;
	var posTop  = getRealTop(document.all[elmName]) + document.body.scrollTop;

	var newDate = new Date();
	if(window.document.all('CalanderIframe')==null)
	{
		window.document.body.insertAdjacentHTML("afterBegin","<iframe id='CalanderIframe' name='CalanderIframe' style='position:absolute;left:"+posLeft+";top:"+posTop+"; z-index:10' frameborder=0 class='popupCalander' width=220px height=180px></iframe>");	
	}
		
	var CalanderIframe = window.document.all["CalanderIframe"];
		
	if (document.all[elmName].value != "")
		var currDate = new Date(document.all[elmName].value);
	else
		var currDate = new Date();
	
	currDate.toFormat = "m-d-yyyy";
	currDate.DatePart = 1;	
		
	CalanderIframe.style.display = "block";
	CalanderIframe.src="http://10.100.200.164/calendar.asp?NameId="+ elmName +"&currDate="+currDate.dateObject2StringNDU();
	
	
			
	CalanderIframe.style.left = posLeft;
	CalanderIframe.style.top = posTop;
		
	return;
}

function getRealLeft(elm) {
	       
    xPos = elm.offsetLeft;
    tempEl = elm.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getRealTop(elm) {
	       
    xPos = elm.offsetTop;
    tempEl = elm.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function setCheckout(from,to)
{	
	//GETTING FROM DATE IN MILLSECOND
	var checkin = document.all[from].value;
		
	if (document.all["txtCheckin"].value == "" && document.all["txtCheckout"].value == "")
		return;
		
	var to_date = new Date(checkin);
		
	var to_date_millsec = to_date.getTime();
	var millsecRange = 0;
	millsecRange = ((((parseInt(document.all["numberNightSlc"].value,10))*24)*60)*60)*1000;
	
	if (from == "txtCheckin")
		to_date_millsec += millsecRange;
	else
		to_date_millsec -= millsecRange;
		
	var new_date_to = new Date(to_date_millsec);	
	new_date_to.toFormat = "m-d-yyyy";
	new_date_to.DatePart = 1;	
	document.all[to].value = new_date_to.dateObject2StringNDU();
	return;
}

var down = false;

function capture()
{
	if (!down) 
	{
		elm = event.srcElement;		
		capturedString = String(elm.value);	
	}
	down = true;
}

function paste()
{
	elm = event.srcElement;		
	elm.value = capturedString;
	down = false;
}