// Redirect To URL
function go(url) {
window.location = url;
}

// Load default.app
function loadApp() {
	$(".app_bg").fadeIn();
	$(".app_box").show("slow");
}

// Load Status.app
function loadAppStatus() {
//	$(".app_box").css("height", "400px");
//	$(".app_box").css("top", "20%");
	$(".app_bg").fadeIn();
	$(".app_box").slideDown("slow");
	var randomnumber=Math.floor(Math.random()*10001);
	var filename = "loadstatus.php?rand=" + randomnumber;
	var fileref=document.createElement("script");
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
	document.getElementsByTagName("head")[0].appendChild(fileref);
}

// Load UsersOnline.app
function loadUsersOnline() {
	$(".app_bg").fadeIn();
	$(".app_box").slideDown("slow");
	var randomnumber=Math.floor(Math.random()*10001);
	var filename = "usersonline.php?rand=" + randomnumber;
	var fileref=document.createElement("script");
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
	document.getElementsByTagName("head")[0].appendChild(fileref);

}

// Load Login.app
function login() {
	document.getElementById("app_box_login").innerHTML = document.getElementById("app_box_login").innerHTML + '<div style="width:100%;text-align:right;"><span onClick="closeAppLogin();" style="cursor:pointer;">Close [X] &nbsp; &nbsp;</span><br /></div>';
	$(".app_bg").fadeIn();
	$(".app_box_login").slideDown("slow");
	
}

// Load Chat.app
function loadAppChat(id) {
	var randomnumber=Math.floor(Math.random()*10001);
	var filename = "loadchat.php?rand=" + randomnumber + "&id="+id;
	var fileref=document.createElement("script");
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
	document.getElementsByTagName("head")[0].appendChild(fileref);
	$(".app_bg").fadeIn();
	setTimeout('$(".app_box").fadeIn("slow")',200);
}

// close .app
function closeApp() {
	$(".app_bg").fadeOut();
	$(".app_box").slideUp("slow");
	$(".app_box_login").slideUp("slow");
	document.getElementById("app_box").innerHTML = "";
}

// close login.app
function closeAppLogin() {
	$(".app_bg").fadeOut();
	$(".app_box_login").slideUp("slow");
}

// Mouse Over Side Link
function slmouseover(sl) {
	if (document.getElementById("sls").innerHTML == "Show") {
	}
	else {
		if ($("#" + sl).width() == 150) {
		$("#" + sl).animate({width: 180}, 200);
		$("#" + sl).css("font-weight","bold");
		}
	}
}

//Mouse Out Side Link
function slmouseout(sl) {
	if (document.getElementById("sls").innerHTML == "Show") {
	}
	else {
	$("#" + sl).animate({width: 150}, 200);
	$("#" + sl).css("font-weight","normal");
	}
}

// Submit Status In Status.app
function updatestatus() {
var randomnumber=Math.floor(Math.random()*10001);
var status= document.getElementById("statusupdate").value;
  var dataString = "status=" + encodeURIComponent(status) + "&rand=" + randomnumber;
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/status.php",
    data: dataString,
    success: function() {
document.getElementById("message_update").innerHTML = "Status Updated Successfully!";
$(".message_update").slideDown("fast");
document.getElementById("currentstatus").innerHTML = status;
    }
  });
  return false;
}

// Get Username
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}


// Submit Chat in Match.chat
function submitchat(id) {
var uname = getCookie("username")
var chat = encodeURIComponent(document.getElementById('chatboxmatch').value);
var chat1 = document.getElementById('chatboxmatch').value;
var dataString = 'message8=' + chat + '&type=' + id;
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/chatsubmit.php",
    data: dataString,
    success: function() {
document.getElementById("chatboxmatch1").innerHTML = "<div style=\"display: inline; font-size:11px; font-family:Verdana;\">" + chat1 + "<br />" + uname + "</div><hr />" + document.getElementById("chatboxmatch1").innerHTML;
document.getElementById('chatboxmatch').value = "";
    }
  });
  return false;
}

