function verify(msg){
	if (msg){
		msg = msg;
	} else {
		msg = "Are you absolutely sure that you want to delete this record?";
	}
	return confirm(msg);
}

function not_empty(){
	msg = "Please delete all children of this category first.";
	return alert(msg);
}

function navigate(choice) {
	var spot=choice.options[choice.selectedIndex].value;
	var confirmdelete = false;
	var deletefound = spot.indexOf("delete");
	if (deletefound < 1){
		deletefound = spot.indexOf("remove");
	}
	if (deletefound > 0){
		confirmdelete = confirm("Are you absolutely sure that you want to delete this record?");
		if (confirmdelete == true){
			location.href = spot;
		}
		choice.selectedIndex = 0;
	} else {
		var frontendfound = spot.indexOf("../");
		if (frontendfound != 0){ var frontendfound = spot.indexOf("http:"); }
		
		if (frontendfound == 0){
			window.open(spot);
			choice.selectedIndex = 0;
		} else {
			if (spot != 'choose') {
				location.href = spot;
			}
		}
		
	}
}

var state = 'none'; 
function showhide(layer_ref) { 
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		state = document.all[layer_ref].style.display;
		if (state == 'block') { state = 'none'; } else { state = 'block'; } 
		eval( "document.all." + layer_ref + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		state = document.layers[layer_ref].display;
		if (state == 'block') { state = 'none'; } else { state = 'block'; } 
		document.layers[layer_ref].display = state; 
	} 
	if (document.getElementById &&!document.all) { 
		hza = document.getElementById(layer_ref); 
		state = hza.style.display;
		if (state == 'block') { state = 'none'; } else { state = 'block'; } 
		hza.style.display = state; 
	} 
} 
function showdiv(layer_ref){
	var state = 'block';
	if (document.getElementById) { 
		hza = document.getElementById(layer_ref); 
		hza.style.display = state;
		return; 
	}
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		eval( "document.all." + layer_ref + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[layer_ref].display = state; 
	}  
}
function hidediv(layer_ref){
	var state = 'none';
	if (document.getElementById) { 
		hza = document.getElementById(layer_ref); 
		hza.style.display = state; 
		return;
	} 
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		eval( "document.all." + layer_ref + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[layer_ref].display = state; 
	} 
}
// ------------------------------------------------------------------------------------
// Form Functions
// ------------------------------------------------------------------------------------
function fade_form(formname){
	return;
}

