/**
*	addFile add a file to a queue that is stored in a hidden form value, store, and into a view box, view.  Checks for file uniqness based on fullpath and filename
*
* 	@param	string	fileType	passed value declaring the type the file is being assigned
* 	@param	string	fileLabel	passed value declaring the label the file is being assigned
* 	@param	string	fileInfo	passed value declaring the location and file being processed
* 	@param	string	view		name of view box to add to
* 	@param	string	store		name of stored hidden variable to append addition to
*	@see	isUniqueKW
*  	@since	1.0
*	@name	addFile
*/
function addFile(fileType, fileLabel, fileInfo, view, store, counter) {
	//split file information on slashes (WILL HAVE TO ADD MAC AND LINUX COMPATABILTY LATER)
	//if (navigator.appVersion.indexOf("X11")!=-1)
	//if (navigator.appVersion.indexOf("Linux")!=-1)

	//check that all fields have values and begin processing
	if (fileType.value != "" && fileLabel.value != "" && fileInfo.value != "") {
		//bump counter
		var counterNext = counter + 1;

		//hide previous entry
		document.getElementById('up_'+counter).style.display='none';
		//display next entry field
		document.getElementById('up_'+counterNext).style.display='block';
		//rewite add file button
		document.getElementById('buttonAdd').innerHTML = "<input type=\"button\" class=\"button_add_file\" onClick=\"javascript:addFile(nih.file_type_val, nih.file_label_val, nih.file_val_" + counterNext + ", nih.file_label_view, nih.TMP_file_list, " + counterNext + ");\" value=\"Add File\"/>";

		if (navigator.appVersion.indexOf("Win")!=-1) {
			var arrFileInfo = (fileInfo.value).split("\\");
		}
		if (navigator.appVersion.indexOf("Mac")!=-1) {
			var arrFileInfo = (fileInfo.value).split("/");
		}

		//check that submitted fileInfo doesn't already exist in stored list
		var arrStoredList = (store.value).split(";");
		//var dupFileInfo;
		for(var j = 0; j < arrStoredList.length; j++) {
			//split each entry in list
			var arrSingleEntry = (arrStoredList[j]).split("###");
			//duplicate file information submission, alert and do not process
			if (fileInfo.value == arrSingleEntry[3]) {
				//post alert
				alert ('The file at the below location has already been queued:\r\n' + arrSingleEntry[3]);
				//set flag
				var dupFileInfo = "TRUE";
				//fileInfo.value = "";
			}
		}
		//All OK post and pass
		if (dupFileInfo != "TRUE") {
			//push onto view box
			view[view.length] = new Option(fileType.value + " - " + fileLabel.value + " - " + arrFileInfo[arrFileInfo.length-1], fileType.value + "|" + fileLabel.value + "|" + fileInfo.value);
			//add to store value
			store.value += counter + "###" + fileType.value + "###" + fileLabel.value + "###" + fileInfo.value + ";";
			//clear form values
			fileType.value = "";
			fileLabel.value = "";
			//fileInfo.value = "";
		}
	}
}


/**
*	remFile remove a selected file from the view and remove it from store as well
*
* 	@param	string	view	name of the view box to remove the selected file from
* 	@param	string	store	name of the hidden form field to remove the name from
*  	@since	1.0
*	@name	remFile
*/
function remFile(view, store) {
	var kws = new String(store.value).split(";");
	var rem = "";
	for (var i = view.length-1; i > 0; i--) {
		if (view[i].selected) {
			view[i] = null;
			rem += kws[i-1] + ";";
			kws.splice(i-1,1);
		}
	}
	// re-setup store
	store.value = "";
	for(var i = 0; i < kws.length; i++) {
		if(kws[i]) {
			store.value += kws[i] + ";";
		}
	}
	return rem;
}

