// global variables

// subject field
subjectInFocus = "";

// format field
formatInFocus = "";

// display of outputs
showOutputs = false;
viewOutputs = false;
outputsSize = 1;

// display of menus
showLeftMenu = true;

// assigning of labels
labelsSize = 1;

// has read registration terms
readTerms = false;

// update subject field
function updateSubject() {
	select = document.getElementById("subjects");
	subject = document.getElementById("subject1");
	if(subjectInFocus !=""){
		subject = document.getElementById(subjectInFocus);
	}
	var selections = "";
	for (var i = 0; i < select.options.length; i++) {
	   if (select.options[i].selected){
	   	if(selections.length>0){
	   		selections += ", ";
	   	}
	   	selections += select.options[i].text;
	   }
	}
	if(subject.value!="" && selections!=""){
		subject.value += ", ";
	}
	subject.value += selections;
	return false;
}

// update format field
function updateFormat() {
	select = document.getElementById("formats");
	format = document.getElementById("format1");
	if(formatInFocus !=""){
		format = document.getElementById(formatInFocus);
	}
	var selections = "";
	for (var i = 0; i < select.options.length; i++) {
	   if (select.options[i].selected){
	   	if(select.options[i].text.charAt(0)!="-"){
		   	if(selections.length>0){
		   		selections += ", ";
		   	}
		   	selections += select.options[i].text;
	   	}
	   }
	}			
	if(format.value!="" && selections!=""){
		format.value += ", ";
	}
	format.value += selections;
	return false;
}

// check for required item contents
function checkCreateItem (checkFile) {
	var message = "";
	
	agree = document.getElementById("agree");
	agreemsg = document.getElementById("agreelabel");
	if(agree!=null){
		agreed = agree.checked;
	}else{
		agreed = null;
	}
   if(agreed==false){
   	message = "Sorry, you must agree to our terms of use by checking the agreement box!<br/>";
   	agreemsg.style.backgroundColor = "yellow";
   }else if(agree!=null){
   	agreemsg.style.backgroundColor = "white";
   }

	title = document.getElementById("itemtitle");
	if(title!=null){	
		titleValue = title.value;
	}else{
		titleValue = null;
	}
   if(titleValue==""){
   	message += "Please enter a Title for this item!<br/>";
   	title.style.backgroundColor = "yellow";
   }else if(title!=null){
   	title.style.backgroundColor = "white";
   }

	file = document.getElementById("itemfile");
	if(file!=null){	
		fileValue = file.value;
	}else{
		fileValue = null;
	}
	url = document.getElementById("itemurl");
	if(url!=null){	
		urlValue = url.value;
	}else{
		urlValue = null;
	}
	if(checkFile && fileValue=="" && urlValue==""){
		message += "Please enter a File or URL!<br/>";
	  	file.style.backgroundColor = "yellow";
	  	url.style.backgroundColor = "yellow";
	}else{
	  	file.style.backgroundColor = "white";
	  	url.style.backgroundColor = "white";
	}

	context = document.getElementById("itemcontext");
	if(context!=null){
		contextValue = context.options[context.selectedIndex].value;
	}else{
		contextValue = null;
	}
	if(contextValue=="" && (fileValue!="" || urlValue!="")){
		context.style.backgroundColor = "yellow";
		message += "Please enter a Material Type for your file!<br/>";
	}else if(context!=null){
		context.style.backgroundColor = "white";
	}

	copyright = document.getElementById("itemcopyright");
	if(copyright!=null){	
		copyrightValue = copyright.value;
	}else{
		copyrightValue = null;
	}
   if(copyrightValue==""){
   	message += "Please specify who has legal Copyright over this item!<br/>";
   	copyright.style.backgroundColor = "yellow";
   }else if(copyright!=null){
   	copyright.style.backgroundColor = "white";
   }

   if(message!=""){
	   document.getElementById('toperror').innerHTML = '<h2>A required field is empty!</h2>' + message;
  		return false;
  	}
  	return true;
}


