//Helper method for checkFileItemXXXX
//Finds the right submission file type
//and fills an array with its values
function fillArray(submissionFileType){
	var types = document.getElementsByName("sft");
	var fileItems = null;
	var extensionBox = new Array();
	
	if(types.length > 0) {
		
		fileItems = document.getElementsByName("fileitem");
		
		switch(submissionFileType)
		{
			case 'LETTER_TO_THE_EDITOR':
				for(var i=0; i<types.length; i++) {
					if(types[i].value == 'LETTER_TO_THE_EDITOR') {
						extensionBox.push(fileItems[i]);
					}
				}
				break;
			case 'MANUSCRIPT':
				for(var i=0; i<types.length; i++) {
					if(types[i].value == 'MANUSCRIPT') {
						extensionBox.push(fileItems[i]);
					}
				}
				break;
			case 'TABLE':
				for(var i=0; i<types.length; i++) {
					if(types[i].value == 'TABLE') {
						extensionBox.push(fileItems[i]);
					}
				}
				break;
			case 'IMAGE':
				for(var i=0; i<types.length; i++) {
					if(types[i].value == 'IMAGE') {
						extensionBox.push(fileItems[i]);
					}
				}
		}

	}
	
	return extensionBox;
}

//Function validating the cover letter suffix
function checkFileItemCoverLetter(inputButton,formId) {
	var extensionBox = fillArray('LETTER_TO_THE_EDITOR');
	
	for(var i=0;i<extensionBox.length;i++) {
		
		if(extensionBox[i].value !="") {
			var sub = extensionBox[i].value.substring(extensionBox[i].value.length-5,extensionBox[i].value.length);
			
			if(sub.lastIndexOf(".doc")==-1 && sub.lastIndexOf(".docx")==-1
					&& sub.lastIndexOf(".rtf")==-1 && sub.lastIndexOf(".odf")==-1
					&& sub.lastIndexOf(".DOC")==-1 && sub.lastIndexOf(".DOCX")==-1
					&& sub.lastIndexOf(".RTF")==-1 && sub.lastIndexOf(".ODF")==-1
					&& sub.lastIndexOf(".pdf")==-1 && sub.lastIndexOf(".PDF")==-1) {
					alert("The file extension for cover letters must be one of .doc,.docx,.rtf,.pdf or .odf");
					inputButton.disabled=false;
					resetSubmissionFilesForm(formId);
					return false;
			}
		}
	}

}

//Function validating the manuscript suffix
function checkFileItemManuscript(inputButton,formId) {
	var extensionBox = fillArray('MANUSCRIPT');
	
	for(var i=0;i<extensionBox.length;i++) {
		
		if(extensionBox[i].value !="") {
			var sub = extensionBox[i].value.substring(extensionBox[i].value.length-5,extensionBox[i].value.length);

			if(sub.lastIndexOf(".doc")==-1 && sub.lastIndexOf(".docx")==-1
					&& sub.lastIndexOf(".rtf")==-1 && sub.lastIndexOf(".odf")==-1
					&& sub.lastIndexOf(".DOC")==-1 && sub.lastIndexOf(".DOCX")==-1
					&& sub.lastIndexOf(".RTF")==-1 && sub.lastIndexOf(".ODF")==-1) {
					alert("The file extension for manuscripts must be one of .doc,.docx,.rtf or .odf");
					inputButton.disabled=false;
					resetSubmissionFilesForm(formId);
					return false;
			}
		}
	}
}

//Function validating the table suffix
function checkFileItemTable(inputButton,formId) {
	var extensionBox = fillArray('TABLE');
	
	for(var i=0;i<extensionBox.length;i++) {
		
		if(extensionBox[i].value !="") {
			var sub = extensionBox[i].value.substring(extensionBox[i].value.length-5,extensionBox[i].value.length);

			if(sub.lastIndexOf(".doc")==-1 && sub.lastIndexOf(".docx")==-1
					&& sub.lastIndexOf(".rtf")==-1 && sub.lastIndexOf(".odf")==-1
					&& sub.lastIndexOf(".DOC")==-1 && sub.lastIndexOf(".DOCX")==-1
					&& sub.lastIndexOf(".RTF")==-1 && sub.lastIndexOf(".ODF")==-1) {
					alert("The file extension for tables must be one of .doc,.docx,.rtf or .odf");
					inputButton.disabled=false;
					resetSubmissionFilesForm(formId);
					return false;
			} 
		}
	}
}

