// Shopsite Wholesale/Retail Pricing
//
// requires: BCAY_DECRYPT01.JS
//           BCAY_COOKIE01.JS
//
// copyright 2007 Beads Creativity and You, all rights reserved

var WholesaleDiscount = -1;
var ProductHidden = false;
var ProductWholesale = false;
var ProductNames = new Array();
var LinkHidden = false;

function ProductBeginQty(type,qty,limit) {
    if (WholesaleDiscount<0) WholesaleDiscount=GetDiscount();
    if ((qty<=limit)||(qty==0)) {
        document.write('\n<div style="display=none">\n');
        ProductHidden = true;
        if (WholesaleDiscount) ProductWholesale = true;
        else                   ProductWholesale = false;
    }
    else if (WholesaleDiscount) {
        if (type == "checked") {
            document.write('\n<div style="display=block">\n');
            ProductHidden = false;
            ProductWholesale = true;
        }
        else {
            document.write('\n<div style="display=none">\n');
            ProductHidden = true;
            ProductWholesale = false;
       } 
    }
    else {
        if (type!= "checked") {
            document.write('\n<div style="display=block">\n');
            ProductHidden = false;
            ProductWholesale = false;
        }
        else {
            document.write('\n<div style="display=none">\n');
            ProductHidden = true;
            ProductWholesale = true;
       } 
    }
}


function ProductBegin(type) {
    if (WholesaleDiscount<0) WholesaleDiscount=GetDiscount();
    if (WholesaleDiscount) {
        if (type == "checked") {
            document.write('\n<div style="display=block">\n');
            ProductHidden = false;
            ProductWholesale = true;
        }
        else {
            document.write('\n<div style="display=none">\n');
            ProductHidden = true;
            ProductWholesale = false;
       } 
    }
    else {
        if (type!= "checked") {
            document.write('\n<div style="display=block">\n');
            ProductHidden = false;
            ProductWholesale = false;
        }
        else {
            document.write('\n<div style="display=none">\n');
            ProductHidden = true;
            ProductWholesale = true;
       } 
    }
}
   
function ProductEnd() {
    document.write('\n</div>\n');
    LinkHidden = ProductHidden;
    ProductHidden = false;  
    ProductWholesale = false;
}
   
function ShowPricing(RecordNumber, price1, break1, price2, break2, price3, break3, price4) {
    if (WholesaleDiscount<0) {
        WholesaleDiscount = GetDiscount();
        ProductWholesale = (WholesaleDiscount) ? true : false;
        ProductHidden = false;
    }
    if (ProductHidden) {
        if (ProductWholesale) document.write('<input type="hidden" name="'+RecordNumber+':price" value='+MoneyValue(price1)+'>\n');
    }
    else {
        if (ProductWholesale) {
            if (WholesaleDiscount>=20) WholesalePrice(price1, 100-WholesaleDiscount, RecordNumber);
            else RetailPrice(price1, RecordNumber);
        }
	      else QuantityPriceTable(price1, break1, price2, break2, price3, break3, price4);
   }
}

function WholesalePrice(price, discount, RecordNumber) {
    wholesaleprice = (price*(100-WholesaleDiscount))/100;
    document.write('<span class="pr_price" style="font-size: 13pt;"><b>\n');
    document.write('Wholesale Price:&nbsp;<font color=#0000FF>'+Money((price*discount)/100)+'</font>&nbsp;each</b>\n');
	  document.write('</span>\n');
    document.write('<input type="hidden" name="'+RecordNumber+':price" value='+MoneyValue(wholesaleprice)+'>\n');
}
    		
function RetailPrice(price, RecordNumber) {
    document.write('<span class="pr_price" style="font-size: 13pt;"><b>\n');
    document.write('Retail Price:&nbsp;<font color=#0000FF>'+Money(price)+'</font>&nbsp;each</b>\n');
	  document.write('</span>\n');
    document.write('<input type="hidden" name="'+RecordNumber+':price" value='+MoneyValue(price)+'>\n');
}

function HiddenPrice(price) {
    document.write('<input type="hidden" name="'+RecordNumber+':price" value='+MoneyValue(price1)+'>\n');
}
    		
function QuantityPriceTable(price1, break1, price2, break2, price3, break3, price4) {
    document.write('<span class="pr_price" style="font-size: 10pt;font-weight: bold;\n">');
	  document.write('<TABLE class="qp_header" BORDER=0 BGCOLOR="#FFFFFF" CELLSPACING=1 CELLPADDING=1><tr>\n');
	  document.write('<td valign=TOP align=left>Quantity</td>');
	  document.write('<td align=center><b><font color=#000000>&nbsp;&nbsp;1&nbsp;-&nbsp;'+(break1-1)+'&nbsp;&nbsp;</font></b></td>\n');
	  document.write('<td align=center><b><font color=#0066cc>&nbsp;&nbsp;'+break1+'&nbsp;-&nbsp;'+(break2-1)+'&nbsp;&nbsp;</font></b></td>\n');
	  document.write('<td align=center><b><font color=#009900>&nbsp;&nbsp;'+break2+'&nbsp;-&nbsp;'+(break3-1)+'&nbsp;&nbsp;</font></b></td>\n');
	  document.write('<td align=center><b><font color=#FF0000>&nbsp;&nbsp;'+break3+'+&nbsp;&nbsp;</font></b></td></tr>\n');
	  document.write('</tr><tr>\n');
	  document.write('<td BGCOLOR="#FFFFFF" valign=TOP align=left>price</td>\n');
	  document.write('<td BGCOLOR="#FFFFFF" align=center><b><font color=#000000>'+Money(price1)+'</font></b></td>\n');
	  document.write('<td BGCOLOR="#FFFFFF" align=center><b><font color=#0066cc>'+Money(price2)+'</font></b></td>\n');
	  document.write('<td BGCOLOR="#FFFFFF" align=center><b><font color=#009900>'+Money(price3)+'</font></b></td>\n');
	  document.write('<td BGCOLOR="#FFFFFF" align=center><b><font color=#FF0000>'+Money(price4)+'</font></b></td>\n');
	  document.write('</tr></TABLE></span>\n');
}

