function enablePdfUpSubmit(upFileID,submitID) {
	var ext = getFileExtension(document.getElementById(upFileID).value);
	if(ext!=='pdf') {
		invalidify(document.getElementById(upFileID+'Text'));
		document.getElementById(submitID).className = "button disabled";
		document.getElementById(submitID).setAttribute("onclick","");
	}
	else {
		validify(document.getElementById(upFileID+'Text'));
		document.getElementById(submitID).className = "button";
		document.getElementById(submitID).setAttribute("onclick","submitPubFileUp('pubPdfUpload');");
	}
	// alert(ext);
}

function enableCoverUpSubmit(upFileID,submitID) {
	var ext = getFileExtension(document.getElementById(upFileID).value);
	if(ext!=='jpg' && ext!=='jpeg' && ext!=='png' && ext!=='gif') {
		invalidify(document.getElementById(upFileID+'Text'));
		document.getElementById(submitID).className = "button disabled";
		document.getElementById(submitID).setAttribute("onclick","");
	}
	else {
		validify(document.getElementById(upFileID+'Text'));
		document.getElementById(submitID).className = "button";
		document.getElementById(submitID).setAttribute("onclick","submitPubFileUp('pubCoverUpload');");
	}
	// alert(ext);
}

function submitPubFileUp(dispatchRequest) {
	var thisForm = document.getElementById("pubFileUploadForm");
	document.getElementById("pageMask").style.visibility="visible";
	thisForm.action = "/research/tsapatsis/support/dispatch.publications.php?dispatchRequest="+dispatchRequest;
	window.setTimeout("document.getElementById('pubFileUploadForm').submit();",100);
}


function ajaxBuildPublications() { 
	var postData	= "dispatchRequest=buildPublications"
					+ "&flt="+escape(document.getElementById("flt").value)
			 		+ "&srt="+escape(document.getElementById("srt").value)
			 		+ "&ord="+escape(document.getElementById("ord").value);
	
	var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; }
	xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);
	
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			var ajaxReturn = jsonParse(xmlHttp.responseText);
			document.getElementById("publicationsWrapper").innerHTML = ajaxReturn['html'];
			window.setTimeout("externaLinks();",50);
		}
	}
}

function ajaxFetchPubs() {
	var postData	= "dispatchRequest=fetchPubsList"
					+ "&fetchCond="+escape(document.getElementById("pubsListCond").value)
					+ "&flt="+escape(document.getElementById("flt").value)
			 		+ "&srt="+escape(document.getElementById("srt").value)
			 		+ "&ord="+escape(document.getElementById("ord").value);
					
	var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert("Browser does not support HTTP Request"); return; }
	xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);
	
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			// alert(xmlHttp.responseText);
			var ajaxReturn = jsonParse(xmlHttp.responseText);
			document.getElementById("pubsList").innerHTML = ajaxReturn["html"];
		}
	}
}


function onPubSelectChange(uid) {
	window.location = "/research/tsapatsis/restricted/publications-mod.php?uid="+uid;
}


function populatePubsMod(uid) {
	var postData	= "dispatchRequest=fetchPubDetails"
					+ "&uid="+uid;
	var inputEls = document.forms.pubModForm.elements;
	
	document.getElementById("uploadPubUID").value = ''+uid+'';
	
	var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert("Browser does not support HTTP Request"); return; }
	xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			// alert(xmlHttp.responseText);
			var ajaxReturn = jsonParse(xmlHttp.responseText);
			if(ajaxReturn['success']) {
				var pubDetails = ajaxReturn['data'];
				for(var i=0 ; i < inputEls.length ; i++) {
					if(inputEls[i].type!="submit" && inputEls[i].type!="hidden" && inputEls[i].id!='uidShow') {
						inputEls[i].value = pubDetails[''+inputEls[i].name+''];
					}
				}
			}
			else {
				alert("Failed to fetch details of the publication--this indicates a serious problem");
			}
		}
	}
}





function makePubPreview() {
	document.getElementById("submitStatus").className = "ltgray";
	document.getElementById("submitStatus").innerHTML = "&#187; editing form...";
	var pubModForm = document.forms.pubModForm;
	var postData = "dispatchRequest=makePubPreview"+postifyPubData(pubModForm);
	
	var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; }
	xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);
	
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			// alert(xmlHttp.responseText);
			var ajaxReturn = jsonParse(xmlHttp.responseText);
			if(ajaxReturn["success"]) {
				document.getElementById("pubPreview").innerHTML=ajaxReturn["html"];
				window.setTimeout("externaLinks();",50);
			}
			else {
				alert("something went wrong with the live preview, which may indicate a serious problem");
			}
		}
	}
}

function attemptPubCoverRem() {
	var postData = "dispatchRequest=remPubCover&uid="+document.forms.pubModForm.uid.value;
	
	var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; }
	xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);
	
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			// alert(xmlHttp.responseText);
			var ajaxReturn = jsonParse(xmlHttp.responseText);
			if(ajaxReturn["success"]) {
				document.getElementById("submitStatus").className="green";
				window.location.reload();
				// window.location = "publications.php?flt="+ajaxReturn['articleTitle'];
				// window.location = "publications.php?flt="+ajaxReturn['uid'];
			}
			else {
				document.getElementById("submitStatus").className="red";
			}
			document.getElementById("submitStatus").innerHTML = ajaxReturn["message"];
			// document.getElementById("jsDebug").innerHTML = responseJson["debug"];
		}
	}
}

