//Get Server
loc1 = location.href;
in1 = loc1.indexOf("/",7);
var server = loc1.substr(0,in1) + "/index.php/";


function add_discussion_comment(company_id,name,comment,div)
{
	if(comment == '' || comment == "Thanks for submitting!")
		return;
	var pre = document.getElementById(div).innerHTML;
	document.getElementById(div).innerHTML = "<div id='dicussion'><div class='discussion_item'><div id='timestamp'></div><div id='name'>" + name + "</div><div id='content'>Adding your comment...</div></div></div>" + pre;
	jx.load(server + 'ajax/add_discussion_comment/?company_id=' + company_id + '&comment=' + escape(comment) + '&name=' + escape(name),
			function(data) {
				document.getElementById(div).innerHTML = "<div id='dicussion'><div class='discussion_item'><div id='timestamp'>Just Now</div><div id='name'>" + name + "</div><div id='content'>" + data + "</div></div></div>" + pre;
			}, 'text','post');	
}

function run_python(path,question_number,code,div)
{
	jx.load(server + 'ajax/run_python/?path=' + path + '&question_number=' + question_number + '&code=' + escape(code.replace(/\+/g, '_PLUS_')),
			function(data) {
				document.getElementById(div).innerHTML = data;
			}, 'text','post','no');	
}


function run_python_2(quest_name,quest_id,page,code,div)
{
	document.getElementById(div).innerHTML = "running...";
	jx.load(server + 'ajax/run_python_2/?quest_name=' + quest_name + '&quest_id=' + quest_id + '&page=' + page + '&code=' + escape(code.replace(/\+/g, '_PLUS_')),
			function(data) {
				document.getElementById(div).innerHTML = data;
			}, 'text','post','no');	
}

function edit_quest(name)
{
	var ret;
	jx.load(server + 'ajax/edit_quest/?name=' + escape(name),
			function(data) {
				ret = data;											
			}, 'text','post','no');	
	return ret;
}


function get_branch(quest_name,page,answer_number)
{
	if(answer_number == undefined)
		answer_number = '';

	var branch;
	jx.load(server + 'ajax/get_branch/?quest_name=' + escape(quest_name) + '&page=' + page + '&answer_number=' + escape(String(answer_number).replace(/\+/g, '_PLUS_')),
			function(data) {
				branch = data;											
			}, 'text','post','no');	
	return branch;
}

function show_followup(quest_name,page,answer_number)
{
	if(answer_number == undefined)
		answer_number = '';

	document.getElementById('followup_answer').innerHTML = 'Getting some feedback on your answer...<br>';
		
	jx.load(server + 'ajax/show_followup/?quest_name=' + escape(quest_name) + '&page=' + page + '&answer_number=' + escape(String(answer_number).replace(/\+/g, '_PLUS_')),
			function(data) {
			
			document.getElementById('followup_answer').innerHTML = data.replace(new RegExp( "\\n", "g" ), '<br>');
													
			var y1 = $('quest_wrapper').getScrollSize().y;
			var y2 = $('quest_wrapper').getSize().y;
			quest_height = y1 > y2 ? y1 : y2;

			var y1 = $('followup_answer').getScrollSize().y;
			var y2 = $('followup_answer').getSize().y;
			var quest_followup_height = y1 > y2 ? y1 : y2;
					
			$('quest_wrapper').setStyle('height',quest_height+quest_followup_height+40);
			$('quest').setStyle('height',quest_height+quest_followup_height+40);

			}, 'text','post');	
	return true;
}

function is_available_quest_name(div,name)
{
	var ret = false;
	document.getElementById(div).value = "";
	jx.load(server + 'ajax/is_available_quest_name/?name=' + name,
			function(data) {
				if(data =='false')
				{
					document.getElementById(div).innerHTML = "\"" + name + "\" is unavailable, please choose another";	
					ret = false;
				}else
				{
					document.getElementById(div).innerHTML = "\"" + name + "\" is available!";	
					ret = true;
				}
													
			}, 'text','post','no');	
	return ret;
}
				
function get_url(name,type,element)
{
	document.getElementById(element).value = "";
	jx.load(server + 'ajax/get_url/?name=' + name + '&type=' + type,
			function(data) {
			
			document.getElementById(element).value = data;
													
			}, 'text','post','no');	
	return true;
}

