//*****************************************************************************//
//  Javascript library
//  Author: Michael Turnwall, Gateway, Inc
//  Created: 09.30.2005
//  Description: various js functions for use on the site
//  Copyright 2005 Gateway, Inc. All rights reserved.
//*****************************************************************************//

var rand1 = 0;
var useRand = 0;
var promoNum = null;
var corpBannerNum = null;
var promoContainer = "";
var prodNum = null;
var prodContainer = "";


// swaps promos using a HTML array defined on the page itself

function promoSwap(elementID, num)
{
    promoContainer = document.getElementById(elementID);
    
    randNum = Math.floor(Math.random() * num + 1);
    promoContainer.innerHTML = promoHTML[randNum];
}

// renders the first current promotion image
// elementID = div container ID
// num = number of array elements or which element to show first
// randTrue = whether or not to randomize the first promo 0 = no random, 1 = random

function showPromo(elementID, num, randTrue)
{
    promoContainer = document.getElementById(elementID);
    randNum = Math.floor(Math.random() * num + 1);  // generate random number
    
    if(randTrue)
    {
        promoContainer.innerHTML = promoHTML[randNum];
    }
    else
    {
        promoContainer.innerHTML = promoHTML[num];  
    }
}


// allows the user to cycle through the current promotions box

function swapPromo(elementID, direction)
{
    promoContainer = document.getElementById(elementID);
    if(promoNum == null)
        promoNum = randNum;
    
    if(direction == "back")
    {
        promoNum--;
        if(promoNum < 1)
            promoNum = 3;
    }
    else
    {
        promoNum++;
        if(promoNum > 3)
            promoNum = 1;
    }
    promoContainer.innerHTML = promoHTML[promoNum];
}

// allows the user to cycle through the current banners box

function swapCorpBanner(elementID, direction)
{
    corpBannerContainer = document.getElementById(elementID);
    var num = 2;
    randNum = Math.floor(Math.random() * num + 1);  // generate random number
    
    if(corpBannerNum == null)
        corpBannerNum = randNum;
    
    if(direction == "back")
    {
        corpBannerNum--;
        if(corpBannerNum < 0)
            corpBannerNum = 2;
        corpBannerContainer.innerHTML = corpBannerHTML[arrayPrimaryBanner[corpBannerNum]];
    } else {
        corpBannerNum++;
        if(corpBannerNum > 2)
            corpBannerNum = 0;
        corpBannerContainer.innerHTML = corpBannerHTML[arrayPrimaryBanner[corpBannerNum]];
    }
}

// randomizes two sets of promotions on the home and sb pages
// elementID = div container ID
// num = number of array elements or which element to show first
// randTrue = whether or not to randomize the first promo 0 = no random, 1 = random
// box = sequential number of promotional container

function showSegmentPromo(elementID, num, randTrue, box)
    {
        promoContainer = document.getElementById(elementID);

    if(randTrue)
    {
        randNum = Math.floor(Math.random() * num + 1);  // generate random number
        switch(box) {
            case 1:
                promoContainer.innerHTML = promoHTML1[randNum];
            break;
            case 2:
                promoContainer.innerHTML = promoHTML2[randNum];
            break;
            case 3:
                promoContainer.innerHTML = promoHTML3[randNum];
            break;
            case 4:
                promoContainer.innerHTML = promoHTML4[randNum];
            break;
            case 5:
                promoContainer.innerHTML = promoHTML5[randNum];
            break;                      
            case 6:
                promoContainer.innerHTML = promoHTML6[randNum];
            break;                      
            default:
            
            break;
        }
    }
    else
    {
        switch(box) {
            case 1:
                promoContainer.innerHTML = promoHTML1[num]; 
            break;
            case 2:
                promoContainer.innerHTML = promoHTML2[num]; 
            break;
            case 3:
                promoContainer.innerHTML = promoHTML3[num]; 
            break;      
            case 4:
                promoContainer.innerHTML = promoHTML4[num]; 
            break;
            case 5:
                promoContainer.innerHTML = promoHTML5[num]; 
            break;          
            case 6:
                promoContainer.innerHTML = promoHTML6[num]; 
            break;          
            default:
            
            break;
        }
    }
}

// swaps products using a HTML array defined on the page itself

function prodSwap(elementID, num)
{
    prodContainer = document.getElementById(elementID);
    
    randNum = Math.floor(Math.random() * num + 1);
    prodContainer.innerHTML = prodHTML[randNum];
}

