
function getParentByClassName(element, className)
{
    var parent = element;

    if (className)
    {
        while (parent && parent.className != className)
        {
            parent = parent.parentNode;
        }
    }

    return parent;
}

function getFirstChildByClassRegex(element, tagName, classRegex, stopClassRegex)
{
    var match;
    var child = element.firstChild;

    while (child != null && (!child.className || !child.className.match(stopClassRegex)))
    {
        if (child.tagName && child.tagName == tagName && child.className && child.className.match(classRegex))
        {
            match = child;
            break;
        }
        else
        {
            match = getFirstChildByClassRegex(child, tagName, classRegex, stopClassRegex);

            if (match)
            {
                break;
            }
        }

        child = child.nextSibling;
    }

    return match;
}

function getChildrenByClassRegex(root, classRegexes, tagName, matches)
{
    matches = (matches ? matches : new Array());

    if (root)
    {
        var child = root.firstChild;

        while (child)
        {
            if (!tagName || child.tagName == tagName)
            {
                for (index in classRegexes)
                {
                    if (child.className && child.className.match(classRegexes[index]))
                    {
                        matches[matches.length] = child;
                    }
                }
            }

            getChildrenByClassRegex(child, classRegexes, tagName, matches);
            child = child.nextSibling;
        }
    }

    return matches;
}

function addEventToElement(target, eventType, func, useCapture)
{
    var result = false;

    if (target.addEventListener)
    {
        target.addEventListener(eventType, func, useCapture);
        result = true;
    }
    else if (target.attachEvent)
    {
        result = target.attachEvent("on" + eventType, func);
    }
    else
    {
        alert("Handler could not be attached");
    }

    return result;
}

function addEventToElements(targets, eventType, func, useCapture)
{
    var result = true;

    for (var i = 0; i < targets.length; i++)
    {
        result &= addEventToElement(targets[i], eventType, func, useCapture);
    }

    return result;
}

function removeEventFromElement(target, eventType, func, useCapture)
{
    var result = false;

    if (target.removeEventListener)
    {
        target.removeEventListener(eventType, func, useCapture);
        result = true;
    }
    else if (target.detachEvent)
    {
        result = target.detachEvent("on" + eventType, func);
    }
    else
    {
        window.alert("Handler could not be removed");
    }

    return result;
}

function removeEventFromElements(targets, eventType, func, useCapture)
{
    var result = true;

    for (var i = 0; i < targets.length; i++)
    {
        result &= removeEventFromElement(targets[i], eventType, func, useCapture);
    }

    return result;
}

function getEvent(event)
{
    var evt = event;

    if (!evt)
    {
        evt = window.event;
    }

    return evt;
}

function getEventSource(event)
{
    var source;

    if (event && event.srcElement)
    {
        source = event.srcElement;
    }
    else if (event && event.currentTarget)
    {
        source = event.currentTarget;
    }
    else
    {
        window.alert("Event source not found!");
    }

    return source;
}

function getClickableImageEventSource(source)
{
    // If IE, the original source will be the image being clicked instead of the parent element that generated the event
    if (source && source.src)
    {
        source = source.parentNode;
    }

    return source;
}

/**
 * @deprecated
 */
var console;
if (typeof beaPortalConsole != 'undefined' && beaPortalConsole) {
	console = beaPortalConsole 
} else {
	console = new Console();
}

/**
 * @deprecated
 */
function Console()
{
    this.canvas = null;
    this.println = consolePrintln;
    this.show = consoleShow;
}

/**
 * @deprecated
 */
function consolePrintln(object)
{
    if (!this.canvas)
    {
        this.canvas = window.open("about:blank", "Console", "toolbar = no, width = 640, height = 480, directories = no, status = no, scrollbars = yes, resize = no, menubar = no");
    }

    this.canvas.document.write(object);
    this.canvas.document.write("<br/>");
}

/**
 * @deprecated
 */
