function AjaxUpdate( div, url ){
	// load the url into the div via ajax
	// and rebind cluetip links (if any)
	//var divclue = div + ' a.jt';
	$(div).load(url, {}, function(){
   		//$(divclue).cluetip();
 	});
}
function HideDiv(div){
	$(div).slideUp('slow');	
}
function ShowDiv(div){
	$(div).slideDown('slow');	
}
function EmptyDiv(div){
	$(div).empty();	
}
function StrikeDiv(div){
	$(div).css('text-decoration','line-through');	
}
function AppendDiv(div, atext){
	$(div).append(atext);	
}

function clueTipBind( link, wid, opt ){
	var linkid = '#' + link;
	if (!wid) {wid = 300;}
	if (opt == 'splitTitle') {$(linkid).cluetip({width:wid, splitTitle: '|'});}
	else {$(linkid).cluetip({width:wid});}
    if(document.createEventObject){
        document.getElementById(link).fireEvent("onclick");
    }else if (document.createEvent){
        $(linkid).click();
    }
}

function openModal(page, department) {
	// load the contact form using ajax
	var thepage = baseurl+'/demo/' + page + '.php?d=' + department + '&b=' + baseurl;
	$.get(thepage, function(data){
		// create a modal dialog with the data
		//var data = 'this is a test';
		$(data).modal({
			close: false,
			overlayId: 'contact-overlay',
			containerId: 'contact-container',
			position: ["10%",""],
			opacity: 75,
			closeClass: "modalClose",
			onOpen: contact.open,
			//onShow: contact.show,
			onClose: contact.close
		});
	});
}

function UpdateNotes(obj, qid) {
      var tail = "q=" + qid + "&notes=" + encodeURI( document.getElementById("notestextarea" + qid).value ) ;
      var div = "#notes" + qid;
      $.post(baseurl+"/question/updatenotesajax/",
        tail,function(data){
                $(div).append(data).show('fast');
        });
}

function UpdateTopicDesc(obj, tid) {
      var tail = "t=" + tid + "&descrip=" + encodeURI( document.getElementById("topicdesctextarea").value ) ;
      var div = "#tooltip";
      $.post(baseurl+"/question/topicdescajax/",
        tail,function(data){
                $(div).replaceWith(data).show('fast');
        });
}

function AddQToNewAssign(obj, qid) {
      var tail = "q=" + qid + "&name=" + encodeURI( document.getElementById("newassignname").value ) + "&c=" + encodeURI( document.getElementById("newassignc").value ) ;
      //alert(tail);
      var div = "#tooltip";
      $.post(baseurl+"/course/addassignajax/",
        tail,function(data){
                $(div).replaceWith(data).show('fast');
        });    
}

function FlagQ(obj, qid, un) {
      var tail = "q=" + qid + "&reason=" + encodeURI( document.getElementById("flagreason").value ) + "&un=" + un ;
      //alert(tail);
      var div = "#tooltip";
      $.post(baseurl+"/question/flagajax/",
        tail,function(data){
                $(div).replaceWith(data).show('fast');
                var qdiv = "#" + qid;
			    AjaxUpdate(qdiv,baseurl+'/question/refreshajax/q/'+ qid);
        });
}

function AddNoteQ(obj, qid) {
      var tail = "q=" + qid + "&note=" + encodeURI( document.getElementById("note").value )  ;
      //alert(tail);
      var div = "#tooltip";
      $.post(baseurl+"/question/addnoteajax/",
        tail,function(data){
                $(div).replaceWith(data).show('fast');
        });
}

function AddFamQ(obj, qid) {
      var tail = "q=" + qid + "&bro=" + encodeURI( document.getElementById("bro").value )  ;
      //alert(tail);
      var div = "#tooltip";
      $.post(baseurl+"/question/addfamajax/",
        tail,function(data){
                $(div).replaceWith(data).show('fast');
                var qdiv = "#" + qid + "full";
			    AjaxUpdate(qdiv,baseurl+'/question/refreshajax/full/1/q/'+ qid);
        });
}

function BloomsEdit(obj, qid, approve) {
	  arrayCheckBox = new Array;
      $("#tooltip input:checked").each(function(id) {
      myVar = $("#tooltip input:checked").get(id);
      arrayCheckBox.push(myVar.name + '=' + myVar.value); 
      }); 
      var bloomstail = arrayCheckBox.join('&'); 
      var tail = "q=" + qid + "&approve=" + approve + "&" + bloomstail ;
      //alert(tail);
      var div = "#tooltip";
      $.post(baseurl+"/question/bloomseditajax/",
        tail,function(data){
                $(div).replaceWith(data).show('fast');
                var qdiv = "#" + qid;
			    AjaxUpdate(qdiv,baseurl+'/question/refreshajax/q/'+ qid);
        });
}

function BheadEdit(obj, qid) {
	  arrayCheckBox = new Array;
      $("#tooltip input:checked").each(function(id) {
      myVar = $("#tooltip input:checked").get(id);
      arrayCheckBox.push(myVar.name + '=' + myVar.value); 
      }); 
      var bheadstail = arrayCheckBox.join('&'); 
      var tail = "q=" + qid + "&" + bheadstail ;
      //alert(tail);
      var div = "#tooltip";
      $.post(baseurl+"/question/bheadsajax/",
        tail,function(data){
                $(div).replaceWith(data).show('fast');
                var qdiv = "#" + qid;
			    AjaxUpdate(qdiv,baseurl+'/question/refreshajax/q/'+ qid);
        });
}

