﻿// JavaScript usado en AutoCompleteTextControl

var HOTEL_SEARCH_TYPE = "Hotel";
var FLIGHT_SEARCH_TYPE = "Flight";
var ACTIVITIES_SEARCH_TYPE = "Activities";
var FLIGHT_AND_HOTEL_SEARCH_TYPE = "FlightAndHotel";

var _warningLabelName;
var _searchButton;

var incorrectDestination;
var incorrectAirport;

function showWaitImage(controlName, imageControlName, e, timeout)
{
    var objControl = document.getElementById(controlName);
	var objImage = document.getElementById(imageControlName);
	
	if(haveHideImage(e))
	{
	    objImage.style.display = 'none';
	}
	else
	{
        var inputText = objControl.value;
    	
        if(inputText.length >= 3)
	    {
		    objImage.style.display = '';   
		    setTimeout("hideImage('" + imageControlName +  "')", timeout);
	    }
	    else
	    {
		    objImage.style.display = 'none';
	    }
	}
}

function hideImage(imageName)
{
    var objImage = document.getElementById(imageName);    
    objImage.style.display = 'none';
}

function lostFocus(inputControlName, warningControlName, imageControlName, searchType)
{
    var objImage = document.getElementById(imageControlName);
    
    objImage.style.display = 'none';
    
    isValidDestination(inputControlName, warningControlName, searchType);
}

function isValidDestination(inputControlName, warningControlName, searchType)
{
    var objInput = document.getElementById(inputControlName);
    var ws = "GetCitiesWS";
     
    _warningLabelName = warningControlName;

    
    switch(searchType)
    {
        case HOTEL_SEARCH_TYPE:
            var getCitiesWS = new Karabel.Web.WebServices.GetCitiesWS();
            getCitiesWS.IsValidDestination(objInput.value, IsValidDestination_Handler);
            break;

        case ACTIVITIES_SEARCH_TYPE:
            var getCitiesWS = new Karabel.Web.WebServices.GetCitiesWS();
            getCitiesWS.IsValidDestination(objInput.value, IsValidDestination_Handler);
            break;

        case FLIGHT_SEARCH_TYPE:
            var getAirportsWS = new Karabel.Web.WebServices.GetAirportsWS();
            getAirportsWS.IsValidAirport(objInput.value, IsValidAirport_Handler);
            break;

        case FLIGHT_AND_HOTEL_SEARCH_TYPE:
            var getCitiesWS = new Karabel.Web.WebServices.GetCitiesWS();
            getCitiesWS.IsValidDestination(objInput.value, IsValidDestination_Handler);
    }
}

function IsValidDestination_Handler(result)
{
    objWarningLabel = document.getElementById(_warningLabelName);
    
    if(!result)
    {   
        if(objWarningLabel != null) 
        {
            objWarningLabel.style.display = '';
            objWarningLabel.innerHTML = incorrectDestination; 
            
            var objSearchButton = document.getElementById(_searchButton);
            if(objSearchButton != null) objSearchButton.disabled = true;
        }
    }
}

function IsValidAirport_Handler(result)
{
    objWarningLabel = document.getElementById(_warningLabelName);
    
    if(!result)
    {   
        if(objWarningLabel != null) 
        {
            objWarningLabel.style.display = '';
            objWarningLabel.innerHTML = incorrectAirport;
            
            var objSearchButton = document.getElementById(_searchButton);
            if(objSearchButton != null) objSearchButton.disabled = true;
        }
    }
}

function setFocus(spanControlName)
{
    var objSpan = document.getElementById(spanControlName);
    
    if(objSpan != null) objSpan.style.display = 'none';
    
    var objSearchButton = document.getElementById(_searchButton);
    if(objSearchButton != null) objSearchButton.disabled = false;
}

function haveHideImage(e)
{
	var keyNum;
	
	if(window.event)
	{
		keyNum = e.keyCode;
	}
	else if(e.which)
	{
		keyNum = e.which;
	}
	
	if(keyNum == 13 || keyNum == 38 || keyNum == 40)
	{
		return true;
	}
	
	return false;
}
