function submitForm(frm,slct){
    if (slct.selectedIndex != 0)
        frm.submit();
}
function selectAllCmn(ch_name){
    if (!selected_all) {
        selectAll(ch_name);
        selected_all = true;
    } else {
        cleanAll(ch_name);
        selected_all = false;
    }
}
function selectAll(ch_name){
    var ch = document.getElementsByName(ch_name);
    for (i=0;i<ch.length;i++)
        ch[i].checked = true;
}
function cleanAll(ch_name) {
    var ch = document.getElementsByName(ch_name);
    for (i=0;i<ch.length;i++)
        ch[i].checked = false;
}
function checkForm(frm,ch_name,lang) {
    var atleastone = false;
    var ch = document.getElementsByName(ch_name);

    for(i=0;i<ch.length;i++){
        if(ch[i].checked){
            atleastone = true;
            break;
        }
    }

    if (!atleastone){
        if (lang == 'en')
            alert("Please select at least one league!");
        else
            alert("Lütfen en az bir lig seçiniz!");
        return false;
    }

    return true;
}

function checkFormIddaa(frm,lang) {
    for (i=0;i<frm.length-1;i++) {
        if (frm.elements[i].value != '')
            return true;
    }

    alert('Lütfen en az bir maç giriniz!');
    return false;
}

function openMatchPage(id,lang) {
    w = 520;
    h = 200;
    if (lang == 'en')
        page = '/canliskorlar/match_info.php?lang=en&id=' + id;
    else
        page = '/canliskorlar/match_info.php?id=' + id;
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	LeftPosition = 20;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    TopPosition = 20;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no'
	win = window.open(page,'matchSX',settings);
}

function goStats(url) {
   // wopen(url,"580","400","h2h");
    window.open(url);
}

function wopen(adres,w,h,r){
	yuk = (screen.height/2)-(h/2);
	gen = (screen.width/2)-(w/2);
	if (!r) r = "yenip";
	yeni = window.open( adres , r ,'location=no,scrollbars=yes,width='+ w+ ',height=' + h + ',top='+ yuk +',left='+ gen);
}

function openIddaaPage(id,lang) {
    w = 500;
    h = 230;
    if (lang == 'en')
        page = '/canliskorlar/iddaa_info.php?lang=en&id=' + id;
    else
        page = '/canliskorlar/iddaa_info.php?id=' + id;
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	LeftPosition = 20;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    TopPosition = 20;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no'
	win = window.open(page,'iddaaSX',settings);
}

function hideMatch(id) {
    div = "#match_" + id;
    $(div).hide("slow");
}

var viewby = 1;
function toggleView(value) {
    viewby = value;
    match_array = new Array();
    match_array_temp = new Array();
    $('#live_list div[id^=match_]').each(function(i) {
        cnt = $(this).attr("country");
        lg = $(this).attr("league");
        tm = $(this).attr("time");
        mid = $(this).attr("id");
        iddaa = $(this).attr("iddaa");
        mid = mid.substr(6);
        match_array[i] = [mid,tm,cnt,lg,iddaa,i];
        //match_array.push($(this).)
        //$(this).show();
    });
    //alert(match_array);
    text_1 = ''
    for (i=0;i<match_array.length;i++) {
        td = '#td_l_' + i;
        temp_html = $(td).html();
        match_array_temp[i] = temp_html;
        text_1 = text_1 + '\n' +match_array[i][0] + '*' + match_array[i][1];
    }

    // silinecek
    //alert(text_1);
    match_array.sort(sortArray);
    text = '';
    text_2 = '';
    for (i=0;i<match_array.length;i++) {
        text_2 = text_2 + '\n' +match_array[i][0]+ '*' + match_array[i][1];
    }
    //alert(text_2);
    //
    for (i=0;i<match_array.length;i++) {
        text = text + match_array[i] + '\n';
        td = '#td_l_' + i;
        $(td).html('');
        $(td).html(match_array_temp[match_array[i][5]]);
    }
    //alert(text);
}
function sortArray(a,b) {
    //return  b[viewby] - a[viewby];
    return  a[viewby] - b[viewby];
}

