var changed = 0;

	// find item with selected number
	// set that menu item to old value of recently changed menu
	
function swap_order(changer) {
	new_val = eval("document.forms[0].sort_"+changer+".options[document.forms[0].sort_"+changer+".selectedIndex].text");
	old_val = old_vals[changer];
	found = 0;
	for(i=1;((i<=num_items) && (!found));i++) {
		if (old_vals[i] == new_val) {
			eval("document.forms[0].sort_"+i+".selectedIndex = "+(old_val-1)+";");	
			old_vals[i] = old_val;
			found = 1;
		}
	}
	old_vals[changer] = new_val;
}

function didchange(inst) {
	changed=1;
}

function check_for_change(flag, url) {
	if (flag) {
		if (confirm("You have unsaved changes on this page. Click OK to continue without saving your changes.")) {
			if (url != "") {
				location.href=url;
			} else {
				document.the_form.submit();
			}
		}
	} else {
		if (url != "") {
			location.href=url;
		} else {
			document.the_form.submit();
		}
	}
}

//can replace with oneline mootools/prototype call
function update_div(block, content) { 
	d = document.getElementById(block);
	d.innerHTML = content;
}

//shortcut to count string items for characters
function count_chars(str) {
	return count_string_items(str, "char");
}

//shortcut to count string items for words
function count_words(str) {
	return count_string_items(str, "word");
}

//count either characters or words, ignoring tags
function count_string_items (orig,type) {	
	//no type, defaults to char
	
	//remove tags
	text = orig.replace(/<[^>].*?>/gim, "");
	//replace spaces
	text = text.replace(/(&nbsp;|&#160;|&#39;)/gi,' ');

	if(type=="word") {
		return text.replace(/\S+/g, 'a').replace(/\s+/g, '').length; 
	} else {
		return text.length;
	}
}

//show an alert, just once
//should inject error code so similar message as backend
var g_show_word_count_exceeded_msg = true;
function hitlimit(max, force) {
	if (g_show_word_count_exceeded_msg || force) {
		alert('You have exceeded the maximum limit of '+max+' characters for the description.');
		g_show_word_count_exceeded_msg = false;
	}
}

//return string with tags removed
function stripHTML(oldString) {
	return oldString.replace(/<[^>].*?>/gim, "");
}


function countChars(e) { 
	if (e.type == "keyup" ) {
		charcount = count_chars(tinyMCE.getContent()) 
		if(charcount > charlimit) {			
			update_div('remaining',(charcount - charlimit)+' over the limit');
			hitlimit(charlimit);
		} else {
			update_div('remaining',(charlimit - charcount)+' left');
		}
	}
	return true; // Continue handling
}

function douploads(e) { 
	uplooad.upload(); 
	return false; 
}

function removefile() {
	var ajax = new Ajax("file_remove.php?r=<?=$vars['file_id'] ?>", { 
		method: 'get',
		onComplete: function() { 
			if(ajax.response.text == "1") {
				$('currentfile').setStyle('display','none');
				$('file_id').value ="";
				$('fileaction').value = "none";
			}
		}
	});
	ajax.request();
	return false;
}

var emailexp = /^[a-z][a-z_0-9\.+-]+@[a-z_0-9\.]+\.[a-z]{3}$/i
var letterexp = /^[a-z]+$/i
var numberexp = /^[-0-9]+$/i

function is_valid(pattern, str) {
	return pattern.test(str)
}

function trim1(str) { 
	str.replace(/^\s*/, '').replace(/\s*$/, ''); 
	return str;
}

function trim(str) { 
	if (str != "") {
		// this will get rid of leading spaces 
		while (str.substring(0,1) == ' ') 
		str = str.substring(1, str.length);
		
		// this will get rid of trailing spaces 
		while (str.substring(str.length-1,str.length) == ' ')
		str = str.substring(0, str.length-1);
	}			
	return str;
}

function is_number(txt) {
	return is_valid(numberexp,txt);
}

function is_number_or_blank(txt) {
	return (is_blank(txt) || is_valid(numberexp,txt));
}

function is_blank(txt) {
	return (trim(txt) == "");
}

