/**
 * (c) dumanoid.ru 2009
 */


// form: 

function upp(id) {
	$('target'+id).innerHTML = 'картинка загружена';
}

function jsUpload(upload_field,unid) {
    // this is just an example of checking file extensions
    // if you do not need extension checking, remove 
    // everything down to line
    // upload_field.form.submit();

    var re_text = /\.png|\.jpg|\.gif/i;
    var filename = upload_field.value;

    /* Checking file type */
    if (filename.search(re_text) == -1) {
        alert("Поддерживаются только PNG, JPG и GIF");
        upload_field.form.reset();
        return false;
    }
    
    upload_field.form.f.value = 'fu';

    upload_field.form.submit();
    
    $('upload'+unid).style.display='none';
    $('target'+unid).style.display='block';
    $('target'+unid).innerHTML = '<img src="/project/i/al.gif">';
    //window.frames['utc'+cid].document.getElementsByTagName("body")[0].style.display = 'block';
    //window.frames['utc'+cid].document.getElementsByTagName("body")[0].innerHTML = 'загружаем...';
    
    
    // document.getElementById(upload_status).innerHTML = "uploading file...";
    upload_field.disabled = true;
    return true;
}


//<input type="button" onClick="var id = document.getElementById('test'); var hide = document.getElementById('test2'); if(id.style.display == 'block'){ id.style.display = 'none'; id.style.visibility = 'hidden'; }else{ id.style.display = 'block'; id.style.visibility = 'visible';  hide.style.display = 'none'; hide.style.visibility = 'hidden';}" value="Rogers" />
//<input type="button" onClick="var id = document.getElementById('test2'); var hide = document.getElementById('test'); if(id.style.display == 'block'){ id.style.display = 'none'; id.style.visibility = 'hidden'; }else{ id.style.display = 'block'; id.style.visibility = 'visible'; hide.style.display = 'none'; hide.style.visibility = 'hidden';}" value="May" />
//<embed src="http://www.hispeedchase.com/hosted/dan/rogers.pdf" style="WIDTH: 79%; HEIGHT: 1604px; TEXT-ALIGN: center; display:none; visibility: hidden;" id="test" name="test">
//<embed src="http://www.hispeedchase.com/hosted/dan/rank.pdf" style="WIDTH: 79%; HEIGHT: 1604px; TEXT-ALIGN: center; display:none; visibility: hidden;" id="test2" name="test2">



function cn(pid,cid,sid) {
	
	var unid = pid+''+cid+''+sid;
	
	var cform = $('cform'+unid);
	var cctxt = $('replyto_'+unid);
	var ccbtn = $('submitrc_'+unid);
	var img = $('img'+cid);
	cctxt.disabled=true;
	ccbtn.disabled=true;
	
	var filedata = window.frames['utc'+unid].document.getElementsByTagName("body")[0].innerHTML;
	
	var url = '/a';
	var comment = $('replyto_'+unid).value;
	if ((comment.length < 1) && (filedata.length < 1)) {
		alert("Отлично! Пустой комментарий.");
		return false;
	}
	
	var params = 'f=cr&cid='+cid+'&c='+comment+'&pid='+pid+'&sid='+sid+'&fd='+filedata;
	var commarea = $('replyto'+unid);
	
    xmlhttp.open("POST",url,true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // xmlhttp.setRequestHeader("Content-type", "multipart/form-data");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
	
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;       
			if (response != 'null') commarea.innerHTML = response;
		}
	};
	xmlhttp.send(params);
}

function cr(pid,cid,sid) {
		
	var cform = $('cform'+cid);
	
	var cctxt = $('replyto_'+cid);
	var ccbtn = $('submitrc_'+cid);
	var img = $('img'+cid);
	cctxt.disabled=true;
	ccbtn.disabled=true;
	
	
	var url = '/a';
	var comment = $('replyto_'+cid).value;
	if (comment.length < 1) return false;
	
	var params = 'f=cr&cid='+cid+'&c='+comment+'&pid='+pid+'&sid='+sid;
	var commarea = $('replyto'+cid);
	
    xmlhttp.open("POST",url,true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // xmlhttp.setRequestHeader("Content-type", "multipart/form-data");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
	
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;       
			if (response != 'null') commarea.innerHTML = response;
		}
	};
	xmlhttp.send(params);
}


function scomm(pid,id) {

	var sform = $('sform'+id);
//	sform.sctext.disabled=1;
//	sform.sc.disabled=1;
	
	var sctxt = $('cs_'+id);
	var scbtn = $('submitsc'+id);
	sctxt.disabled=true;
	scbtn.disabled=true;
	
	var url = '/a';
	var comment = $('cs_'+id).value;
	if (comment.length < 1) return false;
	
	var params = 'f=scomm&id='+id+'&c='+comment+'&pid='+pid;
	var commarea = $('addsc'+id);
	
    xmlhttp.open("POST",url,true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
	
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			if (response != 'null') commarea.innerHTML = response;
		}
	};
	xmlhttp.send(params);
}

