/*
	common.js - 20070218 for www.cyanworks.net
		Requires mootools (http://mootools.net/) Ajax.js
*/

var quickFeedback;

function init()
{
	quickFeedback = new QuickFeedback("feedbackForm", "feedbackResult");
	rewriteForm();
//	rewriteMail();
}

function rewriteForm()
{
	if ($("feedbackForm"))
		$("feedbackForm").innerHTML = "<fieldset><div><textarea name='comment' cols='44' rows='6'></textarea></div><div><input type='hidden' name='mode' value='submitAjax' /><input type='button' value='Send' onclick='quickFeedback.send();'/> <span id='feedbackResult'>Message to the WebMaster</span></div></fieldset>";
}

/*
function rewriteMail()
{
	var imgs = document.getElementsByTagName("img");
	for (var i = imgs.length - 1; 0 <= i; i--) {
		if (imgs[i].src.match(/cyanworks\.png/)) {
			var anchor = document.createElement("a");
			anchor.innerText  = "ADDRESS";
			anchor.href = "ADDRESS";
			imgs[i].parentNode.replaceChild(anchor, imgs[i]);
		}
	}
}
*/

// ================================================
// QuickFeedback
// ================================================
var QuickFeedback = function(formID, resultID) {
	this.formID = formID;
	this.resultID = resultID;
	this.uri = "./cgi-bin/quickFeedback/feedback2009NV08.cgi";
}

QuickFeedback.prototype.send = function() {
	var referer = window.location.href;
	var queryString = $(this.formID).toQueryString() + "&referer=" + referer;

	var myAjax = new Ajax(
		this.uri,
		{
			method: "post", 
			postBody: queryString,
			update: $(this.resultID),
			onComplete: this.showResponse.bind(this),
			onFailure: this.showError.bind(this)
		});
	$(this.resultID).innerHTML = "Sending...";
	myAjax.request();
}

QuickFeedback.prototype.showResponse = function() {
	$(this.formID).reset();
}

QuickFeedback.prototype.showError = function() {
	$(this.resultID).innerHTML = "Sorry. There was an error.";
}
