﻿/*
 * Functions to open and hide popup divs
 */
function openPopup(type, header, text, onconfirm) 
{
	if (type == 'div_confirm_delete_team') {
		$('lp_popup_custom').style.display = 'block';
	}
	else {
		if (type == 'confirm') {
			if (onconfirm != undefined) 
				$('lp_popup_confirm').onclick = function(){
					eval(onconfirm);
				};
			$('lp_popup_content').style.borderColor = '#cc0000';
			$('lp_popup_close').style.backgroundColor = "white";
			$('lp_popup_close').style.color = "#3399cc";
			$('lp_popup_close').style.textDecoration = "underline";
			$('lp_popup_close').style.border = "0";
			$('lp_popup_close').style.marginLeft = "20px";
			$('lp_popup_confirm').style.display = '';
		}
		else {
			$('lp_popup_confirm').style.display = 'none';
			$('lp_popup_close').value = 'OK';
		}
		if (header != undefined) 
			$('lp_popup_header').innerHTML = header;
		if (text != undefined) 
			$('lp_popup_text').innerHTML = text;
		$('lp_popup').style.display = 'block';
	}
	fadeInBackground();
}

function getDocHeight() {
	var D = document;
	return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
}
function getDocWidth() {
	var D = document;
	return Math.max(Math.max(D.body.scrollWidth, D.documentElement.scrollWidth), Math.max(D.body.offsetWidth, D.documentElement.offsetWidth), Math.max(D.body.clientWidth, D.documentElement.clientWidth));
}

function closePopup() {
	fadeOutBackground();
    $('lp_popup').style.display = 'none';
}

function setOpacity(value) {
	$('lp_popup_background').style.filter = 'alpha(opacity=' + (value * 10) + ')';
	$('lp_popup_background').style.opacity = value / 10;
	$('lp_popup_background').style.MozOpacity = value / 10;
}

// Fade in the background
// Skip the fading for ie6
function fadeInBackground() {
//	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
//	if (!isIE6) {
		setOpacity(0);
		$('lp_popup_background').style.height = getDocHeight()+'px';
		$('lp_popup_background').style.width = getDocWidth()+'px';
		$('lp_popup_background').style.display = 'block';
		for (var i = 0; i <= 50; i++) {
			setTimeout('setOpacity(' + (i / 10) + ')', 8 * i);
		}
//	}
//	else {
//		setOpacity(5);
//		$('lp_popup_background').style.height = getDocHeight()+'px';
//		$('lp_popup_background').style.display = 'block';
//	}
}

// Fade out the background
// Skip the fading for ie6
function fadeOutBackground() {
//	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
//	if (!isIE6) {
		setTimeout("hideBackground()", 400);
		var id;
		for( var i = 50 ; i <= 100 ; i++ ) {
			setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 8 * (i-50) );
		}
//	}
//	else
//	{
//		hideBackground();
//	}
}

function hideBackground() {
	setOpacity(0);
	$('lp_popup_background').style.display = 'none';
}

/*
 * Functions to show hide elements by id (eg. custom popups)
 */
function showById(nid) 
{
    $(nid).style.display = 'block';	
}

function hideById(nid)
{
    $(nid).style.display = 'none';
}

function hidePlayerCardById(nid)
{
	fadeOutBackground();
	$(nid).style.display = 'none';
	if ($('div_search_players'))
	{
		showById('div_search_players');
		hideById('whitearea');
	}
}

/*
 * Function to hide and show boxes when click image
 */
function hideshowDivByImg(box_div, image_div, imagepath)
{
	
	if ($(box_div).style.display == 'block' || $(box_div).style.display == '')
	{
		  $(box_div).style.display = 'none';
		  $(image_div).src = imagepath + "/toggle-arrow-down.png";
	} 
	else
	{
		  $(box_div).style.display = 'block';
		  $(image_div).src = imagepath + "/toggle-arrow-right.png";
	}	
}

/*
 * Function to limit writing characters in textarea
 */
function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}

/*
 * Inbox recipient field functions
 */
function updateItemList(item) 
{
	var receiver = item.innerHTML;
	var receiverId = item.id;
	if($('receiver' + receiver) == null)
	{
		$('peopleReceiverList').innerHTML = $('peopleReceiverList').innerHTML + '<li class="receiverListItem" id="receiver' + receiver + '" onclick="onClickDeleteItem(\'' + receiver + '\')">' + receiver + '</li>';
		$('hiddenReceiverList').innerHTML = $('hiddenReceiverList').innerHTML + '<input type="hidden" id="hiddenReceiver' + receiver + '" name="receiver[]" value="' + receiverId + '" />';
	}
	$('peopleReceivers').value = '';
}
	
function deleteItem(event)
{
	if(event.keyCode == 8 && $('peopleReceivers').value == '')
	{
		var container = $('peopleReceiverList');
		var hiddenContainer = $('hiddenReceiverList');
		if(container.innerHTML != '')
		{
			var containerElements = container.getElementsByTagName('li');
			var lastElement = containerElements[containerElements.length-1];
			container.removeChild(lastElement);
			if(hiddenContainer.innerHTML != '')
			{
				var hiddenContainerElements = hiddenContainer.getElementByTagName('input');
				var lastHiddenElement = hiddenContainerElements[hiddenContainerElements.length-1];
				hiddenContainer.removeChild(lastElement);	
			}
		}
	}	
}
	
function onClickDeleteItem(receiver)
{
	var receiverElement = $('receiver' + receiver);
	var hiddenReceiverElement = $('hiddenReceiver' + receiver);
		
	receiverElement.parentNode.removeChild(receiverElement);
	hiddenReceiverElement.parentNode.removeChild(hiddenReceiverElement);
}

function countTextLength(text_field_id,count_field_id,max_length)
{
	var text_length=$F(text_field_id).length;
	
	if (text_length > max_length) // if too long...trim it!
		$(text_field_id).value = $F(text_field_id).substring(0, max_length);
	// otherwise, update 'characters left' counter
	else 
		$(count_field_id).innerHTML = max_length - text_length;

}
function countDescriptionLength(description_field_id,show_field_id)
{
	var description_length=$(description_field_id).innerHTML.length;
	$(show_field_id).innerHTML=300-description_length;
	
}

/*
 * Friend invitation functions
 */
function trim(str) 
{  
	if(str.charAt(0) == " ")
	{  
		str = TrimUsingRecursion(str.substring(1));
	}
	if (str.charAt(str.length-1) == " ")
	{  
		str = TrimUsingRecursion(str.substring(0,str.length-1));
	}
	return str;
}

