/* ***************************************************** */
function switchTabs(screen){
	for (var x=0; x<screens.length; x++){
		var tempLI = getDiv('tab_' + screens[x]);
		if ( screens[x] == screen){
			tempLI.className = 'selected';
		} else {
			tempLI.className = '';
		}
	}
}
/* ***************************************************** */
function switch2(screen){
	switchTabs(screen);
	for (var foo=0;foo<screens.length;foo++){
		hideDiv(screens[foo]);
	}
	showDiv(screen);
	fixUnderflow();
	setLogoPos();
} 
/* ***************************************************** */
function validate(){
	var blankReg = /^\s*$/;
	var sendType;
	var sendTo;
	var tmpD;
	var errorDiv = $('errorDiv');
	if (errorDiv != null)
		errorDiv.innerHTML = '';
	for (var x=0;x<screens.length;x++ ){
		var tempLI = getDiv('tab_' + screens[x]);
		if (tempLI.className == 'selected'){
			sendType = screens[x];
			break;
		}
	}
	var error_ul = document.createElement('ul');
	var result = true;
	if (sendType == 'library'){
		if ($('library_name').selectedIndex < 0){
			tmpD = document.createElement('li');
			tmpD.appendChild(document.createTextNode("You must choose the library to send your request to."));
			error_ul.appendChild(tmpD);
			result = false;
		} else {
			sendTo = $('library_name').options[$('library_name').selectedIndex].value;
		}
	} else if (sendType == 'subject'){
		if ($('biblio_name').selectedIndex < 0){
			tmpD = document.createElement('li');
			tmpD.appendChild(document.createTextNode("You must choose the subject librarian to send your request to."));
			error_ul.appendChild(tmpD);
			result = false;
		} else {
			var tmpSubj = subjects['sub_' + $('biblio_name').options[$('biblio_name').selectedIndex].value];
			if (tmpSubj != null){
				sendTo = "";
				var sendAll = "";
				if (tmpSubj.biblios.length > 1){
					var LIs = $('biblio_choose').descendants();
					if (LIs != null){
						for (var x=0; x<LIs.length; x++){
							if (LIs[x].nodeName == "LI"){
								if (LIs[x].className == "select")
									sendTo+= ((sendTo != "")? ", ":"") + biblios[ LIs[x].id].e_mail;
								sendAll+= ((sendAll != "")? ", ":"") + biblios[ LIs[x].id].e_mail;
							}
						}
					}
					if (sendTo != sendAll && sendTo == "")
						sendTo = sendAll; // If none selected, send to all
				} else {
					sendTo = biblios['bib_' + tmpSubj.biblios[0]].e_mail;
				}
					
			} 
		}
	} 
	tmpD = $('the_ques');
	if ( $('the_ques').value == ''){
		tmpD = document.createElement('li');
		tmpD.appendChild(document.createTextNode("You forgot to enter your question/comment/suggestion."));
		error_ul.appendChild(tmpD);
		result = false;
	}
	if ( $('pat_name').value == '' || $('pat_email').value == ''){
		tmpD = document.createElement('li');
		tmpD.appendChild(document.createTextNode("You must enter your name and e-mail address so we can respond."));
		error_ul.appendChild(tmpD);
		result = false;
	}
	if ($('sendType') == null || $('sendTo') == null){
		window.alert("Error sending your question/comment. Please reload the page and try again.");
		return false;
	}
	if (! result){
		var tP = document.createElement('p');
		var tB = document.createElement('b');
		tB.appendChild(document.createTextNode('The following errors occured when sending your question, please fix them and re-send your question'));
		tP.appendChild(tB);
		errorDiv.appendChild(tP);
		errorDiv.appendChild(error_ul);
		showDiv('errorDiv');
		$('errorDiv').scrollIntoView();
	} else {
		var params = {
			'sendType': sendType, 
			'sendTo': sendTo, 
			'action': 'send', 
			'pat_email': encodeURIComponent($('pat_email').value),
			'pat_name': encodeURIComponent($('pat_name').value),
			'the_ques': encodeURIComponent($('the_ques').value)
		};
		if (sendType == "subject"){
			params['subject'] = tmpSubj.name;
		}
		new Ajax.Request(theOnlyURL, {
			'method' : 'post',
			'parameters': params,
			'onSuccess' : function(transport){ doResult(transport); }
		});
	}
	/*$('sendType').value = sendType;
	$('sendTo').value = sendTo;
	return result;*/
}
/* ***************************************************** */
// Load information if any when choosing from select box
function loadInfo(arrayName,divName,index){
	eval("var tmpArray = " + arrayName + ";");
	var tmpDiv = getDiv(divName);
	if (tmpDiv != null){
		tmpDiv.innerHTML = '';
		if (tmpArray[index].length > 0){
			tmpDiv.className = 'information full';
		} else {
			tmpDiv.className = 'information';
		}
		tmpDiv.innerHTML = tmpArray[index];
		fixUnderflow();
	}
}
/* ***************************************************** */
function sortByLast(a, b){
   if(a.l_name > b.l_name)
      return 1
   if(a.l_name < b.l_name)
      return -1
   return 0 
}
/* ***************************************************** */
function chooseSubject(subID,divName){
	var tmpDiv = getDiv(divName);
	if (tmpDiv != null){
		tmpDiv.innerHTML = '';
		var tP = document.createElement('p');
		var tB = document.createElement('b');
		tB.appendChild(document.createTextNode('Subject Librarians'));
		tP.appendChild(tB);
		tmpDiv.appendChild(tP);
		var currSub = subjects['sub_' + subID];
		if (currSub.biblios.length > 0){
			if (currSub.biblios.length > 1){
				tP = document.createElement('p');
				tP.appendChild(document.createTextNode('Select one or more names to send your question to, or leave unselected to send to all.'));
				tmpDiv.appendChild(tP);
			}
			var tmpBibs = new Array();
			for (var foo=0;foo<currSub.biblios.length; foo++){
				var tBID = currSub.biblios[foo];
				tmpBibs.push(biblios['bib_' + tBID]);
			}
			var sorted = tmpBibs.sort(sortByLast);
			var tUL = document.createElement('ul');
			tUL.id = "biblio_choose";
			for (foo=0;foo<sorted.length;foo++){
				var tLI = document.createElement('li');
				tLI.id = "bib_" + sorted[foo].id;
				if (sorted.length > 1){
					var tA = document.createElement('a');
					if (document.all){
						tA.onclick = function(){ if (this.parentNode.className == ""){ this.parentNode.className = 'select'; } else { this.parentNode.className = '';}};
						tA.href = 'javascript://';
					} else {
						tA.setAttribute('onclick',"if (this.parentNode.className == ''){ this.parentNode.className = 'select'; } else { this.parentNode.className = '';}");
						tA.setAttribute('href','javascript://');
					}
					tA.appendChild(document.createTextNode(decodeURIComponent(sorted[foo].f_name) + ' ' + decodeURIComponent(sorted[foo].l_name) + ' '));
					tA.appendChild(document.createTextNode(' (' + decodeURIComponent(sorted[foo].location) + ') '));
					tLI.appendChild(tA);
				} else {
					tLI.appendChild(document.createTextNode(decodeURIComponent(sorted[foo].f_name) + ' ' + decodeURIComponent(sorted[foo].l_name) + ' '));
				}
				/* ** 2009-04-13 (whikloj) Stripping the mailto tag for multiple bibliographer entries
				tA = document.createElement('a');
				tA.href = 'mailto:' + sorted[foo].e_mail;
				tA.appendChild(document.createTextNode(decodeURIComponent(sorted[foo].e_mail)));
				tLI.appendChild(tA);
				*/
				var tBR = document.createElement('br');
				tLI.appendChild(tBR);
				tLI.appendChild(document.createTextNode(decodeURIComponent(sorted[foo].e_mail)));
				tUL.appendChild(tLI);
			}
			tmpDiv.appendChild(tUL);
		} else {
			tP = document.createElement('p');
			tP.appendChild(document.createTextNode('No librarian available. We apologize and suggest you try contacting a '));
			var tA = document.createElement('a');
			if (document.all)
				tA.href = "/libraries/reference/";
			else 
				tA.setAttribute("href","/libraries/reference/");
			tA.appendChild(document.createTextNode('reference librarian'));
			tP.appendChild(tA);
			tP.appendChild(document.createTextNode(' directly.'));
			tmpDiv.appendChild(tP);
		}
		fixUnderflow();
	}
}
/* ***************************************************** */
function doResult(oXML){
	var oJSON = oXML.responseJSON;
	var errorDiv = $('errorDiv');
	if (errorDiv != null)
		errorDiv.innerHTML = '';
	if (oJSON.result == "success"){
		var tP = document.createElement('p');
		errorDiv.className = 'success';
		tP.appendChild(document.createTextNode('Thank you. A response will be sent to the email address ('));
		var tB = document.createElement('b');
		tB.appendChild(document.createTextNode(decodeURIComponent(oJSON.pat_email)));
		tP.appendChild(tB);
		tP.appendChild(document.createTextNode(') you have provided.'));
		errorDiv.appendChild(tP);
	} else {
		var tP = document.createElement('p');
		errorDiv.className = '';
		tP.appendChild(document.createTextNode('There was a problem sending your message. [' + decodeURIComponent(oJSON.error_msg)+ ']'));
		errorDiv.appendChild(tP);
	}
	showDiv('errorDiv');
	$('errorDiv').scrollIntoView();
}
		

