function nothing() {}

function imgswap(img,ref){
  if (document.images) {
        document.images[img].src = eval(ref+".src");
  }
}

function glomSpec() { }
function nothing() { }

// generic window open function
function openWin(url,name,w,h,sb,resize) {
 var popUp = window.open(url,name,'width='+w+',height='+h+',scrollbars='+sb+',resizable='+resize);
}

// open the site editor window
function openEditor(url) {
	var popup = window.open('http://' + url + '/admin/login.shtml','editor','width=600,height=500,scrollbars=yes,resizeable=no,location=no,status=no');
}

// open the site plate preview
function openPreview(url) {
	var popup = window.open(url,'preview','width=600,height=500,scrollbars=yes,resizeable=no,location=no,status=no');
}

// open the PILOT: SCRATCH
function openScratch(url) {
	var popup = window.open(url,'scratch','width=700,height=500,scrollbars=yes,resizeable=no,location=no,status=no');
}

// open the PILOT: INFO
function openInfo(url) {
	var popup = window.open(url,'info','width=600,height=400,scrollbars=yes,resizeable=no,location=no,status=no');
}

// open the PILOT: QUEUE
function openQueue(url) {
	var popup = window.open(url,'queue','width=400,height=600,scrollbars=yes,resizeable=no,location=no,status=no');
}


// insertSites function populates the site rating radio buttons with
// the actual ratings.  It compares the name of the radio to the name of
// the stored rating.  If there is no match, the name and rating pair are
// not displayed.
// This function is used in the Specify stage.
function insertSites(form) {
	var obj = document.forms[form];
	var numSites = obj.elements['totalSites'].value;
	var siteField = obj.elements['op2.sites'].value;
	var siteArray = new Array();
	var innerSiteArray = new Array();
	var thisSite = '';
	var a = 0;
	var thisElement;
	
	siteArray = siteField.split(',');
	siteArrayLen = siteArray.length - 1; // trim excess array element due to extra ',' in string.

	for(i=0;i<siteArrayLen;i++) {
		innerSiteArray = siteArray[i].split(':');
		siteArray[i] = innerSiteArray;
	}

	for(i=0;i<numSites;i++) {
		thisSite = 'site'+(i+1);
		
		for(x=0;x<siteArrayLen;x++) {
			a = 0;
			while (a < 5) {
				if (obj.elements[thisSite][a].value.indexOf(siteArray[x][0]) > -1) {
					thisElement = obj.elements[thisSite][a];
					if (thisElement.value.indexOf(siteArray[x][1]) > 0) {
						thisElement.checked = true;
					}
				}
				a++;
			}
		}
		
	}
}

// the glomSites function makes a single string out of several form elements
// and assigns them to a single field.
// This is used in the Specify stage.
function glomSites(form) {
	var obj = document.forms[form];
	var numSites = obj.elements['totalSites'].value;
	var fieldString = "";
	var thisSite = "";
	var a = 0;

	for(i=1;i<=numSites;i++) {
		a = 0;
		thisSite = 'site'+i;
		while (a < 5) {
			if (obj.elements[thisSite][a].checked) {
				fieldString += obj.elements[thisSite][a].value+",";
			}
			a++;
		}
	}
 obj.elements['op2.sites'].value = fieldString;
}

// glomHotCold puts the values of all hot/cold questions in a string
// and submits them as the field 'words'.
// This is used on the Specify page.
function glomHotCold(form,num) {
	var obj = document.forms[form];
	var returnString = "";
	var thisSelect = "";
	
	for(i=1;i<=num;i++) {
		thisSelect = 'hotCold'+i;
		if (obj.elements[thisSelect].value != "") {
			returnString += i+":"+obj.elements[thisSelect].value+",";
		}
	}
	
	obj.elements['op2.words'].value = returnString;
}

// setHotCold sets all 20 hot/cold pop-ups.
// This is used on the Specify page.
function setHotCold(form) {
	var obj = document.forms[form];
	var wordField = document.forms[form].elements['op2.words'].value;
	var thisSelect = "";
	var wordArray = new Array();
	var innerWordArray = new Array();
	var wordArrayLen = 0;
	
	wordArray = wordField.split(',');
	wordArrayLen = wordArray.length - 1;
	
	for(i=0;i<wordArrayLen;i++) {
		innerWordArray = wordArray[i].split(':');
		wordArray[i] = innerWordArray;
		setPop('hotCold'+wordArray[i][0],wordArray[i][1],form);
	}

}

// valNumChecked validates the number of checked checkboxes against
// the argument 'max'.  The argument 'field' is the checkboxes it validates
// the argument 'rfield' is the field where the results are returned.
// This is used in the Specify stage.
var autoCheck = -1;

function valNumChecked(max,form,rfield) {
var c = 0;
var obj = document.forms[form];
var len = obj.elements.length;
var rVal = "";
max = Number(max);
var incNum;
var autoChecked = false;
var doneMsg = false;

for (i=0;i<len;i++) {
	
	if (Number(obj.elements[i].value) == autoCheck && autoCheck > 0) {
		obj.elements[i].checked = true;
		rVal += obj.elements[i].value+",";
		incNum = 1
		c += incNum;
		autoCheck = -1;
		autoChecked = true;
	}
	
	if (obj.elements[i].checked && obj.elements[i].name.length > 0) {
	
		if (!autoChecked) {
			if (c <= max) {
				if(obj.elements[i].type.indexOf("radio") > -1) {
					incNum = Number(obj.elements[i].alt);
					c += incNum;
					rVal += obj.elements[i].value+",";
				} else {
					incNum = 1;
					c += incNum
					rVal += obj.elements[i].value+",";
				}
			
			}
	 	}
	 	
		if (c > max) {
			obj.elements[i].checked = false;
			c -= incNum;
			if (!doneMsg) {
				if (form == "goalForm") {
					alert("Too many arrows miss many targets.  Please limit your choices to the one or two most important goals.");
				} else if (form == "featureForm") {
					alert("Please limit your choices to the seven most important aspects.");
				} else if (form == "addFeatureForm") {
					alert("Please limit the number of additional features to 4 or less.");
				}
				doneMsg = true;
			}
		} 
		
		autoChecked = false;
	}
}

if (c == 0) {
	rVal = "";
}

document.forms['defineForm'].elements[rfield].value = rVal;
}

