// define xb styles
var xbListDisplay = (document.all) ? "block" : "list-item";
var xbShow = (document.layers) ? "show" : "visible";
var xbHide = (document.layers) ? "hide" : "hidden";
var xbTrShow = (document.all) ? "block" : "table-row";
var xbTbodyShow = (document.all) ? "block" : "table-row-group";
var xbCursorHand = (document.all) ? "hand" : "pointer";

// browser detection
var mac = (navigator.userAgent.indexOf("Mac") != -1);
var ie = document.all;
var ie5 = ie && document.getElementById;
var ie5mac = mac && ie5;


// define cross browser style object
function xbStyleObj(el)
{
    if(document.getElementById)
        return document.getElementById(el).style;
    else if(document.all)
        return document.all[el].style;
    else if(document.layers)
        return document.layers[el];
    else
        return false;                        
}

// define cross browser object
function xbObj(el)
{
    if(document.getElementById)
        return document.getElementById(el);
    else if(document.all)
        return document.all[el];
    else if(document.layers)
        return false;
    else
        return false;                        
}


function hideLayer(el)
{    
    xbStyleObj(el).visibility = xbHide;
}

function showLayer(el)
{    
    xbStyleObj(el).visibility = xbShow;
}

//=====================================================================
// DOM Image Rollover v3
//=====================================================================
//     How to use:
//     1 : Place this script RIGHT ABOVE the closing body tag
//     2 : * For images that need rollovers, edit/add a "class" attribute
//           with the string "domroll your_rollover_image.jpg"
//         * If you already have a class attribute on that image:
//           &lt;img class="original class domroll image.jpg other stuff"&gt;
//
//    Example: &lt;img src="off.gif" class="domroll on.gif"&gt;
//=====================================================================
// copyright Chris Poole
// http://chrispoole.com
// domroll@chrispoole.com
// demo: http://chrispoole.com/scripts/dom_image_rollover/
//=====================================================================

function domRollover() {
    if (navigator.userAgent.match(/Opera (\S+)/)) {
        var operaVersion = parseInt(navigator.userAgent.match(/Opera (\S+)/)[1]);
    }
    if (!document.getElementById||operaVersion <7) return;
    var imgarr=document.getElementsByTagName('img');
    var imgPreload=new Array();
    var imgSrc=new Array();
    var imgClass=new Array();
    for (i=0;i<imgarr.length;i++){
        if (imgarr[i].className.indexOf('domroll')!=-1){
            imgSrc[i]=imgarr[i].getAttribute('src');
            imgClass[i]=imgarr[i].className;
            imgPreload[i]=new Image();
            if (imgClass[i].match(/domroll (\S+)/)) {
                imgPreload[i].src = imgClass[i].match(/domroll (\S+)/)[1]
            }
            imgarr[i].setAttribute('xsrc', imgSrc[i]);
            imgarr[i].onmouseover=function(){
                this.setAttribute('src',this.className.match(/domroll (\S+)/)[1])
            }
            imgarr[i].onmouseout=function(){
                this.setAttribute('src',this.getAttribute('xsrc'))
            }
        }
    }
}


//window.onload = function() {
//    domRollover();
//}


/**************************************
» Autor: Jonas Raoni Soares Silva
» Site: http://www.joninhas.ath.cx:666
**************************************/

fmtMoney = function( n, c, d, t ) {
    var m = ( c = Math.abs( c ) + 1 ? c : 2, d = d || ",", t = t || ".", /(\d+)(?:(\.\d+)|)/.exec( n + "" ) ), x = m[1].length % 3;
    return ( x ? m[1].substr( 0, x ) + t : "" ) + m[1].substr( x ).replace( /(\d{3})(?=\d)/g, "$1" + t ) + ( c ? d + ( +m[2] ).toFixed( c ).substr( 2 ) : "" );
};



/***********************************************
* Image Thumbnail Viewer II script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use
***********************************************/