function attemptPubPdfRem() {
	var postData = "dispatchRequest=remPubPdf&uid="+document.forms.pubModForm.uid.value;
	
	var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; }
	xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);
	
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			// alert(xmlHttp.responseText);
			var ajaxReturn = jsonParse(xmlHttp.responseText);
			if(ajaxReturn["success"]) {
				document.getElementById("submitStatus").className="green";
				window.location.reload();
				// window.location = "publications.php?flt="+ajaxReturn['articleTitle'];
				// window.location = "publications.php?flt="+ajaxReturn['uid'];
			}
			else {
				document.getElementById("submitStatus").className="red";
			}
			document.getElementById("submitStatus").innerHTML = ajaxReturn["message"];
			// document.getElementById("jsDebug").innerHTML = responseJson["debug"];
		}
	}
}


function attemptPubRem() {
	var postData = "dispatchRequest=remPub&uid="+document.forms.pubModForm.uid.value;
	
	var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; }
	xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);
	
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			// alert(xmlHttp.responseText);
			var ajaxReturn = jsonParse(xmlHttp.responseText);
			if(ajaxReturn["success"]) {
				document.getElementById("submitStatus").className="green";
				window.location = "/research/tsapatsis/restricted/publications-cmd.php?selectOpt=own";
				// window.location = "publications.php?flt="+ajaxReturn['articleTitle'];
				// window.location = "publications.php?flt="+ajaxReturn['uid'];
			}
			else {
				document.getElementById("submitStatus").className="red";
			}
			document.getElementById("submitStatus").innerHTML = ajaxReturn["message"];
			// document.getElementById("jsDebug").innerHTML = responseJson["debug"];
		}
	}
}

function setStatusError() {
	document.getElementById("submitStatus").className = "red"
	document.getElementById("submitStatus").innerHTML = "&#187; required field(s) empty or invalid";
}

function attemptPubMod() {
	var pubModForm = document.getElementById("pubModForm");
	if(validatePubMod(pubModForm)) {	// check for user-errors
		setTimeout("setStatusError();",100);
	}
	else {	// no errors
		document.getElementById("submitStatus").className = "blue";
		document.getElementById("submitStatus").innerHTML = "&#187; submitting form...";
		
		var postData = "dispatchRequest=modPub"+postifyPubData(pubModForm);
					
		// document.getElementById("jsDebug").innerHTML = postData;
		
		var xmlHttp = GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; }
		xmlHttp.open("post","/research/tsapatsis/support/dispatch.publications.php",true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.send(postData);
		
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4) {
				// alert(xmlHttp.responseText);
				var ajaxReturn = jsonParse(xmlHttp.responseText);
				if(ajaxReturn["success"]) {
					document.getElementById("submitStatus").className="green";
					window.location = "/research/tsapatsis/restricted/publications-mod.php?uid="+ajaxReturn['uid'];
				}
				else {
					document.getElementById("submitStatus").className="red";
				}
				document.getElementById("submitStatus").innerHTML = ajaxReturn["message"];
				// document.getElementById("jsDebug").innerHTML = responseJson["debug"];
			}
		}
	}
}


function validatePubMod(pubModForm) {
	var invalidity = '';
	invalidity += csValidate(pubModForm.author,'noComma','required');
	invalidity += csValidate(pubModForm.articleTitle,'notEmpty','required');
	invalidity += csValidate(pubModForm.journal,'noAbbrv','required');
	invalidity += csValidate(pubModForm.volume,'alphaNumeric','required');
	invalidity += csValidate(pubModForm.number,'alphaNumeric','optional');
	invalidity += csValidate(pubModForm.year,'year','required');
	invalidity += csValidate(pubModForm.month,'shortMonthDay','optional');
	invalidity += csValidate(pubModForm.pages,'pages','optional');
	invalidity += csValidate(pubModForm.doi,'doi','optional');
	invalidity += csValidate(pubModForm.owner,'notEmpty','required');
	invalidity += csValidate(pubModForm.pubTags,'alphaNumeric','optional');
	return invalidity;
}

function postifyPubData(pubModForm) {
	var postData	= "&uid="+encodeURIComponent(pubModForm.uid.value)
					+ "&owner="+encodeURIComponent(pubModForm.owner.value)
					+ "&author="+encodeURIComponent(pubModForm.author.value)
					+ "&articleTitle="+encodeURIComponent(pubModForm.articleTitle.value)
					+ "&journal="+encodeURIComponent(pubModForm.journal.value)
					+ "&volume="+encodeURIComponent(pubModForm.volume.value)
					+ "&number="+encodeURIComponent(pubModForm.number.value)
					+ "&year="+encodeURIComponent(pubModForm.year.value)
					+ "&month="+encodeURIComponent(pubModForm.month.value)
					+ "&pages="+encodeURIComponent(pubModForm.pages.value)
					+ "&pubTags="+encodeURIComponent(pubModForm.pubTags.value)
					+ "&doi="+encodeURIComponent(pubModForm.doi.value);
	return postData;
}



function showHideRemoveButton(el) {
	if(el.checked) {
		document.getElementById("removePubButton").style.visibility="visible";
		if(document.getElementById("pdfExists").value=="exists") {
			document.getElementById("removePubPdfButton").style.visibility="visible";
		}
		else {
			document.getElementById("removePubPdfButton").style.visibility="hidden";
		}
		if(document.getElementById("coverExists").value=="exists") {
			document.getElementById("removePubCoverButton").style.visibility="visible";
		}
		else {
			document.getElementById("removePubCoverButton").style.visibility="hidden";
		}
	}
	else {
		document.getElementById("removePubButton").style.visibility="hidden";
		document.getElementById("removePubPdfButton").style.visibility="hidden";
		document.getElementById("removePubCoverButton").style.visibility="hidden";
	}
}

function removePeriod(el) {
	var strObj = el.value;
	var newStrObj = strObj.replace(".","");
	el.value = newStrObj;
}


function replaceComma(el) {
	var strObj = el.value;
	var newStrObj = strObj.replace(","," and");
	el.value = newStrObj;
}
