/***CLASS - SiteColor***************************/
/***********************************************/
function getColor() //SiteColor method
{	return this.currColor;
}
function setColor(requested) //SiteColor method
{	if(this.active) // must be on to work!
	{	if(this.limit) // if limit set on page, setColor() only works once (ie. no user color changes)
		{	this.active = false;}
		this.oldColor = this.currColor;
		this.currColor = requested;
		var off = document.getElementsByTagName('td');
		for(var i=0;i<off.length;i++)
		{	if(off[i].id == this.oldColor)
			{	off[i].bgColor = "";
				off[i].className = 'nav_section_inactive';
			}
		}
		
		var source = document.getElementById(this.currColor).childNodes[0].innerHTML;
		if(this.currColor != 'none' &&
		   window.location.href != 'http://www.quant.ku.edu/index.html' && 
		   window.location.href != 'http://www.quant.ku.edu/' &&
		   window.location.href != 'http://quant.ku.edu/index.html' &&
		   window.location.href != 'http://quant.ku.edu/') //only Quant Home page will do this
		{	
			document.getElementById('black_menu').innerHTML = page_title; //found in header of each page
			document.getElementById('home_menu').style.visibility = 'visible';
		}
		var on = document.getElementsByTagName('td');
		for(i=0;i<on.length;i++)
		{	if(on[i].id == this.currColor)
			{	on[i].bgColor = this[this.currColor];
				on[i].className = 'nav_section_active';
			}else if(on[i].id == 'home_title' || on[i].id == 'multi_color' )
			{	on[i].bgColor = this[this.currColor];
			}
		}
	}
}

function SiteColor() //class definition
{	this.currColor = "none";
	this.oldColor = "none";
	this.getColor = getColor;
	this.setColor = setColor;
	this.active = true; //turn color object on or off (you shouldn't need to change this)
	this.limit = false; //true to prevent users from changing colors (set this in header of secondary pages)
	//colorhex  values
	this.none = '';
	this.yellow = '#DDCD5B';
	this.green = '#3ACC7F';
	this.red = '#F22A39';
	this.blue = '#0368F9';
	this.dark_blue = '#04045B';
}

/***END CLASS - siteColor***********************/
/***********************************************/

/***Library Search Functions********************/
/***********************************************/
/*************LIBRARY GLOBALS********/

var blockNote = 0; //prevents user from editing more than 1 note at a time. When set to 1, liveNote() cannot be used.
var oldComment = ""; //stores original comment in case user decides to cancel changes

/*********END LIBRARY GLOBALS********/

function searchLib(library,type)
{
	if(typeof(type)=='undefined')
		type="main_search";
	
	document.getElementById('progress').style.display = 'inline';
	var title = document.getElementById('title').value;
	var author = document.getElementById('author').value;
	var flags = document.getElementById('flags');
	var total = 0;
	var flagInt = 0;
	for(var i=0;i<flags.length;i++)
	{	if(flags.options[i].selected)
		{	flagInt += Math.pow(2,flags.options[i].id);
			total++;
		}
	}
	if(total > 2)
	{	var ok = confirm('Warning: Too many parameters may limit search results. Continue?');
		if(!ok)
		{	document.getElementById('progress').style.display = 'none';
			return;
		}
	}
	if(author!='' || title!='' || flagInt!=0)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{	document.getElementById('progress').style.display = 'none';
			alert ("Your browser does not support important functions needed for this application.");
			return;
		}
		url="../scripts/process_libraries.php";
		url=url+"?process=search";
		url=url+"&type="+type;
		url=url+"&library="+library;
		url=url+"&title="+title;
		url=url+"&author="+author;
		url=url+"&flags="+flagInt;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=searchReturned;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}else
	{
		document.getElementById('progress').style.display = 'none';
		alert('No search options selected!');
		return;
	}
}

