emailshow=0;
function show_email(id,email,max,label) {
        y="";
        if(emailshow>=max) {
                alert("Due to spammers we are forced to hide our emails and we only show up to "+max+" emails at the same time. Please reload the page if you need to see more email addresses.");
                return false;
        }
        for(i=0;i<email.length;i++) 
                y+=String.fromCharCode(email.charCodeAt(i)-5);
        document.getElementById(id).innerHTML="<a href=mailto:"+y+">"+y+"</a>";
        if(label && label.length>0) {
                r=document.getElementById(label);
                if(r) {
                        r.value=y;
                }
        }
        emailshow++;
        return false;
}

var cteam;

var COL_SELECT='#8cee9c';
var COL_RELATE='#bcbcee';
var COL_RELATE_OTHER='#ccecff';
var COL_NONE='white';
var COL_LOTS='#ffff00';
var COL_MISS='#0000ff';
var COL_EMPTY='#000000';
var COL_PREFTIME='#cbb2f1';
var COL_NONPREFTIME='#cd9782';
var COL_BADTIME='#eebcbc';
var COL_GOODTIME='#dceedc';
var COL_UNRELATED_GAME='#cccccc';
var COL_UNRELATED_GAME='rgb(204, 204, 204)';
var COL_TOOCLOSE='#eeeebc';
var COL_BADTEAM='#ff0000';

var swapping=0;
var swapid1="";
var swapid2="";
var dogames=0;

function sched_init() {
        sched_del();
        sched_swap();
        document.getElementById("badteam").style.backgroundColor=COL_BADTEAM;
        document.getElementById("unrelated_game").style.backgroundColor=COL_UNRELATED_GAME;
        document.getElementById("badtime").style.backgroundColor=COL_BADTIME;
        document.getElementById("goodtime").style.backgroundColor=COL_GOODTIME;

        document.getElementById("select").style.backgroundColor=COL_SELECT;
        document.getElementById("relate").style.backgroundColor=COL_RELATE;
        document.getElementById("relate_other").style.backgroundColor=COL_RELATE_OTHER;
        document.getElementById("badteamt").style.backgroundColor=COL_BADTEAM;
        document.getElementById("preftime").style.backgroundColor=COL_PREFTIME;
        document.getElementById("nonpreftime").style.backgroundColor=COL_NONPREFTIME;
        document.getElementById("tooclose").style.backgroundColor=COL_TOOCLOSE;
}

function sched_swap() {
        if(swapping) {
                swapping=0;
                dogames=0;
                var id=document.getElementById("swapper");
                id.style.backgroundColor='white'; 
                swapid1="";
                swapid2="";
                sched_del();
        } else {
                if(dogames) sched_dogames();
                swapping=1;
                dogames=1;
                var id=document.getElementById("swapper");
                id.style.backgroundColor='blue'; 
                sched_del();
        }
}

function sched_dogames() {
        if(dogames) {
                var id=document.getElementById("dogames");
                id.style.backgroundColor='white'; 
                sched_del();
        } else {
                if(swapping) sched_swap();
                var id=document.getElementById("dogames");
                id.style.backgroundColor='blue'; 
                sched_del();
        }
}

function sched_del() {
        sched_select('X');
}

function abs(t) {
        if(t<0) return -t;
        return t;
}

function hasPrefTime(id) {
   return (pref1_start[id]>0 && pref1_end[id]<60*24*7) ||
          (pref2_start[id]>0 && pref2_end[id]<60*24*7) ||
          (pref3_start[id]>0 && pref3_end[id]<60*24*7) ||
          (pref4_start[id]>0 && pref4_end[id]<60*24*7);

}

function isPrefTime(id,time) {
   return (pref1_start[id]<time+GAME_LENGTH && pref1_end[id]>time) ||
          (pref2_start[id]<time+GAME_LENGTH && pref2_end[id]>time) ||
          (pref3_start[id]<time+GAME_LENGTH && pref3_end[id]>time) ||
          (pref4_start[id]<time+GAME_LENGTH && pref4_end[id]>time);
}

function isByeTime(id,time) {
   return (bye1_start[id]<time+GAME_LENGTH && bye1_end[id]>time) ||
          (bye2_start[id]<time+GAME_LENGTH && bye2_end[id]>time) ||
          (bye3_start[id]<time+GAME_LENGTH && bye3_end[id]>time) ||
          (bye4_start[id]<time+GAME_LENGTH && bye4_end[id]>time);
}

