/*  rate.js
 *  JavaScript for rating text toggle.
 *
/*--------------------------------------------------------------------------*/


/* TODO:
 *
 */

function rate_article(boolHelped,idOldDiv,idNewDiv) {
	if (document.URL.indexOf('feature_article') >= 0) {
		var id = getURLParam("contentXML");
		var boolFeatured=1;
	} else if (document.URL.indexOf('archive_article') >= 0) {
		var id = getURLParam("document");
		var boolFeatured=0;
	}
		
	channelCode = getURLParam("channelCode");

	var opt = {
		// Use POST
		method: 'post',
		// define post data
		postBody: '&boolHelpful=' + boolHelped 
				+ '&boolFeatured=' + boolFeatured 
				+ '&idArticle=' + id
				+ '&chrArticle=' + id.substr(id.lastIndexOf('/')+1)
				+ '&chrChannelCode=' + channelCode,
		// don't tie up browser wiating for transaction to complete
		onSuccess: function(t) {
			document.cookie="rated_"+id.substr(id.lastIndexOf('/')+1)+"=true; path=/; expires=Sun, 18 Jan 2038 00:00:00 GMT;";
			new Effect.Fade(document.getElementById(idOldDiv));
			new Effect.Appear(document.getElementById(idNewDiv));
		},
		asynchronous:true
	}

	new Ajax.Request('/cgi_root/en_US/rating.cgi', opt);
	return false;
}

function checkRated(idOldDiv,idNewDiv) {
	cookies = document.cookie;
	elmOldDiv = document.getElementById(idOldDiv);
	elmNewDiv = document.getElementById(idNewDiv);
	
	if (document.URL.indexOf('feature_article') >= 0) {
		var id = getURLParam("contentXML");
		id=id.substr(id.lastIndexOf('/')+1);
	} else if (document.URL.indexOf('archive_article') >= 0) {
		var id = getURLParam("document");
		id=id.substr(id.lastIndexOf('/')+1);
	}

	if (!cookies || cookies == "") {
		elmOldDiv.style.display = "inline";
		elmOldDiv.style.visibility = "visible";
		elmNewDiv.style.display = "none";
		elmNewDiv.style.visibility = "visible";
		return false;
	} else {
		thisCookie = cookies.split("; ")
		for (i=0; i<thisCookie.length; i++) {
			if (thisCookie[i] == "rated_"+id+"=true") {
				elmOldDiv.style.display = "none";
				elmOldDiv.style.visibility = "visible";
				elmNewDiv.style.display = "inline";
				elmNewDiv.style.visibility = "visible";
				return true;
			}
		}
		elmOldDiv.style.display = "inline";
		elmOldDiv.style.visibility = "visible";
		elmNewDiv.style.display = "none";
		elmNewDiv.style.visibility = "visible";
		return false;
	}
}

function getURLParam( name ) {
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return false;
  else
    return results[1];
}