﻿/**
* jquery.numberformatter - Formatting/Parsing Numbers in jQuery
* Written by Michael Abernethy (mike@abernethysoft.com)
*
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Date: 2/6/08
*
* @author Michael Abernethy
* @version 1.1.2
*
* many thanks to advweb.nanasi.jp for his bug fixes
*
* This plugin can be used to format numbers as text and parse text as Numbers
* Because we live in an international world, we cannot assume that everyone
* uses "," to divide thousands, and "." as a decimal point.
*
* The format() function will take the text within any selector by calling
* text() or val() on them, getting the String, and applying the specified format to it.
* It will return the jQuery object
*
* The parse() function will take the text within any selector by calling text()
* or val() on them, turning the String into a Number, and returning these
* values in a Number array.
* It WILL BREAK the jQuery chain, and return an Array of Numbers.
*
* The syntax for the formatting is:
* 0 = Digit
* # = Digit, zero shows as absent
* . = Decimal separator
* - = Negative sign
* , = Grouping Separator
* % = Percent (multiplies the number by 100)
* For example, a format of "#,###.00" and text of 4500.20 will
* display as "4.500,20" with a locale of "de", and "4,500.20" with a locale of "us"
*
*
* As of now, the only acceptable locales are 
* United States -> "us"
* Arab Emirates -> "ae"
* Egypt -> "eg"
* Israel -> "il"
* Japan -> "jp"
* South Korea -> "kr"
* Thailand -> "th"
* China -> "cn"
* Hong Kong -> "hk"
* Taiwan -> "tw"
* Australia -> "au"
* Canada -> "ca"
* Great Britain -> "gb"
* India -> "in"
* Germany -> "de"
* Vietnam -> "vn"
* Spain -> "es"
* Denmark -> "dk"
* Austria -> "at"
* Greece -> "gr"
* Brazil -> "br"
* Czech -> "cz"
* France  -> "fr"
* Finland -> "fi"
* Russia -> "ru"
* Sweden -> "se"
* Switzerland -> "ch"
* 
* TODO
* Separate positive and negative patterns separated by a ":" (e.g. use (#,###) for accounting)
* More options may come in the future (currency)
**/

