var auditArea = "";
var currentPage = 1 ;

function postfamousWordsVote(url,obj,area) {
  if(obj.rand.value){
	poststr =  makeURL(url,obj);
	poststr += "rand=" + obj.rand.value ;

	makeRequest(url, poststr,area);
	http_request.open('GET', url+"?" + poststr, true);
	http_request.send(null);
  }else{
  	alert("請先輸入認證數字後,才可以評選!");
  }
}

function selfOK(areaID){
  if(areaID.indexOf("subarea") !=-1){
    auditArea = 'audit' +areaID.substring(7,areaID.length);
    document.getElementById(auditArea).innerHTML="<p align='center'><font size='+1'>您己經完成對這句話的評價了!</font></p>";
    createCookie(auditArea,auditArea,365);
    auditArea="";
  }

}

function selfError(msg){
  msg1= msg.substring(7,msg.length);
  alert( msg1);
}


function prePage(url,totalPage,area,type,category,during,figure,kword){
    if ( currentPage == 1 )
	  return ;
    if ( currentPage > 1 )  currentPage -=1 ;
    document.topPage.pageselect.selectedIndex= currentPage -1 ;
    document.bottomPage.pageselect.selectedIndex= currentPage -1 ;
    poststr = "?page="+ currentPage ;
    poststr += "&type="+ type ;
    if(figure&& figure!=("") )
	    poststr += "&figure="+ figure ;
    if(category&& category!=("") )
	    poststr += "&category="+ category ;
    if(during && during!=("") )
    	poststr += "&year="+ during ;
    if(kword && kword!=("") )
    	poststr += "&kword="+ kword ;
    reloadPage(url,poststr,area);
}

function nextPage(url,totalPage,area,type,category,during,figure,kword){
    if ( currentPage == totalPage )  return ;
    if ( currentPage < totalPage )  currentPage +=1 ;
    document.topPage.pageselect.selectedIndex= currentPage -1 ;
    document.bottomPage.pageselect.selectedIndex= currentPage -1 ;
    poststr = "?page="+ currentPage ;
    poststr += "&type="+ type ;
    if(figure&& figure!=("") )
	    poststr += "&figure="+ figure ;
    if(category&& category!=("") )
    	poststr += "&category="+ category ;
    if(during && during!=("") )
    	poststr += "&year="+ during ;
    if(kword && kword!=("") )
    	poststr += "&kword="+ kword ;
    reloadPage(url,poststr,area);
}
function submitfamousWords(url,obj,id){
   formVar=eval('document.form'+id);
   formVar.type.value = obj.value ;
   if(auditArea!="") {
        if(document.getElementById(auditArea))
                document.getElementById(auditArea).style.display="none";
   }
   auditArea = 'audit'+id;
   document.getElementById(auditArea).style.display="block";
	textArea="<table align=\"center\">\n";
	textArea+="<tr class=\"vote_result_number\" valign=\"top\">\n";
	textArea+="<td>認證碼 :\n";
	textArea+="<kanhanbypass><img id=\"authImage" + id + "\"";
	textArea+=" src=\"/news/newswords/images/jodo.jpg\" border=\"0\">\n";
	textArea+="請輸入左邊的認證數字 : <input type=\"text\" name=\"rand\" maxlength=\"4\" size=\"6\" value=\"\"></td>\n";
	textArea+="<td><input name=\"image22\" type=\"image\" src=\"/news/newswords/images/vote.gif\" border=\"0\" /> </td>\n";
	textArea+="</tr>\n";
	textArea+="</table>";
	document.getElementById("audit"+id).innerHTML=textArea;
	do_change_img(url,id);

}

function selectPage(url,objPage,area,type,category,during,figure,kword){
    currentPage = objPage.selectedIndex + 1 ;
    document.topPage.pageselect.selectedIndex= objPage.selectedIndex ;
    document.bottomPage.pageselect.selectedIndex = objPage.selectedIndex ;
    poststr = "?page="+ currentPage ;
    poststr += "&type="+ type ;
    if(figure&& figure!=("") )
	    poststr += "&figure="+ figure ;
    if(category&& category!="" )
    	poststr += "&category="+ category ;
    if(during && during!=("") )
    	poststr += "&year="+ during ;
    if(kword && kword!=("") )
    	poststr += "&kword="+ kword ;
    reloadPage(url,poststr,area);
}

function reloadPage(url,poststr,area){
  ajaxpage(url+ encodeURI(poststr),area);
}