function sched_addgame(h,a) {
        // check if there is already a game for one of the teams, if so check if there is a good spot, if
        // not ignore that fact.
        for(var i in home) {
                if(home[i]==h || away[i]==h || home[i]==a || away[i]==a) {
                        p=gamenext[gamenext[i]];
                        if(p) {
                                if((home[p]=='X' || home[p]=='x') && (away[p]=='X' || away[p]=='x') && !isByeTime(h,game[i]) && !isByeTime(a,game[i])) {
                                        swapping=0;
                                        cteam=h;
                                        sched_set('H',p);
                                        cteam=a;
                                        sched_set('A',p);
                                        sched_del();
                                        swapping=1;
                                        return;
                                }

                        }
                        p=gameprev[gameprev[i]];
                        if(p) {
                                if((home[p]=='X' || home[p]=='x') && (away[p]=='X' || away[p]=='x') && !isByeTime(h,game[i]) && !isByeTime(a,game[i])) {
                                        swapping=0;
                                        cteam=h;
                                        sched_set('H',p);
                                        cteam=a;
                                        sched_set('A',p);
                                        sched_del();
                                        swapping=1;
                                        return;
                                }

                        }
                }
        }
        // Check if there is a related team, use spot if there is a good one, otherwise ignore
        // this fact
        for(var i in home) {
                if(sameteam[home[i]+"-"+h] || sameteam[away[i]+"-"+h] || sameteam[home[i]+"-"+a] || sameteam[away[i]+"-"+a]) {
                        p=gamenext[i];
                        if(p) {
                                if((home[p]=='X' || home[p]=='x') && (away[p]=='X' || away[p]=='x') && !isByeTime(h,game[i]) && !isByeTime(a,game[i])) {
                                        swapping=0;
                                        cteam=h;
                                        sched_set('H',p);
                                        cteam=a;
                                        sched_set('A',p);
                                        sched_del();
                                        swapping=1;
                                        return;
                                }

                        }
                }
        }

        var besti=-1;
        var bestval=-1;
        for(var i in home) {
                if((home[i]=='X' || home[i]=='x') && (away[i]=='X' || away[i]=='x') && !isByeTime(h,game[i]) && !isByeTime(a,game[i])) {
                        var val;
                        val=0;
                        if(isPrefTime(h,game[i])) { val+=100; }
                        if(isPrefTime(a,game[i])) { val+=100; }
                        if(val>bestval) {
                                bestval=val;
                                besti=i;
                                //alert("val="+val +"  bestval="+bestval+"  i="+i);
                        }
                }
        }
        if(besti==-1) {
                alert("No free game spot found");
        } else {
                swapping=0;
                cteam=h;
                sched_set('H',besti);
                cteam=a;
                sched_set('A',besti);
                sched_del();
                swapping=1;
        }
}
function sched_delgame(h,a) {
        for(var i in home) {
                if(home[i]==h && away[i]==a || home[i]==a && away[i]==h) {
                        cteam='X';
                        swapping=0;
                        sched_set('H',i);
                        sched_set('A',i);
                        swapping=1;
                        return;
                }
        }
        alert("No free game spot found");
}


