SITEURL = "http://"+window.location.hostname;

// BASIC AJAX HANDLER
function GetXmlHttpObject(handler){ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0){
		xmlHttp=new XMLHttpRequest();
		xmlHttp.onload=handler
		xmlHttp.onerror=handler
		return xmlHttp;
	}
	if (navigator.userAgent.indexOf("MSIE")>=0){ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP"
		} try	{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} catch(e) { 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0){
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}

function ShowHide(){
  // CROSS-BROWSER, GENERIC
  // Hide or show the object - the method used depends on the user's browser.

  // First parameter: name of object to show/hide
  // Second parameter: whether to hide, show or reverse the current status of the object.
  //   Values are: 'hidden', 'visible', 'reverse'
  //   Second param is optional if a third parameter is not supplied. When left out, the
  //   second param defaults to 'reverse'
  // Third parameter: if to affect surrounding content - 'reflow' or 'fixed'
  //   Defaults to 'reflow'
  //   When 'reflow', content below the object moves up or down depending on whether
  //   the object is visible or not. This uses (for W3C) the display style property.
  //   When 'fixed', showing or hiding the object does not affect other parts of the
  //   page. This uses (for W3C) the visibility style propery.
  
  // E.g. ShowHide('divLayer1') or ShowHide('divLayer1','visible') or
  // ShowHide('divLayer1','reverse','chkBox1')

  // Get arguments
  Args = ShowHide.arguments;

  // If the first argument doesn't exist, leave function
  if(Args.length>0) Args = new Array(Args[0], Args[1], Args[2]);
  else return false;

	//alert(Args[0]+","+Args[1]+","+Args[2]);
  switch (Args[1])
  {
    case 'hidden':
      // If the second argument is 'hidden', hide the object using the method appropriate to the browser
      if(Args[2] == 'fixed')
      {
      // Use the visibility property
        if(document.getElementById) document.getElementById(Args[0]).style.visibility = 'hidden';
        else if(document.all[Args[0]]) document.all[Args[0]].style.visibility = "hidden";
        else if(document.layers) document.layers[Args[0]].visibility = "hide";
        else return false;
				
				return true;
      }
      else
      {  
        // Use the display property 
        if(document.getElementById) {document.getElementById(Args[0]).style.display = 'none';}
        else if(document.all[Args[0]]) document.all[Args[0]].style.display = "none";
        else if(document.layers) document.layers[Args[0]].visibility = "hide";
        else return false;
				
				return true;
      }
      break;
    case 'visible':
      // If the second argument is 'visible', hide the object using the method appropriate to the browser
      if(Args[2] == 'fixed')
      {  
        if(document.getElementById) document.getElementById(Args[0]).style.visibility = 'visible';
        else if(document.all[Args[0]]) document.all[Args[0]].style.visibility = "visible";
        else if(document.layers) document.layers[Args[0]].visibility = "show";
        else return false;
				
				return true;
      }
      else
      {
        if(document.getElementById) document.getElementById(Args[0]).style.display = 'block';
        else if(document.all[Args[0]]) document.all[Args[0]].style.display = "block";
        else if(document.layers) document.layers[Args[0]].visibility = "show";
        else return false;    
				
				return true;
      }
      break;
    default:
      // Else if the object is current visible set to hidden else set to visible
   
      if(document.getElementById)
      {
        // Use getElementByID method for IE5+ and NS6+ (W3C standard)
        if(Args[2] == 'fixed')
        {
          if(document.getElementById(Args[0]).style.visibility == 'visible') document.getElementById(Args[0]).style.visibility = 'hidden';
          else document.getElementById(Args[0]).style.visibility = 'visible';
					
					return true;
        }
        else
        {
          if(document.getElementById(Args[0]).style.display == 'block') document.getElementById(Args[0]).style.display = 'none';
          else document.getElementById(Args[0]).style.display = 'block';
        
					return true;
				}

      }
      else 
      {
        if(document.all[Args[0]])
        {
          if(Args[2] == 'fixed')
          {
            // Use document.all method for older IE
            if(document.all[Args[0]].style.visibility == 'visible') document.all[Args[0]].style.visibility = 'hidden';
            else document.all[Args[0]].style.visibility = 'visible';
						return true;
          }
          else
          {
            if(document.all[Args[0]].style.display == 'block') document.all[Args[0]].style.display = 'none';
            else document.all[Args[0]].style.display = 'block';    
						return true;
          }
        }
        else 
        {
          // Try to use NS4 layers
          if(document.layers[Args[0]])
          {
            if(document.layers[Args[0]].visibility = "show") document.layers[Args[0]].visibility = "hide";
            else document.layers[Args[0]].visibility = "show";
						return true;
          }
          else
          {
            // Give up
            return false;
          }  // give up
        }  // NS
      }  // IE4
    }
		return 654654654654;// end of Switch
}

