
var gamewin;
var altgamewin;
var javaOk = true;
var javaTested = false;
var javaVersion = 0;

function gotJava(version) {
  javaOk = true;
  javaTested = true;
  javaVersion = parseInt(version);
}

setTimeout('if (!javaTested) javaOk=false;', 5000);

function popupGame(game, force)
{

    if ((gamewin != null) && (!gamewin.closed)) {
        gamewin.focus();
        return;
    }

    // disabled while our testapplet fails to build, i have no idea what's wrong or
    // where to look from the publish-log-errors.
    //if (!force && !javaOk && navigator.appVersion.lastIndexOf("Mac")==-1) {
    //    gamewin = window.open("/nojava.html?"+game, "Java", "resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=yes,width=400,height=200");
    //    return;
    //}
    gamewin = window.open('/symfoni/startspil.php?spil='+game,'gamewindow','resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=yes,width=770,height=550');
}

function popupAltGame(url,width,height)
{
    if ((altgamewin != null) && (!altgamewin.closed)) {
        altgamewin.focus();
        return;
    }
    altgamewin = window.open(url,'altgame','resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=yes,width='+width+',height='+height);
}

function buyCards() {
        var win = window.open('http://betaling.tv2.dk/adresse.php?key=spil365/','paywindow', 'toolbars=no, status=yes, scrollbars=yes, width=511, height=520');
}

function hideFromProfile(user, status)
{
    var url = '/profil/venneliste/?u='+user+'&status='+status;
    var profilewin = window.open(url,'','toolbars=no,scrollbars=yes,height=500,width=476');
}

function buySpil()
{
    var o = document.payform.product;
	var checked = false;
	var value = '';
    for (var i=0; i<o.length && !checked; i++) {
		checked = o[i].checked;
		value = o[i].value;
	}
	if (!checked && !o.checked) {
		alert('Du skal vælge et produkt'); 
	} else {
        if(!checked)
            value = o.value;
   		var win = window.open('http://betaling.tv2.dk/betal/'+value+'/',
                              'paywindow',
                              'toolbars=no, status=yes, scrollbars=yes, width=511, height=520');
	}
}

function setTheme(id, init) {
    curTheme = document.getElementById('theme').value;
    // if no theme is selected, don't try to mark one (by adding 'filter')
    if(curTheme != '') {
        document.getElementById(curTheme).className = 'theme filter';
    }
    if(curTheme == id && init != 1) {
        // if the theme selected is selected again, unselect it
        // unless we are initializing the page, then it is ok to select allready selected theme.
        document.getElementById(id).className = 'theme filter';
        document.getElementById('theme').value = '';
    } else {
        // remove filter from selected theme
        document.getElementById(id).className = 'theme';
        // set theme value in hidden form input field
        document.getElementById('theme').value = id;
    }
}

function popVerification() {
    var url = 'http://login.tv2.dk/mobilverificering/popup.php';
    var wtitle = 'mobilverificering'
    var wspecs = 'top=150,left=150,width=300,height=200,scroll=no';
    window.open(url, wtitle, wspecs);
}

function buyMobileAccess() {
    url = 'http://betaling.tv2.dk/betal/mobilspil30/';
    var win = window.open(url, 'paywindow', 'toolbars=no, status=yes, scrollbars=yes, width=511, height=520');
}

function manuelTournament(game, tid, room) {
    url = 'tree.php?game='+game+'&tid='+tid+'&room='+room;
    var win = window.open(url, 'Turneringer', 'toolbars=no, status=yes, scrollbars=yes, width=300, height=520');
}

function buyVip10() {
    var win = window.open('http://betaling.tv2.dk/betal/spil10/','paywindow', 'toolbars=no, status=yes, scrollbars=yes, width=511, height=520');
}

function WPTSignup(game, room, tid, username) {
    var win = window.open('poker.php?game='+game+'&room='+room+'&tid='+tid+'&username='+username+'','poker', 'toolbars=no, status=yes, scrollbars=no, width=425, height=425');
}

function buyVip30() {
    var win = window.open('http://betaling.tv2.dk/betal/spil30/','paywindow', 'toolbars=no, status=yes, scrollbars=yes, width=511, height=520');
}

