function download_file(path) {
	window.location = site_base_href + 'download/?f=' + path;
}

function open_page(path) {
	var new_page_win = window.open(path,'new_page','width=600,height=400,top=0,left=0,resizable,scrollbars');
	new_page_win.focus();
}

function send_email() {
	if (document.contactForm.email_from.value=='' || document.contactForm.email_message.value=='') {
		alert('Please fill in both a message and your email address.');
	} else {
		document.contactForm.submit();
	}
}


// -------------------- ALERT WINDOW
var alert_win_obj = null;
function alert_win(msg) {
	var alert_button = '<div class="form_links"><ul class="buttons"><li class="first"><a href="javascript:close_alert();"><span>Ok</span></a></li></ul></div>';
	alert_win_obj = new Window('alert_win',{className:"alphacube",width:400,height:250,minimizable: false,maximizable: false,closable: false,resizable: false,draggable: false,showEffectOptions: {duration:1}});
	alert_win_obj.getContent().innerHTML = msg + alert_button;
	alert_win_obj.showCenter(true,0,0);
	alert_win_obj.setDestroyOnClose();
}

function close_alert() {
	alert_win_obj.hide();
}


// -------------------- COMMENTS
function show_comments(element_id) {
	Effect.BlindDown(element_id);
	$(element_id + '_link').innerHTML = '<a href="javascript:hide_comments(\'' + element_id + '\');">hide comments</a>';
}
function hide_comments(element_id) {
	Effect.BlindUp(element_id);
	$(element_id + '_link').innerHTML = '<a href="javascript:show_comments(\'' + element_id + '\');">show comments</a>';
}

var comment_win_obj = '';
function post_comment(item_id,category,element_id) {
	var ajax_params = 'cmd=form&item_id=' + item_id + '&category=' + category + '&element_id=' + element_id;
	var ajax_url = site_ajax_dir + 'post_comment.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			comment_win_obj = new Window('comments_win',{className:"alphacube",width:400,height:250,minimizable: false,maximizable: false,closable: false,resizable: false,draggable: false,showEffectOptions: {duration:1}});
			comment_win_obj.getContent().innerHTML = ajax_resp.responseText;
			comment_win_obj.showCenter(true,0,0);
			comment_win_obj.setDestroyOnClose();
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}

function cancel_comment() {
	comment_win_obj.hide();
}

function submit_comment() {
	var comment_form_holder = comment_win_obj.getContent().innerHTML;
	var form_id = 'post_comment_form';
	var errors = '';
	var comment_copy = $('comment_copy').value;
	var comment_item_id = $('comment_item_id').value;
	var comment_category = $('comment_category').value;
	var comment_element_id = $('comment_element_id').value;
	
	// error check form
	if (comment_copy == '') { errors = 'Please enter some copy.'; }
	
	if (errors.length === 0) {
		var ajax_params = 'cmd=post&item_id=' + comment_item_id + '&category=' + comment_category + '&comment_copy=' + comment_copy;
		var ajax_url = site_ajax_dir + 'post_comment.php';
		
		var ajax_opts = {method: 'post',
			postBody: ajax_params,
			onSuccess: function(ajax_resp) {
				if (comment_element_id != '') { update_comments(comment_item_id,comment_category,comment_element_id); }
				comment_win_obj.hide();
			}
		}
		
		new Ajax.Request(ajax_url,ajax_opts);
	} else {
		new Effect.Shake(Windows.focusedWindow.getId());
		alert(errors);
	}
}

function update_comments(comment_item_id,comment_category,comment_element_id) {
	var ajax_params = 'category=' + comment_category + '&item_id=' + comment_item_id + '&element_id=' + comment_element_id;
	var ajax_url = site_ajax_dir + 'update_comments.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			$(comment_element_id).innerHTML = ajax_resp.responseText;
			show_comments(comment_element_id);
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}


// -------------------- LOGIN/REGISTER
var login_win_obj = null;
function site_login() {
	var ajax_params = 'cmd=form';
	var ajax_url = site_ajax_dir + 'site_login.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			login_win_obj = new Window('login_win',{className:"alphacube",width:400,height:250,minimizable: false,maximizable: false,closable: false,resizable: false,draggable: false,showEffectOptions: {duration:1}});
			login_win_obj.getContent().innerHTML = ajax_resp.responseText;
			login_win_obj.showCenter(true,0,0);
			login_win_obj.setDestroyOnClose();
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}

