var msglr;
var resource_saver;
var update_timeoutref;
var current_server_date;
var updates_cycle_counter;
var arrt;
var m_arrt;
var updates_cycle_messages;

function ajaxFunction2(type, action, handlerref, eparams){
	m_handlerref = handlerref.split("||~||");
	m_type = type.split("||~||");
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser does not support a feature required to run this website");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if((ajaxRequest.readyState == 4)&&(ajaxRequest.status == 200)){
			arrt = ajaxRequest.responseText;
			if(m_type.length>1){
				m_arrt = arrt.split("||~||");
				for(var i=0; i<m_type.length; i++){
					if(m_type[i]==0) document.getElementById(m_handlerref[i]).innerHTML = m_arrt[i];
					if(m_type[i]==1) eval(m_handlerref[i] + "();");
				}
			} else {
				m_arrt = "normal";
				if(type==0) document.getElementById(handlerref).innerHTML = ajaxRequest.responseText;
				if(type==1) eval(handlerref + "();");
			}
			ajaxRequest = "";
		}
	}
	if((eparams!==undefined)&&(eparams!="")){
		temp_params = eparams.split("&");
		i = 0;
		var params = "";
		while(i < temp_params.length){
			temp_params_i = temp_params[i].split("=");
			if(temp_params_i[0] == "site_id"){
				t_site_id_keep = temp_params_i[1];
			} else if(temp_params_i[0] == "c"){
				c_global = temp_params_i[1];
			} else if(temp_params_i[0] == "sc"){
				sc_global = temp_params_i[1];
			} else if(temp_params_i[0] == "psc"){
				psc_global = temp_params_i[1];
			} else {
				params = params + ((params=="")?(""):("&")) + temp_params_i[0] + "=" + temp_params_i[1];
			}
			i++;
		}
	}
	params = params + ((params=="")?(""):("&")) + "site_id=" + t_site_id_keep + "&c=" + c_global + "&sc=" + sc_global + "&psc=" + psc_global + "&type=" + type + "&a=" + action;
	if(action=="mode") params = params + "&mode=" + document.getElementById("viewSiteMode").value;
	var ajaxAddress = "http://www.hsyacf.org.uk/scripts/ajax10.php"
	ajaxRequest.open("POST", ajaxAddress, true);
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.send(params);
}


function ajaxFunction(site_id, type, action, handlerref, content, subcontent, eparams){
	t_site_id_keep = site_id;
	m_handlerref = handlerref.split("||~||");
	m_type = type.split("||~||");
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser does not support a feature required to run this website");
				return false;
			}
		}
	}
	c_global = content;
	sc_global = subcontent;
	ajaxRequest.onreadystatechange = function(){
		if((ajaxRequest.readyState == 4)&&(ajaxRequest.status == 200)){
			arrt = ajaxRequest.responseText;
			if(m_type.length>1){
				m_arrt = arrt.split("||~||");
				for(var i=0; i<m_type.length; i++){
					if(m_type[i]==0) document.getElementById(m_handlerref[i]).innerHTML = m_arrt[i];
					if(m_type[i]==1) eval(m_handlerref[i] + "();");
				}
			} else {
				m_arrt = "normal";
				if(type==0) document.getElementById(handlerref).innerHTML = ajaxRequest.responseText;
				if(type==1) eval(handlerref + "();");
			}
			ajaxRequest = "";
		}
	}
	var params = "site_id=" + site_id + "&type=" + type + "&a=" + action + "&c=" + c_global + "&sc=" + sc_global;
	if((eparams)&&(eparams!="null")) params = params + eparams;
	if(action=="mode") params = params + "&mode=" + document.getElementById("viewSiteMode").value;
	var ajaxAddress = "http://www.hsyacf.org.uk/scripts/ajax10.php"
	ajaxRequest.open("POST", ajaxAddress, true);
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.send(params);
}

