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() + "; path=/; domain=joynotes.net;");
}
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 showPopUp(message, id)
{
   if (id)
      div = document.getElementById(id);
   else
      div = document.getElementById('popup');
   if (message && message != "")
      div.innerHTML = message;
   div.style.visibility = 'visible';
   var t=setTimeout("closePopUp("+id+")",3000);
}
function showLicenseInfo(e)
{
	div=document.getElementById('popup');
	div.innerHTML = "This document is licensed under the Attribution-<br />Noncommercial-Share Alike 3.0 United States License.";
	div.style.visibility = 'visible';
	div.style.width = "200px";
	div.style.height="75px";
	div.style.zIndex = 5;
	div.style.outline = "1px solid #cde9a7";
	//div.style.border = "10px solid #cde9a7"; 
	var x;var y;
	if(e.pageX)
	{
	    x = e.pageX+5;
	    y = e.pageY-5-80-20;
	}
	else
	{
	    x = e.clientX+5;
	    y = e.clientY-5-80-20;
	}
	div.style.left = x+"px";
	div.style.top = y+"px";
}
function hideLicenseInfo()
{
	div=document.getElementById('popup');
	div.innerHTML = "";
	div.style.visibility = 'hidden';
	div.style.width = "40%";
	div.style.left = "30%";
	div.style.top = "300px";
}
function showFlagInfo(e)
{
	div=document.getElementById('popup');
	div.innerHTML = "If you feel this document is inappropriate, please click this link to call it to our attention and flag it.  Thanks.";
	div.style.visibility = 'visible';
	div.style.width = "200px";
div.style.height="75px";
div.style.zIndex = 5;
div.style.outline = "1px solid #cde9a7";
//div.style.border = "10px solid #cde9a7"; 
       var x;
       var y;
	if(e.pageX)
	{
	    x = e.pageX+5;
	    y = e.pageY-5-80-20;
	}
	else
	{
	    x = e.clientX+5;
	    y = e.clientY-5-80-20;
	}
	        div.style.left = x+"px";
	        div.style.top = y+"px";
}
function hideFlagInfo()
{
	div=document.getElementById('popup');
	div.innerHTML = "";
	div.style.visibility = 'hidden';
	div.style.width = "40%";
	div.style.left = "30%";
	div.style.top = "300px";
}
function closePopUp(id)
{
   if (id)
      div = document.getElementById(id);
   else
      div = document.getElementById('popup');
   div.style.visibility = 'hidden';
}
function checkConfirmTerms()
{
     var x;
     x = getCookie('terms');
     if ( x == "1")
     {
        closePopUp("confirmTerms");
      }
      else
      {
         showPopUp("", "confirmTerms");
       }
}
function confirmTerms()
{
   setCookie("terms", "1", 365);
   closePopUp("confirmTerms");
}
function checkDocEdu()
{
     var x;
     x = getCookie('docEdu');
     if ( x == "1")
     {
        closePopUp("docEdu");
      }
      else
      {
         showPopUp("", "docEdu");
       }
}
function confirmDocEdu()
{
   setCookie("docEdu", "1", 365);
   closePopUp("docEdu");
}
function searchText() {
  var text = document.getElementById('search').value;
  if(text == "search") {
    document.getElementById('search').style.color = '#2e2e2e';
    document.getElementById('search').value = "";
  } else if(text == "") {
    document.getElementById('search').style.color = 'gray';
    document.getElementById('search').value = "search";
  }
}
function toDoOnLoad()
{
  //alert('loaded');
  window.dhx_globalImgPath = "includes/imgs/"; 
  //var z=dhtmlXComboFromSelect("findSchool");
  //z.enableFilteringMode(true);
}
 // Removes leading whitespaces
function LTrim( value ) {

	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}
// Removes ending whitespaces
function RTrim( value ) {

	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}