function update_leaderboard(pop_segment,q,level_section,limit_all,limit_local)
{
	document.getElementById("leaderboard").innerHTML = "Sorting and shuffling...";
	jx.load(server + 'ajax/print_leaderboards/?pop_segment=' + pop_segment + '&q=' + q + '&level_section=' + level_section + '&limit_all=' + limit_all + '&limit_local=' + limit_local,
			function(data) {
			
			document.getElementById("leaderboard").innerHTML = data;
													
			}, 'text','post','no');	
	return true;
}

function quest_session_save(quest_id,page,answer)
{	
	if(answer == undefined)
		answer = '';
		
	jx.load(server + 'ajax/quest_session_save/?quest_id=' + quest_id + '&page=' + page + '&answer=' + escape(String(answer).replace(/\+/g, '_PLUS_')), null, 'text','post','no');	
	return true;
}

function quest_session_save_2(quest_id,page,answer)
{	
	if(answer == undefined)
		answer = '';
		
	jx.load(server + 'ajax/quest_session_save_2/?quest_id=' + quest_id + '&page=' + page + '&answer=' + escape(answer.replace(/\+/g, '_PLUS_')), null, 'text','post','no');	
	return true;
}

function start_challenge(id)
{
	jx.load(server + 'ajax/start_challenge/?id=' + id, null, 'text','post','no');	
	return true;
}


function stop_challenge(id)
{
	document.getElementById("stop_challenge_" + id).innerHTML = "Shuffling papers...";
	jx.load(server + 'ajax/stop_challenge/?id=' + id,
			function(data) {
			
			document.getElementById("stop_challenge_" + id).innerHTML = data;
													
			}, 'text','post');	
}

