/*******************************************************************
 * Utility.js
 * This page contains generic client-side scripting functions. Do
 * not add functions to this page unless they can be re-used in
 * any website.
 ******************************************************************/

//==========
// Variables
//==========
var popWin = null;  // Use this when referring to pop-up window
var winCount = 0;
var winName = "popWin";

//==========
// Functions
//========== 
function openPopWin(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
 var d_winLeft = 20;  // Default, pixels from screen left to window left
 var d_winTop = 20;   // Default, pixels from screen top to window top
 
 winName = "popWin" + winCount++; // Unique name for each pop-up window
 
 closePopWin();      // Close any previously opened pop-up window   
              
 if (openPopWin.arguments.length >= 4)  // Any additional features? 
  winFeatures = "," + winFeatures;
 
 else 
  winFeatures = "" 
 
 if (openPopWin.arguments.length == 6)  // Location specified
  winFeatures += getLocation(winWidth, winHeight, winLeft, winTop);
 
 else
  winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop);
  popWin = window.open(winURL, winName, "width=" + winWidth + ",height=" + winHeight + winFeatures);
 }
 
function closePopWin(){    // close pop-up window if it is open 
 // Do not close if early IE
 if ((navigator.appName != "Microsoft Internet Explorer") || (parseInt(navigator.appVersion) >=4)) { 
  if(popWin != null) {
   if(!popWin.closed) {
    popWin.close();
   }
  }
 }
}
 
function getLocation(winWidth, winHeight, winLeft, winTop){
 var winLocation = ""
 
 if (winLeft < 0)
  winLeft = screen.width - winWidth + winLeft;
 if (winTop < 0)
  winTop = screen.height - winHeight + winTop;
 if (winTop == "cen")
  winTop = (screen.height - winHeight)/2 - 20;
 if (winLeft == "cen")
  winLeft = (screen.width - winWidth)/2;
 if (winLeft>0 && winTop>0)
  winLocation =  ",screenX=" + winLeft + ",left=" + winLeft + ",screenY=" + winTop + ",top=" + winTop;
 else
  winLocation = "";
 return winLocation;
}
  

 
function clearFormField(field) {
	if (field.tagName) {
		switch (field.tagName) {
			case "SELECT":
				field.selectedIndex = -1;
				break;
			case "OPTION":
				if (field.parentElement) {
					field.parentElement.selectedIndex = -1;
				}
				break;
			case "INPUT":
				switch (field.type) {
					case "text":
					case "password":
						field.value = "";
						break;
					case "checkbox":
					case "radio":
						field.checked = false;
						break;
				}
				break;
		}
	}
	else {
		if (field.value!=null) {
			switch (field.type) {
				case "text":
				case "password":
					field.value = "";
					break;
				case "checkbox":
				case "radio":
					field.checked = false;
					break;
			}
		}
		else {
			field.selectedIndex = -1;
		}
	}
}

function clearFormFields(form) {
	for (var i=0; i<form.elements.length; i++) {
		var e = form.elements[i];
		clearFormField(e);
		e = null;
	}
}

function confirmDelete(type, item) {
	var s;
	if (item==null) {
		s = "Are you sure you want to delete the selected " + type + "?";
	}
	else {
		s = "You are about to delete the following " + type + ":\n\n" + 
			item + "\n\n" + 
			"Do you want to continue?";
	}
	return confirm(s);
}

function getOffset(property, elem) {
	if (elem.offsetParent) {
		return elem[property] + getOffset(property, elem.offsetParent);
	}
	else {
		return elem[property];
	}
}

function getPropertiesHTML(obj, recursive) {
	var s = "<ol>";
	if (recursive==null) {
		recursive = false;
	}
	for (var prop in obj) {
		if ((typeof(obj[prop])=="object") && (recursive==true)) {
			s += "<li>" + prop + "=" + getPropertiesHTML(obj[prop], false) + "</li>";
		}
		else {
			s += "<li>" + prop + "=" + obj[prop] + "</li>";
		}
	}
	s += "</ol>";
	return s;
}

function getPropertiesText(obj, recursive) {
	var s = "";
	if (recursive==null) {
		recursive = false;
	}
	for (var prop in obj) {
		if ((typeof(obj[prop])=="object") && (recursive==true)) {
			s += "\n" + prop + "=" + getPropertiesHTML(obj[prop], false);
		}
		else {
			s += "\n" + prop + "=" + obj[prop];
		}
	}
	return s;
}

function getSelectedCheckboxValues(ctl) {
	if (ctl==null) {
		return null;
	}
	var values = new Array();
	if (ctl.length==null) {
		if (ctl.checked) {
			values.push(ctl.value);
		}
	}
	else {
		for (var i=0; i<ctl.length; i++) {
			if (ctl[i].checked) {
				values.push(ctl[i].value);
			}
		}
	}
	return values;
}

function getSelectedOptionValue(ctl) {
	if (ctl==null) {
		return null;
	}
	if (ctl.length==null) {
		if (ctl.checked) {
			return ctl.value;
		}
	}
	else {
		for (var i=0; i<ctl.length; i++) {
			if (ctl[i].checked) {
				return ctl[i].value;
			}
		}
	}
	return null;
}

function imageLoaded(url) {
}

function imageLoadFailed(url) {
}

function preloadImages(urls, successFunction , failureFunction) {
	var imgs = new Array(urls.length);
	for (var i=0; i<imgs.length; i++) {
		imgs[i] = new Image();			
		imgs[i].onreadystatechange = successFunction;
		imgs[i].onerror = failureFunction;
		imgs[i].src = urls[i];
	}
}

function selectListItem(list, value) {
	for (var i=0; i<list.options.length; i++) {
		if (list.options[i].value==value) {
			list.selectedIndex = i;
			return;
		}
	}
	list.selectedIndex = -1;
}

function setImageSource(imgName, source) {
	var img = document.images[imgName];
	if (img) {
		img.src = source;
	}
}

function showProperties(obj, recursive) {
	var features = "scrollbars=yes," +
			"resizable=yes," + 
			"width=400," + 
			"height=400";
	var w = window.open("", "Utility", features, null);
	w.document.write("<html><head><title>Object Properties</title></head><body>" + 
			getPropertiesHTML(obj, recursive) + 
			"</body></html>");
}

function stripPhone(number) {
	var s = "",mask="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", t;
	for (var i=0; i<number.length; i++) {
		t = number.substr(i,1).toUpperCase();
		if (mask.indexOf(t)!=-1) {
			s += t;
		}
	}
	return s;
}


//=====
// Body
//=====
// The following code allows the site to be used with the revision tool
if (document.domain.indexOf("richardsi.com")!=-1) {
	document.domain = "richardsi.com";
}

// The following code block works with the BrowserInfo Backend function
var isWin = (window.navigator.platform.indexOf("Win")!=-1)
var isMac = (window.navigator.platform.indexOf("Mac")!=-1)
if (document.cookie.indexOf("browser=")==-1) {
	document.cookie = "browser=" + 
		"layers=" + (document.layers!=null) +
		"&getElementById=" + (document.getElementById!=null) +
		"&all=" + (document.all!=null) + 
		"&isMac=" + isMac + 
		"&isWin=" + isWin;
	if (document.layers) {
		if (isMac) {
			window.location.href = "/Home.asp";
		}
		else {
			window.setTimeout("window.location.reload();", 1000);
		}
	}
}
