var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1) && (agt.indexOf('webtv') == -1));
var is_nav4up = (is_nav && (is_major >= 4));
var is_ie = (agt.indexOf("msie") != -1);
var is_ie3  = (is_ie && (is_major < 4));
var is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5") == -1) && (agt.indexOf("msie 6") == -1) && (agt.indexOf("msie 7") == -1));
var is_ie4up = (is_ie && (is_major >= 4));
var is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
var is_mac = (agt.indexOf("mac") != -1);
var is_gecko = (agt.indexOf("gecko") != -1);

function getObject(id)
{
	//if (is_ie4) 
		//var el = eval(id);
	//if (is_ie5up || is_gecko)
		var el = document.getElementById(id);
	return el;
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1, menubar=0,resizable=1, width=790,height=600');");
}
function popUpFeedback(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1, width=790,height=800');");
}
function popUpQ(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,resizable=1,scrollbars=1,location=0,statusbar=0,menubar=0,width=550,height=450');");
}
function popUp2(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0, width=500,height=510');");
}
function popUpGraphCalc(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0, width=800,height=500');");
}
//This one is used on the teacher page
function popUp3(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1, width=600,height=600');");
}
function popUpMovie(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0, width=675,height=550');");
}
function popUpChart(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1, width=435,height=535');");
}
function popUpPrint(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1, width=800,height=600');");
}
function popUpGallery(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0, statusbar=1, menubar=0,resizable=1, width=500,height=410');");
}
function popUpAudio(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0, width=290,height=60');");
}
function popUpTestGraph(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1, width=670,height=360');");
}

function addLoadEvent(func)
{
	if(typeof func == 'string')
  	eval('func = function() {'+func+';}');
    
	var oldonload = window.onload;
  if (typeof window.onload != 'function')
  	window.onload = func;
  else
  	window.onload = function() { oldonload(); func(); }
}

function disableEnter(k)
{
	var key;
	if(window.event)
		key = window.event.keyCode;
	else
		key = k.which;

	if(key == 13)
		return false;
}

function chooseImage(imageid)
{
	if(window.opener && !window.opener.closed)
		window.opener.document.getElementById('imageid').value = imageid;
  window.close();
}



function showHide(obj, s, type)
{
	if(s)
  	show(obj, type);
  else
  	hide(obj);
}

function swap(obj, type)
{
	if(document.getElementById(obj).style.display == 'none')
  	show(obj, type);
  else
  	hide(obj);
}

function show(obj, type)
{
	if(document.getElementById(obj))
		document.getElementById(obj).style.display = type || type == "" ? type : 'block';
}

function hide(obj)
{
	if(document.getElementById(obj))
		document.getElementById(obj).style.display = 'none';
}

function checkByClassName(c)
{
	var boxes = document.getElementsByClassName(c);
  for(i=0; i<boxes.length; i++)
  	boxes[i].checked = "checked";
}

function uncheckByClassName(c)
{
	var boxes = document.getElementsByClassName(c);
  for(i=0; i<boxes.length; i++)
  	boxes[i].checked = false;
}

function showTab(tab)
{
	if(tab.value)
  {
  	setImage("menu_tab_image_back", "/images/tabs/"+tab.value+"_back.jpg");
    setImage("menu_tab_image_selected", "/images/tabs/"+tab.value+"_selected.jpg");
  	show("menu_tab_image");
  }
  else
  	hide("menu_tab_image");
}

function setImage(img, src)
{
	document.getElementById(img).src = src;
}

function showToolTip(e){
		if(document.all)e = event;
		
		var obj = document.getElementById('hint');
		//var obj2 = document.getElementById('bubble_tooltip_content');
		//obj2.innerHTML = text;
		obj.style.display = 'block';
		
		var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		var leftPos = e.clientX - 100;
		if(leftPos<0)leftPos = 0;
		obj.style.left = leftPos + 'px';
		obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';
	}	
	
	function hideToolTip()
	{
		document.getElementById('hint').style.display = 'none';
		
	}
  