// Submit Chat in Chat.app
function submitchatappb() {
var uname = getCookie("username")
var chat = escape(document.getElementById('chatboxregb').value);
var chat1 = document.getElementById('chatboxregb').value;

var dataString = 'message8=' + chat + '&type=reg';
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/chatsubmit.php",
    data: dataString,
    success: function() {
document.getElementById("chatboxreg2").innerHTML = "<div style=\"display: inline; font-size:11px; font-family:Verdana;\">" + chat1 + "<br />" + uname + "</div><hr />" + document.getElementById("chatboxreg2").innerHTML;
document.getElementById('chatboxregb').value = "";
    }
  });
  return false;
  }
 
// Submit Chat in Sidebar.chat
  function submitchatappa() {
  var uname = getCookie("username")
var chat = escape(document.getElementById('chatboxrega').value);
var chat1 = document.getElementById('chatboxrega').value;

var dataString = 'message8=' + chat + '&type=reg';
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/chatsubmit.php",
    data: dataString,
    success: function() {
document.getElementById("chatboxreg1").innerHTML = "<div style=\"display: inline; font-size:11px; font-family:Verdana;\">" + chat1 + "<br />" + uname + "</div><hr />" + document.getElementById("chatboxreg1").innerHTML;
document.getElementById('chatboxrega').value = "";
    }
  });
  return false;
  }

// Get Profile Widget
function getprofilewidget( user ) {
  var rand_no = Math.random();
  var filename = 'getprofilewidget.php';
  filename = filename + "?username=" + user;
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

// Check If Ads Are Being Blocked
function checkad() {
if ($(".myadvert").height() == 0) {
var text = "We have detected that you are blocking our ads. Because the WRL functions completely on the revenue earned from ads, we would appreciate it if you would disable Adblock. Thanks!";
document.getElementById("myadvert").innerHTML = "";
}
}

// PM Mouseover
function pmmouseover() {
$("#pm").attr("class","pm_mo")
}

// PM Mouseout
function pmmouseout() {
$("#pm").attr("class","pm_normal")
}

// Mouseover Profile Tab
function ptabmouseover(id) {
var color = $("#ptab" + id).css("background-color");
if (color == "rgb(22, 17, 120)") {
document.getElementById("ptab" + id).style.backgroundColor="#2116F6";
}
}

// Mouseout Profile Tab
function ptabmouseout(id) {
var color = document.getElementById("ptab" + id).style.backgroundColor;
//alert(color);
if (color == "rgb(33, 22, 246)") {
document.getElementById("ptab" + id).style.backgroundColor="#161178";
}
}

// Mouseover Home Tab
function htabmouseover(id) {
var color = $("#htab" + id).css("background-color");
if (color == "rgb(22, 17, 120)") {
document.getElementById("htab" + id).style.backgroundColor="#2116F6";
}
}

// Mouseout Home Tab
function htabmouseout(id) {
var color = document.getElementById("htab" + id).style.backgroundColor;
//alert(color);
if (color == "rgb(33, 22, 246)") {
document.getElementById("htab" + id).style.backgroundColor="#161178";
}
}

function uploadimageapp() {
	document.getElementById("app_box").innerHTML = '<br /><center><div style="width:200px;text-align:center;"><div id="file-uploader"><noscript><p>Please enable JavaScript to use file uploader.</p></noscript></div></div></center><br /><br />';
	document.getElementById("app_box").innerHTML = document.getElementById("app_box").innerHTML + '<div style="width:100%;text-align:right;"><span onClick="closeApp();" style="cursor:pointer;">Close [X] &nbsp; &nbsp;</span><br /></div>';
loaduploadimageapp();
	$(".app_bg").fadeIn();
	$(".app_box").slideDown("slow");
}

function loaduploadimageapp() {
var uploader = new qq.FileUploader({
element: document.getElementById("file-uploader"), 
action: "uploads/usr/upload.php", 
allowedExtensions: ["jpg", "jpeg", "png", "gif"], 
sizeLimit: 3145728 
});
}

function loadphoto(id) {
  var rand_no = Math.random();
  var filename = 'loadphoto.php';
  filename = filename + "?id=" + id;
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
  document.getElementsByTagName("head")[0].appendChild(fileref)
	$(".app_bg").fadeIn();
	$(".app_box").slideDown("slow");
}

function loadInstructions() {
	document.getElementById("app_box").innerHTML = '<br /><center><div style="width:550px;text-align:center;"><h3>Instructions</h3><b>Nikeplus</b>:<ol><li>Just make sure to upload your runs to Nikeplus.com and the WRL will import your runs automatically</li></ol></div>';
	document.getElementById("app_box").innerHTML = document.getElementById("app_box").innerHTML + '<div style="width:100%;text-align:right;"><span onClick="closeApp();" style="cursor:pointer;">Close [X] &nbsp; &nbsp;</span><br /></div>';
	$(".app_bg").fadeIn();
	$(".app_box").slideDown("slow");
}

function friend(username) {
var dataString = 'username=' + username;
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/friend.php",
    data: dataString,
    success: function() {
    }
  });
}

