/*

Cookie related function


Change the following parameters to adjust freight and insurance details
*/

var Note = "";

var insurancechargeunit = 1.25;
var insurancedollarisper = 50; 
var insuranceincrement = 0.75;
var freightcost = 7.25;
var freightchargecutout = 275;

var recentBrowser = false 

if ((parseInt(navigator.appVersion) >= 4 && navigator.appVersion.indexOf("MSIE") != -1) || ((parseInt(navigator.appVersion) >= 6 && navigator.appName.indexOf("Netscape") != -1))){
	recentBrowser = true
	//opera 7 also true for these parameters
}
if ((parseInt(navigator.appVersion) >= 5 && navigator.appName.indexOf("Netscape") != -1) || ((parseInt(navigator.appVersion) >= 6 && navigator.appName.indexOf("Netscape") != -1))){
	recentBrowser = true
}

function rCookiesOn() {
var hascookie = 2;
if (!document.cookie){
  cookie_name = "interPetOrder";
  document.cookie=cookie_name+"=; path=/";
  (!document.cookie)?hascookie = 0:hascookie=1; 
}
return hascookie;
}


function CookieWarning() {
  if (rCookiesOn()==0) {
  alert("Sorry, you need to enable 'Cookies' on your browser to use this site: Look in Edit menu:preferences (Macintosh, FF or Netscape), Tools menu Internet option (Windows Internet Explorer) on your browser")
 // var noCookies = window.open("/cookies.html","NoCookies","HEIGHT=600,WIDTH=450,status=0,resizable=1,scrollbars=1,menubar=0");
}
}

function getCookieValue(cookie_name) {
var cv="";
  if(document.cookie)
   {
        index = document.cookie.indexOf(cookie_name+"=");
        if (index != -1)
     {
           	namestart = (document.cookie.indexOf("=", index) + 1);
            nameend = document.cookie.indexOf(";", index);
            if (nameend == -1) {nameend = document.cookie.length;}
            cv = document.cookie.substring(namestart, nameend);     
          
		
           }
           }
return cv;

}

function eatCookie(cookie_name) {
if(document.cookie) {
        index = document.cookie.indexOf(cookie_name);
        if (index != -1) document.cookie=cookie_name+"=; path=/";
    }
}

function rememberOrder() {
 var order= unescape(getCookieValue("interPetOrder"));
 if (order =="undefined") order = "";
 return order;
}


function storeOrder(order) {
 document.cookie="PalmCurrentProduct="+escape(order)+"; path=/"; 
}

function forgetOrder() {
eatCookie("interPetOrder");
history.go(0);
}

//calle by interpetorder.html
function forgetngoback() {
eatCookie("interPetOrder");
history.go(0);
}



function priceProductNorder(productPrice) {
p = productPrice.split("$")
document.write(p[0]+'</td><td align="Right">$'+p[1]+'</td><td><input type=text size=2 name="'+productPrice+'" value="'+norder(productPrice)+'" maxlength=2 onChange="orderIt(\''+productPrice+'\');">');

}