function validate_email(email)
{
	if(email.match(/[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s\'"<>]+\.+[a-z]{2,6}))/))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validate_insert_email(error_empty_email, error_invalid_email)
{
	// get the the email and compare it with the mail list.
	var new_mail = trim($("new_email").value);
	var a="&#228;";
	var o="&#246;";
	if(new_mail.length < 1)
	{
		openPopup('notification', error_empty_email, '');
		return false;
	}
	else
	{
		// check the validity of the email.
		if(!validate_email(new_mail))
		{
			openPopup('notification', error_invalid_email, '');
			return false;
		}
		else
		{
			return true;
		
		}
	}
}

function check_invite_email_exists(error_duplicate_email)
{	var a="&#228;";
	var o="&#246;";
	var new_mail = trim($("new_email").value);
	// get the tbody which contains the list of emails.
	var tbody = $("email_tbody");
	var email_list = new Array();
	var return_value = new Array();
	var last_bgcolor = "#ffffff";
	//alert(tbody.childNodes.length);
	for(i=0; i<tbody.childNodes.length; i++)
	{
		var mail_tr = tbody.childNodes[i];
		if(mail_tr.nodeName.toLowerCase() == "tr")
		{
			//alert(mail.childNodes.length);
			for(j=0; j<mail_tr.childNodes.length; j++)
			{
				var mail_td = mail_tr.childNodes[j];
				if(mail_td.nodeName.toLowerCase() == "td")
				{
					for(k=0; k<mail_td.childNodes.length; k++)
					{
						var mail_label = mail_td.childNodes[k];
						if(mail_label.name == "invited_mails[]")
						{
							email_list.push(mail_label.value);
						}
						if(mail_label.value == new_mail)
						{						
							openPopup('notification', error_duplicate_email, '');
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}

function change_send_invitation_button_status(button_id, tbody_id)
{
	// get the tbody.
	tbody = $(tbody_id);
	
	// set the original status to false.
	btn = 0;
	
	for(i=0; i<tbody.childNodes.length; i++)
	{
		var mail_tr = tbody.childNodes[i];
		if(mail_tr.nodeName.toLowerCase() == "tr")
		{
			// if there is at least one table row, set the status to true.
			btn = 1;
		}
	}
	if(btn==1)
	{
		$(button_id).className = "button";
		$(button_id).disabled = false;
	}
	else
	{
		$(button_id).className = "button_inactive";
		$(button_id).disabled = true;
	}
}

function delete_invite_friends(body_id,tr_id)
{
	// get the tbody and the tr which is going to be deleted.
	var tbody = $(body_id);
	var deleted_tr = $(tr_id);
	//alert(deleted_tr.nodeName);
	tbody.removeChild(deleted_tr);
	reclass_table_odd_even(body_id);
	change_send_invitation_button_status('btn_send', body_id);
	
}
function add_email_to_invite_list(type, error_empty_email, error_invalid_email, error_duplicate_email)
{
	if(validate_insert_email(error_empty_email, error_invalid_email)==true)	
	{
		var new_mail = trim($("new_email").value);
		var tbody = $("email_tbody");

		if (check_invite_email_exists(error_duplicate_email)) return 0;
		// if the email is not in the mail list, add this email to the list.
		
		// create the label of new mail
		var new_label = document.createElement('label');
		new_label.innerHTML = new_mail;
		
		var new_space = document.createElement('label');
		new_space.innerHTML = '&nbsp;';
		
		// the the checkbox.
		var new_check = document.createElement('input');
		new_check.setAttribute("type", "hidden");
		new_check.setAttribute("name", "invited_mails[]");
		new_check.setAttribute("value", new_mail);
		//new_check.setAttribute("style", "display: none");
		//new_check.setAttribute("checked", "checked");
		
		if(type == "invite_all")
		{
		// the delete button delete
			var new_delete = document.createElement('img');
			new_delete.setAttribute("src", "/images/btn-x.png");
			new_delete.setAttribute("class", '"pngFix"');
			new_delete.setAttribute("name", "delete");
			if (new_delete.attachEvent) {
				new_delete.attachEvent ("onclick", function(){ open_delete_buddy_mail_popup(new_mail, new_mail)});
			} else {
				new_delete.setAttribute("onclick", "open_delete_buddy_mail_popup('" + new_mail + "', '" + new_mail + "')");
			}

			var new_td1 = document.createElement('td');
			var new_td2 = document.createElement('td');
			new_td1.setAttribute("align", "center");
			new_td1.setAttribute("width","20px");
			new_td2.setAttribute("align", "left");
			
			new_td1.appendChild(new_delete);
			new_td2.appendChild(new_check);
			new_td2.appendChild(new_label);
			
			var new_tr = document.createElement('tr');
			new_tr.setAttribute("class", "buddy_mail");
			new_tr.setAttribute("id", new_mail);
			new_tr.appendChild(new_td1);
			new_tr.appendChild(new_td2);

			tbody.appendChild(new_tr);

			reorder_table_background("email_tbody", "#e6e6e6", "#ffffff");
	
		}
		else
		{
			var new_delete = document.createElement('img');
			new_delete.setAttribute("src", "/images/delete-x.png");
			new_delete.setAttribute("alt", 'X');
			new_delete.setAttribute("class", '"pngFix"');
			new_delete.style.cursor = 'pointer';
			if (new_delete.attachEvent) {
				new_delete.attachEvent ("onclick", function(){ delete_invite_friends('email_tbody', new_mail)});
			} else {
				new_delete.setAttribute("onclick", "delete_invite_friends('email_tbody', '" + new_mail + "')");
			}
			
			var new_td1 = document.createElement('td');
			new_td1.style.width = "10px";

			var new_td2 = document.createElement('td');
			new_td2.setAttribute("align","left");
			new_td2.appendChild(new_label);

			var new_td3 = document.createElement('td');
			new_td3.setAttribute("align","right");
			new_td3.appendChild(new_check);
			new_td3.appendChild(new_delete);

			var new_tr = document.createElement('tr');
			new_tr.setAttribute("id", new_mail);
			new_tr.appendChild(new_td1);
			new_tr.appendChild(new_td2);
			new_tr.appendChild(new_td3);

			tbody.appendChild(new_tr);
	
			reclass_table_odd_even("email_tbody");
		}
				
		change_send_invitation_button_status('btn_send', 'email_tbody');
		
		// after add the new mail, delete the one in the input box.
		$('new_email').value = "";		
	}
}

/**
 * Re classes the table tr's when table is messed up
 * @param String tbody_id
 */
function reclass_table_odd_even(tbody_id)
{
	tbody = $(tbody_id);
	
	attribute = 'odd';
	
	for(i=0; i<tbody.childNodes.length; i++)
	{
		var mail_tr = tbody.childNodes[i];
		if(mail_tr.nodeName.toLowerCase() == "tr")
		{	
			mail_tr.className = attribute;
			if(attribute == 'odd')
			{
				attribute = 'even';
			}
			else
			{
				attribute = 'odd';
			}
		}
	}
}

/*
	define the sorting order here.
*/
var order_m = "ASC";
var order_p = "ASC";
var order_h = "ASC";

/*
	define the sort default value.
*/
/*var sort_by_m = "smliiga_team ASC,name";
var sort_by_p = "smliiga_team ASC,name";
var sort_by_h = "smliiga_team ASC,name";*/
var sort_by_m = "";
var sort_by_p = "";
var sort_by_h = "";

function player_sort_m(url, div_id, page_number, type, new_sort_by_m)
{
	update_search_player_result(url, div_id, page_number, type, new_sort_by_m, "no_change", "no_change");
}

function player_sort_p(url, div_id, page_number, type, new_sort_by_p)
{
	update_search_player_result(url, div_id, page_number, type, "no_change", new_sort_by_p, "no_change");
}

function player_sort_h(url, div_id, page_number, type, new_sort_by_h)
{
	update_search_player_result(url, div_id, page_number, type, "no_change", "no_change", new_sort_by_h);
}

// when the user has change the criteria of the search player select box, this function is called to update the search result.
function update_search_player_result(url, div_id, page_number, type, new_sort_by_m, new_sort_by_p, new_sort_by_h)
{
	// get the  value of the select boxes player position, player team, and player value.
	var position = $("player_position").value;
	var team = $("player_team").value;
	var value = $("player_value").value;

	if(new_sort_by_m == "no_change")
	{
		new_sort_by_m = sort_by_m;
	}
	else if(new_sort_by_m == null)
	{
		new_sort_by_m = sort_by_m;
		order_m = "DESC";
	}
	else
	{
		new_sort_by_m = get_sort_by(new_sort_by_m);
		order_m = get_order(order_m, new_sort_by_m, sort_by_m);
	}
	
	if(new_sort_by_p == "no_change")
	{
		new_sort_by_p = sort_by_p;
	}
	else if(new_sort_by_p == null)
	{
		// when the page load for the first time
		new_sort_by_p = sort_by_p;
		order_p = "DESC";	
	}
	else
	{
		new_sort_by_p = get_sort_by(new_sort_by_p);
		order_p = get_order(order_p, new_sort_by_p, sort_by_p);	
	}
	
	if(new_sort_by_h == "no_change")
	{
		new_sort_by_h = sort_by_h;
	}
	else if(new_sort_by_h == null)
	{
		new_sort_by_h = sort_by_h;
		order_h = "DESC";	
	}
	else
	{
		new_sort_by_h = get_sort_by(new_sort_by_h);
		order_h = get_order(order_h, new_sort_by_h, sort_by_h);	
	}
	
		
	sort_by_m = new_sort_by_m;
	sort_by_p = new_sort_by_p;
	sort_by_h = new_sort_by_h;
	
	paras = "player_position=" + position + "&player_team=" + team + "&player_value=" + value + "&current_page=" + page_number + "&type=" + type + "&sort_by_m="+ sort_by_m + "&sort_by_p=" + sort_by_p + "&sort_by_h=" + sort_by_h + "&order_m=" + order_m + "&order_p=" + order_p + "&order_h=" + order_h;

	// use ajax to load the page of search result to the div.
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: 'get', 
			parameters: paras,
			onCreate: function() {
				$('ajax-loader-stats').style.display = "";
			  },
			onComplete:function() {
				$('ajax-loader-stats').style.display = "none";
				change_osta_button_status_type(type);
			}

		});
	
}

/**
	help function for update_search_player_result(): check if there is any sort by attribute if not returns "none"
*/
function get_sort_by(sort_by)
{
	if(sort_by == null)
	{
		sort_by = "smliiga_team ASC,name";
	}
	else if(sort_by.length == 0)
	{
		sort_by = "smliiga_team ASC,name";
	}
	
	return sort_by;
}

/**
	Help function for update_search_player_result(): check and return sorting order.
*/
function get_order(order, new_sort_by, sort_by)
{
	// if the new_sort_by == sort by, change the sorting order, other wise, put the sorting order ASC.
	if(new_sort_by == sort_by)
	{
		if(order == "DESC")
		{
			order = "ASC";
		}
		else
		{
			order = "DESC";
		}
	}
	else
	{
		order = "DESC";
	}
	return order;
}

/*
	Sort player in the player list (search_player, choose_player, and trade_player).
*/
function sort_search_player_result(url, div_id, sort_by)
{
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: 'get', 
			parameters: paras,
			onComplete:function()	{
				change_osta_button_status_type(type);
			}
		});
	
}

var	player_positions = new Array("m", "p1", "p2", "h1", "h2", "h3");

// this function deals with the choose player.
function choose_player(player_id, player_name, position, player_value, team, injury, type, imageurl, stats, translations)
{	
	var a="&#228;";
	var o="&#246;";
	// check if the player position is full
	var player = "";
	var notification_text;
	var notification_subject;
	switch(position)
	{
		case "m":
			if($("m_player").innerHTML != "" && $("m_player_delete").value!="<")
			{	
				notification_subject = translations['error_subject'];		
				notification_text = translations['error_forward'];
				openPopup("notification", notification_subject, notification_text);
				return;
			}
			else
			{
				player = "m";
			}
			break;
		
		case "p":
			if($("p1_player").innerHTML == "" || $("p1_player_delete").value == "<")
			{
				player = "p1";
			}
			else if($("p2_player").innerHTML == "" || $("p2_player_delete").value == "<")
			{
				player = "p2";
			}
			else
			{	
				notification_subject = translations['error_subject'];				
				notification_text = translations['error_defender'];
				openPopup("notification", notification_subject, notification_text);
				return;
			}
			break;
			
		case "h":			
			if($("h1_player").innerHTML == "" || $("h1_player_delete").value == "<")
			{
				player = "h1";
			}
			else if($("h2_player").innerHTML == "" || $("h2_player_delete").value == "<")
			{
				player = "h2";
			}
			else if($("h3_player").innerHTML == "" || $("h3_player_delete").value == "<")
			{
				player = "h3";
			}
			else
			{
				notification_subject = translations['error_subject'];
				notification_text = translations['error_forward'];
				openPopup("notification", notification_subject, notification_text);
				return;
			}
			break;
		
	}
	// check the player already chosen or not.
	if(!check_choose_players(player_id, player_name))
	{
		return;
	}
	
	// if player not chosen yet, and the position is not full either, add this player name to the input text box, and the player id to the hidden input, 
	if(player.length > 0)
	{		
		if(type == "create_team")
		{
			// change the color of player name, renew the selected player to the input boxes.			
			$(player + "_player_id").value = player_id;
			$(player + "_player").innerHTML = player_name;
			$(player + "_player_value").innerHTML = player_value;
			
			if(injury == "1")
			{
				$(player + "_player").style.color = "#FF0000";
			}
			
			else 
			{
				$(player + "_player").style.color = "#3399cc";
			}
			// Update player card event
			if ($(player + "_player").attachEvent) {
				$(player + "_player").attachEvent ("onclick", function(){ load_player('/player', 'player_card', player_id); return false; });
			} else {
				$(player + "_player").setAttribute("onclick", "load_player('/player', 'player_card', '"+player_id+"'); return false;");
			}
			
			// The delete button should be changed to "X" if it was "<", and show this button if it was invisible.
			$(player + "_player_value").style.color = "#000000";
			$(player + "_player_delete").value = "X";
			$(player + "_arrow").className = "select_button";
			$(player + "_arrow").value = translations['delete'];
			$(player + "_team_logo").style.background = "url(" + imageurl + "/defaults/smliiga_logos_small/" + team.toLowerCase() +".png) no-repeat";
			//$(player + "_player_delete").style.display = "block";
			//$(player + "_player_delete").style.color = "#ff0000";
			//$(player + "_player_delete").style.background = "#fff278";
		}
		else if(type == "trade_players")
		{
			// in the jersey view, change the color of player name, renew the selected player to the label.
			$(player + "_player_id").value = player_id;
			$(player + "_player").innerHTML = player_name;
			if(injury == "1")
			{
				$(player + "_player").style.color = "#FF0000";								
			}

			else 
			{
				$(player + "_player").style.color = "#3399cc";
			}
			// Update player card event
			if ($(player + "_player").attachEvent) {
				$(player + "_player").attachEvent ("onclick", function(){ load_player('/player', 'player_card', player_id); return false; });
			} else {
				$(player + "_player").setAttribute("onclick", "load_player('/player', 'player_card', '"+player_id+"'); return false;");
			}
			// Update player card event in data view
			if ($("data_" + player + "_player").attachEvent) {
				$("data_" + player + "_player").attachEvent ("onclick", function(){ load_player('/player', 'player_card', player_id); return false; });
			} else {
				$("data_" + player + "_player").setAttribute("onclick", "load_player('/player', 'player_card', '"+player_id+"'); return false;");
			}

			$(player + "_team_logo").style.background = "url("+imageurl+"/defaults/smliiga_logos_small/"+ team.toLowerCase() +".png) no-repeat";
			$(player + "_player_value").innerHTML = player_value;
			
			// in the data view, renew the selected player to the label.
			$(player + "_data").style.color = "#000000";				
			$("data_" + player + "_player").innerHTML = player_name;
			$("data_" + player + "_value").innerHTML = player_value;
			$("data_" + player + "_team").innerHTML = team;
			
			// The delete button should be changed to "X" if it was "<", and show this button if it was invisible.
			$(player + "_player_delete").value = "X";
			
			// in data view
			$(player + "_player_select").value = translations['sell'];

			// in jersey view
			$(player + "_arrow").value = translations['sell'];
			$(player + "_arrow").className = "select_button";
			// change the image of the jersey, anc background.
			$(player + "_jersey").style.backgroundImage = "url("+imageurl+"/player-slot-jersey.png)";
			//$(player + "_player_details").style.background = "#fceca6";
			//$(position + "_player_details").style.background = "transparent";
			$(player + "_player_data").style.backgroundImage = "url("+imageurl+"/player-box--bg.png)";
		
			
			check_trade_players();
		}


		if(type != "create_team")
		{			
			// change the player stats in the data view.
			for (key in stats)
			{				
				if(stats[key]=='')
				{
					$(player + "_" + key).innerHTML = "0";
				}
				else
				{
					$(player + "_" + key).innerHTML = stats[key];
				}
			}
		}
	}
		
	// after the player name and id are inserted into the input boxes, check if all the players are inserted, enable the "continue" button.
	calculate_saldo();
	change_continue_button_status(type);

}

// when user tries to push "X" to delete the selected or "<" or cancel removal, this function is called to change the button and player name status.
function change_player_selection_status(position, type, cancel_text, buy_text, delete_text, warning_text)
{
	// in create team, when the player section is "free slot", we don't change anything.
	if(type == "create_team" && $(position + "_arrow").className == "empty_slot_button")
	{
		return;
	}
	if($(position + "_player_delete").value == "X")
	{
		if(type == "trade_players")
		{
			$(position + "_player_delete").value = "<";
			
			// in data view
			$(position + "_data").style.color = "#838B8B";
			$(position + "_player_select").value = cancel_text;	
		
			// in jersey view
			$(position + "_arrow").value = cancel_text;
			$(position + "_arrow").className = "cancel_button";				
			// change the image of the jersey, anc background.
			$(position + "_jersey").style.backgroundImage = "url(/images/player-slot-jersey-empty.png)";
			$(position + "_player_data").style.backgroundImage = "url(/images/player-box-cancel-bg.png)";			
		}
		else if(type == "create_team")
		{
			
			// if the user want to unselect player.
			$(position + "_player").style.color = "#838B8B";
			$(position + "_player_value").style.color = "#838B8B";
			$(position + "_arrow").value = cancel_text;
			$(position + "_player_delete").value = "<";
			$(position + "_player_delete").style.color = "#76c473";
			$(position + "_player_delete").style.background = "#e6e6e6";
		}
	}
	else if($(position + "_player_delete").value == "<")
	{
		// if the user want to select the player (again)
		
		
		// check if the player's saldo will exced the current saldo.
		var saldo;
		var value;
		var player_value;
		var notification_text;
		saldo = $("saldo").innerHTML;
		saldo=clearWhiteSpace(saldo);
		
		player_value = $(position + "_player_value").innerHTML;
		value = player_value.replace(" ", "");

		if(parseInt(saldo) < parseInt(value))
		{
			// if the saldo is smaller than the value, open popup to tell the user that he select this player again.
			//$("notification_text").innerHTML = 
			notification_text = warning_text;
			openPopup("notification", null, notification_text);
			return;
		}
		
		// check the player already chosen or not.
		if(!check_choose_players($(position + "_player_id").value, $(position + "_player").value))
		{
			return;
		}
		
		if(type == "trade_players")
		{
			$(position + "_player_delete").value = "X";
			
			// in data view
			$(position + "_data").style.color = "#000000";	
			$(position + "_player_select").value = buy_text;			
		
			// in jersey view
			$(position + "_arrow").value = buy_text;
			$(position + "_arrow").className = "select_button";
			// change the image of the jersey, anc background.
			$(position + "_jersey").style.backgroundImage = "url(/images/player-slot-jersey.png)";
			$(position + "_player_data").style.backgroundImage = "url(/images/player-box--bg.png)";
			
			check_trade_players();
		}
		else if(type == "create_team")
		{

			// check the player existence.
			$(position + "_player").style.color = "#3399cc";
			$(position + "_player_value").style.color = "#000000";
			$(position + "_arrow").value = delete_text;
			$(position + "_player_delete").value = "X";
			$(position + "_player_delete").style.color = "#ff0000";
			$(position + "_player_delete").style.background = "#fff278";
			//$(position + "_selected_player_data_name").style.background = "#fff278";
		}
	}
	calculate_saldo();
	change_continue_button_status(type);
}

function check_trade_players()
{
	// get the trade number the user should have.
	var available_trade = $("available_trade").value;
	
	// create arrays to store the orginal players and current players.
	var original_players = new Array();
	var current_players = new Array();
	
	original_players[0] = $("old_h1_player_id").value;
	original_players[1] = $("old_h2_player_id").value;
	original_players[2] = $("old_h3_player_id").value;
	original_players[3] = $("old_p1_player_id").value;
	original_players[4] = $("old_p2_player_id").value;
	original_players[5] = $("old_m_player_id").value;
	
	current_players[0] = $("h1_player_id").value;
	current_players[1] = $("h2_player_id").value;
	current_players[2] = $("h3_player_id").value;
	current_players[3] = $("p1_player_id").value;
	current_players[4] = $("p2_player_id").value;
	current_players[5] = $("m_player_id").value;

	// compare the $original_players and $current_players to see how many difference there are.
	num = 0; // number of same players
	
	// compare the 3 forwards
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			if(current_players[i] == original_players[j])
			{
				num = num + 1;				
			}
		}
	}

	// compare the 2 defensemen.
	for(i=3; i<5; i++)
	{
		for(j=3; j<5; j++)
		{
			if(current_players[i] == original_players[j])
			{
				num = num + 1;
			}
		}
	}
	
	// compare the last goalie.
	if(current_players[5] == original_players[5])
	{
		num = num + 1;
	}
	
	trade_needed = 6 - num;
	// calculate the trade left.
	
	$("trade").innerHTML = available_trade - trade_needed;
	$("trade_left").value = $("trade").innerHTML;
	/*
	if(available_trade < trade_needed)
	{
		// change the trade color
		$('trade').style.background = "red";
	}
	else
	{
		$('trade').style.background = "green";
	}*/
	
}


// everytime the player is selected or deleted, this function will be called to enable and disable the "Continue" button, which inserts the players team relationships.
function change_continue_button_status(type)
{
	// when the 6 players are not chosen yet, disable the continue button.
	for(i=0; i<(player_positions.length); i++)
	{
		if($(player_positions[i] + "_player").innerHTML == "" || $(player_positions[i] + "_player_delete").value == "<")
		{
			$("btn_choose_player").disabled = true;
			$("btn_choose_player").className = "button_inactive";
			change_choose_player_instruction_status(type);
			return;
		}
	}
	var saldo;
	saldo=$("saldo").innerHTML;
	saldo=clearWhiteSpace(saldo);
	
	// check the saldo number, if it is minus, disable the button too.
	if(Math.abs(saldo) == saldo)
	{
		$("btn_choose_player").className = "button";
		$("btn_choose_player").disabled = false;
	}
	else
	{
			$("btn_choose_player").disabled = true;
			$("btn_choose_player").className = "button_inactive";
	}
	
	// check the trade left, if the trade left is more minus, then disabled the button.
	
	if(type == "trade_players" && Math.abs(saldo) == saldo)
	{
		// check if there is enough trade
		if(Math.abs($("trade").innerHTML) == $("trade").innerHTML)
		{
			$("btn_choose_player").disabled = false;
			$("btn_choose_player").className = "button";
		}
		
		else
		{
			$("btn_choose_player").disabled = true;
			$("btn_choose_player").className = "button_inactive";
		}
	}
	change_choose_player_instruction_status(type);
	return;
}

function change_choose_player_instruction_status(type)
{
	// if it is the creation process, display or hide the choose_player_instruction. 
	if(type == "create_team")
	{
		if($("btn_choose_player").disabled == true)
		{
			$("choose_player_instruction").style.display = "block";
		}
		else
		{
			$("choose_player_instruction").style.display = "none";
		}
	}

}

// this function is to check if the selected palyer is already choosen by the user.
function check_choose_players(player_id, player_name)
{
	var notification_text;
	// check if this player is already selected.
		
	for(i=0; i<(player_positions.length); i++)
	{
		if(player_id == $(player_positions[i] + "_player_id").value && $(player_positions[i] + "_player_delete").value == "X")
		{
			//$("notification_text").innerHTML = 
			notification_text = "Pelaaja " + player_name + " on jo valittu!";
			openPopup("notification", null, notification_text);
			return false;
		}
	}
	return true;
}

// this function check the value of saldo and change the saldo color by its value.
function change_saldo_color()
{	
	saldo=$("saldo").innerHTML;
	saldo=clearWhiteSpace(saldo);
	if(Math.abs(saldo) == saldo)
	{
		// positive saldo, the saldo should be green.
		//$("saldo_container").style.background = "#76c473";
	}
	else
	{
		// negative saldo, the saldo should be red.
		//$("saldo_container").style.background = "#FF0000";
	}
}

// this function is used to calculate the value of saldo after the user select or remove the player.
function calculate_saldo()
{
	var available_saldo = $("available_saldo").value;
		
	for(i=0; i<player_positions.length; i++)
	{
		// check if the player's value is not empty and the delete button is "X", calculate with current saldo.
		if($(player_positions[i] + "_player_value").innerHTML !="" && $(player_positions[i] + "_player_delete").value == "X")
		{
			var player_value;
			player_value = $(player_positions[i] + "_player_value").innerHTML;
			var value;
			
			value = player_value.replace(/ /g, "");			
			available_saldo = available_saldo - parseInt(value);
		}
	}
	
	$("saldo").innerHTML = formatThousand(available_saldo);	
	$("saldo_left").value = available_saldo;
	//change_saldo_color();
	change_osta_button_status();
}


// This function call the php page to pick 6 players ramdonly.
function quick_pick_players(div_id, url, saldo, team_id)
{
	var params = "teamId=" + team_id + "&saldo=" + saldo + "&quick_pick=true";
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: 'get', 
			parameters: params,
			onComplete:function() {
				calculate_saldo();
	    		change_continue_button_status("create_team");
			}
		});	
}

/*
	this function take the value of the saldo and compare to the each players' value, if they value is greater than the saldo, hide the button, otherwise, display the osta button;
	and if the player is already in slot, disable the saldo as well.
*/
function change_osta_button_status()
{
	// check the total saldo, if it is smaller than any player's value, then hiden the osta,
	var saldo;
	saldo=document.getElementById("saldo").innerHTML;
	saldo=clearWhiteSpace(saldo);
	//osta_btns = document.getElementsByClassName("player_result_id");
	var i=0;
	var k=0;
	var osta_btns;
	var tab_osta_btns;
	osta_btns = new Array();
	tag_osta_btns = new Array();
	
	// get the searched player ids inputs and tagged player ids inputs.
	inputs = document.getElementsByTagName("input");
	for(j=0;j<inputs.length;j++)
	{
		if(inputs[j].className == "player_result_id")
		{
			osta_btns[i] = inputs[j];
			i++;
			
		}
		else if(inputs[j].className == "tag_player_result_id")
		{
			tag_osta_btns[k] = inputs[j];
			k++;
			
		}
	}
	
	
	// check the each player in the player list, if their value is bigger than saldo, or if they are already in the selection slots.
	for(i=0;i<osta_btns.length;i++)
	{		
		// check the value VS. saldo
		var player_value;
		var value;
		
		id = osta_btns[i].value;		
		player_value = $(id +"_player_value").innerHTML;
		value = player_value.replace(" ", "");
		value = value.replace(" ", "");		
		if(parseInt(value) > parseInt(saldo))
		{
			// if the value is bigger than the saldo, hide the osta button
			 $(id + "_osta_btn").style.display = "none";
		}
		else
		{
			// if the value is smaller than the saldo, show the osta button
			$(id + "_osta_btn").style.display = "block";
		}
		
		// check if the player is already in the player slot.
		
		change_osta_by_player_existence("m_player", id);
		change_osta_by_player_existence("p1_player", id);
		change_osta_by_player_existence("p2_player", id);
		change_osta_by_player_existence("h1_player", id);
		change_osta_by_player_existence("h2_player", id);
		change_osta_by_player_existence("h3_player", id);
		
	}
	
	for(i=0;i<tag_osta_btns.length;i++)
	{
		id = tag_osta_btns[i].value;
		player_value = $("tag_" + id +"_player_value").innerHTML;
		value = player_value.replace(" ", "");
		
		if(parseInt(value) > parseInt(saldo))
		{
			// if the value is bigger than the saldo, hide the osta button
			 $("tag_" + id + "_osta_btn").style.display = "none";
		}
		else
		{
			// if the value is smaller than the saldo, show the osta button
			$("tag_" + id + "_osta_btn").style.display = "";
		}
		
		change_osta_by_player_existence("m_player", "tag_" + "tag_" + id);
		change_osta_by_player_existence("p1_player", "tag_" + "tag_" + id);
		change_osta_by_player_existence("p2_player", "tag_" + "tag_" + id);
		change_osta_by_player_existence("h1_player", "tag_" + "tag_" + id);
		change_osta_by_player_existence("h2_player", "tag_" + "tag_" + id);
		change_osta_by_player_existence("h3_player", "tag_" + "tag_" + id);

	}
	return;
}

/*
	This function will check if the current_player(which is in the player slot) has the same id as in the search_player_id (which is in the player list). 
	If they are the same, hide the osta button of the one in the player list.
*/
function change_osta_by_player_existence(current_player, search_player_id)
{
	if($(current_player + "_id").value == search_player_id && $(current_player + "_delete").value == "X")
	{
		$(search_player_id + "_osta_btn").style.display = "none";
	}
	return;
}
/*
	This function will check is the current_palyer position is already full. If yes, hidden the osta button.
*/
function change_osta_by_player_position(current_player)
{
	return;
}

/*
	this function will check the type, if it is choose player and trade players, we call change_osta_button_status() function.
*/
function change_osta_button_status_type(type)
{
	if(type == "create_team" || type == "trade_players")
	{
		change_osta_button_status();
	}
}

function formatThousand(stringVal)
{
	stringVal=stringVal+'';
	var left_chars=stringVal.length%3;
	var string_result = stringVal.substring(0, left_chars);
	if(stringVal.length>left_chars)
	{
	for(i=left_chars; i<stringVal.length;i=i+3)
	{
		string_result=string_result+' '+stringVal.substr(i,3);
	}
	}
return string_result;
}

function clearWhiteSpace(stringVal)
{
	var string_result="";
	for(i=0; i<stringVal.length; i++)
	{
		if(stringVal.charAt(i)==" ")
		{
			string_result=string_result+"";	
		}
		else
		{
			string_result=string_result+stringVal.charAt(i);
		}
		
	}
	return string_result;
}

/*
 * Load player card for player with specific player_id
 */
function load_player(url, player_div, player_id, phase)
{
	if (!phase) phase = 0;
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		player_div, 
		url2, 
		{
			method: 'get', 
			parameters: "player_id=" + player_id + "&phase=" + phase,
			evalScripts: true,
			onSuccess: function() {
				if ($('div_search_players'))
				{
					hideById('div_search_players');
					showById('whitearea');
				}
				showById(player_div);
				fadeInBackground();
			}
		});
}

/*
 * Player tagging functions
 */
function tag_player(player_id, notification_header, notification_header_full, notification_text_full, url, button, url_tagged)
{			
	var btn_type=button.alt;	
	var is_full = 0;
	var notification_head = notification_header;
	var notification_text = '';	
	
	// Check if user have 5 tagged players
	if (btn_type == "tag" && $('taggedCount').value >= 5)
	{
		is_full = 1;
		notification_head = notification_header_full;
		notification_text = notification_text_full;
	}
	
	else if (btn_type == "tag" && $('taggedCount').value < 5)
	{
		$('taggedCount').value = parseInt($('taggedCount').value) + 1;
	}
	
	else if (btn_type == "untag" && $('taggedCount').value > 0)
	{
		$('taggedCount').value = parseInt($('taggedCount').value) - 1;
	}	
	
	// use ajax to load a page checking if this user is already tagged,  or if there are five players tagged already, or tag the player if everthing goes right.
	var params = "player_id=" + player_id + "&button=" + btn_type;	
	var url2 = url+'?js=1&rand='+Math.random();
	if(url_tagged == null)
	{
		var myAjax = new Ajax.Request(				 
			url2, 
			{				
				method: 'get', 
				parameters: params,
				onComplete:function()	{													
					change_tag_btn_status(player_id, btn_type, is_full);	
					openPopup('notification', notification_head, notification_text);
				}
			});
	}
	else
	{
		var myAjax = new Ajax.Request(				 
			url2, 
			{
				method: 'get', 
				parameters: params,
				onComplete:function()	{							
					change_tag_btn_status(player_id, btn_type, is_full);	
					openPopup('notification', notification_head, notification_text);
					change_view('trades', 'tagged_players', url_tagged, 1);
				}
			});
	}
}

function tag_in_player_card(player_id, notification_header, notification_header_full, notification_text_full, notification_header_already, url, button, alreadyTagged)
{
	var btn_type=button.alt;		
	var notification_head = notification_header;
	var notification_text = '';
	
	// Check if player already tagged by user
	if (alreadyTagged == 1)
	{
		notification_head = notification_header_already;
	}
	
	// Check if user have 5 tagged players
	else if ($('taggedCardCount').value >= 5)
	{
		notification_head = notification_header_full;
		notification_text = notification_text_full;
	}
		
	// use ajax to load a page checking if this user is already tagged,  or if there are five players tagged already, or tag the player if everthing goes right.
	var params = "player_id=" + player_id + "&button=" + btn_type;
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Request(				 
		url2, 
		{
			method: 'get', 
			parameters: params,
			onComplete:function()	{					
				openPopup('notification', notification_head, notification_text);				
			}
		});		 
}

function change_tag_btn_status(player_id, btn_type, is_full)
{			
	if (is_full == 1 && btn_type == "tag")
	{
		if ($("t" + player_id + "_tag_btn").style.display == "none")
		{
			$("t" + player_id + "_tag_btn").style.display = "none";
			$("u" + player_id + "_untag_btn").style.display = "block";			
		}

		else 
		{
			$("t" + player_id + "_tag_btn").style.display="block";
			$("u" + player_id + "_untag_btn").style.display="none";		
		}			
	}
	
	else if (btn_type == "tag")
	{	
		$("t" + player_id + "_tag_btn").style.display = "none";
		$("u" + player_id + "_untag_btn").style.display = "block";
	}
	
	else 
	{
		$("t" + player_id + "_tag_btn").style.display="block";
		$("u" + player_id + "_untag_btn").style.display="none";		
	}	
}


function tag_untag_player(player_id, player_name, div_id, container_id, url, button)
{
	btn_type=button.alt;
	// use ajax to load a page checking if this user is already tagged,  or if there are five players tagged already, or tag the player if everthing goes right.
	var params = "player_id=" + player_id + "&button=" + btn_type;
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: 'get', 
			parameters: params,
			onComplete:function()	{
				openPopup(container_id, true);	
				change_tag_btn_status(player_id,div_id);				
			}
		});		
}
function refresh_tag_list(player_id, div_id, url)
{
	var params = "player_id="+player_id;
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
			div_id, 
			url2, 
			{
				method: 'get',
				parameters: params,
				onComplete:function()	{
					change_osta_button_status();
				}
			}
		);
}

