//////////////////////////////////////////////
// Javascript scripts for Onenightcup website
// by Dieter Vanden Eynde
// www.unrated.be
//////////////////////////////////////////////

///////////////
// BOOT
///////////////
if(document.getElementById) window.onload = load;

function load(){
  ONC.gameSelect();
  ONC.charactersLeft("sig","charsleft",250,"Signature too long");
  ONC.charactersLeft("shoutmsg","charsLeft",150,"Shout too long");
  ONC.charactersLeft("comment","charsLeft",150,"Comment too long");
}
//namespace
var ONC = new Object();

//GAME SELECT FORM
//redirects on select
ONC.gameSelect = function(){
  var form = document.getElementById("game_select");
  
  if(form != null){  
	  form.onchange = function(){

	    if(form.options[form.selectedIndex].value != ''){
	      window.location.href = 'competition/'+form.options[form.selectedIndex].value;
	    }
	  }
  }	 
}

ONC.charactersLeft = function(form,span,maxChars,errorMsg){

  var sig = document.getElementById(form); 
  var msg = document.getElementById(span);
   
  if(sig != null && msg != null){
  
    msg.innerHTML = maxChars - sig.value.length + " characters left";

    sig.onkeyup = function(){
    
    	if(sig.value.length > maxChars){
    		msg.innerHTML = errorMsg;
    		msg.style.color = "red";
    	}else{
    		msg.innerHTML = maxChars - sig.value.length + " characters left";
    		msg.style.color = null;
    	}      
    }
  }
}

ONC.friend = 0;
function friendControl(id,action,linkId){
  ONC.friend = linkId;
  http = ONC.getHTTPObject();
  var url = ONC.url+"ajax/friendrequest.php";
  var pars = "id="+id+"&act="+action;
   
  http.open("POST", url, true);
  http.onreadystatechange = ONC.friendshipOnStateChange;
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
  http.send(pars);
}

ONC.friendshipOnStateChange = function(){
  var button = document.getElementById(ONC.friend);

  if(button != null){
	  if(http.readyState == 4 && http.status == 200) {
	    button.innerHTML = http.responseText;
	    button.onclick = "alert('lol');";
	  }
  }
}

/*********************
 * AJAX 
 *********************/
ONC.getHTTPObject = function(){
  var xmlhttp;
 
  /*@cc_on
 
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(E){
      xmlhttp = false;
    }
  }
  @else
    xmlhttp = false;
  @end @*/
 
  if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    try {
      xmlhttp = new XMLHttpRequest();
    }catch(e){
      xmlhttp = false;
    }
  }
 
  return xmlhttp;
}