/************************************************************
ON-COMPILE EXECUTABLES
*************************************************************/
var bName = navigator.appName;				
var bPlatform = navigator.platform;			
var bVer = parseInt(navigator.appVersion);	
var browser = "NS6";							
var doc = "";								
var sty = "";		
var ns6mod = "";
var documentReady = false;

var minBottom=441;
var pageCornerGap= 10;

// window width & height used to handle NN resize bug	
var NNResize_initWidth = 0;								
var NNResize_initHeight = 0;

// set up dhtml references depending on browser
if (document.all) {
	// MSIE 4+
	browser="IE";
	doc = "document.all.";
	sty = ".style";
	if (bPlatform=="MacPPC") {
		bVerStart = navigator.appVersion.indexOf("MSIE")+5;
		bVer = parseInt(navigator.appVersion.substr(bVerStart,5));
	} 
} else if (document.layers) {
	// Netscape Navigator 4+
	browser="NN";
	doc = "document.";
} else if (document.getElementById) {
	// Netscape 6
	browser="NS6";
	doc = "document.getElementById('";
	ns6mod = "')";
	sty = ".style";
	bVerStart = navigator.userAgent.lastIndexOf("/")+1;		
	bVer = parseInt(navigator.userAgent.substr(bVerStart,navigator.userAgent.length));
}

/************************************************************
initialization
*************************************************************/

function init() {
	// generic init function, called onload of all documents
    //   init_corners();
	
	// set ready status
	documentReady = true;
	
	// handle NS4.x resize bug
	if ((browser=="NN")&&(bVer==4)) {
		// store current window width & height
		NNResize_initWidth = window.innerWidth;
		NNResize_initHeight = window.innerHeight;
		window.captureEvents(Event.RESIZE);
		window.onresize = NNResize_fix();
	}
	//make sure not in checkout
	var boSecure = (document.location.href.indexOf("shopping_list/step")!=-1)?true:false;
}

/************************************************************
page layout
*************************************************************/

function init_corners() {
	// positions the rounded lower-right corner of the page at 32px below the bottom of the bottom-most layer
	var t=0;
	var h=0;
	var maxB=minBottom;		// max bottom - set to default minimum
	var divsArray = new Array();
	
	if (!(browser=="NN")) {
		divsArray = (browser=="IE")?document.all.tags("DIV"):document.getElementsByTagName("DIV");
		for (var i=0;i<divsArray.length;i++) {
			if (divsArray[i].id!="" && divsArray[i].id!="pcCorners") {
                                t = divsArray[i].offsetTop;
                                h = divsArray[i].offsetHeight;
				if ((t+h)>maxB) { maxB=(t+h); }
 //                               if (browser=="IE") { maxB +; }
			}
		}
	} else if (browser=="NN") {
		for (var i=0;i<document.layers.length;i++) {
			t=document.layers[i].top;
			h=document.layers[i].clip.bottom;
			if ((t+h)>maxB) { maxB=(t+h); }
		}
	}
	
	positionAt("pcCorners",0,maxB+pageCornerGap);
	setSize("pcBackground",800,maxB+pageCornerGap);
        var corner = getObjectStyle("pcCorners");;
		corner.display = "block";
        corner.visibility = "visible";
        corner.position = "absolute";
               
}


/************************************************************
DHTML
*************************************************************/
function getTop(obj) {
	var Ob = getObjectStyle(obj);
	return (browser=="IE")?Ob.pixelTop:parseInt(Ob.top);	// NN & NS6 same
}

function getHeight(obj) {
	var Ob = null;
	if (browser=="IE") {
		Ob = getObjectNoStyle(obj);
		return Ob.offsetHeight;
	} else if (browser=="NN") {
		Ob = getObjectStyle(obj);
		return Ob.clip.bottom;
	} else if (browser=="NS6") {
		Ob = getObjectStyle(obj);
		return parseInt(Ob.height);
	}
}

function getObjectStyle(obj) {
	return (typeof obj=="string")? eval(doc + obj + ns6mod + sty):obj;
}

function getObjectNoStyle(obj) {
	return (typeof obj=="string")? eval(doc + obj + ns6mod):obj;
}

function positionAt(obj, x, y) {
	var Ob = getObjectStyle(obj);
	if (browser=="NN") {
		Ob.moveTo(x,y);
	} else if (browser=="NS6") {
		Ob.left = x;
		Ob.top = y;
	} else {
		Ob.pixelLeft = x;
		Ob.pixelTop = y;
	}
}

function setSize(obj, x, y) {
	var Ob = getObjectStyle(obj);
	if (browser=="NN") {
		Ob.resizeTo(x,y);
	} else if (browser=="NS6") {
		Ob.width = x;
		Ob.height = y;
	} else {
		Ob.posWidth=x;
		Ob.posHeight=y;
	}
}

/************************************************************
rollovers
*************************************************************/
function getImgName(imgRef) {
	// returns a name based on the image reference given
	return (imgRef.indexOf("/")==-1)?imgRef.substring(0,imgRef.lastIndexOf(".")):imgRef.substring((imgRef.lastIndexOf("/")+1),imgRef.lastIndexOf("."));
}

function loadImages() {
	// loads X amount of images passed as a parameter, and gives them a unique name based on the src
	// the name will be referred to later in "changeImages"
	
	var tempImgName="";
	var nameEnd="";
	
	if (document.images) {
		for (i=0;i<loadImages.arguments.length; i++) {
			tempImgName = getImgName(loadImages.arguments[i]);
			eval(tempImgName + "= new Image()");
			eval(tempImgName + ".src='" + loadImages.arguments[i] + "'");
		}
	}
}

