
function clearErrors(form, list)
{
    $('div.error', form).remove();
}

function toggleError(input, message)
{
    $(input).parent().append($('<div>').addClass('error').text(message));
}

function reload_captcha(img,url)
{
	img.src = url + ( url.indexOf('?') > -1 ? '&'+Math.random() : '?'+Math.random() );
}

function validateImage(value)
{
    if (!value.match(/\.(gif|jpe?g|png)$/i)) {
        return false;
    }
    
    return true;
}

loginDisabled = false;

function login()
{
    if (loginDisabled) return;
    var form = $('form[@name=login]');
    var effect = {height: 'toggle', opacity: 'toggle'};
    loginDisabled = true;
    $('.loginError', form).hide('fast');
    $('.loginWait', form).animate(effect, 'fast');
    $.post('/ajax/login', form.getFormValues(), function(response) {
        loginDisabled = false;
        $('.loginWait', form).hide('fast');
        if (response === 0) {
            $('.loginWait', form).animate(effect, 'fast');
        }
        else if (response === false) {
            $('.badLogin', form).animate(effect, 'fast');
        }
        else {
            loginDisabled = true;

            var temp = unescape($("input[@name=refer_url]").val());
            if(!temp) temp = document.location.href;

            document.location.href = temp + temp.indexOf('?') > -1 ? '&' : '?' + response;
            //document.location.reload();
        }
    }, 'json');
}

function goAnchor(anchor)
{
    document.location.href = document.location.href.replace(/(#[^#]+)?$/, '#' + anchor);
}

function isEmpty(value)
{
    return !String(value).length;
}

function trim(value)
{
    return value.replace(/(^\s*|\s$)/, '');
}

//-----------------------------------------------------------

/**
 * JQuery get form values plugin
 */
$.fn.getFormValues = function(assoc) {
    var a = assoc ? {} : [];
    $('input,textarea,select,button', this).each(function() {
        var n = this.name;
        var t = this.type;
        if ( !n || this.disabled || t == 'reset' ||
            (t == 'checkbox' || t == 'radio') && !this.checked ||
            (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
            this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
            return;
        if (t == 'image' && this.form.clicked_x)
            return a.push(
                {name: n+'_x', value: this.form.clicked_x},
                {name: n+'_y', value: this.form.clicked_y}
            );
        if (t == 'select-multiple') {
            $('option:selected', this).each( function() {
                a.push({name: n, value: this.value});
            });
            return;
        }
        if (assoc) {
            a[n] = this.value;
        }
        else {
            a.push({name: n, value: this.value});
        }
    });
    return a;
};

//-----------------------------------------------------------

$.fn.fork = function(i) {
    this.each(function(){
        var th = $(this);
        if (i == undefined) i = th.text();
        var rate = Number(i) * 2;
        th.empty();
        var n = rate % 2;
        for (var i = 0; i < rate - n; i+=2) {
            th.append($('<div>').addClass('fork-3'));
        }
        if (n) {
            i+=2;
            th.append($('<div>').addClass('fork-2'));
        }
        for (var k = 0; k < 8 - i; k+=2) {
            th.append($('<div>').addClass('fork-1'));
        }
        th.append($('<br>').attr('clear', 'all').css('font-size', '0'));
        if (!th.is(':visible')) {
            th.show('slow');
        }
    })
    return this;
}

$.fn.attrAll = function(name) {
    var list = [];
    this.each(function(){
        var th = $(this);
        list.push(th.attr(name));
    });
    return list;
}

//-----------------------------------------------------------

var showPopupImage_counter = 0;

function imgPopup(src,title) {
    
	if(!src) return;
	
	var newWindow = window.open("","showPopupImage_"+(showPopupImage_counter++),"width="+200+",height="+100+",left="+100+",top="+100);
	newWindow.document.open();
	newWindow.document.write('<html><head><title>'+(title || "cookingzone.net")+'</title>\n\
<'+'script>function resize(){\n\
	var pic = document.getElementById("pic");\n\
	if(typeof(pic) != "undefined") {\n\
		document.getElementById("Layer1").style.display = "none";\n\
		self.resizeTo(pic.width,pic.height+50);\n\
	}\n\
}\n\
\<'+'/'+'script\>\n\
</head>\n\
<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onload="resize()">\n\
 <div id="Layer1" style="position:absolute; z-index:2; left: 0px; top:0px;">\n\
  Загрузка изображения...\n\
 </div>\n\
 <div id="Layer2" style="position:absolute; z-index:1; left: 0px; top:0px;">\n\
  <img id="pic" src="'+src+'" />\n\
 </div>\n\
</body></html>');
	newWindow.document.close();
	newWindow.focus();
	
}