function check_username(){
	var username = document.getElementById('username').value;
	var url=SITEURL+"/libs/ajax/ajax_check_username.php?username="+username;
	xmlHttp=GetXmlHttpObject(check_username_helper);//fails on safari 1
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}
function check_username_helper() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		var return_text = xmlHttp.responseText;
		//ok got the text back
		return_obj = document.getElementById('username_check');
		return_obj.innerHTML = return_text;
		
		
	} 
}

function insertquote(code,commentbox_id) {
//alert(commentbox_id);


 var textarea = document.getElementById(commentbox_id);
 var scrollPos = textarea.scrollTop;
 

 
var open = "[quote]";
 

 
	// our close tag
 var close = "[/quote]";
 
 var text = open+code+close;

		if(document.selection) { 
				textarea.focus(); 
				var orig = textarea.value.replace(/\r\n/g, "\n"); 
				var range = document.selection.createRange(); 
 
				if(range.parentElement() != textarea) { 
						return false; 
				} 
 
				range.text = text; 
				 
				var actual = tmp = textarea.value.replace(/\r\n/g, "\n"); 

				for(var diff = 0; diff < orig.length; diff++) { 
						if(orig.charAt(diff) != actual.charAt(diff)) break; 
				} 
 
				for(var index = 0, start = 0;  
						tmp.match(text)  
								&& (tmp = tmp.replace(text, ""))  
								&& index <= diff;  
						index = start + text.length 
				) { 
						start = actual.indexOf(text, index); 
				} 
		} 
		else if(textarea.selectionStart) { 
				var start = textarea.selectionStart; 
				var end   = textarea.selectionEnd; 
 
				textarea.value = textarea.value.substr(0, start)  
						+ text  
						+ textarea.value.substr(end, textarea.value.length); 
		} 
		 
	if(start != null) { 
			 setCaretTo(textarea, start + text.length); 
	} 
	else { 
		 textarea.value += text; 
	}
	var textarea = document.getElementById(commentbox_id);
	caretPos(textarea,scrollPos);
	parent.location='#addcomment';		
}

function insertcode(tag,text_field) {
    	var txt = document.getElementById(text_field);


        	if(document.selection) {
        		txt.focus();
        		sel = document.selection.createRange();
        		sel.text = '[' + tag + ']' + sel.text + '[/' + tag + ']';
        	} else if(txt.selectionStart || txt.selectionStart == '0') {	
        		txt.value = (txt.value).substring(0, txt.selectionStart) + "["+tag+"]" + (txt.value).substring(txt.selectionStart, txt.selectionEnd) + "[/"+tag+"]" + (txt.value).substring(txt.selectionEnd, txt.textLength);


            	} else {
            		txt.value = '[' + tag + '][/' + tag + ']';
            	}
            	return;
        }

function insertsmiley(tag,text_field){
		var txt = document.getElementById(text_field);


        	if(document.selection) {
        		txt.focus();
        		sel = document.selection.createRange();
        		sel.text = '' + tag + '' + sel.text;
        	} else if(txt.selectionStart || txt.selectionStart == '0') {	
        		txt.value = (txt.value).substring(0, txt.selectionStart) + ""+tag+"" + (txt.value).substring(txt.selectionStart, txt.selectionEnd) +  (txt.value).substring(txt.selectionEnd, txt.textLength);


            	} else {
            		txt.value =  tag;
            	}
            	return;
}

