// JavaScript Document

function setmaxlength(limitField, limitNum, showCount) {
    if (limitField.value.length > limitNum) {
    	document.getElementById(showCount).style.color = "red";	
		limitField.value = limitField.value.substring(0, limitNum);
    } 
	document.getElementById(showCount).innerHTML =  limitField.value.length;
}

function cleardefault(destination, default_text) {
	if (document.getElementById(destination).value == default_text)
	document.getElementById(destination).value = "";
}

function ask_user(question_text, var_to_pass, var_to_pass2)
{
	document.getElementById("askuser_yes_btn").onclick=function(){ask_user_close(); user_yes_btn_clicked(var_to_pass, var_to_pass2);};
	document.getElementById("askuser_text").innerHTML = question_text;
	document.getElementById("askuser_bg").style.display="";
	document.getElementById("askuser_container").style.display="";
	document.getElementById("askuser_no_btn").focus();
}

function ask_user_close() {
	document.getElementById("askuser_bg").style.display="none";
	document.getElementById("askuser_container").style.display="none";
	document.getElementById("askuser_text").innerHTML = "";
}

function alert(alert_text, submit_btn_text) {
	submit_btn_text == "Sorry About That!";
	document.getElementById("alert_text").innerHTML = alert_text;
	document.getElementById("alert_bg").style.display="";
	document.getElementById("alert_container").style.display="";
	document.getElementById("alert_submit_btn").focus();
}

function alert2(alert_text) {
	document.getElementById("alert2_text").innerHTML = alert_text;
	document.getElementById("alert2_bg").style.display="";
	document.getElementById("alert2_container").style.display="";
}


function alert_close() {
	document.getElementById("alert_bg").style.display="none";
	document.getElementById("alert_container").style.display="none";
	document.getElementById("alert_text").innerHTML = "";
	document.getElementById("alert2_bg").style.display="none";
	document.getElementById("alert2_container").style.display="none";
	document.getElementById("alert2_text").innerHTML = "";
}






var xmlhttp;
var twittbox_oldonclick;
var TEMPVAR1;
var TEMPVAR2;
function mark_correct(answer_id, question_id)
{
	do_ajax_work('/scripts/flag_answer.php', mark_correct_response, '?qid='+question_id+'&aid='+answer_id);
} 



 

function do_ajax_work(where_to, function_after, sendvars)
{
	xmlhttp=GetXmlHttpObject()
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support XML HTTP Request");
	  return;
	  }
	var url=where_to;
	url=url+"?"+sendvars;
	xmlhttp.onreadystatechange=function_after;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}

function ajax_step2()
{
if (xmlhttp.readyState==4)
  {
  	alert2(xmlhttp.responseText);
  }
}


function twittbox_submit(user_message)
{
	twittbox_oldonclick=document.getElementById("twittbox_submit").style.display="none";
	
	xmlhttp=GetXmlHttpObject()
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support XML HTTP Request");
	  return;
	  }
	var url="/scripts/postboth.php";
	url=url+"?fb_status="+$("#twittbox_facebook").attr('class')+"&twit_status="+$("#twittbox_twitter").attr('class')+"&post_message="+user_message;
	xmlhttp.onreadystatechange=recievedResponse_TwittBox;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}



function recievedResponse_TwittBox()
{
if (xmlhttp.readyState==4)
  {
  	alert(xmlhttp.responseText);
	twittbox_oldonclick=document.getElementById("twittbox_submit").style.display="";
  }
}


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}





$(document).ready(function() {

	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}

	$(".find_college").autocomplete("/scripts/findcollege.php", {
		width: 380,		
		scrollHeight: 220,
		max: 30,
		mustMatch: true,
		selectFirst: false
	});

	$(".find_college").result(function(event, data, formatted) {
		if (data)
		{
			$(this).parent().next().find("input").val(data[1]);
			$(this).val(data[0]);
		}
		else
		{
			$(this).parent().next().find("input").val("");
		}
	});
	
	
	$(".find_major").autocomplete("/scripts/findmajor.php", {
		width: 380,		
		scrollHeight: 220,
		max: 30,
		mustMatch: true,
		selectFirst: false
	});

	$(".find_major").result(function(event, data, formatted) {
		if (data)
		{
			$(this).parent().next().find("input").val(data[1]);
			$(this).val(data[0]);
		}
		else
		{
			$(this).parent().next().find("input").val("");
		}
	});	
	
	
	$(".find_cal_cat").autocomplete("/scripts/find_cal_cat.php", {
		width: 380,		
		scrollHeight: 220,
		max: 30,
		mustMatch: true,
		selectFirst: false
	});

	$(".find_cal_cat").result(function(event, data, formatted) {
		if (data)
		{
			$(this).parent().next().find("input").val(data[1]);
			$(this).val(data[0]);
		}
		else
		{
			$(this).parent().next().find("input").val("");
		}
	});	
	
	
	
	
	$("#twittbox_twitter").click(function() {
		//alert ("STATECHANGE"+$("#twittbox_twitter").attr('class'));
		if ($("#twittbox_twitter").attr('class') == "on")
		{
		         $("#twittbox_twitter").removeClass("on");
                 $("#twittbox_twitter").addClass("off"); 
		}
		else
		{
		         $("#twittbox_twitter").removeClass("off");
                 $("#twittbox_twitter").addClass("on"); 			
		}
		
		if ($("#twittbox_twitter").attr('class') == "off" && $("#twittbox_facebook").attr('class') == "off")
		{
			twittbox_oldonclick=document.getElementById("twittbox_submit").style.display="none";
		}
		else
		{
			twittbox_oldonclick=document.getElementById("twittbox_submit").style.display="";
		}
	
		
	});	
	
	$("#twittbox_facebook").click(function() {
		//alert ("STATECHANGE"+$("#twittbox_twitter").attr('class'));
		if ($("#twittbox_facebook").attr('class') == "on")
		{
		         $("#twittbox_facebook").removeClass("on");
                 $("#twittbox_facebook").addClass("off"); 
		}
		else
		{
		         $("#twittbox_facebook").removeClass("off");
                 $("#twittbox_facebook").addClass("on"); 			
		}
		
		if ($("#twittbox_twitter").attr('class') == "off" && $("#twittbox_facebook").attr('class') == "off")
		{
			twittbox_oldonclick=document.getElementById("twittbox_submit").style.display="none";
		}
		else
		{
			twittbox_oldonclick=document.getElementById("twittbox_submit").style.display="";
		}
		
	});		
	

});





function line_breaks(text_todooo){
	return text_todooo.replace(/\n/g, '<br />');
}

