//menu constructor
function menu(allitems,thisitem,startstate){ 
  callname= "gl"+thisitem;
  divname="subglobal"+thisitem;  
  this.numberofmenuitems = allitems;
  this.caller = document.getElementById(callname);
  this.thediv = document.getElementById(divname);
  this.thediv.style.visibility = startstate;
}

//menu methods
function ehandler(event,theobj){
  for (var i=1; i<= theobj.numberofmenuitems; i++){
    var shutdiv =eval( "menuitem"+i+".thediv");
    shutdiv.style.visibility="hidden";
  }
  theobj.thediv.style.visibility="visible";
}
	
function closesubnav(event){
  var containerLeft = document.getElementById("container").offsetLeft;
  if ((event.clientX - containerLeft < 5) || (event.clientX - containerLeft > 400) || (event.clientY < 25)  || (event.clientY > 110)){
    for (var i=1; i<= numofitems; i++){
      var shutdiv =eval('menuitem'+i+'.thediv');
      shutdiv.style.visibility='hidden';
    }
  }
}

function getFileName(theForm) {
	var result = true;	
	
	for(var i = 0; i<theForm.elements.length; i++){
		var e = theForm.elements[i];
		
		if(e.id=='fileData'){
			var fileDataId = i;		
			}

		if(e.id=='fileName'){
			var fileNameId = i;
			}						
		}
		
	theForm.elements[fileNameId].value = theForm.elements[fileDataId].value;	
	var s = theForm.elements[fileDataId].value;
	var i = s.lastIndexOf("\\");
	if (i == -1) {
		i = s.lastIndexOf("/");
	}
	var theFileName = s.substring(i + 1, s.length);
	theForm.elements[fileNameId].value = theFileName;	
	return result;	
}

function ActionDeterminator(theForm, newAction){
	var result = true;	
	theForm.action= newAction;	
	if(newAction == 'index.php?ses=worksheets.delete') {
		if (!confirm('Are you sure that you want to delete this worksheet?')){
			result = false;
			}
	}		
	return result;
}

function addWorksheet(theForm) {
	var result = true;	
	theForm.action= 'index.php?ses=worksheets.add';	
	theForm.method= 'post';
	theForm.submit();
}

function forceGet(theForm) {
	form.method='get';
	form.RealMethod.value='get';
}

function publish(theForm) {
	var result = false;
	if(checkboxController(theForm)==true){	
		theForm.action= 'index.php?ses=worksheets.publish';	
		method="post";
		result = true;
	}else{
		alert('Publish what?' );	
		result = false;
	}
	return result;
}

function unpublish(theForm) {
	var result = false;
	if(checkboxController(theForm)==true){	
		theForm.action= 'index.php?ses=worksheets.unpublish';
		theForm.method="post";
		result = true;
	}else{
		alert('Unpublish what?' );	
		result = false;
	}
	return result;
}

function removeFavouriteWorksheets(theForm) {
	var result = false;
	if(checkboxController(theForm)==true){	
		theForm.action= 'index.php?ses=worksheets.unfavourite';	
		theForm.method="post";
		result = true;
	}else{
		alert('Remove what?' );	
		result = false;
	}
	return result;
}

function checkboxController(theForm){
	var list = new Array();
	for(var i = 0; i<theForm.elements.length; i++){
		var e = theForm.elements[i];
		if (e.type=="checkbox") {			
			if(e.checked){
				e.value ="checked";
				list.push(e);
			}
		}
	}
	return (list.length > 0);			
}

function countSelected(theForm){
	var count = 0;
	for(var i = 0; i<theForm.elements.length; i++){
		var e = theForm.elements[i];
		if (e.type=="checkbox") {			
			if(e.checked){
				count++;
			}
		}
	}
	return count;			
}


function deleteWorksheets(theForm) {
	var result = false;
	var selected = countSelected(theForm);
	if(selected > 0){	
		var confirmStr = "You are about to delete " + selected + " worksheet";
		if (selected > 1) {
			confirmStr = confirmStr + "s";
		}
		confirmStr = confirmStr + ".\nAre you sure?";
		if(confirm(confirmStr)){
			theForm.action= 'index.php?ses=worksheets.delete';	
			theForm.method="post";
			result = true;
		}
	} else {
		alert('Delete what?' );	
	}
	return result;
}

// DA's function for searching out disabled buttons and applying style accordingly

