function Go(link)
{
    document.location=link;
	return true;
}

function CGo(link, mesg)
{
    if (confirm(mesg))
	{
		document.location=link;
		return true;
	}
}

function _v(id)
{
    return document.getElementById(id);
}

function isNumeric(str)
{
  if (str.length == 0) return false;
  for (var i=0; i < str.length; i++)
     {
      var ch = str.substring(i, i+1);
      if ( ch < "0" || ch>"9" || str.length == null)  return false;
    }
  return true;
}

function isDigit(charCode) 
{ 
	return (charCode >= 48 && charCode <= 57) 
}

function Show(img, w, h)
{
    if (!w)
	{
	    w = 100;	
	}
	if (!h)
	{
	    h = 100;	
	}
	window.open('/showimg.php?img='+img,'popup','resizable=1,left=0,top=0, width='+w+', height='+h+', scrollbars=yes'); 	
}

function ShowM(fid)
{
	window.open('/play.php?fid='+fid,'popup','resizable=1,left=0,top=0, width=435, height=420, scrollbars=yes'); 	
}

//**************************************************************//
//           Vote 
//**************************************************************//
function Vote( id,  rate, block, rblock ) {   
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
		    _v( block ).innerHTML =  req.responseJS.q;
			if ( req.responseJS.eq != '' ) {
			    _v( rblock).innerHTML = req.responseJS.eq; 
			}
        }
    }
	rate = (1 == rate || 2 == rate) ? rate : 1;
    req.open(null, '/pajax.php', true);
	req.send( {action: 'rate', id: id, rate: rate, block: block, rblock: rblock} );
}

function VoteNews( what, eid,  rate, block, rblock ) {   
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
		    _v( block ).innerHTML =  req.responseJS.q;
		    if ( req.responseJS.eq != '' ) {
			    _v( rblock).innerHTML = req.responseJS.eq; 
			}
        }
    }
	rate = (1 == rate || 2 == rate) ? rate : 1;
    req.open(null, '/pajax.php', true);
	req.send( {action: 'vote', what: what, eid: eid, rate: rate, block: block, rblock: rblock} );
}


function DelRate(id)
{
    $.ajax({
        type:     'POST',
        dataType: 'json',
        data:     'id='+id,
        url:      '/pajax.php?action=del_rate',
        success: function () {
            
        }
    });
}


function Stop(uid)
{
     $.ajax({
        type:     'POST',
        dataType: 'json',
        url:      '/pajax.php?action=stop&uid='+uid,
        success: function () {
            document.location = document.location;
        }
    });
    
}

function JuriOut(uid)
{
    $.ajax({
        type:     'POST',
        dataType: 'json',
        url:      '/pajax.php?action=juri_out&uid='+uid,
        success: function () {
            document.location = document.location;
        }
    });
    
}

function UpdCanPlay(id, flag)
{
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/pajax.php?action=can_play&id='+id+'&can='+flag,
        success: function () {
            document.location = document.location;
        }
    });

}


//**************************************************************//
//          Links for original tracks
//**************************************************************//
function AddLink(id){
 
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
                $('#links').html(req.responseJS.q);
                $('a.links').bind('click', function(e){
                    $(this).remove();
                });
        }
    }
    req.caching = false;
    req.open('POST', '/ajax.php', true);
    var id = (null == id) ? 0 : id;
    req.send( {act: 'makelink', file_id:id} );
     
//   $.post("/ajax.php",
//            { act: 'makelink', file_id : id },
//            function(data){
//                $('#links').html(data);
//            });
 
}


//**************************************************************//
//          Comments
//**************************************************************//
function AddCom(module, tblname, item_id, item_uid, story, err_list, res_list, waitb, id ) {
	_v(waitb).innerHTML = 'Подождите...';
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {  
			_v(waitb).innerHTML = 'Оставить комментарий';
			if (null != req.responseJS.q && req.responseJS.q) {
                _v(err_list).style.color = 'green';	
                _v( err_list ).innerHTML = (null == id || !id) ? 'Комментарий добавлен' : 'Комментарий изменен'; 				
			    _v(res_list).innerHTML = req.responseJS.q;
			}
			if (null != req.responseJS.eq && req.responseJS.eq) {
			    _v(err_list).style.color = 'red';		
                _v( err_list ).innerHTML = req.responseJS.eq;  
            }			
        }
    }        
    req.caching = false;
    req.open('POST', '/ajax.php', true);
	var id = (null == id) ? 0 : id;
    req.send( {act: 'addcom', module:module, tblname: tblname, fm: {item_id: item_id, item_uid: item_uid, story: story, id: id}} );
}