// check for required item contents
function checkAssignLabels () {
	var message = "";
	
	agree = document.getElementById("agree");
	agreemsg = document.getElementById("agreelabel");
	if(agree!=null){
		agreed = agree.checked;
	}else{
		agreed = null;
	}
   if(agreed==false){
   	message = "Sorry, you must agree to our terms of use by checking the agreement box!<br/>";
   	agreemsg.style.backgroundColor = "yellow";
   }else if(agree!=null){
   	agreemsg.style.backgroundColor = "white";
   }

	for(var i=1; i<=labelsSize;){
		context = document.getElementById("itemcontext"+i);
		if(context!=null){
			contextValue = context.options[context.selectedIndex].value;
		}else{
			contextValue = null;
		}
		if(contextValue==""){
			context.style.backgroundColor = "yellow";
			message += "Please enter a Material Type for file number " + i + "!<br/>";
		}else if(context!=null){
			context.style.backgroundColor = "white";
		}		
		i++;
	}

	for(var i=1; i<=labelsSize;){
		copyright = document.getElementById("itemcopyright"+i);
		if(copyright!=null){	
			copyrightValue = copyright.value;
		}else{
			copyrightValue = null;
		}
	   if(copyrightValue==""){
	   	message += "Please specify who has legal Copyright over file number " + i +"!<br/>";
	   	copyright.style.backgroundColor = "yellow";
	   }else if(copyright!=null){
	   	copyright.style.backgroundColor = "white";
	   }
		i++;
	}

   if(message!=""){
	   document.getElementById('toperror').innerHTML = '<h2>A required field is empty!</h2>' + message;
  		return false;
  	}
  	return true;
}

// check for required field contents
function checkRequired () {
	var heading = '<h2>Alerts</h2>';

	title = document.getElementById("title1");
	if(title!=null){	
		titleValue = title.value;
	}else{
		titleValue = null;
	}
	discipline = document.getElementById("discipline");
	disciplineValue = discipline.value;
	subject = document.getElementById("subject1");
	subjectValue = subject.value;
	coverage = document.getElementById("coverage1");
	coverageValue = coverage.value;
	rights = document.getElementById("rights1");
	rightsValue = rights.value;
	research = document.getElementById("researchType");
	if(research!=null){
		researchValue = research.options[research.selectedIndex].value;
	}else{
		researchValue = null;
	}
  	study = document.getElementById("studyInput1");
  	if(study!=null){
  		studyValue = study.value;
  	}else{
  		studyValue = null;
  	}
  	source = document.getElementById("source1");
  	if(source!=null){
  		sourceValue = source.value;
  	}else{
  		sourceValue = null;
  	}
	type = document.getElementById("type");
	typeValue = type.options[type.selectedIndex].value;
  	programme = document.getElementById("programme");
  	if(programme!=null){
  		programmeValue = programme.value;
  	}else{
  		programmeValue = null;
  	}
  	progcheck = document.getElementById("progcheck");
  	if(progcheck!=null){
  		progcheckValue = progcheck.checked;
  	}else{
  		progcheckValue = null;
  	}
  	
   if(titleValue==""){
		title.style.backgroundColor = "yellow";
	}else{
		title.style.backgroundColor = "white";
	}
   if(disciplineValue==""){
		discipline.style.backgroundColor = "yellow";
	}else{
		discipline.style.backgroundColor = "white";
	}
   if(subjectValue==""){
		subject.style.backgroundColor = "yellow";
	}else{
		subject.style.backgroundColor = "white";
	}
   if(coverageValue==""){
		coverage.style.backgroundColor = "yellow";
	}else{
		coverage.style.backgroundColor = "white";
	}
   if(rightsValue==""){
		rights.style.backgroundColor = "yellow";
	}else{
		rights.style.backgroundColor = "white";
	}
   if(researchValue==""){
		research.style.backgroundColor = "yellow";
	}else{
		research.style.backgroundColor = "white";
	}   	
	if((researchValue=="Primary_Secondary" || researchValue=="Secondary") && studyValue=="" && sourceValue==""){
		study.style.backgroundColor = "yellow";
		source.style.backgroundColor = "yellow";
		message = "You must provide either a study number or data source for secondary type research!";
	}else{
		study.style.backgroundColor = "white";
		source.style.backgroundColor = "white";
		message = "";
	}
   if(typeValue==""){
		type.style.backgroundColor = "yellow";
	}else{
		type.style.backgroundColor = "white";
	}
	var programmeEmpty = false;
	if(!progcheckValue && programmeValue==""){
		programme.style.backgroundColor = "yellow";
		programmeEmpty = true;
	}else if(programme!=null){
		programme.style.backgroundColor = "white";
	}
	
	if(titleValue=="" || disciplineValue=="" || subjectValue=="" || programmeEmpty){
		if(document.getElementById("part1")!=null){
			document.getElementById("part1").style.color = 'red';
		}
	}else{
		if(document.getElementById("part1")!=null){
			document.getElementById("part1").style.color = '#5B88C4';
		}
	}
	if(rightsValue=="" || coverageValue=="" || researchValue=="" || typeValue=="" || message!=""){
		if(document.getElementById("part2")!=null){
			document.getElementById("part2").style.color = 'red';
		}
	}else{
		if(document.getElementById("part2")!=null){
			document.getElementById("part2").style.color = '#5B88C4';
		}
	}
   if(rightsValue=="" || titleValue=="" || disciplineValue=="" || subjectValue=="" || coverageValue=="" || researchValue=="" || typeValue=="" || message!="" || programmeEmpty){
   	if(message!=""){
   		message = "<br/><br/>" + message;
   	}
   	message = "A required field is empty!" + message;
		clearRightHelp();
		synchRightMenu("empty");
	   document.getElementById('righterror').innerHTML = heading + message;
	   return false;
   }
	return true;
}