function submit_login() {
	var ajax_params = 'cmd=submit&password=' + $('login_password').value + '&username=' + $('login_username').value;
	var ajax_url = site_ajax_dir + 'site_login.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			if (ajax_resp.responseText != 'failed') {
				window.location.replace(ajax_resp.responseText);
			} else {
				new Effect.Shake(Windows.focusedWindow.getId());
				alert('Login failed!' + "\n\n" + 'Please try again or register.');
			}
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}

function site_logout() {
	var ajax_params = 'cmd=confirm';
	var ajax_url = site_ajax_dir + 'site_logout.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			login_win_obj = new Window('logout_win',{className:"alphacube",width:400,height:250,minimizable: false,maximizable: false,closable: false,resizable: false,draggable: false,showEffectOptions: {duration:1}});
			login_win_obj.getContent().innerHTML = ajax_resp.responseText;
			login_win_obj.showCenter(true,0,0);
			login_win_obj.setDestroyOnClose();
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}

function submit_logout() {
	var ajax_params = 'cmd=logout';
	var ajax_url = site_ajax_dir + 'site_logout.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			window.location.replace(ajax_resp.responseText);
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}

function cancel_login() {
	login_win_obj.hide();
}


function site_register() {
	var ajax_params = 'cmd=form';
	var ajax_url = site_ajax_dir + 'site_register.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			if (login_win_obj == null) {
				login_win_obj = new Window('login_win',{className:"alphacube",width:400,height:250,minimizable: false,maximizable: false,closable: false,resizable: false,draggable: false,showEffectOptions: {duration:1}});
				login_win_obj.showCenter(true,0,0);
				login_win_obj.setDestroyOnClose();
			}
			login_win_obj.getContent().innerHTML = ajax_resp.responseText;
			
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}

function submit_register() {
	var first_name = $('register_first_name').value;
	var last_name = $('register_last_name').value;
	var email = $('register_email').value;
	
	if (first_name != '' && last_name != '' && email != '') {
		var ajax_params = 'cmd=submit' + '&first_name=' + first_name + '&last_name=' + last_name + '&email=' + email;
		var ajax_url = site_ajax_dir + 'site_register.php';
		
		var ajax_opts = {method: 'post',
			postBody: ajax_params,
			onSuccess: function(ajax_resp) {
				if (ajax_resp.responseText != 'failed') {
					login_win_obj.getContent().innerHTML = ajax_resp.responseText;
				} else {
					alert('There was a problem with the registration process.' + "\n" + 'You might already be registered.');
					new Effect.Shake(Windows.focusedWindow.getId());
				}
			}
		}
		
		new Ajax.Request(ajax_url,ajax_opts);
	} else {
		alert('Please enter all information.');
		new Effect.Shake(Windows.focusedWindow.getId());
	}
	
	
}

// -------------------- POST RESOURCE
var resource_win_obj = '';
function post_resource() {
	var ajax_params = 'cmd=form';
	var ajax_url = site_ajax_dir + 'post_resource.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			resource_win_obj = new Window('resource_win',{className:"alphacube",width:500,height:340,minimizable: false,maximizable: false,closable: false,resizable: false,draggable: false,showEffectOptions: {duration:1}});
			resource_win_obj.getContent().innerHTML = ajax_resp.responseText;
			resource_win_obj.showCenter(true,0,0);
			resource_win_obj.setDestroyOnClose();
		}
	}
	
	new Ajax.Request(ajax_url,ajax_opts);
}

function cancel_resource() {
	resource_win_obj.hide();
}

function submit_resource() {
	var errors = new Array();
	var error_msg = 'Please fix the following errors:'+"\n";
	if ($('title').value == '') { errors.push('Please enter a title!'); }
	if ($('description').value == '') { errors.push('Please enter a description!'); }
	if ($('filename').value == '') { errors.push('Please uplaod a file!'); }
	
	
	var num_errors = errors.length;
	if (num_errors > 0) {
		new Effect.Shake(Windows.focusedWindow.getId());
		for (var i=0;i<num_errors;i++) {
			error_msg += errors[i] + "\n";
		}
		alert(error_msg);
	} else {
		$('resource_form').submit();
	}
}



// -------------------- ACCOUNT INFO
function edit_account(el_id) {
	var ajax_params = 'cmd=form';
	var ajax_url = site_ajax_dir + 'account_info.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			var new_content = ajax_resp.responseText;
			new Effect.BlindUp($(el_id),{afterFinish: function() {$(el_id).innerHTML=new_content;new Effect.BlindDown($(el_id));}});
			
		}
	}
	new Ajax.Request(ajax_url,ajax_opts);

}










// ------------------ CMS

// attachments
var attach_win_obj = null;
function cancel_attach() {
	attach_win_obj.hide();
}