function delete_tag_player(view, div_id, refresh_url, delete_url, player_id)
{	
        if ($('taggedCount').value > 0)
        {
                $('taggedCount').value = parseInt($('taggedCount').value) - 1;
        }

	var params = "player_id="+player_id+"&button=untag";
	var url2 = delete_url+'?js=1&rand='+Math.random();
	new Ajax.Request( 
		delete_url, 
		{
			method: 'get', 
			parameters: params,
			onSuccess:function() {						 
				change_view(view, div_id, refresh_url);
				change_tag_btn_status(player_id, 'untag');
			}
		});

		
}

function change_view(view, div_id, url, do_check, ajax)
{
	if (ajax == null)
	{
		ajaxparam = 1;
	}
	
	else 
	{
		ajaxparam = null;
	}
	
	var params = "view=" + view + "&ajax="+ajaxparam;
	var url2 = url+'?js=1&rand='+Math.random();	
	if (do_check == null)
	{
		new Ajax.Updater(
				div_id, 
				url2, 
				{
					method: 'get', 
					parameters: params
				});
	}
	else
	{
		new Ajax.Updater(
				div_id, 
				url2, 
				{
					method: 'get', 
					parameters: params,
					onComplete:function() {						 
						check_trade_players();
						change_osta_button_status();
					}
				});
	}
}

