﻿function EmailSubmit(url) {
    var pageurl = url + "?Title=" + document.title + "&Link=" + document.URL;
    var newwindow = window.open(pageurl, "_blank", "width=525,height=400");
    return false;
}
function clearFields(formdata) {
    if (!formdata || !formdata.elements) return;

    for (var i = 0, k = formdata.elements.length; i < k; i++) {
        if (typeof (formdata.elements[i].type) != 'string')
            continue;

        switch (formdata.elements[i].type.toLowerCase()) {
            case 'text':
            case 'textarea':
            case 'password':
                formdata.elements[i].value = '';
                break;
            case 'radio':
            case 'checkbox':
                formdata.elements[i].checked = false;
                break;
            case 'select-one':
            case 'select-multiple':
                formdata.elements[i].selectedIndex = 0;
                break;
        }
    }
}

function ActivateTab(tabindex, activeTabIndex) {
    var tabvalue = document.getElementById("HdnTabInformation").value;
    var tabs = tabvalue.split('~');
    var tabToActivate;

    for (var counter = 0; counter < tabs.length; counter++) {
        var tab = tabs[counter].split(':');
        if (tab.length == 2) {
            if (tab[0] == tabindex) {
                tabToActivate = tab[1];
                break;
            }
        }
    }

    var tabDiv = document.getElementById(tabToActivate);


    $(document).ready(function() {
        $(tabDiv).tabs('select', activeTabIndex);
    });
}



function toLeft(id) {

    document.getElementById(id).scrollLeft = 0
    return false;

}

function scrollDivLeft(id) {

    /* clearTimeout(timerRight) 
    document.getElementById(id).scrollLeft+=scrollStep
    timerRight=setTimeout("scrollDivLeft('"+id+"')",3)*/
    document.getElementById(id).scrollLeft += 90;
    return false;

}

function scrollDivRight(id) {

    /*clearTimeout(timerLeft)
    document.getElementById(id).scrollLeft-=scrollStep
    timerLeft=setTimeout("scrollDivRight('"+id+"')",3)*/

    document.getElementById(id).scrollLeft -= 90;
    return false;
}

function toRight(id) {

    document.getElementById(id).scrollLeft = document.getElementById(id).scrollWidth
    return false;
}
//You need an anonymous function to wrap around your function to avoid conflict  
(function($) {

    //Attach this new method to jQuery  
    $.fn.extend({

        //This is where you write your plugin's name  
        HideControlsIfEmpty: function(options) {

            //Iterate over the current set of matched elements  
            return this.each(function() {

                switch (options) {
                    case "spControl-inner":
                        $(this).each(function(index) {
                            var content = $(this).html().replace(/&nbsp;/gi, '');
                            content = jQuery.trim(content);

                            if (content.length == 0) {
                                //alert(index);
                                var parentTag = $(this).parent();
                                if (window.XMLHttpRequest) {
                                    if (jQuery.browser.version == '7.0') {
                                        if (parentTag[0].nodeName.toLowerCase() == "div") {
                                            $(this).parent().attr("style", "display:none");

                                        }
                                    }
                                    else if (parentTag[0].localName.toLowerCase() == "div") {
                                        $(this).parent().attr("style", "display:none");
                                    }

                                }
                                else if (window.ActiveXObject) {
                                    if (parentTag[0].nodeName.toLowerCase() == "div") {
                                        $(this).parent().attr("style", "display:none");
                                    }

                                }
                                $(this).attr("style", "display:none");

                            }
                            content = "";

                        });

                        break;
                    case "slm-layout-main":
                        $(this).each(function(index) {

                            //alert(index + ': ' + $(this).text());
                            var content = $(this).html();
                            content = jQuery.trim(content);
                            if (content.length == 0) {
                                $(this).parent().attr("style", "display:none");
                                //var parentTag = $(this).offsetParent().attr("style","display:none");
                                $(this).parent().parent().attr("style", "display:none");
                                $(this).parent().parent().parent().attr("style", "display:none");
                                $(this).parent().parent().parent().parent().parent().attr("style", "display:none");



                            }
                            content = "";

                        });

                        break;
                    case "_ControlWrapper_RichHtmlField":
                        //debugger;
                        $(this).each(function(index) {

                            //alert(index + ': ' + $(this).text());
                            var content = $(this).html();
                            content = jQuery.trim(content);
                            if (content.length == 0) {
                                $(this).parent().attr("style", "display:none");
                                //var parentTag = $(this).offsetParent().attr("style","display:none");
                                $(this).parent().parent().attr("style", "display:none");
                                //$(this).parent().parent().parent().attr("style","display:none");


                            }
                        });

                        break;
                    case "_ControlWrapper_RichImageField":
                        //alert(index + ': ' + $(this).text());
                        $(this).each(function(index) {

                            var content = $(this).html();
                            content = jQuery.trim(content);
                            if (content.length == 0) {
                                $(this).parent().attr("style", "display:none");
                                //var parentTag = $(this).offsetParent().attr("style","display:none");
                                $(this).parent().parent().attr("style", "display:none");
                                $(this).parent().parent().parent().attr("style", "display:none");


                            }
                            content = "";

                        });


                        break;
                }
                //code to be inserted here  
            });
        }
    });

    //pass jQuery to the function,   
    //So that we will able to use any valid Javascript variable name   
    //to replace "$" SIGN. But, we'll stick to $)         
})(jQuery);

