function addEvent(ctl,eventType,eventFunction){if(ctl.attachEvent){ctl.attachEvent("on"+eventType,eventFunction);}else if(ctl.addEventListener){ctl.addEventListener(eventType,eventFunction,false);}else{ctl["on"+eventType]=eventFunction;}}
function removeEvent(ctl,eventType,eventFunction){if(ctl.detachEvent){ctl.detachEvent("on"+eventType,eventFunction);}else if(ctl.removeEventListener){ctl.removeEventListener(eventType,eventFunction,false);}else{ctl["on"+eventType]=function(){};}}
function stopEvent(e){if(e.stopPropagation){e.stopPropagation();e.preventDefault();}else{e.returnValue=false;e.cancelBubble=true;}}
function $(id){return document.getElementById(id);}
function AsyncRequest(){
	this.method;
	this.url;
	this.cache=false;
	this.data;
	this.callback;
	this.onError=null;
	this.xmlHttp=new function(){
		try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
		try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
		try{return new XMLHttpRequest();}catch(e){}
		alert("XMLHttpRequest not supported. Please contact technical support.");
		return null;
	}
	this.send=function(){
		with(this){
			if(method=="GET"){
				if(data.left(1)!='?'){
					url=url+"?"+data;
				}else{
					url=url+data;
				}
				xmlHttp.open(method,url,true);
			}else{
				xmlHttp.open(method,url,true);
			}
			xmlHttp.onreadystatechange=function(){
				if(xmlHttp.readyState==4){
					var responseText=xmlHttp.responseText;
					switch(xmlHttp.status){
						case 404:
							if(onError==null){
								alert('Page Not Found. The requested url \''+url+'\' could not be found.');
							}else{
								onError('Page Not Found. The requested url \''+url+'\' could not be found.');
							}
						break;
						case 405:
							if(onError==null){
								alert('Cannot post form data to this page. The requested url \''+url+'\' could be missing.');
							}else{
								onError('Cannot post form data to this page. The requested url \''+url+'\' could be missing.');
							}
						break;
						case 500:
							if(onError==null){
								alert(responseText);
							}else{
								onError(responseText);
							}
						break;
						case 200:
							if(responseText.indexOf('Error:')>-1||responseText.indexOf('Debug:')>-1){
								if(onError==null){
									alert(responseText);
								}else{
									onError(responseText);
								}
							}else{
								callback(responseText);
							}
						break;
						default:
							if(onError==null){
								alert('Undefined error code: '+xmlHttp.status+'. Please report this to technical support');
								alert(responseText);
							}else{
								onError(responseText);
							}
						break;
					}
				}
			}
			xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			if(!cache&&method!="POST"){
				xmlHttp.setRequestHeader('If-Modified-Since','Mon, 1 Jan 1996 00:00:00 GMT');
			}
			if(method=="GET"){
				xmlHttp.send(null);
			}else{
				xmlHttp.send(data);
			}
		}
	}
}


function setRate(ctlid,rating,articleId,onclass,offclass) {
	$('pRateValue').value=rating.toString();
	$('pRateId').value=articleId.toString();
	var baseid=ctlid.substr(0,ctlid.length-1);
	for(i=1;i<=rating;i++) $(baseid+i.toString()).className=onclass;
	for(j=rating+1;j<=5;j++) $(baseid+j.toString()).className=offclass;
}

addEvent(window,"load",initRateArticle);

function initRateArticle(){
	var pRate=new RateArticle();
}

RateArticle=function() {
	var pRateForm=$('pRateForm');
	var pRateSubmit=$('pRateSubmit');
	var pRateId=$('pRateId');
	var pRateValue=$('pRateValue');
	var pRateHeader=$('pRateHeader');
	var unit1=$('unit1');
	var unit2=$('unit2');
	var unit3=$('unit3');
	var unit4=$('unit4');
	var unit5=$('unit5');
	var pRateRequest=new AsyncRequest();

	function DoRateArticle() {
		if(pRateId.value<=0) {
			alert('Invalid article information for rating.');
			return false;
		}
		if((pRateValue.value<1)||(pRateValue.value>5)) {
			alert('Invalid rate value.  Select rate value 1-5.');
			return false;
		}
		pRateRequest.data="pid="+pRateId.value+"&rating="+pRateValue.value;
		pRateRequest.method="POST";
		pRateRequest.url=pRateForm.action;
		pRateRequest.cache=false;
		pRateRequest.callback=function(responseText) {
			if(responseText.length>0) {
				// nothing
			} else {
				pRateHeader.innerHTML='YOUR RATING';
				unit1.onmouseover=null;
				unit2.onmouseover=null;
				unit3.onmouseover=null;
				unit4.onmouseover=null;
				unit5.onmouseover=null;
			}
		}
		pRateSubmit.style.display='none';
		pRateRequest.send(null);
	}

	function Cleanup() {
		removeEvent(pRateForm,"submit",stopEvent);
		removeEvent(window,"unload",Cleanup);
		pRateForm=null;
		pRateSubmit=null;
		pRateRequest=null;
		pRateResponses=null;
	}

	addEvent(window,"unload",Cleanup);
	addEvent(pRateForm,"submit",stopEvent);

	if(pRateSubmit) {
		addEvent(pRateSubmit,"click",DoRateArticle);
	}
}