// check for required field contents in registration
function checkRegistration (hasReadTerms, checkPassword) {
	message = "";
	passcnf = null;
	if(document.getElementById("password")){
		password = document.getElementById("password").value;
	}else{
		password = null;
	}
	if(document.getElementById("passwordConfirm")){
		var confirm = document.getElementById("passwordConfirm").value;
	}else{
		var confirm = null;
	}
	if(document.getElementById("userName")){
		user = document.getElementById("userName").value;
	}else{
		user = null;
	}
	first = document.getElementById("firstName").value;
	last = document.getElementById("lastName").value;
	email = document.getElementById("email").value;
	agree = document.getElementById("agree");
	agreemsg = document.getElementById("agreelabel");

	var alert = '<h2>Alerts</h2>';
	if(checkPassword){
		if(confirm==""){
			document.getElementById("passwordConfirm").style.backgroundColor = "yellow";
			passcnf = "";
		}else if(confirm!=null){
			document.getElementById("passwordConfirm").style.backgroundColor = "white";
		}
		if(password==""){
			document.getElementById("password").style.backgroundColor = "yellow";
			passcnf = "";
		}else if(password!=null){
			document.getElementById("password").style.backgroundColor = "white";
		}
	}else{
		if(password && password!="" && confirm==""){
			document.getElementById("passwordConfirm").style.backgroundColor = "yellow";
			passcnf = "";
		}else if(confirm!=null){
			document.getElementById("passwordConfirm").style.backgroundColor = "white";
		}
		if(confirm && confirm!="" && password==""){
			document.getElementById("password").style.backgroundColor = "yellow";
			passcnf = "";
		}else if(password!=null){
			document.getElementById("password").style.backgroundColor = "white";
		}
	}
	if(user=="" || !startsWithLetter(user)){
		document.getElementById("userName").style.backgroundColor = "yellow";
	}else if(user!=null){
		document.getElementById("userName").style.backgroundColor = "white";
	}
	if(first==""){
		document.getElementById("firstName").style.backgroundColor = "yellow";
	}else{
		document.getElementById("firstName").style.backgroundColor = "white";
	}
	if(last=="" || last.length<2){
		document.getElementById("lastName").style.backgroundColor = "yellow";
	}else{
		document.getElementById("lastName").style.backgroundColor = "white";
	}
	var emailMessage = emailCheck(email);
	if(email=="" || emailMessage!=""){
		document.getElementById("email").style.backgroundColor = "yellow";
	}else{
		document.getElementById("email").style.backgroundColor = "white";
	}
	if(agree!=null){
		agreed = agree.checked;
	}else{
		agreed = null;
	}
	if(hasReadTerms==true){
		// if registration failed but terms were read, we preset readTerms to true
		readTerms = true;
	}
	if(readTerms==false){
		message += "You must read our Terms and Conditions before registering with UKDA-store!<br/>"
	}
	if(agreed==false){
   	message += "Sorry, you must agree to our Terms and Conditions by checking the agreement box!<br/>";
	}
	if(!startsWithLetter(user)){
		message += "Please begin your username with an english letter!<br/>";
	}
	if(last.length<2){
		message += "Please enter at least 2 letters for your Surname!<br/>";
	}
	if(emailMessage!=""){
		message += "Please enter a valid Email Address! " + emailMessage + "<br/>";
	}
	if(user=="" || first=="" || last=="" || email=="" || passcnf==""){
		message += "A required field is empty!";
	}
   if(message!=""){
	   document.getElementById('toperror').innerHTML = alert + message;
  		return false;
  	}
  	return true;
}

