var xmlHttp
var str1 = 0;

// show clubs in event
function open_clubs (id, path)
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url = path;
    url=url+"?id="+id;
    url=url+"&sid="+Math.random();
    str1 = "";
    xmlHttp.onreadystatechange=stateChangedClubs;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChangedClubs()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        id = "result_club";

//        alert (xmlHttp.responseText);
        var xmldoc = xmlHttp.responseXML;
        if (xmldoc.getElementsByTagName ('content').item (0).firstChild.data.length != 0) {
            document.getElementById(id).innerHTML = xmldoc.getElementsByTagName ('content').item (0).firstChild.data;
        }
    }
}


function add_vote (path, id)
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url = path+'add_vote.php';
    urnm = document.getElementById('quantity_'+id).value;
    
    url=url+"?id="+id;
    url=url+"&value="+urnm;
    url=url+"&sid="+Math.random();
    str1 = "";
    xmlHttp.onreadystatechange=stateChangedVote;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChangedVote()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        id = "resultik";

//        alert (xmlHttp.responseText);
        var xmldoc = xmlHttp.responseXML;
        if (xmldoc.getElementsByTagName ('content').item (0).firstChild.data.length != 0) {
            document.getElementById(id).innerHTML = xmldoc.getElementsByTagName ('content').item (0).firstChild.data;
        }
    }
}




function is_active (table, field, id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url="is_active.php"
    url=url+"?table="+table
    url=url+"&field="+field
    url=url+"&id="+id
    url=url+"&sid="+Math.random()
    str1 = id
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function is_finish (table, field, id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url="is_finish.php"
    url=url+"?table="+table
    url=url+"&field="+field
    url=url+"&id="+id
    url=url+"&sid="+Math.random()
    str1 = "1" + id
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function stateChanged()
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    { 
        id = "resultik";

//        alert (xmlHttp.responseText);
        var xmldoc = xmlHttp.responseXML;
        if (xmldoc.getElementsByTagName ('content').item (0).firstChild.data.length != 0) {
            document.getElementById("resultik").innerHTML = xmldoc.getElementsByTagName ('content').item (0).firstChild.data;
        }
    }
}

function GetXmlHttpObject()
{ 
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }

    return objXMLHttp
}

function getUsername (e, nameField)
{
    var objSel = document.getElementById(nameField+"Select");
    varSelect = objSel.options[objSel.selectedIndex].value;
    document.getElementById(nameField+"Input").value = varSelect;

    // get the event
    e = (!e) ? window.event : e;
    // get the character code of the pressed button
    code = (e.charCode) ? e.charCode :
       ((e.keyCode) ? e.keyCode :
       ((e.which) ? e.which : 0));
    // check to see if the event was keyup
    if (e.type == "keyup")
    {
        if (code == 13) {
            document.getElementById("resultik").style.display = "none";
            document.getElementById(nameField+"Input").focus();
        }
    }
}

function callAction (e, id, path, nameField)
{
    if (nameField == "" || nameField == " ") return;

    // get the event
    e = (!e) ? window.event : e;
    // get the character code of the pressed button
    code = (e.charCode) ? e.charCode :
       ((e.keyCode) ? e.keyCode :
       ((e.which) ? e.which : 0));
    // check to see if the event was keyup
    if (e.type == "keyup")
    {
        // check to see we if are interested in the current character
        if (code == 38 || code == 40)
        {
            document.getElementById(nameField+"Select").focus();
            objSel = document.getElementById(nameField+"Select");

            if (objSel.options.length == 1) {
                varSelect = objSel.options[objSel.selectedIndex].value;
                document.getElementById(nameField+"Input").value = varSelect;
            }
        }
        else if (code == 13) {
            document.getElementById("resultik").innerHTML = "";
            document.getElementById(nameField+"Input").focus();
        }
        else {
            open_league (id, path, nameField)
        }
    }
}

function open_league (id, path, nameField)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url = path;
    urnm = document.getElementById(nameField+"Input").value;

    url=url+"?id="+urnm;
    url=url+"&nameField="+nameField;
    url=url+"&sid="+Math.random()
    str1 = ""
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}




function open_first_level (path, id, second_level, event_id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url = path;
    url= url + "?id=" + id;
    url = url + "&event_id=" + event_id;
    url = url + "&second_l=-1";
    url = url + "&sid=" + Math.random();

    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}





