function SetCookie (name,value,expires,path,domain,secure) {
 document.cookie = name + "=" + escape (value) +
 ((expires) ? "; expires=" + expires.toGMTString() : "") +
 ((path) ? "; path=" + path : "") +
 ((domain) ? "; domain=" + domain : "") +
 ((secure) ? "; secure" : ""); 
}

function GetCookie (name) {
 var arg = name + "=";
 var alen = arg.length;
 var clen = document.cookie.length;
 var i = 0;
 while (i < clen) {
 var j = i + alen;
 if (document.cookie.substring(i, j) == arg)
 return getCookieVal (j);
 i = document.cookie.indexOf(" ", i) + 1;
 if (i == 0) break;
 }
 return null;
}

function getCookieVal (offset) {
 var endstr = document.cookie.indexOf (";", offset);
 if (endstr == -1)
 endstr = document.cookie.length;
 return unescape(document.cookie.substring(offset, endstr));
}

function DeleteCookie (name,path,domain) {
 if (GetCookie(name)) {
 document.cookie = name + "=" +
 ((path) ? "; path=" + path : "") +
 ((domain) ? "; domain=" + domain : "") +
 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
 }
}
// This function will accept the price, and seperate it with the $, comma, and decimal point.
function FormatCurrency (s, cursym, sepsym, decsym) {
 var rstr = "";
 var str = "" + Math.round(eval(s) * Math.pow(10, 2));
 rpos = 0;
 // Pad out the string to a minimum of three characters
 if (str.length < 3) { 
 for (i = 0; i <= 3 - str.length; i++) {
 str = "0" + str;
 }
 }
 
 // loop through the string and add the currency formatting
 for (i = 0; i <= str.length; i++) {
 var c = str.charAt(str.length - i);
 rpos = i - 2;
 rstr = c + rstr;
 if (i == 2 && (decsym.length > 0)) {
 rstr = decsym + rstr;
 }
 else if ((sepsym.length > 0) && ((i-2)%3) == 0 && (str.length - i) > 0) {
 rstr = sepsym + rstr;
 }
 }
 return cursym + rstr;
}
// This function will accept the mulitvalues text field and return it in an array
function ExplodeList(vallist) 
{ 
 var delim = " "; 
 var id = new Array(); 
 var i = 0; var val = ""; 
 var msgtext = ""; 
 if (vallist.indexOf(delim)) 
 { 
 stpos = 0; 
 endpos = 1; 
 while (endpos > 0) {
 endpos = vallist.indexOf(delim, stpos); 
 if (endpos > 0) {
 val = vallist.substring(stpos, endpos); 
 stpos = endpos + 1; 
 } 
 else {
 val = vallist.substring(stpos, vallist.length); 
 } 
 id[i] = val; 
 i++; 
 } 
 } 
 return id; 
} 