// renders the first current promotion image
// elementID = div container ID
// num = number of array elements or which element to show first
// randTrue = whether or not to randomize the first promo 0 = no random, 1 = random

function showProd(elementID, num, randTrue)
{
    prodContainer = document.getElementById(elementID);
    randNum = Math.floor(Math.random() * num + 1);  // generate random number
    
    if(randTrue)
    {
        prodContainer.innerHTML = prodHTML[randNum];
    }
    else
    {
        prodContainer.innerHTML = prodHTML[num];    
    }
}


// allows the user to cycle through the current promotions box

function swapProd(elementID, direction)
{
    prodContainer = document.getElementById(elementID);
    if(prodNum == null)
        prodNum = randNum;
    
    if(direction == "back")
    {
        prodNum--;
        if(prodNum < 1)
            prodNum = 3;
    }
    else
    {
        prodNum++;
        if(prodNum > 3)
            prodNum = 1;
    }
    prodContainer.innerHTML = prodHTML[prodNum];
}

// randomizes two sets of promotions on the home and sb pages
// elementID = div container ID
// num = number of array elements or which element to show first
// randTrue = whether or not to randomize the first promo 0 = no random, 1 = random
// box = sequential number of promotional container

function showSegmentProd(elementID, num, randTrue, box)
    {
        prodContainer = document.getElementById(elementID);

    if(randTrue)
    {
        randNum = Math.floor(Math.random() * num + 1);  // generate random number
        switch(box) {
            case 1:
                prodContainer.innerHTML = prodHTML1[randNum];
            break;
            case 2:
                prodContainer.innerHTML = prodHTML2[randNum];
            break;
            case 3:
                prodContainer.innerHTML = prodHTML3[randNum];
            break;
            case 4:
                prodContainer.innerHTML = prodHTML4[randNum];
            break;
            case 5:
                prodContainer.innerHTML = prodHTML5[randNum];
            break;                      
            default:
            
            break;
        }
    }
    else
    {
        switch(box) {
            case 1:
                prodContainer.innerHTML = prodHTML1[num];   
            break;
            case 2:
                prodContainer.innerHTML = prodHTML2[num];   
            break;
            case 3:
                prodContainer.innerHTML = prodHTML3[num];   
            break;      
            case 4:
                prodContainer.innerHTML = prodHTML4[num];   
            break;
            case 5:
                prodContainer.innerHTML = prodHTML5[num];   
            break;          
            default:
            
            break;
        }
    }
}


// function that hides or shows an element

function showHide(id)
{
    // check if browser is DOM compliant
    if (!document.getElementById)
    document.getElementById = function() { return null; }
    
    if (id == null || id == "") return;
    
    var tag = document.getElementById(id);
    var state = tag.style.display;
    tag.style.display = (state == "block") ? "none" : "block";
}

// function that hides or shows an element. Also closes a previous opened element

var oldElement = " ";
function showHideSwitch(id)
{
    // check if browser is DOM compliant
    if (!document.getElementById)
    document.getElementById = function() { return null; }
    
    var selection = id;
    
    var tag = document.getElementById(selection);
    var state = tag.style.display;
    if(oldElement != " ")
    {
        var oldTag = document.getElementById(oldElement);
        oldTag.style.display = "none";
        tag.style.display = "block";
    }
    else
    {
        tag.style.display = "block";
    }   
    
    oldElement = selection;
}

// adds stripes to tables

function stripeOdd(id)
{
    var table = document.getElementById(id);
    if (! table) { return; }
    
    var trs = table.getElementsByTagName("tr");
    
    for (var i = 1; i < trs.length; i += 2)
    {
      trs[i].className += " odd";
    }
}

function stripeEven(id)
{
    var table = document.getElementById(id);
    if (! table) { return; }
    
    var trs = table.getElementsByTagName("tr");
    
    for (var i = 2; i < trs.length; i += 2)
    {
      trs[i].className += " odd";
    }
}

// javascript that opens windows 

function openWin(url, width, height, opt1, opt2)
{
    var win;
    var windowName;
    var params;
    windowName="btns";
    params="toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars="+opt1+",resizable="+opt2+",top=50,left=50,width="+width+",height="+height;
    win=window.open(url, windowName, params);
}

// creates dynamic links

function dynLink(a,tags)
{
    a.href += ((a.href.indexOf('?')>0)?"&":"?") + tags;
}

// legacy code - creates dynamic links