function insertlink(text_field)
{
    // our textfield
    var textarea = document.getElementById(text_field);
		var scrollPos = textarea.scrollTop;
   // our open tag
   

    if(!textarea.setSelectionRange)
    {
        //var selected = document.selection.createRange().text;
				var selected="";
				if (window.getSelection)
				{
					selected = window.getSelection();
					
				}
				else if (document.getSelection)
				{
					selected = document.getSelection();
				
				}
				else if (document.selection)
				{
					selected = document.selection.createRange().text;
					
				}
				
				
				
				
        if(selected.length <= 0)
        {
            // no text was selected so prompt the user for some text
						var url = prompt("Please enter the url", "http://");
						if(url!=null) {
   
							var open = "[link=" + url + "]";

				    // our close tag
							var close = "[/link]";
						
						
							textarea.value += open + "link"+ close;
						}
						//alert(textarea.value+"here");
        }
        else
        {
				    var text_box_text=document.getElementById(text_field).value;
				    if(selected!=null)
						{
								if(text_box_text.indexOf(selected)!=-1)
								{	    
            // put the code around the selected text
										var url = prompt("Please enter the url", "http://");
										
										if(url!=null) {
   
									    var open = "[link=" + url + "]";

									    // our close tag
									    var close = "[/link]";
							
							
											document.selection.createRange().text = open + selected + close;
										}
										//alert(document.selection.createRange().text+"here1");
								}
						}
        }

    }
    else
    {
       
        var url = prompt("Please enter the url", "http://");
				if(url!=null) {
   
			    var open = "[link=" + url + "]";

			    // our close tag
			    var close = "[/link]";


				 // the text before the selection
	        var pretext = textarea.value.substring(0, textarea.selectionStart);

	        // the selected text with tags before and after
	        var codetext = open + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + close;

	        // the text after the selection
	        var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);

	        // check if there was a selection
	        if(codetext == open + close)
	        {
	            //prompt the user
	            codetext = open + "link"+ close;
	        }
					//alert(codetext);
	        // update the text field
	        if(codetext == "") {
						codetext = "link";
					}
					textarea.value = pretext + codetext + posttext;
				}
				
				
				//alert(textarea.value+"here2");
    }
		var textarea = document.getElementById(text_field);
		caretPos(textarea,scrollPos);
    // set the focus on the text field
    textarea.focus();
}

function insertimage(text_field)
{
    // our textfield
    var textarea = document.getElementById(text_field);
		var scrollPos = textarea.scrollTop;
   // our open tag
   

    if(!textarea.setSelectionRange)
    {
        //var selected = document.selection.createRange().text;
				var selected="";
				if (window.getSelection)
				{
					selected = window.getSelection();
					
				}
				else if (document.getSelection)
				{
					selected = document.getSelection();
				
				}
				else if (document.selection)
				{
					selected = document.selection.createRange().text;
					
				}
				
				
				
				
        if(selected.length <= 0)
        {
            // no text was selected so prompt the user for some text
						var url = prompt("Please enter the image url", "http://");
						if(url!=null) {
   
							var open = "[img=" + url + "]";

						
						
							textarea.value += open;
						}
						//alert(textarea.value+"here");
        }
        else
        {
				    var text_box_text=document.getElementById(text_field).value;
				    if(selected!=null)
						{
								if(text_box_text.indexOf(selected)!=-1)
								{	    
            // put the code around the selected text
										var url = prompt("Please enter the image url", "http://");
										
										if(url!=null) {
   
									    var open = "[img=" + url + "]";

							
							
											document.selection.createRange().text = open;
										}
										//alert(document.selection.createRange().text+"here1");
								}
						}
        }

    }
    else
    {
       
        var url = prompt("Please enter the image url", "http://");
				if(url!=null) {
   
			    var open = "[img=" + url + "]";



				 // the text before the selection
	        var pretext = textarea.value.substring(0, textarea.selectionStart);

	        // the selected text with tags before and after
	        var codetext = open;

	        // the text after the selection
	        var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);

	        // check if there was a selection
	        if(codetext == open)
	        {
	            //prompt the user
	            codetext = open;
	        }
					//alert(codetext);
	        // update the text field
	        if(codetext == "") {
						codetext = "";
					}
					textarea.value = pretext + codetext + posttext;
				}
				
				
				//alert(textarea.value+"here2");
    }
		var textarea = document.getElementById(text_field);
		caretPos(textarea,scrollPos);
    // set the focus on the text field
    textarea.focus();
}
function address_book_user(user_id) {
	var url=SITEURL+"/libs/ajax/ajax_address_book_add.php?user_id="+user_id;
	xmlHttp=GetXmlHttpObject(change_address_book_user);//fails on safari 1
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}
function change_address_book_user() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		the_div = document.getElementById("address_book_link");
		the_div.innerHTML = xmlHttp.responseText;
	} 
}

function faq_mark_helpful(faq_id,vote) {
	//alert("here");
	var url=SITEURL+"/libs/ajax/ajax_faq_helpful.php?faq_question_id="+faq_id+"&vote="+vote;
	xmlHttp=GetXmlHttpObject(change_faq_mark_helpful);//fails on safari 1
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);

}