function pcomm(id) {
	
	var pform = $('pform'+id);
	var pctxt = $('comment_for_post_'+id);
	var pcbtn = $('submitpc');
	pctxt.disabled=true;
	pcbtn.disabled=true;
	
	var url = '/a';
	var comment = document.pcomment.pctext.value;
	if (comment.length < 1) return false;
	
	var params = 'f=pcomm&id='+id+'&c='+comment;
	var commarea = $('addpc');
	
    xmlhttp.open("POST",url,true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
	
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			if (response != 'null') commarea.innerHTML = response;
		}
	};
	xmlhttp.send(params);
}

function sg(id,uid,pid) {
	
	if ((id!=0) && (uid!=0) && (pid!=0)) {
		var func = 'selgov';
		var url = '/a/?m=gov&f='+func+'&id='+id+'&uid='+uid+'&pid='+pid;
		var addgov = $('addgov');
		
		xmlhttp.open("POST",url,true);
	    xmlhttp.onreadystatechange=function() {

	    	if (xmlhttp.readyState==4) {
				var response = xmlhttp.responseText;
				if (response != 'null') addgov.innerHTML = response;
			}
		};
		xmlhttp.send(null);
		
	}
	
	return false;
}
function vss(id,vote,uid,type) {
	if (uid == 0) {
		window.location="/enter";
		return false;
	}
	
	var func = '';
	var pm = '';
	var svoting = '';
	if (type == 's') {
		func = 'votesolution';
		pm = $('sv'+id);
		voting = $('svote'+id);
		
		// hide current vote
		$('vsc'+id).style.display='none';
		if (vote == 'plus') {
			$('vsl'+id).style.display='block';
			$('vsd'+id).style.display='none';
			$('vsp'+id).setAttribute('class','vote upped');
			$('vsp'+id).setAttribute('onclick','');
			$('vsm'+id).setAttribute('class','vote down');
		} else
		if (vote == 'minus') {
			$('vsd'+id).style.display='block';
			$('vsl'+id).style.display='none';
			$('vsm'+id).setAttribute('class','vote downed');
			$('vsm'+id).setAttribute('onclick','');
			$('vsp'+id).setAttribute('class','vote up');
		}
		
	} else if (type == 'p') {
		func = 'voteproblem';
		pm = $('pv'+id);
		voting = $('pvote'+id);
		
		// hide current vote
		$('vpc'+id).style.display='none';
		if (vote == 'plus') {
			$('vpl'+id).style.display='block';
			$('vpd'+id).style.display='none';
			$('vpp'+id).setAttribute('class','vote upped');
			$('vpp'+id).setAttribute('onclick','');
			$('vpm'+id).setAttribute('class','vote down');
		} else
		if (vote == 'minus') {
			$('vpd'+id).style.display='block';
			$('vpl'+id).style.display='none';
			$('vpm'+id).setAttribute('class','vote downed');
			$('vpm'+id).setAttribute('onclick','');
			$('vpp'+id).setAttribute('class','vote up');
		}
	} else if (type == 'c') {
		func = 'votecomment';
		pm = $('cv'+id);
		voting = $('cvote'+id);
		
		// hide current vote
		$('vcc'+id).style.display='none';
		if (vote == 'plus') {
			$('vcl'+id).style.display='block';
			$('vcd'+id).style.display='none';
			$('vcp'+id).setAttribute('class','vote upped');
			$('vcp'+id).setAttribute('onclick','');
			$('vcm'+id).setAttribute('class','vote down');
		} else
		if (vote == 'minus') {
			$('vcd'+id).style.display='block';
			$('vcl'+id).style.display='none';
			$('vcm'+id).setAttribute('class','vote downed');
			$('vcm'+id).setAttribute('onclick','');
			$('vcp'+id).setAttribute('class','vote up');
		}
		
	} else {
		return false;
	}
	
	var url = '/a/?m=posts&f='+func+'&id='+id+'&vote='+vote;
    xmlhttp.open("POST",url,true);
	xmlhttp.send(null);
	return false;
	
	return false;
}

function vs(id,vote,uid,type) {
	
	if (uid == 0) {
		window.location="/enter";
		return false;
	}
	var func = '';
	var pm = '';
	var svoting = '';
	if (type == 's') {
		func = 'votesolution';
		pm = $('sv'+id);
		voting = $('svote'+id);
	} else if (type == 'p') {
		func = 'voteproblem';
		pm = $('pv'+id);
		voting = $('pvote'+id);
	} else {
		return false;
	}
	
	var url = '/a/?m=posts&f='+func+'&id='+id+'&vote='+vote;
    xmlhttp.open("POST",url,true);
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			if (response != 'null') voting.innerHTML = response;
			if (vote == 'plus') {
				pm.innerHTML = '<span class="plus">+</span><a class="minus" onclick="return vs('+id+',\'minus\','+uid+',\''+type+'\')" href="#">&#x2015;</a>';
			} else if (vote == 'minus'){
				pm.innerHTML = '<a class="plus" onclick="return vs('+id+',\'plus\','+uid+',\''+type+'\')" href="#">+</a><span class="minus">&#x2015;</span>';
			}
		}
	};
	xmlhttp.send(null);
	return false;
}