//Function to Change the Home text in Breadcrumb to Chinese and Thai on Page Load
function ChangeBreadCrumbText() {

    var pathname = window.location.href;
    if (pathname.toLowerCase().indexOf("th-ap") > 0) {
        $('.RootBreadCrumb').html("Home");
    }
    else if (pathname.toLowerCase().indexOf("zh-ap") > 0) {
        $('.RootBreadCrumb').html("首页");
    }

}

/*This functions hides the tooltip present for left navigation*/
function HideToolTip() {

    $(document).ready(function() {
        // All links in main menu div
        var menu_links = $('table[id=zz1_QuickLaunchMenu] tr');
        // On mouse hover
        menu_links.hover(
        // In: Store and remove title
            function() {
                old_title = $(this).attr('title');
                $(this).attr('title', '');
            },
        // Out: Replace title
            function() {
                $(this).attr('title', old_title);
            }
        );
    });

}

function HideIfEmpty() {
    /*$("div[id='header']").css({ 'z-index': '50' });
    $("div[id='bodycontainer']").css({ 'z-index': '49' });*/
    ChangeBreadCrumbText();
    HideToolTip();
    SetSearchValue();
    $("table.slm-layout-main tr:first-child").each(function(index) {

        //alert(index + ': ' + $(this).text());
        var content = $(this).html();
        content = jQuery.trim(content);
        if (content.length == 0) {
            $(this).parent().attr("style", "display:none");
            //var parentTag = $(this).offsetParent().attr("style","display:none");
            $(this).parent().parent().attr("style", "display:none");
            $(this).parent().parent().parent().attr("style", "display:none");
            $(this).parent().parent().parent().parent().parent().attr("style", "display:none");



        }
    });
    $("div[id$='_ControlWrapper_RichImageField'] span:first-child").each(function(index) {

        //alert(index + ': ' + $(this).text());
        var content = $(this).html();
        content = jQuery.trim(content);
        if (content.length == 0) {
            $(this).parent().attr("style", "display:none");
            //var parentTag = $(this).offsetParent().attr("style","display:none");
            $(this).parent().parent().attr("style", "display:none");
            $(this).parent().parent().parent().attr("style", "display:none");


        }
    });
    $("div[id$='_ControlWrapper_RichHtmlField']").each(function(index) {

        //alert(index + ': ' + $(this).text());
        var content = $(this).html();
        content = jQuery.trim(content);
        if (content.length == 0) {
            $(this).parent().attr("style", "display:none");
            //var parentTag = $(this).offsetParent().attr("style","display:none");
            $(this).parent().parent().attr("style", "display:none");
            //$(this).parent().parent().parent().attr("style","display:none");


        }
    });
    $("div.spControl-inner").each(function(index) {

        //alert(index + ': ' + $(this).text());
        var content = $(this).html().replace(/&nbsp;/gi, '');

        content = jQuery.trim(content);
        if (content.length == 0) {
            $(this).parent().attr("style", "display:none");
            //var parentTag = $(this).offsetParent().attr("style","display:none");
            //$(this).parent().parent().attr("style","display:none");
            //$(this).parent().parent().parent().attr("style","display:none");


        }
    });

    var ECLColumn3num = $("td[id='ECLColumn3'] div > div.spControl").children().size();
    var ECLColumn3status = 0;
    $("td[id='ECLColumn3'] div > div.spControl").each(function(index) {

        if ($(this).css('display') == 'none') {
            ECLColumn3status = ECLColumn3status + 1;
        }
        if (ECLColumn3status == ECLColumn3num) {
            $("td[id='ECLColumn3']").attr("style", "display:none");
            ECLColumn3status = 0;
        }
    });
    var ECLColumn2num = $("td[id='ECLColumn2'] div.spControl").children().size();
    var ECLColumn2status = 0;
    $("td[id='ECLColumn2'] div.spControl").each(function(index) {

        if ($(this).css('display') == 'none') {
            ECLColumn2status = ECLColumn2status + 1;
        }
        if (ECLColumn2status == ECLColumn2num) {
            $("td[id='ECLColumn2']").attr("style", "border-left:none !important");
            ECLColumn2status = 0;
        }
    });

}

