// JavaScript Document
var t;
var selected_item;
var btn_clicked = false;
var curr_menu;
var over_btn = false;	// Is the mouse hovering over a button on top of a button?
var current_cardbox = null;
var teamlist_view = "active";
var team_viewmode = "schedule";
var reg_view = "login";
var sel_callback = null;
var team_sched_id = false;

var Validate = {
	form_id: null,
	submit_callback: null,
	btn_text: null,
	run: function(form_id,page_name,submit_callback) {
		this.form_id = form_id;
		if (submit_callback !== undefined) {
			this.submit_callback = submit_callback;
		}
		this.btn_text = $(form_id).B1.value;
		$(form_id).B1.disabled = true;
		$(form_id).B1.value = "Hang on...";
		var params = Form.serializeElements($(this.form_id).getElements(),true);
		params.page = page_name;
		params.ajax_request = 1;
		params.request_type = "validation";
		var aj = new Ajax.Request('/index.php',{
			method: 'post',
			parameters: params,
			onSuccess: function(transport) {
				var resp = transport.responseText;
				if (resp == '+OK') {
					if (Validate.submit_callback !== null) {
						Validate.submit_callback();
					}
					else {
						$(Validate.form_id).submit();
					}
				}
				else {
					alert(resp);
					$(Validate.form_id).B1.disabled = false;
					$(Validate.form_id).B1.value = Validate.btn_text;
				}
			}
		});
		return false;
	}
};