function do_login(atype){
	log_ok_to_cont = 1;
	if(atype=="w"){
		if((document.getElementById("login_Username").value=="")||(document.getElementById("login_Password_w").value=="")){
			alert("Please enter your username and password");
			log_ok_to_cont = 0;
		}
	} else {
		if((document.getElementById("login_Password_s").value=="")||(document.getElementById("login_Unit").value=="0")){
			alert("Please select your unit and enter the password");
			log_ok_to_cont = 0;
		}
	}
	if(log_ok_to_cont==1){
		document.getElementById("usersZoneWaitDiv").style.display = "block";
		eparams = "username=";
		if(atype=="s") eparams = eparams + document.getElementById("login_Unit").value;
		if(atype=="w") eparams = eparams + document.getElementById("login_Username").value;
		eparams = eparams + "&password=";
		if(atype=="s") eparams = eparams + document.getElementById("login_Password_s").value;
		if(atype=="w") eparams = eparams + document.getElementById("login_Password_w").value;
		eparams = eparams + "&acc_type=";
		if(atype=="w") eparams = eparams + "WEB";
		if(atype=="s") eparams = eparams + document.getElementById("login_Acc_Type").value;
		ajaxFunction2('1', 'login', 'do_login_2', eparams);
	}
}

function do_login_2(){
	if(arrt=="N"){
		document.getElementById("usersZoneWaitDiv").style.display = "none";
		alert("Invalid Username and/or Password");
		if(document.getElementById("usersZoneUserNameTextBox")) document.getElementById("usersZoneUserNameTextBox").focus();
	} else {
		window.location.replace(arrt);
	}
}

function logout(){
	current_server_date = "";
	window.location.replace(arrt);
}

function mode(){
	window.location.replace(arrt);
}

function notices_function(n_action, n_id){
	if(n_action=="add"){
		if((document.getElementById("notices_add_title").value=="")||(document.getElementById("notices_add_notice").vaue=="")||(document.getElementById("notices_add_expires").value=="")){
			alert("Please Complete All Required Information First");
		} else {
			n_date = document.getElementById("notices_add_expires").value;
			currentDate = current_server_date;
			n_date_i = n_date.split('/');
			n_date_j = (n_date_i[2] * 10000) + (n_date_i[1] * 100) + (n_date_i[0] * 1);
			if(n_date_j < currentDate){
				alert("Your Expiry Date Cannot Be In The Past");
			} else {
				eparams='pi1=add&pi2=0&pi3=' + encodeURIComponent(document.getElementById("notices_add_title").value) + '&pi4=' + document.getElementById("notices_add_type").value + '&pi5=' + encodeURIComponent(document.getElementById("notices_add_notice").value) + '&pi6=' + encodeURIComponent(document.getElementById("notices_add_expires").value) + '&pi7=';
				if(document.getElementById("notices_add_site")){
					eparams+=encodeURIComponent(document.getElementById("notices_add_site").value);
					tempval = "ok";
				} else {
					tempval = "";
					for(var i=0; i<document.getElementById('notices_add_sites').options.length; i++){
						if(document.getElementById('notices_add_sites').options[i].selected==true){
							if(tempval!="") tempval+=",";
							tempval+=document.getElementById('notices_add_sites').options[i].value;
						}
					}
					eparams+=encodeURIComponent(tempval);
				}
				if(tempval==""){
					alert("Please select one or more sites to display the notice on");
				} else {
					ajaxFunction2('0', '/home/hsyacf/public_html/global10/notices.php', 'notices_container_div', eparams);
				}
			}
		}
	}
	if(n_action=="edit"){
		if((document.getElementById("notices_notice_title_" + n_id).value=="")||(document.getElementById("notices_notice_notice_" + n_id).value=="")||(document.getElementById("notices_notice_expires_" + n_id).value=="")){
			alert("Please Complete All Required Information First");
		} else {
			n_date = document.getElementById("notices_notice_expires_" + n_id).value;
			currentDate = current_server_date;
			n_date_i = n_date.split('/');
			n_date_j = (n_date_i[2] * 10000) + (n_date_i[1] * 100) + (n_date_i[0] * 1);
			if(n_date_j < currentDate){
				alert("Your Expiry Date Cannot Be In The Past");
			} else {
				eparams = 'pi1=edit&pi2=' + n_id + '&pi3=' + encodeURIComponent(document.getElementById("notices_notice_title_" + n_id).value) + '&pi4=' + document.getElementById("notices_notice_type_" + n_id).value + '&pi5=' + encodeURIComponent(document.getElementById("notices_notice_notice_" + n_id).value) + '&pi6=' + encodeURIComponent(document.getElementById("notices_notice_expires_" + n_id).value) + '&pi7=';
				if(document.getElementById("notices_notice_site_" + n_id)){
					eparams+=encodeURIComponent(document.getElementById("notices_notice_site_" + n_id).value);
					tempval = "ok";
				} else {
					tempval = "";
					for(var i=0; i<document.getElementById('notices_notice_sites_' + n_id).options.length; i++){
						if(document.getElementById('notices_notice_sites_' + n_id).options[i].selected==true){
							if(tempval!="") tempval+=",";
							tempval+=document.getElementById('notices_notice_sites_' + n_id).options[i].value;
						}
					}
					eparams+=encodeURIComponent(tempval);
				}
				if(tempval==""){
					alert("Please select one or more sites to display the notice on");
				} else {
					ajaxFunction2('0', '/home/hsyacf/public_html/global10/notices.php', 'notices_container_div', eparams);
				}
			}
		}
	}
}

