// JavaScript Document

YAHOO.widget.Module.prototype.contextItems = Array();
YAHOO.widget.Module.prototype.contextItemAction = Array();
YAHOO.widget.Module.prototype.buttonConfig = "&background=000000&color=000000&button=ffffff";
YAHOO.widget.Module.prototype.addClass = function(newclass) {
	YAHOO.util.Dom.addClass(this.element, newclass);
	return true;
}; // end YAHOO.widget.Module.prototype.scrollToBottom
YAHOO.widget.Module.prototype.scrollToBottom = function() {
	this.scrollToBottom = true;
	return true;
}; // end YAHOO.widget.Module.prototype.scrollToBottom
YAHOO.widget.Module.prototype.scrollToTop = function() {
	this.scrollToTop = true;
	return true;
}; // end YAHOO.widget.Module.prototype.scrollToTop
YAHOO.widget.Module.prototype.addControlButton = function(label, func) {
	control = document.createElement("a");
	control.href = "javascript:" + func + "; void 0";
	button = document.createElement("img");
	button.src = "/apps/images/buttons.php?text=" + encodeURI(label) + this.buttonConfig;
	button.border = "0";
	button.alt = label;
	control.appendChild(button);
	this.appendToFooter(control);
	return true;
}; // end YAHOO.widget.Module.prototype.addControlButton
YAHOO.widget.Module.prototype.addContextItem = function(label, func, admin) {
	if (!admin || userType == "dm") this.contextItems.push({ 'label' : label, 'action' : func });
	return true;
}; // end YAHOO.widget.Module.prototype.addContextItem
YAHOO.widget.Module.prototype.getPanelData = function(url) {
	this.setBody("Loading data..");
	var handleSuccess = function(o) {
		this.setBody(o.responseText);
		if (this.scrollToBottom) this.body.scrollTop = this.body.scrollHeight;
		else if (this.scrollToTop) this.body.scrollTo(0);
		if (typeof this.contextMenu == "object") this.contextMenu.destroy();
		if (this.contextItems.length > 0) {
			var td = this.body.getElementsByTagName("TD");
			this.contextMenu = new YAHOO.widget.ContextMenu(this.element.id + "_ContextMenu", { trigger: td, lazyload: true });
			for(var c in this.contextItems) {
				this.contextMenu.addItem({ text: this.contextItems[c]['label'] });
				this.contextItemAction.push(this.contextItems[c]['action']);
			}; // end for(var c in this.contextItems)
			this.contextMenu.render("main");
			this.contextMenu.clickEvent.subscribe(function(e, obj, act) {
				target = this.activeItem;
				var row = YAHOO.util.Dom.getAncestorByTagName(this.contextEventTarget, "TR");
				a = row.id.split("_");
				id = a.pop();
				eval(act[target.index] + "(" + id + ")");
			}, this.contextItemAction);
		} // if (this.contextItems)
		return true;
	}; // end success: function()
	var handleFailure = function(o) { 
		alert("Data could not be found!");
		return false;
	}; // failure: function(o)
	var callback = { success: handleSuccess, failure: handleFailure, scope: this };
	var DataConnection = YAHOO.util.Connect.asyncRequest('POST', url, callback);
	return true;
}; // YAHOO.widget.Module.prototype.getDataSource

// JavaScript Document

