function home(){
	window.location="index.php";
}

function redirect(location){
	window.location=location;
}

function verify(){
	if(document.form.news.value == "")
		alert("You must fill in the news field");
	else{
		document.form.submit();
	}
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}


function addCategory(){
	if(document.getElementById("catAdd").style.display == 'none'){
		//hidediv("catPreview");
		showdiv("catAdd");
		document.getElementById("catToggle").innerHTML = "(<a href=\"javascript:addCategory()\">Hide Uploader</a>)";
	}
	else{
		hidediv("catAdd");
		//showdiv("catPreview");
		document.getElementById("catToggle").innerHTML = "(<a href=\"javascript:addCategory()\">Add Image</a>)";
	}
}

/**
 * AJAX functions
 */

function createAJAX(){
	var xmlHttp;
	//Open the AJAX object
	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;
		  }
		}
	}
	
	return xmlHttp;
}

/*/Doesn't work.
function checkSession(){
	//Open the AJAX object
	var xmlHttp = createAJAX();
	
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4)
			{
			if(!xmlHttp.responseText)
				location.reload(true);
			}
		}
	xmlHttp.open("GET","./scripts/session_status.php",true);
	xmlHttp.send(null);
	//Re-check every five minutes until logout
	setTimeout('checkSession()',300000);
}//*/

//Background login
function acm_login(user,pass,ref){
	//Open the AJAX object
	var xmlHttp = createAJAX();
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.responseText==3){
				document.getElementById("auth_error").className="";
				document.getElementById("auth_error").innerHTML="Login successful. <br />Please wait you are redirected to the previous page.";
				setTimeout("redirect('" + ref + "')",2000);
			}
			else if(xmlHttp.responseText==10 || xmlHttp.responseText==11){
				document.getElementById("auth_error").className="error";
				document.getElementById("auth_error").innerHTML="Invalid username or password. <br />Please make sure you spelled everything correctly.";
			}
			else{
				document.getElementById("auth_error").className="error";
				document.getElementById("auth_error").innerHTML="An unexpected problem occurred while logging you in.  If this problem persists, please contact the webmaster.";
			}
		}
	}
	document.getElementById("auth_error").className="";
	document.getElementById("auth_error").innerHTML="Logging in...";
	var scriptLoc = "./scripts/acm_login.php?user=" + escape(user) + "&pass=" + escape(pass);
	xmlHttp.open("GET",scriptLoc,true);
	xmlHttp.send(null);
}

//Member management
function setUserPaid(id){
	//Open the AJAX object
	var xmlHttp = createAJAX();
	
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4)
		  {
			if(xmlHttp.responseText)
			  location.reload(true);
			else
			  document.getElementById(postDiv).innerHTML = "A problem occurred.  Try again later.";
		  }
		}
	var scriptLoc = "./scripts/setUserPaid.php?id=" + id;
	xmlHttp.open("GET",scriptLoc,true);
	var postDiv = "usr_" + id;
	document.getElementById(postDiv).innerHTML = "Updating user data...";
	xmlHttp.send(null);
}

//Delete news or event entries from the database
function remove(id){
	//Open the AJAX object
	var xmlHttp = createAJAX();
	
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4)
		  {
			if(xmlHttp.responseText)
			  location.reload(true);
		  }
		}
	if(confirm("Are you sure you want to delete this entry?")){
	  var scriptLoc = "./scripts/delete_entry.php?id=" + id;
	  xmlHttp.open("GET",scriptLoc,true);
	  var postDiv = "mod_" + id;
	  document.getElementById(postDiv).innerHTML = "Deleting...";
	  //sleep(5000);
	  xmlHttp.send(null);
	}
}

