
function disableCheckBoxes(blnOff)
{
	var ttForm = document.getElementById('frm_dept_tt');
	
	//cycle through checkbox array, enable all
	for (var i=3;i<ttForm.elements['actTypes[]'].length;i++) {
			ttForm.elements['actTypes[]'][i].disabled = blnOff;
			if (blnOff){
				ttForm.elements['actTypes[]'][i].checked = !blnOff;
				eraseCookie(ttForm.elements['actTypes[]'][i].value,'input-');
			}
		}

	
}

/*
sets a client side cookie with information passed through
default expiry is 30 days unless stated.
*/
function setCookie(name,value,days2expire, path, domain, secure) {

	if (!days2expire)
		days2expire = 3;
		
	var date = new Date();
	date.setTime(date.getTime()+(days2expire*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	
	document.cookie = name+"="+escape(value)+expires+	
		((path) ? ";path=" + path: "path=/") + 
		((domain) ? ";domain=" + domain: "") + 
		((secure) ? ";secure": "");
}

function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1)
        return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1)
        end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

function eraseCookie(name,prefix) {
	name = prefix+name;
	setCookie(name,"",-1);
}
function erasePageCookies(form_id, prefix) {
    var form = document.getElementById(form_id);
	    for (var i=0;i<form.elements.length;i++) {
			eraseCookie(form.elements[i].value,'input-');
			eraseCookie(form.elements[i].name,'input-');
		}		
}


function IEWorkAround()
{
//not really needed anymore, if someone wants to remove and test, fine by me
	document.getElementById('frm_dept_tt').department.options.length = 0;
	document.getElementById('frm_dept_tt').department.options[0] = new Option(" - Select Campus From List Above First - ",0,true,true);
}


function timetableFormReset(){
//resets form to blank
//also sets the page on startup with cookie values
	var ttForm = document.getElementById('frm_dept_tt');

	rememberFormInputs('frm_dept_tt', 'input-');
	
	disableCheckBoxes(!ttForm.elements['actTypes[]'][2].checked);
}

function CheckActivitiesSelection(){
//if no checkboxes are clicked, then return false
//used for form validation, and startup with cookies.
	var ttForm = document.getElementById('frm_dept_tt');
	var blnChecked = false;
	
	for (var i=0;i<ttForm.elements['actTypes[]'].length;i++) {
		if (ttForm.elements['actTypes[]'][i].checked == true){
			if (ttForm.elements['actTypes[]'][i].value !="choose")
				return true;
		}
	}
	return false;
}

function rememberFormInputs(form_id, prefix) {
    var strCookieName;
    var strCookieVal;
    var iFormsCount = 0;
    var iElementsCount = 0;
    var form = document.getElementById(form_id);
    var elsInput = document.getElementsByTagName('input');
    var elsSelect = document.getElementsByTagName('select');

//cycles through all input elements
    for (var i = 0; i < elsInput.length; i++) {
		// this is the input field element with index of i
	    var el = elsInput.item(i);
		// makes sure this is a text input field
		if (el.type == 'text') {
			// sets event handler to catch onblur events
			// it sets the cookie values each time you move out of an input field
			el.onblur = function() {
				// this is the name of the input field
				var name = this.name;
				// this is the value of the input field
				var value = this.value;
				// set the cookie
				setCookie( prefix + name, value);
			};
			// this inserts all the remembered cookie values into the input fields

			var old_value = getCookie(prefix + el.name);
	
			if (old_value && old_value != '') {
				el.value = old_value;
			}
				
		}
		else if (el.type == 'radio'){
			//remember radio buttons (different function - onclick)
			el.onclick = function() {
				if (this.name=="actTypes[]"){
					disableCheckBoxes(this.value!="choose")
				}
				var name = this.name;
				var value = this.value;
				// set the cookie
				setCookie( prefix + name, value);
			};
			
			var old_value = getCookie(prefix + el.name);
			// this sets all the remembered cookie values for radio buttons 
			if (old_value && old_value != '') {
				if(el.value == old_value){
					el.checked = true
				}
			}
		}
		else if (el.type == 'checkbox'){
			//remember checkboxes (different function - onclick)
			el.onclick = function() {
				var name = this.value;
				var value = this.checked;	
				// sets the cookie for checkbox values
				setCookie( prefix + name, value);		
			}; 

			var old_checked = getCookie(prefix + el.value);
			if (old_checked && old_checked == 'true') {
				//sets the cookie ONLY if the checkbox is checked 
				el.checked = old_checked; 
			}
			else if (old_checked && old_checked == 'false'){
				//otherwise the checkbox has been unchecked, remove the cookie
				eraseCookie(el.value,'input-');
			}
		}
		else{
			//this fields are not remembered
			//elvalue: hidden
			//elvalue: submit
			//elvalue: button
			//elvalue: submit
			//elvalue: hidden
		}
		
		
	}
//cycles through all select box elements	
	for (var i = 0; i < elsSelect.length; i++) {
		var elSel = elsSelect.item(i);
		if (elSel.type == 'select-one'){
		//sets javascript to catch onchange function
			elSel.onchange = function(){
				var nameSel=this.name;
				var valSel=this.options[this.selectedIndex].value;
				//remembers selected value by name in the cookie
				setCookie( prefix + nameSel, valSel);
				if (this.name == 'campus'){
				//if it was the campus selectbox, build the department selectbox
					ValidateCampusSelection();
				}

			};
			var old_index = getCookie(prefix + elSel.name);
	
			if (old_index && old_index != '') {
				//if a cookie was remembered
				for(var j=0; j < elSel.length; j++){
					//cycle through options to find name of cookie var
					if (elSel.options[j].value == old_index){
						//once found set it
						elSel.options[j].selected = true;
						if (elSel.name == 'campus'){
							//if it was the campus selectbox, build the department selectbox
							ValidateCampusSelection();
						}
					}
				}
			}
		}
		
	}
	
}
