﻿Common = {};
Common.collapse = function(jheader, jpanel, animationSpeed) {
    jheader.removeClass('closer');
    if (animationSpeed) {
        jpanel.hide(animationSpeed);
    } else {
        jpanel.hide();
    }
};

Common.parseCurrency = function Number$parseCurrency(str) 
    {
       /// <summary> 
       /// Parses a string containing a culture-sensitive currency value into a number.  
       /// </summary>  
       /// <param name="str" type="String">  
       /// The numeric string with optional currency symbol to convert into a number.  
       /// </param>  
       /// <returns type="Number" /> 
       var currencySymbol = Sys.CultureInfo.CurrentCulture.numberFormat.CurrencySymbol;
       var numberOnly = str.replace(/s+/g,"").replace(eval("/" + currencySymbol + "/g"),"");  
       var parsed = Number.parseLocale(numberOnly);  
       if (isNaN(parsed)) 
       {
           // Try all 5 possible negative number formats.
           var customCi = Sys.CultureInfo.InvariantCulture;
           for (var i = 0; i < 5; i++)
           {      customCi.numberFormat.NumberNegativePattern = i;      parsed = Number._parse(numberOnly, customCi);      if (!isNaN(parsed)) {        break;      }    }  
       }  
       return parsed;}

Common.expand = function(jheader, jpanel, animationSpeed) {
    if (!jheader.hasClass('closer')) {
        jheader.addClass('closer');
    }
    if (animationSpeed) {
        jpanel.show(animationSpeed);
    } else {
        jpanel.show();
    }
};

Common.toggle = function(jheader, jpanel, animationSpeed) {
    jheader.click(function() {
        if (jpanel.is(':visible')) {
            Common.collapse(jheader, jpanel, animationSpeed);
        }
        else {
            Common.expand(jheader, jpanel, animationSpeed);
        }
        return false;
    });
};

Common.getChildObj = function(oCont, tag) {
    for (var i = 0; i < oCont.childNodes.length; i++) {
        if (oCont.childNodes[i].tagName == tag)
            return oCont.childNodes[i];
    }
    return false;
};

AutoCompleteExt = {};
AutoCompleteExt.onClientShown = function(sender, args) {
    if (jQuery.browser.msie && jQuery.browser.version < 7) {
        var oList = sender.get_completionList();
        var oSpan = oList.parentNode;
        var ifr = Common.getChildObj(oSpan, "IFRAME");
        if (ifr) {
            oSpan.removeChild(ifr);
        }
        ifr = document.createElement("IFRAME");
        oSpan.appendChild(ifr);
        ifr.style.display = "block";
        ifr.style.position = "absolute";
        ifr.style.width = oList.style.width;
        ifr.style.height = oList.offsetHeight;
        ifr.style.left = oList.style.left;
        ifr.style.top = oList.style.top;
    }
};

AutoCompleteExt.onClientHiding = function(sender, args) {
    if (jQuery.browser.msie && jQuery.browser.version < 7) {
        var oList = sender.get_completionList();
        var oSpan = oList.parentNode;
        var ifr = Common.getChildObj(oSpan, "IFRAME");
        if (ifr) {
            oSpan.removeChild(ifr);
        };
    }
};

Polling = {};

Polling.showSearch = function() {
    jQuery('.uipolling').hide('')
    .filter('.uipolling_search').show('');
};

Polling.showResults = function() {
    jQuery('.uipolling').hide('')
    .filter('.uipolling_results').show('');
};

Polling.showNoResults = function() {
    jQuery('.uipolling').hide('')
    .filter('.uipolling_noresults').show('');
};

function switchOffers(chosenOffer) 
{
    document.getElementById('latest-offers-flights').style.display = 'none';
    document.getElementById('latest-offers-hotels').style.display = 'none';
    document.getElementById('latest-offers-holidays').style.display = 'none';
    document.getElementById('latest-offers-car').style.display = 'none';
    document.getElementById(chosenOffer).style.display = 'block';
}

function InitializePopupDialogue() {
    jQuery(".uidialog").unbind().dialog({
        autoOpen: false,
        width: 250,
        bgiframe: true
    });
}

function showPopUp(id) {
		jQuery("#"+id).dialog("open");
}

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function Set_Cookie(name, value, expires) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    if (expires) {
        expires = expires * 1000 * 60 * 60; //per hours
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ";path=/";
}

var submitButton;

function submitButtonClick() {
    removeDisabledClass();
    submitButton = jQuery(this);
}

function disableSubmitButton() {
    if (Page_IsValid && submitButton) {
        submitButton.addClass("btn-disabled");
    }
}

function removeDisabledClass() {
    if (submitButton) {
        submitButton.removeClass("btn-disabled");
        submitButton = null;
    }
}

function initialiseSubmitButtons() {
    removeDisabledClass();
    jQuery("input[type=submit]")
        .unbind('click', submitButtonClick)
        .click(submitButtonClick);
    jQuery("form")
        .unbind('submit', disableSubmitButton)
        .submit(disableSubmitButton);
}

jQuery(initialiseSubmitButtons);

function pageLoad() {
    initialiseSubmitButtons();
    InitializePopupDialogue();
}