function priceNorder(productPrice) {
	
	productPrice = productPrice.replace(/\s+/g," "); //convert multiple spaces to single space (which used for db sorting)
	productPrice = productPrice.replace(/[']+/g,""); //remove apostrophes
	p = productPrice.split("$")
	var addBtnSrc
	if (document.cookie && getCookieValue("added").indexOf(productPrice)>-1 && parseInt(norder(productPrice))>0){
		addBtnSrc="images/interface/added.gif"
	}else{
		addBtnSrc="images/interface/add.gif"
	}
	
	var fontStart = "";
	var fontEnd = "";
	if (location.pathname.indexOf("Specials.") > -1){
		var fontStart = "<font color=\"red\">"
		var fontEnd = "</font>"
	}
	var tabindex = ""
	var ct = 1
	if (recentBrowser){
		document.write('<td><b>' + fontStart + p[0] + fontEnd + '</b></td><td align="center"><b>' + fontStart +'$'+ p[1] + fontEnd + '</b></td><td><input type="text" id="qty" class="text-align-center" size="2" name="'+productPrice+'" value="'+norder(productPrice)+'" maxlength=2 onChange="orderIt(\''+productPrice+'\');"  style="border:solid blue 1px" ></td><td><img src="' + addBtnSrc + '" alt="Add" onMouseover="if(this.src.indexOf(\'added\')==-1){this.src=\'images/interface/add_over.gif\'}" onMouseout="if(this.src.indexOf(\'added\')==-1){this.src=\'images/interface/add.gif\'}" onClick="doAdd(this)"  name="' + productPrice + '$add" style="cursor:hand"></td>');
	}else{
		document.write('<td><b>' + p[0] + '</b></td><td align="center"><b>$' + p[1]+'</b></td><td><input type="text" class="text-align-center" size="2" name="'+productPrice+'" value="'+norder(productPrice)+'" maxlength=2 onChange="orderIt(\''+productPrice+'\');"></td><td><a href="javascript: void(0); var thisItem = \'' + productPrice + ' \'; var currqty = parseInt(document.forms[0].elements[\''+productPrice+'\'].value); if(currqty>0){alert(currqty + \' x \' + thisItem + \'  Was Added \')}else{alert(\'Item was not added OR was removed from order. \nPlease enter a Valid Qty if you wish to order this item\')}"; history.go(0)><img src="' + addBtnSrc + '" alt="Add" border="0" name="' + productPrice + '$add"></a></td>');
	}
}

function doAdd(addImgObj){
	var qtyfieldbits = addImgObj.name.split("$")
	var qtyfieldname = qtyfieldbits[0]+"$"+qtyfieldbits[1]
	var qtyEntered = document.forms[0].elements[qtyfieldname].value
	if (parseInt(qtyEntered) > 0){
		var currentAdded = getCookieValue("added");
		document.cookie="added="+currentAdded+"|"+qtyfieldname; //store which Add btn is in Added state in a cookie
		addImgObj.src="images/interface/added.gif";
		window.status=qtyfieldbits[0]+" x "+qtyEntered+"  ADDED to ORDER."
	}else{
		addImgObj.src="images/interface/add.gif";
		alert("\n\Item was not added OR was removed from Order. \nPlease enter a valid quantity if ordering this item. \n.")
	}
//removed: style="cursor:hand", style="border: solid blue 1px" for NN4- script in with Browser detection for IE 5+ later
}

function norder(productPrice) {
sep = "\n";
fsep = "$";
cname = "interPetOrder";
order = unescape(getCookieValue(cname)).split(sep);
var n="";
for (i=0;i<order.length;i++) {
if (order[i].indexOf(productPrice)==0)  {
 fields = order[i].split(fsep);
  n = fields[2];
  }
}
//document.interpet[productPrice].value =n;
return n;
}

function forcenorder() {
/*
sep = "\n";
fsep = "$";
cname = "interPetOrder";
order = unescape(getCookieValue(cname)).split(sep);
for (i=0;i<document.interpet.length;i++) {
if (document.interpet[i].name.indexOf(fsep)>0) document.interpet[i].value = "";
}
for (i=0;i<order.length;i++) {
if (order[i].length > 0) {
fields = order[i].split(fsep);
productPrice = fields[0]+fsep+fields[1];
document.interpet[productPrice].value=fields[2];
}
   }
   */
}

/*
order display

*/

function showOrder() {

sep = "\n";
fsep = "$";
cname = "interPetOrder";
iname = "interPetStatus";
curstat = unescape(getCookieValue(iname));

curorder = unescape(getCookieValue(cname));
//alert(curorder);
order =  unescape(getCookieValue(cname)).split(sep);
total =0;
ct=0;
document.write('<table width="100%"><tr><td><B>Product</B></td><td align="center"><B>Cost</B></td><td align="center"><B>Qty</B></td><td align="center"><B>Total</B></td><td></td></tr>\n');
document.write('<tr><td colspan=4><Hr size=1></td></tr>\n');
for (i=0;i<order.length;i++) {
if (order[i].length > 0 && curorder != "undefined") {
 p = order[i].split(fsep);
 productPrice = p[0]+fsep+p[1];

if (recentBrowser){
 	document.write('<tr><td><A HREF="'+p[3]+'"><img src="images/interface/goto.gif" border="0" alt="go to page"></A> '+p[0]+' </td><td align="Right">$'+twodp(p[1])+'</td><td align="center"><input type="text" class="text-align-center" size=2 style="border: solid blue 1px" name="'+productPrice+'" value="'+p[2]+'" maxlength=3 onChange="orderUpdate(\''+productPrice+'\');"> <img align="top" src="images/interface/change.gif" alt="change" style="cursor:hand" onMouseover="this.src=\'images/interface/change_over.gif\'" onMouseout="this.src=\'images/interface/change.gif\'"></td><td align="Right">$'+twodp(p[1]*p[2])+'</td><td></td>');
}else{
 	document.write('<tr><td>'+p[0]+'</td><td align="Right">$'+twodp(p[1])+'</td><td align="center"><input type="text" class="text-align-center" size=2 name="'+productPrice+'" value="'+p[2]+'" maxlength=3 onChange="orderUpdate(\''+productPrice+'\');"> <img valign="top" src="images/interface/change.gif" alt="change" onMouseover="this.src=\'images/interface/change_over.gif\'" onMouseout="this.src=\'images/interface/change.gif\'"></td><td align="Right">$'+twodp(p[1]*p[2])+'</td><td><A HREF="'+p[3]+'"><font size=-1>go to</font></A></td>');
}

v = p[0]+'$'+twodp(p[1])+'$'+p[2]+'$'+twodp(p[1]*p[2]);
 document.write('<input type="hidden" Name="OrderDisplay_'+i+'" VALUE="'+v+'">');
 total +=p[1]*p[2];
 ct += parseInt(p[2]);
 }}
  if (ct > 0) {
  cs= unescape(getCookieValue("interPetStatus")).split(",");
  if (cs.length!=6) {
  	statusUpdate("No");
  	cs= unescape(getCookieValue("interPetStatus")).split(",");
  	}
  	hasins = (cs[0]*1==1);
   nominalins = cs[1]*1+cs[3]*Math.ceil(total/cs[2]*1);
   (hasins)?ins = nominalins:ins=0;
  (total<cs[5]*1)?freight = cs[4]*1:freight=0;

  fullcost = total+freight;
  document.write('<tr><td colspan=4><Hr size=1></td></tr>\n');
     document.write('<tr><td colspan=2>Product Totals</td><td align="center">'+ct+' &nbsp;  items</td><td align="Right">$'+twodp(total)+'</td></tr>\n');
     document.write('<tr><td colspan=4><Hr size=1></td></tr>\n');
     document.write('<tr><td colspan=3>Freight Charge <font size=1>(free over $275)</font></td><td align="Right">$'+twodp(freight));
     document.write('<input type="hidden" name="Pieces" value="'+ct+'"><input type="hidden" name="subTotal" value="'+twodp(total)+'"><input type=hidden name ="Freight" value="'+twodp(freight)+'"></td></tr>\n');
     
     if (hasins) {
     document.write('<tr><td colspan=3><A HREF="javascript:insuranceNote();" title="Pop-up --- Why Insurance?">Insurance</A>&nbsp;No<input type="Radio" name="insurance" value="No" onClick="statusUpdate(\'No\');">&nbsp; Yes<Input type="Radio" name="insurance" value="Yes" checked onClick="statusUpdate(\'Yes\');"></td><td align="Right">$'+twodp(ins)+'<input type=hidden name ="Insurance" value="'+twodp(ins)+'"></td></tr>\n');
     fullcost += ins;
     }else {
        document.write('<tr><td colspan=3><A HREF="javascript:insuranceNote();" title="Pop-up --- Why Insurance?">Insurance</A>&nbsp;No<input type="Radio" name="insurance" value="No" checked onClick="statusUpdate(\'No\');" >&nbsp;<font size=1></font>&nbsp;Yes<Input type="Radio" name="insurance" value="Yes" onClick="statusUpdate(\'Yes\');"> ($'+twodp(nominalins)+')</td><td align="Right">$'+twodp(0)+'<input type="hidden" name="Insurance" value="0"></td></tr>\n');
   
     }
 document.write('<tr><td colspan=4><Hr size=3 noshade></td></tr>\n');
 document.write('<tr><td colspan=2><b>Full Cost</b></td><td></td><td align="Right">$'+twodp(fullcost)+'<input type="hidden" name ="orderTotal" value="'+twodp(fullcost)+'"></td></tr></table>\n');
  } else 
    { document.write('<tr><td>No current order</td></tr></table>\n'); }
}

function orderUpdate(productPrice) {
newn = parseInt(document.yourOrder[productPrice].value);
(isNaN(newn)) ? newv=0: newv=newn;
(newv>0) ? document.yourOrder[productPrice].value = newv : document.yourOrder[productPrice].value = "";
newcookie = "";
sep = "\n";
fsep = "$";
cname = "interPetOrder";
found = -1;
order = unescape(getCookieValue(cname)).split(sep);
for (i=0;i<order.length;i++) {
if (order[i].indexOf(productPrice)==0)  {
  
  found = 0;
  p = order[i].split(fsep);
 if (newv > 0) newcookie+=productPrice + fsep + newv + fsep + p[3] + sep;
 }
  else {

   if (order[i].length > 0)newcookie += order[i] + sep;
}

}
document.cookie=cname+"="+escape(newcookie)+"; path=/";
history.go(0);
}

var istub = ","+insurancechargeunit +"," + insurancedollarisper + "," + insuranceincrement + "," + freightcost + "," + freightchargecutout;
var withinsurance = "1" + istub;
var noinsurance = "0" + istub;

function statusUpdate(x) {
(x=="Yes")?plist=withinsurance: plist=noinsurance;
document.cookie="interPetStatus="+escape(plist)+"; path=/";
history.go(0);
}


// use this for adjusting the number ordered or ordering a new item or deleting an ordered item
function orderIt(productPrice) {

newn = parseInt(document.interpet[productPrice].value);
(isNaN(newn)) ? newv=0: newv=newn;
(newv>0) ? document.interpet[productPrice].value = newv : document.interpet[productPrice].value = "";
newcookie = "";
sep = "\n";
fsep = "$";
cname = "interPetOrder";
found = -1;
order = unescape(getCookieValue(cname)).split(sep);
for (i=0;i<order.length;i++) {
if (order[i].indexOf(productPrice)==0)  {

  found = 0;
 if (newv > 0) newcookie+=productPrice + fsep + newv + fsep + document.location.pathname + sep;
 }
  else {

   if (order[i].length > 0 && order[i] != "undefined")newcookie += order[i] + sep;
}

}
if (found==-1 && newv !=0) newcookie += productPrice + fsep + newv + fsep + document.location.pathname + sep;

document.cookie=cname+"="+escape(newcookie)+"; path=/";
}
function twodp(n) {
   //if (is.js < 1.1) {
   n+=.005;
     ns = " "+n;
    ns = ns.substring(1,ns.length);
 //   }
  //else {
  //  var ns = n.toString();
 //  }
    var dp = ns.indexOf(".");
   if (dp < 0) ns = ns+".00";
   else if (dp == ns.length-2) ns = ns+"0";
   else if (dp < ns.length-1) ns = ns.substring(0,dp+3);
   return ns;
}

 function insuranceNote() {
 window.open("insurance.html","insurance","HEIGHT=250,WIDTH=500,status=0,resizable=1,scrollbars=0,menubar=0");
 }
 
 
 CookieWarning();
//alert(document.cookie);
//forcenorder();
