var alert_height;
var alert_id;

function popup(url,name,w,h)
{
    newWindow = window.open (url, name, 'scrollbars=1,width='+w+',height='+h, true);
    newWindow.focus();
}

function swap_divs(on, off) {
    document.getElementById(off).style.display = 'none';
    document.getElementById(on).style.display = 'block';
}

function alert_fade(id) {
    document.getElementById(id).style.backgroundColor = 'ff0';
    alert_height = document.getElementById(id).offsetHeight;
    setTimeout('fade_alert()', 2000);
    alert_id = id;
}

function fade_alert() {
    alert_height = alert_height - 1;
    document.getElementById(alert_id).style.height = alert_height;
    if (alert_height > 0) {
	setTimeout('fade_alert()', 1);
    } else {
	document.getElementById(alert_id).style.display = 'none';
    }
}

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var menu_http = createRequestObject();

function update_draft_status() {
    var chat_on = document.getElementById('chat_on');
    if (chat_on) {
	chat_on = 1;
    } else {
	chat_on = 0;
    }
    menu_http.abort();
    menu_http.open('get', 'draft_status.php?chat_on='+chat_on, true);
    menu_http.onreadystatechange = status_response;
    menu_http.send(null);    
}

function status_response() {
    if (menu_http.readyState == 4) {
	var data = menu_http.responseText.split('<><>');
	document.getElementById('draft_status').innerHTML = data[0];
	setTimeout("update_draft_status()", 5000);

	// Play a sound?
	if (data[1] == '1') {
	    soundManager.play('ring');
	} else if (data[1] == '2') {
	    soundManager.play('click');
	} else if (data[1] == '3') {
	    soundManager.play('bell');
	}
    }
}

var comments_http = createRequestObject();
var current_comment;

function edit_comments(id) {
    document.getElementById('player_comments_text_'+id).style.display = 'none';
    document.getElementById('player_comments_text_edit_'+id).style.display = 'block';
}
function cancel_comments(id) {
    document.getElementById('player_comments_text_'+id).style.display = 'block';
    document.getElementById('player_comments_text_edit_'+id).style.display = 'none';
}
function save_comments(id) {
    if (comments_http.readyState == 0 || comments_http.readyState == 4) {
	document.getElementById('player_comments_progress_'+id).style.display = 'block';
	document.getElementById('player_comments_text_edit_'+id).style.display = 'none';

	var data = 'player_comments_text='+document.getElementById('player_comments_text_edit_box_'+id).value;
	data += '&player_id='+id;
	comments_http.open('post', 'save_comments.php', true);
	comments_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	comments_http.onreadystatechange = update_player_comments;
	comments_http.send(data);
	current_comment = id;
    } else {
	alert(comments_http.readyState);
    }
}
function update_player_comments() {
    if (comments_http.readyState == 4) {
	document.getElementById('player_comments_'+current_comment).innerHTML = comments_http.responseText;
	document.getElementById('player_comments_text_'+current_comment).style.display = 'block';
	document.getElementById('player_comments_progress_'+current_comment).style.display = 'none';
    }
}

var column_http = createRequestObject();

function select_column(id) {
    column_http.abort();
    column_http.open('get', 'select_column.php?column_id='+id, true);
    column_http.onreadystatechange = update_column_options;
    column_http.send(null);
}
function deselect_column(id) {
    column_http.abort();
    column_http.open('get', 'deselect_column.php?column_id='+id, true);
    column_http.onreadystatechange = update_column_options;
    column_http.send(null);
}
function update_column_options() {
    if (column_http.readyState == 4) {
	document.getElementById('column_detail').innerHTML = column_http.responseText;
    }
}

bpa_http = createRequestObject();

function bpa_pos_change() {
    bpa_http.abort();
    bpa_http.open('get', 'bpa_list.php?position_id='+document.getElementById('position_id').value, true);
    bpa_http.onreadystatechange = update_bpa_options;
    bpa_http.send(null);
}
function update_bpa_options() {
    if (bpa_http.readyState == 4) {
	document.getElementById('bpa_select').innerHTML = bpa_http.responseText;
    }
}

function private_chat(team_id) {
    popup('private_chat.php?team_id='+team_id, '_blank', 600, 450);
}
