// JavaScript Document
var xmlHttp

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
	// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}catch(e){
	// Internet Explorer
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function add(formid){
	
	xmlHttp=GetXmlHttpObject()
	var file="ext/addcomment.php"
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
	}
	var cname = document.getElementById('cname').value;
	var ccomment = document.getElementById('ccomment').value;
	var ccaptcha = document.getElementById('captcha_code').value;
//	if((pval.indexOf(" ") == 0) || (pval.length-1 == pval.lastIndexOf(" "))){
//	if(!cname || (cname.indexOf(" ") == 0) || (cname.lastIndexOf(" ") == 0)){
	if(!cname || (document.getElementById('cname').length == '0')){
		alert('Please input name');
	}else if((cname.indexOf(" ") == 0) || (cname.length-1 == cname.lastIndexOf(" "))){
		alert('No space please..');
	}else if(!ccomment){
		alert('Please input comment');
	}else if(ccomment && cname){
		parameters="type="+formid
		parameters=parameters+"&cname="+cname
		parameters=parameters+"&ccomment="+ccomment
		parameters=parameters+"&ccaptcha="+ccaptcha
		parameters=parameters+"&enter=proceed"
		xmlHttp.onreadystatechange=stateChanged 
		xmlHttp.open("POST",file,true)
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
		xmlHttp.send(parameters)
	}

	function stateChanged(){
		if(xmlHttp.readyState==1){
			document.getElementById('button_ajax').innerHTML='<img src="images/ajax-loader.gif" />'
		}
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			if(xmlHttp.responseText){
				document.getElementById('captcha_warn').innerHTML=xmlHttp.responseText;
				document.getElementById('button_ajax').innerHTML='<input type="button" onClick="add(\'comment\');" name="submitcomment" value="Post Comment" />';
			}else{
				document.getElementById('ccomment').value='';
				document.getElementById('captcha_code').value='';
				document.getElementById('countdown').value='60';
				self.setInterval("show_button()",3000);
				document.getElementById('captcha_warn').innerHTML='';
			}

//			document.getElementById(divid).innerHTML=xmlHttp.responseText
//			alert(xmlHttp.responseText)
		} 
	}
}

function show_button(){
	document.getElementById('button_ajax').innerHTML='<input type="button" onClick="add(\'comment\');" name="submitcomment" value="Post Comment" />'	
}

function refresh_comment(formid){

	xmlHttp=GetXmlHttpObject()
	var file="ext/addcomment.php"
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
	}

	parameters="type="+formid
	parameters=parameters+"&enter=proceed"
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("POST",file,true)
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlHttp.send(parameters)

	function stateChanged(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
//			if(navigator.appName=='Microsoft Internet Explorer'){
//				document.getElementById('display_comments').innerText=xmlHttp.responseText;
//			}else{
				document.getElementById('display_comments').innerHTML=xmlHttp.responseText;
//			}
		} 
	}
}

function limitText(limitNum){
	if(document.getElementById('ccomment').value.length > limitNum){
		document.getElementById('ccomment').value = document.getElementById('ccomment').value.substring(0, limitNum);
	}else{
		document.getElementById('countdown').value = limitNum - document.getElementById('ccomment').value.length;
	}
}

function selectavatar(avtr){
	document.getElementById('avatar').selectedIndex=avtr
	
}