/**
*	addAuthor add an author to a queue that is stored in a hidden form value, store, and into a view box, view.  No check for uniqueness.
*
* 	@param	string	nameLast	passed value declaring the last name
* 	@param	string	nameFirst	passed value declaring the first name
* 	@param	string	view		name of view box to add to
* 	@param	string	store		name of stored hidden variable to append addition to
*  	@since	1.0
*	@name	addAuthor
*/
function addAuthor(nameLast, nameFirst, view, store) {
	//check that all fields have values and begin processing
	if (nameLast.value != "" && nameFirst.value != "") {
		view[view.length] = new Option(nameLast.value + ", " + nameFirst.value);
		//add to store value
		store.value += nameLast.value + "###" + nameFirst.value + ";";
		//clear form values
		nameLast.value = "";
		nameFirst.value = "";
	}
}

/**
*	remAuthor remove a selected Author from the queue and the hidden form field
*
* 	@param	string	view	name of the view box to remove the selected author from from
* 	@param	string	store	name of the hidden form field to remove the author from
*  	@since	1.0
*	@name	remFile
*/
function remAuthor(view, store) {
	var kws = new String(store.value).split(";");
	var rem = "";
	for (var i = view.length-1; i > 0; i--) {
		if (view[i].selected) {
			view[i] = null;
			rem += kws[i-1] + ";";
			kws.splice(i-1,1);
		}
	}
	// re-setup store
	store.value = "";
	for(var i = 0; i < kws.length; i++) {
		if(kws[i]) {
			store.value += kws[i] + ";";
		}
	}
	return rem;
}

/**
 *
 * @access public
 * @return void
 **/
var on;
function setState(el) {
	if(el != on) {
		if(on)
		on.className = "up";
		el.className = "dn";
		on = el;
	}
}

/**
 *
 * @access public
 * @return void
 **/
function validateForm(){
	var alertMsg = "All fields are required, the following have not been supplied";
	var badFound;

	if (document.nih.submitter_first.value == "") {
		alertMsg += "\r\n  - Submitter First Name";
		badFound = 1;
	}
	if (document.nih.submitter_last.value == "") {
		alertMsg += "\r\n  - Submitter Last Name";
		badFound = 1;
	}
	if (document.nih.submitter_email.value == "") {
		alertMsg += "\r\n  - Submitter Email";
		badFound = 1;
	}
	if (document.nih.journal_title.value == "") {
		alertMsg += "\r\n  - Journal Title";
		badFound = 1;
	}
	if (document.nih.TMP_author_list.value == "") {
		alertMsg += "\r\n  - You need to add at least one Manuscript (article) Author Name";
		badFound = 1;
	}
	if (document.nih.article_title.value == "") {
		alertMsg += "\r\n  - Manuscript Title";
		badFound = 1;
	}
	if (document.nih.grantee_first.value == "") {
		alertMsg += "\r\n  - NIH Grantee (Principal Investigator) First Name";
		badFound = 1;
	}
	if (document.nih.grantee_last.value == "") {
		alertMsg += "\r\n  - NIH Grantee (Principal Investigator) Last Name";
		badFound = 1;
	}
	if (document.nih.grant_number.value == "") {
		alertMsg += "\r\n  - NIH Grant/Project Number";
		badFound = 1;
	}
	if (document.nih.embargo_period.value == "") {
		alertMsg += "\r\n  - Embargo Period";
		badFound = 1;
	}
	if (document.nih.TMP_file_list.value == "") {
		alertMsg += "\r\n  - You need to add at least one file";
		badFound = 1;
	}
	if (badFound == 1) {
		alert (alertMsg);
	}
	else {
		document.nih.submit();
	}
}

/**
 *
 * @access public
 * @return void
 **/
function checkFormNIH (){
	if (document.nih.rights_type.value == ""){
		alert("Please choose a Statement of Endorsement before proceeding.");
	}
	else {
		document.nih.submit();
	}
}

/**
 *
 * @access public
 * @return void
 **/
function checkFormRUCORE (){
	if (document.nih.deposit_agreement.value == ""){
		alert("Please choose a Statement of Endorsement before proceeding.");
	}
	else {
		document.nih.submit();
	}
}