function change_faq_mark_helpful() { 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		
		var return_text = xmlHttp.responseText;
		
		var return_text2 = return_text.split(",");

		
		comm_id = return_text2[0];

		
		the_div = document.getElementById("helpful_"+comm_id);
		the_div.innerHTML = return_text2[1];
		
		the_div.onclick=function() {return false};
		the_div.style.textDecoration="none";
		the_div.style.cursor="default";
	} 
}


function edit_comment(comment_id,comment_status){
	var link = document.getElementById("edit_"+comment_id).innerHTML;
	document.getElementById("delete_comment_"+comment_id).innerHTML = "";
	if(link == "Edit"){
		document.getElementById("edit_"+comment_id).innerHTML = "Hide";
		document.getElementById("edit_comment_"+comment_id).style.display='block';
		if(comment_status == 0){
			document.getElementById("delete_"+comment_id).innerHTML = "Un-Delete";
		}	
		else{
			document.getElementById("delete_"+comment_id).innerHTML = "Delete";
		}
	}
	else if(comment_status == 0){
		document.getElementById("delete_"+comment_id).innerHTML = "Un-Delete";
		document.getElementById("edit_"+comment_id).innerHTML = "Edit";
		document.getElementById("edit_comment_"+comment_id).style.display='none';
	}
	else{
		document.getElementById("edit_"+comment_id).innerHTML = "Edit";
		document.getElementById("delete_"+comment_id).innerHTML = "Delete";
		document.getElementById("edit_comment_"+comment_id).style.display='none';
	}	
}

function delete_comment(comment_id,comment_status){
	var link = document.getElementById("delete_"+comment_id).innerHTML;
	document.getElementById("edit_comment_"+comment_id).style.display='none';
	if(link == "Delete"){
		document.getElementById("delete_"+comment_id).innerHTML = "Hide";
		document.getElementById("edit_"+comment_id).innerHTML = "Edit";
		document.getElementById("delete_comment_"+comment_id).innerHTML = '<div style="margin-top: 10px;"><form action="" method="POST"><strong>Delete Comment</strong><p>Are you sure you wish to delete this comment?</p><input type="button" value="No Thanks" name="action" onclick="delete_comment('+comment_id+');" ><input type="submit" value="Delete Comment" name="action"><input type="hidden" value="'+comment_id+'" name="comment_id"></form></div>';
		
	}
	else if(link == "Un-Delete"){
	
		document.getElementById("delete_"+comment_id).innerHTML = "Hide";
		document.getElementById("edit_"+comment_id).innerHTML = "Edit";
		document.getElementById("delete_comment_"+comment_id).innerHTML = '<div style="margin-top: 10px;"><form action="" method="POST"><strong>Un-Delete Comment</strong><p>Are you sure you wish to un-delete this comment?</p><input type="button" value="No Thanks" name="action" onclick="delete_comment('+comment_id+','+comment_status+');" ><input type="submit" value="Un-Delete Comment" name="action"><input type="hidden" value="'+comment_id+'" name="comment_id"></form></div>';
	}
	else if(comment_status == 0){
		document.getElementById("delete_"+comment_id).innerHTML = "Un-Delete";
		document.getElementById("edit_"+comment_id).innerHTML = "Edit";
		document.getElementById("delete_comment_"+comment_id).innerHTML = "";
	}
	else{
		document.getElementById("delete_"+comment_id).innerHTML = "Delete";
		document.getElementById("edit_"+comment_id).innerHTML = "Edit";
		document.getElementById("delete_comment_"+comment_id).innerHTML = "";
	}	
}

ajax_done_puzzle = 0;
var global_diff = 0;
var global_puzzle_id = 0;
var global_gamer_id = 0;
var global_difficulty = 0;
var global_score = 0;


