﻿    function DateToTriplet(d)
    {
        var t = new Array(d.getFullYear(), d.getMonth() + 1, d.getDate());
        return t;
    }
    function trim(tx) {
        return tx.replace(/^\s+|\s+$/g,"");
    }
    function ltrim(stringToTrim) {
        return tx.replace(/^\s+/,"");
    }
    function rtrim(stringToTrim) {
        return tx.replace(/\s+$/,"");
    }
   
    function ShowTooltip(tooltip)
    {
        window.setTimeout(function()
        {         
            tooltip.show();
        }, 0);
    }   
    function openWindow(url, name)
    {
        var curWnd = GetRadWindow();
        if (curWnd) {
            var wnd = curWnd.BrowserWindow;
            if (wnd == null) {
                wnd = curWnd.get_windowManager();
            }
            if (wnd) {
                var mWnd = wnd.radopen(url, name);
                return mWnd;
            }
        }
        return radopen(url, name);
    }
    
//New stuff from master page

function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}
function openRadWindowWithReturn(url, width, height, refresh, name,modal) 
{
    return BuildWindow(url, width, height, refresh, name,modal);
}

function openRadWindow(url, width, height, refresh, name,modal) 
{
    BuildWindow(url, width, height, refresh, name,modal);
}

function BuildWindow(url, width, height, refresh, name,modal) 
{
    try {
        var windowWidth = 650;
        var windowHeight = 400;
        var windowName = "OnlyOneWindowAtATime";
        var refreshParent = false;

        if (name) {
            windowName = name;
        }

        if (width) {
            windowWidth = width;
        }

        if (refresh) {
            if (refresh == true) {
                refreshParent = true;
            }
        }

        if (height) {
            windowHeight = height;
        }

        var dialog = radopen(url, windowName);
        dialog.set_width(windowWidth);
        dialog.set_height(windowHeight);
        dialog.center();
        if (modal != null ) {
            dialog.set_modal(modal);
        }
        else {
            dialog.set_modal(true);
        }
        if (refreshParent == true) {
            dialog.add_close(refreshWindow);
        }
    }
    catch (err) {
        alert(err.description + "\n Try again please..");
    }
    return dialog;
}

// Event occurs when user click on RadTabStrip.
function onTabSelecting(sender, args)
{
    if (args.get_tab().get_pageViewID())
    {
        args.get_tab().set_postBack(false);
    }
}

function getToolBarMenu() {
    var menuData = {
        "searchLoads": { "Width": 670, "Height": 450, "URL": "addUpdateSearches.aspx?type=Loads"},
        "searchTrucks": { "Width": 670, "Height": 450, "URL": "addUpdateSearches.aspx?type=Trucks"},
        "searchLanes": { "Width": 670, "Height": 450, "URL": "addUpdateSearches.aspx?type=Lanes"},
        "favoriteLoadSearch": { "Width": 670, "Height": 510, "URL": "FavoriteSearches.aspx?type=Loads"},
        "favoriteTruckSearch": { "Width": 670, "Height": 510, "URL": "FavoriteSearches.aspx?type=Trucks"},
        "favoriteLaneSearch": { "Width": 670, "Height": 510, "URL": "FavoriteSearches.aspx?type=Lanes"},
        "favoriteLoadPosts": { "Width": 670, "Height": 560, "URL": "Favorites.aspx?type=Loads"},
        "favoriteTruckPosts": { "Width": 670, "Height": 560, "URL": "Favorites.aspx?type=Trucks"},
        "favoriteLanePosts": { "Width": 670, "Height": 560, "URL": "Favorites.aspx?type=Lanes"},
        "postLoads": { "Width": 670, "Height": 560, "URL": "addUpdateSingleLoads.aspx"},
        "postTrucks": { "Width": 670, "Height": 560, "URL": "addUpdateSingleTrucks.aspx"},
        "postLanes": { "Width": 670, "Height": 560, "URL": "addUpdateSingleLanes.aspx"},
        "postTruckBids": { "Width": 670, "Height": 560, "URL": "AddUpdateSingleTruckBids.aspx"},
        "historicalLoads": { "Width": 770, "Height": 510, "URL": "HistoricalPostings.aspx?type=Loads" },
        "historicalTrucks": { "Width": 770, "Height": 510, "URL": "HistoricalPostings.aspx?type=Trucks" },
        "historicalLanes": { "Width": 770, "Height": 510, "URL": "HistoricalPostings.aspx?type=Lanes" }
    }
    return menuData;
}

