//
// JavaScript functions for Projects.
// Michael Benson - 1/12/05
//
//
// COPYRIGHT NOTICE
// Copyright 2006 Michael L. Benson. All rights reserved.
//
// This script may be used and modified free of charge provided that this
// copyright notice remains intact. By using this code you are agreeing to
// indemnify the author, Michael L. Benson, from any liability which might arise
// from its use.
//
// You may give copies of this script to any third party, but you may not sell
// this script for any reason whatsoever without the prior written consent of
// Michael L. Benson. 
//
// You must obtain prior written consent from Michael L. Benson if you intend to
// redistribute this script over the Internet or via any other medium.
//
// In all cases where this script is given to a third party, this copyright
// notice must remain intact and at the beginning of the script. At all times
// this script remains the property of Michael L. Benson.
//


var PAGEWIDTH = 740;

var ns4;
var ie4;
var ns6;

// Test for which browser
if (document.layers) ns4 = true;
if (document.all) ie4 = true;
if ((document.getElementById) && (!document.all)) ns6 = true;


function ShowDiv(which)
{
	if (ie4) {
		document.all[which].style.visibility = "visible";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "visible";
	}
	if (ns4) {
		document.layers[which].visibility = "show";
	}
}

function HideDiv(which)
{
	if (ie4) {
		document.all[which].style.visibility = "hidden";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "hidden";
	}
	if (ns4) {
		document.layers[which].visibility = "hide";
	}
}

function ShowDivs(divlist)
{
	var divs = divlist.split(",");
	for (i = 0;  i < divs.length;  i++) {
		ShowDiv(divs[i]);
	}
}

function HideDivs(divlist)
{
	var divs = divlist.split(",");
	for (i = 0;  i < divs.length;  i++) {
		HideDiv(divs[i]);
	}
}

function DisplayBlock(which)
{
	if (ie4) {
		document.all[which].style.display = "block";
	}
	if (ns6) {
		document.getElementById(which).style.display = "block";
	}
	if (ns4) {
		document.layers[which].display = "block";
	}
}

function DisplayNone(which)
{
	if (ie4) {
		document.all[which].style.display = "none";
	}
	if (ns6) {
		document.getElementById(which).style.display = "none";
	}
	if (ns4) {
		document.layers[which].display = "none";
	}
}

function ToggleExpando(formname, which, imageName, imageExpanded, imageCollapsed, more, less)
{
	divstate = "block";
	if (ie4) {
		divstate = document.all[which].style.display;
	}
	if (ns6) {
		divstate = document.getElementById(which).style.display;
	}
	if (ns4) {
		divstate = document.layers[which].display;
	}
	
	theState = eval("document." + formname + "." + which + "_state");
	if (divstate == "none") {
		DisplayBlock(which);
		SetImage(imageName, imageExpanded);
		theState.value = "expanded";
		if (more != "" && less != "") {
			if (document.all || document.getElementById) {
				cross_el = document.getElementById ?
					document.getElementById(which + "_moreless") :
					document.all[which + "_moreless"];
				cross_el.innerHTML = less;
			}
		}
	} else {
		DisplayNone(which);
		SetImage(imageName, imageCollapsed);
		theState.value = "collapsed";
		if (more != "" && less != "") {
			if (document.all || document.getElementById) {
				cross_el = document.getElementById ?
					document.getElementById(which + "_moreless") :
					document.all[which + "_moreless"];
				cross_el.innerHTML = more;
			}
		}
	}
}

function ToggleTree(formname, which, buttonName, buttonExpanded, buttonCollapsed, iconName, iconExpanded, iconCollapsed)
{
	divstate = "block";
	if (ie4) {
		divstate = document.all[which].style.display;
	}
	if (ns6) {
		divstate = document.getElementById(which).style.display;
	}
	if (ns4) {
		divstate = document.layers[which].display;
	}

	theState = eval("document." + formname + "." + which + "_state");
	if (divstate == "none") {
		DisplayBlock(which);
		SetImage(buttonName, buttonExpanded);
		SetImage(iconName, iconExpanded);
		theState.value = "expanded";
	} else {
		DisplayNone(which);
		SetImage(buttonName, buttonCollapsed);
		SetImage(iconName, iconCollapsed);
		theState.value = "collapsed";
	}
}

function SetDivContent(divID, content)
{
	if (ie4) {
		document.all[divID].innerHTML = content;
	}
	if (ns6) {
		document.getElementById(divID).innerHTML = content;
	}
	if (ns4) {
		document.layers[divID].innerHTML = content;
	}
}

function SetImage(imageName, imagePath)
{
	img = new Image();
	img.src = imagePath;
	document[imageName].src = img.src;
}