function PrintEvents() {
    var mywindow;
    var eventitems
    $("div[id$='_divItemDetails']").each(function(index) {
        eventitems = $(this).html();
        eventitems = jQuery.trim(eventitems);

        if (eventitems.length == 0) {
            return false;
        }
        else {
            mywindow = window.open('', '', 'width=700,height=300');
            mywindow.document.write('<html><head><title>Events in Calendar</title>');
            mywindow.document.write('<link rel="stylesheet" href="/Asia/Style Library/Emerson_styles.css" type="text/css" />');
            mywindow.document.write('<link rel="stylesheet" href="/asia/Style%20Library/Emerson_Calendar.css" type="text/css" />');
            mywindow.document.write('</head><body >');
            mywindow.document.write(eventitems);
            mywindow.document.write('</body></html>');
            mywindow.print();
            mywindow.document.close();
            mywindow.close();

            return true;
        }
    });


}
/*Java script for setting the OOTB search box text value to keyword*/
function fnGetQval(name) {
    var m, Q = window.location.search.substring(1);

    if ('' != Q) {
        var re = new RegExp(escape(name) + '=([^&$]+)');

        if (m = Q.match(re)) {
            return m[1];
        }
        else {
            return '';
        }
    }

    return '';
}

function fnTrim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}

function SetSearchValue() {
    ///This function sets the text box value
    var queryString = fnGetQval('k');
    while (queryString.indexOf('+') > 0) {
        queryString = queryString.replace('+', ' ');
    }
    queryString = unescape(queryString);
    var endIndex = queryString.toLowerCase().indexOf('emersoncontent');

    if (endIndex < 0) {
        endIndex = queryString.toLowerCase().indexOf('content');
    }

    if (endIndex > 0) {
        queryString = queryString.substring(0, endIndex);
    }
    queryString = unescape(queryString);
    queryString = fnTrim(queryString);
    $("input[id$='_InputKeywords']").val(queryString);
    $("input[id$='_ASB_TQS_AndQ_tb']").val(queryString);
}

function CenterFlyout(Flashwidth, Flashheight, isFlashFile) {
    var documentWidth = $(window).width();
    var htmlWidth = $(document).width();
    var documentHeight = window.document.height;

    if ($("div[id$='_divMainID']") != null) {
        var FlyoutDivWidth = $("div[id$='_divMainID']").width();
    }
    else {
        return;
    }

    if (isFlashFile == "true") {

        $("div[id$='_divMainID']").css({ 'left': '' + (documentWidth / 2) - (Flashwidth / 2) + 'px', 'width': '' + Flashwidth + 'px', 'height': '' + Flashheight + 'px' });

        return;
    }
    else if ($("#imgPopup") != null) {
        var FlyImageWidth = $("#imgPopup").width();

    }
    else {
        return;
    }

    if (FlyImageWidth >= FlyoutDivWidth) {
        $("div[id$='_divMainID']").css({ 'left': '' + (documentWidth - FlyImageWidth) / 2 + 'px', 'width': '' + FlyImageWidth + 'px' });
    }
    else if (FlyoutDivWidth < (documentWidth / 2)) {
        $("div[id$='_divMainID']").css({ 'left': '' + (documentWidth / 2) - (FlyoutDivWidth / 2) + 'px' });
    }
    else {
        $("div[id$='_divMainID']").css({ 'left': '' + (documentWidth / 2) - (FlyoutDivWidth / 2) + 'px' });

    }


}
function AddZindex() {
    /*$("div[id='header']").css({ 'z-index': '49' });
    $("div[id='bodycontainer']").css({ 'z-index': '50' });*/
}
function RemoveZindex() {
    /*$("div[id='header']").css({ 'z-index': '50' });
    $("div[id='bodycontainer']").css({ 'z-index': '49' });*/
}