function toolbarMenu_Click(sender, eventArgs) {

    var menuData = getToolBarMenu();
    var menuItem = menuData[eventArgs.get_item().get_value()];
    openRadWindow(menuItem.URL, menuItem.Width, menuItem.Height);
}
function reportMenu_Click(sender, args)
{
    var myWidth = 650;
    var myHeight = 400;
    switch(args.get_item().get_value())
    {
        case "non_payment":
        {
            myWidth = 630;
            myHeight = 600;
            openRadWindow("/Report/NonPayment.aspx", myWidth, myHeight);
            break;
        }
        case "daystopay":
        {
            myWidth = 630;
            myHeight = 600;
            openRadWindow("/Report/DaysToPay.aspx", myWidth, myHeight);
            break;
        }
        case "broker_performance_report":
        {
            myWidth = 630;
            myHeight = 600;
            openRadWindow("/Report/BrokerPerformanceReport.aspx", myWidth, myHeight);
            break;
        }
        case "carrier_performance_report":
        {
            myWidth = 630;
            myHeight = 600;
            openRadWindow("/Report/CarrierPerformanceReport.aspx", myWidth, myHeight);
            break;
        }
        default:
        {
            //Do nothing
            break;
        }
    }
}
function manageVisitedCookie(SearchType, SearchID)
{
    if(SearchID != null && SearchID != "")
    {
        var cookieName = SearchType + "Visited";
        var id = SearchID;
        ///Retrieve the value section of the cookie
        var cookieValue = getCookie(cookieName);
        if(cookieValue == "")
        {
            ///If it doesn't have a value create a new cookie
            setCookie(cookieName, id, 30);
        }
        else
        {
            ///Is the ID already set in the cookie
            //if(cookieValue.cont)
            //{
                ///If it does have a value append the new value to the cookie
                setCookie(cookieName, cookieValue + "," + id, 30);
            //}
        }
    }
    return false;
}
function getCookie(cookieName)
{
    ///Are there any cookies
    if (document.cookie.length>0)
    {
        ///Does our specific cookie exist
        cookieValueStart=document.cookie.indexOf(cookieName + "=");
        if (cookieValueStart != -1)
        {
            ///This value figures out where the value starts
            cookieValueStart = cookieValueStart + cookieName.length+1;
            ///This value figures out where the value ends. The ; would indicate and expire date
            cookieValueEnd = document.cookie.indexOf(";",cookieValueStart);
            if (cookieValueEnd == -1) 
            {
                ///If for some reason there is no expire date, just set it to the cookie length
                cookieValueEnd = document.cookie.length;
            }
            return unescape(document.cookie.substring(cookieValueStart, cookieValueEnd));
        }
    }
    return "";
}

function setCookie(cookieName,value,expiredays)
{
    ///Grab the current date
    var exdate=new Date();
    ///Add the days that are passed in
    exdate.setDate(exdate.getDate()+expiredays);
    ///Set a new cookie
    document.cookie = cookieName+ "=" + value + ((expiredays==null) ? "" : ";expires="+exdate);
}

function Searching_PageSizeChanging(sender, eventArgs)
{
    var item = eventArgs.get_item();
    setCookie("SearchingPageSize",item.get_text(),30);
}
function Posting_PageSizeChanging(sender, eventArgs)
{
    var item = eventArgs.get_item();
    setCookie("PostingPageSize",item.get_text(),30);
}
function OpenAUP()
{
    myWidth = 630;
    myHeight = 600;
    openRadWindow("/Content/AUP.htm", myWidth, myHeight);
}

function findParameterInURL(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 bustFrameToAuthenticateRedirectingToParent() {
    if (parent.location.href != location.href) {
        parent.location.href = "/Authenticate.aspx?redirect=" + escape(parent.location.href)
    }
}