function flagSearch(library,flag) //called by flag links in search results, searches with only 1 flag parameter
{	
	if(typeof(type)=='undefined')
		type="main_search";
		
	document.getElementById('progress').style.display = 'inline';
	var flags = document.getElementById('flags');
	for(var i=0;i<flags.length;i++)
	{	if(flags.options[i].value == flag)
		{	flagInt = Math.pow(2,flags.options[i].id);
			break;
		}
	}
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	document.getElementById('progress').style.display = 'none';
		alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	url="../scripts/process_libraries.php";
	url=url+"?process=search";
	url=url+"&type="+type;
	url=url+"&library="+library;
	url=url+"&title=";
	url=url+"&author=";
	url=url+"&flags="+flagInt;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=searchReturned;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function authorSearch(library,author) //called by author links in search results, searches with only 1 flag parameter
{	
	if(typeof(type)=='undefined')
		type="main_search";
	
	document.getElementById('progress').style.display = 'inline';
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	document.getElementById('progress').style.display = 'none';
		alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	url="../scripts/process_libraries.php";
	url=url+"?process=search";
	url=url+"&type="+type;
	url=url+"&library="+library;
	url=url+"&title=";
	url=url+"&author="+author;
	url=url+"&flags=0";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=searchReturned;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function searchReturned()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 	{
	 	try 
		{	document.getElementById("results_container").innerHTML=xmlHttp.responseText;
		}
		catch (e) 
		{	// IE fails unless you create a new element to wrap string in
			var container = document.getElementById('results_container');
			// First, check for old wrapper...
			if(document.getElementById('wrapping'))
			{	
				var olddiv = document.getElementById('wrapping');
				container.removeChild(olddiv);
			}
  			var wrapping = document.createElement('div');
  			wrapping.innerHTML = xmlHttp.responseText;
			wrapping.setAttribute('id','wrapping');
 			container.appendChild(wrapping);
		}
	 }
 
	 document.getElementById('progress').style.display = 'none';
	 alternate('results',0);
	 var results = document.getElementById("results");
	 var result_rows = results.getElementsByTagName("tr");
	 
	 if(xmlHttp.responseText.length > 700)
	 	document.getElementById('result_note').innerHTML = "<em>Search returned "+result_rows.length+" results.</em>";
	else
		document.getElementById('result_note').innerHTML = "";
		
	 commentFix = 1; //for temp fix described in Global variable section of this file
}
function reprint()
{
xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	//document.getElementById('progress').style.display = 'none';
		alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	url="../forms/process_reprints.php";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=searchReturned;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
	
	
function liveNote(id)
{	if(!blockNote)
	{	blockNote = true;
		var library = document.getElementById('library').value;
		var dbid = id.substring(4);
		oldComment = document.getElementById(id).childNodes[0].innerHTML;
		var formattedComment = '';
		formattedComment = (oldComment.split('<br>')).join('\n');
		formattedComment = (formattedComment.split('<BR>')).join('\n');
		document.getElementById(id).innerHTML='<textarea id="edit'+id+'" cols="107" rows="3">'+formattedComment+'</textarea><input name="save" type="button" value="save" onclick="saveNote(\''+id+'\',\''+library+'\',\''+dbid+'\')" />&nbsp;&nbsp;<input name="cancel" type="button" value="cancel" onclick="cancelNote(\''+id+'\');" /></div><br />';
	}
}

function cancelNote(id)
{	//oldComment = (oldComment.split('\n')).join('<br>');
	document.getElementById(id).innerHTML = '<a href="javascript:liveNote(\''+id+'\');">'+oldComment+'</a>';
	blockNote = false;
	
	commentFix = 0;
}

function saveNote(id,library,dbid)
{	var comment = document.getElementById('edit'+id).value;
	comment = (comment.split('\n')).join('\\n');
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	document.getElementById('progress').style.display = 'none';
		alert ("Your browser does not support important functions needed for this application.")
		return;
	}
	url="../scripts/process_libraries.php";
	url=url+"?process=annotate";
	url=url+"&library="+library;
	url=url+"&comment="+comment;
	url=url+"&dbid="+dbid;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	comment = (comment.split('\\n')).join('<br>');
	document.getElementById(id).innerHTML = '<a href="javascript:liveNote(\''+id+'\');">'+comment+'</a>';
	blockNote = false;
	commentFix = 0;
}

function saveReferences(library,name)
{	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	document.getElementById('progress').style.display = 'none';
		alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	
	url = "../scripts/process_libraries.php";
	url += "?library="+library;
	url += "&process=references";
	var tempGet = collectSelected(name);
	if(tempGet == 0)
	{	document.getElementById('progress').style.display = 'none';
		alert ("No references selected.");
		return;
	}else
	{	url += tempGet;
	}
	
	xmlHttp.onreadystatechange=download;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function deleteEntry(library,name)
{
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	document.getElementById('progress').style.display = 'none';
		alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	
	url = "../scripts/process_admin.php";
	url += "?process=delete_statslib";
	var tempGet = collectSelected(name);
	if(tempGet == 0)
	{	document.getElementById('progress').style.display = 'none';
		alert ("No entries selected.");
		return;
	}
	if(!confirm("WARNING: This will permanently delete the selected entries. Do you wish to continue?"))
	{	
		return;
	}
	
	url += tempGet;
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			alert(xmlHttp.responseText);
			window.location = 'index.php?user='+user+'&pass='+pass+'&mod=mod_files';
 		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function collectSelected(name)
{ 
	var urlPiece="&sid="+Math.random();
	var count = 0;
	var choices = document.getElementsByName(name)

	for(var i=0;i < choices.length;i++)
	{	
		if(choices[i].checked)
		{	urlPiece=urlPiece+"&choice"+ count +"="+choices[i].value;
			count++;
		}
	}
	//url=url+"&number_of="+count;
	urlPiece = (count == 0)? 0 : urlPiece+"&number_of="+count; //return 0 if no choices selected
	return urlPiece;
}

function selectAll(name,caller)
{	var choices = document.getElementsByName(name);
	var head = document.getElementById(caller);
	for(var i = 0;i < choices.length;i++)
	{	
		choices[i].checked = (head.checked)?true:false;
	}
}

/***Admin Page Functions************************/
/***********************************************/
/*************ADMIN GLOBALS**********/

var currMod = 'mod_home'; //keeps track of current open module

/*********END ADMIN GLOBALS**********/

function editUser(id,user,pass)
{
	var info = document.getElementsByName('user'+id);
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	
	url = "../scripts/process_admin.php";
	url += "?id="+id;
	
	if(info[0].value=='' && info[1].value=='' && info[2].value=='' && info[3].value=='' && info[4].value=='') //all fields empty, delete user
	{
		var confirmation = confirm("Are you sure you want to delete this user?");
		if(confirmation)
		{
			url += "&process=delete_user";
		}
		else
		{
			return;
		}
	}
	else //edit user
	{
		url += "&process=edit_user";
		url += "&name="+info[0].value;
		url += "&user="+info[1].value;
		url += "&pass="+info[2].value;
		url += "&access="+info[3].value;
		url += "&university="+info[4].value;
	}
	
	xmlHttp.onreadystatechange=function() 
	{		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			alert(xmlHttp.responseText);
			window.location = 'index.php?user='+user+'&pass='+pass+'&mod=mod_users';
 		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function addUser(user,pass)
{
	var info = document.getElementsByName('new');
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	
	url = "../scripts/process_admin.php";
	url += "?process=add_user";
	url += "&name="+info[0].value;
	url += "&user="+info[1].value;
	url += "&pass="+info[2].value;
	url += "&access="+info[3].value;
	url += "&university="+info[4].value;
	
	xmlHttp.onreadystatechange=function() 
	{		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if(xmlHttp.responseText.substr(0,23) == 'Successfully added user')
			{
				alert(xmlHttp.responseText);
				window.location = 'index.php?user='+user+'&pass='+pass+'&mod=mod_users';
			}
			else //alert error
			{
				alert(xmlHttp.responseText);
			}
 		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function formatSelected(area,tagstart,tagend) 
{
  	var target = document.getElementById(area);
	if (target.setSelectionRange) //mozilla and opera
	{

		var pre = target.value.substring(0,target.selectionStart) ;
		var mid = target.value.substring(target.selectionStart,target.selectionEnd) ; 
		var post = target.value.substring(target.selectionEnd,target.value.length);
		
		target.value = pre + tagstart + mid + tagend + post;
  	}
  	else //ie
	{
  		var selectedText = document.selection.createRange().text;
        
        if (selectedText != "" && target.value.search(selectedText)) 
		{
            var newText = tagstart + selectedText + tagend;
            document.selection.createRange().text = newText;
        }  
  	}
}

function lib_add_prep()
{
	var flags_new = document.getElementById('flags_new');
	var total = 0;
	var flagInt = 0;
	document.getElementById('finish').value = "1";
	
	for(var i=0;i<flags_new.length;i++)
	{	if(flags_new.options[i].selected)
		{	flagInt += Math.pow(2,flags_new.options[i].id);
			total++;
		}
	}
	if(total > 3)
	{	
		var ok = confirm('Warning: Too many flags may limit search effectiveness. Continue?');
		if(!ok)
			document.getElementById('finish').value = "0";
	}
	else if(total == 0)
	{
		alert('You must select at least one flag for this item');
		document.getElementById('finish').value = "0";
	}
	
	document.getElementById('flagInt').value = "" + flagInt + "";
}

/***Coded Styles********************************/
/***********************************************/

function showhide(id)
{ 
	if (document.getElementById)
	{	var obj = document.getElementById(id); 
		(obj.style.display == "none")?obj.style.display = "inline":obj.style.display = "none"; 
	} 
} 

function showhide_block(id)
{ 
	if (document.getElementById)
	{	var obj = document.getElementById(id); 
		(obj.style.display == "none")?obj.style.display = "block":obj.style.display = "none"; 
	} 
} 

function switch_arrow(id) //very messy and ugly, needs to be cleaned up!
{
	if (document.getElementById)
	{
		var obj = document.getElementById(id);
		if(obj.style.height == "10px")
		{
			obj.innerHTML = '<img src="http://www.quant.ku.edu/images/arrow_down.png" alt="arrow_icon" height="7px" width="7px" />'; 
			obj.style.height = "11px";
		}
		else if(obj.style.height == "11px")
		{
			obj.innerHTML = '<img src="http://www.quant.ku.edu/images/arrow_right.png" alt="arrow_icon" height="7px" width="7px" />'; 
			obj.style.height = "10px";
		}
	} 
}

function alternate(target,rev)
{
	var mytable = document.getElementById(target);
	var myrows = mytable.getElementsByTagName("tr");
	for(var j=0;j < myrows.length;j++)
	{	if((j%2) == 1)
			{	myrows[j].bgColor = (!rev)? '#EEEEEE':'#FFFFFF';
			}
		else
			{	myrows[j].bgColor = (!rev)? '#FFFFFF':'#EEEEEE';	
			}
	}
}

function blinker() //for blinking text (mostly just used for "processing..." or "searching..." messages)
{	if (!document.all) 
	{	return;
	}else
	{	for(i=0;i<document.all.tags('blink').length;i++)
		{	mytext=document.all.tags('blink')[i];
     		mytext.style.visibility=(mytext.style.visibility=='visible')?'hidden':'visible';
		}
 	}
}


/***AJAX Specific Functions*********************/
/***********************************************/

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function download()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{	var target = xmlHttp.responseText;
		document.getElementById('progress').style.display = 'none';
	 	if(target != '0')
		{	window.location = target;
		}else
		{	alert("Error processing request. If problems persist, contact donaldquixote@gmail.com");
		}
 	}
}

function Random_Quote()
{
	
// ==============================================
// Copyright 2004 by CodeLifter.com
// Free for all; but please leave in this header.
// ==============================================

var Quotation=new Array() // do not change this!

// Set up the quotations to be shown, below.
// To add more quotations, continue with the
// pattern, adding to the array.  Remember
// to increment the Quotation[x] index!

Quotation[0] = "Time is of the essence! Comb your hair.";

// ======================================
// Do not change anything below this line
// ======================================
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
}

function Selected_Reprint(choice)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{	document.getElementById('progress').style.display = 'none';
		alert ("Your browser does not support important functions needed for this application.");
		return;
	}
	
	document.getElementById('reprint_form');
	
	url = "../forms/reprint_request.php";
	var tempGet = collectSelected(choice);
	if(tempGet == 0)
	{	document.getElementById('progress').style.display = 'none';
		alert ("No pdf selected.");
		return;
	}else
	{	url += tempGet;
	}
	
	xmlHttp.onreadystatechange=download;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}