var AgntUsr = navigator.userAgent.toLowerCase();
var DomYes = document.getElementById ? 1 : 0;
var NavYes = AgntUsr.indexOf('mozilla') != -1 && AgntUsr.indexOf('compatible') == -1 ? 1 : 0;
var ExpYes = AgntUsr.indexOf('msie') != -1 ? 1 : 0;
var OprYes = AgntUsr.indexOf('opera') != -1 ? 1 : 0;
var Opr6OrLess = window.opera && navigator.userAgent.search(/opera.[1-6]/i) != -1; 
var DomNav = DomYes && NavYes ? 1 : 0;
var DomExp = DomYes && ExpYes ? 1 : 0;
var Nav4 = NavYes && !DomYes && document.layers ? 1 : 0;
var Exp4 = ExpYes && !DomYes && document.all ? 1 : 0;
var Exp7 = (ExpYes && window.XMLHttpRequest);
var Linux = AgntUsr.indexOf('linux') != -1 || AgntUsr.indexOf('x11') != -1 ? 1 : 0;

function printSection(link, figureSection, asPDF) {

  // look for section heading
  var section = null;
  var content = null;
  if (!figureSection) {
      section = link.parentNode.parentNode.parentNode;
      do section = section.previousSibling;
      while (section && section.nodeType != 1 && section.tagName != "H2");
      section = section.getElementsByTagName("span")[0].innerHTML;
      content = link.parentNode.parentNode.parentNode.innerHTML; 
  } else {
      section = figureSection;
      content =link.parentNode.parentNode.parentNode.innerHTML; 
  }

  if (typeof(title) == "undefined") {
      title = "Print";
  }

  document.getElementById("section_print_title").value = title;
  document.getElementById("section_print_section").value = section;
  document.getElementById("section_print_content").value = content;
  document.getElementById("section_print_asPdf").value = (!asPDF ? "0" : "1");
  document.forms["section_print_form"].submit();
  return false;
}

function initFloat(name, offTop, marginTop) {
    clearInterval();
    if (ExpYes && !Exp7) {
        floatElem = document.getElementById(name);
        floatElemOffsetTop = offTop;
        floatElementMarginTop = 0;
        if (typeof(marginTop) != "undefined") {
            floatElementMarginTop = marginTop;
        }
        bodyIE = 
            (document.compatMode && document.compatMode != "BackCompat")
            ? document.documentElement 
            : document.body
        setInterval("positionFloat()", 100);
    }
}

function positionFloat(){
    
    // only run this for exploder<7, use css for other browsers
    if (ExpYes) {
        var dsoctop =  bodyIE.scrollTop;
        var bodySize = getPageBodySize();
        var bodyHeight = bodySize[1];
        if (dsoctop < floatElemOffsetTop) {
            floatElem.style.top = floatElementMarginTop + "px";
        } else {
            floatElem.style.top = dsoctop - floatElemOffsetTop + floatElementMarginTop + "px";
        }
        dsoctop = null;
        bodySize = null;
        bodyHeight = null;
    }
}