//Specify image paths and optional link (set link to "" for no link):
var dynimages=new Array()
dynimages[0]=["/graphics/map_1_day.gif", ""]
dynimages[1]=["/graphics/map_2_day.gif", ""]
dynimages[2]=["/graphics/map_3_day.gif", ""]
dynimages[3]=["/graphics/map_4_day.gif", ""];
dynimages[4]=["/graphics/map_5_day.gif", ""];
dynimages[5]=["/graphics/map_6_day.gif", ""];
dynimages[6]=["/graphics/map_usa.gif", ""];
dynimages[7]=["/graphics/map_ups_1_day.gif", ""]
dynimages[8]=["/graphics/map_ups_2_day.gif", ""]
dynimages[9]=["/graphics/map_ups_3_day.gif", ""]
dynimages[10]=["/graphics/map_ups_4_day.gif", ""];
dynimages[11]=["/graphics/map_ups_5_day.gif", ""];
dynimages[12]=["/graphics/map_ups_6_day.gif", ""];
dynimages[13]=["/graphics/map_ups_usa.gif", ""];


//Preload images ("yes" or "no"):
var preloadimg="yes"

//Set optional link target to be added to all images with a link:
var optlinktarget=""

//Set image border width
var imgborderwidth=0

//Optionally, change 1.0 and 0.7 below to affect Wipe gradient size and duration in seconds in IE5.5+:
var filterstring="progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0 Duration=0.7)"

///////No need to edit beyond here/////

if (preloadimg=="yes"){
for (x=0; x<dynimages.length; x++){
var myimage=new Image()
myimage.src=dynimages[x][0]
}
}

function returnimgcode(theimg){
var imghtml=""
if (theimg[1]!="")
imghtml='<a href="'+theimg[1]+'" target="'+optlinktarget+'">'
imghtml+='<img src="'+theimg[0]+'" border="'+imgborderwidth+'">'
if (theimg[1]!="")
imghtml+='</a>'
return imghtml
}

function modifyimage(loadarea, imgindex){
if (document.getElementById){
var imgobj=document.getElementById(loadarea)
if (imgobj.filters && window.createPopup){
imgobj.style.filter=filterstring
imgobj.filters[0].Apply()
}
imgobj.innerHTML=returnimgcode(dynimages[imgindex])
if (imgobj.filters && window.createPopup)
imgobj.filters[0].Play()
return false
}
}

function updatePricePerCarton(addtlCostVal, qtyVal, totalVal, priceEachElm, totalElm, qtyPerCarton, priceEachVal)
{
    var extra = Number(addtlCostVal * qtyPerCarton);
    //var price = parseInt(qtyVal) * ((totalVal*1) + extra);
    var pricePer = (priceEachVal*1) + (addtlCostVal*1);
    
    //alert(addtlCostVal);
    var price = parseInt(qtyVal)*(((priceEachVal*1)+(addtlCostVal*1)) *qtyPerCarton);//       parseInt(qtyVal) * ((totalVal*1) + extra);
    
    price = price.toFixed(2);
    
    
    if (parseInt(qtyVal) == 0)
    {
        price = 0;
    }
    //alert((addtlCostVal*1) + (priceEachVal*1));
    //document.getElementById(priceEachElm).innerHTML = pricePer.toFixed(2);
    document.getElementById(priceEachElm).innerHTML = ((price/qtyVal)/qtyPerCarton).toFixed(2);
    document.getElementById(totalElm).innerHTML = (qtyVal*(((priceEachVal*1)+(addtlCostVal*1)) *qtyPerCarton)).toFixed(2);
    //document.getElementById(totalElm).innerHTML = price;
}

function updatePricePerUnit(pricePerSize, qtyVal, totalElm)
{
    //var price = parseInt(qtyVal * pricePerSize);
    var price = qtyVal * pricePerSize;
    price = price.toFixed(2);
    if (parseInt(qtyVal) == 0 || isNaN(price))
    {
        price = 0;
    }
    document.getElementById(totalElm).innerHTML = price;
}