function save_puzzle_time(puzzle_id,game_id,difficulty,score) {
	save_puzzle_time_record(puzzle_id,game_id,difficulty,score);
	//refresh_puzzle_scores('scores_'+difficulty,puzzle_id,game_id,difficulty);
	
	 global_diff = difficulty;
	 global_puzzle_id = puzzle_id;
	 global_gamer_id = game_id;
	 global_difficulty = difficulty;
	 //global_score = 0;
}
/*
function save_puzzle_time_record(puzzle_id,game_id,difficulty,score) {
	
	var url= SITEURL+"/libs/ajax/ajax_save_puzzle_score.php";
	//alert(url);
	var params = "puzzle_id="+puzzle_id+"&game_id="+game_id+"&difficulty="+difficulty+"&score="+score;
	
	var xmlHttp=postAJAX(url,params,display_puzzle_time);//fails on safari 1	
	//xmlHttp.open("POST", url , true);
	
	
	//xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//xmlHttp.setRequestHeader("Content-length", params.length);
	//xmlHttp.setRequestHeader("Connection", "close");
	
	
	//xmlHttp.send(params);
	
	/*
		xmlHttp10=GetXmlHttpObject(chat_message_display);//fails on safari 1
		xmlHttp10.open("POST", url , true);
		xmlHttp10.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp10.setRequestHeader("Content-length",params.length );
		xmlHttp10.setRequestHeader("Connection", "close");
		//xmlHttp10.onreadystatechange = send_chat_message_display;
		xmlHttp10.send(params);
	
}
*/

function save_puzzle_time_record(puzzle_id,game_id,difficulty,score) {
	
	var d = new Date();
	var time = d.getTime();
	var url= SITEURL+"/libs/ajax/ajax_save_puzzle_score.php?timestamp="+time;
	
	// using prototype to do this call as they probably know more about ajax then we do
	/*new Ajax.Request(url,{
		parameters: {puzzle_id: puzzle_id, game_id: game_id, difficulty: difficulty,score: score},
		onSuccess:display_puzzle_time
	});*/
	
	var params_score_params = "puzzle_id="+puzzle_id+"&game_id="+game_id+"&difficulty="+difficulty+"&score="+score;
	xmlHttp_game_score=GetXmlHttpObject(display_puzzle_time);//fails on safari 1
	xmlHttp_game_score.open("POST", url , true);
	
	
	xmlHttp_game_score.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp_game_score.setRequestHeader("Content-length", params_score_params.length);
	xmlHttp_game_score.setRequestHeader("Connection", "close");
	xmlHttp_game_score.send(params_score_params);
}
function display_puzzle_time(results) {
	if (xmlHttp_game_score.readyState==4 || xmlHttp_game_score.readyState=="complete"){ 
		var return_text = xmlHttp_game_score.responseText;
		document.getElementById("display_area").innerHTML = return_text;
		ajax_done_puzzle = 1;
		refresh_puzzle_scores(global_puzzle_id,global_gamer_id,global_difficulty);
	}
	
	
	
}


function refresh_puzzle_scores(puzzle_id,game_id,difficulty) {
	var url=SITEURL+"/libs/ajax/ajax_refresh_puzzle_scores.php?puzzle_id="+puzzle_id+"&game_id="+game_id+"&difficulty="+difficulty;
	//alert(url);

	xmlHttp2=GetXmlHttpObject(refresh_puzzle_scores_helper);//fails on safari 1
	xmlHttp2.open("GET", url , true);
	xmlHttp2.send(null);
}
function refresh_puzzle_scores_helper() {
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){ 
		var return_text2 = xmlHttp2.responseText;
		var return_text3 = return_text2.split("[SPLIT]");
		//var  = return_text3[0];
		//alert(div);
		// 0 = daily 1=weekly 2=overall 3=personal
		
		//var the_html = return_text3[1];
		document.getElementById('scores_daily').innerHTML = return_text3[0];
		document.getElementById('scores_weekly').innerHTML = return_text3[1];
		document.getElementById('scores_overall').innerHTML = return_text3[2];
		if(return_text3[3]!="") {
			document.getElementById('scores_personal').innerHTML = return_text3[3];
		}
		
		//alert(the_html);
		//document.getElementById(div).innerHTML = the_html;
	} 
}

function top_100_tab(tab){
	if(tab == 'points'){
		ShowHide('top_100_coins','hidden');
		ShowHide('top_100_points','visible');
		document.getElementById('top_100_points_tab').className = 'current';
		document.getElementById('top_100_coins_tab').className = '';
	}
	else if(tab == 'coins'){
		ShowHide('top_100_coins','visible');
		ShowHide('top_100_points','hidden');
		document.getElementById('top_100_points_tab').className = '';
		document.getElementById('top_100_coins_tab').className = 'current';
	}
}


function testlength(textarea,display_message,number_of_chars)		{
	var ele=document.getElementById(textarea);
	var ele2=document.getElementById(display_message);
	var str=ele.value;
	if(str.length>=number_of_chars) {
		ele.value=str.substring(0,number_of_chars);
	}
	ele2.innerHTML="Number of Characters Left : "+(number_of_chars-str.length);
}