function consoleShow(object)
{
    this.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

    for (property in object)
    {
        this.println(property + " = " + eval("object." + property));
    }

    this.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
/**
 * @ Rotation Banners for OS (START)
 */

function RotateBanner(bannerArrayE, bannerArrayC, delay, ancherId, imgId, OpenStrId) {
  this.bannerArrayE = bannerArrayE;
  this.bannerArrayC = bannerArrayC;
  this.delay = delay;
  this.ancherId = ancherId;
  this.imgId = imgId;
  this.OpenStrId = OpenStrId;
  this.idx = 0;

  this.nextImg = function() {
    // this anchor is defined in banner.html
    var homebannerAnchor = document.getElementById(this.ancherId);
    var homebannerImg = document.getElementById(this.imgId);
    var homebannerOpenStr = document.getElementById(this.OpenStrId);


    if (homebannerAnchor && homebannerImg) {
      var pageLang = homebannerAnchor.name;
      if (pageLang.match("Lang-E")) {
        this.idx = ++this.idx % this.bannerArrayE.length;
        homebannerAnchor.href =this.bannerArrayE[this.idx].href;
        homebannerImg.src = this.bannerArrayE[this.idx].img;
		//alert(this.bannerArrayE[this.idx].winprop);
		homebannerOpenStr.value = this.bannerArrayE[this.idx].winprop;
		
      } else {
        this.idx = ++this.idx % this.bannerArrayC.length;
        homebannerAnchor.href = this.bannerArrayC[this.idx].href;
        homebannerImg.src = this.bannerArrayC[this.idx].img;
		//alert(this.bannerArrayC[this.idx].winprop);
		homebannerOpenStr.value = this.bannerArrayC[this.idx].winprop;
      }
    }
    var bannerObj = this;
    setTimeout(function() {bannerObj.nextImg()}, this.delay);
  }
}

/**
 * @ Rotation Banners for OS (END)
 */

/* skin.js */
function initSkin()
{
    initDynamicMenus();
    initRolloverMenus();
    initPortletDeleteButtons();

    //Rotation Banners for OS (START)
    //
    // Call RotateBanner function defined in :
    // /shkco-web/framework/skins/shkonline/js/util.js
    //
    // RotateBanner(DelayTime) - to control the banner interval time
    //  
    var banner = new RotateBanner(
        [{img:'\/shkinfo\/en_US\/images\/fx_trader_pro_706x215_en.jpg', href:'http://www.shkdirect.com/product/en_US/foreign_exchange/main.html', winprop:'height=650,width=828,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no'},
	{img:'\/shkinfo\/en_US\/images\/700x215_en.gif', href:'http://www.shkdirect.com/shkco-web/portal/portlets/whatsnews/whatsnewsDetail.portlet?_nfpb=true&siteLang_=en_US&_portlet.lafUniqueId=shkonlineDefinitionLabel_1&_portlet.portalUrl=/shkco-web/portal/shkdirect.portal&_portlet.portalId=portal_shkdirect_portal&_portlet.contentType=text/html;+charset=UTF-8&id=10089&locale=en_US', winprop:'height=650,width=828,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no'},
	{img:'\/shkinfo\/en_US\/images\/FXTrader2_promo_201009_en.gif', href:'http://www.shkdirect.com/shkco-web/portal/portlets/whatsnews/whatsnewsDetail.portlet?_nfpb=true&siteLang_=en_US&_portlet.lafUniqueId=shkonlineDefinitionLabel_1&_portlet.portalUrl=/shkco-web/portal/shkdirect.portal&_portlet.portalId=portal_shkdirect_portal&_portlet.contentType=text/html;+charset=UTF-8&id=10077&locale=en_US', winprop:'height=650,width=828,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no'},
	{img:'\/shkinfo\/en_US\/images\/banner_os_v3_en_01-optimized.jpg', href:'http://www.shkf.com/index.html', winprop:'height=768,width=1024,status=yes,toolbar=yes,scrollbars=yes,menubar=no,location=no'}],
        [{img:'\/shkinfo\/zh_TW\/images\/fx_trader_pro_706x215_ch.jpg', href:'http://www.shkdirect.com/product/zh_TW/foreign_exchange/main.html', winprop:'height=650,width=828,status=yes,toolbar=yes,scrollbars=yes,menubar=no,location=no'},
	{img:'\/shkinfo\/zh_TW\/images\/700x215_ch.gif', href:'http://www.shkdirect.com/shkco-web/portal/portlets/whatsnews/whatsnewsDetail.portlet?_nfpb=true&siteLang_=zh_TW&_portlet.lafUniqueId=shkonlineDefinitionLabel_1&_portlet.portalUrl=/shkco-web/portal/shkdirect.portal&_portlet.portalId=portal_shkdirect_portal&_portlet.contentType=text/html;+charset=UTF-8&id=10089&locale=zh_TW', winprop:'height=650,width=828,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no'},
	{img:'\/shkinfo\/zh_TW\/images\/FXTrader2_promo_201009_tc.gif', href:'http://www.shkdirect.com/shkco-web/portal/portlets/whatsnews/whatsnewsDetail.portlet?_nfpb=true&siteLang_=zh_TW&_portlet.lafUniqueId=shkonlineDefinitionLabel_1&_portlet.portalUrl=/shkco-web/portal/shkdirect.portal&_portlet.portalId=portal_shkdirect_portal&_portlet.contentType=text/html;+charset=UTF-8&id=10077&locale=zh_TW', winprop:'height=650,width=828,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no'},
         {img:'\/shkinfo\/zh_TW\/images\/banner_os_v3_tc_01-optimized.jpg', href:'http://www.shkf.com/index_b5.html', winprop:'height=768,width=1024,status=yes,toolbar=yes,scrollbars=yes,menubar=no,location=no'}], 
         15000, "homebannerAnchor", "homebannerImg", "OpenStr");
    banner.nextImg();
    //
    // Call Rotation Banners for OS (END)
    // See the comments in float.js about this function...
    //initPortletFloatButtons();
}

/* delete.js */
function showDialogBoxNS(evt)
{
    var name = confirm("Are you sure you want to delete this portlet?")
    if(name == true)
    {
        var source = getEventSource(evt);
        var url = source.href;
        source.href = url+"&__deletePermanently=false";
        return true;
    }
    return false;
}

function showMultipleChoiceDialogBoxNS(evt)
{
    var name = confirm("Are you sure you want to delete this portlet?")
    var name1 = false;
    if(name == true)
    {
        var source = getEventSource(evt);
        var url = source.href;
        name1 = confirm("Press OK to delete permanently \nor Cancel to only delete from this session.\n");
        if(name1 == true)
        {
            source.href = url+"&__deletePermanently=true";
        }
        else
        {
            source.href = url+"&__deletePermanently=false";
        }
        return true;
    }
    return false;
}


function showDialogBoxIE()
{
    var name = confirm("Are you sure you want to delete this portlet?")
    if(name == true)
    {
        var source;
        var event = window.event;
        if(event != null)
        {
            source = event.srcElement;
        }
        var parent = source.parentNode;
        var url = parent.href;
        parent.href = url+"&__deletePermanently=false";
    }
    else
    {
        var event = window.event;
        if(event != null)
        {
            event.returnValue = false;
        }
    }
}

function showMultipleChoiceDialogBoxIE()
{
    var name = confirm("Are you sure you want to delete this portlet?")
    var name1 = false;
    if(name == true)
    {
        var source;
        var event = window.event;
        if(event != null)
        {
            source = event.srcElement;
        }
        var parent = source.parentNode;
        var url = parent.href;
        name1 = confirm("Press OK to delete permanently \nor Cancel to only delete from this session.\n");
        if(name1 == true)
        {
            parent.href = url+"&__deletePermanently=true";
        }
        else
        {
            parent.href = url+"&__deletePermanently=false";
        }
    }
    else
    {
        var event = window.event;
        if(event != null)
        {
            event.returnValue = false;
        }
    }
}

function detectNetscape()
{
    var n=navigator.appName
    if(n == "Netscape")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function initPortletDeleteButtons()
{
    var portletDeleteButtonsSpanned = getSpannedDeleteButtons();
    var portletDeleteButtonsNonSpanned = getNonSpannedDeleteButtons();
    if(detectNetscape() == true)
    {
        for(var i = 0; i < portletDeleteButtonsSpanned.length; i++)
        {
            portletDeleteButtonsSpanned[i].onclick = showMultipleChoiceDialogBoxNS;
        }
        for(var i = 0; i < portletDeleteButtonsNonSpanned.length; i++)
        {
            portletDeleteButtonsNonSpanned[i].onclick = showDialogBoxNS;
        }
    }
    else
    {
        for(var i = 0; i < portletDeleteButtonsSpanned.length; i++)
        {
            portletDeleteButtonsSpanned[i].onclick = showMultipleChoiceDialogBoxIE;
        }
        for(var i = 0; i < portletDeleteButtonsNonSpanned.length; i++)
        {
            portletDeleteButtonsNonSpanned[i].onclick = showDialogBoxIE;
        }
    }
}

function getSpannedDeleteButtons()
{
    var anchors = document.getElementsByTagName("a");
    var spannedDeletes = new Array(0);
    var j = -1;
    for (var i = 0; i < anchors.length; i++)
    {
        var _className = anchors[i].className;
        if((_className == "bea-portal-button-delete") &&
           (anchors[i].parentNode.tagName == "span" || anchors[i].parentNode.tagName == "SPAN"))
        {
            j++;
            spannedDeletes[j] = anchors[i];
        }
    }
    return spannedDeletes;
}

function getNonSpannedDeleteButtons()
{
    var anchors = document.getElementsByTagName("a");
    var nonSpannedDeletes = new Array(0);
    var j = -1;
    for (var i = 0; i < anchors.length; i++)
    {
        var _className = anchors[i].className;
        if((_className == "bea-portal-button-delete") &&
           (anchors[i].parentNode.tagName != "span" && anchors[i].parentNode.tagName != "SPAN"))
        {
            j++;
            nonSpannedDeletes[j] = anchors[i];
        }
    }
    return nonSpannedDeletes;
}
/* float.js */
/*
 * This file illustrates one way to customize floating popup portlets.  However, it is not enabled by default.
 * To enable this javascript functionality, uncomment the call to "initPortletFloatButtons()" in skin.js.
 */

function initPortletFloatButtons()
{
    var links = document.getElementsByTagName("a");

    for (var i = 0; i < links.length; i++)
    {
        if (links[i].className && links[i].className == "bea-portal-button-float")
        {
            initPortletFloatButton(links[i]);
        }
    }
}

function initPortletFloatButton(link)
{
    link.onclick = floatPortlet;
}

function floatPortlet(event)
{
    var href = getEventAnchorHref(getEvent(event));
    var target = getEventAnchorTarget(getEvent(event));
    // This is the interesting line for customization purposes:
    window.open(href, target, "toolbar = no, width = 320, height = 240, directories = no, status = no, scrollbars = yes, resize = yes, menubar = no");
    // Returning false keeps the browser from submitting this button click to the server.
    return false;
}

function getEventAnchorHref(event)
{
    var source = getEventSource(event);
    // Correct source for IE
    source = getClickableImageEventSource(source);
    return (source && source.href ? source.href : "about:blank");
}

function getEventAnchorTarget(event)
{
    var source = getEventSource(event);
    // Correct source for IE
    source = getClickableImageEventSource(source);
    return (source && source.target ? source.target : "_blank");
}

