/* alerts */

function starwayAlert(header, message, callback) {
	return starwayAlertEngine("error", header, message, callback);
}

function starwayConfirm(header, message, callback) {
	return starwayAlertEngine("confirm", header, message, callback);
}

var starwayAlertCallback = null;
var starwayAlertLink = null;
function starwayAlertEngine(type, header, message, callback) {
	// remove previous instance, if possible

	try {
		Element.remove(type + "window");
	} catch(e) {
		// do nothing
	}

	// some set up based on callback (string-link, anchor or form reference, or anonymous function)

	var buttonGroup = Builder.node("div", {className: "alertbtns"});	
	var okLink = Builder.node("a", {id: "btnok", className: "lightwindow_action", rel: "deactivate", href: "#"});
	var cancelLink = Builder.node("a", {id: "btncancel", className: "lightwindow_action", rel: "deactivate", href: "#"});

	if(callback) {
		switch(typeof callback) {
			case "function":
				// nothing, yet
				break;
			case "object":
				if(callback.href) {
					starwayAlertLink = callback.href;
					callback = function() {
						location.href = starwayAlertLink;
					}
				} else if(callback.action && callback.name) {
					starwayAlertLink = callback.name;
					callback = function() {
						document.forms[starwayAlertLink].submit();
					}
				}
				break;
			default:
				// treat as string
				starwayAlertLink = callback;
				callback = function() {
					location.href = starwayAlertLink;
				}	
		};

		starwayAlertCallback = callback;
		okLink = Builder.node("a", {id: "btnok", href: "javascript:starwayAlertHandler();"})		
	}
	
	// build modal display

	buttonGroup.appendChild(okLink);
	if(type == "confirm") {
		buttonGroup.appendChild(cancelLink);
	}
	
	document.body.appendChild(
		Builder.node("div", {id: type + "window", style: "display: none;"}, [
			Builder.node("img", {src: "img/icon_header_alert" + type + ".png", className: "icon"}),
			Builder.node("h1", [header]),
			Builder.node("div", {className: "alertcontents"}, [
				message,
				buttonGroup
			])
		])
	);

	// render

	myLightWindow.activateWindow({
		href: ("#" + type + "window"),
		type: "inline",
		width: 530,
		height: 220
	});
	
	$("lightwindow_contents").addClassName("alert" + type);
	
	return false;	
}

function starwayAlertHandler() {
	if(starwayAlertCallback) {
		starwayAlertCallback();
		starwayAlertCallback = null;
	}
}

/* new windows */
function newWindow(page, name, width, height, scroll){
	var leftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	var topPosition = (screen.height) ? (screen.height-height)/2 : 0;
	window.open(page, name, "height=" + height + ", width=" + width + ", top=" + topPosition + ", left=" + leftPosition + ", scrollbars=" + scroll + ",resizable");
}

/* spellcheck */
function spellCheckAction(){
	var arg = arguments[0];
	if(arg.name) {
		// actual field passed in
		if(arg.value != "") {
			window.open("index.cfm?fuseaction=home.spellInit&formName=" + arg.form.name + "&fieldName=" + arg.name,
				"spellCheck",
				"toolbars=no,alwaysRaised=yes,width=650,height=600,scrollbars=yes,resizable=yes"
			);
		}
	} else {
		// string name passed in, loop over forms to find it
		for(var i=0; i<document.forms.length; i++) {
			// loop over form elements
			for(var j=0; j<document.forms[i].elements.length; j++) {
				if(document.forms[i].elements[j].name == arg) {
					if(document.forms[i].elements[j].value != "") {
						window.open("index.cfm?fuseaction=home.spellInit&formName=" + document.forms[i].name + "&fieldName=" + document.forms[i].elements[j].name,
							"spellCheck",
							"toolbars=no,alwaysRaised=yes,width=650,height=600,scrollbars=yes,resizable=yes"
						);
						break;
					}
				}
			}
		}
	}
}