function change_trades_view(view)
{
	if (view == 'data')
	{
		$("trade_section").style.display = 'block';
		$("player_list_content").className = 'player_list_content_data';
		$("tab1").className = 'button';
		$("tab2").className = 'button_inactive';		
		$("trades_data_view").style.display = 'block';
		$("trades_jersey_view").style.display = 'none';	
		$("search_players_section").style.display = 'block';
		//$("top_transfer_section").style.display = 'none';	
		$("h1_player_details").style.display = 'none';
		$("h1_player").style.display = 'none';
		$("h2_player_details").style.display = 'none';
		$("h2_player").style.display = 'none';
		$("h3_player_details").style.display = 'none';
		$("h3_player").style.display = 'none';
		$("p1_player_details").style.display = 'none';
		$("p1_player").style.display = 'none';
		$("m_player_details").style.display = 'none';
		$("m_player").style.display = 'none';
		$("p2_player_details").style.display = 'none';
		$("p2_player").style.display = 'none';
	}
	
	else if (view == 'jersey')
	{
		$("trade_section").style.display = 'block';
		$("player_list_content").className = 'player_list_content_jersey';
		$("tab1").className = 'button_inactive';
		$("tab2").className = 'button';				
		$("trades_jersey_view").style.display = 'block';
		$("trades_data_view").style.display = 'none';
		$("search_players_section").style.display = 'block';
		//$("top_transfer_section").style.display = 'none';
		$("h1_player_details").style.display = 'block';
		$("h1_player").style.display = 'block';
		$("h2_player_details").style.display = 'block';
		$("h2_player").style.display = 'block';
		$("h3_player_details").style.display = 'block';
		$("h3_player").style.display = 'block';
		$("p1_player_details").style.display = 'block';
		$("p1_player").style.display = 'block';
		$("m_player_details").style.display = 'block';
		$("m_player").style.display = 'block';
		$("p2_player_details").style.display = 'block';
		$("p2_player").style.display = 'block';
	}
	
	else if (view == 'top10')
	{	
		$("tab1").className = 'inactive_tab';
		$("tab2").className = 'inactive_tab';
		$("tab3").className = 'active_tab';		
		$("trade_section").style.display = 'none';	
		$("search_players_section").style.display = 'none';
		//$("top_transfer_section").style.display = 'block';
	}
}
// Get user selected players
function get_selected_players(div_id, url, teamId, saldo, type, view)
{
	var params="teamId="+teamId+"&saldo="+saldo+"&type="+type+"&view="+view;
	var url2 = url+'?js=1&rand='+Math.random();
	var selectPlayerAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: "get", 
			parameters: params
		});
}