(function(jQuery) {

    function FormatData(dec, group, neg) {
        this.dec = dec;
        this.group = group;
        this.neg = neg;
    };

    function formatCodes(locale) {

        // default values
        var dec = ".";
        var group = ",";
        var neg = "-";

        if (locale == "us" ||
             locale == "ae" ||
             locale == "eg" ||
             locale == "il" ||
             locale == "jp" ||
             locale == "sk" ||
             locale == "th" ||
             locale == "cn" ||
             locale == "hk" ||
             locale == "tw" ||
             locale == "au" ||
             locale == "ca" ||
             locale == "gb" ||
             locale == "in"
            ) {
            dec = ".";
            group = ",";
        }

        else if (locale == "de" ||
             locale == "vn" ||
             locale == "es" ||
             locale == "dk" ||
             locale == "at" ||
             locale == "gr" ||
             locale == "br" ||
             locale == "nl"
            ) {
            dec = ",";
            group = ".";
        }
        else if (locale == "cz" ||
              locale == "fr" ||
             locale == "fi" ||
             locale == "ru" ||
             locale == "se"
            ) {
            group = " ";
            dec = ",";
        }
        else if (locale == "ch") {
            group = "'";
            dec = ".";
        }

        return new FormatData(dec, group, neg);

    };

    jQuery.formatNumber = function(number, options) {
        var options = jQuery.extend({}, jQuery.fn.parse.defaults, options);
        var formatData = formatCodes(options.locale.toLowerCase());

        var dec = formatData.dec;
        var group = formatData.group;
        var neg = formatData.neg;

        var numString = new String(number);
        numString = numString.replace(".", dec).replace("-", neg);
        return numString;
    }

    
    jQuery.formatNumber1 = function(number, options) {
        /*
        var options = jQuery.extend({}, jQuery.fn.parse.defaults, options);
        var formatData = formatCodes(options.locale.toLowerCase());

        var dec = formatData.dec;
        var group = formatData.group;
        var neg = formatData.neg;

        var numString = new String(number);
        numString = numString.replace(".", dec).replace("-", neg);
        return numString;
        */
        number = jQuery.formatNumber(number);
        return jQuery.formatNumber2(number);

    };
    //
    jQuery.formatNumber2 = function(number, options)
    {
     var options = jQuery.extend({}, jQuery.fn.format.defaults, options);

        var formatData = formatCodes(options.locale.toLowerCase());

        var dec = formatData.dec;
        var group = formatData.group;
        var neg = formatData.neg;

        var validFormat = "0#-,.";


        var text = new String(number);
        var prefix = "";
        var negativeInFront = false;
        for (var i = 0; i < options.format.length; i++) {
            if (validFormat.indexOf(options.format.charAt(i)) == -1)
                prefix = prefix + options.format.charAt(i);
            else if (i == 0 && options.format.charAt(i) == '-') {
                negativeInFront = true;
                continue;
            }
            else
                break;
        }
        var suffix = "";
        for (var i = options.format.length - 1; i >= 0; i--) {
            if (validFormat.indexOf(options.format.charAt(i)) == -1)
                suffix = options.format.charAt(i) + suffix;
            else
                break;
        }

        options.format = options.format.substring(prefix.length);
        options.format = options.format.substring(0, options.format.length - suffix.length);


        // now we need to convert it into a number
        while (text.indexOf(group) > -1)
            text = text.replace(group, '');
        var number = new Number(text.replace(dec, ".").replace(neg, "-"));

        // special case for percentages
        if (suffix == "%")
            number = number * 100;

        var returnString = "";

        var decimalValue = number % 1;
        if (options.format.indexOf(".") > -1) {
            var decimalPortion = dec;
            var decimalFormat = options.format.substring(options.format.lastIndexOf(".") + 1);
            var decimalString = new String(decimalValue.toFixed(decimalFormat.length));
            decimalString = decimalString.substring(decimalString.lastIndexOf(".") + 1);
            for (var i = 0; i < decimalFormat.length; i++) {
                if (decimalFormat.charAt(i) == '#' && decimalString.charAt(i) != '0') {
                    decimalPortion += decimalString.charAt(i);
                    continue;
                }
                else if (decimalFormat.charAt(i) == '#' && decimalString.charAt(i) == '0') {
                    var notParsed = decimalString.substring(i);
                    if (notParsed.match('[1-9]')) {
                        decimalPortion += decimalString.charAt(i);
                        continue;
                    }
                    else {
                        break;
                    }
                }
                else if (decimalFormat.charAt(i) == "0") {
                    decimalPortion += decimalString.charAt(i);
                }
            }
            returnString += decimalPortion
        }
        else
            number = Math.round(number);

        var ones = Math.floor(number);
        if (number < 0)
            ones = Math.ceil(number);

        var onePortion = "";
        if (ones == 0) {
            onePortion = "0";
        }
        else {
            // find how many digits are in the group
            var onesFormat = "";
            if (options.format.indexOf(".") == -1)
                onesFormat = options.format;
            else
                onesFormat = options.format.substring(0, options.format.indexOf("."));
            var oneText = new String(Math.abs(ones));
            var groupLength = 9999;
            if (onesFormat.lastIndexOf(",") != -1)
                groupLength = onesFormat.length - onesFormat.lastIndexOf(",") - 1;
            var groupCount = 0;
            for (var i = oneText.length - 1; i > -1; i--) {
                onePortion = oneText.charAt(i) + onePortion;

                groupCount++;

                if (groupCount == groupLength && i != 0) {
                    onePortion = group + onePortion;
                    groupCount = 0;
                }

            }
        }
        returnString = onePortion + returnString;

        // handle special case where negative is in front of the invalid
        // characters
        if (number < 0 && negativeInFront && prefix.length > 0) {
            prefix = neg + prefix;
        }
        else if (number < 0) {
            returnString = neg + returnString;
        }

        if (!options.decimalSeparatorAlwaysShown) {
            if (returnString.lastIndexOf(dec) == returnString.length - 1) {
                returnString = returnString.substring(0, returnString.length - 1);
            }
        }
        returnString = prefix + returnString + suffix;
        return returnString;   
    };

    jQuery.fn.parse = function(options) {

        var options = jQuery.extend({}, jQuery.fn.parse.defaults, options);

        var formatData = formatCodes(options.locale.toLowerCase());

        var dec = formatData.dec;
        var group = formatData.group;
        var neg = formatData.neg;

        var valid = "1234567890.-";

        var array = [];
        this.each(function() {

            var text = new String(jQuery(this).text());
            if (jQuery(this).is(":input"))
                text = new String(jQuery(this).val());

            // now we need to convert it into a number
            while (text.indexOf(group) > -1)
                text = text.replace(group, '');
            text = text.replace(dec, ".").replace(neg, "-");
            var validText = "";
            var hasPercent = false;
            if (text.charAt(text.length - 1) == "%")
                hasPercent = true;
            for (var i = 0; i < text.length; i++) {
                if (valid.indexOf(text.charAt(i)) > -1)
                    validText = validText + text.charAt(i);
            }
            var number = new Number(validText);
            if (hasPercent) {
                number = number / 100;
                number = number.toFixed(validText.length - 1);
            }
            array.push(number);
        });

        return array;
    };

    jQuery.fn.format = function(options) {

        var options = jQuery.extend({}, jQuery.fn.format.defaults, options);

        var formatData = formatCodes(options.locale.toLowerCase());

        var dec = formatData.dec;
        var group = formatData.group;
        var neg = formatData.neg;

        var validFormat = "0#-,.";

        return this.each(function() {

            var text = new String(jQuery(this).text());
            if (jQuery(this).is(":input"))
                text = new String(jQuery(this).val());

            // strip all the invalid characters at the beginning and the end
            // of the format, and we'll stick them back on at the end
            // make a special case for the negative sign "-" though, so 
            // we can have formats like -$23.32
            var prefix = "";
            var negativeInFront = false;
            for (var i = 0; i < options.format.length; i++) {
                if (validFormat.indexOf(options.format.charAt(i)) == -1)
                    prefix = prefix + options.format.charAt(i);
                else if (i == 0 && options.format.charAt(i) == '-') {
                    negativeInFront = true;
                    continue;
                }
                else
                    break;
            }
            var suffix = "";
            for (var i = options.format.length - 1; i >= 0; i--) {
                if (validFormat.indexOf(options.format.charAt(i)) == -1)
                    suffix = options.format.charAt(i) + suffix;
                else
                    break;
            }

            options.format = options.format.substring(prefix.length);
            options.format = options.format.substring(0, options.format.length - suffix.length);


            // now we need to convert it into a number
            while (text.indexOf(group) > -1)
                text = text.replace(group, '');
            var number = new Number(text.replace(dec, ".").replace(neg, "-"));

            // special case for percentages
            if (suffix == "%")
                number = number * 100;

            var returnString = "";

            var decimalValue = number % 1;
            if (options.format.indexOf(".") > -1) {
                var decimalPortion = dec;
                var decimalFormat = options.format.substring(options.format.lastIndexOf(".") + 1);
                var decimalString = new String(decimalValue.toFixed(decimalFormat.length));
                decimalString = decimalString.substring(decimalString.lastIndexOf(".") + 1);
                for (var i = 0; i < decimalFormat.length; i++) {
                    if (decimalFormat.charAt(i) == '#' && decimalString.charAt(i) != '0') {
                        decimalPortion += decimalString.charAt(i);
                        continue;
                    }
                    else if (decimalFormat.charAt(i) == '#' && decimalString.charAt(i) == '0') {
                        var notParsed = decimalString.substring(i);
                        if (notParsed.match('[1-9]')) {
                            decimalPortion += decimalString.charAt(i);
                            continue;
                        }
                        else {
                            break;
                        }
                    }
                    else if (decimalFormat.charAt(i) == "0") {
                        decimalPortion += decimalString.charAt(i);
                    }
                }
                returnString += decimalPortion
            }
            else
                number = Math.round(number);

            var ones = Math.floor(number);
            if (number < 0)
                ones = Math.ceil(number);

            var onePortion = "";
            if (ones == 0) {
                onePortion = "0";
            }
            else {
                // find how many digits are in the group
                var onesFormat = "";
                if (options.format.indexOf(".") == -1)
                    onesFormat = options.format;
                else
                    onesFormat = options.format.substring(0, options.format.indexOf("."));
                var oneText = new String(Math.abs(ones));
                var groupLength = 9999;
                if (onesFormat.lastIndexOf(",") != -1)
                    groupLength = onesFormat.length - onesFormat.lastIndexOf(",") - 1;
                var groupCount = 0;
                for (var i = oneText.length - 1; i > -1; i--) {
                    onePortion = oneText.charAt(i) + onePortion;

                    groupCount++;

                    if (groupCount == groupLength && i != 0) {
                        onePortion = group + onePortion;
                        groupCount = 0;
                    }

                }
            }
            returnString = onePortion + returnString;

            // handle special case where negative is in front of the invalid
            // characters
            if (number < 0 && negativeInFront && prefix.length > 0) {
                prefix = neg + prefix;
            }
            else if (number < 0) {
                returnString = neg + returnString;
            }

            if (!options.decimalSeparatorAlwaysShown) {
                if (returnString.lastIndexOf(dec) == returnString.length - 1) {
                    returnString = returnString.substring(0, returnString.length - 1);
                }
            }
            returnString = prefix + returnString + suffix;

            if (jQuery(this).is(":input"))
                jQuery(this).val(returnString);
            else
                jQuery(this).text(returnString);

        });
    };

    jQuery.fn.parse.defaults = {
        locale: "nl",
        decimalSeparatorAlwaysShown: true
    };

    jQuery.fn.format.defaults = {
        format: "#,###.00",
        locale: "nl",
        decimalSeparatorAlwaysShown: true
    };



})(jQuery);
//
function daysInMonth(month, year) {
    var dd = new Date(year, month, 0);
    return dd.getDate();
}
//Show/Hide Waiting Layer
function showWaitingPage(msg) {

    if (typeof msg != "undefined") {  
        $("#waitingPage .waitingText").html(msg);
    }
    //alert("watiing");
    $.blockUI({ message: $("#waitingPage"), css: { width: '390px'} });
   
    
}

