document.attachEvent("onclick", hideMenu);


//==============================
// OPTIONS
//==============================

function moveOptionUp(obj){

	newListVals = new Array();
	newListText = new Array();
	
	
	if((obj.value=="NEW")||(obj.value=="")){return;}
	
	for(i=0;i<obj.length;i++){
		if(obj.options[i].selected==true){
			var master_newpos = i-1;
			var slave_newpos = i;
			if(i==0){return;}
		}
	}

	//insert before options
	
	for(i=0;i<master_newpos;i++){
		if((i!=master_newpos)&&(i!=slave_newpos)){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}

	// insert new options
	
	newListVals.push(obj.options[slave_newpos].value);
	newListText.push(obj.options[slave_newpos].text);
	newListVals.push(obj.options[master_newpos].value);
	newListText.push(obj.options[master_newpos].text);	
	
	//insert after options
	
	for(i=slave_newpos;i<obj.length;i++){
		if((i!=master_newpos)&&(i!=slave_newpos)){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}
	
	obj.options.length = 0;
	
	for(i=0;i<newListVals.length;i++){
		obj.options[i]= new Option(newListText[i],newListVals[i]);
	}
	obj.options[master_newpos].selected=true;
}

function moveOptionDown(obj){

	newListVals = new Array();
	newListText = new Array();
	
	
	if((obj.value=="NEW")||(obj.value=="")){return;}
	
	for(i=0;i<obj.length;i++){
		if(obj.options[i].selected==true){
			var master_newpos = i+1;
			var slave_newpos = i;
			if(i==obj.length-1){return;}
		}
	}
	
	
	//insert before options
	
	for(i=0;i<slave_newpos;i++){
		if((i!=master_newpos)&&(i!=slave_newpos)){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}
	
	// insert new options
	
	newListVals.push(obj.options[master_newpos].value);
	newListText.push(obj.options[master_newpos].text);	
	newListVals.push(obj.options[slave_newpos].value);
	newListText.push(obj.options[slave_newpos].text);


	//insert after options
	
	for(i=master_newpos;i<obj.length;i++){
		if((i!=master_newpos)&&(i!=slave_newpos)){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}
	
	obj.options.length = 0;
	
	for(i=0;i<newListVals.length;i++){
		obj.options[i]= new Option(newListText[i],newListVals[i]);
	}
	obj.options[master_newpos].selected=true;
}

function moveOptionTop(obj){

	newListVals = new Array();
	newListText = new Array();
	
	
	if(obj.value==""){return;}

	for(i=0;i<obj.length;i++){
		if(obj.options[i].selected==true){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}

	for(i=0;i<obj.length;i++){
		if(obj.options[i].selected==false){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}

	obj.options.length = 0;
	
	for(i=0;i<newListVals.length;i++){
		obj.options[i]= new Option(newListText[i],newListVals[i]);
	}
	obj.options[0].selected=true;
}

function moveOptionBottom(obj){

	newListVals = new Array();
	newListText = new Array();
	
	
	if(obj.value==""){return;}

	for(i=0;i<obj.length;i++){
		if(obj.options[i].selected==false){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}

	for(i=0;i<obj.length;i++){
		if(obj.options[i].selected==true){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}
	}

	obj.options.length = 0;
	
	for(i=0;i<newListVals.length;i++){
		obj.options[i]= new Option(newListText[i],newListVals[i]);
	}
	obj.options[obj.options.length-1].selected=true;
}

function moveOptionNum(obj){

	newListVals = new Array();
	newListText = new Array();
	
	var num = document.forms[0].ob_num.value;
	
	if(num>obj.length){num=obj.length;}
	
	if(obj.value==""){return;}

	for(i=0;i<obj.length;i++){
		if(obj.options[i].selected==true){
			var ob_pos = [i];
		}
	}

	for(i=0;i<obj.length;i++){
		if(i==(num-1)){
			newListVals.push(obj.options[ob_pos].value);
			newListText.push(obj.options[ob_pos].text);
		}

		if(obj.options[i].selected==false){
			newListVals.push(obj.options[i].value);
			newListText.push(obj.options[i].text);
		}

	}


	obj.options.length = 0;
	
	for(i=0;i<newListVals.length;i++){
		obj.options[i]= new Option(newListText[i],newListVals[i]);
	}
	obj.options[num-1].selected=true;
}


//=============================
// MENU
//=============================

var menu_db_id;
var menu_id;

function showMenu(db_id,id,menu_name){
	moveMenu();
	setTimeout("showMenu2("+db_id+","+id+",'"+menu_name+"')",10);
}

function showMenu2(db_id,id,menu_name){
	menu_db_id = db_id;
	menu_id = id;
	document.getElementById("menu").style.visibility="visible";
	document.getElementById("menu_title").innerText = menu_name;
}

function hideMenu(){
	
	var n_frames = top.document.frames.length;
	for(i=0;i<n_frames;i++){
		if(top.document.frames[i].document.getElementById("menu")){
			top.document.frames[i].document.getElementById("menu").style.visibility="hidden";
		}
	}
	
	if(document.getElementById("menu")){
		document.getElementById("menu").style.visibility="hidden";
	}
}

function moveMenu(){

	var menu = document.getElementById("menu");
	var xMousePos = window.event.x+document.body.scrollLeft;
	var yMousePos = window.event.y+document.body.scrollTop;
	var xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
	var yMousePosMax = document.body.clientHeight+document.body.scrollTop;
	var menu_width = menu.clientWidth;
	var menu_height = menu.clientHeight;
	var maxPosX = (xMousePosMax-menu_width)-10;
	var maxPosY = (yMousePosMax-menu_height)-10;

	if(xMousePos>maxPosX){var leftPos = xMousePos-(menu_width+10);}
	else{var leftPos = xMousePos+10;}
	if(leftPos<10){leftPos=10;}
	menu.style.left = leftPos
	
	if(yMousePos>maxPosY){var topPos = yMousePos-(menu_height+10);}
	else{var topPos = yMousePos+10;}
	if(topPos<10){topPos=10;}
	menu.style.top = topPos
}

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){while (obj.offsetParent){curleft += obj.offsetLeft;obj = obj.offsetParent;}}
	else if(obj.x){curleft += obj.x;}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){while (obj.offsetParent){curtop += obj.offsetTop;obj = obj.offsetParent;}}
	else if(obj.x){curleft += obj.x;}
	return curtop;
}


//===========================
// ADD VARIABLES TO TEXT
//===========================

function insertText(input, insTexte){

	if(!insTexte){return;}

	startTag = '';
	endTag = '';

	if(input.createTextRange){
		var text;
		input.focus(input.caretPos);
		input.caretPos = document.selection.createRange().duplicate();
		if(input.caretPos.text.length>0){
			input.caretPos.text = startTag + input.caretPos.text + endTag;
		} else{
			input.caretPos.text = startTag +  insTexte +  endTag;
		}
	}
	else{input.value += startTag + insTexte + endTag;}
}

//===========================
// ADD HTML TAGS TO TEXT
//===========================

var curSelVal = ""; 
var startPos = 0; 
var endPos = 0;

function storeCurVal(obj, start, end, ob) { 
	startPos = obj.selectionStart; 
	endPos = obj.selectionEnd; 
	curSelVal = document.forms[0].email_content.value; 
} 

function getSel(tag){    
	
	if(document.selection){ 
		oldString = document.selection.createRange().text; 
		if (oldString!=""){ 
			document.selection.createRange().text = '<' + tag + '>' + oldString + '</' + tag + '>'; 
		} 
		return false; 
	}
	
	else if(window.getSelection){ 
		oldString = curSelVal; 
		newString = ''; 
		finalString = ''; 
		len = curSelVal.length;
		if((len>0)&&(document.forms[0].email_content.value.length>0)){ 
			firstPart = oldString.substring(0, startPos); 
			lastPart = oldString.substring(endPos, len); 

			for(i=startPos;i<endPos;i++){ 
				newString += oldString[i]; 
			}
			finalString = '<' + tag + '>' + newString + '</' + tag + '>'; 
			document.forms[0].email_content.value = firstPart + finalString + lastPart; 
		}
		else{curSelVal = "";} 
	}
	else if(document.getSelection){
		txt = obj.setSelectionRange(obj.selectionStart, obj.selectionEnd); 
	} 
} 
//=============================
// COLOR PICKER
//=============================

function showColor(color){
	textbox = document.getElementById("color");
	colorbox = document.getElementById("col");
	textbox.value=color;
	colorbox.style.backgroundColor=color;
}
function clearColor(){
	textbox = document.getElementById("color");
	colorbox = document.getElementById("col");
	textbox.value="";
	colorbox.style.backgroundColor="#ffffff";
}
function chooseColor(color){
	if(color){
		textbox = document.getElementById(whichcolor);
		textbox.value=color;
		document.getElementById(whichcolor+"_cell").style.backgroundColor=color;
		whichcolor="";
		hideAllDivs();
	}
}
function hideAllDivs(){

	picker= document.getElementById("colorpicker");
	picker.style.visibility="hidden";
	picker.style.left = 0;
	picker.style.top = 0;

}

function showPicker(ob,check){
	if(!check){
		setTimeout("showPicker('"+ob+"',1)",100);
		return;
	}
	//ob = document.getElementById(ob);
	whichcolor=ob;
	picker = document.getElementById("colorpicker");
	movePicker(ob);
	picker.style.visibility="visible";

}

function checkColor(val,box){

	var char0 = val.charAt(0);
	var char;
	var ok;

	val = val.toUpperCase();
	box.value=val;
	
	if(val.length!=7){box.style.backgroundColor="";box.value="";return;}
	if(char0!="#"){box.style.backgroundColor="";box.value="";return;}
	
	for(i=1;i<=6;i++){
		char = val.charAt(i);
		if(isNaN(char)){
			if((char=="A")||(char=="B")||(char=="C")||(char=="D")||(char=="E")||(char=="F")){}
			else{box.style.backgroundColor="";box.value="";return;}
		}
	}
	
	box.style.backgroundColor=val;
}

function movePicker(obj){

	var ob = document.getElementById(obj);
	var picker = document.getElementById("colorpicker");
	var lefty = findPosX(ob);
	var toppy = findPosY(ob);
	picker.style.left = lefty+200;
	picker.style.top = toppy+0;

}

var whichcolor;


//=============================
// TREE
//=============================

function getView(){
	bits = new Array();
	for(i=0;i<ids.length;i++){
		if(document.getElementById(ids[i])){
			if(document.getElementById(ids[i]).style.display=="block"){
				bits.push(ids[i]);
			}
		}
	}
	return bits.join("<>");
}

function showView(bits){
	for(i=0;i<bits.length;i++){
		if(document.getElementById(bits[i])){
			document.getElementById(bits[i]).style.display="block";
		}
	}
}

function changeView(children,all_children,this_scale_id,img,items,server,questions){
	if(img.src==server+'/images/plus.gif'){showChildren(children,this_scale_id,img,items,questions);}
	else{hideChildren(all_children,this_scale_id,img,items,questions);}
}


function showChildren(children,this_scale_id,img,items,questions){
	img.src="/images/minus.gif";
	if(children!=""){
		scale_id = children.split("<>");
		for(i=0;i<scale_id.length;i++){
			document.getElementById("scale_"+scale_id[i]).style.display = "block";
		}
	}else{
		showItems(items,questions);
	}
}

function hideChildren(children,this_scale_id,img,items,questions){
	if(children!=""){
		scale_id = children.split("<>");
		for(i=0;i<scale_id.length;i++){
			document.getElementById("scale_"+scale_id[i]).style.display = "none";
			document.getElementById("img_"+scale_id[i]).src="/images/plus.gif";
		}
	}
	if(items!=""){
		item_id = items.split("<>");
		for(i=0;i<item_id.length;i++){
			if(document.getElementById("item_"+item_id[i])){
				document.getElementById("item_"+item_id[i]).style.display = "none";
			}
		}
	}
	if(questions!=""){
		question_id = questions.split("<>");
		for(i=0;i<question_id.length;i++){
			if(document.getElementById("question_"+question_id[i])){
				document.getElementById("question_"+question_id[i]).style.display = "none";
			}
		}
	}
	img.src="/images/plus.gif";
}

function showItems(items,questions){
	if(items!=""){
		item_id = items.split("<>");
		for(i=0;i<item_id.length;i++){
			if(document.getElementById("item_"+item_id[i])){
				document.getElementById("item_"+item_id[i]).style.display = "block";
			}
		}
	}
	if(questions!=""){
		question_id = questions.split("<>");
		for(i=0;i<question_id.length;i++){
			if(document.getElementById("question_"+question_id[i])){
				document.getElementById("question_"+question_id[i]).style.display = "block";
			}
		}
	}
}

//=============================
// QUESTIONNAIRE
//=============================


function removeQuestionnaire(){
	if(!confirm("Remove this questionnaire and all associated data?")){return;}
	document.forms[0].which_action.value = "remove_questionnaire";
	document.forms[0].submit();
}

function duplicateQuestionnaire(){
	if(!confirm("Duplicate this questionnaire?")){return;}
	document.forms[0].which_action.value = "duplicate_questionnaire";
	document.forms[0].submit();
}

function saveQuestionnaireChanges(){
	var bad=0;
	if(document.forms[0].questionnaire_name.value==""){bad=1;}
	if(document.forms[0].questionnaire_display.value==""){bad=1;}
	if(document.forms[0].questionnaire_rating_scale_id.value==""){bad=1;}
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Save changes?")){return;}
	document.forms[0].which_action.value = "save_questionnaire";
	document.forms[0].submit();
}

function addQuestionnaire(){
	var bad=0;
	if(document.forms[0].questionnaire_name.value==""){bad=1;}
	if(bad==1){alert("Please enter a name for this questionnaire.");return;}
	if(!confirm("Create this questionnaire?")){return;}
	document.forms[0].which_action.value = "add_questionnaire";
	document.forms[0].submit();
}

function randomise(){
	if(!confirm("Randomise the questionnaire display order?")){return;}
	document.forms[0].which_action.value = "randomise";
	document.forms[0].submit();
}

function addScale(){
	if(document.forms[0].new_scale_name.value==""){alert("Please enter a name for this scale.");return;}
	if(!confirm("Add this scale?")){return;}
	document.forms[0].which_action.value = "add_scale";
	document.forms[0].submit();
}

function addItemToScale(){
	bits = new Array();
	for(i=0;i<document.forms[0].elements.length;i++){
		if(document.forms[0].elements[i].type=="checkbox"){
			if(document.forms[0].elements[i].checked==true){
				bits.push(document.forms[0].elements[i].value);
			}
		}
	}
	if(!bits[0]){
		alert("Please select at least 1 item.");
		return;
	}
	if(!confirm("Add selected to scale?")){return;}
	parent.document.forms[0].library_id.value=bits.join("<>");
	parent.document.forms[0].which_action.value = "add_item";
	parent.document.forms[0].submit();
}

function addQuestionToScale(){
	bits = new Array();
	for(i=0;i<document.forms[0].elements.length;i++){
		if(document.forms[0].elements[i].type=="checkbox"){
			if(document.forms[0].elements[i].checked==true){
				bits.push(document.forms[0].elements[i].value);
			}
		}
	}
	if(!bits[0]){
		alert("Please select at least 1 question.");
		return;
	}
	if(!confirm("Add selected to scale?")){return;}
	parent.document.forms[0].library_id.value=bits.join("<>");
	parent.document.forms[0].which_action.value = "add_question";
	parent.document.forms[0].submit();
}

function saveScaleChanges(){
	if(document.forms[0].scale_name.value==""){alert("Please enter a name for this scale.");return;}
	if(!confirm("Save changes?")){return;}
	document.forms[0].which_action.value = "save_scale";
	document.forms[0].submit();
}

function removeScale(){
	if(document.forms[0].child_id.value==""){alert("Please select a scale to remove.");return;}
	if(!confirm("Remove this scale and all child scales and items?")){return;}
	document.forms[0].which_action.value = "remove_scale";
	document.forms[0].submit();
}

function removeItem(){
	if(document.forms[0].item_id.value==""){alert("Please select an item to remove.");return;}
	if(!confirm("Remove this item?")){return;}
	document.forms[0].which_action.value = "remove_item";
	document.forms[0].submit();
}

function removeQuestion(){
	if(document.forms[0].question_id.value==""){alert("Please select an question to remove.");return;}
	if(!confirm("Remove this question?")){return;}
	document.forms[0].which_action.value = "remove_question";
	document.forms[0].submit();
}

//=============================
// CANDIDATE
//=============================

function addRater(){
	var bad=0;
	if(document.forms[0].f_name.value==""){bad=1;}
	if(document.forms[0].l_name.value==""){bad=1;}
	if(document.forms[0].email.value==""){bad=1;}
	if(document.forms[0].relationship_id.value==""){bad=1;}
	if(bad==1){alert("Please complete all fields.");return;}
	if(!confirm("An email will now be sent to this rater asking them to\ntake part in your appraisal.\n\nContinue?")){return;}
	document.forms[0].which_action.value="add_rater";
	document.forms[0].submit();
}
function saveRaterChanges(){
	var bad=0;
	if(document.forms[0].f_name.value==""){bad=1;}
	if(document.forms[0].l_name.value==""){bad=1;}
	if(document.forms[0].email.value==""){bad=1;}
	if(document.forms[0].relationship_id.value==""){bad=1;}
	if(bad==1){alert("Please complete all fields.");return;}
	if(!confirm("Save your changes?")){return;}
	document.forms[0].which_action.value="save_changes";
	document.forms[0].submit();
}
function removeRater(){
	if(!confirm("Remove this rater?")){return;}
	document.forms[0].which_action.value="remove_rater";
	document.forms[0].submit();
}

//=============================
// LIBRARY
//=============================

function showItem(ob){
	if(ob.value){
		bits = ob.value.split("<>");
		document.forms[0].item_id.value=bits[0];
		document.forms[0].item_direction.value=bits[1];
		for(i=0;i<ob.length;i++){
			if(ob.options[i].selected==true){
				document.forms[0].item_text.value = ob.options[i].innerText;
			}
		}
		document.getElementById("add_item").style.display="none";
		document.getElementById("clear_item").style.display="inline";
		document.getElementById("save_item").style.display="inline";
		document.getElementById("remove_item").style.display="inline";
	}
}

function clearItem(ob){
	document.forms[0].items.value="";
	document.forms[0].item_id.value="";
	document.forms[0].item_direction.value="1";
	document.forms[0].item_text.value="";
	document.getElementById("add_item").style.display="inline";
	document.getElementById("clear_item").style.display="none";
	document.getElementById("save_item").style.display="none";
	document.getElementById("remove_item").style.display="none";
}

function addItem(){
	if(document.forms[0].item_text.value==""){alert("Please enter the text for this item.");return;}
	if(!confirm("Add this item?")){return;}
	document.forms[0].which_action.value="add_item";
	document.forms[0].submit();
}

function saveItem(){
	if(document.forms[0].item_text.value==""){alert("Please enter the text for this item.");return;}
	if(!confirm("Save your changes?")){return;}
	document.forms[0].which_action.value="save_item";
	document.forms[0].submit();
}

function removeItem(){
	if(!confirm("Remove this item?")){return;}
	document.forms[0].which_action.value="remove_item";
	document.forms[0].submit();
}

function showQuestion(ob){
	if(ob.value){
		document.forms[0].question_id.value=ob.value;
		for(i=0;i<ob.length;i++){
			if(ob.options[i].selected==true){
				document.forms[0].question_text.value = ob.options[i].innerText;
			}
		}
		document.getElementById("add_question").style.display="none";
		document.getElementById("clear_question").style.display="inline";
		document.getElementById("save_question").style.display="inline";
		document.getElementById("remove_question").style.display="inline";
	}
}

function clearQuestion(ob){
	document.forms[0].questions.value="";
	document.forms[0].question_id.value="";
	document.forms[0].question_text.value="";
	document.getElementById("add_question").style.display="inline";
	document.getElementById("clear_question").style.display="none";
	document.getElementById("save_question").style.display="none";
	document.getElementById("remove_question").style.display="none";
}

function addQuestion(){
	if(document.forms[0].question_text.value==""){alert("Please enter the text for this question.");return;}
	if(!confirm("Add this question?")){return;}
	document.forms[0].which_action.value="add_question";
	document.forms[0].submit();
}

function saveQuestion(){
	if(document.forms[0].question_text.value==""){alert("Please enter the text for this question.");return;}
	if(!confirm("Save your changes?")){return;}
	document.forms[0].which_action.value="save_question";
	document.forms[0].submit();
}

function removeQuestion(){
	if(!confirm("Remove this question?")){return;}
	document.forms[0].which_action.value="remove_question";
	document.forms[0].submit();
}

function addCategory(){
	if(document.forms[0].category_name.value==""){alert("Please enter a name for this category.");return;}
	if(!confirm("Add this category?")){return;}
	document.forms[0].which_action.value="add_category";
	document.forms[0].submit();
}

function saveCategory(){
	if(document.forms[0].category_name.value==""){alert("Please enter a name for this category.");return;}
	if(!confirm("Save your changes?")){return;}
	document.forms[0].which_action.value="save_category";
	document.forms[0].submit();
}

function removeCategory(){
	if(!confirm("Remove this category and all associated items and open questions?")){return;}
	document.forms[0].which_action.value="remove_category";
	document.forms[0].submit();
}

//=============================
// CLIENTS
//=============================


function removeClient(){
	if(!confirm("Remove this client and all associated data?")){return;}
	document.forms[0].which_action.value = "remove_client";
	document.forms[0].submit();
}

function addClient(){
	var bad=0;
	if(document.forms[0].client_name.value==""){bad=1;}
	if(bad==1){alert("Please enter a name for this client.");return;}
	if(!confirm("Create this client?")){return;}
	document.forms[0].which_action.value = "add_client";
	document.forms[0].submit();
}

function saveClientChanges(){

	var bad=0;
	
	if(document.forms[0].client_name.value==""){bad=1;}
	if(document.forms[0].email.value==""){bad=1;}
	if(document.forms[0].user_name.value==""){bad=1;}
	if(document.forms[0].password.value==""){bad=1;}
	if(document.forms[0].main_color.value==""){bad=1;}
	if(document.forms[0].dark_color.value==""){bad=1;}
	if(document.forms[0].button_color.value==""){bad=1;}
	if(document.forms[0].selected_color.value==""){bad=1;}
	if(bad==1){alert("Please complete all fields marked with *.");return;}
	if(!confirm("Save your changes?")){return;}
	document.forms[0].which_action.value="save_changes";
	document.forms[0].submit();
	
}

function saveClientPermissions(){

	
	q = new Array();
	r = new Array();
	
	for(i=0;i<document.forms[0].questionnaires.options.length;i++){
		if(document.forms[0].questionnaires.options[i].selected==true){
			q.push(document.forms[0].questionnaires.options[i].value);
		}
	}
	
	for(i=0;i<document.forms[0].reports.options.length;i++){
		if(document.forms[0].reports.options[i].selected==true){
			r.push(document.forms[0].reports.options[i].value);
		}
	}
	
	document.forms[0].allowed_questionnaires.value = q.join("<>");
	document.forms[0].allowed_reports.value = r.join("<>");
	
	if(!confirm("Save your changes?")){return;}
	document.forms[0].which_action.value="save_changes";
	document.forms[0].submit();
	
}

//=============================
// PROJECTS
//=============================

function addProject(){
	var bad=0;
	if(document.forms[0].client_id){
		if(document.forms[0].client_id.value==""){alert("Please select a client.");return;}
	}
	if(document.forms[0].project_name.value==""){bad=1;}
	if(bad==1){alert("Please enter a name for this project.");return;}
	if(!confirm("Create this project?")){return;}
	document.forms[0].which_action.value = "add_project";
	document.forms[0].submit();
}

function removeProject(){
	if(!confirm("Remove this project and all associated data?")){return;}
	document.forms[0].which_action.value = "remove_project";
	document.forms[0].submit();
}

function duplicateProject(){
	if(!confirm("Duplicate this project?")){return;}
	document.forms[0].which_action.value = "duplicate_project";
	document.forms[0].submit();
}

function saveProjectChanges(){
	bits = new Array();
	bad=0;
	
	if(document.forms[0].project_name.value==""){bad=1;}
	if(document.forms[0].questionnaire_id.value==""){bad=1;}
	if(document.forms[0].project_date_1.value==""){bad=1;}
	if(document.forms[0].project_date_2.value==""){bad=1;}
	if(document.forms[0].project_date_3.value==""){bad=1;}
	if(document.forms[0].closing_date_1.value==""){bad=1;}
	if(document.forms[0].closing_date_2.value==""){bad=1;}
	if(document.forms[0].closing_date_3.value==""){bad=1;}
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Save your changes?")){return;}

	for(i=0;i<document.forms[0].dynamic_fields.options.length;i++){
		bits[i] = document.forms[0].dynamic_fields.options[i].value;
	}
	document.forms[0].df.value=bits.join("<@>");

	document.forms[0].which_action.value = "save_changes";
	document.forms[0].submit();

}

function saveRaterRequirementChanges(){

	var bad=0;

	if(document.forms[0].min_raters.value==""){bad=1;}
	if(document.forms[0].max_raters.value==""){bad=1;}
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Save your changes?")){return;}

	bits = new Array();
	
	for(i=0;i<document.forms[0].raters.options.length;i++){
		bits[i] = document.forms[0].raters.options[i].value;
	}
	document.forms[0].rr.value=bits.join("<@>");

	document.forms[0].which_action.value = "save_changes";
	document.forms[0].submit();

}

function addGroup(){

	var bad=0;
	if(document.forms[0].d_field.value==""){bad=1;}
	if(document.forms[0].d_options.value==""){bad=1;}
	if(bad==1){alert("Please enter a name for this field and then enter\nthe available options.");return;}

	document.forms[0].dynamic_fields.options[document.forms[0].dynamic_fields.options.length] = new Option(document.forms[0].d_field.value,document.forms[0].d_field.value+"@@"+document.forms[0].d_options.value);
	document.forms[0].d_field.value="";
	document.forms[0].d_options.value="";
}
function showGroup(){
	if(document.forms[0].dynamic_fields.value){
		bits = new Array();
		bits = document.forms[0].dynamic_fields.value.split("@@");
		document.forms[0].d_field.value=bits[0];
		document.forms[0].d_options.value=bits[1];
	}
}
function removeGroup(){
	if(document.forms[0].dynamic_fields.value){
		if(confirm("Remove this field?")){
			for(i=0;i<document.forms[0].dynamic_fields.options.length;i++){
				if(document.forms[0].dynamic_fields.options[i].selected==true){
					document.forms[0].dynamic_fields.options[i]=null;
				}
			}
			document.forms[0].d_field.value="";
			document.forms[0].d_options.value="";
		}
	}
}
function clearGroup(){
	document.forms[0].d_field.value="";
	document.forms[0].d_options.value="";
}

function createRelationship(){
	if(document.forms[0].relationship.value==""){alert("Please enter a name for this relationship.");return;}
	if(!confirm("Create this relationship?")){return;}
	document.getElementById("iframe").src="create_relationship.php?relationship="+document.forms[0].relationship.value;
}

function addRelationship(){

	var bad=0;
	if(document.forms[0].relationship_id.value==""){bad=1;}
	if(bad==1){alert("Please select a relationship.");return;}

	bits = new Array();

	for(i=0;i<document.forms[0].raters.options.length;i++){
		bits = document.forms[0].raters.options[i].value.split("<>");
		if(bits[0]==document.forms[0].relationship_id.value){
			alert("You have already selected this rater group.");
			return;
		}
	}


	var selInd = document.forms[0].relationship_id.selectedIndex;
	if((!document.forms[0].relationship_number.value)||(isNaN(document.forms[0].relationship_number.value))){
		document.forms[0].relationship_number.value=0;
	}
	document.forms[0].raters.options[document.forms[0].raters.options.length] = new Option(document.forms[0].relationship_id.options[selInd].text+" ("+document.forms[0].relationship_number.value+")",document.forms[0].relationship_id.value+"<>"+document.forms[0].relationship_number.value);
}

function removeRelationship(){
	if(document.forms[0].raters.value){
		if(confirm("Remove this rater group?")){
			for(i=0;i<document.forms[0].raters.options.length;i++){
				if(document.forms[0].raters.options[i].selected==true){
					document.forms[0].raters.options[i]=null;
				}
			}
		}
	}
}

//===============================
// CANDIDATE
//===============================


function addCandidate(){

	var bad=0;

	if(document.forms[0].f_name.value==""){bad=1;}
	if(document.forms[0].l_name.value==""){bad=1;}
	if(document.forms[0].email.value==""){bad=1;}
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Add this candidate?")){return;}

	document.forms[0].which_action.value = "add_candidate";
	document.forms[0].submit();

}

function saveCandidateChanges(){

	var bad=0;

	if(document.forms[0].f_name.value==""){bad=1;}
	if(document.forms[0].l_name.value==""){bad=1;}
	if(document.forms[0].email.value==""){bad=1;}
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Save your changes?")){return;}

	document.forms[0].which_action.value = "save_changes";
	document.forms[0].submit();

}

function removeCandidate(){
	if(!confirm("Remove this candidate?")){return;}
	document.forms[0].which_action.value="remove_candidate";
	document.forms[0].submit();
}

//=============================
// REPORTS
//=============================


function removeReport(){
	if(!confirm("Remove this report?")){return;}
	document.forms[0].which_action.value = "remove_report";
	document.forms[0].submit();
}

function duplicateReport(){
	if(!confirm("Duplicate this report?")){return;}
	document.forms[0].which_action.value = "duplicate_report";
	document.forms[0].submit();
}

function saveReportChanges(){
	var bad=0;
	if(document.forms[0].report_name.value==""){bad=1;}
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Save changes?")){return;}
	document.forms[0].which_action.value = "save_report";
	document.forms[0].submit();
}

function addReport(){
	var bad=0;
	if(document.forms[0].report_name.value==""){bad=1;}
	if(document.forms[0].questionnaire_id.value==""){bad=1;}
	if(bad==1){alert("Please enter a name for this report and select a questionnaire.");return;}
	if(!confirm("Create this report?")){return;}
	document.forms[0].which_action.value = "add_report";
	document.forms[0].submit();
}

function removeElement(){
	var bad=0;
	if(document.forms[0].element_id.value==""){bad=1;}
	if(bad==1){alert("Please select an element to remove.");return;}
	if(!confirm("Remove this element?")){return;}
	document.forms[0].which_action.value = "remove_element";
	document.forms[0].submit();
}

function addElement(){
	var bad=0;
	if(document.forms[0].element_type.value==""){bad=1;}
	if(bad==1){alert("Please select the type of element you wish to add.");return;}
	if(!confirm("Add this element?")){return;}
	document.forms[0].which_action.value = "add_element";
	document.forms[0].submit();
}

function changeElement(val){
	if(val == ""){showRow();document.getElementById("button").style.display='none';}
	else{document.getElementById("button").style.display='block';}
	if(val == "page_title"){showRow("small_content");}
	if(val == "header"){showRow("small_content");}
	if(val == "rater_details"){showRow();}
	if(val == "free_text"){showRow("large_content");}
	if(val == "box"){showRow("small_content");}
	if(val == "basic"){showRow("scales");}
	if(val == "basic_session"){showRow("scales");}
	if(val == "item"){showRow("items");}
	if(val == "item_session"){showRow("items");}
	if(val == "open_question"){showRow("questions");}
	if(val == "top_bottom"){showRow();}
	if(val == "page_break"){showRow();}
	if(val == "space"){showRow("tiny_content");}
}

function showRow(which){

	document.getElementById("tiny_content").style.display='none';
	document.getElementById("small_content").style.display='none';
	document.getElementById("large_content").style.display='none';
	document.getElementById("scales").style.display='none';
	document.getElementById("items").style.display='none';
	document.getElementById("questions").style.display='none';
	
	if(which){document.getElementById(which).style.display='block';}
}



function saveOrder(){
	bits = new Array();
	if(!confirm("Save your changes to the order in which the objects\nwithin this report are displayed?")){return;}
	for(i=0;i<document.forms[0].element_id.options.length;i++){
		bits.push(document.forms[0].element_id.options[i].value); 
	}
	document.forms[0].element_order.value = bits.join("<~>");
	document.forms[0].submit();
}


function selectCandidateForReport(ob){
	var si =ob.selectedIndex;
	document.forms[0].member_id.value = ob.value;
	document.forms[0].member_name.value = ob.options[si].innerText;
}

function selectCandidateForGroupReport(ob){
	var si =ob.selectedIndex;
	var bad=0;
	for(i=0;i<document.forms[0].member_id.options.length;i++){
		if(document.forms[0].member_id.options[i].value==ob.value){
			bad=1;
		}
	}
	if(bad==0){
		document.forms[0].member_id.options[document.forms[0].member_id.options.length] = new Option(ob.options[si].innerText,ob.value);
	}
}

function selectCandidateForCrossSessionReport(ob,pid,pn){
	var si =ob.selectedIndex;
	var bad=0;
	if(document.forms[0].member_id.options.length>1){return;}
	for(i=0;i<document.forms[0].member_id.options.length;i++){
		if(document.forms[0].member_id.options[i].value==ob.value+"<>"+pid){
			bad=1;
			msg = "You have already selected this person";
		}
	}
	for(i=0;i<document.forms[0].member_id.options.length;i++){
		bits = document.forms[0].member_id.options[i].value.split("<>");
		if(bits[1]==pid){
			bad=1;
			msg = "You must select someone from a different project.";
		}
	}
	if(bad==0){
		document.forms[0].member_id.options[document.forms[0].member_id.options.length] = new Option(ob.options[si].innerText+" - "+pn,ob.value+"<>"+pid);
	}else{
		alert(msg);
	}
}

function makeMemberString(){
	
	var mem = new Array();
	
	for(i=0;i<document.forms[0].member_id.options.length;i++){
		mem.push(document.forms[0].member_id.options[i].value+"<@>"+document.forms[0].member_id.options[i].innerText);
	}
	return mem.join("<@@>");
}

function generateReport(){

	var bad=0;
	if(document.forms[0].member_id.value==""){bad=1;}
	if(document.forms[0].report_id.value==""){bad=1;}
	if(bad==1){alert("Please select a candidate and a report to run.");return;}
	window.open("/management/reports/report.php?report_id="+document.forms[0].report_id.value+"&member_id="+document.forms[0].member_id.value+"&project_id="+document.forms[0].project_id.value);
}

function generateGroupReport(){

	var bad=0;
	var member_id = new Array();
	for(i=0;i<document.forms[0].member_id.options.length;i++){
		member_id.push(document.forms[0].member_id.options[i].value);
	}
	
	if(member_id.length<2){bad=1;}
	if(document.forms[0].report_id.value==""){bad=1;}
	if(document.forms[0].group_name.value==""){bad=1}
	if(bad==1){alert("Please select at least 2 candidates, give your group a name and select a report to run.");return;}
	window.open("/management/reports/group_report.php?report_id="+document.forms[0].report_id.value+"&member_id="+member_id.join("<>")+"&project_id="+document.forms[0].project_id.value+"&group_name="+document.forms[0].group_name.value);
}

function generateCrossSessionReport(){

	var bad=0;
	var member_id = new Array();
	for(i=0;i<document.forms[0].member_id.options.length;i++){
		member_id.push(document.forms[0].member_id.options[i].value);
	}
	
	if(member_id.length<2){bad=1;}
	if(document.forms[0].report_id.value==""){bad=1;}
	if(bad==1){alert("Please select 2 candidates from seperate projects and select a report to run.");return;}
	window.open("/management/reports/cross_session_report.php?report_id="+document.forms[0].report_id.value+"&member_id="+member_id.join("<@>"));
}

//===========================
// Self report projects
//===========================

function saveSelfReportProjectChanges(){
	bits = new Array();
	bad=0;
	
	if(document.forms[0].project_name.value==""){bad=1;}
	if(document.forms[0].questionnaire_id.value==""){bad=1;}
	if(document.forms[0].report_id.value==""){bad=1;}
	if(document.forms[0].project_password.value==""){bad=1;}
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Save your changes?")){return;}

	document.forms[0].which_action.value = "save_changes";
	document.forms[0].submit();

}

function registerSelf(){

	var bad=0;
	
	if(document.forms[0].f_name.value==""){bad=1;}
	if(document.forms[0].l_name.value==""){bad=1;}
	if(document.forms[0].job_title.value==""){bad=1;}
	if(document.forms[0].department.value==""){bad=1;}
	if(document.forms[0].organisation.value==""){bad=1;}
	if(document.forms[0].email.value==""){bad=1;}
	
	if(bad==1){alert("Please enter a value in all fields marked with *.");return;}
	if(!confirm("Register now?")){return;}

	document.forms[0].which_action.value = "register";
	document.forms[0].submit();
}

function createSelfQuestionnaire(){

	if(!confirm("Create a new questionnaire?")){return;}

	document.forms[0].which_action.value = "create_session";
	document.forms[0].submit();
}




