function updateGeneric( spanid, value )
{
	$( '#' + spanid ).html( value );
}

function refreshPoints()
{
	reloadGeneric( 'greenptn' );
	reloadGeneric( 'pinkptn' );	
	reloadGeneric( 'rankpts' );	
	reloadGeneric( 'global_score' );
	setTimeout(updateOnline, 5000);
	setTimeout(refreshPoints, 180000);
}

function updateOnline()
{
	$.ajax({
		type: "GET",
		url: "/online-status.php",
    cache: false,
    success: function(msg) {

		}
	});
}

function reloadGeneric( spanid )
{
	$.ajax({
		type: "GET",
		url: "/avatar-status.php?action=retrieve&field=" + spanid,
    cache: false,
    success: function(msg) {
        updateGeneric( spanid, msg );
		}
	});
}

function updateRankingPoint( value )
{
	updateGeneric( 'rankpts', value );
}

function updateGlobalScore( value )
{
	updateGeneric( 'global_score', value );
}

function updateFashionPoint( value )
{
	updateGeneric( 'fashionpts', value );
}

function updateCupNumber( value )
{
	updateGeneric( 'cupno', value );
}

function updateGreenPotion( value )
{
	updateGeneric( 'greenptn', value );
}

function updatePinkPotion( value )
{
	updateGeneric( 'pinkptn', value );
}

function closePDA()
{
	slidelayer_in('PDA', 'PDAOverLay');
	//showhide_layer('PDAOverLay');
	
	return true;
}

function openPDA()
{
	slidelayer_out('PDA', 'PDAOverLay');
	//showhide_layer('PDAOverLay');
	
	return true;
}

function openMag(image_name)
{
	slidelayer_out('Mag', 'MagOverLay');
	
	//Aligning the images
	var imgObj = new Image();
	imgObj.src = "images/mag_images/"+image_name;
	var imgWidth = imgObj.width;
	
	if (self.innerWidth) // all except Explorer
		var doc_width = self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)	// Explorer 6 Strict Mode
		var doc_width = document.documentElement.clientWidth;
	else if (document.body) // other Explorers
		var doc_width = document.body.clientWidth;

	mag_div = document.getElementById("Mag");
	leftpos = (doc_width/2 - imgWidth/2) + 50;
	mag_div.style.left = leftpos+"px";
	
	return true;
}

function closeMag()
{
	slidelayer_in('Mag', 'MagOverLay');
	var layer = document.getElementById("Mag");
	layer.style.left = "-1000px";

	return true;
}

function checkPDAStatus() {

	var layer = document.getElementById("PDAOverLay");
	var myStatus = (layer.style.visibility == "visible") ? "visible" : "hidden";
	
	return myStatus;
}

function openPP( $personal_user_id )
{
	$("#PP").toggle();
	$.ajax({
		type: "GET",
		url: "personal.php",
		data: "id="+$personal_user_id,
		cache: false,
		success: function(msg){
			$("#PP").html(msg)
		}
	});
}

function view_club_info_from_ranks( $club_id ) 
{
	$("#PP").toggle();
	$.ajax({
		type: "GET",
		url: "club.php",
		data: "action=view&club_id="+$club_id,
		cache: false,
		success: function(msg){
                	$("#PP").html( msg );
		}
	});
} 

//---------- Player List Functions ----------//

function openPlayerList()
{
	if ($("#PP").show())
		$("#PP").hide()
		
	$("#PL").dialog({ 
	width: 1000,
	height: 600,
	draggable: true,
    modal: true, 
	url: "players.php",
    overlay: { 
        opacity: 0.5, 
        background: "black" 
    } 
});
	$.ajax({
		type: "GET",
		url: "players.php",
		cache: false,
		success: function(msg){
			$("#PL").html(msg)
		}
	});
}

function closePlayerList()
{
	$("#PL").toggle();
}

function player_filter( $filter ) 
{
	$.ajax({
		type: "GET",
		url: "players.php",
		data: "action=filter&filter="+$filter,
		cache: false,
		success: function(msg){
			$("#PL").html( msg );
		}
	});
}

function player_search( $reported_id ) 
{
	$.ajax({
		type: "POST",
		url: "players.php",
		data: "action=search&search_name=" + $("#search_name").attr("value"),
		cache: false,
		success: function(msg){
			$("#PL").html( msg );
		}
	});
}

function request_friendship_pl( $user_id ) 
{
	$.ajax({
		type: "GET",
		url: "personal.php",
		data: "action=request_friendship&id="+$user_id,
		cache: false,
		success: function(msg){
			alert("Your request has been sent!!");

		}
	});
}

function update_list( $limit ) 
{
	$.ajax({
		type: "GET",
		url: "players.php",
		data: "limit="+$limit,
		cache: false,
		success: function(msg){
			$("#PL").html( msg );
		}
	});
}

//---------- Player List Functions ----------//


//---------- Credit Code Functions ----------//

