function scrollWindowToTop(speed)
{
    if (!speed) {
        speed = 1000;
    }
    $('html,body').animate({scrollTop: 0}, speed);
}

function updateRating(what, id, mark)
{
    var url = '/vote/'+what+'/';
    params = 'vote_for='+what+'&id='+id+'&mark='+mark;
    $.post(url, {'ajax':1, 'block_name':'VoteBlock', 'params':params}, function(html){  // голосование 
        eval('response=' + html); 
        if((response.div_id != 'undefined') && (response.div_content != 'undefined')) {
            $('#' + response.div_id).html('');
            $('#' + response.div_id).html(response.div_content);
        }
        
        if(response.js_code) {
            try{
                eval(response.js_code);
            } 
            catch(err) {
                //alert("Inline script error " + err.name + ": " + err.message + "|"+err.line);
            }
        }
    });
}

function changeQIPContestStatus(id, status)
{
    var url = '/contests/qipcontest/';
    params = 'id='+id+'&status='+status;
    $.post(url, {'ajax':1, 'block_name':'QIPContestBlock', 'params':params}, function(html){  // голосование 
        eval('response=' + html); 
        if((response.div_id != 'undefined') && (response.div_content != 'undefined')) {
            $('#' + response.div_id).html('');
            $('#' + response.div_id).html(response.div_content);
        }
        
        if(response.js_code) {
            try{
                eval(response.js_code);
            } 
            catch(err) {
                //alert("Inline script error " + err.name + ": " + err.message + "|"+err.line);
            }
        }
    });
}

//jquery function
$.fn.tagName = function() {
    return this.get(0).tagName.toLowerCase();
}

function isTagExists(text, tag) 
{
    re = /[ \n\r\t]*/gi;
    tag = ' '+tag;
    var trim_text=',' + text.replace(re, '');
    var trim_tag = tag.replace(re, '');
    if (trim_text.indexOf(trim_tag)<0) {
        return false;
    }
    return true;
}

function addSingleTag(tag, identifier, field_id) {
    var exist_tags = $('#'+field_id).attr('value');
    if (isTagExists(exist_tags, tag) == false) {
        $('#current_'+identifier).html(tag);
        if($('#'+field_id).tagName() == 'textarea'){
            $('#'+field_id).html(tag);
        } else {
            $('#'+field_id).attr('value', tag);
        }
    } else {
        $('#popup_'+identifier+'_error_text').html('Тег "' + tag + '" уже добавлен!');
        $('#popup_'+identifier+'_error_div').show();
        setTimeout(function(){$('#popup_'+identifier+'_error_div').hide();}, 2500);
    }
}

function addMultipleTags(tag, identifier, field_id) {
    var exist_tags = $('#'+field_id).html();
    if (isTagExists(exist_tags, tag) == false) {
        if (exist_tags!='') {
            tag = ', ' + tag;
        }
        $('#current_'+identifier).html(exist_tags + tag);
        if($('#'+field_id).tagName() == 'textarea'){
            $('#'+field_id).html(exist_tags + tag);
        } else {
            $('#'+field_id).attr('value', exist_tags + tag);
        }
    } else {
        $('#popup_'+identifier+'_error_text').html('Тег "' + tag + '" уже добавлен!');
        $('#popup_'+identifier+'_error_div').show();
        setTimeout(function(){$('#popup_'+identifier+'_error_div').hide();}, 2500);
    }
}

function getTags(url, block, identifier, field_id)
{
    showPopupDiv('_'+identifier);
    scrollWindowToTop(500);
    params = {'ajax':1, 'block_name':block, 'params':''};
    $.post(url, params, function(html){  // голосование 
        eval('response=' + html); 
        if((response.div_id != 'undefined') && (response.div_content != 'undefined')) {
            $('#' + response.div_id).html('');
            $('#' + response.div_id).html(response.div_content);
            if($('#'+field_id).tagName() == 'textarea'){
                $('#current_'+identifier).html($('#'+field_id).html());
            } else {
                $('#current_'+identifier).html($('#'+field_id).attr('value'));
            }
            $('#' + response.div_id).show();
            $('#popup_'+identifier+'_ajax_loader').hide();
        }
        
        if(response.js_code) {
            try{
                eval(response.js_code);
            } 
            catch(err) {
            }
        }
    });
}