function ShowHideCountry(divName, countrySelectName)
{
	var selectbox = eval("document." + countrySelectName);
	country = selectbox.options[selectbox.selectedIndex].value;
	if (country == "other") {
		ShowDiv(divName);
	} else {
		HideDiv(divName);
	}
}

function ShowHideStates(divUS, divCanada, divOther, countrySelectName)
{
	var selectbox = eval("document." + countrySelectName);
	country = selectbox.options[selectbox.selectedIndex].value;
	if (country == "USA") {
		ShowDiv(divUS);
		HideDiv(divCanada);
		HideDiv(divOther);
	} else if (country == "Canada") {
		HideDiv(divUS);
		ShowDiv(divCanada);
		HideDiv(divOther);
	} else {
		HideDiv(divUS);
		HideDiv(divCanada);
		ShowDiv(divOther);
	}
}

var stateNames = [
	'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'American Samoa', 'California',
	'Colorado', 'Connecticut', 'Delaware', 'Washington D.C.', 'Florida', 'F.S. of Micronesia',
	'Georgia', 'Guam', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
	'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Marshall Islands',
	'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'N. Mariana Islands',
	'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
	'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
	'Ohio', 'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico',
	'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas',
	'Utah', 'Vermont', 'Virginia', 'Virgin Islands', 'Washington', 'West Virginia',
	'Wisconsin', 'Wyoming'
];

var provinceNames = [
	'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland',
	'Nova Scotia', 'Northwest Territories', 'Ontario', 'Prince Edward Island', 'Quebec', 
	'Saskatchewan', 'Yukon Territory', 'Nunavut'
];

var stateCodes = [
	'AL', 'AK', 'AZ', 'AR', 'AS', 'CA',
	'CO', 'CT', 'DE', 'DC', 'FL', 'FM',
	'GA', 'GU', 'HI', 'ID', 'IL', 'IN',
	'IA', 'KS', 'KY', 'LA', 'ME', 'MH',
	'MD', 'MA', 'MI', 'MN', 'MS', 'MP',
	'MO', 'MT', 'NE', 'NV', 'NH',
	'NJ', 'NM', 'NY', 'NC', 'ND',
	'OH', 'OK', 'OR', 'PW', 'PA', 'PR',
	'RI', 'SC', 'SD', 'TN', 'TX',
	'UT', 'VT', 'VA', 'VI', 'WA', 'WV',
	'WI', 'WY'
];

var provinceCodes = [
	'AB', 'BC', 'MB', 'NB', 'NF',
	'NS', 'NT', 'ON', 'PE', 'QC',
	'SK', 'YT', 'NU'
];

function ReFillStates(countrysel, statesel)
{
	var countrybox = eval("document." + countrysel);
	var statebox = eval("document." + statesel);
	country = countrybox.options[countrybox.selectedIndex].value;
	
	statebox.options.length = 0;
	statebox.options[0] = new Option("(whole country)", "all");
	if (country == "USA") {
		for (i = 0;  i < stateNames.length;  i++) {
			statebox.options[i+1] = new Option(stateNames[i] + " (" + stateCodes[i] + ")", stateCodes[i]);
		}
	} else if (country == "Canada") {
		for (i = 0;  i < provinceNames.length;  i++) {
			statebox.options[i+1] = new Option(provinceNames[i] + " (" + provinceCodes[i] + ")", provinceCodes[i]);
		}
	}
}

function CheckRangeRadio(name)
{
	var radioBtn = eval("document." + name);
	radioBtn[0].checked = false;
	radioBtn[1].checked = true;
}

function SetRadioGroup(name, maxnum, setnum)
{
	var radioBtn = eval("document." + name);
	for (i = 0;  i < maxnum;  i++) {
		radioBtn[i].checked = false;
	}
	radioBtn[setnum].checked = true;
}

function PageResults(formname, pagenum)
{
	theform = eval("document." + formname);
	theform.pagenum.value = pagenum;
	theform.submit();
	return true;
}

function SubmitForm(formname, pagename)
{
	theform = eval("document." + formname);
	theform.action = pagename;
	theform.submit();
	return true;
}

function ConfirmSubmitForm(formname, pagename, confirmmsg)
{
	msg = "Are you sure?";
	if (confirmmsg != "") {
		msg = confirmmsg;
	}
	if (!confirm(msg)) {
		return false;
	}

	theform = eval("document." + formname);
	theform.action = pagename;
	theform.submit();
	return true;
}

function SubmitFormSetVar(formname, pagename, varname, varvalue)
{
	theform = eval("document." + formname);
	thevar = eval("document." + formname + "." + varname);
	thevar.value = varvalue;
	if (pagename != "") {
		theform.action = pagename;
	}
	theform.submit();
	return true;
}