function open_league_player (id, club_id, team_id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_league.php"
    url=url+"?id="+id
    url=url+"&club_id="+club_id
    url=url+"&team_id="+team_id
    url=url+"&sid="+Math.random()
    str1 = ""
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function open_club (club_id, league_id, team_id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_club.php"
    url=url+"?club_id="+club_id
    url=url+"&league_id="+league_id
    url=url+"&team_id="+team_id
    url=url+"&sid="+Math.random()
    str1 = "_t"
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function opentft (team_id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_team.php"
    url=url+"?team_id="+team_id
    url=url+"&sid="+Math.random() 

    str1 = ""

    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function opentcl (club_id, top_type_id, filterDate)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_club_top.php"
    url=url+"?club_id="+club_id
    url=url+"&top_type_id="+top_type_id
    url=url+"&filterDate="+filterDate
    url=url+"&sid="+Math.random() 
    str1 = ""
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function openlg (league_id, top_type_id, filterDate)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_league_top.php"
    url=url+"?league_id="+league_id
    url=url+"&top_type_id="+top_type_id
    url=url+"&filterDate="+filterDate
    url=url+"&sid="+Math.random()
    str1 = ""
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}





function open_league1 (id, club_id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_league.php"
    url=url+"?id="+id
    url=url+"&club_id="+club_id
    url=url+"&team_id=-1"
    url=url+"&add="+1
    url=url+"&sid="+Math.random()
    str1 = "1"
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function open_club1 (club_id, league_id, team_id)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_club.php"
    url=url+"?club_id="+club_id
    url=url+"&league_id="+league_id
    url=url+"&team_id="+team_id
    url=url+"&add="+1
    url=url+"&sid="+Math.random()
    str1 = "_t1"
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function openteamMOM (team_id, player_id)
{ 

    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_teamMOM.php"
    url=url+"?team_id="+team_id
    url=url+"&player_id="+player_id
    url=url+"&sid="+Math.random()
    str1 = ""
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function open_pos_num (player_id, dest)
{ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="open_pos_num.php"
    url=url+"?player_id="+player_id
    url=url+"&dest="+dest
    url=url+"&sid="+Math.random()
    if (dest == "player_home_id")
    {
        str1 = "_home"
    }
    else
    {
        str1 = "_guest"
    }
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

// show clubs in event
function get_icons (id, mem_id)
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url;
    url="fgame_icons.php?id="+id+"&mem_id="+mem_id;
    str1 = "";
    xmlHttp.onreadystatechange=stateChangedFGIcons;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChangedFGIcons()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        id = "gameID";

        var xmldoc = xmlHttp.responseXML;
//        alert (xmlHttp.responseText);
        if (xmldoc.getElementsByTagName ('content').item (0).firstChild.data.length != 0) {
            //alert(xmldoc.getElementsByTagName ('content').item (0).firstChild.data);
            document.getElementById(id).innerHTML = xmldoc.getElementsByTagName ('content').item (0).firstChild.data;
        }
    }
}


function insertInEditor (flag, editorId, num_editor)
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    if (num_editor == 1)
    {
        if (flag == 1){
           fname = document.getElementById("player1Input").value;
           document.getElementById("player1Input").value = "";
        }
        else{
            fname = document.getElementById("team1Input").value;
            document.getElementById("team1Input").value = "";
        }
    }
    else {
        if (flag == 1){
           fname = document.getElementById("player2Input").value;
           document.getElementById("player2Input").value = "";
        }
        else{
            fname = document.getElementById("team2Input").value;
            document.getElementById("team2Input").value = "";
        }
    
    }
    

    var url;
    url="into_editor.php?fname="+fname+"&flag="+flag;
    str1 = editorId;
    xmlHttp.onreadystatechange=stateChangedEditor;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChangedEditor()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        var xmldoc = xmlHttp.responseXML;
        if (xmldoc.getElementsByTagName ('content').item (0).firstChild.data.length != 0) {
            var sign;
            sign = xmldoc.getElementsByTagName ('content').item (0).firstChild.data;
            tinyMCE.execInstanceCommand (str1,'mceInsertContent', false, sign);               
        }
    }
}


function fAddRewardType()
{
	rname = document.getElementById('reward_name').value;
	rpic = document.getElementById('reward_pic').value;
	
	if(rname != '' && rpic!= '')
	{
		$.post(
		  'settings.php',
		  {
		   'act' : "add_reward_type",
		   'rname' : rname,
		   'rpic' : rpic
		  },
		  onAjaxAddRewardType
		);
	}
}

function onAjaxAddRewardType(data)
{
	eval(data);
	if(data.rez == 'ok')
	{
		sel = document.getElementById('sel_rewards');
		op = document.createElement("OPTION");
		op.value = data.pic;
		op.text = data.rw;
		op.id = data.id;
		sel.appendChild(op);

		alert('Награда добавлена');
	}
	else
		alert('Ошибка при добавлении');
}

function onAjaxEditRewardType(data)
{
	eval(data);
	sel = document.getElementById('sel_rewards');
	op = document.getElementById(data.op_id);
	op.value = data.pic;
	op.text = data.rw;

	alert('Награда отредактирована');
}

function fEditRewardType()
{
	rname = document.getElementById('reward_name').value;
	rpic = document.getElementById('reward_pic').value;
	sel = document.getElementById('sel_rewards');
	id = sel.options[sel.selectedIndex].id;
	
	if(rname != '' && rpic!= '')
	{
		$.post(
		  'settings.php',
		  {
		   'act' : "edit_reward_type",
		   'rname' : rname,
		   'rpic' : rpic,
		   'id' : id
		  },
		  onAjaxEditRewardType
		);
	}
}

function onAjaxDelRewardType(data)
{
	sel = document.getElementById('sel_rewards');
	op = sel.options[sel.selectedIndex];
	sel.removeChild(op);
	document.getElementById('reward_pic_img').src = 'null';
	document.getElementById('reward_name').value = '';
	document.getElementById('reward_pic').value	= '';

	alert('Награда была удалена');	
}

function fDeleteRewardType()
{
	sel = document.getElementById('sel_rewards');
	pic = sel.options[sel.selectedIndex].value;
	rw = sel.options[sel.selectedIndex].text;
	id = sel.options[sel.selectedIndex].id;

	if(rw != '')
	{
		$.post(
		  'settings.php',
		  {
		   'act' : "del_reward_type",
		   'rw' : rw,
		   'pic' : pic,
		   'id' : id
		  },
		  onAjaxDelRewardType
		);
	}
}

function fSelectReward(sel)
{
	pic = sel.options[sel.selectedIndex].value;
	img = document.getElementById('reward_pic_img');
	img.src = '/images/rewards/' + pic;
	
	document.getElementById('reward_name').value = sel.options[sel.selectedIndex].text;
	document.getElementById('reward_pic').value = sel.options[sel.selectedIndex].value;
}