//Function validating the image suffix
function checkFileItemImage(inputButton,formId) {
	var extensionBox = fillArray('IMAGE');
	
	for(var i=0;i<extensionBox.length;i++) {
		
		if(extensionBox[i].value !="") {
			var sub = extensionBox[i].value.substring(extensionBox[i].value.length-5,extensionBox[i].value.length);
			
			if(sub.lastIndexOf(".jpg")==-1 && sub.lastIndexOf(".jpeg")==-1
					&& sub.lastIndexOf(".tif")==-1 && sub.lastIndexOf(".tiff")==-1
					&& sub.lastIndexOf(".pdf")==-1 && sub.lastIndexOf(".eps")==-1
					&& sub.lastIndexOf(".JPG")==-1 && sub.lastIndexOf(".JPEG")==-1
					&& sub.lastIndexOf(".TIF")==-1 && sub.lastIndexOf(".TIFF")==-1
					&& sub.lastIndexOf(".PDF")==-1 && sub.lastIndexOf(".EPS")==-1) {
					alert("The file extension for images must be one of .jpg,.jpeg,.tif,.tiff,.pdf,.eps .");
					inputButton.disabled=false;
					resetSubmissionFilesForm(formId);
					return false;
			}
		}
	}
	//Placed here because we need one submit
	//not in checkFileItemExtension
	document.getElementById(formId).submit();
}

//Function that uses the functions above
//for validating the file suffix
//NOTE:The functions above are not unified
//due to constant requirements changes
function validateFields(inputButton,formId) {
	var retvalue = null;
	
	retvalue = checkFileItemCoverLetter(inputButton,formId);
	if(retvalue == false) {return retvalue; }
	retvalue = checkFileItemManuscript(inputButton,formId) ;
	if(retvalue == false) { return retvalue; }
	retvalue = checkFileItemTable(inputButton,formId);
	if(retvalue == false) { return retvalue; }
	retvalue = checkFileItemImage(inputButton,formId);
	if(retvalue == false) { return retvalue; }
}

//A confirmation pop-up for reason textarea in 
//submissionDetails.jsp 
function respConfirm() {
	if((document.getElementById("opensubmissionText").value =="")) {
	    var response = confirm('You have not inserted a reason: Continue?');

	    if (response) return true;
	    else return false;
	} else {
		return true;
	}
}

//Reset form field
function resetSubmissionFilesForm(formId) {
	if(formId!=null){
		document.getElementById(formId).reset();
	}
}

//Generic clear form elements function
//TODO- the other elements left
function clearForm(form) {
	
	var formElements = form.elements;
	var fieldType;

	for(var i = 0; i < formElements.length; i++) {
		fieldType = formElements[i].type;

		switch(fieldType) {
			
			case "textarea":alert(formElements[i].value);
			case "text": formElements[i].value ="";
			case "select-one": formElements[i].selectedIndex = 0 ;
							   break;
							 	
		}
	}
}


/*
*
*  Javascript trim,
*  http://doc.infosnel.nl/javascript_trim.html
*
*/
function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}


//Generic email validation specialiazed for esubmission 
//Needs change in order to be generic
//Found at http://javascript.internet.com/forms/email-validation---basic.html
function checkEmail(myForm) {
	var emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var emailFirstAuthorValue = trim(myForm.emailFirstAuthor.value);
	var emailCorrespondingAuthorValue = trim(myForm.emailCorresponding.value);
	
	if (emailFirstAuthorValue.length > 0 && !emailRegex.test(emailFirstAuthorValue)){
		alert("Invalid First Author E-mail Address! Please re-enter.");
		return (false);
	} else if (emailCorrespondingAuthorValue.length > 0  && !emailRegex.test(emailCorrespondingAuthorValue)) {
		alert("Invalid Corresponding Author E-mail Address! Please re-enter.");
		return (false);
	}
	
	return (true)
}

//Redirect function
function proceedToNextPage(url) {
	window.location = url;
}

//Function checking if fields
//of register are filled.
function validateRegistrationFields() {
	var email = document.getElementById('email').value;
	var emailConfirmation = document.getElementById('email_confirmation').value;
	var password = document.getElementById('password').value;
	var passwordConfirmation = document.getElementById('password_confirmation').value;
	var firstName = document.getElementById('first_name').value;
	var lastName = document.getElementById('last_name').value;
	var company = document.getElementById('company').value;
	var position = document.getElementById('position').value;
	var address = document.getElementById('address').value;
	
	var message = new String(' field is empty. Please insert your ');
	
	if(trim(email)==""){
		alert('E-mail'.concat(message.concat('e-mail information.')));
		return false;
	} else if(trim(emailConfirmation)==""){
		alert('E-mail confirmation'.concat(message.concat('e-mail confirmation information.')));
		return false;
	} else if(trim(password)==""){
		alert('Password'.concat(message.concat('password information.')));
		return false;
	} else if(trim(passwordConfirmation)==""){
		alert('Password confirmation'.concat(message.concat('password confirmation information.')));
		return false;
	} else if(trim(firstName)==""){
		alert('First name'.concat(message.concat('first name information.')));
		return false;
	} else if(trim(lastName)==""){
		alert('Last name'.concat(message.concat('last name information.')));
		return false;
	} else if(trim(company)==""){
		alert('Institution / Company'.concat(message.concat('institution / company information.')));
		return false;
	} else if(trim(position)==""){
		alert('Position'.concat(message.concat('position information.')));
		return false;
	} else if(trim(address)==""){
		alert('Address'.concat(message.concat('address information.')));
		return false;
	}
	
	return true;
}

