function setXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		return;
	}
	// let's try to find MSXML releases
	var msxmls = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	for (var msxml in msxmls) {
		try {
			var o = new ActiveXObject(msxmls[msxml]);
			window.XMLHttpRequest = function() {
				return new ActiveXObject(msxmls[msxml]);
			}
			return;
		} catch (e) { }
	}
	throw new Error('Your Browser does not support HTTP subrequests. AJAX is not available.');
}

setXMLHttpRequest();

function showme(id) {
	var cont = document.getElementById('comcont'+id);
	var com = document.getElementById('com'+id);
	var slink =  document.getElementById('slink'+id);
	var hlink =  document.getElementById('hlink'+id);
	cont.style.width = '100%';
	com.style.display = 'block';
	hlink.style.display = 'inline';
	slink.style.display = 'none';
}

function hideme(id) {
        var cont = document.getElementById('comcont'+id);
        var com = document.getElementById('com'+id);
	var slink =  document.getElementById('slink'+id);
	var hlink =  document.getElementById('hlink'+id);
	var syo = document.getElementById('syo'+id);
	com.style.display = 'none';
	hlink.style.display = 'none';
	slink.style.display = 'inline';
	syo.style.display = 'inline';
	var form = document.getElementById('form'+id);
        var cont2 = document.getElementById('comcontb'+id);
	if (form) {
		cont2.removeChild(form);
	}
	cont.style.width = '360px'; // it's bad, really bad.
}

function realpost(id) {

	var form = document.getElementById('form'+id);
	var code = document.getElementById('code'+id).value;
	var params = document.getElementById('params'+id).value;
	var req = new XMLHttpRequest();
	req.open('POST','../post.php?id='+id,false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=utf-8'); 
	var params = params + encodeURIComponent(code);
	req.send(params);
	if (req.responseText == "BADCAPTCHA") {
		alert(totranslate.mispelled);
		return;
	}
	var cont = document.getElementById('comcontb'+id);
	cont.removeChild(form);
	var com = document.getElementById('com'+id);
	var post = document.createElement('div');
	post.setAttribute('class','comment');
	post.innerHTML = req.responseText;
	if (com.firstChild)
		com.insertBefore(post,com.firstChild);
	else
		com.appendChild(post);
}

function checkcap(id,cid) {
	var form = document.getElementById('form'+id);
	var cont = document.getElementById('comcontb'+id);
	var com = document.getElementById('com'+id);
	var sender = document.getElementById('senderta'+id);
	var body = document.getElementById('ta'+id);
	var params = 'name='+encodeURIComponent(sender.value)+'&body='+encodeURIComponent(body.value)+'&cid='+encodeURIComponent(cid)+'&code=';
	cont.removeChild(form);
	var form = document.createElement('form');
	form.setAttribute('id', 'form'+id);
	form.setAttribute('class', 'formcomment');
	form.setAttribute('action','javascript:realpost(\''+id+'\')');
	var captcha = document.createElement('img');
	captcha.setAttribute('src','../captcha.php?id='+cid);
	captcha.setAttribute('class','formcap');
	var text = document.createElement('input');
	text.setAttribute('type','text');
	text.setAttribute('name','code');
	text.setAttribute('id', 'code'+id);
	var label = document.createElement('span');
	label.innerHTML = totranslate.captcha ;
	var button = document.createElement('input');
	button.setAttribute('type','submit');
	button.setAttribute('value',totranslate.check);
	var paramsc = document.createElement('input');
	paramsc.setAttribute('type','hidden');
	paramsc.setAttribute('id','params'+id);
	paramsc.setAttribute('value', params);
	form.appendChild(label);
	form.appendChild(captcha);
	form.appendChild(text);
	form.appendChild(button);
	form.appendChild(paramsc);
	cont.insertBefore(form,com);
}

function post(id,cid) {
	showme(id);
	var syo = document.getElementById('syo'+id);
	syo.style.display = 'none';
	var form = document.createElement('form');
	form.setAttribute('class', 'formcomment');
	form.setAttribute('id', 'form'+id);
	form.setAttribute('action', 'javascript:checkcap('+id+',\''+cid+'\')');
	var ta = document.createElement('textarea');
	ta.innerHTML= totranslate.typemsg;
	ta.setAttribute('name', 'ta');
	ta.setAttribute('id', 'ta'+id);
	ta.setAttribute('class','richta');
	form.appendChild(ta);
	var sanskrit = new Sanskrit(ta, { className: "richta" } );
	var cont = document.getElementById('comcontb'+id);
	var com = document.getElementById('com'+id);
	cont.insertBefore(form,com);
}

function showlang(id) {
	var l = document.getElementById('languages'+id);
	if (l.style.display == "inline")
		l.style.display = "none";
	else
		l.style.display = "inline";
}

function trpost(id,locale) {
	var p = document.getElementById('bcom'+id);
	var req = new XMLHttpRequest();
	req.open('GET','../dpost.php?id='+id+'&locale='+locale, false);
	req.send(null);
	p.innerHTML = req.responseText;
}