function hideWaitingPage() {
    $.unblockUI();
}

/*
Sys.Mvc.ValidatorRegistry.validators["postcode"] = function(rule) {
    return function(value, context) {
        if (value) {
            var s = value.split("@");
            if (s.length >= 2) {
                var postCodeNumberRegex = /^\d{4}$/;
                var postCodeLetterRegex = /^[A-Za-z]{2}$/;
                if (postCodeNumberRegex.test(s[0]) && postCodeLetterRegex.test(s[1])) {
                    return true;
                }
            }
        }
        return rule.ErrorMessage;
    }
};
*/

// TODO: ???
//$(document).ready(function(){
//	$('input').each(function(){
//		$(this).addClass($(this).attr('type'));
//    });
//})
//Keep Alive
function keepAlive() {
    $.get("/Hotel/KeepAlive?random=" + Math.random());
    window.setTimeout("keepAlive()", 60000);
}
//Calculate the height of car summary
function reFixCartSummaryHeight() {
    $('.right-box-content>span').not('.full, .break').each(function(index) {
        if (index % 2 == 0) {
            $this = $(this);
            $next = $this.next();
            if ($this.hasClass("flightcaption")) {
                $this.css('width', '92px');
                //$this.css('width', '100px');
                $next.css('display', 'none');
                //$next.css('height', '1px');
            }
            else {
                $this.css('width', '81px');
                //$this.css('width', '100px');
                $next.css('text-align', 'right');
                $next.css('width', '65px');

                if ($this.height() > $next.height()) {
                    $next.height($this.height());
                    if ($('a', $next).length == 0) {
                        //$next.wrapInner('<span class="bottom"></span>');
                        $next.wrapInner('<span class="bottom" style="width:65px"></span>');
                    }
                }
                else if ($this.height() < $next.height()) {
                    $this.height($next.height());
                    if ($('a', $this).length == 0) {
                        $this.wrapInner('<span class=""></span>');
                    }
                }
            }
            //

        }
    });
}