var bubbleStatus = new Array();
function showImageBubble(e, i){
		for(j in bubbleStatus)
			hideImageBubbleAction(j);
      
		bubbleStatus[i] = true;
		if(document.all)
    	e = event;
		
		var obj = document.getElementById('hint'+i);
		obj.style.display = 'block';
		
		var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		var leftPos = e.clientX - 100;
		if(leftPos<0)
    	leftPos = 0;
		obj.style.left = leftPos + 'px';
    var topPos = e.clientY - 15 - obj.offsetHeight -1 + st;
    if(topPos < document.body.scrollTop)
    	topPos = document.body.scrollTop;
		obj.style.top = topPos + 'px';
	}	
	
  function hideImageBubble(i)
  {
  	bubbleStatus[i] = false;
  	setTimeout("hideImageBubbleAction("+i+")", 400);
  }
  
	function hideImageBubbleAction(i)
	{
  	if(!bubbleStatus[i])
			document.getElementById('hint'+i).style.display = 'none';
	}

  function showToolTip2(e){
		if(document.all)e = event;
		
		var obj = document.getElementById('spanishHint');
		//var obj2 = document.getElementById('bubble_tooltip_content');
		//obj2.innerHTML = text;
		obj.style.display = 'block';
		
		var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		var leftPos = e.clientX - 100;
		if(leftPos<0)leftPos = 0;
		obj.style.left = leftPos + 'px';
		obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';
	}	
  
	function hideToolTip2()
	{
		document.getElementById('spanishHint').style.display = 'none';
		
	}
  