//Swaps the preview image on the new_entry page
function changeCatImage(){
	var xmlHttp = createAJAX();
	//Open the AJAX object
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4)
		  {
		  //alert("hi");
		  var imageInfo = xmlHttp.responseText.split("\n");
		  var imageSrc = "./images/AnnouncementsAndEvents/" + imageInfo[0];
		  //document.form.news.value=imageSrc;
		  //Set the preview to the new category image
		  document["catImg"].src=imageSrc;
		  //Make sure we size down, but don't size up.
		  if(imageInfo[1]>120)
		  	document["catImg"].width="120";
		  else
		  	document["catImg"].width=imageInfo[1];
		  //Adjust the height of the uploader panel
		  //document.form.news.value="Bottom: " + document.getElementById("catToggle").position;
		  //document.getElementById("catAdd").style.top=0-(document["catImg"].height-5);
		  }
		}
	//document.form.image.value=document.fileUpload.image.value;
	var id = document.form.image.value;
	//var id = document.form.category.options[document.form.category.selectedIndex].value;
	xmlHttp.open("GET","./scripts/getCatImg.php?id=" + id,true);
	xmlHttp.send(null);
}

function uploadImage(){
	document.form.action = "upload.php";
	document.form.encoding = "multipart/form-data";
	document.form.submit();
}

/*/Supposedly uploads an image in the background.  Which apparently is impossible.
//Try: https://blueprints.dev.java.net/complib/v2/file-upload.html
function oldUploadImage(){
	//Open the AJAX object
	var xmlHttp = createAJAX();
	
	xmlHttp.onreadystatechange=function()
		{
		document.form.news.value+="\nReady State " + xmlHttp.readyState;
		if(xmlHttp.readyState==4)
		  {
		  //alert("hi");
		  var returnInfo = xmlHttp.responseText.split("\n");
		  if(returnInfo[0]==1){
			document.form.news.value="\nDone.";
		  }
		  else{
		    document.form.news.value+="\nA problem occurred:\n";
			document.form.news.value+=returnInfo[0];
		  }
		  //var imageSrc = "./subpages/projects_images/" + imageInfo[0];
		  //document.form.news.value=imageSrc;
		  //Set the preview to the new category image
		  //document["catImg"].src=imageSrc;
		  //Make sure we size down, but don't size up.
		  //if(imageInfo[1]>120)
		  //	document["catImg"].width="120";
		  //else
		  //	document["catImg"].width=imageInfo[1];
		  //Adjust the height of the uploader panel
		  //document.form.news.value="Bottom: " + document.getElementById("catToggle").position;
		  //document.getElementById("catAdd").style.top=0-(document["catImg"].height-5);
		  }
		}
	//var id = document.fileUpload.image.value;
	//var id = document.form.category.options[document.form.category.selectedIndex].value;
	//xmlHttp.open("GET","./scripts/getCatImg.php?id=" + id,true);
	//xmlHttp.send(null);
	
	//document["catImg"].src="./images/loading.gif";
	document.form.news.value = "Working...\n";
	
	var params = "image=" + document.form.image.value;
	params += "&catName=" + document.form.catName.value;
	params += "&MAX_FILE_SIZE=" + document.form.MAX_FILE_SIZE.value;
	params += "&fileToUpload=" + document.form.fileToUpload.value;
	document.form.news.value += params;
	//Send data to the script
	xmlHttp.open("POST","./scripts/upload.php",true);
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length",params.length);
	xmlHttp.setRequestHeader("Connection","close");
	xmlHttp.send(params);
	
	//xmlHttp.open("POST", "scripts/upload.php", true);
	//xmlHttp.setRequestHeader("image", document.fileUpload.image.value);
	//xmlHttp.setRequestHeader("catName", document.fileUpload.catName.value);
	//xmlHttp.setRequestHeader("MAX_FILE_SIZE", document.fileUpload.MAX_FILE_SIZE.value);
	//xmlHttp.setRequestHeader("fileToUpload", document.fileUpload.fileToUpload.value);
	//xmlHttp.send(null);
    //http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //http_request.setRequestHeader("Content-length", parameters.length);
    //http_request.setRequestHeader("Connection", "close");
    //http_request.send(parameters);
}//*/