function changeImages(imgObjName,newImgRef,layerName) {
	// pass an empty string for layerName if the image isn't within a layer
	if (documentReady) {
		var obj="";
		var newImgObj = eval(getImgName(newImgRef));
		
		if (document.images) {
			if ((document.layers)&&(layerName!="")) {
				// NN
				obj = eval("document."+layerName+".document."+imgObjName);
			} else {
				// IE & NS6
				obj = eval("document."+imgObjName);			
			}
			obj.src=newImgObj.src;
		}
	}
}

/************************************************************
popups
*************************************************************/
function getPlacementStr(w,h) {
	var left = Math.round((screen.availWidth/2)-(w/2));
	var top = Math.round((screen.availHeight/2)-(h/2));
	if (browser=="NN") {
		var placementStr = "screenX="+left+",screenY="+top; 
	} else if (browser=="IE") {
		var placementStr = "left="+left+",top="+top;
	} else if (browser=="NS6") {
		var placementStr = "left="+left+",top="+top;
	}
	return placementStr;
}

function openBasicWindow(name,url,width,height,scrollbars,toolbar,statusbar,resize) {
	// opens a window with no extras, centered in screen, with unique name
	var placementStr = getPlacementStr(width,height);
	var x,y;
	
	// resize & scrollbar should be yes/no
	
	// set toolbar
	var tools = (toolbar=="tools")?"yes":"no";
	
	// set statusbar
	if (statusbar=="NSstatus") { 
		var status= ((bName=="Netscape")&&(bPlatform="MacPPC"))?"yes":"no";
	} else {
		var status = (statusbar=="status")?"yes":"no";
	}
	
	eval(name + "Window = window.open(url,'" + name + "', 'toolbar=" + tools + ",location=no,directories=no,status=" + status + ",scrollbars=" + scrollbars + ",resizable=" + resize + ",copyhistory=no," + placementStr + ",width=" + width + ",height=" + height+"')");                     

	// make sure window comes to the front
	eval(name + "Window.focus()");
	eval("setTimeout('"+name+"Window.focus()',50)");
	
	// move window to center after the fact (ie mac problem)
	if((bName=="Microsoft Internet Explorer")&&(bPlatform=="MacPPC")) {
		x = Math.round((screen.availWidth/2)-(width/2));
		y = Math.round((screen.availHeight/2)-(height/2));
		eval(name + "Window.moveTo(x,y)"); 
	}
}

function openMediumWindow(name,url) {
	// opens the standard medium-sized popup window (400 x 360), resizable and scrolling set to yes
	openBasicWindow(name,url,400,360,"yes","no","no","yes");
}

function openSmallWindow(name,url) {
	// opens the standard medium-sized popup window (400 x 360), resizable and scrolling set to yes
	openBasicWindow(name,url,240,180,"yes","no","no","yes");
}

function openSmallNoScrollWindow(name,url) {
	// opens the standard medium-sized popup window (400 x 360), resizable and scrolling set to no
	openBasicWindow(name,url,240,180,"no","no","no","no");
}

function getWindowName(url) {
	// returns a valid window name argument based on a given url
	var str="";
	
	// remove any non-letters from url to make name
	for (var i=0;i<url.length;i++) {
		if (((url.charCodeAt(i)>=65)&&(url.charCodeAt(i)<=90))||((url.charCodeAt(i)>=97)&&(url.charCodeAt(i)<=122))) {
			str += url.charAt(i);
		}
	}
	
	return str;
}

function newMainWindow(urlStr) {
	// this window is unique, so doesn't use openBasicWindow
	// gets 'mainFocuser' to focus the pop-up, because normal focus caused error in IE4.0PC
	var windowName = getWindowName(urlStr);

	URLStr = "/mixedgreens/util/mainFocuser.html?goto="+escape(urlStr);
	mainWindow = window.open(urlStr,windowName,"toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,resizable=yes,copyhistory=yes," + getPlacementStr(760,500) + ",width=760,height=500");           
	
	if((bName=="Microsoft Internet Explorer")&&(bPlatform=="MacPPC")) {
		var x = Math.round((screen.availWidth/2)-380);
		var y = Math.round((screen.availHeight/2)-250);
		mainWindow.moveTo(x,y);
	}
}

function launchWhatsThis(topic) {
	// launches a what's this? topic in the medium-sized window
	var boSecure = (document.location.href.indexOf("shopping_list/step")!=-1)?true:false;
	openMediumWindow("whats_this","/mixedgreens/util/whats_this_template.jhtml?topic=" + escape(topic) + "&boSecure=" + escape(boSecure));
}

/************************************************************
util
*************************************************************/

function redirect(url) {
	// redirects the browser to the given url
	document.location.href = url;
}

function pubToolLink(url) {
	// handles internal/external links for images added to a page by the publishing tool
	if (url!="") {
		// check for javascript pseudo-url
		if (url.indexOf("javascript:")!=-1) {
			eval(url);
		} else {
			if (url.indexOf("/mixedgreens")==-1) {
				// external
				newMainWindow(url);
			} else {
				// internal
				document.location.href=url;
			}
		}
	}
}

function removeHTML(str) {
	// removes any HTML tags from str
	var tagStart = str.indexOf("<");
	var tagEnd = str.indexOf(">");
	var newStr = "";
	
	while ((tagStart!=-1)&&(tagEnd!=-1)) {
		newStr = str.substring(0,tagStart) + str.substring(tagEnd+1,str.length);
		str = newStr;
		tagStart = str.indexOf("<");
		tagEnd = str.indexOf(">");
	}

	return str;
}

function truncate(str,len) {
	// truncates the given string to the given length
	return (str.length>len)?str.substring(0,(len-3)) + "...":str;
}

