// Requires ListOnBC, set in _ClientVars and _Application 
var OldRow        = "";
var OldColor      = "";
var NoDel         = new Array();
var oStatus=window.status;

function LoadFlash(){ // Workaround for Flash in IE.  Define fv1-fv6 in LoadFlashVars.js, which is not shared.
 document.write(fv1 + '\n');
 document.write(fv2 + '\n');
 document.write(fv3 + '\n');
 document.write(fv4 + '\n');
 document.write(fv5 + '\n');
 document.write(fv6 + '\n');
 document.write('</object>\n');
}

function ShowStatus(theMess){
 if (document.entryform){
  if (document.entryform.DisplayStatus){
   document.entryform.DisplayStatus.value=theMess;
  }
 }
 oStatus = window.status;
 window.status = theMess;
 return true;
}

function RestoreStatus(){
 if (document.entryform){
  if (document.entryform.DisplayStatus){
   document.entryform.DisplayStatus.value=oStatus;
  }
 }
 window.status = oStatus;
 return true;
}

function HighlightRow(theID,newColor) {
 if (!newColor) newColor = ListOnBC;
 wasColor = document.getElementById(theID).style.backgroundColor;
 document.getElementById(theID).style.backgroundColor=newColor;
}

function RestoreRow(theID) {
 document.getElementById(theID).style.backgroundColor=wasColor;
}  

function DollarFormat(Num,Width,Cents) {
// Pass Cents as "CENTS" if you want to force the value returned 
// to contain cents even if Num does not.
// Pass Cents as "NO" if you want to round to the nearest $
	
	var theNumber = parseInt(Num);
	
// This version returns blank Num is NaN 
	if ( isNaN(theNumber)) {
		return " ";
	}

	end = "";
//	if (Width < Num.length) Width = 0;
	
	if (Cents == "NO") {
		Num = "" + Math.round(Num);
		
	}else{
		dec = Num.indexOf(".");
		if (dec > -1) end += Num.substring(dec,Num.length);
		
		if (end.length >3) {
			var theCents = Math.round(end.substring(1,3) + "." + end.substring(3,end.length));
			end = "." + theCents;
		}
			 
		if ( (end.length >1 && end.length <3) || Cents == "CENTS"){
			trailing = ".00";
			end += trailing.substring(end.length,3);
		}
		
		Num = "" + theNumber;
	}
	
	var temp1 = "";
	var temp2 = "";
	var count = 0;
	for (var k = Num.length-1; k >= 0; k--) {
		var oneChar = Num.charAt(k);
		if (count == 3) {
			temp1 += ",";
			temp1 += oneChar;
			count = 1;
			continue;
		}else {
			temp1 += oneChar;
			count ++;
	   }
	}
	
	for (var k = temp1.length-1; k >= 0; k--) {
		var oneChar = temp1.charAt(k);
		temp2 += oneChar;
	}
	
	var TotLength = temp2.length +end.length;
	
	if (TotLength < Width){
		var Spaces = "            ";
		temp2 = Spaces.substring(1,Width -TotLength) +temp2;
	}
	temp2 = "$" + temp2 + end;
	return temp2;
}

function MakeNumber(theStr){
 var DollarSpot= theStr.indexOf("$");
 
 if (DollarSpot>=0){
  var stripped = theStr.substring(DollarSpot +1);
 }else{
  var stripped = theStr;
 }
 
 var aStr = stripped.split(",");
 var PureNum="";
 for (var i = 0; i < aStr.length; i++) {
  PureNum = PureNum +aStr[i];
 }
 
 return parseFloat(PureNum);
}

