﻿/// <reference path="jquery-1.3.2.js"/>
/* for menu */
var hoverClass = "AspNet-Menu-Hover";
var topmostClass = "AspNet-Menu";
var userAgent = navigator.userAgent;
var versionOffset = userAgent.indexOf("MSIE");
var isIE = (versionOffset >= 0);
var isIE7 = false;
var isPostIE7 = false;
var isPreIE7 = false;
var fullVersionIE = "";
var majorVersionIE = "";
if (isIE) {
    fullVersionIE = parseFloat(userAgent.substring(versionOffset + 5, userAgent.length));
    majorVersionIE = parseInt('' + fullVersionIE);
    isPreIE7 = majorVersionIE < 7;
    isIE7 = majorVersionIE == 7;
    isPostIE7 = majorVersionIE > 7;
}
/* end menu */

function ModalDialog(url, title, width, height) {
    if (!url)
        this.Url = "/ContactUs/Corporate/corporateDiv.aspx";
    else
        this.Url = url;

    if (!width)
        this.Width = 578;
    else
        this.Width = width;

    if (!height)
        this.Height = 600;
    else
        this.Height = height;

    if (!title)
        this.Title = "Contact Us";
    else
        this.Title = title;
}
var ContactUs = new ModalDialog();

function GlobalReady() {
    InitializeNewWindows();
    InitializeSearchValidation();

    $('div#search-toggle').click( function () { $('div#search').slideToggle(); });

    if (top != self)
        top.location.replace(self.location.href);

    /* for Menu */
    $('.SaiMenu').css('display', 'block');

    if (isPreIE7) {
        window.onload = SetHover__AspNetMenu;
    }

    // for IE8+ also since we run in ie7 compatibility mode
    if (isIE7 || isPostIE7) {
        /* IE7 has a bug which displays the 3rd Tier menu without
        the text as it retains its block status visually. Here we
        force the third tier menu to not display, and only display
        it when mouseover the 2nd tier element for that 3rd tier menu */
        $('ul.AspNet-Menu ul ul').css('display', 'none');
        $('ul.AspNet-Menu ul li').mouseover(function() {
            $(this).children('ul').css('display', 'block');
        });
        $('ul.AspNet-Menu ul li').mouseout(function() {
            $(this).children('ul').css('display', 'none');
        });
    }    
    
    /* end menu */
}