function acceptfriend(username, id) {
var dataString = 'username=' + username + '&id=' + id;
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/acceptedfriend.php",
    data: dataString,
    success: function() {
    friend(username)
    $("#" + id).hide();
    }
  });
}

function declinefriend(username, id) {
var dataString = 'username=' + username + '&id=' + id;
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/acceptedfriend.php",
    data: dataString,
    success: function() {
    $("#" + id).hide();
    }
  });
}

function changefriend() {
var src = $(".friend").attr("src");
if (src == "images/friend.png" || src == "http://worldrunningleague.com/images/friend.png") {
$(".friend").attr("src", "images/unfriend.png")
}
else {
$(".friend").attr("src", "images/friend.png")
}
}

function profileimage(id) {
var dataString = 'id=' + id;
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/profileimage.php",
    data: dataString,
    success: function() {
    location.reload(true);
    }
  });
}

function deleteimage(id) {
var dataString = 'id=' + id;
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/deleteimage.php",
    data: dataString,
    success: function() {
    location.reload(true);
    }
  });
}




idleTimer = null;
idleState = false;
idleWait = 240000;


function loadjs(filename){
if (idleState == false) {
  var rand_no = Math.random();
  filename = filename + "&id=" + rand_no;
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
  document.getElementsByTagName("head")[0].appendChild(fileref)
}
setTimeout('loadjs("livechatcheck.php?type=normal")',5000);
}

function command_check() {
if (idleState == false) {
var rand_no = Math.random();
  filename = "commandprompt.php" + "?id=" + rand_no;
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
  document.getElementsByTagName("head")[0].appendChild(fileref)
}
setTimeout('command_check()',10000);
}

function typing(){
if (idleState == false) {
  var rand_no = Math.random();
  filename = "typing.php" + "?id=" + rand_no;
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
  document.getElementsByTagName("head")[0].appendChild(fileref)
}
setTimeout('typing()',2000);
}


 $(document).ready(function () {

        $('*').bind('mousemove keydown scroll', function () {

            clearTimeout(idleTimer);

            if (idleState == true) { 

                // Reactivated event
		$("#inactive").slideUp();
            }

            idleState = false;

            idleTimer = setTimeout(function () { 

                // Idle Event
		$("#inactive").slideDown();
                idleState = true; }, idleWait);
        });

        $("body").trigger("mousemove");

    });
    
    
function submitlivechat() {
var chat = $("#chat12").val();
var e = document.getElementById("to");
var to = e.options[e.selectedIndex].value;
var dataString = 'chat='+ chat + '&to=' + to;
	if(chat=='')
	{

	}
	else
	{
	var rand = Math.floor(Math.random()*10001)
		$.ajax({
		type: "POST",
		url: "livechatsubmit.php?rand=" + rand,
		data: dataString,
		success: function(){
		document.getElementById("chat12").value = "";
		var currentTime = new Date()
		var hours = currentTime.getHours()
		var minutes = currentTime.getMinutes()
		if (minutes < 10){
		minutes = "0" + minutes
		}
		if(hours > 11){
		var am = "PM";
		if (hours > 12) {
		hours = hours - 12;
		}
		} 
		else {
		var am = "AM";
		}
		var month = currentTime.getMonth() + 1;
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		var date = month + "." + day + "." + year + " " + hours + ":" + minutes + " " + am;
		document.getElementById("livechat").innerHTML = "<div style=\"border-bottom-style:solid;padding-top:7px;line-height:15px;paddding-bottom:7px;background-color:#A2BDFA\" onMouseOver=\"this.style.backgroundColor='#A2BDFA'\"><font size=3>" + chat + "</font><br /><font size=2 color=grey>me to " + to + "<br />" + date + "</font></div>" + document.getElementById("livechat").innerHTML;
		}
		});
	}
}

