//
// JavaScript functions for Projects.
// Michael Benson - 1/12/05
//


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 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', 'California',
	'Colorado', 'Connecticut', 'Delaware', 'Washington D.C.', 'Florida',
	'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
	'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
	'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
	'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
	'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
	'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Puerto Rico',
	'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas',
	'Utah', 'Vermont', 'Virginia', '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'
];

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

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

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 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 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 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 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 GetExpirationDate()
{
	var oneMinute = 60 * 1000;
	var oneHour = oneMinute * 60;
	var oneDay = oneHour * 24;
	var oneWeek = oneDay * 7;

	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;
	}
	
	document.write("Expires " + month + "/" + day + "/" + year);
}

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