var chat_val = '';

function return_send_chat_message(e,chat_box_id,object_type,object_id) {
	ok=false;
	if(window.event) // IE
  {
		keynum = e.keyCode;
		ok=true;
  }
	else if(e.which) // Netscape/Firefox/Opera
  {
		keynum = e.which;
		ok=true;
  }
	if(ok==true) {
		//alert(keynum);
		if(keynum==13&&e.shiftKey==false&&document.getElementById(chat_box_id).value!='') {
			send_chat_message(chat_box_id,object_type,object_id);
			return false;
		}
		else if(keynum==13&&e.shiftKey==true) {
			//alert("here");
			//check previous chars
			var ele_text = document.getElementById(chat_box_id);
			//text_val = ele_text.value.substr(ele_text.value.length-2,2);
		
			
			ele_text.value = 	ele_text.value +'\r\n';
			//alert(ele_text.value);
			//caretPos(ele_text,ele_text.scrollTop);
			setCaretPosition(ele_text,ele_text.value.length);
			return false;
		}
	}
	return true;
}

function send_chat_message(chat_message_id,object_type,object_id) {
	var id = document.getElementById(chat_message_id);
	chat_val = id.value;
	//convert the val /r to br
	chat_val = new String(chat_val);
	//char_val = nl2br(chat_val);
	//newstring=chat_val.replace(/\r/g,"").replace(/\n/g,"<br />"); 
	//alert(chat_val.search("\n"));
	//chat_val = newstring;
	document.getElementById(chat_message_id).value = '';
	testlength('chat_box','chat_box_num',100);
	if(chat_val.replace(" ","")!="") {
		var url=SITEURL+"/libs/ajax/ajax_chat_send_message.php?object_type="+object_type+"&object_id="+object_id;
		//alert(chat_val);
		
		params = "value="+chat_val+"&object_type="+object_type+"&object_id="+object_id;
		
		xmlHttp10=GetXmlHttpObject(chat_message_display);//fails on safari 1
		xmlHttp10.open("POST", url , true);
		xmlHttp10.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp10.setRequestHeader("Content-length",params.length );
		xmlHttp10.setRequestHeader("Connection", "close");
		//xmlHttp10.onreadystatechange = send_chat_message_display;
		xmlHttp10.send(params);
		
	}
	document.getElementById(chat_message_id).value = '';
}
function send_chat_message_display() {
	//ok got the chat_val which is
	chat_val;
}
function chat_message_display() {
	//ok this need to take whats in chat and the face and the user and then display
	if (xmlHttp10.readyState==4 || xmlHttp10.readyState=="complete"){ 
		chat = xmlHttp10.responseText;
		//alert(chat);
		chat_split = chat.split("[SPLIT]");
		//ok 0 = username 1= message 2 = face_image
		html = "<div class=\"chat_container\"><strong><a target=\"_blank\" href=\""+SITEURL+"/user/"+chat_split[0]+"\">"+chat_split[0]+":</a></strong> "+chat_split[1]+"</div>";
		
		document.getElementById('chat_message_box').innerHTML = document.getElementById('chat_message_box').innerHTML + html;
		divconsole1 = document.getElementById('chat_message_box');
		divconsole1.scrollTop = divconsole1.scrollHeight - divconsole1.clientHeight;		
	}
}