function load_top_10_list(div_id, url)
{
	var url2 = url+'?rand='+Math.random();
	var loadTransfersAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{

		});	
}

function load_smliiga_team_stats(teamId, teamName, position, phase, round, order, asc, img_url, loading_image)
{
	var params="teamId="+teamId+"&teamName="+teamName+"&position="+position+"&phase="+phase+"&round="+round+"&order="+order+"&asc="+asc;
	var url2 = '/sm-liiga/ajax-stats-team-season?rand='+Math.random();
	var div_id = 'stats_'+position+'_'+teamId;
	var loadTransfersAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: "get", 
			parameters: params,
			onCreate: function() {
				if (loading_image == 1) $(div_id).innerHTML = '<img src="'+img_url+'/ajax-loader_green.gif" alt="" style="padding-left:270px;" />';
			  }
		});	
}

function hideShowSMliigaGameReports(url, match_id, hometeam, awayteam, background, img_url)
{	
	if ($('match_statistics_'+match_id).style.display == 'block')
	{
		$('match_statistics_'+match_id).style.display = 'none';		  
		$('match_'+match_id).style.background = '#'+background;		
		$('match_'+match_id).style.marginTop = '0';
		$('match_'+match_id).style.color = '#000000';
		$('match_lineup_link_'+match_id).style.color = '#3399cc';
		$('match_report_link_'+match_id).style.color = '#000000';
				
		if (background == 'f8f7f2')
		{
			$('match_'+match_id).style.borderTop = '1px solid #cccccc';	
			$('match_'+match_id).style.borderBottom = '1px solid #cccccc';
			$('match_'+match_id).style.borderLeft = 'none';	
			$('match_'+match_id).style.borderRight = 'none';		
		}
		
		else 
		{
			$('match_'+match_id).style.border = 'none';
		}
	} 
	else
	{	
		var params="matchId="+match_id+"&homeTeam="+hometeam+"&awayTeam="+awayteam;
		var url2 = url+'?rand='+Math.random();
		var target = 'match_statistics_'+match_id;
		var myAjax = new Ajax.Updater(
			target, 
			url2, 
			{
				method: 'get', 
				parameters: params,
				onCreate: function() {
					$('match_statistics_'+match_id).style.display = 'block';					
					$('match_lineups_'+match_id).style.display = 'none';
					$('match_'+match_id).style.background = 'url('+img_url+'/title-bar-bg.png)';
					$('match_'+match_id).style.color = '#ffffff';
					$('match_'+match_id).style.border = '1px solid #a4a3a4';
					$('match_'+match_id).style.marginTop = '2px';
					$('match_statistics_'+match_id).innerHTML = '<img src="'+img_url+'/ajax-loader_black.gif" alt="" style="padding-left:270px;" />';
		    		$('match_lineup_link_'+match_id).style.color = '#ffffff';
					$('match_report_link_'+match_id).style.color = '#ffffff';
				}
			});		
	}
}