var FormPanel = function(id, url, act) {
	this.showForm = false;
	this.classes = Array();
	this.url = url;
	this.act = act;
	this.submitLabel = "Save Changes";
	this.cancelLabel = "Skip for now";
	this.setHeader = function(text) {
		this.header = text;
		return true;
	}; // end this.setHeader(text)
	this.addClass = function(newclass) {
		this.classes.push(newclass);
		return true;
	}; // end this.setHeader(text)
	this.canClose = function() {
		this.canClosePanel = true;
		return true;
	}; // end this.setHeader(text)
	this.canCancel = function() {
		this.canCancelPanel = true;
		return true;
	}; // end this.setHeader(text)
	this.getFormData = function(url, el) {
		var handleSuccess = function(o) {
			this.dialog.setBody(o.responseText);
			this.dialog.render(document.body);
			if (this.showForm) this.dialog.show();
			return true;
		}; // end success: function()
		var handleFailure = function(o) { 
			alert("Form data could not be found!");
			return false;
		}; // failure: function(o)
		var callback = { success: handleSuccess, failure: handleFailure, scope: this };
		var DataConnection = YAHOO.util.Connect.asyncRequest('POST', url, callback);
		return true;
	}; // end this.getFormData(url)
	this.render = function(el) {
		this.dialog = new YAHOO.widget.Dialog(id, { modal:true, fixedcenter:true, visible:false, constraintoviewport:true });
		if (this.canClosePanel) this.dialog.cfg.queueProperty("close", true);
		else this.dialog.cfg.queueProperty("close", false);
		if (this.canCancelPanel) this.dialog.cfg.queueProperty("buttons", [ { text: this.submitLabel, handler:{ fn: this.handleSubmit, scope:this }}, { text: this.cancelLabel, handler:{ fn: this.handleCancel, scope:this } } ] );
		else this.dialog.cfg.queueProperty("buttons", [ { text: this.submitLabel, handler:{ fn: this.handleSubmit, scope:this }} ] );
		this.dialog.callback = { success: this.handleSuccess, failure: this.handleFailure, scope: this };
		if (this.header)
			this.dialog.setHeader(this.header);
		if (this.classes.length > 0)
			for(var c in this.classes)
				this.dialog.addClass(this.classes[c]);
		this.getFormData(this.url, el);
		return true;
	}; // this.render(el)
	this.show = function() {
		this.dialog.show();
		return true;
	} // end this.show
	this.hide = function() {
		this.dialog.hide();
		return true;
	} // end this.show
	this.handleSubmit = function() {
//		this.dialog.hide();
		QuestionForm.wait.show();
		this.dialog.doSubmit();
	} // end var handleSubmit = function()
	this.handleCancel = function() {
		this.dialog.destroy();
		if (this.act) eval(this.act);
	} // end var handleSubmit = function()
	this.handleSuccess = function(o) {
		var response = o.responseText;
		if (response == "1") {
			QuestionForm.wait.hide();
			this.dialog.destroy();
			if (this.act) eval(this.act);
			return true;
		} else {
			mySimpleDialog = new YAHOO.widget.SimpleDialog("dlg", { width: "30em", effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}, fixedcenter:true, modal:true, visible:true, draggable:false });
			mySimpleDialog.setHeader("Error!");
			mySimpleDialog.setBody(response);
			mySimpleDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
			mySimpleDialog.addClass("error_dialog");
			var handleOkay = function() { 
				this.destroy();
//				this.dialog.show();
			} // end var handleOkay
			var myButtons = [ { text:"Okay", handler:handleOkay } ];
			mySimpleDialog.cfg.queueProperty("buttons", myButtons);
			QuestionForm.wait.hide();
			mySimpleDialog.render(document.body);
			return false;
		} // end if (o.responseText == "1")
	} // end var handleSubmit = function(o)
	this.handleFailure = function() { 
		alert("Failed to submit data."); 
		return false;
	} // handleFailure
	this.destroy = function() {
		this.dialog.destroy();
		return true;
	} // end this.destroy = function() {
}; // FormPanel