var localType;
var userTyping = false;

$(document).ready(function() {
	$("#chat12").bind("keypress", function(e)  {
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code) {
			typeWatch();
		}
	});
});

function typeWatch() {
	if (localType) {
	if (userTyping == true) {
	}
	else {
var e = document.getElementById("to");
var to = e.options[e.selectedIndex].value;
  var rand_no = Math.random();
  var filename = 'istyping.php?to=' + to;
  filename = filename + "&rand=" + rand_no;
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", filename);
  document.getElementsByTagName("head")[0].appendChild(fileref);
  }
  userTyping = true;
		clearTimeout(localType);
	}

	localType = setTimeout(function() {
	userTyping = false;
	}, 1000);
}

function set_sidebar() {
var dataString = 'type=sidebar';
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/setside.php",
    data: dataString,
    success: function() {
    }
  });
}

function set_livechat() {
var dataString = 'type=livechat';
  $.ajax({
    type: "POST",
    url: "http://worldrunningleague.com/setside.php",
    data: dataString,
    success: function() {
    }
  });
}

function nikeLogin() {
document.getElementById("app_box").innerHTML = '<br /><br /><center><div class="nike_error" style="margin-left:auto;margin-right:auto;width:65%;"></div><div style="display: inline;color:white;"><font color=white>Email: &nbsp;<input type="text" size="21" maxlength="45" class="nike_email" /><br /> Password: &nbsp;<input type="password" size="15" maxlength="35" class="nike_password" /><br /><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Login" onClick="nikeLoginSubmit();"/><br /><br /></font></div><br /><div style="margin-left:auto;margin-right:auto; width:65%;">**Note: We do <b>not</b> store any of your Nike login details anywhere. We only use them to find your Nike+ UserID (a long string of numbers).</div><br /><br /></center><br /><br />';
document.getElementById("app_box").innerHTML = document.getElementById("app_box").innerHTML + '<div style="width:100%;text-align:right;"><span onClick="closeApp();" style="cursor:pointer;">Close [X] &nbsp; &nbsp;</span><br /></div>';
$(".app_bg").fadeIn();
$(".app_box").slideDown("slow");
}

function nikeLoginAlternate() {
document.getElementById("app_box").innerHTML = '<br /><br /><center><div class="nike_error" style="margin-left:auto;margin-right:auto;width:65%;"></div><div style="display: inline;color:white;"><font color=white>Email: &nbsp;<input type="text" size="21" maxlength="45" class="nike_email" /><br /> Password: &nbsp;<input type="password" size="15" maxlength="35" class="nike_password" /><br /><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Login" onClick="nikeLoginSubmit(2);"/><br /><br /></font></div><br /><div style="margin-left:auto;margin-right:auto; width:65%;">**Note: We do <b>not</b> store any of your Nike login details anywhere. We only use them to find your Nike+ UserID (a long string of numbers).</div><br /><br /></center><br /><br />';
document.getElementById("app_box").innerHTML = document.getElementById("app_box").innerHTML + '<div style="width:100%;text-align:right;"><span onClick="closeApp();" style="cursor:pointer;">Close [X] &nbsp; &nbsp;</span><br /></div>';
$(".app_bg").fadeIn();
$(".app_box").slideDown("slow");
}

