// JavaScript Document

	var Page = 'display';

function getRule(ruleName) {
	for (var i=0; i < document.styleSheets.length; i++) {
		if( document.styleSheets[i].title == "CSS" ) {
			var stylesheet = document.styleSheets[i];
		}
	}
	if(stylesheet.cssRules) {
		var theRules = stylesheet.cssRules;
	} else {
		var theRules = stylesheet.rules;
	}
	var ii = 1;
	for (var ii=0; ii < theRules.length; ii++) {
		if(theRules[ii].selectorText == ruleName) {
			return theRules[ii];
		}
	}
}

function hide(id) {
	document.getElementById(id).style.display="none"; //hides an element
}

function show(id) {
	document.getElementById(id).style.display="block"; //shows an element
}

function toggle(id, which) { // Used to toggle between visible and non
	var obj = document.getElementById(id);
	if ( which == "show") {
		obj.style.display = "block";
	} else if (which == "hide") {
		obj.style.display = "none";
	} else {
		if ( obj.style.display != "none" ) {
			obj.style.display = "none";
		}
		else {
			obj.style.display = "block";
		}
	}
}

function PassEntered() { //Once this function is called,
							var pass = document.getElementById('Password').value; //get the password that the user submitted
							if( pass == "gvchoirs" ) { //if the password submitted is equal to "" then
								document.getElementById('ShowStatus').style.display='block'; //change the ShowStatus element to display: block, making it visible
								Page = "edit"; //set Page = "edit"
								var editable = getRule(".editable"); //set the variable editable equal to the selector ".editable" in the stylesheet named "CSS"
								editable.style.background = "#DDFFFF"; //changes .editable's background color
								editable.style.border = "solid 1px blue"; //changes .editable's border
								var hidden = getRule(".hidden"); //set the variable hidden equal to the selector ".hidden" in the stylesheet named "CSS"
								hidden.style.display = "block";
								toggle('PasswordDiv','hide');
								document.getElementById('Password').value = '';
							}
						}

shortcut.add("Ctrl+Shift+e",
				 function() {
					if(Page == "display") {
						document.getElementById('PasswordDiv').style.display='block';
						document.getElementById('Password').focus();
						
					} else if (Page == 'edit') {
						document.getElementById('ShowStatus').style.display="none";
						Page = 'display';
						var editable = getRule(".editable");
						editable.style.background = "";
						var hidden = getRule(".hidden");
						hidden.style.display = "hidden";
						pass = null;
					}
						
				}
			);



function edit(id, page, command, template) {
	if(Page == "edit") {
		toggle('ChangeTextForm','show'); //displays the ChangeTextForm
		document.getElementById('ChangeTextFormID').value=id;
		document.getElementById('command').value=command;
		
		if(template) {
			var text_to_edit = template;
		} else {
			var text_to_edit = document.getElementById(id).innerHTML;
		}
		/*old version: 
		var oEditor = FCKeditorAPI.GetInstance('UpdatedText');
		oEditor.SetData(text_to_edit);
		*/
		CKEDITOR.instances.UpdateText.setData(text_to_edit); 
	}
}

function ajaxRequestFunction() {
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		return ajaxRequest;
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			return ajaxRequest;
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				return ajaxRequest;
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
}

function updatetext() {
	// var oEditor = FCKeditorAPI.GetInstance('UpdateText'); // sets the variable oEditor equal to the FCKeditor instance (aka the rich-text text box)
	
	var page = document.getElementById('page').value; // stores the value of page from ChangeTextForm.php in the variable page
	var command = document.getElementById('command').value; // stores the value of command from ChangeTextForm.php in the variable command
	var id = document.getElementById('ChangeTextFormID').value; // stores the value of CahngeTextFormID from ChangeTextForm.php in the variable id
	var changedtext = CKEDITOR.instances.UpdateText.getData();
	
	//encodeURIComponent(oEditor.GetData()); // stores the changed text from the FCKeditor in the variable changedtext
	
	var ajaxRequest = ajaxRequestFunction();
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var newText = ajaxRequest.responseText; //sets the variable newText equal to the response from UpdateText.php
			
			CKEDITOR.instances.UpdateText.setData(newText); //Changes the text in the FCKeditor to match that displayed on the page now.
			document.getElementById(id).innerHTML = newText; //sets the text of the Edited area equal to the changed text.
			if ( document.getElementById(page + 'List') ) { //if the page includes a list
				document.getElementById(page + 'List').appendChild() //then add a new list to the end.
			}
		}
	}
	var url = "/Parts/UpdateText.php";
	var params = "page=" + encodeURIComponent(page);
	params += "&command=" + encodeURIComponent(command); //'insert' or 'update'
	params += "&id=" + encodeURIComponent(id);
	params += "&changedtext=" + encodeURIComponent(changedtext);
	
	ajaxRequest.open("POST", url, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(params);
	
	
}