function check_team_tabs() {
	if (location.hash && location.hash.match(/#tab_/)) {
		var sel_tab = location.hash.substring(5);	// # plus 'tab_'
		toggle_teamview(sel_tab);
	}
}

// Submit contact form via AJAX:
function submit_contact() {
	var form_el = $('Feedback_Form');
	var params = Form.serializeElements(form_el.getElements(),true);
	params.page = 'contact';
	params.ajax_request = 1;
	params.request_type = 'process_input';
	show_throbber("contact_tr");
	var aj = new Ajax.Updater('content_div','/index.php',{
		parameters: params,
		evalScripts: true
	});
}

function winOpen(addy,w,h) {
	LeftPosition = (screen.width) ? (screen.width - w)/2 : 0;
	TopPosition = (screen.height) ? ((screen.height - h)/2)-50 : 0;
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+",status=yes,scrollbars=yes";
	mwin = window.open(addy,'win1',settings);
}

function check_searchbox(form,element,text) {
	searchbox = document.forms[form].elements[element];
	if (searchbox.value == "") {
		searchbox.value = text;
	}
	else if (searchbox.value == text) {
		searchbox.value = "";
	}
}

function write_mailto(domain,user,display_name) {
	if (display_name == "") {
		display_name = user+"@"+domain;
	}
	document.write('<a href="mailto:'+user+'@'+domain+'">'+display_name+'</a>');
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 7;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string\'s characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn\'t whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function check_phone_num(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
// Write content into any div:
function write_content(elid,str) {
	Element.update(elid,str);
}
// Roll div on or off screen:
function roll_div(div_id,direction,oncomplete) {
	if (direction == "down") {
		window.scroll(0,0);
		show_div("content_div_container");
		if (oncomplete != undefined) {
			var completion_function = oncomplete;
		} else {
			var completion_function = function() {};
		}
		Effect.Appear("cover_div",{from: 0,to: 0.9,duration: 0.5,afterFinish: function(obj) {
			roll_in(completion_function);
		}});
	}
	else if (direction == "up") {
		Effect.SlideUp("content_div",{duration: 0.5,afterFinish: cover_fade});
	}
}
function roll_in(completion_function) {
	Effect.SlideDown("content_div",{duration: 0.5,afterFinish: completion_function});
}
function cover_fade(obj) {
	write_content(obj.element.id,'');
	Effect.Fade("cover_div",{duration: 0.4,afterFinish: hide_content});
}
function hide_content(obj) {
	hide_div("content_div_container");
}
// Close a rolled out div popup
function close_me() {
	clearTimeout(t);
	if (document.Feedback_Form) {
		document.Feedback_Form.reset();
	}
	else if (document.admin_user_editor) {
		document.admin_user_editor.reset();
	}
	else if (document.admin_team_editor) {
		document.admin_team_editor.reset();
	}
	else if (document.game_editor) {
		document.game_editor.reset();
	}
	else if (document.news_editor) {
		document.news_editor.reset();
	}
	selected_item = "";
	roll_div("content_div","up");
}
// Show/hide throbber animation (to let user know when AJAX div is loading):
function show_throbber() {
	a = show_throbber.arguments;
	if (a.length > 0) {
		throbber_name = a[0];
	}
	else {
		throbber_name = "throbber";
	}
	if (document.images[throbber_name]) {
		document.images[throbber_name].src = "/images/"+throbber_name+"_on.gif";
	}
}

function hide_throbber() {
	a = hide_throbber.arguments;
	if (a.length > 0) {
		throbber_name = a[0];
	}
	else {
		throbber_name = "throbber";
	}
	if (document.images[throbber_name]) {
		document.images[throbber_name].src = "/images/"+throbber_name+"_off.gif";
	}
}
function prev_month(prev_date,game_team_id) {
	show_throbber('cal_div_tr');
	var aj = new Ajax.Updater('cal_div','/index.php',{
		method: 'get',
		parameters: {
			page: 'schedule',
			ajax_request: 1,
			request_type: 'update_page',
			cal_date: prev_date,
			show_cal: 1,
			game_team_id: game_team_id
		},
		evalScripts: true
	});
}
function next_month(next_date,game_team_id) {
	show_throbber('cal_div_tr');
	var aj = new Ajax.Updater('cal_div','/index.php',{
		method: 'get',
		parameters: {
			page: 'schedule',
			ajax_request: 1,
			request_type: 'update_page',
			cal_date: next_date,
			show_cal: 1,
			game_team_id: game_team_id
		},
		evalScripts: true
	});
}
function refresh_cal(cal_date,game_team_id) {
	show_throbber('cal_div_tr');
	var aj = new Ajax.Updater('cal_div','/index.php',{
		method: 'get',
		parameters: {
			page: 'game_selector',
			ajax_request: 1,
			request_type: 'update_page',
			show_cal: 1,
			cal_date: cal_date,
			game_team_id: game_team_id
		},
		evalScripts: true
	});
}
$Calendar.week_select = function(week_date) {
	// Update the Game list:
	show_throbber('game_list_tr');
	var aj = new Ajax.Updater('game_list','/index.php',{
		method: 'get',
		parameters: {
			page: 'schedule',
			ajax_request: 1,
			request_type: 'update_page',
			show_games: 1,
			game_week: week_date,
			game_team_id: team_sched_id
		},
		evalScripts: true
	});
}
function show_cards(game_id,team_id,team_type) {
	if (current_cardbox !== null) {
		Effect.Fade(current_cardbox,{duration: 0.4,afterFinish: clear_cardbox});
	}
	div_id = "addcard_div"+game_id+team_id;
	show_throbber('game_list_tr');
	current_cardbox = div_id;
	var aj = new Ajax.Updater(div_id,'/index.php',{
		method: 'get',
		parameters: {
			page: 'schedule',
			ajax_request: 1,
			request_type: 'update_page',
			show_gamecards: 1,
			cardteam_id: team_id,
			cardgame_id: game_id,
			team_type: team_type
		},
		evalScripts: true,
		onSuccess: function() {
			hide_throbber('game_list_tr');
			Effect.Appear(div_id,{duration: 0.4});
		}
	});
}
function close_cardbox(game_id,team_id) {
	div_id = "addcard_div"+game_id+team_id;
	Effect.Fade(div_id,{duration: 0.4,afterFinish: clear_cardbox});
	current_cardbox = null;
}
function clear_cardbox(obj) {
	write_content(obj.element.id,'');
	obj.element.hide();
}
function view_regs() {
	show_throbber();
	var aj = new Ajax.Updater('content_div','/index.php',{
		method: 'get',
		parameters: {
			page: 'regulations',
			ajax_request: 1,
			request_type: 'popup'
		},
		onSuccess: function() {
			hide_throbber();
			roll_div('content_div','down');
		},
		evalScripts: true
	});
}
function view_terms() {
	show_throbber();
	var aj = new Ajax.Updater('content_div','/index.php',{
		method: 'get',
		parameters: {
			page: 'terms_of_use',
			ajax_request: 1,
			request_type: 'popup'
		},
		onSuccess: function() {
			hide_throbber();
			roll_div('content_div','down');
		},
		evalScripts: true
	});
}
function show_div(div_id) {
	$(div_id).show();
}
function hide_div(div_id) {
	$(div_id).hide();
}
function toggle_teamlist(whichone) {
	if (whichone != teamlist_view) {
		if (whichone == "active") {
			$("teamlist_inactive").hide();
			$("teamlist_active").show();
			$('inactive_list_tab').className = "rowselect";
		}
		else if (whichone == "inactive") {
			$("teamlist_active").hide();
			$("teamlist_inactive").show();
			$('active_list_tab').className = "rowselect";
		}
		// Remember the users chaice in the session:
		var aj = new Ajax.Request('/index.php',{
			method: 'get',
			parameters: {
				page: 'teams',
				ajax_request: 1,
				request_type: 'server_action',
				display_list: whichone
			},
			evalScripts: true
		});
		teamlist_view = whichone;
	}
}
function toggle_regview(whichone) {
	if (whichone != reg_view) {
		if (whichone == "login") {
			$("registration_div").hide();
			$("login_div").show();
			$('registration_tab').className = "rowselect";
		}
		else if (whichone == "registration") {
			$("login_div").hide();
			$("registration_div").show();
			$('login_tab').className = "rowselect";
		}
		reg_view = whichone;
	}
}
function toggle_teamview(whichview) {
	if (whichview != team_viewmode) {
		if (whichview == "teaminfo") {
			$("team_schedule").hide();
			$("profile_roster").show();
			$('schedule_tab').className = "rowselect";
			$('teaminfo_tab').className = "rowselect_hover";
		}
		else if (whichview == "schedule") {
			$("profile_roster").hide();
			$("team_schedule").show();
			$('schedule_tab').className = "rowselect_hover";
			$('teaminfo_tab').className = "rowselect";
		}
		team_viewmode = whichview;
		top.location.href = "#tab_"+whichview;
	}
}
function val_radio(group) {
	selected = false;
	for (i=0;i < group.length;i++) {
		if (group[i].checked == true) {
			selected = true;
			break;
		}
	}
	return selected;
}
function val_radios(radio_group) {
	return ($$('input[name="'+radio_group+'"]:checked').length > 0);
}
function val_select(selector) {
	return (selector.selectedIndex != 0)
}
// Functions for selecting an item.  These are needed to control the selection of buttons underneath buttons
function select_item(item_id,id_label,page_name) {
	a = parseInt(item_id);
	item_id = ((a != 0) ? "'"+item_id+"'" : item_id);
	// Wait for a moment before actually selecting the item in case a button over top of it was clicked:
	setTimeout("do_select("+item_id+",'"+id_label+"','"+page_name+"');",250);
}
function do_select(item_id,id_label,page_name) {
	if (btn_clicked == false) {
		// If a button over top was not clicked, perform the selection action
		selected_item = item_id;
		show_throbber();
		var aj = new Ajax.Updater('content_div','/index.php?'+id_label+'='+item_id,{
			method: 'get',
			parameters: {
				page: page_name,
				ajax_request: 1,
				request_type: 'popup',
				ref_page: page_name
			},
			evalScripts: true,
			onSuccess: function() {
				hide_throbber();
				roll_div('content_div','down');
			}
		});
	}
	else {
		btn_clicked = false;
	}
}
// Special selection scripts for teams page:
function select_team(team_id) {
	t = setTimeout("do_teamsel("+team_id+")",250);
}
function do_teamsel(team_id) {
	if (btn_clicked == false) {
		document.location.href = "/teams/team"+team_id+".html";
	}
	else {
		btn_clicked = false;
	}
}
function close_msg_box(id) {
	if (id === undefined) {
		var id = '';
	}
	Effect.SlideUp("usr_msg"+id,{duration: 0.1,afterFinish: clear_msg_box});
}
function clear_msg_box(obj) {
	write_content(obj.element.id,'');
}
function show_exec(photo,name) {
	var aj = new Ajax.Updater('content_div','/index.php',{
		method: 'get',
		parameters: {
			page: 'executive',
			ajax_request: 1,
			request_type: 'popup',
			show_image: 1,
			image_file: '/images/photos/'+photo,
			title_caption: name
		},
		onSuccess: function() {
			roll_div('content_div','down');
		},
		evalScripts: true
	});
}

function toggle_reg_or_profile_fields() {
	// Toggle applicable fields based on user type selection
	if (document.admin_user_editor) {
		form_el = document.admin_user_editor;
	} else if (document.registration_form) {
		form_el = document.registration_form;
	}
	if (!form_el.is_player.checked) {
		if ((form_el.is_exec.type == "hidden" && form_el.is_exec.value == 0) || (form_el.is_exec.type == 'checkbox' && !form_el.is_exec.checked)) {
			form_el.is_player.checked = true;
		}
	}
	if (form_el.is_capt.checked && !form_el.is_player.checked) {
		form_el.is_player.checked = true;
	}
	if (form_el.is_player.checked) {
		$('player_fields').show();
	}
	else {
		$('player_fields').hide();
	}
	if (form_el.is_exec) {
		if (form_el.is_exec.type == "checkbox" && form_el.is_exec.checked) {
			// Make sure photo can be uploaded:
			$('photo').disabled = false;
			$('exec_fields').show();
		}
		else if (form_el.is_exec.type == "checkbox" && !form_el.is_exec.checked) {
			// Make sure photo does not get uploaded:
			$('photo').disabled = true;
			$("exec_fields").hide();
		}
	}
}