function events_function(n_action, n_id){
	if(n_action=="add"){
		if((document.getElementById("events_add_title").value=="")||(document.getElementById("events_add_sdate").value=="")){
			alert("You must enter at least the Title and Start date");
		} else {
			n_sdate = document.getElementById('events_add_sdate').value;
			n_edate = document.getElementById('events_add_edate').value;
			if(n_edate=="") n_edate = n_sdate;
			currentDate = current_server_date;
			n_sdate_i = n_sdate.split('/');
			n_sdate_j = (n_sdate_i[2] * 10000) + (n_sdate_i[1] * 100) + (n_sdate_i[0] * 1);
			if(n_sdate_j < currentDate){
				alert("Your Start Date Cannot Be In The Past");
			} else {
				n_edate_i = n_edate.split('/');
				n_edate_j = (n_edate_i[2] * 10000) + (n_edate_i[1] * 100) + (n_edate_i[0] * 1);
				if(n_edate_j < n_sdate_j){
					alert("Your End Date Cannot Be Prior To Your Start Date");
				} else {
					eparams = 'pi1=add&pi2=0&pi3=' + encodeURIComponent(document.getElementById("events_add_title").value) + '&pi4=' + encodeURIComponent(document.getElementById("events_add_sdate").value) + '&pi5=' + encodeURIComponent(document.getElementById("events_add_edate").value) + '&pi6=' + encodeURIComponent(document.getElementById("events_add_location").value) + '&pi7=' + encodeURIComponent(document.getElementById("events_add_cost").value);
					if(document.getElementById("events_add_d_type")){
						eparams = eparams + '&pi8=' + document.getElementById("events_add_d_type").value + '&pi10=' + document.getElementById("events_add_m_type").value + '&pi11=' + encodeURIComponent(document.getElementById("events_add_more").value) + '&pi12=' + document.getElementById("events_add_type").value;
					} else {
						eparams = eparams + '&pi8=0';
						eparams = eparams + '&pi10=0&pi11=&pi12=0';
					}
					eparams = eparams + '&pi9=' + encodeURIComponent(document.getElementById("events_add_details").value);
					ajaxFunction2('0', '/home/hsyacf/public_html/global10/events.php', 'events_container_div', eparams);
				}
			}
		}
	}
	if(n_action=="update"){
		if((document.getElementById("events_edit_title_" + n_id).value=="")||(document.getElementById("events_edit_sdate_" + n_id).value=="")){
			alert("You must enter at least the Title and Start date");
		} else {
			n_sdate = document.getElementById('events_edit_sdate_' + n_id).value;
			n_edate = document.getElementById('events_edit_edate_' + n_id).value;
			if(n_edate=="") n_edate = n_sdate;
			currentDate = current_server_date;
			n_sdate_i = n_sdate.split('/');
			n_sdate_j = (n_sdate_i[2] * 10000) + (n_sdate_i[1] * 100) + (n_sdate_i[0] * 1);
			if(n_sdate_j < currentDate){
				alert("Your Start Date Cannot Be In The Past");
			} else {
				n_edate_i = n_edate.split('/');
				n_edate_j = (n_edate_i[2] * 10000) + (n_edate_i[1] * 100) + (n_edate_i[0] * 1);
				if(n_edate_j < n_sdate_j){
					alert("Your End Date Cannot Be Prior To Your Start Date");
				} else {
					eparams = 'pi1=update&pi2=' + n_id + '&pi3=' + encodeURIComponent(document.getElementById("events_edit_title_" + n_id).value) + '&pi4=' + encodeURIComponent(document.getElementById("events_edit_sdate_" + n_id).value) + '&pi5=' + encodeURIComponent(document.getElementById("events_edit_edate_" + n_id).value) + '&pi6=' + encodeURIComponent(document.getElementById("events_edit_location_" + n_id).value) + '&pi7=' + encodeURIComponent(document.getElementById("events_edit_cost_" + n_id).value);
					if(document.getElementById("events_edit_d_type_" + n_id)){
						eparams = eparams + '&pi8=' + document.getElementById("events_edit_d_type_" + n_id).value + '&pi10=' + document.getElementById("events_edit_m_type_" + n_id).value + '&pi11=' + encodeURIComponent(document.getElementById("events_edit_more_" + n_id).value) + '&pi12=' + document.getElementById("events_edit_type_" + n_id).value;
					} else {
						eparams = eparams + '&pi8=0';
						eparams = eparams + '&pi10=0&pi11=&pi12=0';
					}
					eparams = eparams + '&pi9=' + encodeURIComponent(document.getElementById("events_edit_details_" + n_id).value);
					ajaxFunction2('0', '/home/hsyacf/public_html/global10/events.php', 'events_container_div', eparams);
				}
			}
		}
	}
}