function unCheckRadios(radioname, form, max, rfield, status) {
	
	if (!status) {
		var obj = document.forms[form];
		var radiosLen = eval("obj." + radioname + ".length");
		var radios = eval("obj." + radioname);
		for (i = 0; i < radiosLen; i++) {
			radios[i].checked = false;
		}
	}
			
	valNumChecked(max,form,rfield);	
}

// The setChecked function sets the checked items in a list of checkboxes.
// This is used in the Specify stage.
function setChecked(form,valField) {
	var obj = document.forms[form];
	var checkedArray = new Array();
	checkedArray = valField.split(',');
	var len = obj.elements.length;
	var checkedLen = checkedArray.length;
	var x = 0;
	if (valField != "") {
		while (x < checkedLen) {
			for(i=0;i < len;i++) {
				if (checkedArray[x] != "") {
					if (obj.elements[i].value == checkedArray[x]) {
						obj.elements[i].checked = true;
					}
				}
			}
			x++;
		}
	}
}

function openHelp(URL) {
var popup = window.open(URL,"Help","width=250,height=300,scrollbars=yes,resizable=no,location=no,status=no");
}

// glomAndGo is for Logging into the site. It makes a string that gets set as the location in the browser.
function glomAndGo(siteAddr) {
	if(document.forms['loginform'].elements['userID'].value != '' && document.forms['loginform'].elements['password'].value != '') {
		loginURL = 'http://' + document.forms['loginform'].elements['userID'].value + ':' + document.forms['loginform'].elements['password'].value + '@'+siteAddr;
		location.href = loginURL;
		
	}
}

// setPop takes the name of the 'popup' menu and sets it selected to the value 
// given in 'currentval', if 'currentval' exists in the popup array
var asIndex;
function setPop(popup,currentval,form) {

	for(g=0;g<document.forms[form].elements[popup].length;g++) {
		if(document.forms[form].elements[popup].options[g].value == currentval) {
			asIndex = g;
			break;
		}
		else asIndex = 0;
	}

	document.forms[form].elements[popup].selectedIndex = asIndex;

}

// setPop call is a setTimeout to call the real function.  This is a workaround for
// a Netscape issue.
function setPopCall(popup,currentval,form) {
	setTimeout('setPop(\''+popup+'\',\''+currentval+'\',\''+form+'\')',0);
}

// Cookie handling function, retreives a single cookie.
function getSingle(cval) {
	cval+= '';
	var theCookie = document.cookie;
	if(theCookie.indexOf(cval+'=') != -1) {
		// code to find individual cookie
		var tmpcval = cval+'=';
		var pos = theCookie.indexOf(tmpcval);
		var start = Math.round(pos) + Math.round(cval.length) + Math.round(1);
		var end = theCookie.indexOf(";",start);
		if(end == -1) end = theCookie.length;
		var theCVal = theCookie.substring(start,(end));
		theCVal = unescape(theCVal);
		return theCVal;
	} else {
		return false;
	}
}


// Cookie handling function, sets a single cookie to a value.
function setSingle(mode,val,section) {
	mode += '';
	expireDate = new Date();
	if(mode == 'add') expireDate.setMonth(expireDate.getMonth()+6);
	else if(mode == 'delete') expireDate.setMonth(expireDate.getMonth()-6);

	var x = ''+val;
	var cString = section+'=' + escape(x);
	cString += ';expires=' + expireDate.toGMTString();
	cString += ';path=/';
	document.cookie = cString;

}


// brand inserts the logo branding for a site based on cookie data.
function brand(where) {
	var pName = getSingle('pname');
	var output = "";
	if (pName == "") { pName = "intrinsix.net"; }
	if (where == "top") {
		
		output += '<td valign=\"top\" align=\"left\"><a href=\"http://mercury.intrinsix.net/web/index.shtml\"><img src=\"http://mercury.intrinsix.net/web/gifs/brands/'+pName+'/top-left.gif\" height=\"52\" alt="" border=\"0\" alt=\"Back to Home\" title=\"Click here to go to the home page.\"></a></td>';
		output += '<td valign=\"top\" align=\"left\"><img src=\"http://mercury.intrinsix.net/web/gifs/brands/'+pName+'/top-right.gif\" height=\"52\" alt=\"\"></td>';			
	
		return output;
		
	} else if (where == "bottom") {

		output += '<td valign=\"top\" align=\"left\"><a href=\"http://mercury.intrinsix.net/web/index.shtml\"><img src=\"http://mercury.intrinsix.net/web/gifs/brands/'+pName+'/bottom-left.gif\" height=\"29\" alt="" border=\"0\" alt=\"Back to Home\" title=\"Click here to go to the home page.\"></a></td>';
		output += '<td valign=\"top\" align=\"left\"><img src=\"http://mercury.intrinsix.net/web/gifs/brands/'+pName+'/bottom-right.gif\" height=\"29\" alt=\"\"></td>';			
	
		return output;	
		
	}
}


// saveAndGo() switches the returnPage value of the defineForm and then submits the form.
function saveAndGo(page) {
	var obj = document.forms['defineForm'];
	obj.elements['returnPage'].value =  page;
	obj.submit();
}