function changeCategory(url,type,condition){
  if(type==0){
	 document.getElementById("subCategory").innerHTML = "";
	 document.getElementById("conditionArea").innerHTML = "";
  }
  if(type==1){
	 document.getElementById("subCategory").innerHTML = " / 依投票人次瀏覽";
	 document.getElementById("conditionArea").innerHTML = "";
  }
  if(type==2){
	 document.getElementById("subCategory").innerHTML = " / 依網友評價排行";
	 document.getElementById("conditionArea").innerHTML = "";
  }
  if(type==3){
	 document.getElementById("subCategory").innerHTML = " / 依類別瀏覽";
	 document.getElementById("conditionArea").innerHTML = "<font class=sub_category>查詢條件：<font color=#0000CC>依類別瀏覽="+condition+"</font></font>";
  }
  if(type==4){
	 document.getElementById("subCategory").innerHTML = " / 依時間瀏覽";
	 document.getElementById("conditionArea").innerHTML = "<font class=sub_category>查詢條件：<font color=#0000CC>依時間瀏瀏覽="+condition+"</font></font>";
  }
  if(type==5){
	 document.getElementById("subCategory").innerHTML = " / 依人物瀏覽";
	 document.getElementById("conditionArea").innerHTML = "<font class=sub_category>查詢條件：<font color=#0000CC>依人物瀏覽="+condition+"</font></font>";
  }
  if(type==6){
	 document.getElementById("subCategory").innerHTML = " / 依關鍵字瀏覽";
	 document.getElementById("conditionArea").innerHTML = "<font class=sub_category>查詢條件：<font color=#0000CC>依關鍵字瀏覽="+condition+"</font></font>";
  }
  currentPage = 1 ;
// alert("subCategory");	
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function do_change_img(url,areaid){
	var area="authImage" +areaid;
	core = Math.random();
	var objform = eval('document.form'+areaid) ;
	var code='image';
	var reg_img=document.getElementById(area);
	var qparts=reg_img.src.split("?");
	do_request_function=function(){
		if(!xmlobj.readystate_ready_and_ok()){
			return;
		}
		reg_img.src=url+"?img="+core;
		reg_img._ready=1;

	};
	reg_img._ready=0;
	xmlobj=new ajax_request();
        xmlobj.onreadystatechange(do_request_function);
	xmlobj.process(url);
}

function ajax_request(){
  this.isIE=false;this.allow_use=use_enhanced_js?true:false;
  this.xmlhandler=null;
  this.error_string='';
  this.nocache=true;
  this.do_request_functon=function(){};
  this.loading_fired=0;
  this.centerdiv=null;
}


ajax_request.prototype.xml_init=function(){
  try{
    this.xmlhandler=new XMLHttpRequest();
    this.ie=false;this.allow_use=true;
    return true;
  }catch(e){
    try{
      this.xmlhandler=new ActiveXObject('Microsoft.XMLHTTP');
      this.ie=true;this.allow_use=true;
      return true;
    }catch(e){
      this.ie=true;
      this.allow_use=false;
      return false;
    }
  }
};



ajax_request.prototype.process=function(url,type,post){
  type=type=="POST"?"POST":"GET";
  if(this.nocache==true&&type=='GET'){
    url=this.nocache_url(url);
  }
  if(!this.xmlhandler){
    this.xml_init();
  }
  if(!this.readystate_not_ready()){
    this.xmlhandler.open(type,url,true);
    if(type=="GET"){
      this.xmlhandler.send(null);
    }else{
      if(typeof(this.xmlhandler.setRequestHeader)!="undefined"){
        this.xmlhandler.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      }
      this.xmlhandler.send(post);
    }
    if(this.xmlhandler.readyState==4&&this.xmlhandler.status==200){
      return true;
    }
  }
  return false;
};

/*
ajax_request.prototype.get_element_text_ns=function(prefix,local,parentElem,index){
  var result="";
  if(prefix&&this.isIE){
    result=parentElem.getElementsByTagName(prefix+":"+local)[index];
  }else{
    result=parentElem.getElementsByTagName(local)[index];
  }
  if(result){
    if(result.childNodes.length>1){
      return result.childNodes[1].nodeValue;}else{return result.firstChild.nodeValue;
    }
  }else{
    return "n/a";
  }
};
*/
ajax_request.prototype.nocache_url=function(url){
  var sep=(-1<url.indexOf("?"))?"&":"?";
  var mydate=new Date();
  var newurl=url+sep+"__="+mydate.getTime();
  return newurl;
};

/*
ajax_request.prototype.format_for_post=function(arrayfields){
  var str='';
  try{
    for(var i in arrayfields){
      str+=i+'='+this.encodeurl(arrayfields[i])+'&';
    }
  }catch(e){}
  return str;
};
*/
ajax_request.prototype.encodeurl=function(url){
  url=url.toString();
  var regcheck=url.match(/[\x90-\xFF]/g);
    if(regcheck){
      for(var i=0;i<i.length;i++){
        url=url.replace(regcheck[i],'%u00'+(regcheck[i].charCodeAt(0)&0xFF).toString(16).toUpperCase());
      }
    }
  return escape(url).replace(/\+/g,"%2B");
};

ajax_request.prototype.readystate_not_ready=function(){
  return(this.xmlhandler.readyState&&(this.xmlhandler.readyState<4));
};

ajax_request.prototype.readystate_ready_and_ok=function(){
  return(this.xmlhandler.readyState==4&&this.xmlhandler.status==200)?true:false;
};

ajax_request.prototype.onreadystatechange=function(event){
  if(!this.xmlhandler){
  this.xml_init();
  }
  if(typeof(event)=='function'){
    this.xmlhandler.onreadystatechange=event;
  }
};

try{
  if(use_enhanced_js){
    use_enhanced_js=ajax_request.prototype.xml_init()?1:0;
  }
}catch(error){
  var use_enhanced_js=0;
}