/**
 * DHTML textbox character counter (IE4+) script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function charMessage(id, maxLength) {
	var charObj = id.value || id.id || id.name ? id : getObject(id);
	if (charObj.value.length >= maxLength) 
   	alert("The optimum length for each passage is 400-600 characters.\n\nYou have entered "+charObj.value.length+" characters.");
}

function charCount(id, visCnt) { 
	var charObj = id.value || id.id || id.name ? id : getObject(id);
	if (visCnt)
  {
    visCnt = document.getElementById(visCnt);
  	content = charObj.value.length;
  	if (document.getElementById && !document.all)
    {
      rng = document.createRange();
      rng.setStartBefore(visCnt);
      htmlFrag = rng.createContextualFragment(content);
      while (visCnt.hasChildNodes())
	      visCnt.removeChild(visCnt.lastChild);
      visCnt.appendChild(htmlFrag);
    }
    else
	  	visCnt.innerText=content;
  }
}

function popWindow(loc, title, w, h, r, m)
{
	t = getWindow("top", h);
  l = getWindow("left", w);
  if(w == 'full' || w > screen.availWidth)
  	w = screen.availWidth;
  if(h == 'full' || h > screen.availHeight)
  	h = screen.availHeight;  	
    
	win = window.open(loc, title, 'top='+t+',left='+l+',width='+w+',height='+h+',status=no,resizable='+r+',scrollbars='+(h > screen.availHeight || w > screen.availWidth ? "yes" : r)+',toolbar='+(m == 'yes' ? "yes" : "no")+',location='+(m == 'yes' ? "yes" : "no")+',directories='+(m == 'yes' ? "yes" : "no")+',menubar='+(m == 'yes' ? "yes" : "no"));
	win.focus();
}

function getWindow(t, s)
{
	if(t == "top")
  {
  	if(s > screen.availHeight)
    	return 0;
    return (screen.availHeight-s)/2
  }
  else
  {
  	if(s > screen.availWidth)
    	return 0;
    return (screen.availWidth-s)/2
  }
}
function getDateFromMySQLDate(date, part)
{
	mo = date.substr(5, 2);
  d = date.substr(8, 2);
  y = date.substr(0, 4);
  h24 = date.substr(11, 2);
  h = h24 > 12 ? h24-12 : h24;
  mi = date.substr(14, 2);
  s = date.substr(17, 2);
  ap = h24 >= 12 ? "pm" : "am";
  
	if(date)
  {
  	if(part == 'm')
    	return mo;
    else if(part == 'd')
    	return d;
    else if(part == 'y')
    	return y;
    else if(part == 't')
    	return h+":"+mi+":"+s+ap;
    else if(part == 'all')
    	return getDateFromMySQLDate(date)+" "+getDateFromMySQLDate(date, 't');
    else
			return mo+"/"+d+"/"+y;
  }
  return false;
}

document.getElementsByClassName = function(clsName){
	var retVal = new Array();
  var elements = document.getElementsByTagName("*");
  for(var i = 0;i < elements.length;i++){
  	if(elements[i].className.indexOf(" ") >= 0){
    	var classes = elements[i].className.split(" ");
      for(var j = 0;j < classes.length;j++){
      	if(classes[j] == clsName)
        	retVal.push(elements[i]);
      }
    }
    else if(elements[i].className == clsName)
    	retVal.push(elements[i]);
	}
  return retVal;
}

function addTeacher(selectid, name, id)
{
	var sl = document.getElementById(selectid);
  if(name && id)
  {
    sl.options[sl.options.length] = new Option(name, id);
    sl.value = id;
  }
  if(sl.onchange)
  	sl.onchange();
}

function number_format(a, b, c, d) {
	if(!b)
  	b = 0;
	if(!c)
  	c = ".";
  if(!d)
  	d = ",";
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}

function serialize (txt) {
	switch(typeof(txt)){
	case 'string':
		return 's:'+txt.length+':"'+txt+'";';
	case 'number':
		if(txt>=0 && String(txt).indexOf('.') == -1) return 'i:'+txt+';';
		return 'd:'+txt+';';
	case 'boolean':
		return 'b:'+( (txt)?'1':'0' )+';';
	case 'object':
		var i=0,k,ret='';
		for(k in txt){
			//alert(isNaN(k));
			if(!isNaN(k)) k = Number(k);
			ret += serialize(k)+serialize(txt[k]);
			i++;
		}
		return 'a:'+i+':{'+ret+'}';
	default:
		return 'N;';
		alert('var undefined: '+typeof(txt));return undefined;
	}
}

function unserialize(txt){
	var level=0,arrlen=new Array(),del=0,final=new Array(),key=new Array(),save=txt;
	while(1){
		switch(txt.substr(0,1)){
		case 'N':
			del = 2;
			ret = null;
		break;
		case 'b':
			del = txt.indexOf(';')+1;
			ret = (txt.substring(2,del-1) == '1')?true:false;
		break;
		case 'i':
			del = txt.indexOf(';')+1;
			ret = Number(txt.substring(2,del-1));
		break;
		case 'd':
			del = txt.indexOf(';')+1;
			ret = Number(txt.substring(2,del-1));
		break;
		case 's':
			del = txt.substr(2,txt.substr(2).indexOf(':'));
			ret = txt.substr( 1+txt.indexOf('"'),del);
			del = txt.indexOf('"')+ 1 + ret.length + 2;
		break;
		case 'a':
			del = txt.indexOf(':{')+2;
			ret = new Array();
			arrlen[level+1] = Number(txt.substring(txt.indexOf(':')+1, del-2))*2;
		break;
		case 'O':
			txt = txt.substr(2);
			var tmp = txt.indexOf(':"')+2;
			var nlen = Number(txt.substring(0, txt.indexOf(':')));
			name = txt.substring(tmp, tmp+nlen );
			//alert(name);
			txt = txt.substring(tmp+nlen+2);
			del = txt.indexOf(':{')+2;
			ret = new Object();
			arrlen[level+1] = Number(txt.substring(0, del-2))*2;
		break;
		case '}':
			txt = txt.substr(1);
			if(arrlen[level] != 0){alert('var missed : '+save); return undefined;};
			//alert(arrlen[level]);
			level--;
		continue;
		default:
			if(level==0) return final;
			alert('syntax invalid(1) : '+save+"\nat\n"+txt+"level is at "+level);
			return undefined;
		}
		if(arrlen[level]%2 == 0){
			if(typeof(ret) == 'object'){alert('array index object no accepted : '+save);return undefined;}
			if(ret == undefined){alert('syntax invalid(2) : '+save);return undefined;}
			key[level] = ret;
		} else {
			var ev = '';
			for(var i=1;i<=level;i++){
				if(typeof(key[i]) == 'number'){
					ev += '['+key[i]+']';
				}else{
					ev += '["'+key[i]+'"]';
				}
			}
			eval('final'+ev+'= ret;');
		}
		arrlen[level]--;//alert(arrlen[level]-1);
		if(typeof(ret) == 'object') level++;
		txt = txt.substr(del);
		continue;
	}
}

function previewQuestion(e, q)
{
  if(document.all)e = event;
  
  var obj = document.getElementById('question_preview_'+q);
  obj.style.display = 'block';
  var st = navigator.userAgent.toLowerCase().indexOf('safari')>=0 ? 0 : Math.max(document.body.scrollTop,document.documentElement.scrollTop);

  var leftPos = e.clientX + 20;
  var topPos = e.clientY - obj.offsetHeight -1 + st;
  
  // hack because of IE using the div container as the base point instead of the browser window
  if(navigator.userAgent.toLowerCase().indexOf('ie')>=0 && obj.parentNode.parentNode.parentNode.id == 'classify_div_interior')
  {
    if(!isNaN(obj.style.left))
	    obj.style.left = (obj.style.left + 50) + 'px';
  }
  else
  {
    if(leftPos<0)leftPos = 0;
    if(topPos<st)topPos = st;
    obj.style.left = leftPos + 'px';
    obj.style.top = topPos + 'px';
  }
}	

function hidePreviewQuestion(q)
{
	document.getElementById('question_preview_'+q).style.display = 'none';
}

function hoverDisplay(e, id)
{
  if(document.all)e = event;
  
  var obj = document.getElementById(id);
  obj.style.display = 'block';
  var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);

  var leftPos = e.clientX - obj.offsetWidth - 10;
  var topPos = e.clientY - obj.offsetHeight + st;
  
  if(leftPos < 0)
  	leftPos += obj.offsetWidth + 20;
  if(topPos<st)topPos = st;
  obj.style.left = leftPos + 'px';
  obj.style.top = topPos + 'px';
}

function hideHoverDisplay(id)
{
	document.getElementById(id).style.display = 'none';
}

function loadJavaScriptFile(url)
{
	var headID = document.getElementsByTagName("head")[0];
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  newScript.src = url;
  headID.appendChild(newScript);
}

var ajax_data_loaded = new Array();
function replaceContents(id, url, force_reload)
{
	if(force_reload || !ajax_data_loaded[id])
  {
  	if(typeof HTML_AJAX == 'undefined')
    {
    	loadJavaScriptFile("/js/ajax_server.php?client=all");
      
      var bodyID = document.getElementsByTagName("body")[0];
      var newDiv = document.createElement('div');
      newDiv.id = 'HTML_AJAX_LOADING';
      bodyID.appendChild(newDiv);
    }
      
  	setTimeout('HTML_AJAX.replace("'+id+'", "'+url+'")', 500);
    ajax_data_loaded[id] = true;
  }
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
  }
	return [curleft,curtop];
}

function orderDateReset()
{
	getObject('date_pre').selectedIndex = 0;
}

function orderDateSet(dp)
{
	var date_pre = dp ? dp : getObject('date_pre').value;
  var fm = getObject('fm');
  var fd = getObject('fd');
  var fy = getObject('fy');
  var tm = getObject('tm');
  var td = getObject('td');
  var ty = getObject('ty');
  
  var now = new Date();

  if(date_pre == 'months_12')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()+1);
    from.setMonth(now.getMonth()-12);
    if(from.getDate() == 1)
    	from.setMonth(from.getMonth()+1);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'months_6')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()+1);
    from.setMonth(now.getMonth()-6);
    if(from.getDate() == 1)
    	from.setMonth(from.getMonth()+1);

    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'months_3')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()+1);
    from.setMonth(now.getMonth()-3);
    if(from.getDate() == 1)
    	from.setMonth(from.getMonth()+1);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'months_1')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()+1);
    from.setMonth(now.getMonth()-1);
    if(from.getDate() == 1)
    	from.setMonth(from.getMonth()+1);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'days_7')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()-6);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'days_7_full')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()-7);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = (now.getDate()-1) < 10 ? "0"+(now.getDate()-1) : (now.getDate()-1);
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'to_date_year')
  {
  	fm.selectedIndex = 0;
    fd.selectedIndex = 0;
    fy.value = now.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'to_date_quarter')
  {
  	fm.selectedIndex = Math.floor(now.getMonth()/3)*3;
    fd.selectedIndex = 0;
    fy.value = now.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'to_date_month')
  {
  	fm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    fd.selectedIndex = 0;
    fy.value = now.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'to_date_week')
  {
    from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()-now.getDay());
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'previous_year')
  {
  	fm.selectedIndex = 0;
    fd.selectedIndex = 0;
    fy.value = now.getFullYear()-1;
    
    tm.selectedIndex = 11;
    td.selectedIndex = 30;
    ty.value = now.getFullYear()-1;
  }
  else if(date_pre == 'previous_quarter')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setMonth((Math.floor(now.getMonth()/3)-1)*3);
    
    to = new Date();
    to.setTime(now.getTime());
    to.setMonth(Math.floor(now.getMonth()/3)*3);
    to.setDate(-1);
    
  	fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.selectedIndex = 0;
    fy.value = from.getFullYear();
    
    tm.value = to.getMonth() < 9 ? "0"+(to.getMonth()+1) : (to.getMonth()+1);
    td.value = to.getDate()+1;
    ty.value = to.getFullYear();
  }
  else if(date_pre == 'previous_month')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setMonth(now.getMonth()-1);
    
    to = new Date();
    to.setTime(now.getTime());
    to.setMonth(now.getMonth());
    to.setDate(-1);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.selectedIndex = 0;
    fy.value = from.getFullYear();
    
    tm.value = to.getMonth() < 9 ? "0"+(to.getMonth()+1) : (to.getMonth()+1);
    td.value = to.getDate()+1;
    ty.value = to.getFullYear();
  }
  else if(date_pre == 'previous_week')
  {
    from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()-now.getDay()-7);
    
    to = new Date();
    to.setTime(now.getTime());
    to.setDate(now.getDate()-now.getDay()-1);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = to.getMonth() < 9 ? "0"+(to.getMonth()+1) : (to.getMonth()+1);
    td.value = to.getDate() < 10 ? "0"+to.getDate() : to.getDate();
    ty.value = to.getFullYear();
  }
  else if(date_pre == 'today')
  {
  	fm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    fd.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    fy.value = now.getFullYear();
    
  	tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
  else if(date_pre == 'yesterday')
  {
  	from = new Date();
    from.setTime(now.getTime());
    from.setDate(now.getDate()-1);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    fy.value = from.getFullYear();
    
    tm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    td.value = from.getDate() < 10 ? "0"+from.getDate() : from.getDate();
    ty.value = from.getFullYear();
  }
  else if(date_pre == 'pay_cycle_this_2')
  {
  	from = new Date();
    to = new Date();
    to.setMonth(now.getMonth());
    to.setDate(0);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = "16";
    fy.value = from.getFullYear();
    
    tm.value = to.getMonth() < 9 ? "0"+(to.getMonth()+1) : (to.getMonth()+1);
    td.value = to.getDate() < 10 ? "0"+to.getDate() : to.getDate();
    ty.value = to.getFullYear();
  }
  else if(date_pre == 'pay_cycle_this_1')
  {
  	from = new Date();
    to = new Date();
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = "01";
    fy.value = from.getFullYear();
    
    tm.value = to.getMonth() < 9 ? "0"+(to.getMonth()+1) : (to.getMonth()+1);
    td.value = "15";
    ty.value = to.getFullYear();
  }
  else if(date_pre == 'pay_cycle_last_2')
  {
  	from = new Date();
    from.setMonth(now.getMonth()-1);
    
    to = new Date();
    to.setMonth(now.getMonth());
    to.setDate(0);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = "16";
    fy.value = from.getFullYear();
    
    tm.value = to.getMonth() < 9 ? "0"+(to.getMonth()+1) : (to.getMonth()+1);
    td.value = to.getDate() < 10 ? "0"+to.getDate() : to.getDate();
    ty.value = to.getFullYear();
  }
  else if(date_pre == 'pay_cycle_last_1')
  {
  	from = new Date();
    from.setMonth(now.getMonth()-1);
    
    to = new Date();
    to.setMonth(now.getMonth());
    to.setDate(-1);
    
    fm.value = from.getMonth() < 9 ? "0"+(from.getMonth()+1) : (from.getMonth()+1);
    fd.value = "01";
    fy.value = from.getFullYear();
    
    tm.value = to.getMonth() < 9 ? "0"+(to.getMonth()+1) : (to.getMonth()+1);
    td.value = "15";
    ty.value = to.getFullYear();
  }
  else if(date_pre == 'all')
  {
  	fm.selectedIndex = 0;
    fd.selectedIndex = 0;
    fy.selectedIndex = 0;

    tm.value = now.getMonth() < 9 ? "0"+(now.getMonth()+1) : (now.getMonth()+1);
    td.value = now.getDate() < 10 ? "0"+now.getDate() : now.getDate();
    ty.value = now.getFullYear();
  }
}

function checkInternetConnection(f, submit_button)
{
	hide("error_msg");
  
  if(submit_button && getObject(submit_button+"_show") && getObject(submit_button+"_hide"))
  {
  	show(submit_button+"_hide");
    hide(submit_button+"_show");
  }
  
	if(HTML_AJAX)
  {
  	HTML_AJAX.grab("/check.php?force_refresh="+Math.floor(Math.random()*10000000000), function (result) {
    		if(result)
      		f.submit();
      },
      {
      	onError: function (e) {
        		msg = "Error submitting: internet connection latency detected.<br /><br />Please submit again."; 
            if(getObject("error_msg_text"))
            {
            	getObject("error_msg_text").innerHTML = msg;
              show("error_msg");
            }
            else
            	alert(msg.replace(/<br \/>/, "\n"));
            
            if(submit_button && getObject(submit_button+"_show") && getObject(submit_button+"_hide"))
            {
            	hide(submit_button+"_hide");
              show(submit_button+"_show");
            }
          },
        timeout: 5000
      }
    );
 		return false;
	}
  return true;
}

String.prototype.pad = function(l, s, t){
	return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
  + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
  + this + s.substr(0, l - t) : this;
};

function pause(millis) 
{
  var date = new Date();
  var curDate = null;
  
  do { curDate = new Date(); } 
  while(curDate-date < millis);
} 

function loadQuestionResults(resultid, order, sort, module)
{
	if(!resultid)
  	resultid = "";
  if(!order)
  	order = "";
  if(!sort)
  	sort = "";
  if(!module)
  	module = "test/test";
	url = "/modules/"+module+"/question_results.php?resultid="+resultid+"&order="+order+"&sort="+sort;
	HTML_AJAX.replace("question_results_div", url);
}

function checkStudentUsername(user)
{
	HTML_AJAX.grab('/modules/student/student_request.php?action=user_check&username='+user, 
  	function(result) {
    	if(result == "1")
      {
      	username_valid = true;
        document.getElementById("user_check").innerHTML = "";
      }
      else
      {
      	username_valid = false;
	     	document.getElementById("user_check").innerHTML = result;
      }
    }
  );
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
  {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
    {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
    }
  }
	return "";
}

function setSessionValue(name, value)
{
	HTML_AJAX.post('/set_session.php', {name: name, value: value});
}