////////////////////////////////////////////////////////////////////   ORDERING and SHIPPING
/*
function UpdateOrder(){
 var isChanged = document.Orders.isChanged.value;
 theURL = "../UpdateOrder.cfm?gotoURL=Checkout.cfm&isChanged=" +isChanged;
 window.location=theURL;
} 
*/
function Add2Order(prodID){
 var prodField="Add" +prodID;
 theGuest = document.getElementById(prodField).value;
 if (!theGuest){
  alert("You are using either a very old or non-standard browser, which cannot place a secure order.  Please call " +OrderPhone+ " to place your order over the telephone.");
  return false;
 }
 
 if (theGuest=="ADD2ORDER") return false; // User hits back after selecting item for an order then selects Add to Order.
 
 if (theGuest=="New"){
  theNextURL = "MyAccount/GuestList.cfm?action=ADD&gotoURL=" +thisURL+ "&NewOrder=" +prodID;
 }else{
  theNextURL = "AddOrder.cfm?gotoURL=" +thisURL+ "&p=" +prodID +"&g=" +theGuest;  
 }

 // If user selected an item that uses aXtra (cf array, e.g.,  used for Cranberry Creations) we have to set xField's value
  if (document.getElementById("x" +prodID+ "_1")){
  	for (var i = 1;i <=3; i++) {
    xField = document.getElementById("x" +prodID+ "_" +i);
    if (xField){       // For pages on which we mix Cranberry Creations with other contents
     if (xField.checked){xValue=xField.value;}
    }
   }
   theNextURL = theNextURL +"&x=" +xValue;
  }
 
 window.location=theNextURL;
 return true;
}

function MustBeNumber(theID){
 theVal = document.getElementById(theID).value;
 if (isNaN(theVal)){
  alert("Please Enter a Number");
  document.getElementById(theID).select();
  return false;

 }else{
  OrderChanged();
  return true;
 }
}

function OrderChanged() {
 document.Orders.isChanged.value=1;
}

function SetShipCost(theKey){
 prodField  = "prod"  +theKey;
 shipField  = "ship"  +theKey;
 orderField = "order" +theKey;
 scField    = "sc"    +theKey;
  
 prodTotal  = MakeNumber(document.getElementById(prodField).value);
 shipValue  = document.getElementById(shipField).value;

 Dols=shipValue.indexOf("$")
 if (Dols >0){
  aShip    =shipValue.split("$");
  ShipCost =aShip[1];
 }else{
  aShip    =shipValue.split("%");
  ShipCost =Math.round(aShip[1]*prodTotal);
 }
 newOrderTot = prodTotal +parseFloat(ShipCost);

 document.getElementById(scField).value    = DollarFormat(ShipCost+"",10,"CENTS");
 document.getElementById(orderField).value = DollarFormat(newOrderTot+"",10,"CENTS");
 OrderChanged();
}

//////////////////////////////////////////////////////////////////  WINDOWS
function PictureWindow(theImage,theTitle,theDetails,theBorder) {
 theURL="PictureWindow.cfm?Image=" +theImage
      + "&imgBorder=" +theBorder
      + "&Title=" +theTitle;

 if (theDetails){
  theURL=theURL +"&DetailsURL=" +theDetails;
 }

	if (window.picture_window) {picture_window.close();}

	picture_window = window.open(theURL,"LargePicture","resizable,width=425,height=425");
	picture_window.camefrom = self;
}

function PortraitWindow(theImage,theTitle) {
 theURL="PortraitWindow.cfm?Image=" +theImage
      + "&Title=" +theTitle;

	if (window.portrait_window) {portrait_window.close();}

	portrait_window = window.open(theURL,"LargePortrait","resizable,width=425,height=475");
	portrait_window.camefrom = self;
}
 
function HelpWindow(theURL) {
	if (!window.Help_window||Help_window.closed) { 
		Help_window = window.open(theURL,"Help","scrollbars,resizable,width=600,height=400");
	}else{
		Help_window.location =theURL;
		Help_window.focus();
	}	
	Help_window.camefrom = self;
 return false;
}
 
function HelpWindowLg(theURL) {
	if (!window.Help_window||Help_window.closed) { 
		Help_window = window.open(theURL,"Help","scrollbars,resizable,width=640,height=440");
	}else{
		Help_window.location =theURL;
		Help_window.focus();
	}	
	Help_window.camefrom = self;
 return false;
}

////////////////////////////////////////////////////////////////////   MM
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