function toggleCountry(id,action) {
    if (id == 0) {
        showHiddenDivs('match_');
        arrangeLeagueSelect(c_leagues,true);
        $("#frm_trnId").selectOptions("0", true);
        return true;
    }

    hideAllDivs();
    c_leagues = new Array();

    div_c = "div[country=" + id + "]";
    $(div_c).each(function(i) {
        if (jQuery.inArray($(this).attr("league"), c_leagues) == -1)
            c_leagues.push($(this).attr("league"));
        if (action == 'show')
            $(this).hide();
        else
            $(this).show();
    });
    arrangeLeagueSelect(c_leagues,false);
    //c_leagues = jQuery.unique(c_leagues);
    //alert(c_leagues.join());
}


function arrangeLeagueSelect(leagues,all) {
    $("#frm_trnId").removeOption(/./);
    if (lang_value == 'en')
        $("#frm_trnId").addOption("0", "All");
    else
        $("#frm_trnId").addOption("0", "Hepsi");
    //alert(leagues.join() + ' : ' + jQuery.inArray("49", leagues));
    for (i=0;i<js_leagues.length;i++) {
        if (jQuery.inArray(String(js_leagues[i][0]), leagues) != -1 || all) {
            $("#frm_trnId").addOption(js_leagues[i][0],js_leagues[i][1]);
        }
    }
    $("#frm_trnId").selectOptions("0", true);
}

function toggleLeague(id,action) {
    if (id == 0) {
        selected_country = $("#frm_catId").selectedValues();
        if (selected_country == 0) {
            showHiddenDivs('match_');
            return true;
        }
        else {
            id = selected_country;
            toggleCountry(id,'hide');
            return true;
        }
    }
    hideAllDivs();

    div_c = "div[league=" + id + "]";
    $(div_c).each(function(i) {
        if (action == 'show')
            $(this).hide();
        else
            $(this).show();
    });
}

function showHiddenDivs(prefix) {
    div = "#match_"
    $('div[id^=match_]').each(function(i) {
        $(this).show();
    });
}

function hideAllDivs() {
    $('div[id^=match_]').each(function(i) {
        $(this).hide();
    });
}

var default_zone = 2;
function changeTimeZone(slct) {
    //alert(default_zone);
    tm_zone = slct.value;
    diff = tm_zone - default_zone;
    default_zone = slct.value;

    $('div[id^=dk_]').each(function(i) {
        if ($(this).html().indexOf(":") >= 0) {
            value = $(this).html();
            time = value.split(':');
            hour = time[0]*1;
            new_hour = hour + diff;
            if (new_hour > 23)
                new_hour = new_hour % 24;
            if (new_hour < 0)
                new_hour = 24 + new_hour;
            zr = String(new_hour);
            //alert('')
            zr = (zr.length == 1) ? '0' : '';
            //zr = zr + '';
            new_value = zr + new_hour + ':' + time[1];
            $(this).html(new_value);
        }
    });
    changeCurrentTime(diff);
}

function changeCurrentTime(diff) {
    value = $('#time_sp').html();
    time = value.split(':');
    hour = time[0]*1;
    new_hour = hour + diff;
    if (new_hour > 23)
        new_hour = new_hour % 24;
    if (new_hour < 0)
        new_hour = 24 + new_hour;
    zr = String(new_hour);
    //alert('')
    zr = (zr.length == 1) ? '0' : '';
    //zr = zr + '';
    new_value = zr + new_hour + ':' + time[1];
    $('#time_sp').html(new_value);
}

function setSound(value) {
    if (value == 'off')
        sound = true;
    else
        sound = false;
    $.cookie('ck_sound', value, { expires: 7 });
}

function setSessionSound(g,y,r,f) {
    //alert(g + '\n' + y +'\n' + r);
    sound_goal = g;
    sound_yc = y;
    sound_rc = r;
    sound_ft = f;
}
