//This file is for nav-related scripts and other things that need to be included pretty much everywhere by default. Please keep it lightweight.


/*
 *	popItUp
 *	inline function
 *	pevans@highwire.stanford.edu
 *	10.17.07
 *
 *	this is a cross-browser, simple popup window function that accepts
 *	the url to load into the new browser page that's spawned.  The 
 *	spawned popup window will be focused, 600 x 665.
 *
 *	@parm		string url		the url to open in the newly spawned window
 *	@returns	false
 */
function popItUp(url)
{
	newwindow=window.open(url,'name','height=600,width=435,scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}

/*
 *	setModal
 *	inline function
 *	pevans@highwire.stanford.edu
 *	8.29.06
 *
 *	setModal will set a specified element to a "modal" state
 *	by setting the currently displayed document style to 
 *	display:none and the specified element style to display:inline
 *	(via the swapObjs function).  This is essentially a safe
 *	wrapper for the swapObjs function which tests for the browsers
 *	capability to operate on the DOM (appropriate CSS support can
 *	be inferred from this test).
 *
 *	@parm	string modalDivId	the id of the modal dialog DIV element
 *	@return <none>
 */
function setModal(modalDivId)
{
	if( document.getElementById && document.getElementById(modalDivId))
	{
		swapObjs(document.getElementById('main'), document.getElementById(modalDivId));		
	}	
}

/*
 *  swapObjs
 *  inline function
 *  pevans@highwire.stanford.edu
 *  5.15.06
 *
 *  swapObjs will perform the function of "swapping" two DOM 
 *  objects by setting display options.  the element identified by id
 *  obj1 will have it's style.display property set to "none" and the
 *  obj2 element will have it's style.display property set to the
 *  empty string ("").
 *
 *  swapObjs uses the display property to overcome an interesting
 *  feature of IE 5, 6 whereby the setting an object's visibility
 *  property does not cause the web page to reflow.  utilizing the 
 *  display property produces the expected behavior.
 */
function swapObjs(obj1, obj2)
{
    obj1.style.display = "none";
    obj2.style.display = "";
}

/*
 *  swapDivs
 *  inline function
 *  pevans@highwire.stanford.edu
 *  4.6.06
 *
 *  swapDivs will perform the function of "swapping" two DOM objects
 *  by setting visibility options.  the div identified by id currDivName
 *  will have it's style.visibility property set to "hidden" and the
 *  newDivName will have it's style.visibility set to "visible".
 *
 *  @param      currDivName     string   id of DOM element to hide
 *  @param      newDivName      string   id of DOM element to reveal
 *  @returns    <nothing>
 */
function swapDivs(currDivName, newDivName)
{
    var swapOutDiv = document.getElementById(currDivName);
    var swapInDiv = document.getElementById(newDivName);
    
    swapOutDiv.style.visibility = "hidden";
    swapInDiv.style.visibility = "visible";
}

//the below allows for showing/hiding of a div (or maybe any element) by ID.
//first argument should be the div ID, second should be for setting the style
//display to whatever (such as "none" (hidden) or "block" (shown)). the second
//argument can be left off to make your link work as a show/hide toggle.

function showOrHide(divID,displayChange){ 
	if (document.getElementById(divID)){
		if (displayChange) {
			document.getElementById(divID).style.display = displayChange;
		}
		else {
			document.getElementById(divID).style.display = (document.getElementById(divID).style.display == "block" ? "none" : "block");
		}
	}
}

//the below is like the above, but for inline display

function inlineOrHide(spanID,displayChange){ 
	if (displayChange) {
		document.getElementById(spanID).style.display = displayChange;
	}
	else {
		document.getElementById(spanID).style.display = (document.getElementById(spanID).style.display == "inline" ? "none" : "inline");
	}
}

//the below allows for visibility/invisibility of a div (or any element) by ID.
//first argument should be the div ID, second should be for setting the style
//display to whatever (such as "hidden" or "visible"). the second
//argument can be left off to make your link work as a show/hide toggle.


function visOrInvis(divID,visibilityChange){ 
	if (document.getElementById(divID)){
		if (visibilityChange) {
			document.getElementById(divID).style.visibility = visibilityChange;
		}
		else {
			document.getElementById(divID).style.visibility = (document.getElementById(divID).style.visibility == "visible" ? "hidden" : "visible");
		}
	}
}


//validate and submit from the navbar search

function nbsValSubmit(defaultText){
	if (document.navBarSearch.basefield.value==defaultText) {
		void(0);
	}
	else {
		document.navBarSearch.submit();
	}
}

//new method button prep and url generator for navbar search

function prepNavButtons(prepType,defaultText,hasOtherQuicksearch){
	if (prepType == 'mouseOut') {
		if (document.navBarSearch.basefield.value==defaultText || document.navBarSearch.basefield.value=='') {
			prepHref = 'javascript:void(0);';
		} else {
			prepHref = '#';
		}
	}
	if (prepType == 'focus') {
		prepHref = '#';
	}
	
	/* 
		When we remove buttons from the navbar (to customize by role, say) we need 
		to stop a fail-condition on the calls below (can't get them if they don't exist)
	
		// this is initially for an atvb item (removing full ms info for ae role)
	
		7.8.08
		pevans@highwire.stanford.edu
		jgomez@highwire.stanford.edu
	*/
	if( document.getElementById('navbar_find_in_queues') ) document.getElementById('navbar_find_in_queues').href = prepHref;
	if( document.getElementById('navbar_fullms') ) document.getElementById('navbar_fullms').href = prepHref;
	if( document.getElementById('navbar_title_search') ) document.getElementById('navbar_title_search').href = prepHref;
	if( document.getElementById('navbar_author_search') ) document.getElementById('navbar_author_search').href = prepHref;
	if( document.getElementById('navbar_person_search') ) document.getElementById('navbar_person_search').href = prepHref;
	
	if (hasOtherQuicksearch) {
		if( document.getElementById('navbar_other_quicksearch') ) document.getElementById('navbar_other_quicksearch').href = prepHref;
	}
}

// This has been replaced by a new version below, which determines the kind of person search to perform based on the type of field input
// function nbsURLgen(defaultText,buttonID,baseURL,roleName,searchName,addlParams){
//	if (document.navBarSearch.basefield.value==defaultText || document.navBarSearch.basefield.value=='') {
// 		void(0);
// 	}
// 	else {
// 		document.getElementById(buttonID).href=baseURL + '?roleName=' + roleName + '&' + searchName + '=' + document.navBarSearch.basefield.value;
// 		if (addlParams) {
// 			document.getElementById(buttonID).href=document.getElementById(buttonID).href + addlParams;
// 		}
// 	}
// }

function nbsURLgen(defaultText,buttonID,baseURL,roleName,searchName,addlParams,searchType){
	if (document.navBarSearch.basefield.value==defaultText || document.navBarSearch.basefield.value=='') {
		void(0);
	} 
	else {  
		if (searchName == 'data_0_nameLast') {
			if (document.navBarSearch.basefield.value.indexOf('@') != -1) {
				searchName = 'data_0';
				searchType = 'email';
			}
        	for (i=0; i < document.navBarSearch.basefield.value.length; i++) {
        	    currChar = document.navBarSearch.basefield.value.charAt(i);
        	    currInt = parseInt(currChar);
	            if (isNaN(currInt)) {
	                isInteger = false;
	            }
	            else {
	            	isInteger = true;
	            }
			}
			if (isInteger) {
				searchName = 'data_0';
				searchType = 'searchUserId';
			}
			if (!searchType) {
				searchType = 'name';
			}
		}
		document.getElementById(buttonID).href=baseURL + '?roleName=' + roleName + '&' + searchName + '=' + document.navBarSearch.basefield.value;
		if (addlParams) {
			document.getElementById(buttonID).href += addlParams;
		}
		if (searchType) {
			document.getElementById(buttonID).href += '&type_0=' + searchType;
		}
	}
}

function pulldownNav(form,field,newwin){
	var URL = document.form.field.options[document.form.field.selectedIndex].value;
	if (newwin) {
		window.open.location.href = URL;
	}
	else {
		window.location.href = URL;
	}
}

function setInMenu(inMenuChange){
	if( document.nav_pulldown_set ) 
	{
		document.nav_pulldown_set.in_menu.value = inMenuChange;
	}
}

function closeMenus(){
	if(document.nav_pulldown_set.in_menu && document.nav_pulldown_set.in_menu.value=="false") {
		visOrInvis('edit_templates','hidden');
		visOrInvis('jump_to_pulldown','hidden');
		visOrInvis('edit_data_pulldown','hidden');
		visOrInvis('file_man_pulldown','hidden');
		showOrHide('text_editor_options','none');
		visOrInvis('dashboard','hidden');
	}
}


//this function identifies all elements on the page with a NAME attribute matching
//the switchFrom argument, hides them, then displays all elements with NAME
//attributes matching the switchTo argument. it's used for swapping help levels.

function swapElements(switchFrom,switchTo){
var sF = switchFrom;
var sT = switchTo;
sF_Elements = document.getElementsByName(sF);
for (var i = 0; i < sF_Elements.length; i++) {
    var sFID = sF_Elements[i].id;
    document.getElementById(sFID).style.display = "none";
    }
sT_Elements = document.getElementsByName(sT);
for (var i = 0; i < sT_Elements.length; i++) {
    var sTID = sT_Elements[i].id;
    document.getElementById(sTID).style.display = "inline";
    }
}

//test whether any two emails on the author page of submission are the same.
//no longer in use, replaced by conf setting.

function testDupeEmails(auCount){
	auCount++;
	var emails = new Array();
	var noGood = '';
	for(i=1;i<auCount;i++){
		emails.push(document.getElementById("authorEmail"+i).value);
	}
	for(i=1;i<auCount;i++){
		thisEmail = document.getElementById("authorEmail"+i).value;
		if (thisEmail == ''){
			noGood = 'true';
		}
		else {
			var emailMatches = 0;
			for (x in emails){
				if (thisEmail == emails[x]){
					emailMatches++;
				}
			}
			if (emailMatches > 1){
				noGood = 'true';
			}
		}
	}
	if (noGood){
		return true;
	}
}

//highlight all editable text sections on a page.


/*
 *	showHideEditables
 *	inline function
 *	shane@highwire.stanford.edu
 *	
 *	this function highlights all passages on a page that are considered
 *	eligible for text editor editing by adding a background color and
 *	border.
 *
 *	// DEVNOTE:	rewriting sections of this code to no longer rely on the
 *	// 			prototype library, but rather the jquery library.
 */
/*
var editMode;var allTEB;
function showHideEditables(roleName,pageID) {
	if (!allTEB) {
		allTEB = document.getElementsByTagName('div');
	}
	if (!editMode) {
		for (var i=0; i < allTEB.length; i++) {
			if (Element.hasClassName(allTEB[i],'TEB')) {
				Element.addClassName(allTEB[i],'TEBHighlightOn');
				$(allTEB[i]).style.display = 'block';
			}
		}
		$('edit_mode_off').style.display='none';
		$('edit_mode_on').style.display='inline';
		editMode = true;
	} else {
		for (var i=0; i < allTEB.length; i++) {
			if (Element.hasClassName(allTEB[i],'TEB')) {
				Element.removeClassName(allTEB[i],'TEBHighlightOn');
				$(allTEB[i]).style.display = 'inline';
			}
		}
		$('edit_mode_off').style.display='inline';
		$('edit_mode_on').style.display='none';
		editMode = false;
	}	
}
*/

//open a window containing the text editor interface
//for all of the editable sections on the current page

/*
function editTEB(roleName,pageID,tebID,forceEdit) {
	if (editMode || forceEdit) {
		var editTEBLink = '/tracking/editvar?roleName=' + roleName + '&page=' + pageID + '&teb=' + tebID;
		var textEd;
		textEd = window.open(editTEBLink,'textEd','width=730,height=320,status=0,toolbar=0,scrollbars=1');
		textEd.focus();
	}
}

function editTEBMouseOver() {
	var TEBstatus = document.TEBstatus.onOff.value;
	if (TEBstatus == "on") {
		
	}
}

function editTEBMouseOut() {
	var TEBstatus = document.TEBstatus.onOff.value;
	if (TEBstatus == "on") {
	}
}
*/

/*
function editAllTEB(roleName,pageID) {
	var allEl = document.getElementsByTagName('div');
	var allTEB = new Array();
	var tebMatch = '^TEB_';
	var tc = 0;
	for (var s=0; s < allEl.length; s++) 
	{
		var thisId = allEl[s].id;
		var thisEl = document.getElementById(thisId).id.toString();
		if (thisEl.match(tebMatch)) 
		{
			allTEB[tc] = allEl[s].id;
			tc++;
		}
	}
	var editAllTEBLink = '/tracking/editvar?roleName=' + roleName + '&page=' + pageID;
	var textEdAll;
	for (var t=0; t < allTEB.length; t++) 
	{
		var thisTEB = allTEB[t].toString();
		var stringTEB = document.getElementById(thisTEB).id.toString();
		var tebParam = '&teb=' + stringTEB;
		editAllTEBLink += tebParam;
	}
	if (tc > 0) 
	{
		var textEdAll = window.open(editAllTEBLink,'textEdAll','width=730,height=600,status=0,toolbar=0,scrollbars=1');
		textEdAll.focus();
	}
}
*/

function disableSubmit(myform,myname,myvalue) {
var allIn = document.getElementsByTagName('input');
for (var i=0; i < allIn.length; i++) {
	var fieldType;
	fieldType = allIn[i].getAttribute('type').toLowerCase();
	if ((fieldType == 'submit') || (fieldType == 'reset')) {
		allIn[i].disabled = true;
		allIn[i].value="Processing, please wait...";
		}
	}
	document.getElementById('hidden1').name=myname;
	document.getElementById('hidden1').value=myvalue;
	myform.submit();
}



function blockUserAsAu(form, fname, lname, email, text) {
	var auIsUser = false;
	if ((document.submitManuscript.authorFname1.value.toLowerCase() == fname.toLowerCase()) && (document.submitManuscript.authorLname1.value.toLowerCase() == lname.toLowerCase())) {
		auIsUser = true;
	}
	if (document.submitManuscript.authorEmail1.value.toLowerCase() == email.toLowerCase()) {
		auIsUser = true;
	}
	if (auIsUser) {
		alert(text);
		return false;
	} else {
		return true;
	}
}

