
      
      var http = getHTTPObject(); // We create the HTTP Object
var NORMAL_STATE = 4;

function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function getHTTPObject() {
	var http_object;

	// MSIE Proprietary method

	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			http_object = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E) {
				http_object = false;
			}
		}
	@else
		xmlhttp = http_object;
	@end @*/


	// Mozilla and others method

	if (!http_object && typeof XMLHttpRequest != 'undefined') {
		try {
			http_object = new XMLHttpRequest();
		}
		catch (e) {
			http_object = false;
		}
	}

	return http_object;
}

function loadingDiv(div, message)
{
  thediv = document.getElementById(div);
  thediv.innerHTML = '<p>Now Loading <br /> <img src="/images/ajaxLoading1.gif" alt="Loading" /> </p><p>'+message+'</p>';
}

// check to make sure that the browser can
// handle window.addEventListener
if (window.addEventListener) {
    // create the keys and konami variables
    var keys = [],
        konami = "38,38,40,40,37,39,37,39,66,65";
 
    // bind the keydown event to the Konami function
    window.addEventListener("keydown", function(e){
        // push the keycode to the 'keys' array
        keys.push(e.keyCode);
 
        // and check to see if the user has entered
        // the Konami code
        if (keys.toString().indexOf(konami) >= 0) {
            // do something such as:
            // alert('Konami');
            document.location = 'http://wiki.the-big-bang-theory.com/wiki/Vintage_Game_Night';
            // and finally clean up the keys array
            keys = [];
        };
    }, true);
};
      
      function pollVote()
{
  var rads=document.getElementsByName('op');
  for(var j=0;j<rads.length;j++){
  if(rads[j].checked == true){
    option = rads[j].value;
  }
  }
  pollid = document.getElementById('pollid').value;
  showTitle = document.getElementById('showTitle').value;
  pars = 'op='+option+'&pollid='+pollid+'&showtitle='+showTitle;
  
  //Show we are doing something;
  loadingDiv('pollarea', 'We are submitting your vote');
       
  http.open('POST', '/?act=polls&action=quickvote', true);
  		
  //Send the proper header information along with the request
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", pars.length);
  http.setRequestHeader("Connection", "close");
  
  http.onreadystatechange = pollVoted;
  http.send(pars);
}

function pollVoted()
{
  if (http.readyState == NORMAL_STATE) {
	document.getElementById('pollarea').innerHTML =  http.responseText;
	if(document.getElementById('submitbutton'))
	 document.getElementById('submitbutton').style.display = 'none';
  document.getElementById('srlink').style.display = 'none';  	 
 }	
}

function newPoll()
{
  //Get current poll;
  pollid = document.getElementById('pollid').value;

  //Show we are doing something;
  loadingDiv('pollarea', 'We are fetching a new poll');

  //Start the AJAX;
  http.open('GET', '/?act=polls&action=quickpoll&pollid='+pollid, true);  
  http.onreadystatechange = pollChange;
  http.send(null);
}

function pollChange()
{
  if (http.readyState == NORMAL_STATE) {
    pollcon = http.responseText.split('|#|');
    document.getElementById('pollid').value = pollcon[0];
  	document.getElementById('pollarea').innerHTML =  pollcon[1];
  	document.getElementById('submitbutton').style.display = 'block';
  	document.getElementById('srlink').style.display = 'inline';  	
  }	
}

function showResults()
{
  //Get current poll;
  pollid = document.getElementById('pollid').value;

  //Show we are doing something;
  loadingDiv('pollarea', 'We are fetching the results');

  //Start the AJAX;
  http.open('GET', '/?act=polls&action=quickresults&pollid='+pollid, true);  
  http.onreadystatechange = pollResults;
  http.send(null);
}

function pollResults()
{
  if (http.readyState == NORMAL_STATE) {
    pollcon = http.responseText;
  	document.getElementById('pollarea').innerHTML =  pollcon;
  	document.getElementById('submitbutton').style.display = 'none';
  	document.getElementById('srlink').style.display = 'none';  	
  }	
}
      
      function mathCheck()
{
    m = parseInt(m1) + parseInt(m2);
    v = parseInt(document.getElementById('Math'+key).value);
      
    if(v == m)
    {
      document.getElementById('MathResult').innerHTML = '<img src="images/tick.png" alt="Success" />';
      document.getElementById('Math'+key).style.backgroundColor = '#EEF3E2';
      document.getElementById('Math'+key).style.border = '1px solid #556B2F';
      return true;
    } else {
      document.getElementById('MathResult').innerHTML = '<img src="images/cross.png" alt="Failure" />';
      document.getElementById('Math'+key).style.backgroundColor = '#FEF0C9';
      document.getElementById('Math'+key).style.border = '1px solid #A52A2A';
                  
      return false;
    }   
}
    