function avatarEditor() {
    var win = window.open('/avatar/editor.php','avatareditor', 'toolbars=no, status=yes, scrollbars=no, width=425, height=425');
}

function OpenAvatarShop() {
    var win = window.open('/avatar/shop/shop.php', 'avatarshop', 'toolbar=no, status=yes, scrollbars=no, width=490, height=510');
}

function confirmTournament(game, room, tid, type, username, control) {
    // type=1 => tournament1 (Certus system)
    // type=2 => manuel tournament
    if(confirm("Er du sikker på du vil deltage i " + gameName(parseInt(game)) +  " turneringen?")) {
        document.location.href = "open.php?mode=" + type + "&gameid=" + game + "&room=" + room + "&tournamentid=" + tid + "&username=" + username + "&c=" + control;
    }
}

function cancelTournament(game, room, tid, type, username) {
    // type=1 => tournament1 (Certus system)
    // type=2 => manuel tournament
    if(confirm("Er du sikker på du vil afmelde dig " + gameName(parseInt(game)) +  " turneringen?")) {
        document.location.href = "open.php?mode=" + type + "&gameid=" + game + "&room=" + room + "&tournamentid=" + tid + "&username=" + username;
    }
}

function startScribble() {
    var win = window.open('/games/scribble/index.php', 'scribble', 'toolbars=no, status=yes, scrollbars=no, width=800, height=600');
}

function acceptGift(gid) {
    if(confirm("Er du sikker på du vil godkende gaven?")) {
        document.location.href = '/profil/v2/godkendgave.php?cmd=acc&id=' + gid;
    }
}

function declineGift(gid) {
    if(confirm("Er du sikker på du vil afvise gaven?")) {
        document.location.href = '/profil/v2/godkendgave.php?cmd=dec&id=' + gid;
    }
}

function newwindow(url, gamewidth, gameheight) {
    //gamewidth += 165;
    gamewidth += 10;
    gameheight += 111;
    /**
    if(gameheight < 610) {
        gameheight = 610;
    }
    **/
    var popup = window.open(url, 'webgame', 'toolbar=no,location=no,directories=no,status=no,scrollbars=no,menubar=no,titlebar=no,width=' + gamewidth + ',height=' + gameheight);
    popup.focus();
}

function showTree(id) {
    var url    = '/tournament1/showtree.php?tid=' +  id;
    var label  = 'tournamenttree';
    var custom = 'resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,width=800,height=600';
    var win = window.open(url, label, custom);
}

function nyPaaSpil() {
    url = '/nypaaspil/';
    wname = 'nypaaspil';
    windowOptions = 'resizable=no,scrollbars=no,toolbar=no,location=no,status=no,width=800,height=600';
    var pop = window.open(url, wname, windowOptions);
}

function soloGames(id, width, height) {
    if(uservip) {
        var url    = '/solo/startspil.php?gameid=' + id;
        var label  = 'solo';
        var custom = 'resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=yes,width='+width+',height='+height;
        var win    = window.open(url, label, custom);
    } else {
        alert("Dette rum kræver TV2 | Login og VIP-medlemskab.\n Log på til venstre");
    }
}

function popProfile(user_identification, URLappend, width) {
	append = (typeof(URLappend) != 'undefined') ? URLappend : '';
	width = (typeof(width) != 'undefined') ? width : 512;

    // FIXME: ignores URLappend if we're looking for a user id
	if (user_identification.substr(0, 3) == 'id:') {
	    var userid = user_identification.substr(3, user_identification.length);
	    var url = '/profil/v2/proxy.php?id=' + userid;
	} else {
	    var username = user_identification;
	    var url    = '/profil2/' + username + '/' + append;	
	}	
	
    var label  = 'profile';
    var custom = 'resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,width='+width+',height=500';
    var win = window.open(url, label, custom);
}

function showQuiz() {
    url = 'http://quiz.tv2.dk?front=normal';
    wname = 'Quiz';
    windowOptions = 'resizable=no,scrollbars=no,toolbar=no,location=no,status=no,width=800,height=600';
    var pop = window.open(url, wname, windowOptions);

}