function foedodiv(id, onoroff){
	if(onoroff==1){
		document.getElementById("foediv_" + id).style.visibility="visible";
	} else {
		document.getElementById("foediv_" + id).style.visibility="hidden";
	}
}

function page_edit_handler(){
	arrt_sp = arrt.split("||||||||||");
	if(arrt_sp[0]=="delete"){
		tinyMCE.get('page_editor_code').setContent(arrt_sp[1]);
		document.getElementById('page_editor_revert_button').parentNode.removeChild(document.getElementById('page_editor_revert_button'));
		alert('Your page has been reverted to the previous approved state');
	}
	if(arrt_sp[0]=="save"){
		alert("Your page has been saved\n\nYou can request approval by viewing the page in Preview Mode");
		if(!document.getElementById('page_editor_revert_button')){
			new_el = document.createElement('input');
			new_el.setAttribute('id', 'page_editor_revert_button');
			new_el.setAttribute('type', 'button');
			new_el.setAttribute('value', 'Revert to Last Saved');
			new_el.setAttribute('onclick', 'if(confirm(\'Are you sure you want to revert to the last saved version?\')) ajaxFunction2(\'1\', \'/home/hsyacf/public_html/global10/page_edit_ajax.php\', \'page_edit_handler\', \'w=delete\');');
			document.getElementById('page_editor_div_buttons').insertBefore(new_el, document.getElementById('page_editor_save_button'));
		}
	}
	if(arrt_sp[0]=="rapprove"){
		alert("Your approval request has been sent");
		document.getElementById('list_of_approvers').innerHTML = "Approval Requested From " + arrt_sp[1];
	}
	if(arrt_sp[0]=="gapprove"){
		alert("The page has been approved");
		document.getElementById('approval_display_div').style.display='none';
	}
}