var QuestionForm = {
	Forms : Array(),
	getBulletinForm : function(lang, pdf) { 
		var completed = true;
		if (!completed) {
			QuestionForm.wait = new YAHOO.widget.Panel("wait", { width:"240px", fixedcenter:true, close:false, draggable:false, zindex:10, modal:true, visible:false });   
			QuestionForm.wait.setHeader("Submitting data, please wait...");
			QuestionForm.wait.setBody('<img src="http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif" />'); 
			QuestionForm.wait.render(document.body);
			if (pdf) var act = "location.href = '" + pdf + "';";
			else var act = false;
			QuestionForm.Forms['English'] = new FormPanel('EnglishForm', '/apps/questionnaire/ajax.php?lang=en&pdf=' + pdf, act);
			QuestionForm.Forms['English'].canCancel();
			QuestionForm.Forms['English'].canClose();
			QuestionForm.Forms['English'].submitLabel = "Submit Questionnaire";
			QuestionForm.Forms['English'].cancelLabel = "Skip for now";
			QuestionForm.Forms['English'].setHeader("ssrbulletin Questionnaire");
			QuestionForm.Forms['English'].addClass("questionnaire_form");
			if (lang == "en" || !lang) QuestionForm.Forms['English'].showForm = true;
			QuestionForm.Forms['English'].render("dialog");
			QuestionForm.Forms['Spanish'] = new FormPanel('SpanishForm', '/apps/questionnaire/ajax.php?lang=es&pdf=' + pdf, act);
			QuestionForm.Forms['Spanish'].canCancel();
			QuestionForm.Forms['Spanish'].canClose();
			QuestionForm.Forms['Spanish'].submitLabel = "Entregar el cuestionario";
			QuestionForm.Forms['Spanish'].cancelLabel = "Pasar por ahora";
			QuestionForm.Forms['Spanish'].setHeader("El cuestionario del ssrbulletin");
			QuestionForm.Forms['Spanish'].addClass("questionnaire_form");
			if (lang == "es") QuestionForm.Forms['Spanish'].showForm = true;
			QuestionForm.Forms['Spanish'].render("dialog");
			QuestionForm.Forms['French'] = new FormPanel('FrenchForm', '/apps/questionnaire/ajax.php?lang=fr&pdf=' + pdf, act);
			QuestionForm.Forms['French'].canCancel();
			QuestionForm.Forms['French'].canClose();
			QuestionForm.Forms['French'].submitLabel = "Envoyer le questionnaire";
			QuestionForm.Forms['French'].cancelLabel = "Sauter pour l’instant";
			QuestionForm.Forms['French'].setHeader("Le questionnaire du ssrbulletin");
			QuestionForm.Forms['French'].addClass("questionnaire_form");
			if (lang == "fr") QuestionForm.Forms['French'].showForm = true;
			QuestionForm.Forms['French'].render("dialog");
			QuestionForm.Forms['Portuguese'] = new FormPanel('PortugueseForm', '/apps/questionnaire/ajax.php?lang=pt&pdf=' + pdf, act);
			QuestionForm.Forms['Portuguese'].canCancel();
			QuestionForm.Forms['Portuguese'].canClose();
			QuestionForm.Forms['Portuguese'].submitLabel = "Entregar o questionário";
			QuestionForm.Forms['Portuguese'].cancelLabel = "Pasar por agora";
			QuestionForm.Forms['Portuguese'].setHeader("O questionário do ssrbulletin");
			QuestionForm.Forms['Portuguese'].addClass("questionnaire_form");
			if (lang == "pt") QuestionForm.Forms['Portuguese'].showForm = true;
			QuestionForm.Forms['Portuguese'].render("dialog");
			return true;
		} else {
			if (pdf) location.href = pdf;
			return true;
		} // end if (!completed)
	}, // editGameNarrative
	changeLanguage : function(lang) { 
		if (lang != "en") QuestionForm.Forms['English'].hide();
		if (lang != "es") QuestionForm.Forms['Spanish'].hide();
		if (lang != "fr") QuestionForm.Forms['French'].hide();
		if (lang != "pt") QuestionForm.Forms['Portuguese'].hide();
		if (lang == "en") QuestionForm.Forms['English'].show();
		if (lang == "es") QuestionForm.Forms['Spanish'].show();
		if (lang == "fr") QuestionForm.Forms['French'].show();
		if (lang == "pt") QuestionForm.Forms['Portuguese'].show();
		return true;
	} // changeLanguage
} // GameSession