// Removes leading and ending whitespaces
function trim( value ) {

	return LTrim(RTrim(value));

}
function checkID(id)
{
	if ( trim( document.getElementById(id).value ) == "" )
	{
		document.getElementById(id+"Star").style.visibility = 'visible';
		return false;
	}
	else
	{
		document.getElementById(id+"Star").style.visibility = 'hidden';
		return true;
	}
}
function checkEmail()
{
	var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      	var x = xmlHttp.responseText;
      	if ( x == "1")
      	{
      		document.getElementById("emailStar").style.visibility = "hidden";
      		document.getElementById('emailMessage').innerHTML = "";
      		return true;
      	}
      	else
      	{
      		document.getElementById("emailStar").style.visibility = "visible";
      		document.getElementById('emailMessage').innerHTML = x;
      		return false;
      	}
      }
    }
    var email = document.getElementById("email").value;
  xmlHttp.open("GET","ajax/checkEmail.php?email="+email,true);
  xmlHttp.send(null);
  //return xmlHttp.onreadystatechange;
}
function check()
{
	var allSet = true;
	if ( !checkID('name') )
		allSet = false;
	checkEmail();
	if ( document.getElementById("emailStar").style.visibility == "visible" )
		allSet = false;
	if ( !checkID('pw') )
		allSet = false;
	if ( !checkID('findSchoolSELECT') )
		allSet = false;
        if ( !checkChkBox('terms') )
                allSet = false;

	if (allSet)
		document.getElementById('btnSubmit').disabled = false;
	else
		document.getElementById('btnSubmit').disabled = true;
}
function checkChkBox(id)
{
	if ( document.getElementById(id).checked == false)
	{
		document.getElementById(id+"Star").style.visibility = 'visible';
		return false;
	}
	else
	{
		document.getElementById(id+"Star").style.visibility = 'hidden';
		return true;
	}
}
function newNotebook(docID)
{
	var noteName = prompt("What would you like to name your new notebook?","");
		var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      	var x = xmlHttp.responseText;
		if (x == 0)
			alert("I'm sorry there was an error.");
		else
		{
			var nList = document.getElementById('notebooks');
			var last = document.getElementById("addNewNotebook");
			if ( nList )
			{
				var newLi = document.createElement('li');
				var newA = document.createElement('a');
				newA.innerText = x;
				newA.innerHTML = x;
				newLi.appendChild(newA);
				allLi = nList.childNodes;
				//nList[allLi.length] = newLi;
				//nList[allLi.length+1] = last;
				nList.insertBefore(newLi, last);
				//nList.appendChild(newLi);
			}
		}
      }
    }
  xmlHttp.open("GET","/ajax/addNotebook.php?id="+docID+"&name="+noteName,true);
  xmlHttp.send(null);
  //return xmlHttp.onreadystatechange;

}
function saveDoc(docID, notebookID)
{
		var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      	var x = xmlHttp.responseText;
		if (x == 1)
			showPopUp("Saved Successfully", "");
      }
    }
  xmlHttp.open("GET","/ajax/saveToNotebook.php?id="+docID+"&notebook="+notebookID,true);
  xmlHttp.send(null);
  //return xmlHttp.onreadystatechange;
}
function starOn(s, i)
{
	if ( i == 1)
	{
		document.getElementById("rating").value = s;
		starOff(s+1);
	}
	if (s > 1)
	{
		starOn(s-1, 0);
	}
	if (s >= 1)
	{
		document.getElementById("star"+s).src = "/images/star.gif";
	}
}
function starOff(s)
{
	if ( s <= 5 )
	{
		document.getElementById("star"+s).src = "/images/starDull.gif";
	}
	if ( s < 5)
	{
		starOff(s+1);
	}
}
function changePublic(id, pubpriv)
{
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
	      }
	    }
	  xmlHttp.open("GET","/ajax/changePublic.php?id="+id+"&pubpriv="+pubpriv,true);
	  xmlHttp.send(null);
}
function saveDisplay(type)
{
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
			var x = xmlHttp.responseText;
			showPopUp(x, "");
	      }
	    }
	  xmlHttp.open("GET","/ajax/saveDisplay.php?display="+type,true);
	  xmlHttp.send(null);
}
function saveNewTag2(id)
{
	var xmlHttp;
	var s;
	k = document.getElementById("tagKey").value;
	s = document.getElementById("tags"+k).value;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x == 0)
			{
				alert("You have reached the maximum number of tags, so this new tag could not be added.");
			}
			else if ( x==1)
			{
				alert("I'm sorry.  There was an error.");
			}
			else
			{
				var tagList = document.getElementById('currentTagList');
				tagList.innerHTML += "<span><a href='#'>"+x+"</a></span>";
				document.getElementById("tags"+k).value = "";
			}
	      }
	    }
	  xmlHttp.open("GET","/ajax/addTag.php?id="+id+"&s="+s,true);
	  xmlHttp.send(null);
}
function saveNewTag(id)
{
	var xmlHttp;
	var s;
	k = document.getElementById("tagKey").value;
	s = document.getElementById("tags"+k).value;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x == 0)
			{
				alert("You have reached the maximum number of tags, so this new tag could not be added.");
			}
			else if ( x==1)
			{
				alert("I'm sorry.  There was an error.");
			}
			else
			{
				var tagList = document.getElementById('tags');
				if ( tagList )
				{
					var newLi = document.createElement('li');
					var newA = document.createElement('a');
					newA.innerText = x;
					newA.innerHTML = x;
					newLi.appendChild(newA);
					tagList.appendChild(newLi);
					document.getElementById("tags"+k).value = "";
				}
			}
	      }
	    }
	  xmlHttp.open("GET","/ajax/addTag.php?id="+id+"&s="+s,true);
	  xmlHttp.send(null);
}
function saveDocName(id)
{
	var xmlHttp;
	var name;
	name= document.getElementById("docName").value;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x!=1)
			{
				alert("I'm sorry.  There was an error.");
			}
			else
			{
				showPopUp("Successfully saved.");
			}
	      }
	    }
	  xmlHttp.open("GET","/ajax/saveDocName.php?id="+id+"&s="+name,true);
	  xmlHttp.send(null);
}
function saveInfo(id)
{
	var xmlHttp;
	var info;
	info = document.getElementById("info").value;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x!=1)
			{
				alert("I'm sorry.  There was an error.");
			}
			else
			{
				showPopUp("Successfully saved.");
			}
	      }
	    }
	  xmlHttp.open("GET","/ajax/saveInfo.php?id="+id+"&s="+info,true);
	  xmlHttp.send(null);
}
function saveSchool(id)
{
	var xmlHttp;
	var list;
	list = document.getElementById("findSchoolSELECT");
	schoolID = list.options[list.selectedIndex].value;
	//alert(schoolID);
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x==1)
			{
				alert("I'm sorry.  There was an error.");
			}
			else
			{
				label = document.getElementById('currentSchool');
				if ( label )
					label.innerHTML = "<dt>Current School: "+x+"</dt>";
			}
	      }
	    }
	  xmlHttp.open("GET","/ajax/saveSchool.php?id="+id+"&s="+schoolID,true);
	  xmlHttp.send(null);
}
function saveClasstoDB(id)
{
	var xmlHttp;
	var className;
	className = document.getElementById("classHidden").value;
	//alert(schoolID);
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x==1)
			{
				alert("I'm sorry.  There was an error.");
			}
			else
			{
				label = document.getElementById('currentClass');
				if ( label )
					label.innerHTML = "<dt>Current Class: "+x+"</dt>";
			}
	      }
	    }
	  xmlHttp.open("GET","/ajax/saveClass.php?id="+id+"&s="+className,true);
	  xmlHttp.send(null);
}
function deleteTag_Edit(id, tag)
{
	//alert(tag);
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x==0)
			{
				li = document.getElementById('tag-'+tag);
				tagList = document.getElementById('currentTagList');
				if (tagList && li)
				{
					tagList.removeChild(li);
				}
			}
			else
			{
				alert("I'm sorry.  There was an error in deleting this tag.");
			}

	      }
	    }
	  xmlHttp.open("GET","/ajax/deleteTag.php?id="+id+"&tag="+tag,true);
	  xmlHttp.send(null);
}
function deleteTag(id, tag)
{
	//alert(tag);
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x==0)
			{
				li = document.getElementById('tag-'+tag);
				tagList = document.getElementById('tags');
				if (tagList && li)
				{
					tagList.removeChild(li);
				}
			}
			else
			{
				alert("I'm sorry.  There was an error in deleting this tag.");
			}

	      }
	    }
	  xmlHttp.open("GET","/ajax/deleteTag.php?id="+id+"&tag="+tag,true);
	  xmlHttp.send(null);
}
function deleteNotebook(id)
{
	//alert(tag);
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  xmlHttp.onreadystatechange=function()
	    {
	    if(xmlHttp.readyState==4)
	      {
	      	var x = xmlHttp.responseText;
			if ( x==0)
			{
				div = document.getElementById('notebook'+id);
				div.innerHTML = "";
			}
			else
			{
				alert("I'm sorry.  There was an error in deleting this notebook.");
			}

	      }
	    }
	  xmlHttp.open("GET","/ajax/deleteNotebook.php?id="+id,true);
	  xmlHttp.send(null);
}
function arrowDown(id)
{
	//alert('down');
	var a = document.getElementById('arrow'+id);
	a.style.backgroundImage = "url(http://joynotes.net/images/arrow-down-mint.png)";
}
function arrowRight(id)
{
	var a = document.getElementById('arrow'+id);
	a.style.backgroundImage = "url('http://joynotes.net/images/arrow-right.png')";
}
function openInfo(id)
{
	var val = document.getElementById('isOpen'+id).value;
	var div = document.getElementById('docInfo'+id);
	if (val == 0) //NEEDS TO BE OPENED
	{
		div.style.display = "block";
		document.getElementById('isOpen'+id).value = 1;
	}
	else //NEEDS TO BE CLOSED
	{
		div.style.display = "none";
		document.getElementById('isOpen'+id).value = 0;
	}
}
function basicSearch()
{
	var query = document.getElementById('search').value;
	query = replace(/ /g, ";");
	var current = window.location;
	//alert(current);
	window.location = "/search?q="
}
function switchPage(newPage)
{
div = document.getElementById("page"+newPage);
oldPageNum = document.getElementById("lastPage").value;

oldA = document.getElementById("pageNum"+oldPageNum);
oldA.style.fontSize = "inherit";
newA = document.getElementById("pageNum"+newPage);
newA.style.fontSize = "1.5em";

document.getElementById("lastPage").value = newPage;
oldDiv = document.getElementById("page"+oldPageNum);
oldDiv.style.display = "none";
div.style.display = "block";
}