//////////////////////////////////////////////////////////////////////////
//  Program: jscommon.js
//  Purpose: Shared JavaScript Function Library 
//   Author: Robert W, Murray, robert.murray@gateway.com (and past others)
//
//  Common functions shared by both Gateway and eMachines websites.
//  (Not really shared on eMachines US, but created for easier porting
//   to the internation sites when the time comes)
//
//////////////////////////////////////////////////////////////////////////

var sitecode;

//
//  Browser detection
//
var ie = document.all    ? 1 : 0;
var ns = document.layers ? 1 : 0;
var ie4, ie5;
var browname = navigator.appName;
var ver = navigator.appVersion;
var int_ver = parseInt(ver);

if (browname.indexOf("Netscape") >= 0 && int_ver == 4) ns = 1;
if (browname.indexOf("Microsoft Internet Explorer") >=0 && int_ver >= 4) ie = 1;
if (navigator.userAgent.indexOf('MSIE 4') > 0) ie4 = 1;
if (navigator.userAgent.indexOf('MSIE 5') > 0) ie5 = 1;


//////////////////////////////////////////////////////////////////////////
// Fully definable pop-up windows [backwards compatible with openWin]
//
function oWindow(Uri, Wth, Hgt, Resize, Scroll, Name, Left, Top, Params)
{
    Uri = Uri || "/";
    Name = Name || "buttons";
    if (!Params) {
        Wth = (Wth == parseInt(Wth))? Wth : 500;
        Hgt = (Hgt == parseInt(Hgt))? Hgt : 500;
        var Center = oCenter(Wth, Hgt);
        Resize = (Resize == parseInt(Resize))? Resize : 0;
        Scroll = (Scroll == parseInt(Scroll))? Scroll : 1;
        Left = (Left == "center")? Center[0] : (Left == parseInt(Left))? Left : 50;
        Top = (Top == "center")? Center[1] : (Top == parseInt(Top))? Top : 50;
        Params = "toolbar=0,location=0,directories=0,status=0,menubar=0" +
         ",resizable=" + Resize + ",scrollbars=" + Scroll +
         ",width=" + Wth + ",height=" + Hgt + ",left=" + Left + ",top=" + Top;
    }
    var Win = window.open(Uri, Name, Params);
    Win.focus();
}

// (left-top) screen center based on the provided object's width and height
function oCenter(Wth, Hgt)
{
    return new Array( (screen.width - Wth) / 2, (((screen.height - Hgt) / 2) - 20) );
}

function openWin(Uri, Wth, Hgt, Resize, Scroll) { oWindow(Uri, Wth, Hgt, Resize, Scroll); }

//////////////////////////////////////////////////////////////////////////
//  Special windowing functions  
//
function policypops(url)
{
    var opts='resizable=1,scrollbars=1,height=500,width=400,left=100,top=100';
    var popwin = window.open(url,'Policy',opts);
    if (window.focus) { popwin.focus(); }
    return false;
}

///////////////////////////////////////////////////////////////////////////
//  Go to the home page of the country-specific web site as selected from
//  the home pages drop-down country selecteor.
//
function openURL() {
    var coitem = document.coform.sites.selectedIndex;
    var goURL  = document.coform.sites.options[coitem].value;
    top.location.href = goURL;
}


///////////////////////////////////////////////////////////////////////////
//  Special additional functions created for the Omniture web metrics
//  implementation.  *RWM*050216
//
function DownloadTag(dlfile)    // Downloaded file tagging
{
    var s=s_gi('rsid');
    s.eVar3=dlfile;
    s.events="event2";
    s.tl(this,'d',dlfile);
    return true;
}

//-------------------------------------------------------------------------
//
// function that shows/hides an element based on its current display state
//
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 == "none") ? "block" : "none";
}

//-------------------------------------------------------------------------
//
// function that shows/hides an element based on its current display state
//
function findStores(product)
{
    var zip = document.getElementById("zipcode").value;
    if (zip < 00001 || zip > 99999) {
        alert("Please enter a valid 5-digit ZIP Code.");
        zip.focus();
        zip.select();
        return false;
    }
    var url  = 'http://emachines.links.channelintelligence.com/Pages/pp.asp?';
        url += 'cii_sZip=' + zip + '&cii_sSKU=' + product;
    var params = 'location=0,directories=0,status=0,menubar=0,scrollbars,resizable,width=800,height=600';
    var w = window.open(url,"Stores",params);
    w.focus();
    return true;
}

