function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
	}

function fieldClear(fieldTxt,txt) { 
	if (fieldTxt.value == txt) fieldTxt.value = "";
	}

function fieldRestore(fieldTxt,txt) { 
	if (fieldTxt.value == "") fieldTxt.value = txt;
	}

function PopupPic(sPicURL,sPicTitle) {
	window.open('/popup.php?file='+sPicURL+'&title='+sPicTitle,"vd_blowup","height=400,width=400,scrollbars=0,resizable=1");
} 

function PopupWindow(sURL,sW,sH) {
	window.open(sURL,"Brainfag Doohickey Window","height="+sH+",width="+sW+",scrollbars=1,resizable=1");
} 

function makeEmailLink(address, text) {
	document.write('<a href="mailto:' + address + '">' + text + '</a>');
	}

function checkConfirm(form)
{
	var value = true;
	if(button !="noask")
	{
		if(!formSubmit)
		  value = false;
		else
		  value = confirm("Are you sure you want to " + button + "?");
	}
	return value;
}

function toggleDisplay(obj_id,force){
	if (document.getElementById){
		var obj = document.getElementById(obj_id);
		if (obj.style.display == '' || obj.style.display == 'none' || force=='on'){
			var state = 'block';
		} else {
			var state = 'none';
		}
		obj.style.display = state;
	}
}
function toggleSlide(obj_id){
	if (document.getElementById){
		var obj = document.getElementById(obj_id);
		if (obj.style.display == 'none'){
			new Effect.SlideDown(obj);
		} else {
			new Effect.SlideUp(obj);
		}
	}
}

/* Nate's mootools functions for generic form validation */

// Tries to get field name from <label>, resorts to capitalized version of 
function getFieldName(field) {
    if ($(field.id+'-label')) {
        var fieldStr = $(field.id+'-label').innerHTML;
        // strip out <input /> tags (if the <input> is inside the <label> as it is in comments.php)
        fieldStr = fieldStr.replace(/<input[^>]+\/?>/g,'');
        // strip out <span>foo</span> fields and whatnot, such as (required) (not shown) in comments form
        fieldStr = fieldStr.replace(/<[^>]+>[^<]+<[^>]+>/g,'').trim();
        // strip out colons
        fieldStr = fieldStr.replace(/:$/g,'');
    } else {
        // use field's name if <label> can't be found
        var fieldStr = field.name.replace(/-/g,' ').trim().capitalize();
    }
    return fieldStr;
}

function isEmail(str) {
       var isEmail  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return isEmail.test(str);
}

/*  Simple form validator -- checks for inputs with class 'required' -- also validates field 'email' if present */

function checkForm(formToCheck) {
	var errorReturn = '';
	var focusAfter = '';

	var reqFields = $(formToCheck).getElements('.required');
	reqFields.forEach(function(field){
		if (field.value.trim() == '') {
			focusAfter = (focusAfter == '') ? field : focusAfter; // Set focus to first error after check
			errorReturn += 'Please enter a value for ' + getFieldName(field) + ".\n";
		}
	});	
       
	var emailFields = $(formToCheck).getElements('.isEmail');
	emailFields.forEach(function(field){
   		if (!isEmail(field.value))
   		{
   			focusAfter = (focusAfter == '') ? field : focusAfter;
   			errorReturn += 'Please enter a valid email for ' + getFieldName(field) + ".\n";
   		}
    });	
	
	if (errorReturn != '')
	{
		alert(errorReturn);
		focusAfter.focus();
		return false;
	} else {
		return true;
	}
}