function attach(type) { //type = images OR filebase	
	var ajax_params = 'type=' + type + '&cubeit_id=' + admin_cubeit_id + '&cmd_id=' + admin_cmd_id;
	var ajax_url = site_ajax_dir + 'add_attach.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			attach_win_obj = new Window('attach_win',{className:"alphacube",width:500,height:340,minimizable: false,maximizable: false,closable: false,resizable: false,draggable: false,showEffectOptions: {duration:1}});
			attach_win_obj.getContent().innerHTML = ajax_resp.responseText;
			attach_win_obj.showCenter(true,0,0);
			attach_win_obj.setDestroyOnClose();
		}
	}
	new Ajax.Request(ajax_url,ajax_opts);
}

function show_attach_items (cat_id,type) {
	var ajax_params = 'type=' + type + '&cat_id=' + cat_id;
	var ajax_url = site_ajax_dir + 'get_attach_items.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			$('attach_items').innerHTML = ajax_resp.responseText;
		}
	}
	new Ajax.Request(ajax_url,ajax_opts);
}

function attach_item(child_id,child_table) {
	var ajax_params = 'child_id=' + child_id + '&child_table=' + child_table + '&parent_id=' + admin_cmd_id + '&cubeit_id=' + admin_cubeit_id;
	var ajax_url = site_ajax_dir + 'update_attach_items.php';
	
	var ajax_opts = {method: 'post',
		postBody: ajax_params,
		onSuccess: function(ajax_resp) {
			$('attached_' + child_table).innerHTML = ajax_resp.responseText;
			cancel_attach();
		}
	}
	new Ajax.Request(ajax_url,ajax_opts);
}

function detach(type) { //type = images OR filebase
	var params = '';
	var form_obj = $('attached_' + type + '_form');
	for (var i=0;i<form_obj.length;i++) {
		if (form_obj.elements[i].type == 'checkbox' && form_obj.elements[i].name.indexOf('detach') != -1 && form_obj.elements[i].checked == true) {
			params += '-' + form_obj.elements[i].value;
		}
	}

	if (params != '') {
		var ajax_params = 'type=' + type + '&cubeit_id=' + admin_cubeit_id + '&cmd_id=' + admin_cmd_id + '&detach_ids=' + params.substr(1);
		var ajax_url = site_ajax_dir + 'detach.php';

		var ajax_opts = {method: 'post',
			postBody: ajax_params,
			onSuccess: function(ajax_resp) {
				if (ajax_resp.responseText != '' && ajax_resp.responseText != ' ') {
					$('attached_' + type).innerHTML = ajax_resp.responseText;
				}
			}
		}
		new Ajax.Request(ajax_url,ajax_opts);
	} else {
		alert('Nothing is checked!');
	}

}




function update_attach(type) { //type = images OR filebase
	var params = '';
	var errors = false;
	var priority_params = '';
	var priority,priority_id,tmp;
	var form_obj = document.getElementById('attached_' + type + '_form');
	
	for (var i=0;i<form_obj.length;i++) {
		if (form_obj.elements[i].type == 'text' && form_obj.elements[i].name.indexOf('priority') != -1) {
			if (form_obj.elements[i].value != '' && isNaN(form_obj.elements[i].value) == false) {
				tmp = form_obj.elements[i].name.split('_');
				priority_id = tmp[(tmp.length - 1)];
				priority = form_obj.elements[i].value;
				priority_params += '-' + priority_id + '|' + priority;
			} else {
				errors = true;
			}
		}
	}
	if (priority_params != '' && errors == false) {
		var ajax = new Ajax();
		ajax.send_params = 'type=' + type + '&cubeit_id=' + admin_cubeit_id + '&cmd_id=' + admin_cmd_id + '&priorities=' + priority_params.substr(1);
		ajax.send_url = site_ajax_dir + 'update_attach_priority.php';
		ajax.send_method = 'POST';
		ajax.response_handler = function(resp) {
			if (resp != '' || resp != ' ') {
				document.getElementById('attached_' + type).innerHTML = resp;
			}
		}
		ajax.sendRequest();
	} else {
		alert('There is a problem with one of your priorities. Please check and re-update.');
	}
}


// emails
function preview_email(id) {
	var file_path = site_base_href + 'email/?emailing_id=' + id;
	open_page(file_path);
}