function clear_form(formname){
	var num_elements = document[formname].elements.length;
	for(c=0;c<num_elements;c++){
		var hasvalue = false;
		var element_name = document[formname].elements[c].name;
		var element_value = document[formname].elements[c].value;
		var element_type = document[formname].elements[c].type;
		//alert(element_type + " -- " + element_name + "=" + element_value);

		if (element_type == "checkbox" && document[formname].elements[c].checked == true){ document[formname].elements[c].checked = false; }
		if (element_type == "radio" && document[formname].elements[c].checked == true){ document[formname].elements[c].checked = false; }
		if (element_type == "text" && element_value.length > 0){ document[formname].elements[c].value = ''; }
		if (element_type == "hidden" && element_value.length > 0){  }
		if (element_type == "select-one" && element_value.length > 0){ 
			for(d=0;d<document[formname].elements[c].length;d++){
				document[formname].elements[c][d].selected = false;
			}		
		}
		if (element_type == "textarea" && element_value.length > 0){ document[formname].elements[c].value = ''; }
		if (element_type == "select-multiple"){ 
			for(d=0;d<document[formname].elements[c].length;d++){
				document[formname].elements[c][d].selected = false;
			}
		}
	}
}
function ajax_escape(str){
	str = str.replace(/\+/g, "%2B");
	str = escape(str);
	return str;
}
function update_form_readonly(formname){
	// make all form fields readonly ;)
	var num_elements = document[formname].elements.length;
	for(var c=0;c<num_elements;c++){
		var element_type = document[formname].elements[c].type;
		if (element_type == "text"){ document[formname].elements[c].readOnly = true; }
		if (element_type == "select-one"){ document[formname].elements[c].disabled = true; }
		if (element_type == "textarea"){ document[formname].elements[c].readOnly = true; }
		if (element_type == "checkbox" || element_type == "radio"){ 
			if (document[formname].elements[c].length > 1){
				for(var d=0;d<document[formname].elements[c].length;d++){
					alert(document[formname].elements[c][d].value);
					document[formname].elements[c][d].disabled = true;
				}
			} else {
				document[formname].elements[c].disabled = true;
			}
		}
		if (element_type == "select-multiple"){ document[formname].elements[c].disabled = true; }
	}
}
// ------------------------------------------------------------------------------------
// Tab Add/Edit Switching
// ------------------------------------------------------------------------------------
function start_editing(addeditdiv, linktab, edittext, loadingdiv, recordid, customjs){
	document.getElementById(linktab).innerHTML = edittext;
	if (addeditdiv.length > 0){
		document.getElementById(addeditdiv).innerHTML = '';
		ajax_loaddiv(addeditdiv, loadingdiv, '', '&id=' + recordid + '&recordid=' + recordid);
		addedittab = linktab.replace("linktab_", "tab_");
		change_tab(addedittab);
	}
}
function stop_editing(addeditdiv, linktab, addtext, listingtab){
	document.getElementById(linktab).innerHTML = addtext;
	if (listingtab.length > 0){ listingtab = listingtab.replace("linktab_", "tab_"); change_tab(listingtab); }
	if (addeditdiv.length > 0){ 
		var qstring = "&"+window.location.search.substring(1);
		ajax_loaddiv(addeditdiv, '', '', qstring); 
	}
	scroll(0,0);
	remove_editor();
}
// ------------------------------------------------------------------------------------
// TinyMCE Ajax Functions
// ------------------------------------------------------------------------------------
function load_editor(){
	if (document.getElementById('editor_html') != null){
		//alert('Loading editor...');
		tinyMCE.idCounter=0;
		tinyMCE.execCommand('mceAddControl', false, 'editor_html');				
	}
}
function remove_editor(){
	//alert('Removing editor...');
	if (document.getElementById('editor_html')){
		tinyMCE.execCommand('mceRemoveControl', false, 'editor_html');
	}
}
function load_editors_form(formid){
	var textareas = document.getElementById(formid).getElementsByTagName("textarea");
	for (var i=0; i<textareas.length; i++){
		if (textareas[i].className.substr(0, 3) == "mce"){
			var editorstyle = textareas[i].className.replace("mce", "");
			$.get(location.pathname,
				{ ajax: "editor_wysiwyg_ajax", init: editorstyle, id: textareas[i].id },
				function (data){
					eval(data);
					load_editor_wysiwyg(editorid);
				}
			);
		}
	}	
}
function load_editor_wysiwyg(id){
	if (!tinyMCE.get(id)){
		tinyMCE.execCommand('mceAddControl', false, id);
	}
}
function remove_editor_wysiwyg(id){
	if (tinyMCE.get(id)){
		tinyMCE.execCommand('mceRemoveControl', false, id);
	}
}
function remove_editors(){
	var textareas = document.getElementsByTagName("textarea");
	for (var i=0; i<textareas.length; i++){
		if (textareas[i].className.substr(0, 3) == "mce"){
			tinyMCE.execCommand('mceRemoveControl', false, textareas[i].id);
		}
	}
}
function remove_form_editors(form){
	form = document.getElementById(form);
	if (form){
		var textareas = form.getElementsByTagName("textarea");
		for (var i=0; i<textareas.length; i++){
			if (textareas[i].className.substr(0, 3) == "mce"){
				tinyMCE.execCommand('mceRemoveControl', false, textareas[i].id);
			}
		}
	}
}
function save_editors(){
	if (typeof(tinyMCE) != "undefined") {
		tinyMCE.triggerSave();
	}	
}
// ------------------------------------------------------------------------------------
// Format Functions
// ------------------------------------------------------------------------------------
function format_number(total){
	var tempnum = total.toString();
	tempnum = tempnum.replace("$", "");
	tempnum = tempnum.replace(",", "");
	tempnum = tempnum.replace(" ", "");
	var wd = "w"
	if (tempnum.length == 0){ return ""; }
	for (i=0;i<tempnum.length;i++){
		if (tempnum.charAt(i) == "."){ wd = "d"; break; }
	}
	if (wd=="w"){
		total = tempnum + ".00";
		return total;
	} else {
		if (tempnum.charAt(tempnum.length-1) == "."){
			total = tempnum + "00";
			return total;
		}
		if (tempnum.charAt(tempnum.length-2) == "."){
			total = tempnum + "0";
			return total;
		}
		if (tempnum.charAt(tempnum.length-3) == "."){
			return total;
		} 
		tempnum = Math.round(tempnum*100)/100;
		total = tempnum;
		return total;
	}
}
// ------------------------------------------------------------------------------------
// Ajax Silent Data Submit
// ------------------------------------------------------------------------------------
function ajax_silentsubmit(url, form_post){
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				// returned ok				
			} else {
				// error
				//alert("Sorry, there was an error!  Please contact support.");
			}
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	xmlhttp.send(form_post);
}
// ------------------------------------------------------------------------------------
// Ajax Get Data
// ------------------------------------------------------------------------------------
function ajax_getdata(customvars, fieldid){
	var query_string = "";
	if (customvars.length > 0){ query_string = query_string + customvars; }
	
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', location.pathname+'?' + query_string, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				// returned ok
				document.getElementById(fieldid).value = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(null);
}
// ------------------------------------------------------------------------------------
// Ajax Post Form
// ------------------------------------------------------------------------------------
function ajax_postform(formname, loadingdiv, success_message, divreloads, activetab, customjs){
	// disable submit button
	//document.getElementById('submit').disabled = true;
	
	// trigger editor save
	if (typeof(tinyMCE) != "undefined") {
		tinyMCE.triggerSave();
	}
	
	var form_post = "";
	
	var num_elements = document[formname].elements.length;
	for(c=0;c<num_elements;c++){
		var hasvalue = false;
		var element_name = document[formname].elements[c].name;
		var element_value = document[formname].elements[c].value;
		var element_type = document[formname].elements[c].type;
		//alert(element_type + " -- " + element_name + "=" + element_value);

		if (element_type == "radio" && document[formname].elements[c].checked == true){ hasvalue = true; }
		if (element_type == "text" && element_value.length > 0){ hasvalue = true; }
		if (element_type == "hidden" && element_value.length > 0){ hasvalue = true; }
		if (element_type == "select-one" && element_value.length > 0){ hasvalue = true; }
		if (element_type == "textarea" && element_value.length > 0){ hasvalue = true; }
		if (hasvalue == true){ form_post = form_post + "&" + element_name + "=" + ajax_escape(element_value); }
		if (element_type == "checkbox"){ 
			if (document[formname].elements[c].length > 1){
				for(var d=0;d<document[formname].elements[c].length;d++){
					if (document[formname].elements[c][d].checked == true){
						form_post = form_post + "&" + element_name + "=" + ajax_escape(document[formname].elements[c][d].value);
					}
				}
			} else {
				if (document[formname].elements[c].checked == true){ form_post = form_post + "&" + element_name + "=" + ajax_escape(document[formname].elements[c].value); }
			}
		}
		
		if (element_type == "select-multiple"){ 
			for(var d=0;d<document[formname].elements[c].length;d++){
				if (document[formname].elements[c][d].selected == true){
					form_post = form_post + "&" + element_name + "=" + ajax_escape(document[formname].elements[c][d].value);
				}
			}
		}
	}
	
	// add in a flag that this is a AJAX form post
	form_post = form_post + "&ajax_post=1";

	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', location.pathname, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				// returned ok
				if (loadingdiv.length > 0){ hidediv(loadingdiv); }
				
				//changeOpac(90, formname);
				
				var response = xmlhttp.responseText;

				// look for errors
				if (response.indexOf("lert: ") > 0 || response.indexOf("val: ") > 0){
					
					// search for "Eval: "
					if (response.indexOf("val: ") > 0){
						var startpos = response.indexOf("Eval: ");
						var response_eval = response.substring(startpos);
						response_eval = response_eval.replace("Eval: ", "");
						response = response.substring(0, startpos);
						eval(response_eval);
					}	
					
					// search for "Alert: "
					if (response.indexOf("lert: ") > 0){
						response = response.replace("Alert: ", "");			
						alert(response);
					}
					
				} else {
					// enable submit
					//document.getElementById('submit').disabled = false;
					
					// reload divs as specified
					if (divreloads.length > 0){
						var qstring = "&"+window.location.search.substring(1);
						var reloaddivs = new Array();
						reloaddivs = divreloads.split(',');	
						for(c=0;c<reloaddivs.length;c++){
							ajax_loaddiv(reloaddivs[c], loadingdiv, '', qstring)
						}
					}
					if (activetab.length > 0){ change_tab(activetab); }
					if (success_message.length > 0){ ajax_message(success_message); }
					
					// remove mce editor
					remove_editor();
					
					if (customjs.length > 0){ eval(customjs); }
				}
				
				
			} else {
				// error
				if (loadingdiv.length > 0){ hidediv(loadingdiv); }
				//alert("Sorry, there was an error!  Please contact support.");
			}
		}
	}
	if (loadingdiv.length > 0){ 
		showdiv(loadingdiv); 
		if (document.getElementById(loadingdiv+'-img')){ document.getElementById(loadingdiv+'-img').src = document.getElementById(loadingdiv+'-img').src; }
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');	
	xmlhttp.send(form_post);
}
// ------------------------------------------------------------------------------------
// Ajax Get HTML (returns value ;)
// ------------------------------------------------------------------------------------
function ajax_get(url, callback){
	var xmlhttp = new XMLHttpRequest();
	var returnval = "";
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				var str = xmlhttp.responseText;
				str=str.replace(/\'/g,'\\\'')
				str=str.replace(/\n/g,'\\\n')
				str=str.replace(/\r/g,'')
				eval(callback+"('"+(str)+"')");
			}
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(null);
}
// ------------------------------------------------------------------------------------
// Ajax Load Div
// ------------------------------------------------------------------------------------
function ajax_loaddiv(divname, loadingdiv, success_message, customvars){
	var query_string = "?ajax=" + divname;
	var customjs = "";
	if (arguments.length > 4){ customjs = arguments[4]; }
	if (customvars.length > 0){ query_string = query_string + customvars; }
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', location.pathname + query_string, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				// returned ok
				// check for redirect (location.href)
				if (xmlhttp.responseText.indexOf('location.href=') > 0 && xmlhttp.responseText.indexOf('<div') == -1){
					ajax_loaddiv_results(xmlhttp.responseText, divname);
				} else {
					if (loadingdiv.length > 0){ hidediv(loadingdiv); }					
					ajax_loaddiv_results(xmlhttp.responseText, divname);
					if (success_message.length > 0){ ajax_message(success_message); }
					if (customjs.length > 0){ eval(customjs); }
				}
			} else {
				// error
				if (loadingdiv.length > 0){ hidediv(loadingdiv); }
				//alert("Sorry, there was an error!  Please contact support.");
			}
		}
	}
	
	if (loadingdiv.length > 0){ 
		showdiv(loadingdiv); 
		if (document.getElementById(loadingdiv+'-img')){ document.getElementById(loadingdiv+'-img').src = document.getElementById(loadingdiv+'-img').src; }
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(null);
}
function ajax_loaddiv_post(divname, loadingdiv, success_message, customvars){
	var query_string = "?ajax=" + divname;
	
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', location.pathname + query_string, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				// returned ok
				if (loadingdiv.length > 0){ hidediv(loadingdiv); }
				ajax_loaddiv_results(xmlhttp.responseText, divname);
				if (success_message.length > 0){ ajax_message(success_message); }
			} else {
				// error
				if (loadingdiv.length > 0){ hidediv(loadingdiv); }
				//alert("Sorry, there was an error!  Please contact support.");
			}
		}
	}
	
	if (loadingdiv.length > 0){ 
		showdiv(loadingdiv); 
		document.getElementById('ajax-loader').src = document.getElementById('ajax-loader').src;
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(customvars);
}
function ajax_loaddiv_results(responseText, divname){
	document.getElementById(divname).innerHTML = responseText;
	
	// parse out the javascript and eval it.
	strToParse = responseText;
	strToParse2 = strToParse
	var intCursor = 0;
	var intStart = 1;
	var intStart_script = 1;
	var intEnd = 1;
	while(intCursor < strToParse.length && intStart > 0 && intEnd > 0){
		intStart = strToParse.indexOf('<scr', intCursor);
		intStart_script = intStart;
		intEnd = strToParse.indexOf('</scr', intCursor);
		if(intEnd >= intStart && intEnd > -1){
			//alert('intStart = ' + intStart);
			//alert('intEnd = ' + intEnd);
			if(intStart > -1){
				result = strToParse.substring(intStart, intEnd);
				intStart = result.indexOf('>') + 1;
				result = result.substring(intStart);
				//alert('length: ' + result.length);
				if(result.length == 0){
					// This is a script tag with a src value.  It needs to be put outside of the reloaded div
					intSrcStart = strToParse.indexOf('src="', intStart_script);
					intSrcStart = intSrcStart + 5;
					intSrcEnd = strToParse.indexOf('"', intSrcStart);
					//alert('substring: ' + strToParse.substring(intSrcStart, intSrcEnd));
					fragment_url = strToParse.substring(intSrcStart, intSrcEnd);
					//add dir if reloads
					//alert(fragment_url + " needs to be outside of the div: " + divname);
				}
				result = result.replace('<!--', '');
				result = result.replace('-->', '');
				//alert(result);
				eval(result);
			}
			intCursor = intEnd + 1;
		}
	}
}
function ajax_message(success_message){
	if (document.getElementById('message')){
		if (document.getElementById('message').innerHTML.length > 0){
			if (document.getElementById('message').innerHTML == success_message){ return false; }
			setTimeout("ajax_message('"+success_message+"')", 1000);
			return false;
		}
		document.getElementById('message').innerHTML = success_message;
		showdiv('message');
		if(document.getElementById('message').style.opacity == 0) { changeOpac(90, 'message'); }
		setTimeout("opacity('message', 90, 0, 1000)", 2000);
		setTimeout("document.getElementById('message').innerHTML='';", 3000);
	}
}
// ------------------------------------------------------------------------------------
// Checkbox Select Functions
// ------------------------------------------------------------------------------------
function select_all(formname, checkname){
	var checkboxes = document[formname][checkname + '[]'];
	var num_checkboxes = checkboxes.length;
	if (!checkboxes.length){ checkboxes.checked = true; }
	for(c=0;c<num_checkboxes;c++){
		if (checkboxes[c].disabled == false){
			checkboxes[c].checked = true;
		}
	}
}
function deselect_all(formname, checkname){
	var checkboxes = document[formname][checkname + '[]'];
	var num_checkboxes = checkboxes.length;
	if (!checkboxes.length){ checkboxes.checked = false; }
	for(c=0;c<num_checkboxes;c++){
		if (checkboxes[c].disabled == false){
			checkboxes[c].checked = false;
		}
	}
}

