
var idOrigin = null;
var idDestination = null;
var idSearchType = null;
var idAdults = null;
var idAirHotel = null;
var idHotelOnly = null;
var idPromotionCode = null;
var txtDepart = null;
var ddlDepartTime = null;
var txtReturn  = null;
var ddlReturnTime = null;

function InitDateDropdowns()
{
	if(document.getElementById)
	{
		if(this.txtDepart == null)
		{	this.txtDepart = document.getElementById(this.idDepart);	}
		if(this.txtReturn == null)
		{	this.txtReturn = document.getElementById(this.idReturn);	}
	}
}
	

function GetDepartureDate()
{
    this.InitDateDropdowns();

	var d = new Date(txtDepart.value);
	var year = d.getFullYear();
	if (year < 2000)
	{
		d.setFullYear(year+100);
	}
	return d;
}

function GetReturnDate()
{
    this.InitDateDropdowns();

	var d = new Date(txtReturn.value);
	var year = d.getFullYear();
	if (year < 2000)
	{
		d.setFullYear(year+100);
	}
	return d;
}

function ValidateDate()
{
    this.InitDateDropdowns();
    
	var dt = new Date(txtDepart.value);
	var dtRet = new Date(txtReturn.value);
	if (isNaN(dt))
	{
		alert('Please Enter a valid Date. Valid date format is mm/dd/yyyy');
		txtDepart.focus();
		return false;  
	}
	else if (isNaN(dtRet))
	{
	    alert('Please Enter a valid Date. Valid date format is mm/dd/yyyy');
		txtReturn.focus();
		return false;  
	}
	return true; 
}

function UpdateReturnDate()
{
	if(document.getElementById) 
	{
	    this.InitDateDropdowns();
	
	    try 
	    {
		    var DepDate = new Date(txtDepart.value);
		    if(!isNaN(DepDate))
		    {
		        if(DepDate.getFullYear() < 2000) 
		        {
			        DepDate = new Date(DepDate.getFullYear() + 100, DepDate.getMonth(), DepDate.getDate());
			        txtDepart.value = (DepDate.getMonth() + 1) + '/' + DepDate.getDate() + '/' + DepDate.getFullYear(); 
		        }
		        var RetDate = new Date(DepDate.getFullYear(), DepDate.getMonth(), DepDate.getDate() + 2);
		        txtReturn.value = (RetDate.getMonth() + 1) + '/' + RetDate.getDate() + '/' + RetDate.getFullYear(); 
		    }
	    }
	    catch(e)
	    {  
	        txtReturn.value = '';
	    }
    }
}


function Search(autoSubmit)
{
	if(document.getElementById && top.location)	
	{
		this.InitDateDropdowns();
		var DepDate = this.GetDepartureDate();	
		var RetDate = this.GetReturnDate();
		
		if(autoSubmit == 1)
		{		
		    if(DepDate >= RetDate)	
		    {
			    alert('Departure date must be before return date!');
			    return;	
		    }
		    if (ValidateDate() == false)
		    {
			    return;	
		    }
		}
		var ddlDepartTime = document.getElementById(idDepartTime);
		var ddlReturnTime = document.getElementById(idReturnTime);
		//generate random number between 1 and 9998
		var randomnumber=Math.floor(Math.random()*9999);
		
		var txtPromotionCode = document.getElementById(idPromotionCode);
		var txtOrigin = document.getElementById(idOrigin);
	    if(txtOrigin.value == 'city or airport')
		{    txtOrigin.value = '';   }
		    
		var txtDestination = document.getElementById(idDestination);		
		if(txtDestination.value == 'city or airport')
		{    txtDestination.value = '';   }
		    
		var ddlSearchType = document.getElementById(idSearchType);
		var ddlAdults = document.getElementById(idAdults);
		var type = "A";		
		if(document.getElementById(idHotelOnly).checked)
		{
		    type = "H";
		}
		
		var dDate = '';
		if(!isNaN(DepDate))
		{
		    dDate = DepDate.getFullYear() + '-' + (DepDate.getMonth() + 1) + '-' + DepDate.getDate()
		}
		
		var rDate = '';
		if(!isNaN(RetDate))
		{
		    rDate = RetDate.getFullYear() + '-' + (RetDate.getMonth() + 1) + '-' + RetDate.getDate()
		}
		
		
		var url = "/bookingengine/etd.aspx" +
		"?wPromoCode=" + txtPromotionCode.value + 
		"&Destination=" + txtDestination.value + 
		"&wDepCity=" + txtOrigin.value + 
		"&wAirHotel=" + type + 
		"&wAdults=" + ddlAdults.options[ddlAdults.selectedIndex].value + 
		"&wBookingType=" + ddlSearchType.options[ddlSearchType.selectedIndex].value + 
		"&DepartDate=" + dDate + 
		"&wDepartTime="  + ddlDepartTime.value +		
		"&ReturnDate=" + rDate + 
        "&wReturnTime=" + ddlReturnTime.value + 
		"&AutoSubmit=" + autoSubmit +
		"&wTrackingCode=" + 
		"&wMiniBookingSearchId=" + randomnumber.toString();	
		top.location = url;	 
	}
}

function setWatermark(txtBox)
{
    if(txtBox.value.length == 0
        || txtBox.value == 'city or airport')
    {
        txtBox.setAttribute('class', 'fieldwidths watermark');
        txtBox.value = 'city or airport';
        txtBox.onkeydown = function() { removeWatermark(txtBox) };
        txtBox.onclick = function() { removeWatermark(txtBox) };
    }
}

function removeWatermark(txtBox)
{
    txtBox.setAttribute('class', 'fieldwidths');
    txtBox.value = '';
    // txtBox.onkeydown = null;
    // txtBox.onclick = null;
	txtBox.onkeydown = null;
	txtBox.onclick = function() { highLightText(txtBox) };
}

function highLightText(txtBox)
	{
		if(txtBox != null
			&& txtBox.value != '')
		{
			txtBox.focus();
			txtBox.select();
		}
	}

function changeSearchView(searchType)
{
	var txtOrigin = document.getElementById(idOrigin);
	if(txtOrigin != null)
	{
		if(searchType.toString() == 'HotelOnly')
		{
			txtOrigin.disabled = true;			
			txtOrigin.style.backgroundImage  = 'url(../img/hp/inputbox_shadow_gray.png)';
			removeWatermark(txtOrigin);
		}
		else if(searchType.toString() == "HotelAir")
		{
			txtOrigin.disabled = false;
			txtOrigin.style.backgroundImage  = 'url(../img/hp/inputbox_shadow.gif)';
			setWatermark(txtOrigin);
		}
		else
		{
			alert('Invalid search type found');
		}
	}
	else	
	{
		alert('Could not find origin text box');
	}
}


if (typeof(Sys) !== 'undefined') 
{
    Sys.Application.notifyScriptLoaded(); 
}