// forms
function contact_us() {
	var errors = new Array();
	var error_msg = '';
	var email_format = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if ($('first_name').value == '') { errors.push('Please enter your first name.'); }
	if ($('last_name').value == '') { errors.push('Please enter your last name.'); }
	if ($('message').value == '') { errors.push('Please enter a message.'); }
	if (!email_format.test($('email').value)) {
		errors.push('Email address is formatted wrong.');
	} else if ($('email').value != $('email_2').value ) { 
		errors.push('Emails do not match.'); 
	}
	
	if (errors.length > 0) {
		for (var i=0;i<errors.length;i++) {
			error_msg += (i+1) + '. ' + errors[i] + "<br />";
		}
		alert_win(error_msg);
	} else {
		$('contact_form').submit();
	}

}





















function remove_id(id) {
	var remove_item = document.getElementById(id);
	document.body.removeChild(remove_item);
}

function create_cms_box() {
	var cms_div = document.createElement('div');
	cms_div.setAttribute('id','cms_quick_edit');
	document.body.appendChild(cms_div);

	var top_offset = (window.pageYOffset) ? window.pageYOffset + 'px' : document.body.scrollTop + 'px';
	document.getElementById('cms_quick_edit').style.marginTop = top_offset;
	document.getElementById('cms_quick_edit').style.overflow='scroll';
}

function edit_page(page_id) {
	if (document.getElementById('cms_quick_edit')) { remove_id('cms_quick_edit'); }
	var params = 'page_id=' + page_id + '&cmd=edit';
	
	var ajax = new Ajax();
	ajax.send_params = params;
	ajax.send_url = site_ajax_dir + 'page_content.php';
	ajax.send_method = 'POST';
	ajax.response_handler = function(resp) {
		create_cms_box();
		document.getElementById('cms_quick_edit').innerHTML = resp;
	}
	ajax.sendRequest();
}

function update_page(page_id) {
	var p_header = document.getElementById('page_header').value;
	var p_subheader = document.getElementById('page_subheader').value;
	var p_content = document.getElementById('page_content').value;
	
	var params = 'page_id=' + page_id + '&cmd=update&page_header=' + p_header + '&page_subheader=' + p_subheader + '&page_content=' + p_content;
	
	var ajax = new Ajax();
	ajax.send_params = params;
	ajax.send_url = site_ajax_dir + 'page_content.php';
	ajax.send_method = 'POST';
	ajax.response_handler = function(resp) {
		if (resp != '' || resp != ' ') {
			var resp_array = resp.split('~');
			document.getElementById('page_header_' + page_id).innerHTML = resp_array[0];
			document.getElementById('page_subheader_' + page_id).innerHTML = resp_array[1];
			document.getElementById('page_content_' + page_id).innerHTML = resp_array[2];
			cms_close();
		}
	}
	ajax.sendRequest();
}


function cms_close() {
	if (document.getElementById('cms_quick_edit')) { remove_id('cms_quick_edit'); }
}


function set_check_val(id,unchkd_val,chkd_val) {
	document.getElementById(id).value = (document.getElementById(id).checked == true) ? chkd_val : unchkd_val;
	//alert(document.getElementById(id).value);
}

function check_form(id) {
	switch (id) {
	case 'page_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			valid_form.report_errors();
			return false;
		}
		break;
	case 'event_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			valid_form.report_errors();
			return false;
		}
		break;
	case 'news_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			valid_form.report_errors();
			return false;
		}
		
		break;
	case 'sponsor_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			valid_form.report_errors();
			return false;
		}
		
		break;
	case 'image_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			valid_form.report_errors();
			return false;
		}
		break;
	case 'file_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			valid_form.report_errors();
			return false;
		}
		break;
	case 'user_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			valid_form.report_errors();
			return false;
		}
		break;
	case 'email_optin_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			return false;
		}
		break;
	case 'link_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			return false;
		}
		break;
	case 'list_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			return false;
		}
		break;
	case 'emailing_form' :
		var valid_form = new FormValidator(id,'form');
		if (valid_form.validate()) {
			return true;
		} else {
			return false;
		}
		break;
	}
}

function subscribe() {
	if (check_form('email_optin_form')) {
		var ajax = new Ajax();
		ajax.send_params = 'email=' + document.getElementById('optin_email').value;
		ajax.send_url = site_ajax_dir + 'optin.php';
		ajax.send_method = 'POST';
		ajax.response_handler = function(resp) {
			document.getElementById('home_email_optin_form').innerHTML = resp;
		}
		ajax.sendRequest();
	} else {
		alert('Please enter a properly formatted email address in the field provided.');
	}
}



function search_site() {
	if (document.getElementById('search').value != '' && document.getElementById('search').value != ' ') {
		document.getElementById('hdr_search').submit();
	} else {
		alert('Please enter in some search criteria.');
	}
}
function add_page(parent_id) {	
	window.location = "index.php?cubeit_id=1&cmd=add&cmd_id=" + parent_id;
}