var chat_object_type;
var chat_object_id;
function chat_message_checker(object_type,object_id) {
	chat_object_id = object_id;
	chat_object_type = object_type;
	
	//var url=SITEURL+"/libs/ajax/ajax_chat_check_message.php?object_type="+object_type+"&object_id="+object_id+"&last_check_date="+CURDATE;
	var url=SITEURL+"/libs/ajax/ajax_chat_check_message.php?object_type="+object_type+"&object_id="+object_id+"&chat_log_id="+CHATID;
	//alert(url);
	xmlHttp12=GetXmlHttpObject(chat_message_array_display);//fails on safari 1
	xmlHttp12.open("GET", url , true);
	xmlHttp12.send(null);
}
function chat_message_array_display() {
	if (xmlHttp12.readyState==4 || xmlHttp12.readyState=="complete"){ 
		chat = xmlHttp12.responseText;
		//alert(chat);
		chat = chat.split("[SPLIT]");
		
		if(chat[0]!="") {
			//alert(chat[0]);
			document.getElementById('chat_message_box').innerHTML = document.getElementById('chat_message_box').innerHTML + chat[0];
			divconsole2 = document.getElementById('chat_message_box');
			divconsole2.scrollTop = divconsole2.scrollHeight - divconsole2.clientHeight;		
		}
		//CURDATE = chat[1];
		//alert(chat[1]);
		CHATID = chat[1];
		//CURDATE = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+"-"+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
		setTimeout("chat_message_checker("+chat_object_type+","+chat_object_id+")",700);
	}
}
function chat_check_online(object_type,object_id) {
	chat_object_id = object_id;
	chat_object_type = object_type;
	
	//var url=SITEURL+"/libs/ajax/ajax_chat_check_message.php?object_type="+object_type+"&object_id="+object_id+"&last_check_date="+CURDATE;
	var url=SITEURL+"/libs/ajax/ajax_chat_check_online.php?object_type="+object_type+"&object_id="+object_id;
	//alert(url);
	xmlHttp_chat_updater=GetXmlHttpObject(chat_check_online_helper);//fails on safari 1
	xmlHttp_chat_updater.open("GET", url , true);
	xmlHttp_chat_updater.send(null);
}
function chat_check_online_helper() {
	if(xmlHttp_chat_updater.readyState==4 || xmlHttp_chat_updater.readyState=="complete"){ 
		//xmlHttp_chat_updater 
		//ok what we want is two arrays those that we need to add/appear and those to remove.
		return_array = xmlHttp_chat_updater.responseText;
		//ok these people are on the page
		//alert(return_array);
		array_users = return_array.split(',');
		for(var i=0;i<array_users.length&&array_users[i]!="";i++) {
			//ok got the users
			//
			//alert(array_users[i]);
			var div = document.getElementById('in_chat_div_'+array_users[i]);
			//alert(div);
			if(div==null) {
				//display the div
				chat_display_online_box(array_users[i]);			
			}
			else {
				div.style.display = 'block';
			}
			
			
			for(var b=0;b<chat_involved_array.length;b++) {
				//check if is array
				if(array_users[i]==chat_involved_array[b]) {
					//is in array
					//remove
					chat_involved_array.splice(b,1);
					b = chat_involved_array.length;
				}
			}
		
			
		}
		//alert(chat_involved_array);
		for(var b=0;b<chat_involved_array.length;b++) {
			var div = document.getElementById('in_chat_div_'+chat_involved_array[b]);
			//alert(div);
			if(div!=null) {
				div.style.display = 'none';
			}
		}
		chat_involved_array = array_users;
		
		setTimeout("chat_check_online("+chat_object_type+","+chat_object_id+")",4000);
	}
}
function chat_display_online_box(user_id) {
	var url=SITEURL+"/libs/ajax/ajax_chat_get_box_online.php?user_id="+user_id;
	xmlHttp_chat_online=GetXmlHttpObject(chat_display_online_box_helper);//fails on safari 1
	xmlHttp_chat_online.open("GET", url , true);
	xmlHttp_chat_online.send(null);
}
function chat_display_online_box_helper() {
	if (xmlHttp_chat_online.readyState==4 || xmlHttp_chat_online.readyState=="complete"){
		return_div = xmlHttp_chat_online.responseText;
		document.getElementById('in_chat_container').innerHTML = document.getElementById('in_chat_container').innerHTML + return_div;
	}	
}
function caretPos(textEl,scrollPos)
	{
		var i=textEl.value.length+1;
		
		if (!textEl.setSelectionRange)
		{
			theCaret = document.selection.createRange
().duplicate();
			while (theCaret.parentElement()==textEl && 
theCaret.move("character",1)==1) --i;
			return i;
		}
		else {
			textEl.focus();
			//var scrollPos = textEl.scrollTop;
			textEl.scrollTop = scrollPos;
		}
		return -1;
	}
function setCaretPosition(ctrl, pos) { if(ctrl.setSelectionRange) { ctrl.focus();
        ctrl.setSelectionRange(pos,pos); }
    else if (ctrl.createTextRange) { var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select(); }}
				
				