//Refresh Cart Summary
//Refresh Cart
function refreshCart() {
    $.ajax({
        url: "/Combination/CartSummary",
        cache: false,
        success: function(data) {
            //Update the Option List
            var updatePanel = $("#cartpanel div.right");
            $(updatePanel).html("");
            $(data).appendTo($(updatePanel));
            reFixCartSummaryHeight();
        },
        error: function(xhr) {
            checkSessionTimeout(xhr);
        },
        complete: function() {
            hideWaitingPage();
        }
    });
}
//
function ie8SafePreventEvent(e) {
    if (e.preventDefault) { e.preventDefault() }
    else { e.stop() }; 

    e.returnValue = false;
    e.stopPropagation();
}
//
function updateDepartures(destination, destinationId) {
    if (typeof (destinationId) == 'undefined') {
        var dlDestinations = $('#locationlist');
        var destinationText = '- Maak uw keuze -';
        dlDestinations.find('dd ul li').each(function(index, element) {
            var value = element.getAttribute('citycode');
            if (value != null) {
                if (value.toUpperCase() == destination.toUpperCase()) {
                    destinationText = $(element).attr('desc');
                }
            }
        });
        dlDestinations.find("dt a span").html(destinationText);
        //$('#destinationId').val(destination);
        $('#destinationName').val(destinationText);
    }
    $('#destinationId').val(destination);
    $('#locationlist2 dt a span').html('BENELUX (ALLE LUCHTHAVENS)');
    $('#departureId').val('N/REG/7886/52');
    $('#departureName').val('BENELUX (ALLE LUCHTHAVENS)');

    filterDepartures();

    /*var isIEUnder = $.browser.msie && parseInt($.browser.version, 10) <= 8;
    showWaitingPage(SEARCHING);
    $.ajax({
        url: "/AirTicket/UpdateDepartures",
        data: { destination: encodeURIComponent(destination), departure: $('#departureId').val() },
        cache: false,
        success: function(data) {
            var dlDepartures = $('#locationlist2');
            var ulDepartures = dlDepartures.find('dd ul:first');
            ulDepartures.empty();
            ulDepartures.html(data);
            var location = $('#departureName');
            var locationid = $('#departureId');
            dlDepartures.find("dd ul li a").click(function() {
                var text = $(this).html();
                var selectedValue = $(this).parent()[0].getAttribute('value');
                if (selectedValue == 0) {
                    return false;
                }
                var desc = $(this).parent().attr('desc');
                var span = dlDepartures.find("dt a span:first");
                span.html(desc);
                span.shortenText();
                dlDepartures.find("dd ul").hide();
                if (location && locationid) {
                    location.val(desc);
                    locationid.val(selectedValue);
                }

                var isBenelux = $(this).parent()[0].getAttribute('isbenelux');
                if (isBenelux == "True") {
                    $('#locationlist .flight-location li[isbenelux=True]').hide();
                    $('#locationlist .flight-location li[isbenelux=False]').show();
                }
                else {

                    $('#locationlist .flight-location li[isbenelux=False]').hide();
                    $('#locationlist .flight-location li[isbenelux=True]').show();
                }
            });

            var liDeparture = ulDepartures.find('li[selected=True]');
            if (liDeparture.length == 0) {
                liDeparture = ulDepartures.find("li:first");
            }

            $('#departureId').val(liDeparture[0].getAttribute('value'));
            var departureText = liDeparture[0].getAttribute('desc');
            $('#departureName').val(departureText);
            var span = dlDepartures.find("dt a span:first");
            span.html(departureText);
            span.attr("isbenelux", liDeparture[0].getAttribute('isbenelux'));
            span.shortenText();
        },
        complete: function() {
            hideWaitingPage();
        }
    });*/
}