function hideShowFrontpageGameReports(url, match_id, hometeam, awayteam, background, img_url)
{	
	if ($('match_statistics_'+match_id).style.display == 'block')
	{
		$('match_statistics_'+match_id).style.display = 'none';
	}
	else
	{
		var params="matchId="+match_id+"&homeTeam="+hometeam+"&awayTeam="+awayteam;
		var url2 = url+'?rand='+Math.random();
		var target = 'match_statistics_'+match_id;
		var myAjax = new Ajax.Updater(
			target,
			url2,
			{
				method: 'get',
				parameters: params,
				onCreate: function() {
					$('match_statistics_'+match_id).style.display = 'block';					
					$('match_lineups_'+match_id).style.display = 'none';
				}
			});		
	}
}

function hideShowSMliigaLineups(url, match_id, background, img_url)
{
	if ($('match_lineups_'+match_id).style.display == 'block')
	{
		$('match_lineups_'+match_id).style.display = 'none';		  
		$('match_'+match_id).style.background = '#'+background;			
		$('match_lineups_'+match_id).style.marginTop = '0';
		$('match_'+match_id).style.color = '#000000';
		$('match_lineup_link_'+match_id).style.color = '#3399cc';
		$('match_report_link_'+match_id).style.color = '#000000';

		if (background == 'f8f7f2')
		{
			$('match_'+match_id).style.borderTop = '1px solid #cccccc';	
			$('match_'+match_id).style.borderBottom = '1px solid #cccccc';
			$('match_'+match_id).style.borderLeft = 'none';	
			$('match_'+match_id).style.borderRight = 'none';		
		}
		
		else 
		{
			$('match_'+match_id).style.border = 'none';
		}
	} 
	else
	{
		var params="matchId="+match_id;
		var url2 = url+'?rand='+Math.random();
		var target = 'match_lineups_'+match_id;
		var myAjax = new Ajax.Updater(
			target, 
			url2, 
			{
				method: 'get',
				parameters: params,
				onCreate: function() {
					$('match_statistics_'+match_id).style.display = 'none';
					$('match_lineups_'+match_id).style.display = 'block';
					$('match_'+match_id).style.background = 'url('+img_url+'/title-bar-bg.png)';
					$('match_'+match_id).style.color = '#ffffff';
					$('match_'+match_id).style.border = '1px solid #a4a3a4';
					$('match_'+match_id).style.marginTop = '2px';
					$('match_lineups_'+match_id).innerHTML = '<img src="'+img_url+'/ajax-loader_black.gif" alt="" style="padding-left:270px;" />';
		    		$('match_lineup_link_'+match_id).style.color = '#ffffff';
					$('match_report_link_'+match_id).style.color = '#ffffff';
				}
			});
	}
}

