var http_req;
var rated=false;
var req=null;

function cForm_control(form)
{
	if(form.visitor_name_box.value.length<2) {alert('Adınızı ve soyadınızı giriniz.');form.visitor_name_box.focus();return false;}
	if(form.commnet_box.value.length<2) {alert('Yorumunuzu giriniz.');form.commnet_box.focus();return false}
	if(form.scode_box.value.length!=5) {alert('Güvenlik kodunu geçersiz.Kontrol ediniz.');form.scode_box.focus();return false}
	return true;
}

function send_comment()
{
	var form=document.getElementById('c_submit').form;
	if(!cForm_control(form)) return false;
	document.getElementById('c_submit').disabled=true;
	document.getElementById('c_submit').value='Gönderiliyor';
	if(req==null) req=new Ajax();
	data=('http_request=true&id='+form.topic_id.value+'&visitor_name='+encodeURI(form.visitor_name_box.value)+'&email='+encodeURI(form.email.value)+'&comment='+encodeURI(form.commnet_box.value).replace('&','%26')+'&scode='+form.scode_box.value);
	req.state_changing=comment_state_changing;
	req.send_way='POST';
	req.send(get_host_name()+'libraries/article_comment.php',data,false);
	return false;
}

function comment_state_changing()
{
	if(req.http_object.readyState==4)
	{
		document.getElementById('scode_img').src=get_host_name()+'scode.php?id='+(new Date()).getTime();
		document.getElementById('scode_box').value='';
		if(req.http_object.status==200)
		{
			ps=document.getElementById('comment_form').getElementsByTagName('p');
			for(i=0;ps.length>i;i++) if(ps[i].className=='error_msg' || ps[i].className=='ok_msg'){ps=ps[i];ps.style.visibility='visible';break;}
			if(req.http_object.responseText=='1')
			{
				ps.className='ok_msg';
				ps.innerHTML='Yorumunuz eklenmiştir, fikirlerinizi paylaştığınız için teşekkür ederiz.';
			}
			else
			{
				ps.className='error_msg';
				ps.innerHTML=req.http_object.responseText;
			}
		}
		document.getElementById('c_submit').disabled=false;
		document.getElementById('c_submit').value='Gönder';
	}
}

function strlen_counter(obje,max_len,label,prefix)
{
	if(obje.value.length>max_len) obje.value=obje.value.substr(0,max_len);
	if(document.getElementById(label)) document.getElementById(label).innerHTML=prefix+' ['+obje.value.length+'/'+max_len+']:';
}

function show_window(link,width,height)
{
	var wind=window.open(link.href,'','width='+width+',height='+height+',scrollbars=yes,resizable=yes');
	return false;
}

function email_to_author(link)
{
	window.open(link.href,'','width=580,height=335,scrollbars=yes');
	return false;
}
function hide_all_uls()
{
	var divs=document.getElementById('article_archives_list').getElementsByTagName('div');
	for(i=0;divs.length>i;i++) {divs[i].style.display='none';divs[i].style.visibility='hidden';}
}
function show_archives_list(link)
{
	hide_all_uls();
	var active_list=link.href.split('?',2)[1].split('&',2)[1].split('=',2)[1];
	if(document.getElementById('article_archives_ul'+active_list)==null)
	{
		get_archive_list(link);
	}
	else
	{
		document.getElementById('article_archives_ul'+active_list).style.display='block';
		document.getElementById('article_archives_ul'+active_list).style.visibility='visible';
	}
	return false;
}
function get_archive_list(link)
{
	if(http_req==null) http_req=new Ajax();
	http_req.state_changing=archives_list;
	url_parts=link.href.split('?',2);
	http_req.send(get_host_name()+'libraries/article.php?http_request=1&author_id='+document.getElementById('author_id').value+'&'+url_parts[1]);
	return false;
}
function archives_list()
{
	if(http_req.http_object.readyState==4)
	{
		if(http_req.http_object.status==200)
		{
			document.getElementById('article_archives_list').innerHTML+=http_req.http_object.responseText;
		}
	}
}
function rate(link)
{
	if(rated) return false;
	if(http_req==null) http_req=new Ajax();
	http_req.state_changing=rating;
	http_req.send(get_host_name()+'libraries/article.php?http_request=1&'+link.href.split('?',2)[1]);
	rated=true;
	return false;
}
function rating()
{
	if(http_req.http_object.readyState==4)
	{
		if(http_req.http_object.status==200)
		{
			document.getElementById('rating').innerHTML=http_req.http_object.responseText;
		}
	}
}
