function SystemCore() {

}

SystemCore.prototype.initAdmin = function() {
		var h = document.getElementById('header');
		EventControl.listen(h, "oncontextmenu", this.handleHeaderContext, this);
		var tbs = document.getElementById('tab_links');
		var ex = tbs.getElementsByTagName('DIV');
		for (var x = 0; x < ex.length; x++) {
			if (ex[x].className == "tabDiv") {
				var ch = ex[x].getElementsByTagName('A');
				ex[x].attrname = ch[0].innerHTML;
				ex[x].attrurl = ch[0].href;
				EventControl.listen(ex[x], "oncontextmenu", this.handleTabContext, this);
			}
		}

		var tbs = document.getElementById('side_menu');
		var ex = tbs.getElementsByTagName('LI');
		for (var x = 0; x < ex.length; x++) {
			var ch = ex[x].getElementsByTagName('A');
			ex[x].attrname = ch[0].innerHTML;
			ex[x].attrurl = ch[0].href;
			EventControl.listen(ex[x], "oncontextmenu", this.handleMenuContext, this);
		}

		var tbs = document.getElementById('side_link');
		var ex = tbs.getElementsByTagName('LI');
		for (var x = 0; x < ex.length; x++) {
			var ch = ex[x].getElementsByTagName('A');
			ex[x].attrname = ch[0].innerHTML;
			ex[x].attrurl = ch[0].href;
			EventControl.listen(ex[x], "oncontextmenu", this.handleLinkContext, this);
		}

		var tbs = document.getElementById('footer_links_holder');
		var ex = tbs.getElementsByTagName('SPAN');
		for (var x = 0; x < ex.length; x++) {
			var ch = ex[x].getElementsByTagName('A');
			ex[x].attrname = ch[0].innerHTML;
			ex[x].attrurl = ch[0].href;
			EventControl.listen(ex[x], "oncontextmenu", this.handleFooterItemContext, this);
		}

		var tbs = document.getElementById('quotes');
		var ex = tbs.getElementsByTagName('DIV');
		for (var x = 0; x < ex.length; x++) {
			var ch = ex[x].getElementsByTagName('h1');
			ex[x].attrheader = ch[0].innerHTML;
			var ch = ex[x].getElementsByTagName('p');
			ex[x].attrtext = ch[0].innerHTML.substr(1, ch[0].innerHTML.length - 2);
			ex[x].attrauthor = ch[1].innerHTML.substr(2);
			EventControl.listen(ex[x], "oncontextmenu", this.handleQuoteContext, this);
		}

		var tbs = document.getElementById('counselor_list');
		if (tbs != undefined) {
			var ex = tbs.getElementsByTagName('LI');
			for (var x = 0; x < ex.length; x++) {
				var ch = ex[x].getElementsByTagName('A');
				ex[x].attrname = ch[0].innerHTML;
				ex[x].attrbio = ch[0].href;
				var ch = ex[x].getElementsByTagName('SPAN');
				ex[x].attremail = unescape(ch[0].innerHTML);
				EventControl.listen(ex[x], "oncontextmenu", this.handleCounselorContext, this);
			}
		}

		var tbs = document.getElementById('FAQ');
		if (tbs != undefined) {
			var ex = tbs.getElementsByTagName('DIV');
			for (var x = 0; x < ex.length; x++) {
				if (ex[x].className == "FAQ_item") {
					EventControl.listen(ex[x], "oncontextmenu", this.handleFAQContext, this);			
				}
			}
		}



		var side_bar = document.getElementById('sidebar');
		EventControl.listen(side_bar, "oncontextmenu", this.handleSideContext, this);
		//EventControl.listen(side_bar, "onmouseover", this.handleSideContextH, this);
		//EventControl.listen(side_bar, "onmouseout", this.handleSideContextH, this);


		var footer_bar = document.getElementById('footer_links');
		EventControl.listen(footer_bar, "oncontextmenu", this.handleFooterContext, this);
		//EventControl.listen(footer_bar, "onmouseover", this.handleFooterContextH, this);
		//EventControl.listen(footer_bar, "onmouseout", this.handleFooterContextH, this);

		var counselor_list = document.getElementById('counselor_manager');
		if (counselor_list != undefined) {
			EventControl.listen(counselor_list, "oncontextmenu", this.handleCounselorNewContext, this);
			//EventControl.listen(counselor_list, "onmouseover", this.handleCounselorNewContextH, this);
			//EventControl.listen(counselor_list, "onmouseout", this.handleCounselorNewContextH, this);
		}

		var faq_list = document.getElementById('FAQ');
		if (faq_list != undefined) {
			EventControl.listen(faq_list, "oncontextmenu", this.handleFAQNewContext, this);
			//EventControl.listen(faq_list, "onmouseover", this.handleFAQNewContextH, this);
			//EventControl.listen(faq_list, "onmouseout", this.handleFAQNewContextH, this);
		}
		

		var mls = document.getElementById('MailingList');
		if (mls != undefined) {
			EventControl.listen(mls, "oncontextmenu", this.handleMailingContext, this);
			//EventControl.listen(mls, "onmouseover", this.handleMailingContextH, this);
			//EventControl.listen(mls, "onmouseout", this.handleMailingContextH, this);
		}

		var mlm = document.getElementById('mailing_list_letters');
		if (mlm != undefined) {
			var doc_list = document.getElementById('mailing_list_box');
			EventControl.listen(doc_list, "oncontextmenu", this.handleBulkContext, this);
			//EventControl.listen(doc_list, "onmouseover", this.handleBulkContextH, this);
			//EventControl.listen(doc_list, "onmouseout", this.handleBulkContextH, this);

			this.setupMailingList();
		}
}

SystemCore.prototype.initUser = function() {

}

SystemCore.prototype.init = function() {
	if (typeof ADMIN_ACCESS != "undefined" && ADMIN_ACCESS == true) {
		this.initAdmin();	
	}

	if (typeof USER_ACCESS != "undefined" && USER_ACCESS == true) {
		this.initUser();	
	}

	var cfm = document.getElementById('contact_form');
	if (cfm != undefined) {

		cfm.appendChild(this.getContactForm().getForm());
	}

	var ref = document.getElementById('refer_form');
	if (ref != undefined) {
		ref.appendChild(this.getReferForm().getForm());
		EventControl.listen(this.getReferForm().getForm(), "oncontextmenu", this.configureReferContext, this);
	}

	var reg = document.getElementById("register_form");
	if (reg != undefined) {
		reg.appendChild(this.getRegisterForm().getForm());
	}

	var login = document.getElementById("login_form");
	if (login != undefined) {
		if (typeof USER_ACCESS != "undefined" && USER_ACCESS == true) {
			login.appendChild(this.getLogoutForm().getForm());
		}
		else {
			login.appendChild(this.getLoginForm().getForm());
		}
	}
	
	this.updateLoginLink();
}

SystemCore.prototype.updateLoginLink = function() {
	var main_menu = document.getElementById('side_menu');
	if (main_menu != undefined) {
		var chx = main_menu.getElementsByTagName('a');
		for (var x = 0; x < chx.length; x++) {
			if (chx[x].innerHTML == "Login" || chx[x].innerHTML == "Logout") {
				chx[x].innerHTML = ((typeof USER_ACCESS != "undefined" && USER_ACCESS == true) ? "Logout" : "Login");
			}
		}
	}
}

SystemCore.prototype.setupMailingList = function() {
	var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#";
	var let_bar = document.getElementById('mailing_list_letters');
	for (var x = 0; x < alpha.length; x++) {
		var d = document.createElement('SPAN');
		d.className = "MailingList_letter";
		d.innerHTML = alpha.charAt(x);
		EventControl.listen(d, "onclick", this.handleLoadLetter, this);
		let_bar.appendChild(d);
	}
	
	var emails = Array();
	var email_list = document.getElementById('mailing_list_members');
	var members = email_list.getElementsByTagName('DIV');
	for (var x = 0; x < members.length; x++){ 
		emails[emails.length] = members[x].innerHTML;
	}
	this.i_emails = emails;
	
}

SystemCore.prototype.handleLoadLetter = function(e) {
	var let = e.originalScope.innerHTML;
	this.i_last_let = let;
	var use = Array();
	if (let != "#") {
		for (var x = 0; x < this.i_emails.length; x++) {
			if (this.i_emails[x].charAt(0).toUpperCase() == let) {
				use[use.length] = this.i_emails[x];
			}
		}
	}
	else {
		var num = new RegExp("^[0-9]$");
		for (var x = 0; x < this.i_emails.length; x++) {
			if (num.test(this.i_emails[x].charAt(0))) {
				use[use.length] = this.i_emails[x];
			}
		}
	}

	if (this.i_mail_cache == undefined) {
		this.i_mail_cache = Array();
	}
	
	var doc_list = document.getElementById('mailing_list_box');
	var x;
	for (x = 0; x < use.length; x++) {
		if (this.i_mail_cache[x] == undefined) {
			this.i_mail_cache[x] = document.createElement('DIV');
			this.i_mail_cache[x].className = "MailingList_entry";
			EventControl.listen(this.i_mail_cache[x], "oncontextmenu", this.handleMailingMemberContext, this);
			doc_list.appendChild(this.i_mail_cache[x]);	
		}
		this.i_mail_cache[x].innerHTML = use[x];
		this.i_mail_cache[x].style.display = "";
	}
	
	for (x; x < this.i_mail_cache.length; x++) {
		this.i_mail_cache[x].style.display = "none";
	}
}