function displayTerms(terms){
	readTerms = true;
 	window.open(terms,"terms","resizable=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes");
	return false;
}


// display image when accepted terms of use
function showImage(accept, src){
	accepted = accept.checked;
	
	if(accepted){
		img = document.getElementById("imagesrc");
		img.src = src;	
	}
}

// check for accepted terms of use
function accept(accepted){
	if(accepted){
		return true;
	}

	var accept = document.getElementById("accept");
	if(accept!=null){
		accepted = accept.checked;
	}else{
		accepted = false;
	}

   if(!accepted){
		var alert = '<h2>Alerts</h2>';   	
   	var message = "Sorry, you must accept our terms of use by checking the accept box!";
	   document.getElementById('toperror').innerHTML = alert + message;
  		return false;
  	}
  	return true;
}

// copy selected option to field
function selectOption(select) {
	selection = select.options[select.selectedIndex].value;
	if(selection!=""){
		field = document.getElementById(select.name);
		field.value = selection;
	}
}

// synchronise type option field
function updateType(select) {
	selection = select.options[select.selectedIndex].value;
	if(selection=="No_Data"){
		type = document.getElementById("type");
		type.selectedIndex = 4;
	}
}

// copy selected embargo period
function selectEmbargoPeriod(select) {
	field = select.options[select.selectedIndex].value;
	if(field==""){
		display = 'none';
	}else{
		display = '';
	}
	document.getElementById('embargoReason').style.display = display;
}

// display advanced search help text
function selectAdvancedSearch(select) {
	var message = null;
	var field = select.options[select.selectedIndex].value;
	if(endsWith(field.toLowerCase(),"date")){
		message = "<h2>Date Search Syntax:</h2>Use YYYY-MM-DD<br/>month and day are optional, e.g.<br/><br/><strong>2005-12-01</strong><br/><strong>2007-02</strong><br/><strong>2006</strong>";
	}else if(field=="contents"){
		message = "<h2>Full Text Search:</h2>Will search within the textual contents of data files where possible. <br/><br/>The following " +
			       "<a class=\"note\" onmouseover=\"Tip('" + contentsSearched + "')\">data formats</a> can be searched.";
	}
	if(message!=null){
		document.getElementById('advancedSearchHelp').innerHTML = message;
	}else{
		showEditHelp("search");
	}
}

