/// <reference path="GlobalRefs.js" />

// Define Namespaces
if (CBC == null || typeof (CBC) != "object") var CBC = new Object();
if (CBC.Common == null || typeof (CBC.Common) != "object") CBC.Common = new Object();
// End Define Namespaces

CBC.Common.checkDate = function (year, month, day) {
    /// <summary>Test a date to make sure it is valid</summary>
    /// <param name="year" type="int">The year</param>
    /// <param name="month" type="int">The month</param>
    /// <param name="day" type="day">The day</param>
    /// <returns type="bool" />

    var d = CBC.Common.getDate(year, month, day);
    if (d.getDate() == day &&
        (d.getMonth() + 1) == month &&
        d.getFullYear() == year) {

        return true;

    }
    return false;
}

CBC.Common.getDate = function (year, month, day) {
    /// <summary>Craete a date object</summary>
    /// <param name="year" type="int">The year</param>
    /// <param name="month" type="int">The month</param>
    /// <param name="day" type="day">The day</param>
    /// <returns type="Date" />
    var d = new Date(year,month-1, day);
    return d;
};

CBC.Common.isDefined = function (obj) {
    /// <summary>Evaluating the passed obj and decide if it is null, undefined or defined</summary>
    /// <param name="obj" type="object">Object to be avaluated</param>
    /// <returns type="bool" />
    try {
        eval(obj);
        return true;
    } catch (e) {
        return false;
    }
}


CBC.Common.windowPopUp = function (jAnchors, features) {
    /// <summary>Displays a popup window based on the features provided</summary>
    /// <param name="jAnchors" type="string">The jQuery selector of the anchors that you want to apply the popup to</param>
    /// <param name="features" type="string">The features to apply to the popup window</param>

    // Default features
    var WINDOW_DEFAULT_FEATURES = "location=0,statusbar=0,menubar=0,width=400,height=300";
    
    //var features = "";
    // Assign the default features if none have been passed
    if (!CBC.Common.isDefined(features)) {
        features = WINDOW_DEFAULT_FEATURES;
    }

    $(document).ready(function () {
        // Loop through all the anchors
        $(jAnchors).each(function () {
            // Collect the href attribute
            var url = $(this).attr("href");
            // Collect the target attribute (assign _blank if it is empty)
            //var target = $(this).attr("target") == "" ? "_blank" : $(this).attr("target");
            $(this).click(function () {
                // Open the popup
                window.open(url, "_blank", features);
                // We return false to stop the link href from being followed
                return false;
            });
        });
    });
}

CBC.Common.setQuickLinks = function (jAnchors, selectId) {
    /// <summary>Displays a popup window based on the features provided</summary>
    /// <param name="jAnchors" type="string">The jQuery selector of the anchors that you want to apply the settings</param>
    /// <param name="selectId" type="string">The select ID that you want to apply the submit</param>
    $(document).ready(function () {
        $("#" + jAnchors).click(function () {
            CBC.Common.submitQuickLinks(selectId);
        });
    });
}

CBC.Common.submitQuickLinks = function (selectId) {
    /// <summary>Open the link choosen in the passed drop down menu</summary>
    /// <param name="selectId" type="string">The select ID that you want to apply the submit</param>
    var selectMenu = $("#" + selectId);
    var choosenLink = selectMenu[0].value;
    CBC.Common.openUrl(choosenLink);
    return false;
}

CBC.Common.openUrl = function (url, opt) {
    /// <summary>Open the passed url</summary>
    /// <param name="url" type="string">Url to be opened</param>
    /// <param name="opt" type="integer">Option on how to open the window. 0 current window. 1 new window. 2 background window</param>
    if (opt == 0 || CBC.Common.isDefined(opt)) // current window
        window.location = url;
    else if (opt == 1) // new window
        window.open(url);
    else if (opt == 2) // background window
    { window.open(url); self.focus(); }
    return false;
}
