//check the comment input field
function checkInput(oTextArea){
	   var m=oTextArea.value.length;
       var n=m;
       var j=0;
	   
       for (var i=0;i<m;i++){
          if (oTextArea.value.charCodeAt(i)<0||oTextArea.value.charCodeAt(i)>161){ 
			n=n+1;
            if ( i < 100 ){
               j=j+1;
            }
		  }
       }
  
       if(n > 200){  
          oTextArea.value=oTextArea.value.substring(0,200-j);
        }
       else{
		  document.getElementById("remLen3").innerHTML="你还可以输入";
          document.getElementById("remLen4").innerHTML=(200-n);
          document.getElementById("remLen5").innerHTML="字符";
	    }
}

//send the comment to server

function diliverComment(){
	var userName = document.getElementById("userName").value;
	var photoId = document.getElementById("photoId").value;
	var comment = document.getElementById("comment_input").value;
	
	if(userName.length > 0){
		if(comment.length > 0 && comment.length <= 200 ){
			var url = 'components/com_gallery/ajax/ajax.php';
			var params = 'task=comment&username='+ userName+'&photoid='+photoId+'&comment='+comment;
			var myAjax = new Ajax.Request(
				url,
				{
					method: 'post',
					parameters: params,
					onSuccess : function(originalRequest){
						var pages = parseInt(originalRequest.responseText);
						var oldPages = document.getElementById("selectCommentPage").options.length;
						var cb = document.getElementById("commentBody");
						var rows = cb.rows.length;
						alert("谢谢参与！");
						if(rows < 21){
							//add new comment to the comment list
							var time = getTime();
							var tr = document.createElement("tr");
							tr.valign="top";
							var td1 = document.createElement("td");
							td1.innerHTML = "<strong>"+ userName + ":</strong>";
						
							var td2 = document.createElement("td");
							td2.innerHTML = comment;
						
							var td3 = document.createElement("td");
							td3.innerHTML = time;
					
							tr.appendChild(td1);
							tr.appendChild(td2);
							tr.appendChild(td3);
							cb.appendChild(tr);
						}
					
						if(pages > oldPages){
						
							var option = document.createElement("option");
							option.value = pages;
							option.onclick = askForComments(pages);
							option.appendChild(document.createTextNode(pages));
						
							document.getElementById("selectCommentPage").appendChild(option);
						}
					
					},
					onFailure : function(originalRequest){
						alert('服务器忙，请稍候再试!');
					}
				}
			);
		}else{
			alert("请输入评论");
		}
	}else{
		alert("你还没有登录，请先登录再发表评论");
	}
	//check the comment list,if the lengh is less than 10,then add this comment to it's end	
}
//get the current time
function getTime(){
	var time;
	var y,m,d,h,m,s;
	var today;
	today=new Date();
	y=today.getFullYear();
	
	m=today.getMonth()+1;
	if(m < 10)
		m="0"+m;
	d=today.getDate();
	if(d<10)
		d="0"+d;
	h=today.getHours();
	i=today.getMinutes();
	if(i < 10)
		i = "0"+i;
	s=today.getSeconds();
	if(s < 10)
		s = "0" + s;
	
	time = ""+y+"-"+m+"-"+d + " " + h+":"+i+":"+s;
	return time; 
	
}


//ask the server for comments of the giving page
function askForComments(page){
		var photoId = document.getElementById("photoId").value;
		var url = 'components/com_gallery/ajax/ajax.php';
		var params = 'task=paging&page='+page + '&photoId=' + photoId;
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onSuccess : function(originalRequest){
					var data = originalRequest.responseText;
					var trContextList = data.split("&");
					
					var cb = document.getElementById("commentBody");
					var trs = cb.rows.length;
					
					for(var j=trs-1; j > 0; j--){
						cb.deleteRow(j);
					}
										
					for(var i=0; i< trContextList.length; i++){
						var contexts = trContextList[i].split("*");
						
						var tr = document.createElement("tr");
						tr.valign="top";
						var td1 = document.createElement("td");
						td1.innerHTML = "<strong>"+ contexts[0] + ":</strong>";
						
						var td2 = document.createElement("td");
						td2.innerHTML = contexts[1];
						
						var td3 = document.createElement("td");
						td3.innerHTML = contexts[2];
					
						tr.appendChild(td1);
						tr.appendChild(td2);
						tr.appendChild(td3);
						cb.appendChild(tr);
						
					}
					
					
					
				},
				onFailure : function(originalRequest){
					alert('服务器忙，请稍候再试!');
				}
			}
		);
}