function updatePricePerBlock(pricePerBlock, qtyVal, totalElm)
{
    var discount = 0;
    if(qtyVal >= 12) discount = 0.10;
    var price = (qtyVal*1) * ((pricePerBlock-discount)*1);
    price = price.toFixed(2);
    if (parseInt(qtyVal) == 0 || isNaN(price))
    {
        price = 0;
    }
    document.getElementById(totalElm).innerHTML = price;
}

function updatePricePerWicks(firstSelectVal, secondSelectVal, qtyVal, totalElm)
{
    var tot = document.getElementById(totalElm);
    
    if(firstSelectVal != '')
    {        
        var price = (secondSelectVal * firstSelectVal * qtyVal).toFixed(2);
        if(parseInt(qtyVal) == 0 || isNaN(price))
        {
            price = 0;
        }
        tot.innerHTML = price;
    }
}

function updateQtyForWicks(firstSelectVal, secondSelectVal, qtyVal, totalElm)
{
    var tot = document.getElementById(totalElm);
    
    if(firstSelectVal != '' && secondSelectVal != '')
    {        
        var price = (secondSelectVal * firstSelectVal * qtyVal).toFixed(2);
        if(parseInt(qtyVal) == 0 || isNaN(price))
        {
            price = 0;
        }
        tot.innerHTML = price;
    }
}

// open child window
function openChildWin(url,winName,wWidth,wHeight,location)
{
    var newWin = '';
    var monitorWidth = screen.width;
    var monitorHeight = screen.height;
    var screenWidth = monitorWidth;
    var screenHeight = monitorHeight;        
    var loc = location;
    
    if(loc == 'center')
    {
        var winLeft = (screenWidth/2) - (wWidth/2);
        var winTop = (screenHeight/2) - (wHeight/2);
    }    
    else if(loc == 'left')
    {    
        var winLeft = 0;
        var winTop = 0;
    }
    else if(loc == 'right')
    {
        var winLeft = (screenWidth - wWidth) - 8;
        var winTop = 0;
    }
    newWin = window.open(url,winName,'width=' + wWidth + ',height=' + wHeight + 
    ',screenX=' + winLeft + ',left=' + winLeft + ',screenY=' + winTop + ',top=' + 
    winTop + ',scrollbars=yes,status=yes');
    if(document.getElementById || document.layers)
        newWin.focus();
}

//Enhanced
function openChildWin2(url,winName,wWidth,wHeight,location, iCount)
{
    var newWin = '';
    var monitorWidth = screen.width;
    var monitorHeight = screen.height;
    var screenWidth = monitorWidth;
    var screenHeight = monitorHeight;        
    var loc = location;
    
    // Get the important stuff
    var pid = document.getElementById("pid_" + iCount).value;
    var qty = document.getElementById("caseqty_" + iCount).value;
    
    if (qty == "") {
    	return false;
    }
    
    var lids = document.getElementById("lid_" + iCount);
    var lid = '';
    if (lids != undefined) {
    	lid = lids[lids.selectedIndex].value;
    } 
    
    //alert(document.getElementById("lid_" + iCount).selectedIndex);
    if(loc == 'center')
    {
        var winLeft = (screenWidth/2) - (wWidth/2);
        var winTop = (screenHeight/2) - (wHeight/2);
    }    
    else if(loc == 'left')
    {    
        var winLeft = 0;
        var winTop = 0;
    }
    else if(loc == 'right')
    {
        var winLeft = (screenWidth - wWidth) - 8;
        var winTop = 0;
    }
    newWin = window.open(url + "?pid=" + pid + "&q=" + qty + "&l=" + lid,winName,'width=' + wWidth + ',height=' + wHeight + 
    ',screenX=' + winLeft + ',left=' + winLeft + ',screenY=' + winTop + ',top=' + 
    winTop + ',scrollbars=yes,status=yes');
    if(document.getElementById || document.layers)
        newWin.focus();
        
    return false;
}

