//////////////////////////////////////////////////////////////////////////
//  JavaScript Library
//
//  Common functions shared by both Gateway and eMachines websites
//
//////////////////////////////////////////////////////////////////////////

var sitecode;

//////////////////////////////////////////////////////////////////////////
//
//  Windowing functions
//
function openWin(url, width, height)
{
    var params="toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,top=50,left=50,width="+width+",height="+height;
    window.open(url, "btns", params);
}

//------------------------------------------------------
//  Popup function for MS Genuine page
//
function maximize() {
    window.moveTo(0, 0);
    window.resizeTo(screen.width, screen.height);
}

function popMS(url)
{
    var wide = 0;
    var high = 0;
    var pct  = 1.00;       //  Make it 85% of parent window size

    if( typeof( window.innerWidth ) === 'number' ) {
        // Non-IE
        wide = window.innerWidth;
        high = window.innerHeight;
    } else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        // IE 6+ in 'standards compliant mode'
        wide = document.documentElement.clientWidth;
        high = document.documentElement.clientHeight;
    } else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        // IE 4 compatible
        wide = document.body.clientWidth;
        high = document.body.clientHeight;
    }
    wide = wide * pct;
    high = high * pct;

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

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

//-------------------------------------------------------------------------
//  Handles the 'View Large' function on the product detail page.
//  popProduct() is for eMachines
//  popEnlarge() is for Gateway
//
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;
}
function popEnlarge(model)
{
    var opts  = 'width=600,height=500,location=0,directories=0,status=0,menubar=0,';
        opts += 'scrollbars=no,resizable=no';

    var win   = window.open('/products/enlarge.html?prod='+model, "LargeView", opts);
    return true;
}
function popDW(url,name)
{
    if ( name.length < 2 ) name = 'DW';

    var win = window.open(url, name, 'width=700,height=600,scrollbars,resizable');
    win.focus();
    return true;
}


//  I have no idea why this function is defined (could just use window.open!)
function MM_openBrWindow(theURL,winName,features)
{
    window.open(theURL,winName,features);
}

//  Coremetrics version to log hits to these pages.  *rwm*050531
function cmMM_openBrWindow(theURL,winName,features,fileTag,catTag)
{
//  cmCreatePageviewTag(fileTag,catTag);
    window.open(theURL,winName,features);
}


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

function openURL() {
    var coitem = document.coform.sites.selectedIndex;
    var goURL  = document.coform.sites.options[coitem].value;
    top.location.href = goURL;
}

///////////////////////////////////////////////////////////////////////////
//  Used by the product support page to control the expanding/collapsing
//  lists for user guides, drivers, and specs.   *rwm*050216
//
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";
}


//////////////////////////////////////////////////////////////////////////
//
//  These three functions are used by the product support page for the
//  drop-down menu selection controls fro user guides, drivers and specs.
//  They needed to be rewritten because they old way did not work in
//  Firefox or Netscape browsers.   *rwm*March, 2005
//
var supportPage = "./product_support.html";

function clearCat() {
    var cat   = document.thisForm.cat.value;
    var goURL = "";
    if (cat) goURL = "?cat="+[cat];
    if (document.thisForm.subcat) document.thisForm.subcat.value = '';
    if (document.thisForm.model)  document.thisForm.model.value  = '';
    top.location.href = [supportPage]+"?cat="+[cat];
    document.thisForm.submit();
}

function clearSubcat() {
    var cat    = document.thisForm.cat.value;
    var subcat = document.thisForm.subcat.value;
    var goURL  = "";
    if (subcat) goURL = "?cat="+[cat]+"&subcat="+[subcat];
    if (document.thisForm.model) document.thisForm.model.value = '';
    top.location.href = [supportPage]+[goURL];
    document.thisForm.submit();
}

function getInfo() {
    var cat    = document.thisForm.cat.value;
    var subcat = document.thisForm.subcat.value;
    var model  = document.thisForm.model.value;
    var goURL  = "?cat="+[cat]+"&subcat="+[subcat]+"&model="+[model];
    top.location.href = [supportPage]+[goURL];
    document.thisForm.submit();
}


