// This is a common library of scripting functions for use throughout
// the web site.


// Extract the value from the cookie at the given offset.

function GetValue( Offset )
{
var Doc = window.opener.top.frames(0).document;
var End;
End = Doc.cookie.indexOf (";", Offset);
if( End == -1 )
End = Doc.cookie.length;

// Return the portion of the cookie beginning with the offset
// and ending with the ";".

return unescape( Doc.cookie.substring( Offset, End) );
}

function GetCookie( Name )
{
var Len = Name.length;
var Doc = window.opener.top.frames(0).document;
// Look at each substring that's the same length as the cookie name
// for a match.  If found, look up the value and return it.

var i = 0;
while( i < Doc.cookie.length )
{
var j = i + Len + 1;
if( Doc.cookie.substring( i, j) == (Name + "=") )
return GetValue( j );
i = Doc.cookie.indexOf( " ", i ) + 1;
if( i == 0)
break;
}
var a = "";
return a;
}

function GetCookies()
{

userid.value = GetCookie( "username" );
pw.value = GetCookie( "password" );
auth_remember_login.value = GetCookie( "remember_login" );

if ( auth_remember_login.value == "on" ) { 
auth_remember_login.checked = true; }

}

// generic order function 
function order(item,price,weight)
{
//	var price = 100;
	var targ = "./cgi-bin/order.cgi";
//	window.alert("before");
	targ += "?" + GetCookie("userid");  //append the order id
//	window.alert("after");
	targ += "&" + item;  //append the item to order
	targ += "&" + price;  //append the price
	targ += "&" + weight;  //append the weight
	window.open(targ, "catalog", "");
}

function remove(item,id)
{
	var targ = "./unorder.pl";

	targ += "?" + id;  //append the order id
	targ += "&" + item;  //append the item to order
	window.open(targ, "_self", "");
//	targ = "./placeorder.cgi";
//	targ += "?" + id;  //append the order id
//	window.open(targ, "main", "");
}

function cancelOrder(id)
{
	var targ = "./cancel.cgi";
	targ += "?" + id;
	window.open(targ, "main","");
	window.close();
}

function view_order()
{
	var targ = "./cgi-bin/placeorder.cgi";
	targ += "?" + GetCookie("userid");  //append the order id

	window.open(targ, "_self", "");

}

function updateQuantity()
{
	var orderform;

	if(window.navigator.userAgent.indexOf("MSIE") != -1) {
		orderform = window.document.all("form1");
	}
	else {
		orderform = window.document.form1;
	}

	orderform.action = "http://www.thegardenpond.com/cgi-bin/wholeorder.cgi";
	orderform.target = "_self";
	orderform.submit();

}

function addItem()
{
	window.open("../catalog.htm","catalog", "");
}

function checkout()
{
	var orderform;
	if(window.navigator.userAgent.indexOf("MSIE") != -1) {
		orderform = window.document.all("form1");
	}
	else {
		orderform = window.document.form1;
	}
	orderform.submit();
}


function IsValid()
{
	var formdata = window.document.forms.item("form1");

	for(var i=0;i<formdata.elements.length;i++)
	{
		if((formdata.elements(i).name != "month") && (formdata.elements(i).name != "year") && (formdata.elements(i).name != "creditcard")) {
		     if((formdata.elements(i).value == "") && (formdata.elements(i).name != "address2")){
			window.alert(formdata.elements(i).name + "-" + formdata.elements(i).value);
			window.alert("Form must be completed");
			return;}
		}
		else if((formdata.elements(i).name == "month" && formdata.elements(i).selectedIndex == 0) ||
			(formdata.elements(i).name == "year" && formdata.elements(i).selectedIndex == 0)){
			window.alert("Invalid Expiration Date");
			return;}
	}
	formdata.submit();

}

function openCat(targ) {
	var h = .8*window.screen.availHeight-100;
	var options = "screenX=20, screenY=20, width=720, height=";

	options += h + ", menubar=1, scrollbars=1, location=1, toolbar=1, status=1, resizeable=1";
/*	if(targ.indexOf("cgi") != -1){
		window.top.alert("before");
		targ += "?" + GetCookie("userid");  //append the order id
		window.top.alert(targ);
	}
*/
	window.top.open(targ, "catalog", options);
}