function filterDestinations() {
    var isBenelux = $('#locationlist2 dt span').attr('isbenelux');
    var selectedText = $('#locationlist dt a span').text().trim();
    if (isBenelux == "True") {
        $('#locationlist dd li[isbenelux=True]').hide();
        $('#locationlist dd li[isbenelux=False]').show();
        if (selectedText == "BENELUX (ALLE LUCHTHAVENS)") {
            $('#locationlist dt a span').text('EUROPESE BESTEMMINGEN');
            $('#destinationId').val('');
            $('#destinationName').val('- Maak uw keuze -');
        }
    }
    else {
        $('#locationlist dd li[isbenelux=False]').hide();
        $('#locationlist dd li[isbenelux=True]').show();
        if (selectedText == "EUROPESE BESTEMMINGEN") {
            $('#locationlist dt a span').text('BENELUX (ALLE LUCHTHAVENS)');
            $('#destinationId').val('');
            $('#destinationName').val('BENELUX (ALLE LUCHTHAVENS)');
        }
    }
}

function filterDepartures() {
    var destinationId = document.getElementById('destinationId').value;
    if (destinationId == '' || destinationId == '0') return;
    var li = $('#locationlist dd li[value=' + destinationId + ']');
    var isBenelux = 'False';
    if (li.length) {
        isBenelux = li.attr('isbenelux');
    } else {
        li = $('#locationlist dd li[cityCode=' + destinationId + ']');
        isBenelux = li.attr('isbenelux');
    }

    var selectedText = $('#locationlist2 dt a span').text().trim();
    if (isBenelux == "True") {
        $('#locationlist2 dd li[isbenelux=True]').hide();
        $('#locationlist2 dd li[isbenelux=False]').show();
        if ($('#departureId').val() == '' ||
            $('#locationlist2 li[value=' + $('#departureId').val() + ']').attr('isbenelux') == 'True' ||
            $('#locationlist2 li[cityCode=' + $('#departureId').val() + ']').attr('isbenelux') == 'True') {
            $('#locationlist2 dt a span').html('- Maak uw keuze -');
            $('#departureId').val('N/REG/7886/52');
            $('#departureName').val('- Maak uw keuze -');
        }
    }
    else {
        $('#locationlist2 dd li[isbenelux=False]').hide();
        $('#locationlist2 dd li[isbenelux=True]').show();
        if ($('#departureId').val() == '' ||
            $('#locationlist2 li[value=' + $('#departureId').val() + ']').attr('isbenelux') == 'False' ||
            $('#locationlist2 li[cityCode=' + $('#departureId').val() + ']').attr('isbenelux') == 'False') {
            $('#locationlist2 dt a span').html('BENELUX (ALLE LUCHTHAVENS)');
            $('#departureId').val('N/REG/7886/52');
            $('#departureName').val('BENELUX (ALLE LUCHTHAVENS)');
        }
    }
}