// enhanced PPK findPos scripts
// supposed to work well in Safari

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
            curleft += obj.offsetLeft;
            if(!obj.offsetParent)
            break;
            obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
            break;
            obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}


// find position of element to set
function findPos(el,lnk,xOffset,yOffset)
{
    var anch = (document.layers) ? 'NS' + lnk : lnk;
    var o = xbObj(anch);        // xb element
    var s = xbStyleObj(el);        // xb style
    
    if(ie5mac)
        var tOffset = yOffset - 15;
    else if(document.layers)
        var tOffset = yOffset;
    else
        var tOffset = yOffset;
        
    var lOffset = (ie5mac) ? xOffset + 11 : xOffset;
    
    if(!document.layers)
    {
        s.left = (findPosX(o) + lOffset) + "px";
        s.top = (findPosY(o) + tOffset) + "px";
    }
    else
    {
        s.left = document.images[anch].x;
        s.top = document.images[anch].y + tOffset;
    }
}


// function show tooltip
function showTip(tipDiv,lnk,xOffset,yOffset)
{
    findPos(tipDiv,lnk,xOffset,yOffset); 
    showLayer(tipDiv);
}

function showToolTip(tipDiv,lnk,xOffset,yOffset,msg)
{
    showTip(tipDiv,lnk,xOffset,yOffset); 
    xbObj(tipDiv).innerHTML = msg;    
}
    
function hideToolTip(tipDiv)
{
    hideLayer(tipDiv);
}

var increment = 1;
         
function addRow()
{
    // first, check to see if the browser supports dynamic element creation
    if((document.createTextNode) && (increment != "50")) /// max it out at 10
    {

        // identify destination element
        dest = document.getElementById("row-appender");
        // create first row
        row = document.createElement("TR");
        row.id = "myRow" + increment;
        
        // create first table cell
        td1 = document.createElement("TD");

        input = document.createElement("INPUT");
        input.id = "inp_qty_" + increment;
        input.name = "inp_qty_" + increment;
        input.type = "text";
        input.className = "box";
        input.size = "12";
        input.value = "0";

        // append to parent row
        td1.appendChild(input);
        row.appendChild(td1);

        // create second table cell
        td2 = document.createElement("TD");

        selStyle = document.createElement("INPUT");
        selStyle.id = "sel_style_" + increment;
        selStyle.name = "sel_style_" + increment;
        selStyle.type = "text";
        selStyle.className = "box";
        selStyle.size = "12";

        // append to parent cell
        td2.appendChild(selStyle);
        row.appendChild(td2);
        
        // append to parent cell        
        td3 = document.createElement("TD");

        selSize = document.createElement("INPUT");
        selSize.id = "sel_size_" + increment;
        selSize.name = "sel_size_" + increment;
        selSize.type = "text";
        selSize.className = "box";
        selSize.size = "12";

        // append to parent cell
        td3.appendChild(selSize);
        row.appendChild(td3);

        td4 = document.createElement("TD");

        selLidStyle = document.createElement("INPUT");
        selLidStyle.id = "sel_lid_style_" + increment;
        selLidStyle.name = "sel_lid_style_" + increment;
        selLidStyle.type = "text";
        selLidStyle.className = "box";
        selLidStyle.size = "12";

        // append to parent cell
        td4.appendChild(selLidStyle);
        row.appendChild(td4);
        
        // new
        td5 = document.createElement("TD");
        
        selColor = document.createElement("INPUT");
        selColor.id = "sel_color_" + increment;
        selColor.name = "sel_color_" + increment;
        selColor.type = "text";
        selColor.className = "box";
        selColor.size = "12";
        
        // append to parent cell
        td5.appendChild(selColor);
        row.appendChild(td5);
        
        // append to destination TBODY                
        dest.appendChild(row);
        // increment id by 1
        increment++;
    }
}

// remove row
function removeRow(rowID)
{
    document.getElementById(rowID).style.display = "none";
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