function QBatch( div, url, term ){
	// replace the morelink with a loading gif
	$('#'+div).replaceWith('<center><img id='+div+' src='+baseurl+'images/ajax-loader.gif></center>'); 

	// grab the next batch of qs and append them to the div
	$.get(url,
        function(data){
			$('#qlist').append(data).show('fast');
			// eliminate loading gif
			$('#'+div).slideUp('slow');
			if (term.length >0) {HighlightQs(term);}
        });
}

function ShowDash( div, url ){
	// replace the morelink with a loading gif
	$('#'+div).append('<center><img id='+div+'img src='+baseurl+'images/ajax-loader.gif></center>'); 
	
	// grab the next batch of qs and append them to the div
	$.get(url,
        function(data){
			$('#'+div).append(data).slideDown('slow');
			// eliminate loading gif
			$('#'+div+'img').slideUp('slow');
        });
}

function ShowFamily( fam, qid, url ){
	// replace the morelink with a loading gif
	$('#'+qid).after('<div class=qfamily id='+qid+'family><center><img id='+qid+'img src='+baseurl+'images/ajax-loader.gif></center></div>'); 
	
	// grab the next batch of qs and append them to the div
	$.get(url,
        function(data){
			$('#'+qid+'family').append(data).slideDown('slow');
			// eliminate loading gif
			$('#'+qid+'img').slideUp('slow');
        });
}

function OSettings(obj) {
      var tail = "book=" + encodeURI( document.getElementById("book").value );
      //alert(tail);
      //var div = "#tooltip";
      $.post(baseurl+"/question/osettingsajax/",
        tail,function(data){
			window.location.reload(true);
        });
}

function UpdateRegranTopfloat() {
	var tail = "";
	var div = "#topfloat";
	t = document.forms[0].elements['topics'+"[]"];
	//alert('length:'+t.length);
	for (i=0;i<t.length;i++) {
		if (t[i].checked) {
			tail = tail+"topics[]="+t[i].value+"&";
		}
	}
	
	$.post(baseurl+"/question/regrantopicajax/",
    	tail,function(data){
                $(div).replaceWith(data).show('fast');
        });  
}

function updateClassList()
{
	school = document.forms[0].school;
	schoolid = school.options[school.selectedIndex].value;
	//alert(schoolid);
	if (schoolid) AjaxUpdate('#classlist',baseurl+'/admin/classlist/s/'+ schoolid);
}

function updateSchoolList(list)
{
	state = document.forms[0].state;
	stateid = state.options[state.selectedIndex].value;
	cityid =1;
	if (list=='school') {
		// update the school list
		city = document.forms[0].city;
		cityid = city.options[city.selectedIndex].value;
		//alert(list+' '+stateid+' '+cityid);
		if (stateid && cityid) {AjaxUpdate('#schoollist',baseurl+'/admin/schoollist/s/'+ stateid +'/c/'+cityid);}
	}
	else {
		if (stateid) {
			AjaxUpdate('#citylist',baseurl+'/admin/schoollist/s/'+ stateid);
			AjaxUpdate('#schoollist',baseurl+'/admin/schoollist/s/'+ stateid +'/c/'+cityid);
		}
	}
}

function jumpTo(trgt){
	var target_offset = $("#"+trgt).offset();
	// subtract 70 pixels so we don't slide under the fixed header
	var target_top = target_offset.top - 70;
	//goto that anchor by setting the body scroll top to anchor top
	$('html, body').animate({scrollTop:target_top}, 500);
}

function checkAllQs(exby) {
  q = t = document.forms[0].elements['qs'+"[]"];
  for (i = 0; i < q.length; i++)
  q[i].checked = exby.checked? true:false
}

function createPopup(form) {
	window.open ("", "PreviewPopUp","menubar=0,resizable=1,scrollbars=1,width=650,height=600");
}

function createScreencastPopup(url) {
	window.open (url, "Screencast","menubar=0,resizable=1,scrollbars=1,width=850,height=530");
}

function OpenWindow(url) {
    var w = 630, h = 440; // default sizes
    if (window.screen) {
        w = window.screen.availWidth * 50 / 100;
        h = window.screen.availHeight * 50 / 100;
    }
    window.open(url, 'newWnd', 'width='+w+', height='+h+', left=20, top=20, toolbar=1, status=0, scrollbars=1, resizable=1, location=0');
}

function disableElem(elem, enable) {
	if (enable) {
		$(elem).attr('disabled', false); 
		if ($.browser.msie) {$(elem).css('background-color', '#fff'); }	
	}
	else {
		$(elem).attr('disabled', true); 
		$(elem).attr('value', ''); 
		if ($.browser.msie) {$(elem).css('background-color', '#eee'); }
	}
	
}

/*   
function CheckLastTime() {
  for (var i=0;i<document.quiz.elements.length;i++) {
    var e = document.quiz.elements[i];
    if ( (e.name == 'topics[]') && (e.type=='checkbox') $lasttimejava ) {
      e.checked = document.quiz.lasttimebox.checked;
    }
  }
}

function CheckCheckLastTime(fmobj) {
  var TotalBoxes = 0;
  var TotalOn = 0;
  for (var i=0;i<document.quiz.elements.length;i++) {
    var e = document.quiz.elements[i];
    if ( (e.name == 'topics[]') && (e.type=='checkbox') $lasttimejava ) {
      TotalBoxes++;
      if (e.checked) {
       TotalOn++;
      }
    }
  }
  if (TotalBoxes==TotalOn) {
    document.quiz.lasttimebox.checked=true;
  }
  else {
   document.quiz.lasttimebox.checked=false;
  }
}
*/
