// JavaScript Document
//window.onload = initAll();
var xhr = false;

function initAll()
{
	//document.getElementById("searchfield").onkeyup = searchSuggest;

	xhr = getXMLHttpRequestObject();
}



// Create XMLHttpRequest Object
function getXMLHttpRequestObject()
{
	var xmlHttp = false;
	
	if(window.XMLHttpRequest)
	{
		xmlHttp = new XMLHttpRequest();
	}else
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										 "MSXML2.XMLHTTP.5.0",
										 "MSXML2.XMLHTTP.4.0",
										 "MSXML2.XMLHTTP.3.0",
										 "MSXML2.XMLHTTP",
										 "Microsoft.XMLHTTP");
		
		for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}catch(e){}
		}
	}

	return xmlHttp;
}

// Get the URL parameters
function gup( name )
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	
	if( results == null )    
		return "";  
	else    
		return results[1];
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function checkSelection(dlIterm, type)
{
	if(dlIterm[0].selected)
	{
		if(type == "i")
		{
			alert("Please select an instructor.");
		}
		else if(type == "s")
		{
			alert("Please select a course section.");
		}
		return false;
	}
	
	return true;
}

function checkLogin()
{
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	
	//alert("login");
	
	if(trim(username) == "")
	{
		alert("Username cannot be empty.");
		return false;
	}
	
	if(trim(password) == "")
	{
		alert("Password cannot be emtpy.");
		return false;
	}
	
	return true;
}

function addTextbook(cmd)
{
	var title = document.getElementById("txtTitle").value;
	var isbn = document.getElementById("txtIsbn").value;
	var author = document.getElementById("txtAuthor").value;
	var publisher = document.getElementById("txtPublisher").value;
	
	if(trim(title) == "")
	{
		alert("Title field cannot be empty.");
		return;
	}
	//alert(window.location.href);
	if(trim(isbn) == "")
	{
		alert("ISBN field cannot be empty.");
		return;
	}
	
	if(trim(author) == "")
	{
		alert("Author(s) field cannot be emtpy.");
		return;
	}
	
	if(trim(publisher) == "")
	{
		alert("Publisher field cannot be empty.");
		return;
	}
	
	var target = document.getElementById("divErrorMessage");
		
	target.innerHTML = "<img src=\"../images/indicator_medium.gif\" alt=\"Please wait...\" /> Processing...";
	
		
	xhr = getXMLHttpRequestObject();
	var url = "insert_textbook.php";
	
	var dataToSend = "term="+gup("term")+"&sid="+gup("sid")+"&title="+encodeURIComponent(title) + "&isbn="+isbn+"&author="+encodeURIComponent(author)+"&publisher="+encodeURIComponent(publisher);
	//alert(dataToSend);
	//url += "?" + dataToSend;
		
	if(xhr)
	{
		xhr.open("POST", url);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				//callbackFunction(XMLHttpRequestObject.responseText); 
				
				// target.innerHTML = xhr.responseText;
				var result = xhr.responseText;
				if(result == '0')
				{
					alert("Data missing, please try again");
				}
				else if(result == '1')
				{
					target.innerHTML = "WARNING: This book [ISBN: " + isbn + "] is already exist for the course [" + gup("sid") + "]. Please try to add a different book.";
				}
				else if(result == '2')
				{
					target.innerHTML = "Error: Unexpected error occured. Fail adding textbook, please try again.";
				}
				else if(result == '3')
				{
					//alert("This book is successfully inserted ");
					if(cmd == '1')
					{
						//alert("Adding another textbook...");
						window.location = window.location.href;
					}
					else if(cmd == '2')
					{
						// alert("Finish adding textbook...");	
						$grp=gup("grp");
						if($grp == '4')
						{
							window.location = "index.php?grp=4&term="+gup("term")+"&sid="+gup("sid");
						}
						else
						{
							window.location = "index.php?term="+gup("term")+"&sid="+gup("sid");
						}
					}
					
				}
				else
				{
					alert(result);	
				}
				
				delete xhr;
				xhr = null;
			}
		}
	
		xhr.send(dataToSend);	
	}
}

function removeTextbook(bid)
{
	var cnf=confirm("Are you sure you want to remove this textbook?");
	
	if(!cnf)
	{
		return;
	}
	
	var target = document.getElementById("divErrorMessage");
		
	target.innerHTML = "<img src=\"../images/indicator_medium.gif\" alt=\"Please wait...\" /> Processing...";
	
		
	xhr = getXMLHttpRequestObject();
	var url = "remove_textbook.php";
	
	var dataToSend = "term="+gup("term")+"&sid="+gup("sid")+"&bid="+bid;
	//alert(dataToSend);
	//url += "?" + dataToSend;
		
	if(xhr)
	{
		xhr.open("POST", url);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				//callbackFunction(XMLHttpRequestObject.responseText); 
				
				// target.innerHTML = xhr.responseText;
				var result = xhr.responseText;
				
				delete xhr;
				xhr = null;
			}
		}
	
		xhr.send(dataToSend);	
	}
}


function updateTextbook()
{
	//alert("Update Textbook");
	
	var title = document.getElementById("txtTitle").value;
	var isbn = document.getElementById("txtIsbn").value;
	var author = document.getElementById("txtAuthor").value;
	var publisher = document.getElementById("txtPublisher").value;
	
	if(trim(title) == "")
	{
		alert("Title field cannot be empty.");
		return;
	}
	//alert(window.location.href);
	if(trim(isbn) == "")
	{
		alert("ISBN field cannot be empty.");
		return;
	}
	
	if(trim(author) == "")
	{
		alert("Author(s) field cannot be emtpy.");
		return;
	}
	
	if(trim(publisher) == "")
	{
		alert("Publisher field cannot be empty.");
		return;
	}
	
	var target = document.getElementById("divErrorMessage");
		
	target.innerHTML = "<img src=\"../images/indicator_medium.gif\" alt=\"Please wait...\" /> Processing...";
	
		
	xhr = getXMLHttpRequestObject();
	var url = "update_textbook.php";
	
	var dataToSend = "term="+gup("term")+"&sid="+gup("sid")+"&bid="+gup("bid")+"&title="+encodeURIComponent(title) + "&isbn="+isbn+"&author="+encodeURIComponent(author)+"&publisher="+encodeURIComponent(publisher);
	//alert(dataToSend);
	//url += "?" + dataToSend;
		
	if(xhr)
	{
		xhr.open("POST", url);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				//callbackFunction(XMLHttpRequestObject.responseText); 
				
				// target.innerHTML = xhr.responseText;
				var result = xhr.responseText;
				if(result == '1')  
				{
					//alert("This book is successfully updated ");		
					$grp=gup("grp");
					if($grp == '4')
					{
						window.location = "index.php?grp=4&term="+gup("term")+"&sid="+gup("sid");
					}
					else
					{
						window.location = "index.php?term="+gup("term")+"&sid="+gup("sid");		
					}
				}
				else
				{
					target.innerHTML = "Error: Unexpected error occured. Fail updating the textbook, please try again.";			
				}
				
				delete xhr;
				xhr = null;
			}
		}
	
		xhr.send(dataToSend);	
	}
}