function hideShowFrontpageLineups(url, match_id, background, img_url)
{
	if ($('match_lineups_'+match_id).style.display == 'block')
	{
		$('match_lineups_'+match_id).style.display = 'none';		  
	} 
	else
	{
		var params="matchId="+match_id;
		var url2 = url+'?rand='+Math.random();
		var target = 'match_lineups_'+match_id;
		var myAjax = new Ajax.Updater(
			target, 
			url2, 
			{
				method: 'get',
				parameters: params,
				onCreate: function() {
					$('match_statistics_'+match_id).style.display = 'none';
					$('match_lineups_'+match_id).style.display = 'block';
				}
			});
	}
}

function update_results_statistics(url, div_id, phase, round, type, img_url)
{
	var params="phase="+phase+"&round="+round+"&type="+type;
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: 'get',
			parameters: params,
			onCreate: function () {
				$('ajax-loader-stats').style.display = '';
			},
			onComplete: function() {
				$('ajax-loader-stats').style.display = 'none';
				$('active_type').value = type;
			}
		});
}
function update_team_round_statistics(url, div_id, team, phase, round, disp_round)
{
	var params="phase="+phase+"&round="+round+"&disp_round="+disp_round+"&team="+team;
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		div_id, 
		url2, 
		{
			method: 'get',
			parameters: params,
			onComplete: function() {
				$(div_id).style.display = 'block';
			}
		});
}
function change_send_feedback_button_status(textarea_id, button_id, email_id, name_id)
{	var status=false;
	var content_length=trimText($F(textarea_id)).length;
	var cont=true;
	if(content_length>0)
	{
		status=true;	
	}
	else if(content_length==0)
	{
		status=false;
	}
		
	if(status)
	{
		$(button_id).className = "button";
		$(button_id).disabled = false;
	}
	else
	{
		$(button_id).className = "button_inactive";
		$(button_id).disabled = true;
	}
}
function showLength(feedback_field_id,show_field_id, length_size)
{
	var field_length=$(feedback_field_id).innerHTML.length;
	$(show_field_id).innerHTML=length_size-field_length;
	
}
function trimText(text) {
	return text.replace(/^\s+|\s+$/g,"");
}