function key_jumper()
{
	var num = 4; 
	if (self.innerWidth) // all except Explorer
		num = 3;

	$("#cc_str1").keypress(function( e )
	{  
		if ($("#cc_str1").val().length == num) { $("#cc_str2").focus(); }
	});   
	$("#cc_str2").keypress(function( e )
	{  
		if ($("#cc_str2").val().length == num) { $("#cc_str3").focus(); }
	}); 
	$("#cc_str3").keypress(function( e )
	{  
		if ($("#cc_str3").val().length == num) { $("#cc_str4").focus(); }
	});   
	$("#cc_str4").keypress(function( e )
	{  
		if ($("#cc_str4").val().length == num) { $("#add").focus(); }
	}); 
	$("#cc_str1").focus();
}

function check_cc_fields()
{
	if (($("#cc_str1").val().length == 0)||($("#cc_str2").val().length == 0)||($("#cc_str3").val().length == 0)||($("#cc_str4").val().length == 0))
	{
		alert('One or more fields are empty!');
		return false;
	}
	else
		return true;
}

//---------- Credit Code Functions ----------//


//---------- Score Ranking Functions ----------//

function score_rank_select(game_id)
{
	if (game_id != 0)
		url = '?select='+game_id+'&pg=1';
	else
		url = '?pg=1';

	window.location = url;
}

function score_rank_radio(period)
{
	var game_id = document.getElementById("rank_select").value;

	if (period == 'all_time')
		url = '?select='+game_id+'&pg=1';
	else
		url = '?select='+game_id+'&period='+period+'&pg=1';

	window.location = url;
}

//---------- Score Ranking Functions ----------//


//---------- Registration Functions START ----------//

//This function is to stop user entering the special characters
function no_special_chars(e)
{
	var myKeyCode;
	if (window.event) //For IE
		myKeyCode = e.keyCode;
	else if (e.which) //For the rest of the browser
		myKeyCode = e.which;

	if (myKeyCode == 47 || myKeyCode == 36 || myKeyCode == 40 ||
		myKeyCode == 41 || myKeyCode == 63 || myKeyCode == 42 ||
		myKeyCode == 43 || myKeyCode == 45 || myKeyCode == 91 ||
		myKeyCode == 93 || myKeyCode == 124 || myKeyCode == 60 ||
		myKeyCode == 62 || myKeyCode == 34 || myKeyCode == 38)
		return false;
	else
		return true;
}

//This function is to stop user entering alphabet and other special chars
function only_numbers(e)
{
	var myKeyCode;
	if (window.event) //For IE
		myKeyCode = e.keyCode;
	else if (e.which) //For the rest of the browser
		myKeyCode = e.which;
	
	if (myKeyCode >= 48 && myKeyCode <= 57 || myKeyCode == 8)
		return true;
	else
		return false;
}

function show_province(selected_value)
{
	//alert(selected_value);
	if (selected_value == "IT")
	{
		document.getElementById("province_id").style.visibility = "visible";
		document.getElementById("province_value").style.visibility = "visible";
	}
	else
	{
		document.getElementById("province_id").style.visibility = "hidden";
		document.getElementById("province_value").style.visibility = "hidden";
	}
		
}

function check_strength(pwd_value)
{
	if (pwd_value != '')
	{
		var indicator = "weak"; //initial indicator
				
		if (pwd_value.length < 6) //if less than 6 chars, always weak
			indicator = "weak";
				
		else if (pwd_value.length >= 6 && pwd_value.length < 8) //if 6 or 7 chars, can be medium
		{
			if (pwd_value.match(/(.*[0-9].*[0-9].*[0-9])/) && 
				pwd_value.match(/(.*[A-z].*[A-z].*[A-z])/))	//at least 3 numbers and chars are used
				indicator = "medium";		
			else
				indicator = "weak";
		}
	
		else if (pwd_value.length >= 8)	//if 8 or more chars, can consider as strong
		{
			if (pwd_value.match(/(.*[0-9].*[0-9].*[0-9].*[0-9].*[0-9])/) && 
				pwd_value.match(/(.*[A-z].*[A-z].*[A-z])/)) //at least 5 numbers and 3 chars are used
				indicator = "strong";
			else if (pwd_value.match(/(.*[0-9].*[0-9].*[0-9])/) && 
				pwd_value.match(/(.*[A-z].*[A-z].*[A-z])/)) //if at least 3 numbers are used
				indicator = "medium";
			else
				indicator = "weak";
		}
		
		//always set all status not bold first
		document.getElementById("medium").style.fontWeight = "";
		document.getElementById("weak").style.fontWeight = "";
		document.getElementById("strong").style.fontWeight = "";		
		
		//then, set accordingly
		if (indicator == "weak")
			document.getElementById("weak").style.fontWeight = "bold";
		else if (indicator == "medium")
			document.getElementById("medium").style.fontWeight = "bold";		
		else
			document.getElementById("strong").style.fontWeight = "bold";
	}
}

//---------- Registration Functions END ----------//