// rating.js
// Andrew J. Miller
// 2008-02-15

//xhr = new Object();
//browser.cache.check_doc_frequency = 3
var pagerateformel = '<div class="helpful">' + 
			 '<span class="question">Was this page helpful? </span>' + 
			 '<span class="label">Yes</span><input type="radio" class="answer" name="Helpful" value="Y" onclick="pagerateclick(this);"/>' + 
			 '<span class="label">No</span><input type="radio" class="answer" name="Helpful" value="N" onclick="pagerateclick(this);"/>' + 
			 '</div>'; //+ 
			//'<div class="rating">' + 
			//'<span class="question">Rate this page on a scale of 1-10 </span>' + //
			//'<input type="text" class="answer" name="Rating" value="" onchange="UploadValue(this);"/>' + //
			//'</div>';
var pageratethanks = '<span class="Response">Thank you for rating this page.</span>';

var pageratecomments = '<p><span class="question">Please tell us how we can improve this page:</span></p>' + 
               '<div><textarea name="comments" id="ratingcomments" class="ratingcomments" rows="5" cols="60"></textarea></div>' + 
               '<div><input type="submit" onclick="UploadValue(\'N\');" value="Submit"/></div>';

document.write('<div id="pagerate"><form method="post" action="javascript:void(0);" id="pagerateform" >' + pagerateformel + '</form></div>');

//--------------------------------------------------------------------------------------------------
function pagerateclick(el)
{
	
	if (el.value == 'N')
	{
		var form = document.getElementById('pagerateform');
		form.innerHTML = pageratecomments;
		document.getElementById('ratingcomments').focus();
		document.getElementById('ratingcomments').scrollIntoView();
	}	
	else
	{
		UploadValue('Y');
	}
}

//--------------------------------------------------------------------------------------------------
function UploadValue(val)
{
	var el = document.getElementById('pagerate');
	el.xhrHandler = HandleResponse;

	var s = XhrEncodeValue('Helpful', val);
		s += '&' + XhrEncodeValue('Page', document.location);
		s += '&' + XhrEncodeValue('Referrer', document.referrer);

	if (val == 'N')
		s += '&' + XhrEncodeValue('Comments', document.getElementById('ratingcomments').value);
		
	var url = '/includes/page_rating/upload.php';
	var xhr = XhrNewPost(url, el.xhrHandler, s);
	xhr.el = el;
	xhr.init();
}

//--------------------------------------------------------------------------------------------------
function HandleResponse(action)
{
	this.el.innerHTML = pageratethanks;
}


//--------------------------------------------------------------------------------------------------
// code below from xmlhttprequest.js also by AJM
//--------------------------------------------------------------------------------------------------
// Xhr constructor
function Xhr(url, meth, handler, content)
{
	this.url = url;
	this.meth = meth;
	this.handler = handler;
	this.content = content;
	
	if (!this.req)
	{
		this.isIE = false;
		if (window.XMLHttpRequest)
		{
			this.req = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			this.isIE = true;
			this.req = new ActiveXObject("Msxml2.XMLHTTP");
			if (! this.req)
			{
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}	

	// the "this" is not reliably defined to be the XMLHttpRequest object
	// in the onreadystatechange handler. use an anonymous function that
	// invodes the ReadyStateChange method of the xhr object.
	// create local xhr to hold current value of "this" to use in the function.
	// remember, variables in functions hold values from context in which functions
	// are defined. Thus xhr is defined to be a pointer back to object pointed to
	// by "this" within this constructor.
	var xhr = this;
	this.req.onreadystatechange = function() {xhr.ReadyStateChange();}
	
}

//--------------------------------------------------------------------------------------------------
// Xhr onreadystatechange event handler
function XhrReadyStateChange()
{
// 	if (this.req.readyState == 3)
// 	{
// 		if (this.req.status == 200)
// 		{
// 			this.handler('progress');
// 		}
// 		else
// 		{
// 			this.handler('err');
// 		}
// 	
// 	}
// 	else
	if (this.req.readyState == 4)
	{
		if (this.req.status == 200)
		{
			this.handler('run');
		}
		else
		{
			this.handler('err');
		}
	}
}

//--------------------------------------------------------------------------------------------------
Xhr.prototype.ReadyStateChange = XhrReadyStateChange;
//

//--------------------------------------------------------------------------------------------------
// Xhr transmission
function XhrInit()
{
	try
	{
		this.req.open(this.meth, this.url, true);
		
		if (this.meth.toLowerCase() == "post")		
		{
			this.req.setRequestHeader("Content-Type", 
								 "application/x-www-form-urlencoded; charset=UTF-8");
			this.req.send(this.content);
		}
		else
		{
			this.req.setRequestHeader("Content-Type", 
								 "application/x-www-form-urlencoded; charset=UTF-8");
			this.req.send(null); // firefox (gecko) chokes if argument is missing.
		}
	}
	catch (errv)
	{
		alert("The application cannot contact " + 
		      "the server at the moment." + 
		      "Please try again in a few seconds.\n" + 
		      "Error detail: " + errv.message);
	}
}

//--------------------------------------------------------------------------------------------------
Xhr.prototype.init = XhrInit;

//--------------------------------------------------------------------------------------------------
// convenience function to use Xhr with get method
function XhrGetDoc(url, handler)
{
	var xhr = new Xhr(url, "GET", handler, '');
	
	xhr.init();
	
	return xhr;
}

//--------------------------------------------------------------------------------------------------
// convenience function to use Xhr with post method
function XhrPostDoc(url, handler, content)
{
	var xhr = new Xhr(url, "POST", handler, content);
	
	xhr.init();
	
	return xhr;
}

//--------------------------------------------------------------------------------------------------
// convenience function to use Xhr with post method. does not init
function XhrNewPost(url, handler, content)
{
	var xhr = new Xhr(url, "POST", handler, content);
	return xhr;
}

//--------------------------------------------------------------------------------------------------
// convenience function to use Xhr with post method. does not init
function XhrNewGet(url, handler)
{
	var xhr = new Xhr(url, "GET", handler,'');
	return xhr;
}

//--------------------------------------------------------------------------------------------------
function XhrEncodeForm(frm)
{
	var s = '';
	var c = '';
	var nel = frm.elements.length;
	for (var i = 0; i < nel; i++)
	{
		s += c + frm.elements[i].name + "=" + 
		     encodeURIComponent(frm.elements[i].value);
		c = "&";
	}
	
	return s;
}

//--------------------------------------------------------------------------------------------------
function XhrEncodeValue(name, val)
{
	var s = name + '=' + encodeURIComponent(val);
	
	return s;
}