function buttonstate() {
	count = 0;
	// loop through form elements to see what is selected
	for(j=0;j<document.forms.length;j++) {
		var f_name = document.forms[j]; // create form object
		var elcount = f_name.elements.length; // count form elements on the page
		for (i=0;i<elcount;i++) {
			if (f_name.elements[i].type == "button" || f_name.elements[i].type == "submit") {
				// colour buttons
				f_name.elements[i].style.backgroundColor = "#00b000"; // if it's not disabled make it look clickable
				f_name.elements[i].style.color = "#ffffff";
				// change colour if buttons disabled
				if(f_name.elements[i].disabled == "disabled") {
					f_name.elements[i].style.backgroundColor = "#808080"; // colour disabled button
					f_name.elements[i].style.color = "#000000";
				}
			}
		}
	}
}

function enableButtons(theForm){
	if(checkboxController(theForm)==true) {	
		theForm.deleteButton.disabled = false;
		theForm.unpublishButton.disabled = false;
		theForm.publishButton.disabled = false;
	} else {
		theForm.deleteButton.disabled = true;
		theForm.unpublishButton.disabled = true;
		theForm.publishButton.disabled = true;
	}
	buttonstate();
	return true;
}

function enableFavouriteButtons(theForm){
	theForm.removeButton.disabled = !checkboxController(theForm);
	buttonstate();
	return true;
}

function viewInScorch(theForm,worksheetId){
	alert('viewInScorch, worksheetId: ' + worksheetId);
	theForm.worksheetId = worksheetId;
	theForm.action= 'viewFile.ses';		
}

// check that the advanced search form has some text data in it
function t_form() {
	with (document.forms[0]) {
	if (worksheet_name.value != "" || keywords.value !== "" || description.value !== "" || filename.value !== "") {
		do_search.value = "true";
		submit();
		}
	}
}


// simple email checker
// this equest to matching this perl regular expression if you like to think like this (I wouldn't advise it
// becasue I might be wrong, and it will hurt your brain)
// /.+@(.+(\.))+.{2}/
function isEmailValid(email) {

	var valid=true;
	if (email == "") {
		valid=false;
	} else {
		var atPos = email.indexOf('@');
		if (atPos < 1) {	// does the email address contain an @ with something before it?
			valid=false;
			//alert("no at");
		} else {
			var firstDotPos = email.indexOf('.',atPos);
			var lastDotPos =  email.lastIndexOf('.');
			if (firstDotPos == -1) {	// there is no dot
				valid=false;
				//alert("no dot");
			} else {
				if (firstDotPos <= (atPos+1)) {	// make sure that there is something between @ and the first .
					valid=false;
					//alert("first dot not at least one chr after at");
				} else {
					if (lastDotPos >= (email.length-2)) {	// make sure that is at least 2 chrs after the last .
						valid=false;
						//alert("Last dot too near end");
					}
				}
			}
		}
	}
	
	return valid;
}

function loadHelpWindow(helpPage,topic) {
	if (window.location.host == "localhost") {
		strLoc = "/SibEd";
	} else {
		strLoc = "";
	}
	window.open(strLoc + '/index.php?ses=help.' + helpPage,
			'SibeliusEducationHelp' + topic,
			'scrollbars=yes,title=yes,menubar=no,height=600,width=450,resizable=no,toolbar=no,location=no,status=no');
}

function sibeliusVersionString(versionNumber) {
	var ret = versionNumber;
	if (versionNumber == 3) {
		ret = "Student/" + ret;
	}
	return ret;
}

function trimstring(str, len) {
	var ret = str;
	if (str.length > len) {
		ret = str.substring(0,len-3);
		ret = ret + '...';
	}
	return ret;
}

function dateString() {
	var d = new Date();
	return d.toDateString();
}

function appendDateString(field, tag) {
	var beginTag = "";
	if (field.value.length > 0) {
		beginTag = "\n\n"
	}
	var endTag = "\n";
	if (tag != null) {
		beginTag = beginTag + "<" + tag + ">";
		endTag = "</" + tag + ">" + endTag;
	}
	var s = beginTag + dateString() + endTag;
	field.value = field.value + s;
}

// DA's simple two function for showing / hiding layers

function showvid() {
	document.getElementById("video").style.display = "block";
	document.getElementById("videoplay").style.display = "none";
}

function hidevid() {
	document.getElementById("video").style.display = "none";
	document.getElementById("videoplay").style.display = "block";
}

function showhide(layer) {
	
	if (layer == "app") {
		document.getElementById("app").style.display = "block";
		document.getElementById("server").style.display = "none";
	}
	
	if (layer == "server") {
		document.getElementById("app").style.display = "none";
		document.getElementById("server").style.display = "block";
	}
}
