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 items = insertquote.arguments.length;
 if(items>1) {
	//there is a 3rd arg
	var username = insertquote.arguments[2];
	//leave numbers to fit in with other bbcode
	var open = "[quote:3496ace15f=\""+username+"\"]";
 }
 else {
	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 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?puzzle_id="+puzzle_id+"&game_id="+game_id+"&difficulty="+difficulty+"&score="+score;
	//alert(url);

	xmlHttp=GetXmlHttpObject(display_puzzle_time);//fails on safari 1
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	
	
	
	
}
function display_puzzle_time() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		var return_text = xmlHttp.responseText;
		document.getElementById("display_area").innerHTML = return_text;
		ajax_done_puzzle = 1;
		refresh_puzzle_scores('scores_'+global_diff,global_puzzle_id,global_gamer_id,global_difficulty);
	}
	
	
	
}


function refresh_puzzle_scores(score_div_id,puzzle_id,game_id,difficulty) {
	var url=SITEURL+"/libs/ajax/ajax_refresh_puzzle_scores.php?score_div_id="+score_div_id+"&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 div = return_text3[0];
		//alert(div);
		
		var the_html = return_text3[1];
		//alert(the_html);
		document.getElementById(div).innerHTML = the_html;
	} 
}