///////////////////////////////////////////////////////////////////////////
//  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 NiceMetName(file)
{
    var f = file;
    return f;
}
//
//  Debugging - only called under certain conditions (see webmetrics.mas)
//
function MetricsMessage()
{
    var cr = "\r\n";
    var msg = "OMNITURE METRICS DATA ("+s_account+")"+cr+cr;

                            msg += "Category="+s.channel;
    if (s.prop1  != '')   { msg += ",  SubCat="+s.prop1; }
                            msg += cr+"PageName="+s.pageName+cr;
    if (s.products != '') { msg += "Product-ID="+s.products; }

    alert(msg);
}


//////////////////////////////////////////////////////////////////////////
//
//  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;
    }

//-------------------------------------------------------------------------
//
// 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';
    var win = window.open(url,"Stores",params);
    return true;
}

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

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


//-------------------------------------------------------------------------
//  Some boolean functions
//
function isBlank (val)
{
    if (val === null)      { return true; }
    if (val.length === 0 ) { return true; }
    return false;     
}

//  Use /[-]?^\d*\.?\d*$/ for negative and/or decimal numbers
function isNumeric (val) 
{
    if (val === null || !val.toString().match(/^\d+$/)) { return false; }
    return true;
}

function isInRange (val,min,max)  // Returns value if string is numeric and in range of parameters
{
    if (!isNumeric(val)) { return false; }

    if (val >= min && val <= max)
        { return true; }
    else
        { return false; }
}

function isContainSpacesOrTab (val)
{
    if (isBlank(val)) { return false; }
    for (var i=0; i<val.length; i++) {
        if ( (val.charAt(i)===" ") || (val.charAt(i)==="\t") ) 
            { return true; }
    }
    return false;     
}

function isValidEmail (val)
{
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    return val.match(re) ? true : false;
}

function isValidUrl (val)
{
    var re = /^(?:http:\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/i;
    return val.match(re) ? true : false;
}

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

// Print/Email product spec page
function printPage (prod)
{
    var status = 0;
    var page = '/products/print.html?prod=' + prod;
    var win = window.open(page,'PrintSpec','width=600,height=700,scrollbars,resizable');
    win.focus();
}
function emailPage (prod)
{
    var status = 0;
    var url = '/products/email.html?prod=' + prod;
    var win = window.open(url,'SendSpec','width=500,height=140,scrollbars,resizable');
    win.focus();
}

// Adds stripes to tables
function stripeOdd(id)
{
    var table = document.getElementById(id);
    if (!table) { return; }
    var trs = table.getElementsByTagName("tr");
    for (var i = 0; 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 = 1; i < trs.length; i += 2) {
        trs[i].className += " odd";
    }
}

// Zebra: Colors alternating rows automatically in tables and lists
function tableStripe(id, start, stop, cssclass)
{
    var i, obj=document.getElementById(id);

    // rows from top to start striping, positive integer
    start = (start) ? Math.abs(parseInt(start)) : 0; 

    // rows from end stop striping, positive integer
    stop = (stop) ? Math.abs(parseInt(stop)) : 0; 

    // CSS class to use for striping, default to light grey
    cssclass = (cssclass) ? "" : "odd"; 
    if (!obj) { return; }
    var row = obj.getElementsByTagName("tr");
    for (i = start; i < row.length - stop; i += 2) 
        { row[i].className += " " + cssclass; }
}

// Removes leading whitespace
function ltrim(val)
{
    var re = /\s*((\S+\s*)*)/;
    return (val && val.length != 0) ? val.replace(re, "$1") : "";
}

// Removes trailing whitespace
function rtrim(val)
{
    var re = /((\s*\S+)*)\s*/;
    return (val && val.length != 0) ? val.replace(re, "$1") : "";
}

// Removes leading and trailing whitespace
function trim(val)
{
    return (val && val.length != 0) ? ltrim(rtrim(val)) : "";
}

//-------------------------------------------------------------------------
//  The addLoadEvent function takes as an argument another function which
//  should be executed once the page has loaded.  Unlike assigning directly
//  to window.onload, the function adds the event in such a way that any 
//  previously added onload functions will be executed first.
//
function addLoadEvent(func) 
{   
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) { oldonload(); }
            func();
        }
    }
}

