﻿//Adding Contains Method to Array
Array.prototype.IndexOf = function(searchStr) {
    for (i = 0; i < this.length; i++) {
        if (this[i] === searchStr) {
            return i;
        }
    }
    return -1;
}

Array.prototype.Contains = function(searchStr) {
    for (i = 0; i < this.length; i++) {
        if (this[i] == searchStr) {
            return true;
        }
    }
    return false;
}

Array.prototype.Clear = function() {
    while (this.length > 0) {
        this.pop();
    }
}

//String Format Prototypes
String.prototype.format = function() { var pattern = /\{\d+\}/g; var args = arguments; return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; }); }


//Widgets Drag Drop Functions
var WidgetUI = {
    WidgetUIServicePath: '',
    Actions: {
        minimizeWidget: function(divId) {
            var div = $get(divId);
            div.style.display = "none";
        },

        onDrop: function(sender, e) {
            var container = e.get_container();
            var item = e.get_droppedItem();
            var position = e.get_position();
            var instanceId = item.getAttribute("InstanceId");
            var columnNo = parseInt(container.getAttribute("columnNo"));
            var row = position;

            $.ajax({
                type: "GET",
                contentType: "application/json",
                url: WidgetUI.WidgetUIServicePath + "/moveWidgetInstance/" + instanceId + "/" + columnNo + "/" + row,
                data: null,
                cache: false,
                processData: false,
                success: function(result) {
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                }
            });
        },

        hide: function(id) {
            document.getElementById(id).style.display = "none";
        },

        addWidget: function(pageId, widgetName, parameters, OnSucess) {
            $.ajax({
                type: "GET",
                contentType: "application/json",
                url: WidgetUI.WidgetUIServicePath + "/addWidget/" + pageId + "/" + widgetName,
                data: null,
                cache: false,
                processData: false,
                success: function(result) {
                }
            });
        }
    },

    RichTextBox: {
        Init: function(control) {
            try {
                tinyMCE.init(
                {
                    mode: 'textareas',
                    theme: 'advanced',
                    elements: control,
                    skin: 'o2k7',
                    plugins: 'safari,style,table,advhr,advimage,advlink,emotions,insertdatetime,preview,media,searchreplace,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups',
                    theme_advanced_buttons1: 'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect',
                    theme_advanced_buttons2: 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,image,cleanup,code',
                    theme_advanced_buttons3: 'tablecontrols,|,hr,removeformat,|,sub,sup,|,charmap,emotions,media,advhr,|,ltr,rtl',
                    theme_advanced_buttons4: 'insertdate,inserttime,preview,|,forecolor,backcolor,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,|,fullscreen',
                    theme_advanced_toolbar_location: 'top',
                    theme_advanced_toolbar_align: 'left',
                    theme_advanced_statusbar_location: 'bottom',
                    theme_advanced_resizing: false

                }
                );
            }
            catch (e)
            { }
        }
    }
};

var WidgetContainer =
{
    Actions:
     {
         ContainsKey: function(key) {
             for (var x in this) {
                 if (typeof this[x] != 'undefined' && typeof this[x] != 'function' && x == key)
                     return true
             }
             return false;
         },

         AddListener: function(eventName, observer) {
             if (this.ContainsKey(eventName) == false) {
                 this[eventName] = new Array();
             }
             var list = this[eventName];
             if (list.IndexOf(observer) < 0) {
                 list.push(observer);
             }
         },

         RemoveListener: function(eventName, observer) {
             if (this.ContainsKey(eventName) == true) {
                 var list = this[eventName];
                 var index = list.IndexOf(observer);
                 if (index >= 0) {
                     var temp = new Array();
                     for (var i in list) {
                         if (i != index) {
                             temp.push(list[i]);
                         }
                     }
                     list = temp;
                 }
             }
         },
         RaiseEvent: function(e, source, args) {
             if (this.ContainsKey(e) == true) {
                 var event = jQuery.Event(e);
                 event.source = source;
                 event.args = args;

                 var list = this[e];

                 $.each(list, function(i) {
                     $('#' + this).trigger(event);
                 });

             }
         }
     }
}


function ShowFileUploadDialog(uploadControl, uploadUrl) {
    var extender = $find(uploadControl);
    extender.dialog.setUrl(uploadUrl);
    extender.showDialog();
    return false;
}

function ShowFileManager(title, url, paramaters) {
    if (url == null || url == '')
        return false;

    var path;
    if (paramaters == null || paramaters == '')
        path = String.format("{0}?TB_iframe=true&width=721&height=375&modal=true", url);
    else
        path = String.format("{0}?{1}&TB_iframe=true&width=721&height=375&modal=true", url, paramaters);

    tb_show(title, path, null);
    return false;
}


function ShowModalPopupWindow(title, url, paramaters, width, height) {
    if (url == null || url == '')
        return false;
    if (paramaters != null && paramaters == '') {
        url = String.format("{0}?{1}", url, paramaters);
    }

    if (window.showModalDialog) {
        var dialogArguments = new Object();
        window.showModalDialog(url, dialogArguments, 'dialogHeight=' + height + 'px,dialogTop=10,dialogWidth=' + width + 'px,center=1,resizable=1,scroll=1');
    }
    else {
        alert("Can't show pop up window"); 
    }
    return false;
}



//Air Filter Criteria Object for Interwidget Communication
function AirFilterCriteria() {
    var Val = function() {
        this.Min = 0;
        this.Max = 0;
        this.Value = [];
    }
    var FT = function() {
        this.EnableTakeOffTime = null;
        this.EnableLandingTime = null;
        this.TakeOffTime = new Val();
        this.LandingTime = new Val();
    }

    var Stop = function() {
        this.Disabled = null;
        this.Active = null;
    }

    this.CMF = function() {
        this.Checked = false;
        this.Company = new (function() {
            this.Code = '';
            this.ShortName = '';
            this.FullName = '';
        });
        this.MinFare = new (function() {
            this.Amount = 0;
            this.UsdEquivAmount = 0;
            this.Currency = '';
        });
    }

    this.Stops = new Object();
    this.Stops.NonStop = new Stop();
    this.Stops.OneStop = new Stop();
    this.Stops.TwoPlusStops = new Stop();
    
    this.ShowAllAirLines = true;
    this.AirLines = [];
    this.TripDuration = new Val();
    this.Price = new Val();
    this.FlightTimes =
                {
                    EnableReturn: null,
                    Depart: new FT(),
                    Return: new FT()
                };
}

function HotelFilterCriteria() {
    var val = function() {
        this.min = 0;
        this.max = 1;
        this.value = [];
    };
    var nameVal = function() {
        this.name = "";
        this.minVal = 0;
    };

    this.price = new val();
    this.distance = new val();
    this.fareType = {
        prepaid: 1,
        postpaid: 1,
        enablePrepaid: 1,
        enablePostpaid: 1
    };
    this.hotelName = "";
    this.amenities = [];
    this.hotelBrand = [];
    this.starRating = [];
}

function TripsFilterValues() {
    var valClass = function() {
        this.eleId = '';
        this.eleVal = '';
    }

    this.tripName = new valClass();
    this.startDate = new valClass();
    this.endDate = new valClass();
    this.locator = new valClass();
    this.travName = new valClass();
}