function getUSName() {
	var name = $('#input-name').val();
	if (name == ''){
		usname = getCookie('usname');
		if (usname){
			document.getElementById('input-name').value = usname;
		}
	}
	$("#shcaptcha").show(); 
}

function getUSEmail() {
	var email = $('#input-email').val();
	if (email == ''){
		usemail = getCookie('usemail');
		if (usemail){
			document.getElementById('input-email').value = usemail;
		}
	}
	$("#shcaptcha").show(); 
}
function getInEmail() {
	document.getElementById('getInputEmail').style.display = 'none';
	document.getElementById('input-email').style.display = 'block';
}

function setCookie (name, value) {
	//var secure = true;
	var secure = false;
	var path = "/";
	var domain = "osvita.ua";
	var today = new Date();
	expires = new Date(today.getTime() + 604800000);
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function sendComment(action){
    //читаем данные из формы
    $("#divErr").html('<img src="http://doc.osvita.ua/doc/i/spin16.gif">').show();
	setCookie("usname", $('#input-name').val());
	setCookie("usemail", $('#input-email').val());
 	$.post(action,
	{
		"comments[name]": $('#input-name').val(), 
		"comments[email]": $('#input-email').val(), 
		"comments[text]": $('#input-comments').val(), 
		"comments[path]": $('#input-path').val(),     
		"comments[gid]": $('#input-gid').val(), 
		"comments[g_id]": $('#input-g_id').val(), 
		"comments[parent]": $('#input-parent').val(), 
		"comments[news_id]": $('#input-news_id').val(), 
		"comments[code]": $('#input-code').val()
	},
		updatePage
	);
}

function renewCom() {
    var action = $('#input-myaction').val();
    if (action.slice(-1)=='/'){
		action = $('#input-myaction').val() + 'comrenew';
	}else{
		action = $('#input-myaction').val() + '/comrenew';
	}

    var mydate = new Date();
    $("#divErr").html('<img src="http://doc.osvita.ua/doc/i/spin16.gif">').show();
	$.post(action,
	{
		"comments[comrenew]": mydate
	},
		updatePage
	)
}

function updatePage(data) { 
    //var comment = eval( '(' + data + ')' );
	var comment = data;
    var mydate = new Date();
	// Заполняем <div id="divErr"></div> текстом и показываем его   
	if (comment.err != ''){
		$("#divErr").html('<font color="red">' + comment.err + '</font>').show();
	}else{
		if(comment.ok != ''){
			$("#divErr").html('<font color="blue">' + comment.ok + '</font><br />Коментарі оновлено на ' + mydate.toLocaleTimeString() + '.').show();
			$("#formcom")[0].reset();
	    }else{
			$("#divErr").html('<font color="blue">Коментарі оновлено на ' + mydate.toLocaleTimeString() + '.</font>').show();
	    }
		$("#comments-list").html(comment.all).show();
	}
	recaptcha();
}

function checkSymbol() { 
	var maxLen=700; 
	var len=0; 
	len = document.getElementById('input-comments').value.length;
	if (len>maxLen){
		if (len>(maxLen+100)){
			comerror = 'Багато символів. Скоротіть на ' + -(maxLen + 100 - len);
			$("#divErr").html('<font color="red">' + comerror + '</font>').show();
		}else{
			comerror = 'Залишилось символів ' + (maxLen + 100 - len);
			$("#divErr").html('<font color="blue">' + comerror + '</font>').show();
		}
	}
}

function reComm(name,parent) { 
	document.getElementById('input-comments').value = document.getElementById('input-comments').value + ' [b]Для ' + name + ':[/b] ';
	document.getElementById('input-parent').value = parent;
	document.getElementById('input-comments').focus();
}

function recaptcha() { 
	$("#shcaptcha").show(); 
    $("IMG#captcha1").attr('src', 'http://osvita.ua/captcha/?' + Math.random());
	document.getElementById('input-code').value = '';
	document.getElementById('input-code').focus();
}  

function sendUser(action){
    //читаем данные из формы
    $("#delok").html('<center><img src="http://doc.osvita.ua/doc/i/spin16.gif"></center>').show();
 	$.post(action,
	{
		"user[url]": $("#input-url").val(),  
		"user[email]": $("#input-email").val(), 
		"user[delete]": $("#input-delete").val()
	},
		updateDelform
	);
}

function updateDelform(data) { 
    //var user = eval( '(' + data + ')' );
    var user = data;
	if (user.result != ''){
		$("#delok").html(' ' + user.result + ' ').show();
		$("#input-form")[0].reset();
		//$('#input-email').val() = user.email;
		//$('#input-url').val() = user.url;
	}
}

