//validate media.asp, tag.asp, category.asp...
function validateForm(theForm) {
var reason = "";

	reason += validateEmpty(theForm.FromName);
	reason += validateEmail(theForm.FromEmail);
	reason += validateEmpty(theForm.Comments);      
 
	if (reason != "") {
		alert("Some fields need your attention!\n\n" + reason + "\nPlease ensure all changes are accurate\nbefore subbmiting this form.");
		return false;
	}
	
	showStatus();
	 
	return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
        error = "A required field has Been left empty.\n"
    } else {
        fld.style.border = "";
		fld.style.background = "";
    }
    return error;  
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.border = "";
		fld.style.background = "";
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
       error = "The password is the wrong length. \n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
        error = "The password contains illegal characters.\n";
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
        error = "The password must contain at least one numeral.\n";
    } else {
        fld.style.border = "";
		fld.style.background = "";
    }
   return error;
}  

function trim(s)
{
  return s.replace(/^\s+|\s+$/, "");
}

function validateEmail(fld) {
	var error="";
	var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	
	if (fld.value == "") {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
		error = "You didn't enter an email address.\n";
	} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
		error = "Please enter a valid email address.\n";
	} else if (fld.value.match(illegalChars)) {
        fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
		error = "The email address contains illegal characters.\n";
	} else {
		fld.style.background = "White";
	}
	return error;
}

function validateNumber(fld) {
	var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\!\@\#\$\%\^\&\*\ ]/g, "");    
	
	if (fld.value == "") {
		fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
		error = "A required field has Been left empty.\n";
	} else if (isNaN(parseInt(stripped))) {
		fld.style.border = "1px solid #f93a3a";
		fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
		error = "A required field contains illegal characters.\n";
	//} else if (!(stripped.length == 10)) {
	//	fld.style.border = "1px solid #f93a3a";
	//	fld.style.background = "#ffffff url('interface/cancel.gif') no-repeat 98% 50%";
	//	error = "The phone number is the wrong length. Make sure you included an area code.\n";
	}
	return error;
}


function showStatus() {
	//hide the actual form...
	document.myForm.style.display = "none"
	
	//show the status/activity monitor so users know something is happening...
	document.getElementById('myStatus').style.display = "block"
}