//Checks if the approval checkboxes
//are ticked and enables finalize button
function enableFinalizeButton() {
	var imageApproval = document.getElementById('imagesApproval');
	var duplicateSubmissionApproval = document.getElementById('duplicateSubmissionApproval');
	var finalizeButton = document.getElementById('finalizeSubmission');
	
	if(imageApproval.checked && duplicateSubmissionApproval.checked) {
		finalizeButton.disabled = false;
	} else {
		finalizeButton.disabled = true;
	}
}

//Initialize firstAuhtorVariables and then
//copy form fields for 
//esub authorship


var correspondingAuthorTitleIndex = 0;
var correspondingAuthorTitle = "";
var correspondingAuthorFirstName = "";
var correspondingAuthorMiddleName = "";
var correspondingAuthorLastName= "";
var correspondingAuthorEmail = "";

function initCorrespondingAuthorVariables(myForm) {
	 correspondingAuthorTitleIndex = myForm.titleCorresponding.selectedIndex;
	 correspondingAuthorTitle = myForm.titleCorresponding[correspondingAuthorTitleIndex].value;
	 correspondingAuthorFirstName = myForm.firstNameCorresponding.value;
	 correspondingAuthorMiddleName = myForm.middleNameCorresponding.value;
	 correspondingAuthorLastName= myForm.lastNameCorresponding.value;
	 correspondingAuthorEmail = myForm.emailCorresponding.value;
	
}

function copyCorrespondingAuthorDataToFirstAuthor(myForm) {
	if(myForm.copyData.checked) {
		initCorrespondingAuthorVariables(myForm);
		myForm.titleCorresponding.selectedIndex = myForm.titleFirstAuthor.selectedIndex;
		myForm.firstNameCorresponding.value = myForm.firstNameFirstAuthor.value;
		myForm.middleNameCorresponding.value = myForm.middleNameFirstAuthor.value;
	    myForm.lastNameCorresponding.value = myForm.lastNameFirstAuthor.value;
	    myForm.emailCorresponding.value = myForm.emailFirstAuthor.value;
	} else {
		 myForm.titleCorresponding.selectedIndex = correspondingAuthorTitleIndex;
		 myForm.titleCorresponding[titleCorrespondingIndex].value = correspondingAuthorTitle;
		 myForm.firstNameCorresponding.value = correspondingAuthorFirstName;
		 myForm.middleNameCorresponding.value = correspondingAuthorMiddleName;
		 myForm.lastNameCorresponding.value = correspondingAuthorLastName;
		 myForm.emailCorresponding.value = correspondingAuthorEmail;
	}
}

//NEWSLETTER FUNCTIONS

function selectAll() 
{
		document.loginform.subscribtion_ETM[0].checked = true;
		document.loginform.subscribtion_OR[0].checked = true;
		document.loginform.subscribtion_OL[0].checked = true;
		document.loginform.subscribtion_MMR[0].checked = true;
		document.loginform.subscribtion_IJO[0].checked = true;
		document.loginform.subscribtion_IJMM[0].checked = true;
		//document.loginform.general[0].checked = true;
		//document.loginform.conference[0].checked = true;
		
		document.loginform.subscribtion_ETM[1].checked = false;
		document.loginform.subscribtion_OR[1].checked = false;
		document.loginform.subscribtion_OL[1].checked = false;
		document.loginform.subscribtion_MMR[1].checked = false;
		document.loginform.subscribtion_IJO[1].checked = false;
		document.loginform.subscribtion_IJMM[1].checked = false;
		//document.loginform.general[1].checked = false;
		//document.loginform.conference[1].checked = false;
}
function deselectAll()
{		
		document.loginform.subscribtion_ETM[0].checked = false;
		document.loginform.subscribtion_OR[0].checked = false;
		document.loginform.subscribtion_OL[0].checked = false;
		document.loginform.subscribtion_MMR[0].checked = false;
		document.loginform.subscribtion_IJO[0].checked = false;
		document.loginform.subscribtion_IJMM[0].checked = false;
		//document.loginform.general[0].checked = false;
		//document.loginform.conference[0].checked = false;
		
		document.loginform.subscribtion_ETM[1].checked = true;
		document.loginform.subscribtion_OR[1].checked = true;
		document.loginform.subscribtion_OL[1].checked = true;
		document.loginform.subscribtion_MMR[1].checked = true;
		document.loginform.subscribtion_IJO[1].checked = true;
		document.loginform.subscribtion_IJMM[1].checked = true;
		//document.loginform.general[1].checked = true;
		//document.loginform.conference[1].checked = true;
	    
}	

