function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function check(Text) {
	var a = confirm(Text);
	if (a) return true;
	else return false;
	}

function changeLink(selectValue, linkId) {
	var destination = document.getElementById(linkId).href;
	var newDestination = destination.replace(/addteacher\/(\d)*/, "addteacher\/" + selectValue);
	document.getElementById(linkId).href = newDestination;
	}

function changeEmailLink(emailId, linkId) {
	var destination = document.getElementById(linkId).href;
	var email = document.getElementById(emailId).value;
	var newDestination = destination.replace(/addemail\/(.)*/, "addemail\/" + email);
	document.getElementById(linkId).href = newDestination;
	return true;
	}

function changeContextAddLink() {
	var destination = document.getElementById("contextAdd").href;
	var destination2 = document.getElementById("questionAdd").href;
	var newDestination = destination.replace(/contextadd\/(\d)*/, "contextadd\/" + document.forms["formQuestionnaire"].elements["context"].options[document.forms["formQuestionnaire"].elements["context"].selectedIndex].value);
	var newDestination2 = destination2.replace(/context\/(.)*/, "context/" + document.forms["formQuestionnaire"].elements["context"].options[document.forms["formQuestionnaire"].elements["context"].selectedIndex].value + "/questionadd/" + document.forms["formQuestionnaire"].elements["question"].options[document.forms["formQuestionnaire"].elements["question"].selectedIndex].value);
	document.getElementById("contextAdd").href = newDestination;
	document.getElementById("questionAdd").href = newDestination2;
	}

function changeQuestionAddLink() {
	var destination = document.getElementById("questionAdd").href;
	var newDestination = destination.replace(/context\/(.)*/, "context/" + document.forms["formQuestionnaire"].elements["context"].options[document.forms["formQuestionnaire"].elements["context"].selectedIndex].value + "/questionadd/" + document.forms["formQuestionnaire"].elements["question"].options[document.forms["formQuestionnaire"].elements["question"].selectedIndex].value);
	document.getElementById("questionAdd").href = newDestination;
	
	}

 function pwGen(len) {
	
	conso = new Array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z");
	vocal = new Array("a","e","i","o","u");
	consoBig = new Array("B","C","D","F","G","H","J","K","L","M","N","O","P","Q","R","S","W","X","Y","Z");
	vocalBig = new Array("A","E","I","O","U");
	numbers = new Array("0","1","2","3","4", "5", "6", "7", "8", "9");
	
	var password = '';

	for(i=0; i < len; i++) {
		var c = Math.ceil(Math.random() * 1000) % 20;
		var c2 = Math.ceil(Math.random() * 1000) % 20;
		var v = Math.ceil(Math.random() * 1000) % 5;
		var v2 = Math.ceil(Math.random() * 1000) % 5;
		var n = Math.ceil(Math.random() * 1000) % 10;
		var n2 = Math.ceil(Math.random() * 1000) % 10;
		var n3 = Math.ceil(Math.random() * 1000) % 10;
		
		var array = new Array(conso[c], vocal[v], consoBig[c2], vocalBig[v2], numbers[n], numbers[n2], numbers[n3]);
		var pwChar = Math.ceil(Math.random() * 1000) % 5;
		password += array[pwChar];
	}

	return password;

}

 // help function for checking valid email inputs
function isEmail(email) {
	var regex = "^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$";
	var regexp = new RegExp(regex, 'i');
	email = email.toLowerCase();
	return (email.test(regexp));
}