function cat_resize()
{
	var h = window.screen.availHeight;
	var w = window.screen.availWidth;
	var new_win;


	if ((window.navigator.userAgent.indexOf("aol")== -1)||(window.navigator.userAgent.indexOf("AOL") == -1)){
		window.focus();	
		window.resizeTo(720,.9*h);
		window.moveTo(20,20);
	}
}


function win_resize()
{
	var h = window.screen.availHeight;
	var w = window.screen.availWidth;
	var myImg = window.event.srcElement;
	var new_win;
	var iW;
	var iH;
	
		
	
	iW = myImg.width;
	iH = myImg.height;
	var ratio;
	if( iH > iW)
		ratio = (h*.8)/iH;
	else
		ratio = (w*.8)/iW;
		
	iH *= ratio;
	iW *= ratio;		
	
	var targ = myImg.src;
	targ = targ.substring(0,targ.indexOf("thmb.jpg")) +".jpg";
//	window.alert(iH +"x"+ iW);

	new_win = window.top.open(targ,"viewer","menubar=0,toolbar=0,location=0,status=0,scrollbars=0");
	new_win.focus();
	new_win.resizeTo(iW+30,iH+70);
	new_win.moveTo(20,20);
	var targImg = new_win.document.images.item(0);
	targImg.width = iW;
	targImg.height = iH;
	targImg.hspace = 0;
	targImg.vspace = 0;
	
}
		
function load_gallery()
{
	var ratio;
	var cells;
	
//	window.alert(window.screen.width);
	ratio = window.screen.width / 1280;
	
	if(window.navigator.userAgent.indexOf("MSIE") != -1){
		window.alert("using MSIE");
		cells = window.document.all.tags("td");}
	else {
		window.alert("using netscape");
		cells = window.document.tags.all.tags("td");}
//	window.alert(cells.length);
	var imgs = window.document.images;

	for(var i=0;i<imgs.length;i++)
	{
		imgs[i].width *= ratio;
		imgs[i].height *= ratio;
	}
	for(var i=0;i<cells.length;i++)
	{
		cells[i].style.width = 160 * ratio + 15;
		cells[i].style.height = 160 * ratio + 15;
	}

}

function resize_fish()
{
	var correction=0;
	var ratio;
	var winsize;
	var width = window.screen.width;
	ratio = window.screen.width / 1280;	

	winsize = (1280 - 564)*ratio;
	if(width==800)
	{
		correction = 6;
		winsize = (1280 - 564)*ratio;}
	else if(width==1024)
	{
		correction = 3;
		winsize = (1280 - 551)*ratio;}
	else if(width==1280)
	{
		correction = 0;
		winsize = (1280 - 546)*ratio;}
	else if(width==1600)
	{
		correction = -2;
		winsize = (1280 - 540)*ratio;}

	ratio = winsize / 500;

	var img;
	var introP;
	var linksP;
	if(window.navigator.userAgent.indexOf("MSIE") != -1){
		img = window.document.images.item("fish");
		window.document.all("orderlink").href = window.top.frames[3].document.all("orderLink").href;
		window.document.all("pics").href = window.top.frames[3].document.all("pics").href;
		introP = window.document.all("intro");
		linksP = window.document.all("links");
	}
	else {
		img = window.document.fish;
		window.alert(window.top.frames[3].name);
		if(window.top.frames[3].document.readyState == "complete") {
		window.document.links.orderlink.href = window.top.frames[3].document.links.orderlink.href;
		window.document.links.pics.href = window.top.frames[3].document.links.pics.href;
		//introP = window.document.intro;
		//linksP = window.document.links;
		}
	}

	var size = 500*ratio;
	img.style.width =  size +"px";
	size = 640*ratio;
	img.style.height = size +"px";
	if(window.navigator.userAgent.indexOf("MSIE") != -1){
		introP.style.width ="100%";
		linksP.style.width ="100%";
		introP.style.top = size/3 +"px";
		linksP.style.top = size-70 + "px";}

	if(window.navigator.userAgent.indexOf("AOL") != -1){
		var size = (460 - correction)*ratio;
		img.style.width =  size +"px";
	}

}

function load_system()
{
	var ratio;
		
//	window.alert(window.screen.width);
	ratio = window.screen.width / 1024;
	var imgs = window.document.images;

	for(var i=0;i<imgs.length;i++)
	{
		// imgs[i].width *= ratio;
		imgs[i].height *= ratio;
	}

}