function make_sticky(id)
{
	document.getElementById("make_sticky_" + id).innerHTML = "Applying grip...";
	jx.load(server + 'ajax/make_sticky/?id=' + id,
			function(data) {
			
			document.getElementById("make_sticky_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function make_unsticky(id)
{
	document.getElementById("make_sticky_" + id).innerHTML = "Greasing up...";
	jx.load(server + 'ajax/make_unsticky/?id=' + id,
			function(data) {
			
			document.getElementById("make_sticky_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function delete_message(id)
{
	document.getElementById("message_" + id).innerHTML = "Deleting...";
	jx.load(server + 'ajax/delete_message/?id=' + id,
			function(data) {
			
				document.getElementById("message_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function mark_unread(id)
{
	document.getElementById("unread_" + id).innerHTML = "Repackaging...";
	jx.load(server + 'ajax/mark_unread/?id=' + id,
			function(data) {
			
			document.getElementById("unread_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function remove_friend(id)
{
	document.getElementById("remove_friend_" + id).innerHTML = "Removing...";
	jx.load(server + 'ajax/remove_friend/?id=' + id,
			function(data) {
			
			document.getElementById("remove_friend_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function request_friend(id,msg)
{	
	document.getElementById("request_friend_" + id).innerHTML = "Asking nicely...";
	jx.load(server + 'ajax/request_friend/?id=' + id + '&msg=' + escape(msg),
			function(data) {
			
			document.getElementById("request_friend_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function confirm_friend(id)
{
	document.getElementById("request_friend_" + id).innerHTML = "Confirming...";
	jx.load(server + 'ajax/confirm_friend/?id=' + id,
			function(data) {
			
			document.getElementById("request_friend_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function reject_friend(id)
{
	document.getElementById("request_friend_" + id).innerHTML = "Saying \"NO!\"...";
	jx.load(server + 'ajax/reject_friend/?id=' + id,
			function(data) {
			
			document.getElementById("request_friend_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function recommend_friend(id,reason)
{
	document.getElementById("recommend_friend_" + id).innerHTML = "Recommending...";
	jx.load(server + 'ajax/recommend_friend/?id=' + id + '&reason=' + reason,
			function(data) {
			
			document.getElementById("recommend_friend_" + id).innerHTML = data;
													
			}, 'text','post');	
}
function report_abuse(msg,url)
{
	document.getElementById("report_abuse").innerHTML = "Reporting...";
	jx.load(server + 'ajax/report_abuse/?msg=' + escape(msg) + '&url=' + url,
			function(data) {
			
			document.getElementById("report_abuse").innerHTML = data;
													
			}, 'text','post');	
}
function update_person(field, value)
{
	jx.load(server + 'ajax/update_person/?field=' + field + '&value=' + value, null, 'text','post','no');	
	return true;
}
function update_user(field, value)
{
	jx.load(server + 'ajax/update_user/?field=' + field + '&value=' + value, null, 'text','post','no');	
	return true;
}
function update_table(table, field, value)
{
	jx.load(server + 'ajax/update_table/?table=' + table + '&field=' + field + '&value=' + value, null, 'text','post','no');	
	return true;
}



function getElementsByStyleClass (className) {
  var all = document.all ? document.all :
    document.getElementsByTagName('*');
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
    if (all[e].className == className)
      elements[elements.length] = all[e];
  return elements;
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function encrypt_pass(password,md5password)
{
	md5password.value = hex_md5(password.value);
	password.value = "";
}
function on_enter( evt, func, param1, param2 ) 
{
	var keyCode = null;
	if( evt.which ) 
	{
		keyCode = evt.which;
	}else if( evt.keyCode ) 
	{
		keyCode = evt.keyCode;
	}
	if( 13 == keyCode )
	{
		func(param1,param2);
		return false;
	}
	return true;
}
function watch_company(id)
{
	document.getElementById("watch_company_" + id).innerHTML = "Checking...";
	jx.load(server + 'ajax/add_newsfeed_interest/?id=' + id,
			function(data) {
			
			if(data != 'false')
			{
				document.getElementById("watch_company_" + id).innerHTML = data;
			}else
				document.getElementById("watch_company_" + id).innerHTML = "Unknown problem";

													
			}, 'text','post');	
}
function remove_watch_company(id)
{
	document.getElementById("watch_company_" + id).innerHTML = "Removing...";
	jx.load(server + 'ajax/remove_newsfeed_interest/?id=' + id,
			function(data) {
			
			if(data != 'false')
			{
				document.getElementById("watch_company_" + id).innerHTML = data;
			}else
				document.getElementById("watch_company_" + id).innerHTML = "Unknown problem";
			
													
			}, 'text','post');	
}
function cache_block_feed(id)
{
	document.getElementById("feed_str_" + id).innerHTML = "*smack* *smack*";
	var weight;
	jx.load(server + 'ajax/cache_block_feed/?id=' + id,
			function(data) {
			
			if(data != 'false')
			{
				document.getElementById("cache_block_weight_" + id).innerHTML = data;
				document.getElementById("feed_str_" + id).innerHTML = 'Fed';
				document.getElementById("feed_str_" + id).onclick = '';
				weight = parseFloat(data);
				TestBalance.changeBlockWeight(id,weight);
			}else
				document.getElementById("feed_str_" + id).innerHTML = "Unknown problem";
													
			}, 'text','post');
	return weight;
}
function cache_block_starve(id)
{
	var weight;
	document.getElementById("starve_str_" + id).innerHTML = "Removing all food...";
	jx.load(server + 'ajax/cache_block_starve/?id=' + id,
			function(data) {
			
			if(data != 'false')
			{
				document.getElementById("cache_block_weight_" + id).innerHTML = data;
				document.getElementById("starve_str_" + id).innerHTML = 'Starved';
				document.getElementById("starve_str_" + id).onclick = '';
				weight = parseFloat(data);
				TestBalance.changeBlockWeight(id,weight);
			}else
				document.getElementById("starve_str_" + id).innerHTML = "Unknown problem";
													
			}, 'text','post');
	return weight;
}

function cache_block_delete(block_id)
{

	var retval = confirm("Are you sure you'd like to delete this block?");
	if(retval)
	{
		document.getElementById("block_" + block_id).innerHTML = "Deleting...";
		//do the actual delete
		jx.load(server + 'ajax/cache_block_delete/?id=' + block_id,
			function(data) {
						
			if(data != 'false')
			{
				document.getElementById("block_" + block_id).style.visibility = 'hidden';
				//document.removeChild(document.getElementById("block_" + block_id));	
			}
													
			}, 'text','post');			
	}
}
function cache_comment_delete(comment_id)
{
	var retval = confirm("Are you sure you'd like to delete this comment?");
	if(retval)
	{
		//do the actual delete
		document.getElementById("comment_" + comment_id).innerHTML = "Deleting...";
		jx.load(server + 'ajax/cache_comment_delete/?id=' + comment_id,
			function(data) {
						
			if(data != 'false')
			{
				document.getElementById("comment_" + comment_id).style.visibility = 'hidden';
			}
													
			}, 'text','post');			
	}
}
function do_cache_block_reply(block_id,content)
{
	//hide the form
	document.getElementById('reply_div_' + block_id).style.display = 'none';
	//do the actual insert
	jx.load(server + 'ajax/cache_block_reply/?id=' + block_id + '&content=' + escape(content),
			function(data) {
						
			if(data != 'false')
			{
				var NewReply = document.createTextNode(" you just said \"" + data + "\"");
				var NewDiv = document.createElement('div');
				NewDiv.setAttribute('class','block_comment');
				NewDiv.appendChild(document.createElement("<br>"));
				NewDiv.appendChild(NewReply);
				document.getElementById("block_" + block_id).appendChild(NewDiv);
			}
													
		}, 'text','post');		
}

function get_radio_value(form)
{
	if(form==null)
		return '';
	for (var i=0; i < form.length; i++)
	{
		if (form[i].checked)
		{
			var rad_val = form[i].value;
		}
	}
	return rad_val;
}

function get_textarea_value(form)
{
	return form.answer.value;
}

function get_textinput_value(form)
{
	return form.answer.value;
}

function get_form_values(form)
{	
	if(form==null)
		return '';
	var ret = '';
	for (var i=0; i < form.length; i++)
	{
		var ind = form[i].name.indexOf('fd_custom_');
		if (ind != -1)
		{
			var n = form[i].name.substring(10).replace(/\s/g,"_");
			var v = form[i].value;
			ret+= n + '=' + v + '&';
		}
	}
	return ret;
}

function get_money_form_values(form)
{	
	if(form==null)
		return '';
	var ret = '';
	for (var i=0; i < form.length; i++)
	{
		var ind = form[i].name.indexOf('fd_custom_');
		if (ind != -1)
		{
			var n = form[i].name.substring(10).replace(/\s/g,"_");
			var v = convert_to_money(form[i].value);
			ret+= n + '=' + v + '&';
		}
	}
	return ret;
}

function convert_to_money(val)
{
	if(val.length > 0)
	{
		//remove dollar sign
		if(val[0] == '$')
			val = val.substring(1);

		//remove commas
		val = val.replace(/,/g, "");

		//add decimals it they don't exist
		var ind = val.indexOf('.');
		if(ind == -1)
			val+= ".00";

		//make decimals 2 digits
		var len = val.length;
		ind = val.indexOf('.');
		if(ind != len-3)
			val = val.substring(0,ind+3);

		return val;
	}else
		return 0;

}

function apply_css(obj,css,speed)
{
	if(speed == null)
		var speed = 1000;
	
	//do a mootools transition from where we were to where we are now
	var myEffect = new Fx.Morph(obj, {duration: speed, transition: Fx.Transitions.Sine.easeOut});
 
	var css_array = css.split(";");
	var properties = new Object();
	css_array.each(function(str, i) {
	  str = trim(str);
	  if(str != '' && str != 'undefined')
	  {
	   var temp = str.split(":");
	   properties[trim(temp[0])] = trim(temp[1]);
	   //alert(properties[trim(temp[0])] + ' <= ' + trim(temp[0]));
	  }
	});
	
	myEffect.start(properties);

}


function urlencode(str) 
{
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false;
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE)
	document.captureEvents(Event.MOUSEMOVE)
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Temporary variables to hold mouse x-y pos.s
var MouseX = 0
var MouseY = 0
// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
MouseX = tempX;
MouseY = tempY;
  return true;
}


function trim( str, charlist ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // +   improved by: Jack
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
 
    var whitespace, l = 0;
    
    if (!charlist) {
        whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    } else {
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    }
    
    l = str.length;
    for (var i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}