function isArray(v) {
	return v && typeof v === 'object' && typeof v.length === 'number' && !(v.propertyIsEnumerable('length'));
}

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}
}

function openWindow(w, h, c) {
	hideSelectBoxes();
	hideFlash();

	var pageSize = AJS.getWindowSize();
	
	var o = AJS.$('overlay');
	var d = AJS.$('dialog');

	AJS.setTop(o, AJS.getScrollTop());
	AJS.setHeight(o, pageSize['h']);
	AJS.setOpacity(o, 0.3);

	var top = Math.floor(AJS.getScrollTop() + (pageSize['h'] / 2) - h / 2);
	var left = Math.floor(pageSize['w'] / 2 - w / 2);

	AJS.setTop(d, top);
	AJS.setLeft(d, left);
	
	AJS.setWidth(d, w);
	AJS.setHeight(d, h);
	
	var html = "<div id=\"dialoginner\">" + c + "</div>";
	
	AJS.setHTML(d, html);

	AJS.showElement(o, d);
}

function getButton(name, caption, action, width) {
	return "<input class=\"buttonblue\" " +
		"type=\"button\" name=\"" + name + "\" value=\"" + caption + 
		"\" style=\"width:" + width + "px;\" " +
		"onclick=\"" + action + "\"/><div class=\"buttonEndingblue\">";
}

function openDialog(w, h, title, content, buttons) {
	var html = "<table class=\"dialogtable\" width=\"" + w + "\" height=\"" + h + "\">" +
		"<tr><td class=\"dialogtitle\">" + title + "</td></tr>" +
		"<tr><td class=\"dialogcontent\">" + content + "</td></tr>";
		
	if (buttons != null && buttons != '') {
		var buttontable = "<table class=\"dialogtable\" width=\"100%\" height=\"100%\"><tr>";
		for (var i=0; i < buttons.length; i++) {
			buttontable = buttontable + "<td align=\"center\">" + buttons[i] + "</td>";
		}
		buttontable = buttontable + "</tr></table>";
		html = html + "<tr><td class=\"dialogbottom\">" + buttontable + "</td></tr>";
	}
	html = html + "</table>";
			
	openWindow(w, h, html);	
}

function openMessageBox(title, message) {	
	openDialog(300, 200, title, message,
		new Array(getButton('btn_ok', 'ok', 'closeDialog();', 60)));
}

function busyDialog(title) {
	var html = "Please wait ...<br/><br/><img src=\"images/busy.gif\"/>";
	openDialog(200, 150, title, html, ''); 
}

function errorDialog(message) {
	openDialog(300, 150, "ERROR ENCOUNTERED", message,
		new Array(getButton('btn_ok', 'ok', 'closeDialog();', 60)));
}

function closeDialog() {
	var o = AJS.$('overlay');
	var d = AJS.$('dialog');

	AJS.hideElement(d, o);
	AJS.setHeight(o, 0);
	AJS.setHeight(d, 0);
	AJS.setHTML(d, '');
	
	showSelectBoxes();
	showFlash();
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}
	
// NOT USED			
//function initChecklist(theList) {
//	// Only work with those having the class "checklist"
//	if (theList.className.indexOf("checklist") > -1) {
//		var labels = theList.getElementsByTagName("label");
//		
//		// Assign event handlers to labels within
//		for (var j = 0; j < labels.length; j++) {
//			var theLabel = labels[j];
//			theLabel.onmouseover = function() { this.className += " hover"; };
//			theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
//		}
//	}
//}

function getRadioValue(formName, radioInputName) {
	var f = document.forms[formName];
	if (f) {
		f = f.elements[radioInputName];
	}
	if (f) {
		if (typeof f.length == 'undefined') {
			f = [f];
		}
		for (var i = 0; i < f.length; i++) {
			if (f[i].checked) {
				return f[i].value;
			}
		}
	}

	return null;
}