function switchTeam(url, id)
{
	document.location = url + '/0/' + id;
}

function changeTopTrades(type, title)
{	
	$('top_trades_title').innerHTML = '<h3>'+title+'</h3>';
	
	if (type == 'bought')
	{
		$('sold_players').style.display = 'none';
		$('top_players').style.display = 'block';
	}
	
	else
	{
		$('sold_players').style.display = 'block';
		$('top_players').style.display = 'none';		
	}
}
function update_frontpage_matches(url, div_id, dateindex)
{
	var params="dateindex="+dateindex;
	var url2 = url+'?js=1&rand='+Math.random();
	var myAjax = new Ajax.Updater(
		div_id,
		url2,
		{
			method: 'get',
			parameters: params
		});
}
function updatePlayerCardGraph(dataUrl, flashUrl, plus, lastPage)
{
	tmp = parseInt($('graphStart').value);
	if (plus > 0) {
		if (tmp < lastPage) {
			tmp = tmp + 1;
		}
		else
		{
			tmp = lastPage;
		}
		$('graphStart').value = tmp;
	}
	if (plus < 0) {
		if (tmp > 1) {
			tmp = tmp - 1;
			$('graphStart').value = tmp;
		}
	}

	var param1 = {"data": dataUrl+"/"+tmp+"?js=1&random="+Math.random()};
	var param2 = {"wmode": "transparent","allowscriptaccess": "always"};
	swfobject.embedSWF(flashUrl, "player_graph", "283", "122", "9.0.0", null, param1, param2 );
}

function hide_show_search_by(value) {
	if ($('form-favouriteteam')) {
		if (value == 'team') $('form-favouriteteam').style.display = 'none';
		else $('form-favouriteteam').style.display = '';
	}
}

function change_group_standings_graph(value, flashUrl, baseUrl, group_id, phase) {
	if ($('group_team_standings')) {
		if (value == 'standings')
		{
			$('group_team_standings').style.display = '';
			$('group_team_graphs').style.display = 'none';
		}
		else
		{
			$('group_team_standings').style.display = 'none';
			$('group_team_graphs').style.display = '';
			if ($('group_team_graphs').innerHTML == '')
			{
				loadGroupGraphs(flashUrl, baseUrl, group_id, phase, 0, 0, 0);
			}
		}
	}
}
function loadGroupGraphs(flashUrl, baseUrl, group_id, phase, team_id1, team_id2, team_id3)
{
	var params="groupId="+group_id+"&phase="+phase+"&teamId1="+team_id1+"&teamId2="+team_id2+"&teamId3="+team_id3;
	var url2 = baseUrl+'/group/ajax-team-graphs?js=1&rand='+Math.random();
	var target = 'group_team_graphs';
	var myAjax = new Ajax.Updater(
		target, 
		url2, 
		{
			method: 'get',
			parameters: params,
			onComplete: function() {
				swfobject.embedSWF(flashUrl+"/open-flash-chart.swf", "flash_group_chart", "686", "250", "9.0.0", null, {"data":baseUrl+"/group/graph-data/"+team_id1+"/"+team_id2+"/"+team_id3+"/"+phase+"?js=1&random="+Math.random()+""}, {"wmode":"transparent","allowscriptaccess":"always"} );
			}
		});
}