function InitializeNewWindows() {
    $("a.popupSmall").newWindow({ windowoptions: 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=375,height=375,left=20,top=10' });
    $("a.popupLarge").newWindow({ windowoptions: 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=750,height=750,left=20,top=10' });
    $("a.popupFull").newWindow({ windowoptions: 'toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,fullscreen=1' });
    $("a.popupDefault").newWindow();
}

function AdjustSearchBox() {
    if (jQuery.support.cssFloat) {
        $("#searchbutton").css("margin-top", "0px");
    }
    
    $("#searchtext input").focus(function()
    {
        $(this).val("");
    });
}



function InitializeSearchValidation() {
    $("#cse-search-box").submit(function(){
        var search = $("#searchtext > :input").val();
        if (search.length == 0 || search.toLowerCase() == "search")
            return false;

        return true;
    });
}

function LayoutReady() {
    InitializeContactUs();
    InitializeRequestTrial();
    InitializeFreeSubscription();
    InitializeDownloadResource();
    InitializeFreeDownload();
    InitializeRequestDemo();
    InitializeEmailPage();
}

function InitializeContactUs() {

    $("#contactUsButton").bind("click", $("#contactUsButton a").attr("href"), ShowContactUsForm);
    $("#contactUsButton2").bind("click", $("#contactUsButton2 a").attr("href"), ShowContactUsForm);
    $("#contactUsButton3").bind("click", $("#contactUsButton3 a").attr("href"), ShowContactUsForm);
    $("#contactUsButton4").bind("click", $("#contactUsButton4 a").attr("href"), ShowContactUsForm);
    $("#contactUsButton5").bind("click", $("#contactUsButton5 a").attr("href"), ShowContactUsForm);
    
    $("#contactUsLink").bind("click", $("#contactUsLink").attr("href"), ShowContactUsForm);
    $("#contactUsLink2").bind("click", $("#contactUsLink2").attr("href"), ShowContactUsForm);
    $("#contactUsLink3").bind("click", $("#contactUsLink3").attr("href"), ShowContactUsForm);
    $("#contactUsLink4").bind("click", $("#contactUsLink4").attr("href"), ShowContactUsForm);
    $("#contactUsLink5").bind("click", $("#contactUsLink5").attr("href"), ShowContactUsForm);

    $("#contactUsButton a").attr("href", "#");
    $("#contactUsButton2 a").attr("href", "#");
    $("#contactUsButton3 a").attr("href", "#");
    $("#contactUsButton4 a").attr("href", "#");
    $("#contactUsButton5 a").attr("href", "#");
    
    $("#contactUsLink").attr("href", "#");
    $("#contactUsLink2").attr("href", "#");
    $("#contactUsLink3").attr("href", "#");
    $("#contactUsLink4").attr("href", "#");
    $("#contactUsLink5").attr("href", "#");
}

function InitializeRequestTrial() {
    $("#FreeTrial").bind("click", $("#FreeTrial a").attr("href"), ShowRequestTrialForm);
    $("#FreeTrial a").attr("href", "#");

    $("#FreeTrialLink").bind("click", $("#FreeTrialLink").attr("href"), ShowRequestTrialForm);
    $("#FreeTrialLink2").bind("click", $("#FreeTrialLink2").attr("href"), ShowRequestTrialForm);
    $("#FreeTrialLink3").bind("click", $("#FreeTrialLink3").attr("href"), ShowRequestTrialForm);
    $("#FreeTrialLink4").bind("click", $("#FreeTrialLink4").attr("href"), ShowRequestTrialForm);
    $("#FreeTrialLink5").bind("click", $("#FreeTrialLink5").attr("href"), ShowRequestTrialForm);
    
    $("#FreeTrialLink").attr("href", "#");
    $("#FreeTrialLink2").attr("href", "#");
    $("#FreeTrialLink3").attr("href", "#");
    $("#FreeTrialLink4").attr("href", "#");
    $("#FreeTrialLink5").attr("href", "#");
}

function InitializeFreeSubscription() {
    $("#FreeSubscription").bind("click", $("#FreeSubscription a").attr("href"), ShowFreeSubscriptionForm);
    $("#FreeSubscription a").attr("href", "#");

    $("#FreeSubscriptionLink").bind("click", $("#FreeSubscriptionLink").attr("href"), ShowFreeSubscriptionForm);
    $("#FreeSubscriptionLink2").bind("click", $("#FreeSubscriptionLink2").attr("href"), ShowFreeSubscriptionForm);
    $("#FreeSubscriptionLink3").bind("click", $("#FreeSubscriptionLink3").attr("href"), ShowFreeSubscriptionForm);
    $("#FreeSubscriptionLink4").bind("click", $("#FreeSubscriptionLink4").attr("href"), ShowFreeSubscriptionForm);
    $("#FreeSubscriptionLink5").bind("click", $("#FreeSubscriptionLink5").attr("href"), ShowFreeSubscriptionForm);

    $("#FreeSubscriptionLink").attr("href", "#");
    $("#FreeSubscriptionLink2").attr("href", "#");
    $("#FreeSubscriptionLink3").attr("href", "#");
    $("#FreeSubscriptionLink4").attr("href", "#");
    $("#FreeSubscriptionLink5").attr("href", "#");
}

function InitializeDownloadResource() {

    $("#DownloadResource").bind("click", $("#DownloadResource a").attr("href"), ShowDownloadResourceForm);
    $("#DownloadResource a").attr("href", "#");

    $("#DownloadResourceLink").bind("click", $("#DownloadResourceLink").attr("href"), ShowDownloadResourceForm);
    $("#DownloadResourceLink2").bind("click", $("#DownloadResourceLink2").attr("href"), ShowDownloadResourceForm);
    $("#DownloadResourceLink3").bind("click", $("#DownloadResourceLink3").attr("href"), ShowDownloadResourceForm);
    $("#DownloadResourceLink4").bind("click", $("#DownloadResourceLink4").attr("href"), ShowDownloadResourceForm);
    $("#DownloadResourceLink5").bind("click", $("#DownloadResourceLink5").attr("href"), ShowDownloadResourceForm);

    $("#DownloadResourceLink").attr("href", "#");
    $("#DownloadResourceLink2").attr("href", "#");
    $("#DownloadResourceLink3").attr("href", "#");
    $("#DownloadResourceLink4").attr("href", "#");
    $("#DownloadResourceLink5").attr("href", "#");
}

function InitializeFreeDownload() {
    $("#FreeDownload").bind("click", $("#FreeDownload a").attr("href"), ShowFreeDownloadForm);
    $("#FreeDownload a").attr("href", "#");

    $("#FreeDownloadLink").bind("click", $("#FreeDownloadLink").attr("href"), ShowFreeDownloadForm);
    $("#FreeDownloadLink2").bind("click", $("#FreeDownloadLink2").attr("href"), ShowFreeDownloadForm);
    $("#FreeDownloadLink3").bind("click", $("#FreeDownloadLink3").attr("href"), ShowFreeDownloadForm);
    $("#FreeDownloadLink4").bind("click", $("#FreeDownloadLink4").attr("href"), ShowFreeDownloadForm);
    $("#FreeDownloadLink5").bind("click", $("#FreeDownloadLink5").attr("href"), ShowFreeDownloadForm);

    $("#FreeDownloadLink").attr("href", "#");
    $("#FreeDownloadLink2").attr("href", "#");
    $("#FreeDownloadLink3").attr("href", "#");
    $("#FreeDownloadLink4").attr("href", "#");
    $("#FreeDownloadLink5").attr("href", "#");
}

function InitializeRequestDemo() {

    $("#RequestDemo").bind("click", $("#RequestDemo a").attr("href"), ShowRequestDemoForm);
    $("#RequestDemo a").attr("href", "#");

    $("#RequestDemoLink").bind("click", $("#RequestDemoLink").attr("href"), ShowRequestDemoForm);
    $("#RequestDemoLink2").bind("click", $("#RequestDemoLink2").attr("href"), ShowRequestDemoForm);
    $("#RequestDemoLink3").bind("click", $("#RequestDemoLink3").attr("href"), ShowRequestDemoForm);
    $("#RequestDemoLink4").bind("click", $("#RequestDemoLink4").attr("href"), ShowRequestDemoForm);
    $("#RequestDemoLink5").bind("click", $("#RequestDemoLink5").attr("href"), ShowRequestDemoForm);

    $("#RequestDemoLink").attr("href", "#");
    $("#RequestDemoLink2").attr("href", "#");
    $("#RequestDemoLink3").attr("href", "#");
    $("#RequestDemoLink4").attr("href", "#");
    $("#RequestDemoLink5").attr("href", "#");
}

function InitializeEmailPage() {
    var subject = "FYI: SAI Global - " + $("#title").text();
    var body = "Hello,\n\rI thought you might be interested in this...\n\r" + window.location + "\n\rRegards,";

    subject = $.URLEncode(subject);
    body = $.URLEncode(body);

    $("#Emailthispage a").attr("href", "mailto:?subject=" + subject + "&body=" + body);
    $("#Emailthispage").bind("click", function(e) {
        if(!$(e.target).is("a"))
        {
            var win = window.open($("#Emailthispage").find("a:first").attr("href"));
            if (win && win.open && !win.closed)
                win.close();
        }
    });
}

function ShowDownloadResourceForm(e) {
    if (typeof (DownloadResource) != "undefined")
        ShowForm(e, DownloadResource);

    return false;
}

function ShowFreeDownloadForm(e) {
    if (typeof (FreeDownload) != "undefined")
        ShowForm(e, FreeDownload);

    return false;
}

function ShowRequestDemoForm(e) {
    if (typeof (RequestDemo) != "undefined")
        ShowForm(e, RequestDemo);

    return false;
}

function ShowRequestTrialForm(e) {    
    if(typeof(RequestTrial) != "undefined")
        ShowForm(e, RequestTrial);

    return false;
}

function ShowFreeSubscriptionForm(e) {
    if (typeof (FreeSubscription) != "undefined")
        ShowForm(e, FreeSubscription);

    return false;
}


function ShowContactUsForm(e) {
    if (typeof (ContactUs) != "undefined")
        ShowForm(e, ContactUs);

    return false;
}

function ShowForm(e, dialogObj) {
    var newUrl = dialogObj.Url;

    var $target = $(e.target);
    var $link = $target.parent().find("a:first");

    if ($target.is("a"))
        dialogObj.Title = $target.text();
    else if ($target.is("area"))
        dialogObj.Title = $target.attr("alt");
    else if ($link.length == 1)
        dialogObj.Title = $link.text();
    
    if (e.data != "#") {
        if ($.HasQueryStringParams(dialogObj.Url))
            newUrl += "&RedirectURL=" + $.URLEncode(e.data);
        else
            newUrl += "?RedirectURL=" + $.URLEncode(e.data);
    }

    if ($.HasQueryStringParams(newUrl))
        newUrl += "&Title=" + dialogObj.Title;
    else
        newUrl += "?Title=" + dialogObj.Title;
    
    $("#contactFrame").attr("src", "");
    $("#contactFrame").attr("src", newUrl);    
    $("#contactFrame")._load(IFrameLoaded);
    

    $("#contact").removeClass("hidden").dialog({
        modal: true,
        width: dialogObj.Width,
        height: dialogObj.Height,
        title: dialogObj.Title,
        position: 'top'
    }).bind('dialogclose', function(event, ui) {
        $(event.target).dialog('destroy');
    });
}

function IFrameLoaded() {
    $("#contactFrame").contents().find("a").each(function(i) {
        if ($(this).attr("target") != "_blank") {
            $(this).click(function() {
                self.location = $(this).attr("href");
            });
        }
    });
}

function ReadjustLayout() {
    $(".contentButtonText a").each(function(i) {
        if ($(this).height() <= 14) {
            $(this).parent().css("margin-top", "8px");
        }
        else {
            $(this).parent().css("margin-top", "0px");
        }
    });
    $("#extraContent").css("margin-top", ($("#title").height() + 8) * -1);
    AdjustSearchBox();
}


$.extend({ HasQueryStringParams: function(url) {
    return (url.substr(url.lastIndexOf("/")).indexOf("?") == -1) ? false : true;
    }
});

$.extend({ URLEncode: function(c) {
        var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
        while (x < c.length) {
            var m = r.exec(c.substr(x));
            if (m != null && m.length > 1 && m[1] != '') {
                o += m[1]; x += m[1].length;
            } else {
                if (c[x] == ' ') o += '+'; else {
                    var d = c.charCodeAt(x); var h = d.toString(16);
                    o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
                } x++;
            }
        } return o;
    },
    URLDecode: function(s) {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});

(function($) {
    $.extend($.fn, {
        newWindow: function(options) {
            var defaults = { windowoptions: '', open: function() { } };
            options = $.extend(defaults, options || {});

            return this.each(function() {
                $(this).click(function(e) {
                    e.preventDefault();
                    var newWindow = open(this.href, '', options.windowoptions);
                    options.open.call(newWindow, e);
                });
            });
        }
    });
})(jQuery);

function launchcountry(mySelect) {
    var myValue = mySelect[mySelect.selectedIndex].value;
    if (myValue != "") {
        document.location = myValue;
    }
}

/* Menu */
function CanHaveClass__CssFriendlyAdapters(element) {
    return ((element != null) && (element.className != null));
}

function HasAnyClass__CssFriendlyAdapters(element) {
    return (CanHaveClass__CssFriendlyAdapters(element) && (element.className.length > 0));
}

function HasClass__CssFriendlyAdapters(element, specificClass) {
    return (HasAnyClass__CssFriendlyAdapters(element) && (element.className.indexOf(specificClass) > -1));
}

function AddClass__CssFriendlyAdapters(element, classToAdd) {
    if (HasAnyClass__CssFriendlyAdapters(element)) {
        if (!HasClass__CssFriendlyAdapters(element, classToAdd)) {
            element.className = element.className + " " + classToAdd;
        }
    }
    else if (CanHaveClass__CssFriendlyAdapters(element)) {
        element.className = classToAdd;
    }
}

function AddClassUpward__CssFriendlyAdapters(startElement, stopParentClass, classToAdd) {
    var elementOrParent = startElement;
    while ((elementOrParent != null) && (!HasClass__CssFriendlyAdapters(elementOrParent, topmostClass))) {
        AddClass__CssFriendlyAdapters(elementOrParent, classToAdd);
        elementOrParent = elementOrParent.parentNode;
    }
}

function SwapClass__CssFriendlyAdapters(element, oldClass, newClass) {
    if (HasAnyClass__CssFriendlyAdapters(element)) {
        element.className = element.className.replace(new RegExp(oldClass, "gi"), newClass);
    }
}

function SwapOrAddClass__CssFriendlyAdapters(element, oldClass, newClass) {
    if (HasClass__CssFriendlyAdapters(element, oldClass)) {
        SwapClass__CssFriendlyAdapters(element, oldClass, newClass);
    }
    else {
        AddClass__CssFriendlyAdapters(element, newClass);
    }
}

function RemoveClass__CssFriendlyAdapters(element, classToRemove) {
    SwapClass__CssFriendlyAdapters(element, classToRemove, "");
}

function RemoveClassUpward__CssFriendlyAdapters(startElement, stopParentClass, classToRemove) {
    var elementOrParent = startElement;
    while ((elementOrParent != null) && (!HasClass__CssFriendlyAdapters(elementOrParent, topmostClass))) {
        RemoveClass__CssFriendlyAdapters(elementOrParent, classToRemove);
        elementOrParent = elementOrParent.parentNode;
    }
}

function IsEnterKey() {
    var retVal = false;
    var keycode = 0;
    if ((typeof (window.event) != "undefined") && (window.event != null)) {
        keycode = window.event.keyCode;
    }
    else if ((typeof (e) != "undefined") && (e != null)) {
        keycode = e.which;
    }
    if (keycode == 13) {
        retVal = true;
    }
    return retVal;
}

function Hover__AspNetMenu(element) {
    AddClass__CssFriendlyAdapters(element, hoverClass);

    if (isPreIE7) {
        var child = element.firstChild;
        while (child) {
            if (child.tagName == "UL") {
                var grandchild = child.firstChild;
                while (grandchild) {
                    if (grandchild.tagName == "LI") {
                        if ((typeof (grandchild.iFrameFormElementMask) != "undefined") && (grandchild.iFrameFormElementMask != null)) {
                            grandchild.iFrameFormElementMask.style.display = "block";

                            var w = grandchild.offsetWidth;
                            if ((grandchild.offsetWidth == 0) && (typeof (element.iFrameFormElementMask) != "undefined") && (element.iFrameFormElementMask != null) && (element.iFrameFormElementMask.style.width.length > 0)) {
                                w = element.iFrameFormElementMask.style.width;
                            }
                            grandchild.iFrameFormElementMask.style.width = w;

                            var h = grandchild.offsetHeight + 5 /* fudge to cover margins between menu items */;
                            if ((grandchild.offsetHeight == 0) && (typeof (element.iFrameFormElementMask) != "undefined") && (element.iFrameFormElementMask != null) && (element.iFrameFormElementMask.style.height.length > 0)) {
                                h = element.iFrameFormElementMask.style.height;
                            }
                            grandchild.iFrameFormElementMask.style.height = h;
                        }
                    }

                    grandchild = grandchild.nextSibling;
                }
            }

            child = child.nextSibling;
        }
    }
}

function Unhover__AspNetMenu(element) {
    RemoveClass__CssFriendlyAdapters(element, hoverClass);

    if (isPreIE7) {
        var child = element.firstChild;
        while (child) {
            if (child.tagName == "UL") {
                var grandchild = child.firstChild;
                while (grandchild) {
                    if (grandchild.tagName == "LI") {
                        if ((typeof (grandchild.iFrameFormElementMask) != "undefined") && (grandchild.iFrameFormElementMask != null)) {
                            grandchild.iFrameFormElementMask.style.display = "none";
                        }
                    }

                    grandchild = grandchild.nextSibling;
                }
            }

            child = child.nextSibling;
        }
    }
}

function SetHover__AspNetMenu() {
    var menus = document.getElementsByTagName("ul");
    for (var i = 0; i < menus.length; i++) {
        if (menus[i].className == topmostClass) {
            var items = menus[i].getElementsByTagName("li");
            for (var k = 0; k < items.length; k++) {
                items[k].onmouseover = function() { Hover__AspNetMenu(this); }
                items[k].onmouseout = function() { Unhover__AspNetMenu(this); }

                if (isPreIE7 && ((typeof (items[k].iFrameFormElementMask) == "undefined") || (items[k].iFrameFormElementMask == null))) {
                    var iFrameFormElementMask = document.createElement("IFRAME");
                    iFrameFormElementMask.scrolling = "no";
                    iFrameFormElementMask.src = "javascript:false;";
                    iFrameFormElementMask.frameBorder = 0;
                    iFrameFormElementMask.style.display = "none";
                    iFrameFormElementMask.style.position = "absolute";
                    iFrameFormElementMask.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";

                    iFrameFormElementMask.style.zIndex = -1;
                    items[k].insertBefore(iFrameFormElementMask, items[k].firstChild);
                    items[k].iFrameFormElementMask = iFrameFormElementMask;
                }
            }
        }
    }
}