function refresh_connect4_gameroom() {
	var url=SITEURL+"/libs/ajax/ajax_connect4_gameroom_refresh.php";
	xmlHttp_gameroom=GetXmlHttpObject(refresh_connect4_gameroom_helper);//fails on safari 1
	xmlHttp_gameroom.open("GET", url , true);
	xmlHttp_gameroom.send(null);
}
function refresh_connect4_gameroom_helper() {
	if (xmlHttp_gameroom.readyState==4 || xmlHttp_gameroom.readyState=="complete"){
		return_div = xmlHttp_gameroom.responseText;
		document.getElementById('gameroom_list').innerHTML = return_div;
		setTimeout("refresh_connect4_gameroom()",3000);
	}	
}
var global_link_id = '';			
function vote_link(link_id) {
	global_link_id = link_id;
	var url=SITEURL+"/libs/ajax/ajax_link_vote.php?link_id="+link_id;
	xmlHttp=GetXmlHttpObject(vote_link_helper);//fails on safari 1
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}
function vote_link_helper() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		return_div = xmlHttp.responseText;
		document.getElementById('link_inner_'+global_link_id).innerHTML = return_div;
		document.getElementById('link_inner_vote_'+global_link_id).innerHTML = 'Voted';
		
	}
}
function recent_activity_tab(tab) {
	document.getElementById('recent_all_tab').className = '';
	document.getElementById('recent_profiles_tab').className = '';
	document.getElementById('recent_blogs_tab').className = '';
	document.getElementById('recent_albums_tab').className = '';
	document.getElementById('recent_scores_tab').className = '';
	document.getElementById('recent_forum_tab').className = '';
	document.getElementById('recent_groups_tab').className = '';
	
	document.getElementById('all_tab').style.display = 'none';
	document.getElementById('profiles_tab').style.display = 'none';
	document.getElementById('blogs_tab').style.display = 'none';
	document.getElementById('albums_tab').style.display = 'none';
	document.getElementById('scores_tab').style.display = 'none';
	document.getElementById('forum_tab').style.display = 'none';
	document.getElementById('groups_tab').style.display = 'none';
	
	
	if(document.getElementById('recent_feed_tab')!=null) {
		document.getElementById('recent_feed_tab').className = '';
		document.getElementById('feed_tab').style.display = 'none';
	}
	
	
	document.getElementById('recent_'+tab+'_tab').className = 'current';
	//alert(tab);
	document.getElementById(tab+'_tab').style.display = '';
	
	
}
function global_ajax_recent_activity() {
	//k run and then set to run every 15 secs
	url = SITEURL+"/libs/ajax/ajax_recent_activity_updater.php";
	global_recent_act_xmlHttp=GetXmlHttpObject(global_ajax_recent_activity_helper);//fails on safari 1
	global_recent_act_xmlHttp.open("GET", url , true);
	global_recent_act_xmlHttp.send(null);
	
	
	
}
function global_ajax_recent_activity_helper() {
	if (global_recent_act_xmlHttp.readyState==4 || global_recent_act_xmlHttp.readyState=="complete"){
		document.getElementById('global_recent_activity').innerHTML = global_recent_act_xmlHttp.responseText;
		setTimeout("global_ajax_recent_activity()",15000);
	}
	
}

function search_box_fill(id,stringval){
	if (document.getElementById(id).value == ''){
		document.getElementById(id).value = stringval;
	}
}
function search_box_empty(id,stringval){
	if (document.getElementById(id).value == stringval){
		document.getElementById(id).value = '';
	}
}

function global_set_notification(object_type,object_id) {
	url = SITEURL+"/libs/ajax/ajax_set_notification.php?object_id="+object_id+"&object_type="+object_type;
	global_set_note_xmlHttp=GetXmlHttpObject(global_set_notification_helper);//fails on safari 1
	global_set_note_xmlHttp.open("GET", url , true);
	global_set_note_xmlHttp.send(null);
}
function global_set_notification_helper() {
	if (global_set_note_xmlHttp.readyState==4 || global_set_note_xmlHttp.readyState=="complete"){
		//alert(global_set_note_xmlHttp.responseText);
		document.getElementById('global_set_notification_link').innerHTML = global_set_note_xmlHttp.responseText;
	}
}

function friend_alert(status,alert_field,user_friends_id){
	url = SITEURL+"/libs/ajax/ajax_friend_alert.php?status="+status+"&alert_field="+alert_field+"&user_friends_id="+user_friends_id;
	friend_alert_xmlHttp = GetXmlHttpObject(friend_alert_helper);//fails on safari 1
	friend_alert_xmlHttp.open("GET", url , true);
	friend_alert_xmlHttp.send(null);
}

function friend_alert_helper() {
	if (friend_alert_xmlHttp.readyState==4 || friend_alert_xmlHttp.readyState=="complete"){
		result = friend_alert_xmlHttp.responseText;
		result = result.split("[SPLIT]");
		document.getElementById('friend_alert_'+result[0]).innerHTML = result[1];
	}
}









