function facebook_login(loc){
    new Ajax.Request('/facebook/connect'+'/'+loc, {
        method:'get',
        asynchronous:true,
        evalScripts:true
    });
}

function connect_user(name){
    callPublish('',{
        'description': name+' registered to play sport in the UAE on http://DUPLAYS.com'
    },null);
}

//function publish_activity_on_fb(name, provider_name, activity_name, sport, activity_type, city){
//    callPublish('',{
//        'name':'Registered a league',
//        'href':'http://DUPLAYS.com',
//        'description': name+' signed up for '+provider_name+' \"'+activity_name+'\" '+sport+' '+activity_type+' in '+city+' at http://DUPLAYS.com'
//    },null);
//}

function publish_activity_on_fb(name, provider_name, activity_name, sport, activity_type, city, link){
    callPublish('',{
        'name':'Signed up for '+sport+' in '+city,
        'href':link,
        'description': name+' signed up for '+provider_name+' \"'+activity_name+'\" '+sport+' '+activity_type+' in '+city+' at http://DUPLAYS.com'
    },null);
}

function callPublish(msg, attachment, action_link) {
    FB.ensureInit(function () {
        FB.Connect.streamPublish('',attachment,action_link,null,null,null,true,null);
    });
}

function facebook_disconnect(){
    new Ajax.Request('/facebook/disconnect', {
        method:'get',
        asynchronous:true,
        evalScripts:true
    });
}

function set_fb_access_permission_for_user(user_fb_id) {
    if (user_fb_id) {
        var fb_pages = new Array();
        fb_pages[0] = user_fb_id;
        FB.ensureInit(function(){
            FB.Connect.requireSession(function(){
                FB.Connect.showPermissionDialog("read_stream, publish_stream, offline_access, create_note", function(x){
                    if(x){
                        update_autopost_status(x);
                    }
                }, true, fb_pages);
            });
        });
    }
}

function update_autopost_status(){
    window.location = '/user/settings';
}


function show_league_member_div(){
    document.getElementById('league_member_div').style.display = '';
}

function popUp(id){
    if(document.getElementById(id).style.display == 'none'){
        document.getElementById(id).style.display = '';
        if(id == 'loginPopUpId'){
            document.getElementById('registrationPopUpId').style.display = 'none';
        }else if (id == 'registrationPopUpId'){
            document.getElementById('loginPopUpId').style.display = 'none';
        }
    }else{
        document.getElementById(id).style.display = 'none';
    }
}

function bodyDropDown(id){
    if(document.getElementById(id).style.display == 'none'){
        document.getElementById(id).style.display = '';
    }else{
        document.getElementById(id).style.display = 'none';
    }
}

function openAllSports(id,imgId){
    if(document.getElementById(id).style.display == 'none'){
        document.getElementById(id).style.display = '';
        document.getElementById(id).style.display = '';
        document.getElementById(imgId).src='/images/up_arrow.gif';
    }else{
        document.getElementById(id).style.display = 'none';
        document.getElementById(imgId).src='/images/down_arrow.gif';
    }
}

function eFocus(field, obj){
    if (field.value == obj){
        field.value ='';
    }
}

function eBlur(field, obj){
    if (field.value == ''){
        field.value = obj;
    }
}

function show_registration(){
    popUp('registrationPopUpId');
}

function changeAll(field,parentId){
    for (i = 0; i < field.length; i++){
        if(field[i].checked == false){
            document.getElementById(parentId).checked=false;
            return true;
        }else{
            document.getElementById(parentId).checked=true;
        }
    }
    return true;
}
function checkUncheck(field,parentId){
    if(document.getElementById(parentId).checked==true){
        checkAll(field);
    }else{
        uncheckAll(field);
    }
}
function checkAll(field){
    for (i = 0; i < field.length; i++){
        field[i].checked = true ;
    }
}
function uncheckAll(field){
    for (i = 0; i < field.length; i++){
        field[i].checked = false ;
    }
}
function changeCheckBox(id){
    if(document.getElementById(id).checked){
        document.getElementById(id).checked = false;
    }else{
        document.getElementById(id).checked = true;
    }
}

function highlightTab(league_id, showTab1){
    $('details').innerHTML=" ";
    $(showTab1).className = 'bigButtonGreen';
    $('searchLoader').hide();
    if (showTab1=='schedules_rules'){
        $('searchLoader').show();
        if ($('standings')){$('standings').className = 'bigButton'};
        if ($('new_rosters')){$('new_rosters').className = 'bigButton'};
        $('message_box').className = 'bigButton';
        $('details').style.display = '';
        $('message_box_details').style.display = 'none';
        new Ajax.Request('/league/schedules_rules/'+league_id, {
            method:'get',
            asynchronous:true,
            evalScripts:true
        });
    }
    else if(showTab1=='standings'){
        $('searchLoader').show();
        if ($('schedules_rules')){$('schedules_rules').className = 'bigButton'};
        if ($('new_rosters')){$('new_rosters').className = 'bigButton'};
        $('message_box').className = 'bigButton';
        $('details').style.display = '';
        $('message_box_details').style.display = 'none';
        new Ajax.Request('/league/standings/'+league_id, {
            method:'get',
            asynchronous:true,
            evalScripts:true
        });
    }
    else if(showTab1=='new_rosters'){
        $('searchLoader').show();
        if ($('schedules_rules')){$('schedules_rules').className = 'bigButton'};
        if ($('standings')){$('standings').className = 'bigButton'};
        $('message_box').className = 'bigButton';
        $('details').style.display = '';
        $('message_box_details').style.display = 'none';
        new Ajax.Request('/league/new_rosters/'+league_id, {
            method:'get',
            asynchronous:true,
            evalScripts:true
        });
    }
    else if(showTab1=='message_box'){
        if ($('standings')){$('standings').className = 'bigButton'};
        if ($('new_rosters')){$('new_rosters').className = 'bigButton'};
        if ($('schedules_rules')){$('schedules_rules').className = 'bigButton'};
        $('message_box_details').style.display = '';
        $('details').style.display = 'none';
    }
}

function registerNow(){
    document.getElementById('registrationPopUpId').style.display = '';
}