function analink(a,tags)
{
  a.href += ((a.href.indexOf('?')>0)?"&":"?") + tags;
}

// used for the country select form on the corporate homepage

function openURL() {    
    selInd = document.chooseCountry.language.selectedIndex; 
    goURL = document.chooseCountry.language.options[selInd].value;  
    top.location.href = goURL;  
}

function flashWrite(id,path,width,height,bgcolor,name)
{
    var container = document.getElementById(id);
    var containerHTML = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"" + width + "\" height=\"" + height + "\" id=\"" + name + "\" align=\"middle\">";
    containerHTML += "<param name=\"allowScriptAccess\" value=\"sameDomain\">";
    containerHTML += "<param name=\"movie\" value=\"" + path + "\">";
    containerHTML += "<param name=\"quality\" value=\"high\">";
    containerHTML += "<param name=\"scale\" value=\"exactfit\">";
    containerHTML += "<param name=\"bgcolor\" value=\"" + bgcolor + "\">";
    containerHTML += "<embed src=\"" + path + "\" quality=\"high\" scale=\"exactfit\" bgcolor=\"" + bgcolor + "\" width=\"" + width + "\" height=\"" + height + "\" id=\"" + name + "\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed>";
    containerHTML += "</object>";
    container.innerHTML = containerHTML;
    //alert("hello there");
    flashInstalled = 1; 
}

// flash global versions
// -----------------------------------------------------------------------------
// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;
// the version of javascript supported
var jsVersion = 1.0;
// -----------------------------------------------------------------------------

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
jsVersion = 1.1;

// JavaScript helper required to detect Flash Player PlugIn version information

function JSGetSwfVer(i){
    // NS/Opera version >= 3 check for Flash plugin in plugin array
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
            descArray = flashDescription.split(" ");
            tempArrayMajor = descArray[2].split(".");
            versionMajor = tempArrayMajor[0];
            versionMinor = tempArrayMajor[1];
            if ( descArray[3] != "" ) {
                tempArrayMinor = descArray[3].split("r");
            } else {
                tempArrayMinor = descArray[4].split("r");
            }
            versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
        } else {
            flashVer = -1;
        }
    }
    // MSN/WebTV 2.6 supports Flash 4
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
    // WebTV 2.5 supports Flash 3
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
    // older WebTV supports Flash 2
    else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
    // Can't detect in all other cases
    else {
        
        flashVer = -1;
    }
    return flashVer;
}

// If called with no parameters this function returns a floating point value 
// which should be the version of the Flash Player or 0.0 
// ex: Flash Player 7r14 returns 7.14
// If called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available

function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) 
{
    reqVer = parseFloat(reqMajorVer + "." + reqRevision);
    // loop backwards through the versions until we find the newest version 
    for (i=25;i>0;i--) {    
        if (isIE && isWin && !isOpera) {
            versionStr = VBGetSwfVer(i);
        } else {
            versionStr = JSGetSwfVer(i);        
        }
        if (versionStr == -1 ) { 
            return false;
        } else if (versionStr != 0) {
            if(isIE && isWin && !isOpera) {
                tempArray         = versionStr.split(" ");
                tempString        = tempArray[1];
                versionArray      = tempString .split(",");             
            } else {
                versionArray      = versionStr.split(".");
            }
            versionMajor      = versionArray[0];
            versionMinor      = versionArray[1];
            versionRevision   = versionArray[2];
            
            versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
            versionNum        = parseFloat(versionString);
            // is the major.revision >= requested major.revision AND the minor version >= requested minor
            if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
                return true;
            } else {
                return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
            }
        }
    }   
    return (reqVer ? false : 0.0);
}

function drawButton(btnType,url,onClickURL,onKeyPressURL,title,btnText) {
    /* function to draw HTML buttons
        1 - button type - submit, cancel, add, update, detail, back, edit
        2 - URL
        3 - onClick URL
        4 - onKeyPress URL
        5 - title
        6 - button Text
    */
        var button = "<div class=\"btn_" + btnType + "\">";
        button += "<span class=\"btnSTRT\">&nbsp;</span>";
        button += "<span class=\"btnTXT\">";
        button += "<a href=\"" + url + "\" onclick=\"" + onClickURL + "\" onkeypress=\"" + onKeyPressURL + "\" title=\"" + title + "\">" + btnText + "</a>";
        button += "</span><span class=\"btnEND\">&nbsp;</span></div>";
        document.write(button);
    }


