// JavaScript Document - AJAX

var xmlHttp;
var xmlDoc;
var thisLocation = "http://" + location.hostname;

function createXMLHttpRequest(){
	if (window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp = new XMLHttpRequest();
	}
	return;
}

function addNewDepartment(deptName,user_id){
	createXMLHttpRequest();
	url = "/ajax/addDepartment.cfm?department_name=" + deptName + "&user_id=" + user_id;
	xmlHttp.onreadystatechange = returnDepartmentInfo;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}

function returnDepartmentInfo(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			deptObj = document.getElementById("user_dept_id");
			deptInfo = xmlHttp.responseText.split("^");
			if (deptInfo.length == 2){
				dl = deptObj.length;
				deptObj.length = dl+1;
				deptObj.options[dl] = new Option(deptInfo[1],deptInfo[0]);
				deptObj.options[dl].selected = true;
			} else {
				alert("No data was returned. Try refreshing the page \r\nto see if the department you added made it to the system.");
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
}
function getNextIssue(year){
	if (!year || isNaN(year)) return;
	if (isNaN(document.getElementById("issue_id").value)) return;
	createXMLHttpRequest();
	url = "/ajax/getNextIssue.cfm?fi_issue_year=" + year;
	xmlHttp.onreadystatechange = displayNextIssue;
	xmlHttp.open("GET",url, true);
	xmlHttp.send(null);
}
function displayNextIssue(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			returnVal = returnVal.replace(/\D/g,"");
			document.getElementById("fi_issue_number").value = returnVal;
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function getAbstract(id){
	if (isNaN(id)) return;
	window.open("/issues/abstract.cfm?article_id=" + id,"abstract","width=550,height=450,scrollbars,resizable");
	return;
}

function showAbstract(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			abstractObj = document.getElementById("abstract");
			leftVal = parseInt(document.body.clientWidth / 2) - 200;
			abstractObj.style.left = leftVal;
			abstractObj.innerHTML = returnVal;
			abstractObj.style.visibility = "visible";
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function closeAbstract(){
	abstractObj = document.getElementById("abstract");
	abstractObj.innerHTML = "";
	abstractObj.style.visibility = "hidden";
}
function addToCart(id,type){
	if (isNaN(id)) return;
	createXMLHttpRequest();
	url = "/ajax/isLoggedIn.cfm?id=" + id + "&type=" + type;
	xmlHttp.onreadystatechange = isLoggedIn;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function isLoggedIn(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			returnVal = returnVal.replace(/\s/g,"");
			vals = returnVal.split("^");
			if(vals[0] == "false"){
				alert("You are not currently logged in to our website. \r\nPlease log in before adding items to your shopping cart.\r\n\r\nA profile is necessary for full access to this website,\r\nincluding purchasing articles. If you do not have a profile in \r\nour system, please return to the Current Issue page, select the \r\n'Create Profile' button and create a profile.");
			} else {
				saveItemToCart(vals[1],vals[2]);
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
}
function saveItemToCart(id,type){
	if (!id || !type) return;
	createXMLHttpRequest();
	url = "/ajax/saveToCart.cfm?id=" + id + "&type=" + type;
	xmlHttp.onreadystatechange = showCart;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function showCart(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			document.getElementById("shoppingCartDiv").style.visibility = "visible";
			alert("The item you selected has been added to your shopping cart.");
		}
	}
}
function getPassword(){
	emailAddress = document.getElementById("email").value;
	if (!emailAddress) return;
	createXMLHttpRequest();
	url = "/ajax/getLogin.cfm?cust_email=" + emailAddress;
	xmlHttp.onreadystatechange = getLoginStatus;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function getLoginStatus(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			return_values = returnVal.split("^");
			//returnVal = returnVal.replace(/\s/g,"");
			document.getElementById("loginStatus").innerHTML = return_values[0];
			if (return_values.length == 2){
				document.getElementById("cust_username").value = return_values[1];
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function checkUniqueUsername(){
	username = document.getElementById("cust_username").value;
	if (!username) return;
	createXMLHttpRequest();
	url = "/ajax/isUsernameUnique.cfm?cust_username=" + username;
	xmlHttp.onreadystatechange = isUsernameUnique;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function isUsernameUnique(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			returnVal = xmlHttp.responseText;
			returnVal = returnVal.replace(/\s/g,'');
			if (returnVal != "true" && returnVal != "false"){
				alert(returnVal);
			} else if (returnVal == "true"){
				alert("That user name is already in use. Please try another.");
				document.getElementById("cust_username").value = "";
			} else {
			}
		} else if (xmlHttp.status == 404){
			alert("data file not accessed");
		} else {
			alert("Unspecified error");
		}
	}
	return;
}
function openWindow(file,winName,param){
	if (!file) return;
	if (!winName) winName = "newWindow";
	if (!param) param = "width=600,height=400,scrollbars,resizable";
	window.open(file,winName,param);
	return;
}