function getPageBodySize() {
    var myWidth = 0;
    var myHeight = 0;

    if (typeof(window.innerWidth) == "number") {
      
        // Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;

    } else if (document.documentElement 
            && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {

        // IE 6+ in "standards compliant mode"
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;

    } else if (document.body 
            && (document.body.clientWidth || document.body.clientHeight)) {

        // IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    return new Array(myWidth, myHeight);
}


function adjustDGFrame(name, parentID) {
    window.frames[name].adjustDGFrameHeight();
//    newWidth = document.getElementById(parentID+"link").offsetWidth;
    newWidth = 768;
    document.getElementById(name).width = newWidth;
    document.getElementById(name).style.width = newWidth+"px";
}

function adjustDGFramesOnResize(toResize) {
    for (i = 0; i < toResize.length; i++) {
        adjustDGFrame(toResize[i][0], toResize[i][1]);
    }
}

foldImg = new Image();
foldImg.src = "/assets/images/icon_fold.gif";

unfoldImg = new Image();
unfoldImg.src = "/assets/images/icon_unfold.gif";
    
function unfoldKeywordsMatches() {
    if (!DomYes) {
        return;
    }

    firstDiv = null; // the first div that contains keywords highlights
    divs = document.getElementsByTagName("div"); // all divs
    
    divs_loop:
    for (i = 0; i < divs.length; i++) {   // iterate over all divs
        div = divs[i];
        if (div.className == "nofold" || div.className == "unfolded") { // identify a section div 
            spans = div.getElementsByTagName("span"); // spans of the section div
            for (j = 0; j < spans.length; j++) { // iterate over spans 
                span = spans[j];
                if (span.className == "highlight") {  // span with a keyword highlight
                    if (firstDiv == null) {  // remember the first matching div 
                        firstDiv = div;
                    }
                    if (div.className == "unfolded") {
                        doFolding(div.id, true);
                    }
                    break;
                }
            }
        }
    }

    if (firstDiv != null) {
//      location.hash = "#"+firstDiv.id+"anchor"; // navigate to the first div with KH
//        navigateOnPage(firstDiv.id);
    }
}  

function doFolding(name, noNavigate) {
    section = null;
    arrow = null;
    isUnfolding = false;
    
    if (!document.getElementById(name)) {
        return;
    }

    section = document.getElementById(name);
    link = document.getElementById(name+"link");
    arrow = link.getElementsByTagName("span");
    arrow = arrow[0];
    
    if (section == null) {
        return; // (???)
    }
    if (link == null) {
        link = new Object();
    }
    if (section.style.display == "none" || section.style.display == "") {
        section.style.display = "block";
        if (link.className == "sublista") {
            link.className = "sublistaUnfolded";
        } else if (link.className == "sublista2") {
        } else if (link.className == "emptya") {
        } else {
            link.className = "linkActive";
        }
        if (arrow != null) {
            arrow.className = "textActive"
            arrow.title = "Collapse";
        }
        isUnfolding = true;
    } else {
        section.style.display = "none";
        if (link.className == "sublistaUnfolded") {
            link.className = "sublista";
        } else if (link.className == "sublista2") {
        } else if (link.className == "emptya") {
        } else {
            link.className = "";
        }
        if (arrow != null) {
            arrow.className = "text"
            arrow.title = "Expand";
        }
        isUnfolding = false;
    }

    // navigate to the unfolded folder
    if (isUnfolding && !noNavigate && (name == "majorcontractslisting" || name == "mergersacquisitions")) {
//        navigateOnPage(name);
    }

    // activate floating navigation item
    if (document.getElementById("subm-"+name)) {
        if (isUnfolding) {
            document.getElementById("subm-"+name).className = "sub-active";
        } else {
            document.getElementById("subm-"+name).className = "";
        }
    }

    section = null;
    link = null;
    arrow = null
}


function doFoldingRegCount(regionID, section) {
    section1 = section2 = section3 = null;
    d1 = d2 = d3 = "none";
    
    section1 = document.getElementById("Regions"+regionID);
    section2 = document.getElementById("Regions"+regionID+"RelC");
    section3 = document.getElementById("Regions"+regionID+"OtherC");
    if (section == "descr" && section1 && section1.style.display != "block") {
        d1 = "block";
    } 
    if (section == "RelC" && section2 && section2.style.display != "block") {
        d2 = "block";
    } 
    if (section == "OtherC" && section3 && section3.style.display != "block") {
        d3 = "block";
    }
    if (section1) section1.style.display = d1;
    if (section2) section2.style.display = d2;
    if (section3) section3.style.display = d3;
    
    section1 = section2 = section3 = null;
}


function navigateOnPage(name) {

     // mozilla browsers work well with location.replace()
    if (NavYes && Nav4) { // bypassed at the moment, see the next else if
//        location.replace("#"+name+"anchor");

    // internet exploder needs to scrollIntoView() in this case (works with mozilla as well)
    } else if (NavYes || ExpYes) { 
        links = document.anchors;
        for (i = 0; i < links.length; i++) {
            if (links[i].name == name+"anchor") {
                links[i].scrollIntoView(true);
                links[i].focus();
            }
        }

    // other browsers simply follow the hash
    } else {
        location.hash = name+"anchor";
    } 
}

function showHideSearchForm() {
    sf = null;
    link = null;
    qSearch = null;
  
    if (DomYes) {
        sf = document.getElementById("searchFilter");
        link = document.getElementById("searchFilterLink");
        qSearch = document.getElementById("quicksearch");
    } else if (Nav4) {
        sf = document.layers["searchFilter"];
        link = document.layers["searchFilterLink"];
        qSearch = document.layers["quicksearch"];
    } else if (Exp4) {
        sf = document.all["searchFilter"];
        link = document.all["searchFilterLink"];
        qSearch = document.all["quicksearch"];
    }         

    if (sf == null) {
        return; // (???)
    }

    if (sf.style.display == "none" || sf.style.display == "") {
        sf.style.display = "block";
        if (qSearch) qSearch.style.display = "none";
        link.innerHTML = msgHide;
        document.forms["searchFilterForm"].name.focus();
        document.forms["searchFilterForm"].searchFilterShown.value = "true";
    } else {
        sf.style.display = "none";
        if (qSearch) qSearch.style.display = "block";
        document.forms["searchFilterForm"].searchFilterShown.value = "false";
        link.innerHTML =  msgShow;
    }
}


/**
 * Changes class for given element not working needs to looked into
 * tried to make it work with this call xtClass(this,'clasxyz')
 * intention was to change class for current element
 */
function xtClass(obj, xtClassname) { //v1.0 by XTmotion
    obj.style.className=xtClassname
}

///////////////////////////MD5 SCRIPT BEGIN///////////////////////////////
/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Copyright (C) Paul Johnston 1999 - 2000.
 * Updated by Greg Holt 2000 - 2001.
 * See http://pajhome.org.uk/site/legal.html for details.
 */

/*
 * Convert a 32-bit number to a hex string with ls-byte first
 */
var hex_chr = "0123456789abcdef";
function rhex(num)
{
  var str = "";
  for(var j = 0; j <= 3; j++)
    str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
           hex_chr.charAt((num >> (j * 8)) & 0x0F);
  return str;
}

/*
 * Convert a string to a sequence of 16-word blocks, stored as an array.
 * Append padding bits and the length, as described in the MD5 standard.
 */
function str2blks_MD5(str)
{
  var i;
  var nblk = ((str.length + 8) >> 6) + 1;
  var blks = new Array(nblk * 16);
  for(i = 0; i < nblk * 16; i++) blks[i] = 0;
  for(i = 0; i < str.length; i++)
    blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
  blks[i >> 2] |= 0x80 << ((i % 4) * 8);
  blks[nblk * 16 - 2] = str.length * 8;
  return blks;
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally 
 * to work around bugs in some JS interpreters.
 */
function add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left
 */
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * These functions implement the basic operation for each round of the
 * algorithm.
 */
function cmn(q, a, b, x, s, t)
{
  return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t)
{
  return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t)
{
  return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t)
{
  return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t)
{
  return cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Take a string and return the hex representation of its MD5.
 */
function calcMD5(str)
{
  var x = str2blks_MD5(str);
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = ff(c, d, a, b, x[i+10], 17, -42063);
    b = ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = ff(d, a, b, c, x[i+13], 12, -40341101);
    c = ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = ff(b, c, d, a, x[i+15], 22,  1236535329);    

    a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = gg(c, d, a, b, x[i+11], 14,  643717713);
    b = gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = gg(c, d, a, b, x[i+15], 14, -660478335);
    b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = gg(b, c, d, a, x[i+12], 20, -1926607734);
    
    a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = hh(b, c, d, a, x[i+14], 23, -35309556);
    a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = hh(d, a, b, c, x[i+12], 11, -421815835);
    c = hh(c, d, a, b, x[i+15], 16,  530742520);
    b = hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = ii(c, d, a, b, x[i+10], 15, -1051523);
    b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = ii(d, a, b, c, x[i+15], 10, -30611744);
    c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = add(a, olda);
    b = add(b, oldb);
    c = add(c, oldc);
    d = add(d, oldd);
  }
  return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}
///////////////////////////MD5 SCRIPT END/////////////////////////////////

