﻿// Ajax Rating
// Author:Jianglb.com

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function sndReq(vote,id_num,ip_num,units,story) {
	
	if(story!=1){
	var theUL = document.getElementById('unit_ul_story'+id_num); // the UL
	
	}
	else {
	var theUL = document.getElementById('unit_ul'+id_num); // the UL
	}
	// switch UL with a loading div
	theUL.innerHTML = '<div class="loading"></div>';
	var url='/rate/score.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units+'&s='+story+'&rnd='+Math.random();
	//document.write(url);
	//alert(url);return false;
    xmlhttp.open('get', url);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function handleResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText(update[0], update[1]);
        }
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

/* =============================================================== */
var ratingAction = {
		'a.rater' : function(element){
			element.onclick = function(){
			var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
			var parameterTokens = parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
			var parameterList = new Array();

			for (j = 0; j < parameterTokens.length; j++) {
				var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
				var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
				parameterList[parameterName] = parameterValue;
			}
			var theratingID = parameterList['q'];
			var theVote = parameterList['j'];
			var theuserIP = parameterList['t'];
			var theunits = parameterList['c'];
			var thestory = parameterList['s'];
			//for testing	
			//alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+','+thestory+')'); return false;
			sndReq(theVote,theratingID,theuserIP,theunits,thestory); return false;		
			}
		}
		
	};
Behaviour.register(ratingAction);

//Axaj回复
function _postReply() {
	getid('AxajTip3').style.display = 'none';
	getid('AxajTip').style.display = 'none';
	var inobj = getid('comment');
	if(inobj.value==''){	_ShowAxajTip('AxajTip','请填写评论后再发表','AjaxTipWarning');inobj.focus();return false;}
	var inobj = getid('valimg');
	if(inobj.value==''){	_ShowAxajTip('AxajTip','请填写验证码后再发表','AjaxTipWarning');inobj.focus();return false;}
	StopButton('button',15);

   var x = new Ajax('HTML');

   //tid sid valimg_main comment nowsubject
   var queryString = 'tid=' + getid('tid').value + '&nick=' + escape(getid('name').value) + '&comment=' + escape(Replace(getid('comment').value))+ '&code=' + getid('valimg').value ;
	//alert(queryString);
	var ajaxReq = '',reqClass='AjaxTipWarning';
   x.post('/rate/comment.php',queryString,function(r){
			//alert(r);
		switch(r.substr(0,1)){
		case '0':
		        ajaxReq = '您要评论的车次不存在';
		        break;
		case '1':
		        ajaxReq = '验证码不正确';
		        break;
		case '2':
		       ajaxReq = '30秒内不允许再次评论';
		        break;
		case "3":
		        ajaxReq = '请填写评论后再提交';
		        break;
		case "4":
		        ajaxReq = '评论字数超过限制';
		        break;
		case "5":
		        ajaxReq = '发送评论成功.';
		        reqClass = 'AjaxTipComplete';
		        //getid('normal').innerHTML = r.substr(3);
		        _ShowAxajTip('AxajTip3',ajaxReq,reqClass);
				try{getid('comment').value='';getid('valdiv').style.display='none';}catch(e){}
				return true;
		        break;
		case "6":
			    ajaxReq = 'CBFW检测到您输入了不适当字词，请纠正';
		        break;
		case "7":
			    ajaxReq = '您的评论需审核后才能显示';
		        reqClass = 'AjaxTipdelay';
		        _ShowAxajTip('AxajTip3',ajaxReq,reqClass);
				try{getid('comment').value='';}catch(e){}
				return true;
		        break;
		case "8":
			    ajaxReq = '由于种种原因，这条新闻不开放评论，请见谅';
		        break;
		default:
		        ajaxReq = '未知错误' + r;
		}
		_ShowAxajTip('AxajTip',ajaxReq,reqClass);
 		//reloadcode(ReplyID);
	});
}
function Replace(str){
 
   re = /%/g;             // 创建正则表达式模式。
   str = str.replace(re, "％");    //  替换 
   re = /\+/g;
   str = str.replace(re, "＋");
   return(str);                   // 返回替换后的字符串。
}
function StopButton(id,min){ 
	getid(id).disabled='disabled';
	getid(id).value="发表评论("+min+")";
	if(--min>0) setTimeout("StopButton('"+id+"',"+min+")",1000);
	if(min<=0){
		getid(id).value='发表评论';
		getid(id).disabled='';
	}
}
function _HideAxajTip(myobj){
	myobj.parentNode.parentNode.style.display='none';
}

function _ShowAxajTip(divobj,ajaxReq,reqClass){
	var AxajTipDiv = getid(divobj);
	ajaxReq = '<div style="float: right"><img src="/images/'+ reqClass +'.gif" style="margin: 7px 7px;" onclick="_HideAxajTip(this);"></div><div>' + ajaxReq + '<div>';
	AxajTipDiv.className = reqClass;
	AxajTipDiv.innerHTML = ajaxReq;
	AxajTipDiv.style.display = 'block';
}
function _Showvaldiv(){
	var Valdiv = getid('valdiv');
	if (Valdiv.style.display=='none'){
		Valdiv.style.display='block';
		reloadcode();
	}
}
function reloadcode(){ 
	getid('safecode').src = '/rate/seccode.php?'+ Math.random();
} 