function totalme(me)
{ 

if (!(ValidateQty())) {
if (me.TotalPrice) { me.TotalPrice.value = ""; }
window.status = me.failText.value;
return;
} else {
window.status = "";
}
var BaseVal = parseFloat(me.UnitPrice.value); 
var QuantVal = parseFloat(me.Quantity.value);
var Total = BaseVal;
var QuantPrice = BaseVal;
var none = 0;
var Discount = 0; 
if ((me.QuantityPriceFlag.value == "I") || (me.QuantityPriceFlag.value == "T")) {
var breakval;   
var flag = 0;
Total = 0; 
var subqty; 
var decimal;
QBreaks = me.QuantityBreaks.value; 
PBreaks = me.QuantityPriceBreaks.value; 
abreaks = new Array(); 
abreaks = ExplodeList(QBreaks); 
apricebreaks = new Array();
apricebreaks = ExplodeList(PBreaks); 
none = 1; 
// If the order item is using interval pricing
if (me.QuantityPriceFlag.value == "I") {
for (var i = 0; i < abreaks.length; i++) {
if (QuantVal < abreaks[i]) {
breakval = i - 1; 
flag = 1; break; } 
} 
if (flag == 0) {
breakval = (abreaks.length) - 1; } 
if (breakval < 0) {
QuantPrice = BaseVal; }
else {
QuantPrice = parseFloat(apricebreaks[breakval]); }
Discount = (BaseVal-QuantPrice);
Total = (QuantVal * QuantPrice);
} 

// Else if the order item is using Tiered Pricing
else if (me.QuantityPriceFlag.value == "T") {
QuantValTemp = QuantVal; 
for (var i = abreaks.length-1; i >= 0; i--) {
if (QuantValTemp == abreaks[i]) {
Total += (QuantValTemp * apricebreaks[i]); 
QuantValTemp = 0; break;
} 
else if (QuantValTemp >= abreaks[i]) {
subqty = parseInt(QuantValTemp/abreaks[i]); 
Total += (subqty*abreaks[i] * apricebreaks[i]); 
QuantValTemp = (QuantValTemp%abreaks[i]);
} 
} 
if (QuantValTemp >= 1) {
Total += (QuantValTemp * BaseVal);
} 
QuantPrice = (Total/QuantVal);
Discount = (BaseVal-QuantPrice); 
} // end elseif T 
} // end if I or T
var choice; var OptCost; var Cost; var i; var optioncosts = 0; var OpTotal = 0; var optTest = "";
// Now add the extra options cost 
//jasonwashere - start
var optList = ""; optPrice = 0;

// Now add the extra options cost 
for (i = 1; i <= 3; i++)
//me.NumberOfOptions.value; i++) 
{
    if (me.NumberOfOptions.value >= i) 
    {
        optTest = retOptCode(eval("me.Option_Value_" + i + ".value"))
        if(optTest) {
            optList = optList + optTest;
        }
        else {
            alert("The code for Option " + i + " of Item: " + sItemNum + " is not found.  Please notify questions@restoration.com of this error.");
        }
        OptCost = eval("me.ChargeableOptionPrices_" + i + ".value"); 
        chargeopt = new Array(); 
        chargeopt = ExplodeList(OptCost); 
        
        if (eval("me.ChargeableOptionPriceFlag_" + i + ".value") == "L") {
            choice = eval("me.Option_Value_" + i + ".selectedIndex"); 
            Cost = parseFloat(0);
            optioncosts += Cost;
            OpTotal += parseFloat(QuantVal * Cost);
        } else if (eval("me.ChargeableOptionPriceFlag_" + i + ".value") == "V") {
            if(eval("me.Option_Value_" + i + ".value") != "") {
                Cost = parseFloat(0);
                optioncosts += Cost;
                OpTotal += parseFloat(QuantVal * Cost);
            }
        }
    }
    else
     {
    optList = optList + "0000"
    }
} // end for loop

optPrice = retOptPrice(optList, sItemNum);

if (optPrice >= 0) 
{
optioncosts = optPrice - me.UnitPrice.value;
me.UnitPrice.value = parseFloat(QuantVal * optPrice);
Total = me.UnitPrice.value;
BaseVal = Total;
}
else
{
optioncosts = 0;
Total = -99999;  //me.UnitPrice.value;
}

if (Total >= 0) {

    //Jasonwashere - end
    //  if (none == 0) { Total = (Total * QuantVal) + OpTotal; }
    //  else { Total += OpTotal; }
    if (me.BasePrice) {
    me.BasePrice.value = FormatCurrency(BaseVal, "$", ",", "."); }
    if (me.UnitDiscount) {
    me.UnitDiscount.value = FormatCurrency(Discount, "$", ",", "."); }
    if (me.DiscountPrice) {
    me.DiscountPrice.value = FormatCurrency(QuantPrice, "$", ",", "."); }
    if (me.UnitOptionsCharge) {
    me.UnitOptionsCharge.value = FormatCurrency(optioncosts, "$",",","."); } 
    if (me.FinalPrice) {
    me.FinalPrice.value = FormatCurrency((QuantPrice+optioncosts), "$", ",", "."); }
    if (me.TotalPrice) {
        me.TotalPrice.value = FormatCurrency(Total, "$", ",", ".");}
    var OldTotal = Total;
    var NewTotal; var Discount; var MoneyOff;
    Amounts = new Array();
    Amounts = ExplodeList(me.CustomerDiscount.value);
    Discount = parseFloat(Amounts[0])
    for (var i = 0; i < Amounts.length; i++) {
    if (Amounts[i] > Discount) { Discount = parseFloat(Amounts[i]); }
    }
    
    MoneyOff = parseFloat(OldTotal*Discount);
    NewTotal = parseFloat(OldTotal-MoneyOff);
    if (me.TotalPrice) { me.TotalPrice.value = FormatCurrency(NewTotal, "$", ",", "."); }
    if (me.ShoppingGroupDiscount) { me.ShoppingGroupDiscount.value = FormatCurrency(MoneyOff, "$", ",", "."); }
    //doit();
  
  } else {
    me.TotalPrice.value = optPrice ;

  }
doit();
}