function findStoresByZip ()
{
    var zip = document.getElementById("zipcode").value;
    if (zip.length < 5 || zip.length > 7) {
        alert("Please enter a valid 5-digit ZIP code (US) or 6-character postal code (Canada).");
        return false;
    }
    var product = document.getElementById("zipproduct").value;
    var url  = 'http://emachines.links.channelintelligence.com/Pages/pp.asp?';
        url += 'cii_sZip=' + zip;
    if (product) { url += '&cii_sSKU=' + product; }
    var params = 'location=0,directories=0,status=0,menubar=0,scrollbars,resizable,width=800,height=600';
    var w = window.open(url,"Stores",params);
    w.focus();
    return true;
}

function shopOnline(product)
{
    var url  = 'http://emachines.links.channelintelligence.com/pages/cbl.asp?';
        url += 'sSKU='+product;
    var opts = 'location=0,directories=0,status=0,menubar=0,scrollbars,resizable,width=752,height=600';

    var win  = window.open(url, "Shop", opts);
    win.focus();
    return true;
}

function feedback()
{
    var url  = 'http://support.gateway.com/customerreg/WebForm.asp?FormID=124&template=0';
    var opts = 'location=0,directories=0,status=0,menubar=0,scrollbars,resizable,width=600,height=560';

    var win = window.open(url, "Feedback", opts);
    if (win.focus) { win.focus(); }
    return true;
}


//-------------------------------------------------------------------------
//
//  Handles the 'View Large' function on the product detail page.
//
function popProduct(url)
{
    var opts  = 'width=596,height=495,location=0,directories=0,status=0,menubar=0,';
        opts += 'scrollbars=no,resizable=no';

    var win   = window.open(url, "LargeView", opts);
    return true;
}

//-------------------------------------------------------------------------
//  Popup for MS MDA landing page.
//
function popMS(url)
{
    var width = 500;
    var high  = 500;
    var pct   = 1.00;       //  Make it 85% of parent window size

    if ( parseInt(navigator.appVersion) > 3 )
        {
        if ( navigator.appName == "Netscape" )  {
           width = parseInt(window.innerWidth * pct);
           high  = parseInt(window.innerHeight * pct);
           }

        if ( navigator.appName.indexOf("Microsoft") != -1 ) {
            width = parseInt(document.body.offsetWidth * pct);
            high  = parseInt(document.body.offsetHeight * pct);
            }

        //high = high > width ? width : high;
        }

    var opts  = 'width='+width+',height='+high+',location=0,directories=0,';
        opts += 'status=no,menubar=0,scrollbars,resizable';

    var win   = window.open(url, "Genuine", opts);
    return true;
}

//-------------------------------------------------------------------------
//  General-purpose functions
//

function isNumeric(str)          // Returns true if string is numeric
{
    var len = str.length;
    if (len == 0) return false;

    while (n < len) {
        ch = str.charAt(n);
        if ('0' >= ch || ch >= '9') return false;
        n++;
    }
    return true;
}

function getBrowser()
    {
    var nn4 = (document.layers) ? true : false;
    var ie  = (document.all)    ? true : false;
    var dom = (document.getElementById && !document.all) ? true : false;
    }



//-------------------------------------------------------------------------
//  Search page functions
//  
function doSearch(boxid)
{
    var rawquery = document.getElementById(boxid).value;
//  var query = rawquery.replace(/ *$/,'').replace(/^ */,'') 
    var query = rawquery;
    var url = '/search.html?query=' + query;
    if (query == "" || query == null) {
        url = '/search.html';
    }   
    top.location.href = url;
}   

//
//  Used to expand/collapse title and lists for various things.
//  
function showExpand(n)
    {
    document.getElementById(n+"_picon").style.display = "none";
    document.getElementById(n+"_micon").style.display = "block";
    document.getElementById(n+"_content").style.display = "block";
    }

function hideExpand(n)
    {
    document.getElementById(n+"_picon").style.display = "block";
    document.getElementById(n+"_micon").style.display = "none";
    document.getElementById(n+"_content").style.display = "none";
    }