function sched_select(t) {
        var gm=new Array();
        var cnt=new Array();
        var othergametime=new Array();
        var othergametimei=new Array();
        //id=document.getElementById("S_"+cteam);
        //if(id) { id.style.backgroundColor='white'; }
        var firstgame=-1;
        var lastgame=-1;
        var teamsset=new Array();

        for(var i in team) {
                cnt[i]=0;
                for(var j in team) {
                        if(age[i]==age[j]) {
                                gm[i+"-"+j]="";
                                gm[j+"-"+i]="";
                        }
                }
        }
        var to=new Array();
        var ftoc=1;
        to[t]=1;
        while(ftoc) {
                ftoc=0;
                for (var i in home) {
                        for(x in to) {
                                if((home[i]==x || sameteam[away[i]+"-"+x]) && !to[away[i]]) { ftoc=1;to[away[i]]=1; }
                                if((away[i]==x || sameteam[home[i]+"-"+x]) && !to[home[i]]) { ftoc=1;to[home[i]]=1; }
                        }
                }
        }
        for (var i in home) {
                if(firstgame==-1 || firstgame>game[i]) 
                        firstgame=game[i];
                if(lastgame==-1 || lastgame<game[i]) 
                        lastgame=game[i];
                id=document.getElementById("H_"+i);
                var foundrel=false;
                var foundbadrel=false;
                if(id) {
                        if(home[i] == t) {
                                id.style.backgroundColor=COL_SELECT;
                        } else if(sameteam[t+"-"+home[i]]==1) {
                                id.style.backgroundColor=COL_RELATE;
                        } else if(cnt[home[i]]>=2) {
                                id.style.backgroundColor=COL_BADTEAM;
                        } else if(othergametime[home[i]] && abs(othergametime[home[i]]-game[i])!=2*55) {
                                id.style.backgroundColor=COL_TOOCLOSE;
                                document.getElementById(othergametimei[home[i]]).style.backgroundColor=COL_TOOCLOSE;
                        } else if(to[home[i]]) {
                                id.style.backgroundColor=COL_RELATE_OTHER;
                        } else if(isByeTime(home[i],game[i])) {
                                id.style.backgroundColor=COL_BADTEAM;
                        } else if(isPrefTime(home[i],game[i])) {
                                id.style.backgroundColor=COL_PREFTIME;
                        } else if(hasPrefTime(home[i])) {
                                id.style.backgroundColor=COL_NONPREFTIME;
                        } else {
                                id.style.backgroundColor=COL_NONE;
                        }
                        othergametime[home[i]]=game[i];
                        othergametimei[home[i]]="H_"+i;
                        cnt[home[i]]=cnt[home[i]]+1;
                        for(var o in teamsset) {
                                for(var c=0;c<maxsame;c++) {
                                        if(otherteam[c+"-"+home[i]]==o) {
                                                foundrel=true;
                                                foundgood=false;
                                                var p=gameprev[i];
                                                if(p) {
                                                        foundgood=foundgood || sameteam[home[p]+"-"+home[i]] || sameteam[away[p]+"-"+home[i]];
                                                        p=gameprev[p];
                                                        if(p) {
                                                                foundgood=foundgood || home[p]==home[i] || away[p]==home[i];
                                                        }
                                                }
                                                p=gamenext[i];
                                                if(p) {
                                                        foundgood=foundgood || sameteam[home[p]+"-"+home[i]] || sameteam[away[p]+"-"+home[i]];
                                                        p=gamenext[p];
                                                        if(p) {
                                                                foundgood=foundgood || home[p]==home[i] || away[p]==home[i];
                                                        }
                                                }
                                                id=document.getElementById(teamsset[o]);
                                                if(sameteam[home[i]+"-"+away[i]]) {
                                                        foundgood=true;
                                                }
                                                foundbadrel=!foundgood;
                                                if(id.style.backgroundColor==COL_UNRELATED_GAME) {
                                                        //alert("H1: foundgood: "+foundgood+"  " +foundbadrel);
                                                        id.style.backgroundColor=foundbadrel?COL_TOOCLOSE:COL_NONE;
                                                        //alert("H2: foundgood: "+foundgood+"  " +foundbadrel);
                                                }
                                        }
                                }
                        }
                }
                id=document.getElementById("A_"+i);
                if(id) {
                        if(away[i] == t) {
                                id.style.backgroundColor=COL_SELECT;
                        } else if(sameteam[t+"-"+away[i]]==1) {
                                id.style.backgroundColor=COL_RELATE;
                        } else if(cnt[away[i]]>=2) {
                                id.style.backgroundColor=COL_BADTEAM;
                        } else if(othergametime[away[i]] && abs(othergametime[away[i]]-game[i])!=2*55) {
                                id.style.backgroundColor=COL_TOOCLOSE;
                                document.getElementById(othergametimei[away[i]]).style.backgroundColor=COL_TOOCLOSE;
                        } else if(to[away[i]]) {
                                id.style.backgroundColor=COL_RELATE_OTHER;
                        } else if(isByeTime(away[i],game[i])) {
                                id.style.backgroundColor=COL_BADTEAM;
                        } else if(isPrefTime(away[i],game[i])) {
                                id.style.backgroundColor=COL_PREFTIME;
                        } else if(hasPrefTime(away[i])) {
                                id.style.backgroundColor=COL_NONPREFTIME;
                        } else {
                                id.style.backgroundColor=COL_NONE;
                        }
                        cnt[away[i]]=cnt[away[i]]+1;
                        othergametime[away[i]]=game[i];
                        othergametimei[away[i]]="A_"+i;
                        for(var o in teamsset) {
                                for(var c=0;c<maxsame;c++) {
                                        if(otherteam[c+"-"+away[i]]==o) {
                                                foundrel=true;
                                                foundgood=false;
                                                var p=gameprev[i];
                                                if(p) {
                                                        foundgood=foundgood || sameteam[home[p]+"-"+away[i]] || sameteam[away[p]+"-"+away[i]];
                                                        p=gameprev[p];
                                                        if(p) {
                                                                foundgood=foundgood || home[p]==away[i] || away[p]==away[i];
                                                        }
                                                }
                                                p=gamenext[i];
                                                if(p) {
                                                        foundgood=foundgood || sameteam[home[p]+"-"+away[i]] || sameteam[away[p]+"-"+away[i]];
                                                        p=gamenext[p];
                                                        if(p) {
                                                                foundgood=foundgood || home[p]==away[i] || away[p]==away[i];
                                                        }
                                                }
                                                if(sameteam[home[i]+"-"+away[i]]) {
                                                        foundgood=true;
                                                }
                                                id=document.getElementById(teamsset[o]);
                                                foundbadrel=foundbadrel || !foundgood;
                                                if(id.style.backgroundColor==COL_UNRELATED_GAME||id.style.backgroundColor==COL_NONE) {
                                                        //alert("A1: foundgood: "+foundgood+"  " +foundbadrel);
                                                        id.style.backgroundColor=foundbadrel?COL_TOOCLOSE:COL_NONE;
                                                        //alert("A2: foundgood: "+foundgood+"  " +foundbadrel);
                                                }
                                        }
                                }
                        }

                }
                id=document.getElementById("T_"+i);
                if(id) {
                        if(age[home[i]] != age[away[i]] || (home[i]==away[i] && home[i].length>1)) {
                                id.style.backgroundColor=COL_BADTEAM;
                        } else if(isByeTime(t,game[i])) {
                                //alert (game[i]+" : "+bye1_start[t]+" - "+bye1_end[t]+"; "+bye2_start[t]+" - "+bye2_end[t]);
                                id.style.backgroundColor=COL_BADTIME;
                        } else if(isPrefTime(t,game[i])) {
                                //alert (game[i]+" : "+bye1_start[t]+" - "+bye1_end[t]+"; "+bye2_start[t]+" - "+bye2_end[t]);
                                id.style.backgroundColor=COL_GOODTIME;
                        } else if(isByeTime(home[i],game[i]) || isByeTime(away[i],game[i])) {
                                id.style.backgroundColor=COL_BADTEAM;
                        } else if(home[i]=='x' || away[i]=='x' || home[i]=='X' || away[i]=='X') {
                                id.style.backgroundColor=COL_EMPTY;
                        } else {
                                //alert("A foundrel="+foundrel+"  foundbadrel="+foundbadrel);
                                id.style.backgroundColor=foundrel?(foundbadrel?COL_TOOCLOSE:COL_NONE):COL_UNRELATED_GAME;
                                //alert("B foundrel="+foundrel+"  foundbadrel="+foundbadrel);
                        }
                }
                gm[home[i]+"-"+away[i]]+="*";
                gm[away[i]+"-"+home[i]]+="*";
                teamsset[home[i]]="T_"+i;
                teamsset[away[i]]="T_"+i;
        }
        for(var i in team) {
                id=document.getElementById("S_"+i);
                if(id) {
                        if(i==t) {
                                id.style.backgroundColor=COL_SELECT;
                        } else if(sameteam[t+"-"+i]) {
                                id.style.backgroundColor=COL_RELATE;
                        } else if(bye1_start[i]<=firstgame && bye1_end[i]>=lastgame ||
                                  bye2_start[i]<=firstgame && bye2_end[i]>=lastgame ||
                                  bye4_start[i]<=firstgame && bye3_end[i]>=lastgame ||
                                  bye4_start[i]<=firstgame && bye4_end[i]>=lastgame) {
                                id.style.backgroundColor=COL_BADTEAM;
                        } else if(bye1_start[i]<=lastgame && bye1_start[i]>=firstgame ||
                                  bye1_end[i]<=lastgame && bye1_end[i]>=firstgame ||
                                  bye2_start[i]<=lastgame && bye2_start[i]>=firstgame ||
                                  bye2_end[i]<=lastgame && bye2_end[i]>=firstgame ||
                                  bye3_start[i]<=lastgame && bye3_start[i]>=firstgame ||
                                  bye3_end[i]<=lastgame && bye3_end[i]>=firstgame ||
                                  bye4_start[i]<=lastgame && bye4_start[i]>=firstgame ||
                                  bye4_end[i]<=lastgame && bye4_end[i]>=firstgame
                                  ) {
                                id.style.backgroundColor=COL_BADTIME;
                        } else {
                                id.style.backgroundColor=COL_NONE;
                        }
                }
                id=document.getElementById("TOT_"+i);
                if(id) {
                        if(!cnt[i]) cnt[i]=0;
                        id.innerHTML=cnt[i];
                        switch(cnt[i]) {
                        case 0: id.style.backgroundColor=COL_MISS;break;
                        case 1: id.style.backgroundColor=COL_NONE;break;
                        case 2: id.style.backgroundColor=COL_LOTS;break;
                        default: id.style.backgroundColor=COL_BADTEAM;break;
                        }
                }

        }

        if(dogames) {
                for(var i in team) {
                        for(var j in team) {
                                if(age[i]==age[j]) {
                                        id=document.getElementById("X_"+i+"-"+j);
                                        if(id) {
                                                if(gm[i+"-"+j].length==0) {
                                                        id.innerHTML="<a href='#' onclick='sched_addgame(\""+i+"\",\""+j+"\");return false;'>O</a>";
                                                } else {
                                                        id.innerHTML="<a href='#' onclick='sched_delgame(\""+i+"\",\""+j+"\");return false;'>"+gm[i+"-"+j]+"</a>";
                                                }
                                        }
                                }
                        }
                }
        } else {
                for(var i in gm) {
                        id=document.getElementById("X_"+i);
                        if(id) {
                                id.innerHTML=gm[i];
                                if(gm[i].length>1) 
                                        id.style.backgroundColor=COL_BADTEAM;
                        }
                }
        }
        cteam=t;
        document.getElementById('current').innerHTML=team[t];
        document.getElementById('current').style.backgroundColor=COL_SELECT;


}