function SetValue() {
 var cvalue = GetCookie("btordernum");
 if (cvalue==null) {
 var exp = new Date();
 exp.setTime(exp.getTime() + (30*24*60*60*1000));
 SetCookie("btordernum",this.document.forms[0].OrderUniqueID.value,exp,"/");
 }
 else { 
 this.document.forms[0].OrderUniqueID.value=cvalue};
}
function SCItemOrderSubmit(key) {
 var f = document.forms[0];

  if (isantique) {

    if (f.confirmantique.checked) {
      document.getElementById("checkantique").innerHTML = "Being that this is an antique item it is not permitted to be returned once purchased. Please check this box to agree to these terms and conditions before adding it to your cart.<br/><img src='/_item_images/1x1.gif' height='17' width='20'> I Agree";
      isantique=true;
    } else {
      alert("You must agree to the terms and conditions for purchasing Antique items before adding this item to your cart.");
      return;
    }
  } 


    if (f.TotalPrice.value == "N/A") {
        alert("The combination of options you have selected is not valid for this item.  Please select a different option or contact Crown City Hardware at questions@restoration.com for assistance.");
        //window.history.back();
        return;
    }
// if (confirmbeforesubmit() == false) {return;}

 if (f.SubmitAction.value != "*") {
 alert("Only one SubmitAction can be processed.\n Click OK to continue.");
 location.reload();
 return;
 }
 if (key == "AddToCart") {
 SetValue();
 if (!(ValidateQty())) { alert(f.failText.value); return };
 if (!(ValidateGC())) {return};
 }
 if (key == "delete") {
 f.Quantity.value="0";
 }
 if (key == "update") {
 if (f.Quantity.value == "0") { 
 if (confirm("Would you like to remove this item from your cart?")) { key = "delete"; } else { return } ;
 } else {
 if (!(ValidateQty())) { alert(f.failText.value); return };
 if (!(ValidateGC())) {return};
 }
 }
 if (key == "cancel") {
 history.back();
 return;
 }
 f.SubmitAction.value=key;
 if (window.name == "") {f.InFrame.value = "_top"} else {f.InFrame.value=window.name};
 f.submit();
}
function wItemOrderLoad(f) {
document.forms[0].Nocache.value = new Date();
 if (f.Quantity.value=="0") { location.replace(f.ShoppingCartURL.value); return; };
 f.SubmitAction.value ="*";
 totalme(f);

 customOnLoad();

 // MM_preload_rollover_images();
}
function wItemOrderUnload() {
 window.status = "";
}
function ValidateQty(){
 s = document.forms[0].Quantity.value;
 minQuant = parseInt(document.forms[0].MinQty.value);
 incQuant = parseInt(document.forms[0].BoxQty.value);
 failText = "";
 if (isInteger(s)) {
 sInt = parseInt(s);
 if (sInt < minQuant) { failText += "Order is below the minimum quantity of " + minQuant; } 
 else {
 if (incQuant && ((sInt % incQuant) != 0)) {
 failText += "Products must be purchased in units of " + incQuant + "\n";
 }
 }
 } else { failText += "Quanity is not a number"; }
 if (failText.length > 0 ) { 
 document.forms[0].failText.value = failText;
 return false;
 } else { return true; }
}
function isInteger (s){
 var validChars = "1234567890";
 if (s.length == 0) { return false; } 
 else { // make sure all characters are valid
 for (i=0; i <= s.length; i++) {
 var c = s.charAt(i);
 if (validChars.indexOf(c) == -1) { return false; }
 }
 } 
 return true;
}
function IsEmail(s){
 if(s){
 s = s.value;
 // should be of format MyAcct@MyDomain.com
 if ((s.indexOf('@') >0 ) && (s.indexOf('.', s.indexOf('@')) > 0)) {
 return true;
 } else {
 return false;
 }
 }
 return true
}
function ValidateGC() {
 var I = document.forms[0].ItemType.value;
 var failText = "";
 if (I == "G") { 
 var opt1 = document.forms[0].Option_Value_1.value;
 var opt2 = document.forms[0].Option_Value_2.value;
 var opt3 = document.forms[0].Option_Value_3.value;
 
 if (opt1 == "") { failText = "The Gift Certificate Recipient's Name as No Value";
 alert(failText)
 return false}; 
 if (!(IsEmail(opt2))) {failText = "The Recipient's email address is invalid";
 alert(failText);
 return false};
 }
 return true;
}