function nikeLoginSubmit(num) {
var email = $(".nike_email").val();
var password = $(".nike_password").val();

if (email > "" && password > "") {
var rand_no = Math.random();
if (num == 2) {
var filename = 'userid.php?email=' + email + '&password=' + password + "&type=alternate";

}
else {
var filename = 'userid.php?email=' + email + '&password=' + password;
}
filename = filename + "&rand=" + rand_no;
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
document.getElementsByTagName("head")[0].appendChild(fileref);
}
else {
$(".nike_error").html("<font color='red'>Please be sure to fill out the login form entirely!</font><br /><br />")
}

}

function loginFail() {
$(".nike_error").html("<font color='red'>Oops, it would appear that there is a problem with your login information. Please double-check your email and password and then re-submit. If you are positive that you have entered it correctly, <a href='http://worldrunningleague.com/support' target='_blank'>contact us</a> and we will attempt to fix the problem.</font><br /><br />")
}

function loginError() {
$(".nike_error").html("<font color='red'>Unfortunately a server error has occurred. Please let us know <a href='http://worldrunningleague.com/support' target='_blank'>here</a> and we will attempt to fix the problem.</font><br /><br />")
}

function loginSuccess(id, publica, screenName, type) {
if (type == "alternate") {
$(".nike_error").html("<font color='green'>Your Nike+ UserID is " + id + "</font><br /><br />")
}
else {
	if (publica == "true") {
	closeApp();
	$(".register_nike_id").html(id);
	$(".register_nike_uname").html(screenName);
	$(".username").val(screenName);
	$(".userid").val(id);
	}
	else {
	$(".nike_error").html("<font color='red'>It appears that your Nike+ profile is set to private. This means that the WRL is unable to access your run list. Please set your profile public by going <a href='http://nikerunning.nike.com/nikeos/p/nikeplus/en_US/plus/profile?page=privacy_settings' target='_blank'>here</a> and setting your profile to share more information. Make sure to click save at the bottom!</font><br /><br />")
	}
}
}

function replace(uname) {
	var answer = confirm("Are you sure you wish to have " + uname + " replaced? This action is final and cannot be changed! Please note that if you confirm the replacement, it may take up to 20 seconds for the page to load - please wait patiently during this time. Thanks!")
	if (answer){
		window.location = "http://worldrunningleague.com/replacement.php?username=" + uname;
	}
}

function amazon() {
document.getElementById("app_box").innerHTML = '<center>If you would like to show your appreciation for the World Running League, we encourage you to shop on Amazon using the search box below. Thanks!<br /><br /><div id="amazonad"></div><br /><br /><br />If you cannot see the search box above, please be sure to turn Adblock off. Thanks!</center>';
	document.getElementById("app_box").innerHTML = document.getElementById("app_box").innerHTML + '<div style="width:100%;text-align:right;"><span onClick="closeApp();" style="cursor:pointer;">Close [X] &nbsp; &nbsp;</span><br /></div>';
  filename = "http://ws.amazon.com/widgets/q?rt=tf_sw&ServiceVersion=20070822&MarketPlace=US&ID=V20070822/US/worlrunnleag-20/8002/9fc18461-2ac3-47a4-8842-93b8d9a890fb";
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
  document.getElementById("amazonad").appendChild(fileref)
	$(".app_bg").fadeIn();
	$(".app_box").slideDown("slow");
}

function playSound(soundfile) {
 document.getElementById("sound").innerHTML= "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}

function openCustomMileageApp() {
document.getElementById("app_box").innerHTML = '<br /><br /><center>Distance: <input type="text" size="3" class="distance"><br /><br />Miles <input type="radio" name="distance_type" value="mi"><br />Kilometers <input type="radio" name="distance_type" value="km"><br /><br />Hours <input type="text" size="3" class="hours"><br />Minutes <input type="text" size="3" class="minutes"><br /><br />Start Time</center><br /><br />';
document.getElementById("app_box").innerHTML = document.getElementById("app_box").innerHTML + '<div style="width:100%;text-align:right;"><span onClick="closeApp();" style="cursor:pointer;">Close [X] &nbsp; &nbsp;</span><br /></div>';
$(".app_bg").fadeIn();
$(".app_box").slideDown("slow");
}