function ConfirmSubmitFormSetVar(formname, pagename, varname, varvalue, confirmmsg)
{
	msg = "Are you sure?";
	if (confirmmsg != "") {
		msg = confirmmsg;
	}
	if (!confirm(msg)) {
		return false;
	}

	theform = eval("document." + formname);
	thevar = eval("document." + formname + "." + varname);
	thevar.value = varvalue;
	if (pagename != "") {
		theform.action = pagename;
	}
	theform.submit();
	return true;
}

function ConfirmSubmitFormSetVarMsg(formname, pagename, varname, varvalue, droplist)
{
	theform = eval("document." + formname);
	selection = eval("document." + formname + "." + droplist + ".value");
	themsg = eval("document." + formname + "." + selection + ".value");
	return ConfirmSubmitFormSetVar(formname, pagename, varname, varvalue, themsg);
}

function DeleteItems(formname, pagename, num_items, item_prefix, alertmsg, confirmmsg, functionname)
{
	// Check if at least one item is selected:
	atleast1 = false;
	for (i = 0;  i < num_items;  i++) {
		obj = eval("document." + formname + "." + item_prefix + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		msg = "Please select at least one item to delete.";
		if (alertmsg != "") {
			msg = alertmsg;
		}
		alert(msg);
		return false;
	}
	msg = "Are you sure you want to delete the selected items?";
	if (confirmmsg != "") {
		msg = confirmmsg;
	}
	if (!confirm(msg)) {
		return false;
	}
	
	if (functionname != "") {
		thefunctionval = eval("document." + formname + "." + functionname);
		thefunctionval.value = "delete";
	}

	theform = eval("document." + formname);
	theform.action = pagename;
	theform.submit();
	return true;
}

function ModifyItem(formname, pagename, extraparams, num_items, item_prefix, itemid_prefix, id_name, alertmsg, confirmmsg)
{
	// Check that exactly one item is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_items;  i++) {
		obj = eval("document." + formname + "." + item_prefix + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document." + formname + "." + itemid_prefix + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		msg = "Please select a item to view.";
		if (alertmsg != "") {
			msg = alertmsg;
		}
		alert(msg);
		return false;
	}
	msg = "You have selected more than one item.  Do you want to view the first one selected?";
	if (confirmmsg != "") {
		msg = confirmmsg;
	}
	if (numchecked > 1 && !confirm(msg)) {
		return false;
	}

	theform = eval("document." + formname);
	theform.action = pagename + "?" + id_name + "=" + id + extraparams;
	theform.submit();
	return true;
}

function UpdateItem(formname, pagename, id_name, id, functionname, required_fields)
{
	if (required_fields != "") {
		fields = required_fields.split(",");
		for (i = 0;  i < fields.length;  i++) {
			thefield = eval("document." + formname + "." + fields[i]);
			if (thefield.value == "") {
				alert("Please supply a value for " + fields[i] + ".");
				return false;
			}
		}
	}

	if (functionname != "") {
		thefunctionval = eval("document." + formname + "." + functionname);
		thefunctionval.value = "update";
	}

	theform = eval("document." + formname);
	theform.action = pagename + "?" + id_name + "=" + id;
	theform.submit();
	return true;
}

function UpdateItemConfirm(formname, pagename, id_name, id, functionname, required_fields, confirmmsg)
{
	if (required_fields != "") {
		fields = required_fields.split(",");
		for (i = 0;  i < fields.length;  i++) {
			thefield = eval("document." + formname + "." + fields[i]);
			if (thefield.value == "") {
				alert("Please supply a value for " + fields[i] + ".");
				return false;
			}
		}
	}

	if (functionname != "") {
		thefunctionval = eval("document." + formname + "." + functionname);
		thefunctionval.value = "update";
	}

	msg = "Are you sure?";
	if (confirmmsg != "") {
		msg = confirmmsg;
	}
	if (!confirm(msg)) {
		return false;
	}

	theform = eval("document." + formname);
	theform.action = pagename + "?" + id_name + "=" + id;
	theform.submit();
	return true;
}

function AddItem(formname, pagename, functionname)
{
	if (functionname != "") {
		thefunctionval = eval("document." + formname + "." + functionname);
		thefunctionval.value = "add";
	}

	theform = eval("document." + formname);
	theform.action = pagename;
	theform.submit();
	return true;
}

function InsertDate(formname, fieldname)
{
	var oneMinute = 60 * 1000;
	var oneHour = oneMinute * 60;
	var oneDay = oneHour * 24;
	var oneWeek = oneDay * 7;
	var thefield = eval("document." + formname + "." + fieldname);
	
	curval = thefield.value;

	var targetDate = new Date();
	//var dateInMs = targetDate.getTime();
	//dateInMs += (oneDay * 31);
	//targetDate.setTime(dateInMs);
	
	month = targetDate.getMonth() + 1;
	day = targetDate.getDate();
	year = targetDate.getYear()
	if ( ! ie4) {
		year += 1900;
	}
	
	thefield.value += "\n\n" + month + "/" + day + "/" + year + ": ";
	thefield.scrollTop = thefield.scrollHeight;
	return false;
}

function InsertAtCursor(thefield, thevalue)
{
	//IE support
	if (document.selection) {
		thefield.focus();
		sel = document.selection.createRange();
		sel.text = thevalue;
	}
	//MOZILLA/NETSCAPE support
	else if (thefield.selectionStart || thefield.selectionStart == '0') {
		var startPos = thefield.selectionStart;
		var endPos = thefield.selectionEnd;
		thefield.value = thefield.value.substring(0, startPos)
					  + thevalue
					  + thefield.value.substring(endPos, thefield.value.length);
	} else {
		thefield.value += thevalue;
	}
}

function GetSelectedText(thefield)
{
	var seltext = "";
	
	//IE support
	if (document.selection) {
		thefield.focus();
		sel = document.selection.createRange();
		seltext = sel.text;
	}
	//MOZILLA/NETSCAPE support
	else if (thefield.selectionStart || thefield.selectionStart == '0') {
		seltext = thefield.value.substring(thefield.selectionStart, thefield.selectionEnd);
	}
	
	return seltext;
}

function InsertTags(formname, fieldname, starttag, endtag)
{
	var thefield = eval("document." + formname + "." + fieldname);
	sel = GetSelectedText(thefield);
	InsertAtCursor(thefield, starttag + sel + endtag);
}

function InsertHyperlink(formname, fieldname, alinkfield, prefixfield)
{
	var thefield = eval("document." + formname + "." + fieldname);
	sel = GetSelectedText(thefield);
	var alink = eval("document." + formname + "." + alinkfield);
	alinkval = alink.value;
	if (alinkval == "") {
		alinkval = sel;
	}
	if (sel == "") {
		sel = alinkval;
	}
	var selobj = eval("document." + formname + "." + prefixfield);
	prefix = selobj.options[selobj.selectedIndex].value;
	prefixlen = prefix.length;
	if (prefixlen > 0) {
		if (alinkval.substr(0, prefixlen) != prefix) {
			alinkval = prefix + alinkval;
		}
	}
	InsertAtCursor(thefield, "<a href='" + alinkval + "'>" + sel + "</a>");
}

function CheckPasswordsMatch()
{
	pass1 = document.prefs.pass1.value;
	pass2 = document.prefs.pass2.value;
	if (pass1 != pass2) {
		alert("Passwords do not match.");
		return false;
	}
	return true;
}

function SortCols(formname, sortval, ascval)
{
	var theform = eval("document." + formname);
	theform.sort.value = sortval;
	if (ascval == "asc") {
		theform.asc.value = "desc";
	} else {
		theform.asc.value = "asc";
	}
	theform.submit();
	return true;
}

function ClearVals(formname, clear_val_str)
{
	if (clear_val_str == "") {
		return;
	}
	var clear_vals = clear_val_str.split(",");
	for (i = 0;  i < clear_vals.length;  i++) {
		var theformval = eval("document." + formname + "." + clear_vals[i]);
		theformval.value = "";
	}
}

function ToggleYesNoSubmit(formname, fieldname)
{
	var theform = eval("document." + formname);
	var thefield = eval("document." + formname + "." + fieldname);
	if (thefield.value != "yes") {
		thefield.value = "yes";
	} else {
		thefield.value = "no";
	}
	theform.pagenum.value = 1;
	theform.submit();
}

function getWindowWidth(offset)
{
	var winW = 1;
	
	if (parseInt(navigator.appVersion) > 3) {
		if (navigator.appName == "Netscape") {
			winW = window.innerWidth;
		}
		if (navigator.appName.indexOf("Microsoft") != -1) {
			winW = document.body.offsetWidth;
		}
	}
	
	if (winW == 1) {
		return winW;
	} else {
		return winW - offset;
	}
}

function getWindowHeight(offset)
{
	var winH = 1;

	if (parseInt(navigator.appVersion) > 3) {
		if (navigator.appName == "Netscape") {
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft") != -1) {
			winH = document.body.offsetHeight;
		}
	}
	if (winH == 1) {
		return winH;
	} else {
		return winH - offset;
	}
}

function getPageWidth(offset)
{
	return PAGEWIDTH - offset;
}

function isOlderNetscape()
{
	if ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) < 5)) {
		return true;
	} else {
		return false;
	}
}

function NewWindow(url, name, options)
{
	mynewwindow = window.open(url, name, options);
	mynewwindow.document.close();
}