function commentSubmit()
{  
  //Security Check;
  if(!mcAuth){
    if(!mathCheck())
    {
      alert('You must answer the maths question correctly.');
      return false;
    }
  }

  //Get Details;
  numero = escape(document.getElementById('Numero').value);
  SID = escape(document.getElementById('SID'+numero).value);
  Name = escape(document.getElementById('Name'+numero).value);
  Comment = escape(document.getElementById('Comment'+numero).value);
  Camelot = escape(document.getElementById('Camelot'+numero).value);
  FormKey = escape(document.getElementById('FormKey').value);
  GlobalKey = escape(document.getElementById('GlobalKey').value);
  DefigoSecuritas = escape(document.getElementById('DefigoSecuritas').value);
  Math = escape(document.getElementById('Math'+numero).value);
  
  //Show we are doing something;
  toggleLayer('incon-f');
  //toggleLayer('incon-m');
  loadingDiv('incon-m', 'Please wait while we process your comment.');
  
  //String it together;
  pars = 'Numero='+numero+'&id='+SID+'&Name'+numero+'='+Name+'&Comment'+numero+'='+Comment+'&Camelot'+numero+'='+Camelot+'&FormKey='+FormKey+'&GlobalKey='+GlobalKey+'&DefigoSecuritas='+DefigoSecuritas+'&mcAuth='+mcAuth+'&Math'+numero+'='+Math+'&Submit=TRUE&Ban=2';
  
  http.open('POST', url+SID, true);
  		
  //Send the proper header information along with the request
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", pars.length);
  http.setRequestHeader("Connection", "close");
  
  http.onreadystatechange = function(){
      if(http.readyState == NORMAL_STATE) commentReact();
    }
  http.send(pars);
}

function commentReact(inline)
{
  if(http.readyState == NORMAL_STATE)
  {
    Error = http.responseText.split('|');
    
    if(Error[0] == 'F')
    {
      //Problem;
      document.getElementById('incon-f').style.display = 'block';
      document.getElementById('incon-m').innerHTML = '<p class="wrowc">An error occured: '+Error[1]+'</p>';
    } else {
      //Success;
      document.getElementById('incon-m').innerHTML = '<p>'+Error[1]+'</p>';
    }
	}
}

function moreComments()
{
  if(!cmld || position < tCount)
  {
    //Show we're doing something;
  	document.getElementById('mcoml').style.display = 'block';
    loadingDiv('mcoml', 'We are loading the comments. Please wait.');
  	document.getElementById('morecom').style.display = 'none';  	
  	
    //Load Comments;
    http.open('GET', mcr+SID+'&position='+position, true);  
    http.onreadystatechange = function(){
        if(http.readyState == NORMAL_STATE) gotComments();
      }
    http.send(null);
  } else {
  	document.getElementById('fewercom').style.display = 'block';
  	document.getElementById('mcoml').style.display = 'none';
    document.getElementById('mcomsp').style.display = 'block';
    document.getElementById('morecom').style.display = 'none';
  
  }

}

function fewerComments()
{
  document.getElementById('mcomsp').style.display = 'none';
  document.getElementById('fewercom').style.display = 'none';
  document.getElementById('morecom').style.display = 'block';
}

function gotComments()
{
  if (http.readyState == NORMAL_STATE) {  
    resp = http.responseText.split('|');
    outcome = resp[0];
    
    if(outcome == 'S')
    {
      position = resp[1];
      coms = resp[2];
    	document.getElementById('mcomsp').innerHTML +=  coms;
    	document.getElementById('fewercom').style.display = 'block';
    	document.getElementById('morecom').style.display = 'none';
    	document.getElementById('mcoml').style.display = 'none';
      document.getElementById('mcomsp').style.display = 'block';      
      cmld = true;
      
      if(position >= tCount)
        document.getElementById('morecom').style.display = 'none';
      else
        document.getElementById('morecom').style.display = 'block';       
      
    } else {
      alert(resp[1]);
    }      	
  }	
}

function commentReply(to, id, key)
{
  tb = document.getElementById('Comment'+key);
  tb.value = '@'+to+', '+tb.value;
  tb.focus();
}
      
      
// variables
var messages = '';

function action(area, action, ID, obj)
{
  //Message Box Confirmation;
  obj.parentNode.innerHTML = '<span id="'+ID+'oc" style="display: none;">'+obj.parentNode.innerHTML+'</span><span id="'+ID+'dl"><a onclick="action2(\''+area+'\', \''+action+'\', \''+ID+'\', this)">Are you sure you want to '+action+' this? <img src="/images/accept.png" alt="Accept" /></a>&nbsp;/&nbsp;<a onclick="revert(\''+ID+'\', this);"><img src="/images/delete.png" alt="Cancel" /></a></span>';      
}

function revert(ID, obj)
{
  obj.parentNode.innerHTML = document.getElementById(ID+'oc').innerHTML;
}

function action2(area, action, ID, obj)
{
  	if(SEC_KEY == '')
  	{
  	 document.getElementById("message").innerHTML = "Security Key not found.";
  	 return;
  	}
  	
    random_num = (Math.round((Math.random()*9)+1));
    
  	// open the http connection
  	http.open('GET', '/?act=admin&action='+area+'&sub='+action+'&method=ajax&id='+ID+'&num='+random_num+'&hash='+SEC_KEY, true);
  		
  	// where to go
  	http.onreadystatechange = function(){
      if(http.readyState == 4) handleACTResponse(action, ID);
    }
  	http.send(null);
}

function handleACTResponse(action, ID)
{
	// did the connection work?
	if (http.readyState == NORMAL_STATE) {
		// split by the pipe
		results = http.responseText.split('|');
    con = document.getElementById(ID+'dl');
    if(results[0] == 'TRUE')
    {
      con.innerHTML = '<img src="/images/tick.png" alt="Success" /> The action was successful. <img src="/images/clock_red.png" alt="Will disappear shortly" />';
  
    } else {
      con.innerHTML = '<img src="/images/cross.png" alt="Failure" /> The action failed. <img src="/images/clock_red.png" alt="Will disappear shortly" />';
    }
      if(document.getElementById('hide'+results[2]))
        window.setTimeout("toggleLayer('hide'+results[2])", 1500);    
	}
}

function revealMessage()
{
  toggleLayer('message-box');
}