// display edit help text
function showEditHelp(input) {
	field = input.name;
	if(field!=null){
		synchRightMenu("empty");
	}
	if(field==null){
		if(input=="upload"){
			helpfile = 'help/upload.jsp';
		}else if(input=="uploadpub"){
			helpfile = 'help/uploadpub.jsp';			
		}else if(input=="publication"){
			helpfile = 'help/publication.jsp';			
		}else if(input=="search"){
			if(document.getElementById("rightmenuempty")!=null){
				synchRightMenu("empty");
			}
			helpfile = 'help/search.jsp';			
		}else if(input=="access"){
			helpfile = 'help/access.jsp';			
		}else if(input){
			synchRightMenu("empty");
			helpfile = 'help/' + input + '.jsp';
		}else{
			helpfile = null;
		}
	}else if(field=="title"){
		var id = input.id;
		if(id=="title1"){
			helpfile = 'help/title.jsp';
		}else{
			helpfile = 'help/altTitle.jsp';
		}
	}else if(field=="contributor"){
		var id = input.id;
		if(id=="contributor1"){
			helpfile = 'help/institution.jsp';
		}else{
			helpfile = 'help/contributor.jsp';
		}
	}else if(field=="description"){
		var value = input.value;
		if(startsWith(value,"Amount of")){
			helpfile = 'help/descriptionAmount.jsp';
		}else if(startsWith(value,"Type of")){
			helpfile = 'help/descriptionType.jsp';
		}else{
			helpfile = 'help/description.jsp';
		}
	}else if(field=="subject"){
		synchRightMenu("subjects");
		helpfile = 'help/subject.jsp';
	}else if(field=="format"){
		synchRightMenu("formats");
		helpfile = 'help/format.jsp';
	}else{
		helpfile = 'help/' + field + '.jsp';
	}
	if(helpfile==null){
		clearRightHelp();
	}
	if(helpfile!="null"){
	    if (window.XMLHttpRequest) {
	    	  // code for Mozilla, Safari, ** And Now IE 7 **, etc
	        xmlhttp = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
	    	  //IE
	        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    if (typeof(xmlhttp)=='object') {
	        xmlhttp.onreadystatechange = postFileReady;
	        xmlhttp.open('GET', helpfile, true);
	        xmlhttp.send(null);
	    }
	}	
}

// function to handle asynchronous call
function postFileReady() {
   if (xmlhttp.readyState==4) {
      if (xmlhttp.status==200) {
      	var help = document.getElementById('righthelp');
      	if(help==null){
      		help = document.getElementById('advancedSearchHelp')
      	}
      	if(help!=null){
      		help.innerHTML = xmlhttp.responseText;
      	}
      }
   }
}

// show or hide 'edit award' outputs (dc references)
function showHideOutputs(anchor) {
	if(!showOutputs && anchor.title=="Hide Outputs"){
		showOutputs = true;
	}
	if(showOutputs){
		showOutputs = false;
		display = "none";
		var title = "Display Outputs";
		anchor.title = title;
		anchor.innerHTML = title;
	}else{
		showOutputs = true;
		display = "";
		var title = "Hide Outputs";
		anchor.title = title;
		anchor.innerHTML = title;
	}
	for(var i=1; i<outputsSize;){
		var id = "output"+i;
		trow = document.getElementById(id);
		document.getElementById(id).style.display = display;
		i++;
	}
	showEditHelp('output');
	return false;
}

// show or hide 'view award' outputs (dc references)
function showViewOutputs(anchor) {
	if(viewOutputs){
		viewOutputs = false;
		display = 'none';
		var title = "Display Outputs";
		anchor.title = title;
		anchor.innerHTML = title;
	}else{
		viewOutputs = true;
		display = '';
		var title = "Hide Outputs";
		anchor.title = title;
		anchor.innerHTML = title;
	}
	document.getElementById("outputs").style.display = display;
	return false;
}

// show or hide the left menu
function showHideLeftMenu(anchor) {
	if(showLeftMenu){
		document.getElementById("menu").style.display = '';
		anchor.title = "Hide Menu";
		showLeftMenu = false;
		anchor.innerHTML = anchor.innerHTML.replace("Open","Hide");
		anchor.innerHTML = anchor.innerHTML.replace("down","right");
		anchor.innerHTML = anchor.innerHTML.replace(/M_ A_ N_ A_ G_ E_/,"H_ I_ D_ E_");
	}else{
		document.getElementById("menu").style.display = 'none';
		anchor.title = "Open Menu";
		showLeftMenu = true;
		anchor.innerHTML = anchor.innerHTML.replace("Hide","Open");
		anchor.innerHTML = anchor.innerHTML.replace("right","down");
		anchor.innerHTML = anchor.innerHTML.replace(/H_ I_ D_ E_/,"M_ A_ N_ A_ G_ E_");
	}
}

// show or hide the collection left side bar 
function showHideLeftCollBar(anchor) {
	if(showLeftMenu){
		document.getElementById("sidebar").style.display = '';
		document.getElementById("hide").style.backgroundColor = "white";
		anchor.title = "Hide Menu";
		showLeftMenu = false;
		anchor.innerHTML = anchor.innerHTML.replace("Open","Hide");
		anchor.innerHTML = anchor.innerHTML.replace("down","right");
		anchor.innerHTML = anchor.innerHTML.replace(/O_ U_ T_ P_ U_ T_ S_/,"H_ I_ D_ E_");
	}else{
		document.getElementById("sidebar").style.display = 'none';
		document.getElementById("hide").style.backgroundColor = "#EBE7E7";
		anchor.title = "Open Menu";
		showLeftMenu = true;
		anchor.innerHTML = anchor.innerHTML.replace("Hide","Open");
		anchor.innerHTML = anchor.innerHTML.replace("right","down");
		anchor.innerHTML = anchor.innerHTML.replace(/H_ I_ D_ E_/,"O_ U_ T_ P_ U_ T_ S_");
	}
}

// show or hide the item left side bar 
function showHideLeftItemBar(anchor) {
	if(showLeftMenu){
		document.getElementById("sidebar").style.display = '';
		document.getElementById("hide").style.backgroundColor = "white";
		anchor.title = "Hide Menu";
		showLeftMenu = false;
		anchor.innerHTML = anchor.innerHTML.replace("Open","Hide");
		anchor.innerHTML = anchor.innerHTML.replace("down","right");
		anchor.innerHTML = anchor.innerHTML.replace(/F_ I_ L_ E_ S_/,"H_ I_ D_ E_");
	}else{
		document.getElementById("sidebar").style.display = 'none';
		document.getElementById("hide").style.backgroundColor = "#EBE7E7";
		anchor.title = "Open Menu";
		showLeftMenu = true;
		anchor.innerHTML = anchor.innerHTML.replace("Hide","Open");
		anchor.innerHTML = anchor.innerHTML.replace("right","down");
		anchor.innerHTML = anchor.innerHTML.replace(/H_ I_ D_ E_/,"F_ I_ L_ E_ S_");
	}
}

// show or hide the Edit DC menu
function showHideEditDC(anchor) {
	form = anchor.id;
	if(form==null){
		form = document.getElementById(anchor).id;
	}	
	if(form=="part1"){
		// part1 clicked
		document.getElementById("editdc1").style.display = '';
		document.getElementById("editdc2").style.display = 'none';
		if(document.getElementById("editdc3")!=null){
			document.getElementById("editdc3").style.display = 'none';
		}
		document.getElementById("part2").className = 'inactive';
		if(document.getElementById("part3")!=null){
			document.getElementById("part3").className = 'inactive';
		}
		document.getElementById("part").value = "1";
	}else if(form=="part2"){
		// part2 clicked
		document.getElementById("editdc1").style.display = 'none';
		document.getElementById("editdc2").style.display = '';
		if(document.getElementById("editdc3")!=null){
			document.getElementById("editdc3").style.display = 'none';
		}
		document.getElementById("part1").className = 'inactive';
		if(document.getElementById("part3")!=null){
			document.getElementById("part3").className = 'inactive';
		}
		document.getElementById("part").value = "2";
	}else{
		// part3 clicked
		document.getElementById("editdc1").style.display = 'none';
		document.getElementById("editdc2").style.display = 'none';
		document.getElementById("editdc3").style.display = '';
		document.getElementById("part1").className = 'inactive';
		document.getElementById("part2").className = 'inactive';
		document.getElementById("part").value = "3";
	}
	synchRightMenu("empty");
	clearRightHelp();
	anchor.className = 'active';
}

function clearRightHelp(){
	var help = document.getElementById('righthelp');
	if(help==null){
		help = document.getElementById('advancedSearchHelp')
	}
	if(help!=null){
		help.innerHTML = "<br/>";
	}		
}

function alertMessage(message,obj1,obj2){
	document.getElementById(obj1).innerHTML = "<h2>Alerts</h2>" + message;
	if(obj2){
		document.getElementById(obj2).innerHTML = "<h2>Alerts</h2>" + message;
	}
}

function synchRightMenu(panel){
	if(panel=="empty"){
		if(document.getElementById("rightmenuempty")){
			document.getElementById("rightmenutags").style.display = 'none';
			document.getElementById("rightmenuformats").style.display = 'none';
			document.getElementById("rightmenuempty").style.display = '';
		}
	}else if(panel=="subjects"){
		if(document.getElementById("rightmenutags")){
			document.getElementById("rightmenutags").style.display = '';
			document.getElementById("rightmenuformats").style.display = 'none';
			document.getElementById("rightmenuempty").style.display = 'none';
		}
	}else if(panel=="formats"){
		if(document.getElementById("rightmenuformats")){
			document.getElementById("rightmenutags").style.display = 'none';
			document.getElementById("rightmenuformats").style.display = '';		
			document.getElementById("rightmenuempty").style.display = 'none';
		}
	}
}

// startsWith function
function startsWith(str, s){
	var reg = new RegExp("^" + s);
	return reg.test(str);
}

// endsWith function
function endsWith(str, s){
	var reg = new RegExp(s + "$");
	return reg.test(str);
}

// startsWithLetter function
function startsWithLetter(str){
	var reg = new RegExp(/^[a-zA-Z]/);
	return reg.test(str);
}

// Confirmation function
function Confirmation (msg) {
  	if (confirm(msg)) {
   	return true;
	} else {
		return false;
  }
}

maxImageWidth = 800;

function fitImage(id){
	var image = document.getElementById(id);
	if(image.width > maxImageWidth){
		image.width = maxImageWidth;
	}
}

// show or hide the Create CC menu
function showHideCreateCC(anchor) {
	form = anchor.id;
	if(form==null){
		form = document.getElementById(anchor).id;
	}	
	if(form=="create1"){
		// create1 clicked
		document.getElementById("createcc1").style.display = '';
		document.getElementById("createcc2").style.display = 'none';
		document.getElementById("create2").className = 'inactive';
		document.getElementById("create").value = "add";
	}else if(form=="create2"){
		// create2 clicked
		document.getElementById("createcc1").style.display = 'none';
		document.getElementById("createcc2").style.display = '';
		document.getElementById("create1").className = 'inactive';
		document.getElementById("create").value = "create";
	}
	anchor.className = 'active';
}
