//-------- Conversion from hexadecimal --------------
convertBase = "0123456789abcdefghijklmnopqrstuvwxyz";
// This function converts the input number from "origin" system to binary http://www.javascriptkit.com/script/script2/numconvert.shtml
// e.g. convert(wordsArr[i], 16, 2);
function convert(input,origin,dest) 
{
    input=input.toString().toLowerCase(); origin=origin.toString(); dest=dest.toString()
    var b=0; var Result="";
    if (Number(origin)>convertBase.length || Number(dest)>convertBase.length || Number(origin)<2 || Number(dest)<2) 
    {
        return "Invalid numbering system";
    }
    //Build number - decimal representation of the hexadecimal input number
    for (var c=1;c<=input.length;c++) 
    { 
        b+=convertBase.indexOf(input.substring(c-1,c))*(Math.pow(origin,input.length-c)); 
        if(convertBase.indexOf(input.substring(c-1,c)) > Number(origin)) 
        {
            return 'Character "'+input.substring(c-1,c)+'" not present in origin system'; 
        }
    }
    var a = Math.floor(Math.log(b) / Math.log(dest));
    //Create binary representation of the decimal number
    while (a>-1) 
    {
        var e=Math.pow(dest,a)
        a--;
        var d=(b-b%e)/e+1;
        b%=e;
        Ciffer=convertBase.substring(d-1,d);
        Result+=Ciffer;
    }
    return Result
}
//This function checks is bit set in binary representation.
function checkBit(value, mask)
{
    return (value.substring(value.length-mask, value.length-mask-1) == "1");
}
//-------- Creating of the airports list --------------
//This function checks which origin is set and adds correct destinations to destination drop down 
function checkData(originControl, destinationControl)
{
    destinationControl.options.length = 0;
    var selectedOrigin = originControl.selectedIndex;
    if (selAirportTxt.length > 0) // if "selected airport" exists on the list gets lower index 
    {
        selectedOrigin--;
    }
    
    if(marketAirports > 0) {
        // if you select line which divide destinations from this market and rest of them
        // next airport will be selected e.g. market PT first city name on A letter not from Portugal.
        if(selectedOrigin == marketAirports) 
        {
            originControl[originControl.selectedIndex+1].selected = true; 
            checkData(originControl, destinationControl); 
            return;
        }
        // if you select something below line - gets lower index (minus line index)
        if(selectedOrigin >= marketAirports)
        { 
            selectedOrigin--;
        }
    }
    if (selAirportTxt.length > 0)
    {
        destinationControl[destinationControl.length] = new Option(selAirportTxt, "");
    }
    //insert destinations ...
    if (selectedOrigin != -1)
    {
        var wordsArr = destinationsMatrix[originsIndex[selectedOrigin]].split("|");
        for(var i = 0; i < wordsArr.length ; i++)
        {
            var currWord = convert(wordsArr[i], 16, 2);
            var mask = 0;
            for(var j = 0; j < currWord.length; j++)
            {
                if(checkBit(currWord, mask))
                {
                    createOption(destinationControl, wordLength*i+j);
                }
                mask++;
            }
        }
    }
}
// This function creates the option for drop down list control passed as parameter
function createOption(destinationControl, index)
{
    if(destinationsNames.length > index)
    {
        var option = destinationsNames[index].split("|");
        destinationControl[destinationControl.length] = new Option(option[1], option[0]);
    }
}
// This function creates the options in the origin drop down list.
function createOrigins(originControl)
{
    if (selAirportTxt.length > 0)
    {
        originControl[originControl.length] = new Option(selAirportTxt, "");
    }
    for(var i = 0; i < destinationsNames.length; i++)
    {
        if(marketAirports > 0 && i == marketAirports) 
        {
            originControl[originControl.length] = new Option("---------------------------------------------"," ");
        }
        createOption(originControl, originsIndex[i]);
    }
}
function writeToHidden(dropDownControl, hiddenControl)
{
    hiddenControl.value = dropDownControl[dropDownControl.selectedIndex].value;
}
function readFromHidden(dropDownControl, hiddenControl)
{
    var isSelected = false;
    
    if (hiddenControl.value != null && hiddenControl.value.length == 3)
    {
        var hvalue = hiddenControl.value;
        for(var i=0; i<dropDownControl.length; i++)
        {
            if(dropDownControl[i].value == hvalue) 
            {
                dropDownControl[i].selected = true;
                isSelected = true;
                return;
            }
        }
    }
    if(isSelected == false)
    {
        writeToHidden(dropDownControl, hiddenControl)
    }
}
function OriginDestChange(departure, destination)
{
    var depControl = document.getElementById(departure);
    var destControl = document.getElementById(destination);
    var isResident = IsResidentAvailable(depControl, destControl);
    
    PassPickerSetup(isResident, null);
}
function IsResidentAvailable(depControl, destControl)
{
    var residentdepStr = "CVU,FLW,FNC,GRW,HOR,PDL,PIX,SJZ,SMA,TER"; 
    var residentarrStr = "FAO,FNC,LIS,OPO,CVU,FLW,GRW,HOR,PDL,PIX,SJZ,SMA,TER"; 
    var residentsArr = ['1111000000000','1111000000000','0000111111111','1111000000000','1111000000000','1111000000000','1111000000000','1111000000000','1111000000000','1111000000000'];
    var isResident = false;
    if(residentdepStr.indexOf(depControl.value) > -1 && residentarrStr.indexOf(destControl.value) > -1 && depControl.value != destControl.value)
    {
        var depArr = residentdepStr.split(",");
        for(var i=0; i<depArr.length; i++)
        {
            if(depArr[i] == depControl.value)
            {
                var arrArr = residentarrStr.split(",");
                for(var j=0; j<arrArr.length; j++)
                {
                    if(arrArr[j] == destControl.value )
                    {
                        if(residentsArr[i].substring(j,j+1) == "1")
                        {
                            isResident = true;
                        }
                        break;
                    }
                }
                break;
            }
        }
    }
    
    return isResident;
}
function returnDateVisibility(visible)
{
    document.getElementById('destinationDate').style.display = (visible == true ) ? "block" : "none";
}
function setTimesDDL(depTimeClientId, retTimeClientId, isEnable)
{
    setTimeDDL(depTimeClientId, isEnable);
    setTimeDDL(retTimeClientId, isEnable);
}
function setTimeDDL(timeClientId, enable)
{
     var ddl = document.getElementById(timeClientId);
     if(ddl != undefined)
     {
        ddl.options[ddl.options.length - 1].selected = true;
        ddl.disabled = !enable;
     }
}
function ShowPopup(destination)
{
    var destControl = document.getElementById(destination);
        
    if (typeof(specialAirports[destControl.value]) != "undefined")
    {		
		var strQueryString = window.location.href.split("?");
		
		if (strQueryString[1] != "undefined")
		{
			window.open("SpecialAirportsPopup.aspx?" + strQueryString[1],"Warning","status=1, width=300, height=380");
		}else
		{
			window.open("SpecialAirportsPopup.aspx","Warning","status=1, width=300, height=380");
		}
		
    }
}

function FlightType_OneWayAirStartSelected(e) {
    if (e.target.checked) {
        PassPickerSetup(null, false);
        document.getElementById('destinationDate').style.display = "none";
    }
}

function FlightType_ReturnTripAirStartSelected(e) {
    
    if (e.target.checked) {
        PassPickerSetup(null, true);
        document.getElementById('destinationDate').style.display = "block";
    }
}