function batch_checked(formname, checkname){
	var checkboxes = document[formname][checkname + '[]'];
	if (!checkboxes.length && checkboxes.checked == true){ return true; }
	for(var i=0; i<checkboxes.length; i++){
		if (checkboxes[i].checked == true){
			return true;
		}
	}
	return false;
}
// ------------------------------------------------------------------------------------
// Custom URL functions
// ------------------------------------------------------------------------------------
function update_url_type(custom_url_field){
	if (custom_url_field[0].checked == true){
		// auto
		hidediv('custom_url_value');
	} else {
		// custom
		showdiv('custom_url_value');
	}
}
function update_url_type2(custom_url_field){
	if (custom_url_field[0].checked == true){
		// auto
		hidediv('custom_url_value2');
	} else {
		// custom
		showdiv('custom_url_value2');
	}
}
function format_custom_url(custom_url_field){
	var custom_url = custom_url_field.value;
	custom_url = custom_url.replace(/ /g,"-");
	custom_url = custom_url.replace(/_/g,"-");
	custom_url = custom_url.replace(/_/g,"-");
	custom_url = custom_url.replace(/--/g,"-");
	custom_url = custom_url.replace(/\#/g,"");
	custom_url = custom_url.replace(/\?/g,"");
	custom_url = custom_url.replace(/\&/g,"");
	custom_url = custom_url.replace(/\*/g,"");
	custom_url = custom_url.replace(/\(/g,"");
	custom_url = custom_url.replace(/\)/g,"");
	custom_url = custom_url.replace(/\%/g,"");
	custom_url = custom_url.replace(/\@/g,"");
	custom_url = custom_url.replace(/\!/g,"");
	custom_url = custom_url.replace(/\$/g,"");
	custom_url = custom_url.replace(/\^/g,"");
	custom_url = custom_url.replace(/\'/g,"");
	custom_url = custom_url.replace(/\"/g,"");
	custom_url = custom_url.replace(/\:/g,"");
	custom_url = custom_url.replace(/\+/g,"");
	custom_url = custom_url.replace(/--/g,"-");
	custom_url = custom_url.replace(/\\/g,"/");
	custom_url = custom_url.replace(/<?= "é"; ?>/g,"e");
	if (custom_url.substr(0, 1) == "/"){ custom_url = custom_url.replace(/\//,""); }
	custom_url = custom_url.toLowerCase();
	custom_url_field.value = custom_url;
}
// ------------------------------------------------------------------------------------
function multi_selector_filelist_delete(filelist){
// ------------------------------------------------------------------------------------	
	var hasfiles = false;
	var divs = document.getElementById(filelist).getElementsByTagName("div");
	for (var i=0; i<divs.length; i++){
		if ($('#'+divs[i].id).is(":visible")){ hasfiles = true; }
	}
	if (hasfiles){
		showdiv(filelist);
	} else {
		hidediv(filelist);	
	}
}
// ------------------------------------------------------------------------------------
function update_datatable_altcol(table){
// ------------------------------------------------------------------------------------
	// loop through all table rows
	var tbody = document.getElementById(table).getElementsByTagName("tbody");
	var rows = tbody[1].getElementsByTagName("tr");
	var color = false;
	for (var i=0; i<rows.length; i++){
		if (color == true){
			rows[i].style.backgroundColor = "#f9f9f9";
			color = false;
		} else {
			rows[i].style.backgroundColor = "#ffffff";
			color = true;	
		}
	}	
}
// ------------------------------------------------------------------------------------
