// global variables used in all functions
debugMode = 0;
userAgent = window.navigator.userAgent;
browsVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);
var preloadFlag;
var imageSet = new Array();
var imageOver = new Array();
imageDir = 'images/';
var currentPage = '';

function setCurrentPage(arg){
	currentPage=arg;
}

function preloadImages(path) {
	if (path) {
		imageDir=path; }
	if ((document.images) && !preloadFlag) {
		if (debugMode) {alert("preload now " + currentPage);}
		imageNames = new Array("x1","x2","x3","x4","x5","x6","x7","xCurrent","xsample","subscribers-only");
		for (i=0;i<imageNames.length;i++) {
			imageOver[i] = newImagegif(imageDir,imageNames[i]+"_over");
			if (currentPage==imageNames[i]) {
				changeImage(imageNames[i],imageOver[i].src);}
		}
		preloadFlag = true;
		
	}
}

function newImagegif(path,arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = path+arg+".gif";
		rslt.name = arg;
		rslt.id = arg;
		if (debugMode) { alert("new img src : " + rslt.src); }
		return rslt;
	}
}

function findElement(n,ly) {
	if (document.getElementById) // current standard
	{
		it = document.getElementById(n);
	}
	else if (document.layers)	// Nav4 non-standard (not properly tested!)
	{
		curdoc = ly ? ly.document: document; // full document or the layered document
		it = curdoc[n];
		if (!it) {
			for (var i=0;i<curdoc.layers.length;i++) { 
				it = findElement(n,curdoc.layers[i]);
				if (it) return it;
			}
		}
	}
	else if (document.all)		// old IE 4 non-standard (not tested!)
	{
		it = document.all[n];
	}
	else if (browsVer<4) it = document[n];		// for IE or NN older pre version 4 (last try)
	return it;
}

function changeImage(imgId, newSrc) {
	if (document.images) {
		var img = null;
		if (document.layers) {img = findElement(imgId,0);}
		else {img = document.images[imgId];}
		if (img) {img.src = newSrc;}
	}
}

function rollOver(pos,imgId) {
	if (imageOver[pos]) {
		changeImage(imgId,imageOver[pos].src);
		if (debugMode) {alert(imageOver[pos].src);}
	}
	else if (debugMode) { alert("could not overlay img: "+imgId);}
}

function rollOff(pos,imgId) {
	if (debugMode && (imgId == currentPage)) { alert("current, no change");}
	if (imageOver[pos]&&(imgId!=currentPage)) {
		changeImage(imgId,imageOver[pos].src.substring(0,imageOver[pos].src.lastIndexOf('_'))+".gif");
		
	}
	else if (debugMode) {alert("no image change");}
}

function isNumeric(anystring) {
	var digit = "0123456789.";
	var isdigit = true;
	
	for (i=0; i<anystring.length && (isdigit == true); i++ )
	{
		if (digit.indexOf(anystring.charAt(i)) == -1) 
		{	isdigit=false; }
	}
	return isdigit;
}
	
function selectPPvalues() {
	if (isNumeric(document.Watershed_Sentinel_Subscribe_Form.SUBSCRIPTION_TYPE.value))
	{
		document.PPform.amount.value=document.Watershed_Sentinel_Subscribe_Form.SUBSCRIPTION_TYPE.value;
	}
	else
	{	document.PPform.amount.value=0; 
	}
		
	document.PPform.item_name.value = "Subscription: " + 
		document.Watershed_Sentinel_Subscribe_Form.SUBSCRIPTION_TYPE.options[document.Watershed_Sentinel_Subscribe_Form.SUBSCRIPTION_TYPE.selectedIndex].text;
		
	if (document.Watershed_Sentinel_Subscribe_Form.DONATION.value != "")
		{ if (isNumeric(document.Watershed_Sentinel_Subscribe_Form.DONATION.value))
			{	document.PPform.item_name.value += " (with donation)";
				document.PPform.amount.value = +document.PPform.amount.value + +document.Watershed_Sentinel_Subscribe_Form.DONATION.value;}
		}
	return;
}
