// form validation

function checkMessageAdd(theForm) {
    var why = "";
	why += isEmpty(theForm.title.value,"Title");
	why += isTooLong(theForm.title.value,"Title");
    why += checkUsername(theForm.username.value);
    why += checkPassword(theForm.password.value);
	why += checkTextLength(theForm.body.value,"Message Body",5000);
	if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkSignup(theForm) {
    var why = "";
    why += checkUsername(theForm.username.value);
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkLogin(theForm) {
    var why = "";
    why += checkUsername(theForm.username.value);
    why += checkPassword(theForm.password.value);
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkProfileUpdate(theForm) {
    var why = "";
    why += checkEmail(theForm.email.value);
	why += checkTextLength(theForm.interests.value,"Interests",1000);
	why += checkTextLength(theForm.biography.value,"Biography",1000);
	why += checkTextLength(theForm.signature.value,"Signature",1000);
	if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkProfile(theForm) {
    var why = "";
    why += checkPassword(theForm.password.value);
    why += passwordsMatch(theForm.password.value,theForm.password2.value);
	why += checkSelector(theForm.age.selectedIndex,"Age");
    why += checkEmail(theForm.email.value);
	why += checkTextLength(theForm.interests.value,"Interests",1000);
	why += checkTextLength(theForm.biography.value,"Biography",1000);
	why += checkTextLength(theForm.signature.value,"Signature",1000);
	if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkChangePass(theForm) {
    var why = "";
    why += checkPassword(theForm.oldpassword.value);
    why += checkPassword(theForm.newpassword1.value);
    why += passwordsMatch(theForm.newpassword1.value,theForm.newpassword2.value);
	if (why != "") {
       alert(why);
       return false;
    }
return true;
}


// password matches retyped password

function passwordsMatch(strng1,strng2) {
var error = ""; 
  if (strng1 != strng2) {
     error = "The password does not match the retyped password.\n";
  }
return error;
}

// email
function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 4-30 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
	if (strng == "") {
	   error = "You didn't enter a password.\n";
	}
    else {
		var illegalChars = /[\W_]/; // allow only letters and numbers
		
		if ((strng.length < 4) || (strng.length > 30)) {
		   error = "The password must be from 4 to 30 characters.\n";
		}
		else if (illegalChars.test(strng)) {
		  error = "The password contains illegal characters.\n";
		}
	}
return error;    
}    


// username - 4-30 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
	if (strng == "") {
	   error = "You didn't enter a username.\n";
	}
    else {
		var illegalChars = /\W/; // allow letters, numbers, and underscores
		if ((strng.length < 4) || (strng.length > 30)) {
		   error = "The username must be from 4 to 30 characters.\n";
		}
		else if (illegalChars.test(strng)) {
		error = "The username contains illegal characters.\n";
		} 
	}
return error;
}       


// non-empty password or email

function checkPassword(strng1) {
var error = "";
  if (strng1.length == 0) {
     error = "Please enter your password.\n"
  }
return error;	  
}

// non-empty textbox

function isEmpty(strng,desc) {
var error = "";
  if (strng.length == 0) {
     error = "The " + desc + " has not been filled in.\n"
  }
return error;	  
}

// non-empty textbox

function isTooLong(strng,desc) {
var error = "";
var nextpos = 0;
var repos = 0;
nextpos = strng.indexOf(" ");
repos = strng.indexOf("Re: ");
//alert(repos);
if (repos == -1) {
	while (nextpos >= 0) {
		//alert(nextpos + ":" + strng + ":" + strng.length);
		if (nextpos > 20) {
			error = "The " + desc + " has words that are too long.\nPlease insert a space at least every 20 characters.\n"
			return error;	  
		}
		strng = strng.substring(nextpos+1, strng.length-1)
		if (strng.length<20) {
			return error;	  
		}
		nextpos = strng.indexOf(" ");
		//alert(nextpos + ":" + strng + ":" + strng.length);
		if (nextpos == -1 && strng.length > 20) {
			error = "The " + desc + " has words that are too long.\nPlease insert a space at least every 20 characters.\n"
			return error;	  
		}
	}
	if (nextpos == -1 && strng.length > 20) {
		error = "The " + desc + " has words that are too long.\nPlease insert a space at least every 20 characters.\n"
		return error;	  
	}
}
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen
function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list
function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list.\n";
    }    
return error;
}   

// check biography length
function checkTextLength(strng,desc,maxLength) {
var error = "";
var thisLength = strng.length;
    if (thisLength > maxLength) {
    error = "The length of the " + desc + " is " + thisLength + " characters, which exceeds the limit of " + maxLength + ".\n";
    }    
return error;
}    

// valid selector from dropdown list
function checkSelector(index,desc) {
var error = "";
    if (index == 0) {
    error = "You didn't choose an option from the " + desc + " drop-down list.\n";
    }    
return error;
}   