SystemCore.prototype.handleMailingMemberContext = function(e) {
	if (this.i_mail2_context == undefined) {
		this.i_mail2_context = new ContextMenu("Mailing List", 150);
		this.i_mail2_remove = this.i_mail2_context.addItem(new ContextMenuItem("Remove"));
		EventControl.listen(this.i_mail2_remove, "onclick", this.handleMailingMemberRemove, this);
		
		this.i_mail2_send = this.i_mail2_context.addItem(new ContextMenuItem("Contact Member"));
		EventControl.listen(this.i_mail2_send, "onclick", this.handleMailingMemberSend, this);
	}
	this.i_mail2_context.i_scope = e.originalScope.innerHTML;
	this.i_mail2_context.name(e.originalScope.innerHTML);
	this.i_mail2_context.left(Environment.getCursorX());
	this.i_mail2_context.top(Environment.getCursorY());
	this.i_mail2_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleMailingMemberSend = function(e) {
	var tab = e.originalScope.parent().i_scope;
	if (this.i_mail3_send_window == undefined) {
		this.i_mail3_send_window = new WindowObject(500, 350, "Send Mail to " + tab);
		
		this.i_mail3_send_form = new DynamicForm(500, ['Send', 'Cancel'], 5);
			var sec = new DynamicSection("Send Message to " + tab, "Send a message to a single member");
				this.i_mail3_send_subject = sec.addInput(new DynamicTextInput("Subject", "The subject of your message", "LifeWorks Mailing List (Private)"));
				this.i_mail3_send_message = sec.addInput(new DynamicTextAreaInput("Message", "The text message to send", "", 200));
			this.i_mail3_send_form.addSection(sec);
		this.i_mail3_send_window.windowContent(this.i_mail3_send_form.getForm());
		EventControl.listen(this.i_mail3_send_form, "onsubmit", this.handleMailingMemberSendSubmit, this);
	}
	this.i_mail3_send_addr = tab;
	this.i_mail3_send_subject.value("LifeWorks Mailing List");
	this.i_mail3_send_message.value("");
	this.i_mail3_send_window.center();
	this.i_mail3_send_window.visible(true);
}


SystemCore.prototype.handleMailingMemberSendSubmit = function(e) {
	this.i_mail3_send_window.visible(false);
	if (e.button == "Send") {
		var p = new PostData();
		p.param("method", "sendMailingListMember");
		p.param("subject", this.i_mail3_send_subject.value());
		p.param("message", this.i_mail3_send_message.value());
		p.param("email", this.i_mail3_send_addr);
		AJAX.request("./index.php?action=ajax", p, this.handleMailingMemberSendComplete, this);
	}
}

SystemCore.prototype.handleMailingMemberSendComplete = function(req, param) {
	alert(req.getData());
}


SystemCore.prototype.handleMailingMemberRemove = function(e) {
	var tab = e.originalScope.parent().i_scope;
	
	for (var x = 0; x < this.i_emails.length; x++) {
		if (this.i_emails[x] == tab) {
			this.i_emails.splice(x, 1);
			break;
		}
	}
	
	var e = Object();
	e.originalScope = document.createElement('DIV');
	e.originalScope.innerHTML = this.i_last_let;
	this.handleLoadLetter(e);
	
	var p = new PostData();
	AJAX.request("./index.php?action=unsubscribe&email=" + tab, p);
}

SystemCore.prototype.handleBulkContext = function(e) {
	if (this.i_bulk_context == undefined) {
		this.i_bulk_context = new ContextMenu("Mailing List", 150);
		this.i_bulk_config = this.i_bulk_context.addItem(new ContextMenuItem("Configure"));
		EventControl.listen(this.i_bulk_config, "onclick", this.handleConfigureMail, this);
		
		this.i_bulk_import = this.i_bulk_context.addItem(new ContextMenuItem("Import Addresses"));
		EventControl.listen(this.i_bulk_import, "onclick", this.handleBulkImport, this);
		
		this.i_bulk_send = this.i_bulk_context.addItem(new ContextMenuItem("Send Message"));
		EventControl.listen(this.i_bulk_send, "onclick", this.handleSendMail, this);
	}
	this.i_bulk_context.left(Environment.getCursorX());
	this.i_bulk_context.top(Environment.getCursorY());
	this.i_bulk_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}


SystemCore.prototype.handleBulkImport = function(e) {
	if (this.i_bulk_window == undefined) {
		this.i_bulk_window = new WindowObject(500, 310, "Bulk Import");
		
		this.i_bulk_form = new DynamicForm(500, ['Import', 'Cancel'], 5);
			var sec = new DynamicSection("Import Addresses", "import a list of addreses");
				this.i_bulk_addresses = sec.addInput(new DynamicTextAreaInput("Addresses", "A list of email addresses to add, one per line.", "", 200));
			this.i_bulk_form.addSection(sec);
		this.i_bulk_window.windowContent(this.i_bulk_form.getForm());
		EventControl.listen(this.i_bulk_form, "onsubmit", this.handleBulkSubmit, this);
	}
	this.i_bulk_addresses.value("");
	this.i_bulk_window.center();
	this.i_bulk_window.visible(true);
}

SystemCore.prototype.handleBulkSubmit = function(e) {
	this.i_bulk_window.visible(false);
	if (e.button == "Import") {
		var p = new PostData();
		p.param("method", "bulkMailingList");
		p.param("addresses", this.i_bulk_addresses.value());
		AJAX.request("./index.php?action=ajax", p, this.handleBulkComplete, this);
	}
}

SystemCore.prototype.handleBulkComplete = function(req, param) {
	var addr = req.getData().split(",");
	
	var z = 0;
	for (var x = 0; x < addr.length; x++) {
		if (addr[x] != "") {
			this.i_emails[this.i_emails.length] = addr[x];
			z++;
		}
	}
	alert(z + ' addresses added successfully.');
	var e = Object();
	e.originalScope = document.createElement('DIV');
	e.originalScope.innerHTML = this.i_last_let;
	this.handleLoadLetter(e);
	
}

SystemCore.prototype.handleMailingContext = function(e) {
	if (this.i_mail_context == undefined) {
		this.i_mail_context = new ContextMenu("Mailing List", 150);
		this.i_mail_configure = this.i_mail_context.addItem(new ContextMenuItem("Configure"));
		EventControl.listen(this.i_mail_configure, "onclick", this.handleConfigureMail, this);
		
		this.i_mail_send = this.i_mail_context.addItem(new ContextMenuItem("Send Message"));
		EventControl.listen(this.i_mail_send, "onclick", this.handleSendMail, this);
		
		this.i_mail_members = this.i_mail_context.addItem(new ContextMenuItem("Manage Members"));
		EventControl.listen(this.i_mail_members, "onclick", this.handleMailMembers, this);
		
	}
	this.i_mail_context.left(Environment.getCursorX());
	this.i_mail_context.top(Environment.getCursorY());
	this.i_mail_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleMailMembers = function(e) {
	window.location="./Page.MailingList.html";
}

SystemCore.prototype.handleSendMail = function(e) {
	if (this.i_mail_send_window == undefined) {
		this.i_mail_send_window = new WindowObject(500, 350, "Send Mail");
		
		this.i_mail_send_form = new DynamicForm(500, ['Send', 'Cancel'], 5);
			var sec = new DynamicSection("Send Message", "Send a message to all members");
				this.i_mail_send_subject = sec.addInput(new DynamicTextInput("Subject", "The subject of your message", "LifeWorks Mailing List"));
				this.i_mail_send_message = sec.addInput(new DynamicTextAreaInput("Message", "The text message to send", "", 200));
			this.i_mail_send_form.addSection(sec);
		this.i_mail_send_window.windowContent(this.i_mail_send_form.getForm());
		EventControl.listen(this.i_mail_send_form, "onsubmit", this.handleSendMailSubmit, this);
	}
	this.i_mail_send_subject.value("LifeWorks Mailing List");
	this.i_mail_send_message.value("");
	this.i_mail_send_window.center();
	this.i_mail_send_window.visible(true);
}

SystemCore.prototype.handleSendMailSubmit = function(e) {
	this.i_mail_send_window.visible(false);
	if (e.button == "Send") {
		var p = new PostData();
		p.param("method", "sendMailingList");
		p.param("subject", this.i_mail_send_subject.value());
		p.param("message", this.i_mail_send_message.value());
		AJAX.request("./index.php?action=ajax", p, this.handleSendMailComplete, this);
	}
}

SystemCore.prototype.handleSendMailComplete = function(req, param) {
	alert(req.getData());
}

SystemCore.prototype.handleConfigureMail = function(e) {
	if (this.i_mail_conf_window == undefined) {
		this.i_mail_conf_window = new WindowObject(300, 150, "Configure Mailing List");
		
		this.i_mail_conf_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("Mailing List Configuration", "Configuration options for the mailing list");
				this.i_mail_conf_email = sec.addInput(new DynamicTextInput("Email", "The email address to send copies of referal submissions to", ""));
			this.i_mail_conf_form.addSection(sec);
		this.i_mail_conf_window.windowContent(this.i_mail_conf_form.getForm());
		EventControl.listen(this.i_mail_conf_form, "onsubmit", this.handleMailConfigureSubmit, this);
	}
	this.i_mail_conf_email.value(document.getElementById('mail_sender').innerHTML);
	this.i_mail_conf_window.center();
	this.i_mail_conf_window.visible(true);
}


SystemCore.prototype.handleMailConfigureSubmit = function(e) {
	this.i_mail_conf_window.visible(false);
	document.getElementById('mail_sender').innerHTML = this.i_mail_conf_email.value();
	if (e.button == "Save") {
		var p = new PostData();
		p.param("method", "setMailingListConf");
		p.param("email", this.i_mail_conf_email.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}


SystemCore.prototype.getContactForm = function() {
	if (this.i_contact_form == undefined) {
		var cd = document.getElementById('counselor_data');
		var counselors = cd.getElementsByTagName('DIV');
		var counselor_ops = Array();
		for (var x = 0; x < counselors.length; x++) {
			var ex = counselors[x].getElementsByTagName('SPAN');
			var o = new Object();
			o.name = ex[0].innerHTML;
			o.value = ex[1].innerHTML;
			counselor_ops[counselor_ops.length] = o;
		}
	
		console.log(counselor_ops);
	
		this.i_contact_form = new DynamicForm(500, ['Send'], 20);
			var sec = new DynamicSection("", "");
				this.i_fld_counselor = sec.addInput(new DynamicOptionInput("Counselor", "Select one or more counselors to contact", Array(), counselor_ops, 4));
				this.i_fld_name = sec.addInput(new DynamicTextInput("Name", "", ""));
				this.i_fld_email = sec.addInput(new DynamicTextInput("Email", "Enter your email address so we can respond", ""));
				this.i_fld_subject = sec.addInput(new DynamicTextInput("Subject", "Enter a short description of your messages subject matter", ""));
				this.i_fld_message = sec.addInput(new DynamicTextAreaInput("Message", "", "", 80));
			this.i_contact_form.addSection(sec);
		EventControl.listen(this.i_contact_form, "onsubmit", this.handleContactSubmit, this);
	}
	return this.i_contact_form;
}

SystemCore.prototype.handleContactSubmit = function(e) {
	if (e.button == "Send") {
		this.i_contact_form.buttons(Array());
	
		var p = new PostData();
		p.param("method", "contact");
		p.param("counselor", this.i_fld_counselor.value().join(","));
		p.param("name", this.i_fld_name.value());
		p.param("email", this.i_fld_email.value());
		p.param("subject", this.i_fld_subject.value());
		p.param("message", this.i_fld_message.value());
		AJAX.request("./index.php?action=ajax", p, this.handleContactFinish, this);
	}
}

SystemCore.prototype.handleContactFinish = function(req, param) {
	alert(req.getData());
	this.i_contact_form.buttons(Array('Send'));
}

SystemCore.prototype.getReferForm = function() {
	if (this.i_refer_form == undefined) {
		this.i_refer_form = new DynamicForm(500, ['Send'], 20);
			var sec = new DynamicSection("Your information", "Please provide your name and email address");
				this.i_fld2_from_name = sec.addInput(new DynamicTextInput("Your Name", "Please enter your name", ""));
				this.i_fld2_from_email = sec.addInput(new DynamicTextInput("Your Email", "Please enter your email address", ""));
			this.i_refer_form.addSection(sec);	
			var sec2 = new DynamicSection("Friend's Information", "Please enter your friends information");
				this.i_fld2_to_name = sec2.addInput(new DynamicTextInput("Name", "Please enter the name of the person you would like to tell about our site", ""));
				this.i_fld2_to_email = sec2.addInput(new DynamicTextInput("Email", "Please enter your friends email address", ""));
				this.i_fld2_message = sec2.addInput(new DynamicTextAreaInput("Message", "If you would like to include a custom message to your friend, please enter it here", "", 80));
			this.i_refer_form.addSection(sec2);
		EventControl.listen(this.i_refer_form, "onsubmit", this.handleReferSubmit, this);
	}
	return this.i_refer_form;
}

SystemCore.prototype.handleReferSubmit = function(e) {
	if (e.button == "Send") {
		this.i_refer_form.buttons(Array());
	
		var p = new PostData();
		p.param("method", "refer");
		p.param("from_name", this.i_fld2_from_name.value());
		p.param("from_email", this.i_fld2_from_email.value());
		p.param("to_name", this.i_fld2_to_name.value());
		p.param("to_email", this.i_fld2_to_email.value());
		p.param("message", this.i_fld2_message.value());
		AJAX.request("./index.php?action=ajax", p, this.handleReferFinish, this);
	}
}

SystemCore.prototype.handleReferFinish = function(req, param) {
	alert(req.getData());
	this.i_refer_form.buttons(Array('Send'));
}

SystemCore.prototype.configureReferContext = function(e) {
	if (this.i_refer_context == undefined) {
		this.i_refer_context = new ContextMenu("Refer Form", 150);
		this.i_refer_configure = this.i_refer_context.addItem(new ContextMenuItem("Configure"));
		EventControl.listen(this.i_refer_configure, "onclick", this.handleConfigureRefer, this);
		
	}
	this.i_refer_context.left(Environment.getCursorX());
	this.i_refer_context.top(Environment.getCursorY());
	this.i_refer_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleConfigureRefer = function(e) {
	if (this.i_refer_window == undefined) {
		this.i_refer_window = new WindowObject(300, 150, "Configure Refer Form");
		
		this.i_refer_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("Refer Form Configuration", "Configure the referal form");
				this.i_refer_email = sec.addInput(new DynamicTextInput("Email", "The email address to send copies of referal submissions to", ""));
			this.i_refer_form.addSection(sec);
		this.i_refer_window.windowContent(this.i_refer_form.getForm());
		EventControl.listen(this.i_refer_form, "onsubmit", this.handleReferConfigureSubmit, this);
	}
	this.i_refer_email.value(document.getElementById('refer_contact').innerHTML);
	this.i_refer_window.center();
	this.i_refer_window.visible(true);
}

SystemCore.prototype.handleReferConfigureSubmit = function(e) {
	this.i_refer_window.visible(false);
	document.getElementById('refer_contact').innerHTML = this.i_refer_email.value();
	if (e.button == "Save") {
		var p = new PostData();
		p.param("method", "setReferAddress");
		p.param("email", this.i_refer_email.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}


SystemCore.prototype.getLogoutForm = function() {
	if (this.i_logout_form == undefined) {
		this.i_logout_form = new DynamicForm(500, ['Yes', 'No'], 20);
			var sec = new DynamicSection("Logout", "Are you sure you want to logout?");
			this.i_logout_form.addSection(sec);	
		EventControl.listen(this.i_logout_form, "onsubmit", this.handleLogoutSubmit, this);
	}
	return this.i_logout_form;
}

SystemCore.prototype.handleLogoutSubmit = function(e) {
	if (e.button == "Yes") {
		this.i_logout_form.buttons(Array());
	
		var p = new PostData();
		p.param("method", "logout");
		AJAX.request("./index.php?action=ajax", p, this.handleLogoutFinish, this);
	}
	else {
		window.location = "./index.php";
	}
}


SystemCore.prototype.handleLogoutFinish = function(req, param) {
	USER_ACCESS = false;
	ADMIN_ACCESS = false;
	
	this.updateLoginLink();
	alert('You have logged out successfully');
	window.location = "./index.php";
}


SystemCore.prototype.getLoginForm = function() {
	if (this.i_login_form == undefined) {
		this.i_login_form = new DynamicForm(500, ['Login'], 20);
			var sec = new DynamicSection("Login Information", "Please provide your login information");
				this.i_login_username = sec.addInput(new DynamicTextInput("Username", "Enter your username", ""));
				this.i_login_password = sec.addInput(new DynamicTextInput("Password", "Enter your password", ""));
			this.i_login_form.addSection(sec);	
		EventControl.listen(this.i_login_form, "onsubmit", this.handleLoginSubmit, this);
	}
	return this.i_login_form;
}

SystemCore.prototype.handleLoginSubmit = function(e) {
	if (e.button == "Login") {
		this.i_login_form.buttons(Array());
	
		var p = new PostData();
		p.param("method", "login");
		p.param("username", this.i_login_username.value());
		p.param("password", this.i_login_password.value());
		AJAX.request("./index.php?action=ajax", p, this.handleLoginFinish, this);
	}
}

SystemCore.prototype.handleLoginFinish = function(req, param) {
	if (req.getData() == "1") {
		USER_ACCESS = true;
		alert('You have logged in successfully');
		this.initUser();
		
		var login = document.getElementById("login_form");
		if (login != undefined) {
			login.removeChild(this.getLoginForm().getForm());
			login.appendChild(this.getLogoutForm().getForm());
		}
		
		this.updateLoginLink();

	}
	else if (req.getData() == "2") {
		USER_ACCESS = true;
		ADMIN_ACCESS = true;
		alert('You have logged in as a website administrator successfully');
		this.initAdmin();
		
		var login = document.getElementById("login_form");
		if (login != undefined) {
			login.removeChild(this.getLoginForm().getForm());
			login.appendChild(this.getLogoutForm().getForm());
		}
		
		this.updateLoginLink();
	}
	else {
		alert('The username and password you entered are not correct.');
	}
	this.i_login_form.buttons(Array('Login'));
	this.i_login_username.value("");
	this.i_login_password.value("");
}

SystemCore.prototype.getRegisterForm = function() {
	if (this.i_register_form == undefined) {
		this.i_register_form = new DynamicForm(500, ['Register'], 20);
			var sec = new DynamicSection("Login Information", "Please provide your desired login information");
				this.i_register_username = sec.addInput(new DynamicTextInput("Username", "Enter your desired username", ""));
				this.i_register_password = sec.addInput(new DynamicTextInput("Password", "Enter your desired password", ""));
			this.i_register_form.addSection(sec);	
			var sec2 = new DynamicSection("Contact Information", "Please enter your contact information");
				this.i_register_first_name = sec2.addInput(new DynamicTextInput("First Name", "", ""));
				this.i_register_last_name = sec2.addInput(new DynamicTextInput("Last Name", "", ""));
				this.i_register_address = sec2.addInput(new DynamicTextAreaInput("Address", "", "", 50));
				this.i_register_city = sec2.addInput(new DynamicTextInput("City", "", ""));
				this.i_register_state = sec2.addInput(new DynamicTextInput("State", "", ""));
				this.i_register_zip = sec2.addInput(new DynamicTextInput("Zip Code", "", ""));
				this.i_register_phone = sec2.addInput(new DynamicTextInput("Phone", "", ""));
				this.i_register_email = sec2.addInput(new DynamicTextInput("Email", "", ""));
			this.i_register_form.addSection(sec2);
		EventControl.listen(this.i_register_form, "onsubmit", this.handleRegisterSubmit, this);
	}
	return this.i_register_form;
}

SystemCore.prototype.handleRegisterSubmit = function(e) {
	if (e.button == "Register") {
		this.i_register_form.buttons(Array());
	
		var p = new PostData();
		p.param("method", "register");
		p.param("username", this.i_register_username.value());
		p.param("password", this.i_register_password.value());
		p.param("first_name", this.i_register_first_name.value());
		p.param("last_name", this.i_register_last_name.value());
		p.param("address", this.i_register_address.value());
		p.param("city", this.i_register_city.value());
		p.param("state", this.i_register_state.value());
		p.param("zip", this.i_register_zip.value());
		p.param("phone", this.i_register_phone.value());
		p.param("email", this.i_register_email.value());
		AJAX.request("./index.php?action=ajax", p, this.handleRegisterFinish, this);
	}
}

SystemCore.prototype.handleRegisterFinish = function(req, param) {
	alert(req.getData());
	this.i_register_form.buttons(Array('Register'));
}



/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FAQs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleFAQNewContext = function(e) {
	if (this.i_faq_new_context == undefined) {
		this.i_faq_new_context = new ContextMenu("FAQ", 150);
		this.i_context_add_faq = this.i_faq_new_context.addItem(new ContextMenuItem("Add Question"));
		EventControl.listen(this.i_context_add_faq, "onclick", this.handleAddFAQ, this);
		
	}
	this.i_faq_new_context.left(Environment.getCursorX());
	this.i_faq_new_context.top(Environment.getCursorY());
	this.i_faq_new_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleAddFAQ = function(e) {
	if (this.i_new_faq_window == undefined) {
		this.i_new_faq_window = new WindowObject(300, 300, "New Question");
		
		this.i_new_faq_form = new DynamicForm(300, ['Create', 'Cancel'], 5);
			var sec = new DynamicSection("Qusetion Information", "Question and associated answer");
				this.i_new_faq_question = sec.addInput(new DynamicTextAreaInput("Question", "The question text", "", 80));
				this.i_new_faq_answer = sec.addInput(new DynamicTextAreaInput("Answer", "The answer text", "", 80));
			this.i_new_faq_form.addSection(sec);
		this.i_new_faq_window.windowContent(this.i_new_faq_form.getForm());
		EventControl.listen(this.i_new_faq_form, "onsubmit", this.handleNewFAQSubmit, this);
	}
	this.i_new_faq_question.value("");
	this.i_new_faq_answer.value("");
	this.i_new_faq_window.center();
	this.i_new_faq_window.visible(true);
}

SystemCore.prototype.handleNewFAQSubmit = function(e) {
	this.i_new_faq_window.visible(false);
	if (e.button == "Create") {
		var tbs = document.getElementById('FAQ');

		var new_faq = document.createElement('DIV');
		new_faq.className = "FAQ_item";
	
		new_faq.innerHTML = '<div id="q0" class="FAQ_question" onclick="toggleQuestion(this);"><span class="faq_q">Q.</span>' + this.i_new_faq_question.value() + '</div><div id="a0" class="FAQ_answer" style="display:none;">' + this.i_new_faq_answer.value() + '</div>';
		EventControl.listen(new_faq, "oncontextmenu", this.handleFAQContext, this);
		tbs.appendChild(new_faq);
		
		var p = new PostData();
		p.param("method", "newFAQ");
		p.param("question", this.i_new_faq_question.value());
		p.param("answer", this.i_new_faq_answer.value());
		AJAX.request("./index.php?action=ajax", p, this.handleNewFAQFinish, this, new_faq);
	}
}

SystemCore.prototype.handleNewFAQFinish = function(req, param) {
	if (req.getData() != 0) {
		param.id = req.getData();
		var idx = param.getElementsByTagName('DIV');
		idx[0].id = "q" + req.getData();
		idx[1].id = "a" + req.getData();
	}
}

SystemCore.prototype.handleMoveFAQDown = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('FAQ');
	var tabs_x = tbs.getElementsByTagName('DIV');
	var tabs = Array();
	for (var x = 0; x < tabs_x.length; x++) {
		if (tabs_x[x].className == "FAQ_item") {
			tabs[tabs.length] = tabs_x[x];
		}
	}
	
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != tabs.length - 1) {
				if (x == tabs.length - 2) {
					tbs.appendChild(tab);
				}
				else {
					tbs.insertBefore(tab, tabs[x + 2]);
				}
				var p = new PostData();
				p.param("method", "moveFAQ");
				p.param("id", tab.id);
				p.param("direction", "1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleMoveFAQUp = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('FAQ');
	var tabs_x = tbs.getElementsByTagName('DIV');
	var tabs = Array();
	for (var x = 0; x < tabs_x.length; x++) {
		if (tabs_x[x].className == "FAQ_item") {
			tabs[tabs.length] = tabs_x[x];
		}
	}
	
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x  != 0) {
				tbs.insertBefore(tab, tabs[x - 1]);
				var p = new PostData();
				p.param("method", "moveFAQ");
				p.param("id", tab.id);
				p.param("direction", "-1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleDeleteFAQ = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('FAQ');
	tbs.removeChild(tab);
	
	var p = new PostData();
	p.param("method", "deleteFAQ");
	p.param("id", tab.id);
	AJAX.request("./index.php?action=ajax", p);
}

SystemCore.prototype.handleEditFAQ = function(e) {
	if (this.i_edit_faq_window == undefined) {
		var idx = e.originalScope.parent().i_scope.getElementsByTagName('DIV');
	
		this.i_edit_faq_window = new WindowObject(300, 300, "Edit FAQ");
		this.i_edit_faq_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("FAQ Information", "Information about the faq");
				this.i_edit_faq_question = sec.addInput(new DynamicTextAreaInput("Question", "the question text", idx[0].innerHTML, 80));
				this.i_edit_faq_answer = sec.addInput(new DynamicTextAreaInput("Answer", "the answer text",  idx[1].innerHTML, 80));
			this.i_edit_faq_form.addSection(sec);
		this.i_edit_faq_window.windowContent(this.i_edit_faq_form.getForm());
		EventControl.listen(this.i_edit_faq_form, "onsubmit", this.handleEditFAQSubmit, this);
	}
	this.i_edit_faq_question.value(idx[0].innerHTML.replace('<span class="faq_q">Q.</span>', ''));
	this.i_edit_faq_answer.value(idx[1].innerHTML);
	this.i_edit_faq_form.i_scope = e.originalScope.parent().i_scope;
	this.i_edit_faq_window.center();
	this.i_edit_faq_window.visible(true);
}

SystemCore.prototype.handleEditFAQSubmit = function(e) {
	this.i_edit_faq_window.visible(false);
	if (e.button == "Save") {
		var idx = e.originalScope.i_scope.getElementsByTagName('DIV');
	
		idx[0].innerHTML = '<span class="faq_q">Q.</span>' + this.i_edit_faq_question.value();
		idx[1].innerHTML = this.i_edit_faq_answer.value();

		var p = new PostData();
		p.param("method", "editFAQ");
		p.param("id", e.originalScope.i_scope.id);
		p.param("question", this.i_edit_faq_question.value());
		p.param("answer", this.i_edit_faq_answer.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}

SystemCore.prototype.handleFAQContext = function(e) {
	if (this.i_faq_context == undefined) {
		this.i_faq_context = new ContextMenu("", 150);
		
		this.i_context_add_faq2 = this.i_faq_context.addItem(new ContextMenuItem("Add Question"));
		EventControl.listen(this.i_context_add_faq2, "onclick", this.handleAddFAQ, this);
		
		this.i_faq_context.addItem(new ContextMenuDivider());
		
		this.i_context_edit_faq = this.i_faq_context.addItem(new ContextMenuItem("Edit Question"));
		EventControl.listen(this.i_context_edit_faq, "onclick", this.handleEditFAQ, this);
		
		this.i_context_move_faq_up = this.i_faq_context.addItem(new ContextMenuItem("Move Up"));
		EventControl.listen(this.i_context_move_faq_up, "onclick", this.handleMoveFAQUp, this);

		this.i_context_move_faq_down = this.i_faq_context.addItem(new ContextMenuItem("Move Down"));
		EventControl.listen(this.i_context_move_faq_down, "onclick", this.handleMoveFAQDown, this);
		
		this.i_context_delete_faq = this.i_faq_context.addItem(new ContextMenuItem("Delete Question"));
		EventControl.listen(this.i_context_delete_faq, "onclick", this.handleDeleteFAQ, this);
	}
	var idx = e.originalScope.getElementsByTagName('DIV');
	
	this.i_faq_context.name(idx[0].innerHTML);
	this.i_faq_context.left(Environment.getCursorX());
	this.i_faq_context.top(Environment.getCursorY());
	this.i_faq_context.visible(true);
	this.i_faq_context.i_scope = e.originalScope;
	e.cancelBubble = true;
	e.returnValue = false;
}


/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Counselors %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleCounselorNewContext = function(e) {
	if (this.i_counselor_new_context == undefined) {
		this.i_counselor_new_context = new ContextMenu("Counselors", 150);
		this.i_context_add_counselor = this.i_counselor_new_context.addItem(new ContextMenuItem("Add Counselor"));
		EventControl.listen(this.i_context_add_counselor, "onclick", this.handleAddCounselor, this);
		
	}
	this.i_counselor_new_context.left(Environment.getCursorX());
	this.i_counselor_new_context.top(Environment.getCursorY());
	this.i_counselor_new_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleAddCounselor = function(e) {
	if (this.i_new_counselor_window == undefined) {
		this.i_new_counselor_window = new WindowObject(300, 200, "New Counselor");
		
		this.i_new_counselor_form = new DynamicForm(300, ['Create', 'Cancel'], 5);
			var sec = new DynamicSection("Counselor Information", "Information about the counselor");
				this.i_new_counselor_name = sec.addInput(new DynamicTextInput("Name", "The counselors name", ""));
				this.i_new_counselor_email = sec.addInput(new DynamicTextInput("Email", "The counselors email address", ""));
				this.i_new_counselor_bio = sec.addInput(new DynamicTextInput("URL", "The URL of the bio page", ""));
			this.i_new_counselor_form.addSection(sec);
		this.i_new_counselor_window.windowContent(this.i_new_counselor_form.getForm());
		EventControl.listen(this.i_new_counselor_form, "onsubmit", this.handleNewCounselorSubmit, this);
	}
	this.i_new_counselor_name.value("");
	this.i_new_counselor_email.value("");
	this.i_new_counselor_bio.value("");
	this.i_new_counselor_window.center();
	this.i_new_counselor_window.visible(true);
}

SystemCore.prototype.handleNewCounselorSubmit = function(e) {
	this.i_new_counselor_window.visible(false);
	if (e.button == "Create") {
		var tbs = document.getElementById('counselor_list');

		var new_counselor = document.createElement('LI');
		new_counselor.attrname = this.i_new_counselor_name.value();
		new_counselor.attremail = this.i_new_counselor_email.value();
		new_counselor.attrbio = this.i_new_counselor_bio.value();
		
		new_counselor.innerHTML = '<a href="' + this.i_new_counselor_bio.value() + '">' + this.i_new_counselor_name.value() + '</a>';
		EventControl.listen(new_counselor, "oncontextmenu", this.handleCounselorContext, this);
		tbs.appendChild(new_counselor);
		
		var p = new PostData();
		p.param("method", "newCounselor");
		p.param("name", this.i_new_counselor_name.value());
		p.param("email", this.i_new_counselor_email.value());
		p.param("bio", this.i_new_counselor_bio.value());
		AJAX.request("./index.php?action=ajax", p, this.handleNewCounselorFinish, this, new_counselor);
	}
}

SystemCore.prototype.handleNewCounselorFinish = function(req, param) {
	if (req.getData() != 0) {
		param.id = req.getData();
	}
}

SystemCore.prototype.handleMoveCounselorDown = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('counselor_list');
	var tabs = tbs.getElementsByTagName('LI');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != tabs.length - 1) {
				if (x == tabs.length - 2) {
					tbs.appendChild(tab);
				}
				else {
					tbs.insertBefore(tab, tabs[x + 2]);
				}
				var p = new PostData();
				p.param("method", "moveCounselor");
				p.param("id", tab.id);
				p.param("direction", "1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleMoveCounselorUp = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('counselor_list');
	var tabs = tbs.getElementsByTagName('LI');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != 0) {
				tbs.insertBefore(tab, tabs[x - 1]);
				var p = new PostData();
				p.param("method", "moveCounselor");
				p.param("id", tab.id);
				p.param("direction", "-1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleDeleteCounselor = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('counselor_list');
	tbs.removeChild(tab);
	
	var p = new PostData();
	p.param("method", "deleteCounselor");
	p.param("id", tab.id);
	AJAX.request("./index.php?action=ajax", p);
}

SystemCore.prototype.handleEditCounselor = function(e) {
	if (this.i_edit_counselor_window == undefined) {
		this.i_edit_counselor_window = new WindowObject(300, 200, "Edit Counselor");
		this.i_edit_counselor_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("Counselor Information", "Information about the counselor");
				this.i_edit_counselor_name = sec.addInput(new DynamicTextInput("Name", "The name of the counselor", e.originalScope.parent().i_scope.attrname));
				this.i_edit_counselor_email = sec.addInput(new DynamicTextInput("Email", "The email address of the counselor", e.originalScope.parent().i_scope.attremail));
				this.i_edit_counselor_bio = sec.addInput(new DynamicTextInput("URL", "The URL of the counselor's bio", e.originalScope.parent().i_scope.attrbio));
			this.i_edit_counselor_form.addSection(sec);
		this.i_edit_counselor_window.windowContent(this.i_edit_counselor_form.getForm());
		EventControl.listen(this.i_edit_counselor_form, "onsubmit", this.handleEditCounselorSubmit, this);
	}
	this.i_edit_counselor_name.value(e.originalScope.parent().i_scope.attrname);
	this.i_edit_counselor_email.value(e.originalScope.parent().i_scope.attremail);
	this.i_edit_counselor_bio.value(e.originalScope.parent().i_scope.attrbio);
	this.i_edit_counselor_form.i_scope = e.originalScope.parent().i_scope;
	this.i_edit_counselor_window.center();
	this.i_edit_counselor_window.visible(true);
}

SystemCore.prototype.handleEditCounselorSubmit = function(e) {
	this.i_edit_counselor_window.visible(false);
	if (e.button == "Save") {
		var t = e.originalScope.i_scope;
		t.attrname = this.i_edit_counselor_name.value();
		t.attremail = this.i_edit_counselor_email.value();
		t.attrbio = this.i_edit_counselor_bio.value();
		t.innerHTML = '<a href="' + t.attrbio + '">' + this.i_edit_counselor_name.value() + '</a>';
		
		var p = new PostData();
		p.param("method", "editCounselor");
		p.param("id", t.id);
		p.param("name", this.i_edit_counselor_name.value());
		p.param("email", this.i_edit_counselor_email.value());
		p.param("bio", this.i_edit_counselor_bio.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}

SystemCore.prototype.handleCounselorContext = function(e) {
	if (this.i_counselor_context == undefined) {
		this.i_counselor_context = new ContextMenu("", 150);
		
		this.i_context_add_counselor2 = this.i_counselor_context.addItem(new ContextMenuItem("Add Counselor"));
		EventControl.listen(this.i_context_add_counselor2, "onclick", this.handleAddCounselor, this);
		
		this.i_counselor_context.addItem(new ContextMenuDivider());
		
		this.i_context_edit_counselor = this.i_counselor_context.addItem(new ContextMenuItem("Edit Counselor"));
		EventControl.listen(this.i_context_edit_counselor, "onclick", this.handleEditCounselor, this);
		
		this.i_context_move_counselor_up = this.i_counselor_context.addItem(new ContextMenuItem("Move Up"));
		EventControl.listen(this.i_context_move_counselor_up, "onclick", this.handleMoveCounselorUp, this);

		this.i_context_move_counselor_down = this.i_counselor_context.addItem(new ContextMenuItem("Move Down"));
		EventControl.listen(this.i_context_move_counselor_down, "onclick", this.handleMoveCounselorDown, this);
		
		this.i_context_delete_counselor = this.i_counselor_context.addItem(new ContextMenuItem("Delete Counselor"));
		EventControl.listen(this.i_context_delete_counselor, "onclick", this.handleDeleteCounselor, this);
	}
	this.i_counselor_context.name(e.originalScope.attrname);
	this.i_counselor_context.left(Environment.getCursorX());
	this.i_counselor_context.top(Environment.getCursorY());
	this.i_counselor_context.visible(true);
	this.i_counselor_context.i_scope = e.originalScope;
	e.cancelBubble = true;
	e.returnValue = false;
}


/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Quotes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleAddQuote = function(e) {
	if (this.i_new_quote_window == undefined) {
		this.i_new_quote_window = new WindowObject(300, 200, "New Quote Item");
		
		this.i_new_quote_form = new DynamicForm(300, ['Create', 'Cancel'], 5);
			var sec = new DynamicSection("Quote Item Information", "Quote item configuration information");
				this.i_new_quote_header = sec.addInput(new DynamicTextInput("Header", "The header text", ""));
				this.i_new_quote_text = sec.addInput(new DynamicTextInput("Quote", "The quote text", ""));
				this.i_new_quote_author = sec.addInput(new DynamicTextInput("Author", "The quote author", ""));
			this.i_new_quote_form.addSection(sec);
		this.i_new_quote_window.windowContent(this.i_new_quote_form.getForm());
		EventControl.listen(this.i_new_quote_form, "onsubmit", this.handleNewQuoteSubmit, this);
	}
	this.i_new_quote_header.value("Quote");
	this.i_new_quote_text.value("");
	this.i_new_quote_author.value("");
	this.i_new_quote_window.center();
	this.i_new_quote_window.visible(true);
}

SystemCore.prototype.handleNewQuoteSubmit = function(e) {
	this.i_new_quote_window.visible(false);
	if (e.button == "Create") {
		var tbs = document.getElementById('quotes');

		var new_quote = document.createElement('DIV');
		new_quote.attrheader = this.i_new_quote_header.value();
		new_quote.attrtext = this.i_new_quote_text.value();
		new_quote.attrauthor = this.i_new_quote_author.value();
		
		new_quote.innerHTML = "<h1>" + this.i_new_quote_header.value() + "</h1><p>&quot;" + this.i_new_quote_text.value() + "&quot;</p><p class='align-right'>- " + this.i_new_quote_author.value() + "</p>";
		EventControl.listen(new_quote, "oncontextmenu", this.handleQuoteContext, this);
		tbs.appendChild(new_quote);
		
		var p = new PostData();
		p.param("method", "newQuote");
		p.param("name", this.i_new_quote_header.value());
		p.param("text", this.i_new_quote_text.value());
		p.param("author", this.i_new_quote_author.value());
		AJAX.request("./index.php?action=ajax", p, this.handleNewQuoteFinish, this, new_quote);
	}
}

SystemCore.prototype.handleNewQuoteFinish = function(req, param) {
	if (req.getData() != 0) {
		param.id = req.getData();
	}
}

SystemCore.prototype.handleMoveQuoteDown = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('quotes');
	var tabs = tbs.getElementsByTagName('DIV');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != tabs.length - 1) {
				if (x == tabs.length - 2) {
					tbs.appendChild(tab);
				}
				else {
					tbs.insertBefore(tab, tabs[x + 2]);
				}
				var p = new PostData();
				p.param("method", "moveQuote");
				p.param("id", tab.id);
				p.param("direction", "1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleMoveQuoteUp = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('quotes');
	var tabs = tbs.getElementsByTagName('DIV');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != 0) {
				tbs.insertBefore(tab, tabs[x - 1]);
				var p = new PostData();
				p.param("method", "moveQuote");
				p.param("id", tab.id);
				p.param("direction", "-1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleDeleteQuote = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('quotes');
	tbs.removeChild(tab);
	
	var p = new PostData();
	p.param("method", "deleteQuote");
	p.param("id", tab.id);
	AJAX.request("./index.php?action=ajax", p);
}

SystemCore.prototype.handleEditQuote = function(e) {
	if (this.i_edit_quote_window == undefined) {
		this.i_edit_quote_window = new WindowObject(300, 200, "Edit Quote Item");
		this.i_edit_quote_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("Quote Item Information", "Quote item configuration information");
				this.i_edit_quote_header = sec.addInput(new DynamicTextInput("Header", "The header text", e.originalScope.parent().i_scope.attrheader));
				this.i_edit_quote_text = sec.addInput(new DynamicTextInput("Quote", "The quote text", e.originalScope.parent().i_scope.attrtext));
				this.i_edit_quote_author = sec.addInput(new DynamicTextInput("Author", "The quote author", e.originalScope.parent().i_scope.attrauthor));
			this.i_edit_quote_form.addSection(sec);
		this.i_edit_quote_window.windowContent(this.i_edit_quote_form.getForm());
		EventControl.listen(this.i_edit_quote_form, "onsubmit", this.handleEditQuoteSubmit, this);
	}
	this.i_edit_quote_header.value(e.originalScope.parent().i_scope.attrheader);
	this.i_edit_quote_text.value(e.originalScope.parent().i_scope.attrtext);
	this.i_edit_quote_author.value(e.originalScope.parent().i_scope.attrauthor);
	this.i_edit_quote_form.i_scope = e.originalScope.parent().i_scope;
	this.i_edit_quote_window.center();
	this.i_edit_quote_window.visible(true);
}

SystemCore.prototype.handleEditQuoteSubmit = function(e) {
	this.i_edit_quote_window.visible(false);
	if (e.button == "Save") {
		var t = e.originalScope.i_scope;
		t.attrheader = this.i_edit_quote_header.value();
		t.attrtext = this.i_edit_quote_text.value();
		t.attrauthor = this.i_edit_quote_author.value();
		t.innerHTML = "<h1>" + this.i_edit_quote_header.value() + "</h1><p>&quot;" + this.i_edit_quote_text.value() + "&quot;</p><p class='align-right'>- " + this.i_edit_quote_author.value() + "</p>";
		
		var p = new PostData();
		p.param("method", "editQuote");
		p.param("id", t.id);
		p.param("name", this.i_edit_quote_header.value());
		p.param("text", this.i_edit_quote_text.value());
		p.param("author", this.i_edit_quote_author.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}

SystemCore.prototype.handleQuoteContext = function(e) {
	if (this.i_quote_context == undefined) {
		this.i_quote_context = new ContextMenu("", 150);
	
		this.i_context_add_item4 = this.i_quote_context.addItem(new ContextMenuItem("Add Menu Item"));
		EventControl.listen(this.i_context_add_item4, "onclick", this.handleAddMenuItem, this);
		
		this.i_context_add_link4 = this.i_quote_context.addItem(new ContextMenuItem("Add Link"));
		EventControl.listen(this.i_context_add_link4, "onclick", this.handleAddLink, this);
		
		this.i_context_add_quote4 = this.i_quote_context.addItem(new ContextMenuItem("Add Quote"));
		EventControl.listen(this.i_context_add_quote4, "onclick", this.handleAddQuote, this);
		
		this.i_quote_context.addItem(new ContextMenuDivider());
		
		this.i_context_edit_quote = this.i_quote_context.addItem(new ContextMenuItem("Edit Item"));
		EventControl.listen(this.i_context_edit_quote, "onclick", this.handleEditQuote, this);
		
		this.i_context_move_quote_up = this.i_quote_context.addItem(new ContextMenuItem("Move Up"));
		EventControl.listen(this.i_context_move_quote_up, "onclick", this.handleMoveQuoteUp, this);

		this.i_context_move_quote_down = this.i_quote_context.addItem(new ContextMenuItem("Move Down"));
		EventControl.listen(this.i_context_move_quote_down, "onclick", this.handleMoveQuoteDown, this);
		
		this.i_context_delete_quote = this.i_quote_context.addItem(new ContextMenuItem("Delete Item"));
		EventControl.listen(this.i_context_delete_quote, "onclick", this.handleDeleteQuote, this);
	}
	this.i_quote_context.name(e.originalScope.attrheader);
	this.i_quote_context.left(Environment.getCursorX());
	this.i_quote_context.top(Environment.getCursorY());
	this.i_quote_context.visible(true);
	this.i_quote_context.i_scope = e.originalScope;
	e.cancelBubble = true;
	e.returnValue = false;
}



/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Footer Bar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleFooterContext = function(e) {
	if (this.i_footer_context == undefined) {
		this.i_footer_context = new ContextMenu("Footer Bar", 150);
		this.i_context_add_footer = this.i_footer_context.addItem(new ContextMenuItem("Add Footer Link"));
		EventControl.listen(this.i_context_add_footer, "onclick", this.handleAddFooterItem, this);
		
	}
	this.i_footer_context.left(Environment.getCursorX());
	this.i_footer_context.top(Environment.getCursorY());
	this.i_footer_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleEditFooter = function(e) {
	if (e.originalScope.parent().i_scope.id != "home_link") {
		if (this.i_edit_footer_window == undefined) {
			this.i_edit_footer_window = new WindowObject(300, 200, "Edit Footer Item");
			this.i_edit_footer_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
				var sec = new DynamicSection("Footer Item Information", "Footer item configuration information");
					this.i_edit_footer_label = sec.addInput(new DynamicTextInput("Label", "The footer item display text", e.originalScope.parent().i_scope.attrname));
					this.i_edit_footer_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this footer item is selected", e.originalScope.parent().i_scope.attrurl));
				this.i_edit_footer_form.addSection(sec);
			this.i_edit_footer_window.windowContent(this.i_edit_footer_form.getForm());
			EventControl.listen(this.i_edit_footer_form, "onsubmit", this.handleEditFooterSubmit, this);
		}
		this.i_edit_footer_label.value(e.originalScope.parent().i_scope.attrname);
		this.i_edit_footer_url.value(e.originalScope.parent().i_scope.attrurl);
		this.i_edit_footer_form.i_scope = e.originalScope.parent().i_scope;
		this.i_edit_footer_window.center();
		this.i_edit_footer_window.visible(true);
	}
}

SystemCore.prototype.handleEditFooterSubmit = function(e) {
	this.i_edit_footer_window.visible(false);
	if (e.button == "Save") {
		var t = e.originalScope.i_scope;
		t.attrname = this.i_edit_footer_label.value();
		t.attrurl = this.i_edit_footer_url.value();
		t.innerHTML = "&nbsp;|&nbsp;<a href='" + this.i_edit_footer_url.value() + "'>" + this.i_edit_footer_label.value() + "</a>";
		
		var p = new PostData();
		p.param("method", "editFooter");
		p.param("id", t.id);
		p.param("name", this.i_edit_footer_label.value());
		p.param("url", this.i_edit_footer_url.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}

SystemCore.prototype.handleAddFooterItem = function(e) {
	if (this.i_new_footer_window == undefined) {
		this.i_new_footer_window = new WindowObject(300, 200, "New Footer Item");
		
		this.i_new_footer_form = new DynamicForm(300, ['Create', 'Cancel'], 5);
			var sec = new DynamicSection("Footer Item Information", "Footer item configuration information");
				this.i_new_footer_label = sec.addInput(new DynamicTextInput("Label", "The footer item display text", "Tab"));
				this.i_new_footer_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this item is selected", "http://"));
			this.i_new_footer_form.addSection(sec);
		this.i_new_footer_window.windowContent(this.i_new_footer_form.getForm());
		EventControl.listen(this.i_new_footer_form, "onsubmit", this.handleNewFooterSubmit, this);
	}
	this.i_new_footer_label.value("Footer Item");
	this.i_new_footer_url.value("http://");
	this.i_new_footer_window.center();
	this.i_new_footer_window.visible(true);
}


SystemCore.prototype.handleNewFooterSubmit = function(e) {
	this.i_new_footer_window.visible(false);
	if (e.button == "Create") {
		var tbs = document.getElementById('footer_links_holder');
		
		var new_link = document.createElement('SPAN');
		new_link.attrname = this.i_new_footer_label.value();
		new_link.attrurl = this.i_new_footer_url.value();
		new_link.innerHTML = "&nbsp;|&nbsp;<a href='" + this.i_new_footer_url.value() + "'>" + this.i_new_footer_label.value() + "</a>";
		EventControl.listen(new_link, "oncontextmenu", this.handleFooterItemContext, this);
		tbs.appendChild(new_link);
		
		var p = new PostData();
		p.param("method", "newFooter");
		p.param("name", this.i_new_footer_label.value());
		p.param("url", this.i_new_footer_url.value());
		AJAX.request("./index.php?action=ajax", p, this.handleNewFooterFinish, this, new_link);
	}
}

SystemCore.prototype.handleNewFooterFinish = function(req, param) {
	if (req.getData() != 0) {
		param.id = req.getData();
	}
}

SystemCore.prototype.handleDeleteFooter = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('footer_links_holder');
	tbs.removeChild(tab);
	
	var p = new PostData();
	p.param("method", "deleteFooter");
	p.param("id", tab.id);
	AJAX.request("./index.php?action=ajax", p);
}

SystemCore.prototype.handleMoveFooterRight = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('footer_links_holder');
	var tabs = tbs.getElementsByTagName('SPAN');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != tabs.length - 1) {
				if (x == tabs.length - 2) {
					tbs.appendChild(tab);
				}
				else {
					tbs.insertBefore(tab, tabs[x + 2]);
				}
				var p = new PostData();
				p.param("method", "moveFooter");
				p.param("id", tab.id);
				p.param("direction", "1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleMoveFooterLeft = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('footer_links_holder');
	var tabs = tbs.getElementsByTagName('SPAN');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x >= 2) {
				tbs.insertBefore(tab, tabs[x - 1]);
				var p = new PostData();
				p.param("method", "moveFooter");
				p.param("id", tab.id);
				p.param("direction", "-1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleFooterItemContext = function(e) {
	if (this.i_footer_edit_context == undefined) {
		this.i_footer_edit_context = new ContextMenu("", 150);
		
		this.i_context_add_footer2 = this.i_footer_edit_context.addItem(new ContextMenuItem("Add Footer Link"));
		EventControl.listen(this.i_context_add_footer2, "onclick", this.handleAddFooterItem, this);
		
		this.i_footer_edit_context.addItem(new ContextMenuDivider());
		


		this.i_context_edit_footer = this.i_footer_edit_context.addItem(new ContextMenuItem("Edit Item"));
		EventControl.listen(this.i_context_edit_footer, "onclick", this.handleEditFooter, this);

		this.i_context_move_footer_right = this.i_footer_edit_context.addItem(new ContextMenuItem("Move Right"));
		EventControl.listen(this.i_context_move_footer_right, "onclick", this.handleMoveFooterRight, this);

		this.i_context_move_footer_left = this.i_footer_edit_context.addItem(new ContextMenuItem("Move Left"));
		EventControl.listen(this.i_context_move_footer_left, "onclick", this.handleMoveFooterLeft, this);

		this.i_context_delete_footer = this.i_footer_edit_context.addItem(new ContextMenuItem("Delete Item"));
		EventControl.listen(this.i_context_delete_footer, "onclick", this.handleDeleteFooter, this);

	}
	
	if (e.originalScope.attrname == "Site Map") {
		this.i_context_edit_footer.enabled(false);
		this.i_context_move_footer_right.enabled(false);
		this.i_context_move_footer_left.enabled(false);
		this.i_context_delete_footer.enabled(false);
	}
	else {
		this.i_context_edit_footer.enabled(true);
		this.i_context_move_footer_right.enabled(true);
		this.i_context_move_footer_left.enabled(true);
		this.i_context_delete_footer.enabled(true);
	}
	
	this.i_footer_edit_context.name(e.originalScope.attrname);
	this.i_footer_edit_context.left(Environment.getCursorX());
	this.i_footer_edit_context.top(Environment.getCursorY());
	this.i_footer_edit_context.visible(true);
	this.i_footer_edit_context.i_scope = e.originalScope;
	e.cancelBubble = true;
	e.returnValue = false;
}



/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SIDE BAR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleSideContext = function(e) {
	if (this.i_side_context == undefined) {
		this.i_side_context = new ContextMenu("Side Bar", 150);
		this.i_context_add_item = this.i_side_context.addItem(new ContextMenuItem("Add Menu Item"));
		EventControl.listen(this.i_context_add_item, "onclick", this.handleAddMenuItem, this);
		
		this.i_context_add_link = this.i_side_context.addItem(new ContextMenuItem("Add Link"));
		EventControl.listen(this.i_context_add_link, "onclick", this.handleAddLink, this);
		
		this.i_context_add_quote = this.i_side_context.addItem(new ContextMenuItem("Add Quote"));
		EventControl.listen(this.i_context_add_quote, "onclick", this.handleAddQuote, this);
		
	}
	this.i_side_context.left(Environment.getCursorX());
	this.i_side_context.top(Environment.getCursorY());
	this.i_side_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleSideContextH = function(e) {
	var c = document.getElementById('sidebar');
	if (e.type == "mouseover") {
		c.style.backgroundColor = "#f1f8ff";
	}
	else {
		c.style.backgroundColor = "#FFFFFF";
	}
}



/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Link Menu %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleAddLink = function(e) {
	if (this.i_new_link_window == undefined) {
		this.i_new_link_window = new WindowObject(300, 200, "New Link Item");
		
		this.i_new_link_form = new DynamicForm(300, ['Create', 'Cancel'], 5);
			var sec = new DynamicSection("Link Item Information", "Link item configuration information");
				this.i_new_link_label = sec.addInput(new DynamicTextInput("Label", "The link item display text", "Tab"));
				this.i_new_link_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this item is selected", "http://"));
			this.i_new_link_form.addSection(sec);
		this.i_new_link_window.windowContent(this.i_new_link_form.getForm());
		EventControl.listen(this.i_new_link_form, "onsubmit", this.handleNewLinkSubmit, this);
	}
	this.i_new_link_label.value("Link Item");
	this.i_new_link_url.value("http://");
	this.i_new_link_window.center();
	this.i_new_link_window.visible(true);
}

SystemCore.prototype.handleNewLinkSubmit = function(e) {
	this.i_new_link_window.visible(false);
	if (e.button == "Create") {
		var tbs = document.getElementById('side_link');
		
		var new_link = document.createElement('LI');
		new_link.attrname = this.i_new_link_label.value();
		new_link.attrurl = this.i_new_link_url.value();
		new_link.innerHTML = "<a href='" + this.i_new_link_url.value() + "'>" + this.i_new_link_label.value() + "</a>";
		EventControl.listen(new_link, "oncontextmenu", this.handleLinkContext, this);
		tbs.appendChild(new_link);
		
		var p = new PostData();
		p.param("method", "newLink");
		p.param("name", this.i_new_link_label.value());
		p.param("url", this.i_new_link_url.value());
		AJAX.request("./index.php?action=ajax", p, this.handleNewLinkFinish, this, new_link);
	}
}

SystemCore.prototype.handleNewLinkFinish = function(req, param) {
	if (req.getData() != 0) {
		param.id = req.getData();
	}
}

SystemCore.prototype.handleMoveLinkDown = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('side_link');
	var tabs = tbs.getElementsByTagName('LI');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != tabs.length - 1) {
				if (x == tabs.length - 2) {
					tbs.appendChild(tab);
				}
				else {
					tbs.insertBefore(tab, tabs[x + 2]);
				}
				var p = new PostData();
				p.param("method", "moveLink");
				p.param("id", tab.id);
				p.param("direction", "1");
				AJAX.request("./index.php?action=ajax", p);

			}
			break;
		}
	}
}

SystemCore.prototype.handleMoveLinkUp = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('side_link');
	var tabs = tbs.getElementsByTagName('LI');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != 0) {
				tbs.insertBefore(tab, tabs[x - 1]);
				var p = new PostData();
				p.param("method", "moveLink");
				p.param("id", tab.id);
				p.param("direction", "-1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleDeleteLink = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('side_link');
	tbs.removeChild(tab);

	var p = new PostData();
	p.param("method", "deleteLink");
	p.param("id", tab.id);
	AJAX.request("./index.php?action=ajax", p);
}

SystemCore.prototype.handleEditLink = function(e) {
	if (this.i_edit_link_window == undefined) {
		this.i_edit_link_window = new WindowObject(300, 200, "Edit Link Item");
		this.i_edit_link_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("Link Item Information", "Link item configuration information");
				this.i_edit_link_label = sec.addInput(new DynamicTextInput("Label", "The link item display text", e.originalScope.parent().i_scope.attrname));
				this.i_edit_link_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this link item is selected", e.originalScope.parent().i_scope.attrurl));
			this.i_edit_link_form.addSection(sec);
		this.i_edit_link_window.windowContent(this.i_edit_link_form.getForm());
		EventControl.listen(this.i_edit_link_form, "onsubmit", this.handleEditLinkSubmit, this);
	}
	this.i_edit_link_label.value(e.originalScope.parent().i_scope.attrname);
	this.i_edit_link_url.value(e.originalScope.parent().i_scope.attrurl);
	this.i_edit_link_form.i_scope = e.originalScope.parent().i_scope;
	this.i_edit_link_window.center();
	this.i_edit_link_window.visible(true);
}

SystemCore.prototype.handleEditLinkSubmit = function(e) {
	this.i_edit_link_window.visible(false);
	if (e.button == "Save") {
		var t = e.originalScope.i_scope;
		t.attrname = this.i_edit_link_label.value();
		t.attrurl = this.i_edit_link_url.value();
		t.innerHTML = "<a href='" + this.i_edit_link_url.value() + "'>" + this.i_edit_link_label.value() + "</a>";

		var p = new PostData();
		p.param("method", "editLink");
		p.param("id", t.id);
		p.param("name", this.i_edit_link_label.value());
		p.param("url", this.i_edit_link_url.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}

SystemCore.prototype.handleLinkContext = function(e) {
	if (this.i_link_context == undefined) {
		this.i_link_context = new ContextMenu("", 150);
		
		this.i_context_add_item3 = this.i_link_context.addItem(new ContextMenuItem("Add Menu Item"));
		EventControl.listen(this.i_context_add_item3, "onclick", this.handleAddMenuItem, this);
		
		this.i_context_add_link3 = this.i_link_context.addItem(new ContextMenuItem("Add Link"));
		EventControl.listen(this.i_context_add_link3, "onclick", this.handleAddLink, this);
		
		this.i_context_add_quote3 = this.i_link_context.addItem(new ContextMenuItem("Add Quote"));
		EventControl.listen(this.i_context_add_quote3, "onclick", this.handleAddQuote, this);
		
		this.i_link_context.addItem(new ContextMenuDivider());
		
		this.i_context_edit_link = this.i_link_context.addItem(new ContextMenuItem("Edit Item"));
		EventControl.listen(this.i_context_edit_link, "onclick", this.handleEditLink, this);
		
		this.i_context_move_link_up = this.i_link_context.addItem(new ContextMenuItem("Move Up"));
		EventControl.listen(this.i_context_move_link_up, "onclick", this.handleMoveLinkUp, this);

		this.i_context_move_link_down = this.i_link_context.addItem(new ContextMenuItem("Move Down"));
		EventControl.listen(this.i_context_move_link_down, "onclick", this.handleMoveLinkDown, this);
		
		this.i_context_delete_link = this.i_link_context.addItem(new ContextMenuItem("Delete Item"));
		EventControl.listen(this.i_context_delete_link, "onclick", this.handleDeleteLink, this);
	}
	this.i_link_context.name(e.originalScope.attrname);
	this.i_link_context.left(Environment.getCursorX());
	this.i_link_context.top(Environment.getCursorY());
	this.i_link_context.visible(true);
	this.i_link_context.i_scope = e.originalScope;
	e.cancelBubble = true;
	e.returnValue = false;
}


SystemCore.prototype.handleLinkContextH = function(e) {
	var c = document.getElementById('side_link');
	if (e.type == "mouseover") {
		c.style.backgroundColor = "#e2f0ff";
	}
	else {
		c.style.backgroundColor = "#FFFFFF";
	}
}

/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Main Menu %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleAddMenuItem = function(e) {
	if (this.i_new_menu_window == undefined) {
		this.i_new_menu_window = new WindowObject(300, 200, "New Menu Item");
		
		this.i_new_menu_form = new DynamicForm(300, ['Create', 'Cancel'], 5);
			var sec = new DynamicSection("Menu Item Information", "Menu item configuration information");
				this.i_new_menu_label = sec.addInput(new DynamicTextInput("Label", "The menu item display text", "Tab"));
				this.i_new_menu_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this item is selected", "http://"));
			this.i_new_menu_form.addSection(sec);
		this.i_new_menu_window.windowContent(this.i_new_menu_form.getForm());
		EventControl.listen(this.i_new_menu_form, "onsubmit", this.handleNewMenuSubmit, this);
	}
	this.i_new_menu_label.value("Menu Item");
	this.i_new_menu_url.value("http://");
	this.i_new_menu_window.center();
	this.i_new_menu_window.visible(true);
}

SystemCore.prototype.handleNewMenuSubmit = function(e) {
	this.i_new_menu_window.visible(false);
	if (e.button == "Create") {
		var tbs = document.getElementById('side_menu');
		
		var new_menu = document.createElement('LI');
		new_menu.attrname = this.i_new_menu_label.value();
		new_menu.attrurl = this.i_new_menu_url.value();
		new_menu.innerHTML = "<a href='" + this.i_new_menu_url.value() + "'>" + this.i_new_menu_label.value() + "</a>";
		EventControl.listen(new_menu, "oncontextmenu", this.handleMenuContext, this);
		tbs.appendChild(new_menu);
		
		var p = new PostData();
		p.param("method", "newMenu");
		p.param("name", this.i_new_menu_label.value());
		p.param("url", this.i_new_menu_url.value());
		AJAX.request("./index.php?action=ajax", p, this.handleNewMenuFinish, this, new_menu);
	}
}

SystemCore.prototype.handleNewMenuFinish = function(req, param) {
	if (req.getData() != 0) {
		param.id = req.getData();
	}
}

SystemCore.prototype.handleMoveMenuDown = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('side_menu');
	var tabs = tbs.getElementsByTagName('LI');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != tabs.length - 1) {
				if (x == tabs.length - 2) {
					tbs.appendChild(tab);
				}
				else {
					tbs.insertBefore(tab, tabs[x + 2]);
				}
				var p = new PostData();
				p.param("method", "moveMenu");
				p.param("id", tab.id);
				p.param("direction", "1");
				AJAX.request("./index.php?action=ajax", p);
			}
			break;
		}
	}
}

SystemCore.prototype.handleMoveMenuUp = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('side_menu');
	var tabs = tbs.getElementsByTagName('LI');
	
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x] == tab) {
			if (x != 0) {
				tbs.insertBefore(tab, tabs[x - 1]);
				var p = new PostData();
				p.param("method", "moveMenu");
				p.param("id", tab.id);
				p.param("direction", "-1");
				AJAX.request("./index.php?action=ajax", p);

			}
			break;
		}
	}
}

SystemCore.prototype.handleDeleteMenu = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('side_menu');
	tbs.removeChild(tab);
	
	var p = new PostData();
	p.param("method", "deleteMenu");
	p.param("id", tab.id);
	AJAX.request("./index.php?action=ajax", p);
}

SystemCore.prototype.handleEditMenu = function(e) {
	if (this.i_edit_menu_window == undefined) {
		this.i_edit_menu_window = new WindowObject(300, 200, "Edit Menu Item");
		this.i_edit_menu_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("Menu Item Information", "Menu item configuration information");
				this.i_edit_menu_label = sec.addInput(new DynamicTextInput("Label", "The menu item display text", e.originalScope.parent().i_scope.attrname));
				this.i_edit_menu_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this menu item is selected", e.originalScope.parent().i_scope.attrurl));
			this.i_edit_menu_form.addSection(sec);
		this.i_edit_menu_window.windowContent(this.i_edit_menu_form.getForm());
		EventControl.listen(this.i_edit_menu_form, "onsubmit", this.handleEditMenuSubmit, this);
	}
	this.i_edit_menu_label.value(e.originalScope.parent().i_scope.attrname);
	this.i_edit_menu_url.value(e.originalScope.parent().i_scope.attrurl);
	this.i_edit_menu_form.i_scope = e.originalScope.parent().i_scope;
	this.i_edit_menu_window.center();
	this.i_edit_menu_window.visible(true);
}

SystemCore.prototype.handleEditMenuSubmit = function(e) {
	this.i_edit_menu_window.visible(false);
	if (e.button == "Save") {
		var t = e.originalScope.i_scope;
		t.attrname = this.i_edit_menu_label.value();
		t.attrurl = this.i_edit_menu_url.value();
		t.innerHTML = "<a href='" + this.i_edit_menu_url.value() + "'>" + this.i_edit_menu_label.value() + "</a>";
		
		var p = new PostData();
		p.param("method", "editMenu");
		p.param("id", t.id);
		p.param("name", this.i_edit_menu_label.value());
		p.param("url", this.i_edit_menu_url.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}

SystemCore.prototype.handleMenuContext = function(e) {
	if (this.i_menu_context == undefined) {
		this.i_menu_context = new ContextMenu("", 150);
		
		this.i_context_add_item2 = this.i_menu_context.addItem(new ContextMenuItem("Add Menu Item"));
		EventControl.listen(this.i_context_add_item2, "onclick", this.handleAddMenuItem, this);
		
		this.i_context_add_link2 = this.i_menu_context.addItem(new ContextMenuItem("Add Link"));
		EventControl.listen(this.i_context_add_link2, "onclick", this.handleAddLink, this);
		
		this.i_context_add_quote2 = this.i_menu_context.addItem(new ContextMenuItem("Add Quote"));
		EventControl.listen(this.i_context_add_quote2, "onclick", this.handleAddQuote, this);
		
		this.i_menu_context.addItem(new ContextMenuDivider());
		
		this.i_context_edit_menu = this.i_menu_context.addItem(new ContextMenuItem("Edit Item"));
		EventControl.listen(this.i_context_edit_menu, "onclick", this.handleEditMenu, this);
		
		this.i_context_move_menu_up = this.i_menu_context.addItem(new ContextMenuItem("Move Up"));
		EventControl.listen(this.i_context_move_menu_up, "onclick", this.handleMoveMenuUp, this);

		this.i_context_move_menu_down = this.i_menu_context.addItem(new ContextMenuItem("Move Down"));
		EventControl.listen(this.i_context_move_menu_down, "onclick", this.handleMoveMenuDown, this);
		
		this.i_context_delete_menu = this.i_menu_context.addItem(new ContextMenuItem("Delete Item"));
		EventControl.listen(this.i_context_delete_menu, "onclick", this.handleDeleteMenu, this);
	}
	this.i_menu_context.name(e.originalScope.attrname);
	this.i_menu_context.left(Environment.getCursorX());
	this.i_menu_context.top(Environment.getCursorY());
	this.i_menu_context.visible(true);
	this.i_menu_context.i_scope = e.originalScope;
	e.cancelBubble = true;
	e.returnValue = false;
}


/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TAB MANAGEMENT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

SystemCore.prototype.handleEditTab = function(e) {
	if (this.i_edit_tab_window == undefined) {
		this.i_edit_tab_window = new WindowObject(300, 200, "Edit Tab");
		this.i_edit_tab_form = new DynamicForm(300, ['Save', 'Cancel'], 5);
			var sec = new DynamicSection("Tab Information", "Tab configuration information");
				this.i_edit_tab_label = sec.addInput(new DynamicTextInput("Label", "The tab display text", e.originalScope.parent().i_scope.attrname));
				this.i_edit_tab_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this tab is selected", e.originalScope.parent().i_scope.attrurl));
			this.i_edit_tab_form.addSection(sec);
		this.i_edit_tab_window.windowContent(this.i_edit_tab_form.getForm());
		EventControl.listen(this.i_edit_tab_form, "onsubmit", this.handleEditTabSubmit, this);
	}
	this.i_edit_tab_label.value(e.originalScope.parent().i_scope.attrname);
	this.i_edit_tab_url.value(e.originalScope.parent().i_scope.attrurl);
	this.i_edit_tab_form.i_scope = e.originalScope.parent().i_scope;
	this.i_edit_tab_window.center();
	this.i_edit_tab_window.visible(true);
}

SystemCore.prototype.handleEditTabSubmit = function(e) {
	this.i_edit_tab_window.visible(false);
	if (e.button == "Save") {
		var t = e.originalScope.i_scope;
		t.attrname = this.i_edit_tab_label.value();
		t.attrurl = this.i_edit_tab_url.value();
		var it = t.getElementsByTagName('DIV');
		it[0].innerHTML = "<a href='" + this.i_edit_tab_url.value() + "'>" + this.i_edit_tab_label.value() + "</a>";
		
		var p = new PostData();
		p.param("method", "editTab");
		p.param("id", t.id);
		p.param("name", this.i_edit_tab_label.value());
		p.param("url", this.i_edit_tab_url.value());
		AJAX.request("./index.php?action=ajax", p);
	}
}

SystemCore.prototype.handleMoveTabLeft = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('tab_links');
	var tabs = tbs.getElementsByTagName('DIV');
	
	var real_tabs = Array();
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x].className == "tabDiv") {
			real_tabs[real_tabs.length] = tabs[x];
		}
	}
	
	for (var x = 0; x < real_tabs.length; x++) {
		if (real_tabs[x].className = "tabDiv") {
			if (real_tabs[x] == tab) {
				if (x != real_tabs.length - 1) {
					if (x == real_tabs.length - 2) {
						tbs.appendChild(tab);
					}
					else {
						tbs.insertBefore(tab, real_tabs[x + 2]);
					}
					var p = new PostData();
					p.param("method", "moveTab");
					p.param("id", tab.id);
					p.param("direction", "1");
					AJAX.request("./index.php?action=ajax", p);
				}
				break;
			}
		}
	}
}

SystemCore.prototype.handleMoveTabRight = function(e) {
	var tab = e.originalScope.parent().i_scope;
	var tab = e.originalScope.parent().i_scope;
	var tbs = document.getElementById('tab_links');
	var tabs = tbs.getElementsByTagName('DIV');
	
	var real_tabs = Array();
	for (var x = 0; x < tabs.length; x++) {
		if (tabs[x].className == "tabDiv") {
			real_tabs[real_tabs.length] = tabs[x];
		}
	}
	
	for (var x = 0; x < real_tabs.length; x++) {
		if (real_tabs[x].className = "tabDiv") {
			if (real_tabs[x] == tab) {
				if (x != 0) {
					tbs.insertBefore(tab, real_tabs[x - 1]);
					var p = new PostData();
					p.param("method", "moveTab");
					p.param("id", tab.id);
					p.param("direction", "-1");
					AJAX.request("./index.php?action=ajax", p);
					
				}
				break;
			}
		}
	}
}

SystemCore.prototype.handleDeleteTab = function(e) {
	var tab = e.originalScope.parent().i_scope;
	if (tab.id != undefined) {
		var tbs = document.getElementById('tab_links');
		tbs.removeChild(tab);
		var p = new PostData();
		p.param("method", "deleteTab");
		p.param("id", tab.id);
		AJAX.request("./index.php?action=ajax", p);
	}
	else {
		alert('This tab has not been saved to the server yet, you may not delete it.  Please refresh');
	}
}

SystemCore.prototype.handleTabContext = function(e) {
	if (this.i_tab_context == undefined) {
		this.i_tab_context = new ContextMenu("", 150);
		
		this.i_context_add_tab2 = this.i_tab_context.addItem(new ContextMenuItem("Add Tab"));
		EventControl.listen(this.i_context_add_tab2, "onclick", this.handleAddTab, this);

		this.i_tab_context.addItem(new ContextMenuDivider());
		
		this.i_context_edit_tab = this.i_tab_context.addItem(new ContextMenuItem("Edit Tab"));
		EventControl.listen(this.i_context_edit_tab, "onclick", this.handleEditTab, this);
		
		this.i_context_move_tab_right = this.i_tab_context.addItem(new ContextMenuItem("Move Right"));
		EventControl.listen(this.i_context_move_tab_right, "onclick", this.handleMoveTabRight, this);

		this.i_context_move_tab_left = this.i_tab_context.addItem(new ContextMenuItem("Move Left"));
		EventControl.listen(this.i_context_move_tab_left, "onclick", this.handleMoveTabLeft, this);
		
		this.i_context_delete_tab = this.i_tab_context.addItem(new ContextMenuItem("Delete Tab"));
		EventControl.listen(this.i_context_delete_tab, "onclick", this.handleDeleteTab, this);
	}
	this.i_tab_context.name(e.originalScope.attrname);
	this.i_tab_context.left(Environment.getCursorX());
	this.i_tab_context.top(Environment.getCursorY());
	this.i_tab_context.visible(true);
	this.i_tab_context.i_scope = e.originalScope;
	e.cancelBubble = true;
	e.returnValue = false;
}

SystemCore.prototype.handleNewTabSubmit = function(e) {
	this.i_new_tab_window.visible(false);
	if (e.button == "Create") {
		var tbs = document.getElementById('tab_links');
		
		var new_tab = document.createElement('DIV');
		new_tab.className = "tabDiv";
		new_tab.attrname = this.i_new_tab_label.value();
		new_tab.attrurl = this.i_new_tab_url.value();
			var new_tab_i = document.createElement('DIV');
			new_tab_i.className = "tabDiv2";
			new_tab_i.innerHTML = "<a href='" + this.i_new_tab_url.value() + "'>" + this.i_new_tab_label.value() + "</a>";
			new_tab.appendChild(new_tab_i);
		EventControl.listen(new_tab, "oncontextmenu", this.handleTabContext, this);
		tbs.appendChild(new_tab);
		
		var p = new PostData();
		p.param("method", "newTab");
		p.param("name", this.i_new_tab_label.value());
		p.param("url", this.i_new_tab_url.value());
		AJAX.request("./index.php?action=ajax", p, this.handleNewTabFinish, this, new_tab);
	}
}

SystemCore.prototype.handleNewTabFinish = function(req, param) {
	if (req.getData() != 0) {
		param.id = req.getData();
	}
}

SystemCore.prototype.handleAddTab = function(e) {
	if (this.i_new_tab_window == undefined) {
		this.i_new_tab_window = new WindowObject(300, 200, "New Tab");
		
		this.i_new_tab_form = new DynamicForm(300, ['Create', 'Cancel'], 5);
			var sec = new DynamicSection("Tab Information", "Tab configuration information");
				this.i_new_tab_label = sec.addInput(new DynamicTextInput("Label", "The tab display text", "Tab"));
				this.i_new_tab_url = sec.addInput(new DynamicTextInput("URL", "The URL to redirect users to when this tab is selected", "http://"));
			this.i_new_tab_form.addSection(sec);
		this.i_new_tab_window.windowContent(this.i_new_tab_form.getForm());
		EventControl.listen(this.i_new_tab_form, "onsubmit", this.handleNewTabSubmit, this);
	}
	this.i_new_tab_label.value("Tab");
	this.i_new_tab_url.value("http://");
	this.i_new_tab_window.center();
	this.i_new_tab_window.visible(true);
}

SystemCore.prototype.handleHeaderContext = function(e) {
	if (this.i_header_context == undefined) {
		this.i_header_context = new ContextMenu("Header", 150);
		this.i_context_add_tab = this.i_header_context.addItem(new ContextMenuItem("Add Tab"));
		EventControl.listen(this.i_context_add_tab, "onclick", this.handleAddTab, this);
		
	}
	this.i_header_context.left(Environment.getCursorX());
	this.i_header_context.top(Environment.getCursorY());
	this.i_header_context.visible(true);
	e.cancelBubble = true;
	e.returnValue = false;
}


/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% BOOTSTRAP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/


SystemCore.init = function() {
	Environment.init();
	SystemCore.ins = new SystemCore();
	SystemCore.ins.init();
}


function toggleQuestion(s) {
	var id = s.id;
	id = id.replace("q", "a");
	var a = document.getElementById(id);
	a.style.display = (s.st == true ? "none" : "");
	s.st = (s.st == true ? false : true);
	
}