function AddToCart(RecordNumber, AddImage) {
		if (WholesaleDiscount!=1) {
          document.write('<td align=right valign=center>\n');
          document.write('<span class="pr_quantity">Quantity: <input class="pr_quantity" type="text" size="2" name="'+RecordNumber+':qnty" value="1"></span>\n');
          document.write('</td>\n<td align=center valign=center>\n');
          document.write('<input class="pr_addtocart_button" type="image" value="submit" '+AddImage+' alt="Add to Cart">\n</td>\n');
    }
		else document.write('<td colspan=2>&nbsp;</td>\n');
}

function HideIfRetail() {
    if (WholesaleDiscount==1) document.write('\n<div style="display=none">\n');
}

function EndHideIfRetail() {
    if (WholesaleDiscount==1) document.write('\n</div>\n');
}

function GetDiscount() {
    var DiscountName, DiscountCookie;
    var Discount=0;
    var i;
    
    DiscountCookie = getCookieValue('BCAYwholesale');
    if (!DiscountCookie) return 0;
    if (DiscountCookie=='Retail') return 1;
    if (DiscountCookie=='Price2') return 0;
    DiscountName = Decrypt(DiscountCookie);
    if (DiscountName=='------') return 0;
    Discount = eval(DiscountName.substring(4,6));
    if (Discount<30) return 0;
    if (Discount>60) return 0;
    if (Discount%5)  return 0;
    return Discount;
}
 
function Money(amount) {
    var b_negative = Boolean(amount < 0);
    var dollars;
	
	  amount = Math.abs(amount);
	  dollars = MoneyValue(amount);
    
    return  b_negative? '($' + dollars + ')': '$' + dollars;
}

function MoneyValue(mnt) {
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? 
                   mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? 
                   mnt + '0' : mnt);
}

function DisplayAdd(form,pid) {
    var quanity = 1;
    for (var i=0;i<form.length;i++) {
        found = form.elements[i].name.indexOf('qnty');
        if (found != -1) quanity = form.elements[i].value;
    }
    ShowAddProduct(pid, quanity);
    return true;
}

function ShowAddProduct(pid, quanity) {
    config='toolbar=no,location=no,directories=no,status=no,menubar=no';
    config += ',scrollbars=no,resizable=no';
    config += ',width=300,height=200';
    pop = window.open ("","pop",config);
    pop.document.write('<script language="javascript">');
    pop.document.write('setTimeout(');
    pop.document.write('"self.close()');
    pop.document.write(';",3000)');
    pop.document.write('</');
    pop.document.write('script>');
    pop.document.write('<body bgcolor=#FFFFFF>');
    pop.document.write('<center><b><h2>PRODUCT ADDED</h2></b></center>');
    pop.document.write('<center><b><h3>'+ProductNames[pid]+'<br>Quanity: '+quanity+'</h3></b></center>');
    pop.document.write('<center><b><h5>This window will close automatically.</h6></b></center>');
}

function ProductName(number, product)
{
    ProductNames[number] = product;
}

function LinkBegin()
{
    if (LinkHidden) document.write('\n<div style="display=none">\n');
    else            document.write('\n<div style="display=block">\n');
}

function LinkEnd()
{
    document.write('\n</div>\n');
}

function MoreInfo(type)
{
    var CurrentURL, NewURL;
    var p, d, n;
    
    if (WholesaleDiscount<0) WholesaleDiscount=GetDiscount();
    if (((WholesaleDiscount==0) && (type == "checked")) ||
        ((WholesaleDiscount>0)  && (type != "checked"))) {
        CurrentURL = document.URL;
        NewURL = 'fpp';
        p = CurrentURL.indexOf('product');
        d = CurrentURL.lastIndexOf('.');
        n = eval(CurrentURL.substring(p+7,d));
        if ((WholesaleDiscount==0)) n = n-1;
        else                        n = n+1;
        NewURL = CurrentURL.substring(0,p+7)+n+CurrentURL.substring(d);
        document.URL = NewURL;
    } 
}

// Display Limited Quanity
//  usage: LimitedQuanity([-- PRODUCT.QuantityOnHand --],threshold)
function LimitedQuanity(quanity,threshold)
{
    if (quanity>threshold) document.write('LIMITED QUANTITIES AVAILABLE.');
    else document.write('ONLY '+quanity+' AVAILABLE.');
}