function AddComMulti(module, tblname, item_id, item_uid, parent, story, err_list, res_list, waitb, id ) {

	var req = new JsHttpRequest(); 
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (null != req.responseJS.q && req.responseJS.q) {
                    _v(err_list).style.color = 'green';
                    _v(err_list).innerHTML = (null == id || !id) ? 'Комментарий добавлен' : 'Комментарий изменен';
                    _v(res_list).innerHTML = req.responseJS.q;
                }
                if (null != req.responseJS.eq && req.responseJS.eq) {
                    _v(err_list).style.color = 'red';
                    _v( err_list ).innerHTML = req.responseJS.eq;
                }
                ClearFileFormG('file1');
                $('#file3').val(0);

                //ClearFileFormG('file2');
                }
            }
        req.caching = false;
        req.open('POST', '/ajax.php', true);
	var id = (null == id) ? 0 : id;
        req.send( {act: 'addcom', module:module, tblname: tblname, fm: {is_multi: '1', item_id: item_id, item_uid: item_uid, parent: parent, story: story, id: id, file1: $('#file1_name').val(), file3: $('#file3').val()}} );
}


function ShowComEdit(module, tblname, item_id, res_list, id) {
    alert(id);
}

function ShowComDel(module, tblname, item_id, res_list, id) {
    if (confirm('Удалить комментарий?')) {
	    var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {  
			    if (null != req.responseJS.q && req.responseJS.q) {  	
			        _v(res_list).innerHTML = req.responseJS.q;
			    }		
            }
        }        
        req.caching = false;
        req.open('POST', '/ajax.php', true);
	    var id = (null == id) ? 0 : id;
        req.send( {act: 'delcom', module:module, tblname: tblname, fm: {item_id: item_id, id: id}} );
	}
}

function ShowComDelMulti(module, tblname, item_id, res_list, id) {
    if (confirm('Удалить комментарий?')) {
	    var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {  
			    if (null != req.responseJS.q && req.responseJS.q) {  	
			        _v(res_list).innerHTML = req.responseJS.q;
			    }		
            }
        }        
        req.caching = false;
        req.open('POST', '/ajax.php', true);
	    var id = (null == id) ? 0 : id;
        req.send( {act: 'delcom', module:module, tblname: tblname, fm: {item_id: item_id, is_multi: '1', id: id}} );
	}
}


/** ajax file upload */
function ajaxFileUploadG( file_id, file_type )
{
    $("#"+file_id+"_loading")
    .ajaxStart(function(){
        $(this).show();
    })
    .ajaxComplete(function(){
        $(this).hide();
    });
    $('#'+file_id+'_uplFileStatus').hide();

    $.ajaxFileUpload( {
        url:'/ajax.php?act=ajaxfileupload&fid='+file_id+'&ftype='+file_type,
        secureuri:false,
        fileElementId:file_id,
        dataType: 'json',
        success: function (data, status)
        {
            if(typeof(data.error) != 'undefined')
            {
                if(data.error != '')
                {
                    $('#'+file_id+'_uplFileMsg').html(data.error);
                }
                else
                {
                    $('#'+file_id+'_name').val(data.imgname);
                    
                    $('#'+file_id+'_uplFileMsg').html('Загружен файл: ' + data.msg);
                }
                $('#'+file_id+'_uplFileStatus').show();
            }
        },
        error: function (data, status, e)
        {
            $('#'+file_id+'_uplFileMsg').html('При загрузке файла произошла ошибка: '+e);
            $('#'+file_id+'_uplFileStatus').show();
        }
    });
    
    $('#'+file_id).attr('disabled', true);
    $('#'+file_id).attr('value', '');
       
    return true;
}


function ClearFileFormG(file_id)
{
    $('#'+file_id+'_uplFileStatus').hide();
    $('#'+file_id).attr('disabled', false);
    $('#'+file_id+'_name').val('');
}