var undo_t=new Array();
var undo_id=new Array();
var undo_team=new Array();
var redo_t=new Array();
var redo_id=new Array();
var redo_team=new Array();

function sched_undo() {
        if(!swapping) {
                var id=undo_id.shift();
                var t=undo_t.shift();
                var team=undo_team.shift();
                if(!id) return;
                //alert(team);
                var c=cteam;
                cteam=team;
                sched_set(t,id);
                redo_id.unshift(undo_id.shift());
                redo_t.unshift(undo_t.shift());
                redo_team.unshift(undo_team.shift());
                sched_select(c);
        }
}

function sched_redo() {
        if(!swapping) {
                var id=redo_id.shift();
                var t=redo_t.shift();
                var team=redo_team.shift();
                if(!id) return;
                //alert(team);
                var c=cteam;
                cteam=team;
                sched_set(t,id);
                sched_select(c);
        }
}
function sched_set(t,id) {
        var pteam;
        if(swapping) {
                d=document.getElementById("T_"+id);
                if(swapid1==id) {
                        swapid1="";
                        d.style.backgroundColor='white';
                } else if(swapid1=="") {
                        swapid1=id;
                        d.style.backgroundColor='gray';
                } else {
                        swapping=0
                        swapid2=id;

                        var th1=home[swapid1];
                        var th2=home[swapid2];
                        sched_select(th1);
                        sched_set("H",swapid2);
                        sched_select(th2);
                        sched_set("H",swapid1);

                        var ta1=away[swapid1];
                        var ta2=away[swapid2];
                        sched_select(ta1);
                        sched_set("A",swapid2);
                        sched_select(ta2);
                        sched_set("A",swapid1);
                        swapping=1;
                        d=document.getElementById("T_"+swapid1);
                        d.style.backgroundColor='white';
                        sched_select("X");
                        swapid1="";
                        swapid2="";
                }
                return false;
        }
        var i=t+"_"+id;
        undo_t.unshift(t);
        undo_id.unshift(id);
        if(t == 'H') {
                pteam=home[id];
                home[id]=cteam;
                oteam=away[id];
                //document.getElementById('password').value='test';
                document.getElementById('IH_'+id).value=cteam;
                //alert(document.getElementById('IH_'+id));
        } else {
                pteam=away[id];
                away[id]=cteam;
                oteam=home[id];
                document.getElementById('IA_'+id).value=cteam;
        }
        undo_team.unshift(pteam);
        str="<a target=team href='#' title='"+titles[cteam]+"' onclick='sched_select(\""+cteam+"\");return false;'><small>"+team[cteam]+"</small></a>";
        document.getElementById(i).innerHTML=str;
        //alert(str);
        //alert("cteam="+cteam+"; pteam="+pteam+"; "+str);
                
        //gamescount[pteam]--;
        //gamescount[cteam]++;
        //i=document.getElementById("TOT_"+pteam);
        //if(i) i.innerHTML=gamescount[pteam];
        //i=document.getElementById("TOT_"+cteam);
        //if(i) i.innerHTML=gamescount[cteam];
        sched_select(cteam);
}

