// JavaScript Document
// routines to put up a YUI dialog that gets server configuration data from the user

YAHOO.namespace("fbDialog.container");

function init() {
	
	// Define various event handlers for Dialog
	var handleSubmit = function() {
		submitFeedback();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		var response = o.responseText;
		alert (response);
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.fbDialog.container.feedbackDialog = new YAHOO.widget.Dialog("feedbackDialog", 
							{ width : "500px",
							  zIndex : 9999,
							  fixedcenter : true,
							  visible : false,
							  modal : true, 
							  constraintoviewport : true,
							  buttons : [ { text:"submit", handler:handleSubmit, isDefault:true },
								      { text:"cancel", handler:handleCancel } ]
							});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.fbDialog.container.feedbackDialog.validate = function() {
		var data = this.getData();
		if (data.firstname == "" || data.lastname == "") {
			alert("Please enter your first and last names.");
			return false;
		} else {
			return true;
		}
	};

	// Wire up the success and failure handlers
	YAHOO.fbDialog.container.feedbackDialog.callback = { success: handleSuccess,
						     failure: handleFailure };
	
	// Render the Dialog
	YAHOO.fbDialog.container.feedbackDialog.render();

}

function getFeedback () {
	document.getElementById('fb_email').value = "";
	document.getElementById('fb_comment').value = "";
//	document.getElementById('feedbackDialog').style.display = "";
	YAHOO.fbDialog.container.feedbackDialog.show();
	document.getElementById("feedbackDialog_c").style.zIndex = 1000;
	document.getElementById("feedbackDialog_mask").style.zIndex = 999;
	document.getElementById("fb_email").focus();
}

var xmlHttp;

function submitFeedback () {

	var fb_type = getSelectedRadioValue(document.getElementsByName("fb_type"));
	if (fb_type === "") {
		alert ("Please select the type of comment.");
		document.getElementById("fb_type1").focus();
		return false;
	}

	// ok - ready to test data
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = confirmFeedback;
	var url="rm_getHTTPrequestData.php?REQ_TYPE=saveFeedback";
	url += "&EMAIL="+document.getElementById('fb_email').value;
	url += "&FB_TYPE="+fb_type;
	url += "&COMMENT="+encodeURIComponent(document.getElementById("fb_comment").value);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function confirmFeedback () {
	if (xmlHttp.readyState == 4) {
		
		if (xmlHttp.responseText == "OK") {
			alert ("Thanks for your feedback!");
			YAHOO.fbDialog.container.feedbackDialog.hide();
		} else {
			alert ("We must not have liked what you said.");
		}
	}
}

YAHOO.util.Event.onDOMReady(init);

document.write('<div id="feedbackDialog">'+
'<h4>ideas, bugs, &amp; feedback</h4>'+
'<p>'+
'We&rsquo;re always eager to hear from you, but especially so during our beta period.</p>'+
'<p>'+
'Whether you have encountered a bug (or something that just doesn&rsquo;t seem to be working the way you think it should), or have '+
'suggestions for how we could make remail.me easier, clearer, or more useful, please tell us!</p>'+
'<p>'+
'If you feel comfortable doing so, please include your email address so that we may contact you if we have questions.</p>'+
'<p>'+
'Thanks!</p>'+
'<br />'+
'<label class="lbl" for="fb_email">email:</label>'+
'	<input type="text" name="fb_email" id="fb_email" value="" class="infobox" style="width: 250px;" onchange="CheckEmail(this,true);" />'+
'<div class="clear">&nbsp;</div>'+
'<div style="margin-left: 55px;">'+
'<input type="radio" name="fb_type" id="fb_type1" value="bug"  /> I think I found a bug<br />'+
'<input type="radio" name="fb_type" value="suggestion"  /> I have a suggestion<br />'+
'<input type="radio" name="fb_type" value="feedback"  /> I want to give you feedback'+
'<div class="clear">&nbsp;</div>'+
'comment:<br />'+
'</div>'+
'	<textarea name="fb_comment" id="fb_comment" class="textArea" rows="7"  style="margin-left: 30px; width: 400px;"></textarea>'+
'<div class="clear">&nbsp;</div>'+
'</div>');

