// JavaScript Document

function sanityCheck(method, element, objPTR, printIt){
	if(document.getElementById)
		var divID = document.getElementById(element);
	else
		return false; // the browser does not support this write method, give up.
	if(method == "name"){
		/* run code that checks date sanity */
		if(objPTR.value == objPTR.defaultValue){
			if(printIt)
				docuPrint("(First & Last)", 0, divID);
			return false;
		}else if(objPTR.value.indexOf(" ") == -1){
			if(printIt)
				docuPrint("Both your first & last name please.", 1, divID);
			return false;
		}
		if(printIt)
			docuPrint("Name accepted.", 2, divID);
		return true;
	}else if(method == "eMail"){
		/* run code that checks email sanity */
		if(objPTR.value == objPTR.defaultValue){
			if(printIt)
				docuPrint("(user@domain.com)", 0, divID);
			return false;
		}else if(objPTR.value.indexOf("@") == -1){ // missing @
			if(printIt)
				docuPrint("Missing @ sign.", 1, divID);
			return false;
		}else if(objPTR.value.substring(objPTR.value.indexOf("@"), objPTR.value.indexOf(".")).length < 3){
			/* check the domain length */
			if(printIt)
				docuPrint("Invalid domain length ( < 2).", 1, divID);
			return false;
		}else if(objPTR.value.indexOf(".") == -1){ // missing .
			if(printIt)
				docuPrint("Missing period (dot).", 1, divID);
			return false;
		}else if(objPTR.value.substring(objPTR.value.indexOf(".")).length < 3){
			/* the TLD is smaller than 2 characters, fail out */
			if(printIt)
				docuPrint("Invalid TLD (ie, .com, .net)", 1, divID);
			return false;
		}
		/* no errors, accept the email address */
		if(printIt)
			docuPrint("eMail address accepted.", 2, divID);
		return true;
	}else if(method == "password"){
		/* run code that checks password sanity */
		if(objPTR.value == objPTR.defaultValue){
			if(printIt)
				docuPrint("Please enter a password.", 0, divID);
			return false;
		}else if(objPTR.value.length < 6){
			/* the password is shorter than 6 characters, fail out */
			if(printIt)
				docuPrint("Password must be at least 6 characters long.", 1, divID);
			return false;
		}
		/* no errors, accept the email address */
		if(printIt)
			docuPrint("Password accepted.", 2, divID);
		return true;
	}else if(method == "comments"){
		if(objPTR.value == objPTR.defaultValue)
			return false;
		return true;
	}
}

function docuPrint(message, error, divID){
	if(error == 0){ //default case, print in B&W
		/* print the message in red */
		divID.innerHTML = message;
		divID.htmlContent = message;
		return true;
	}else if(error == 1){ //1 means the error is true, print in bold red.
		/* print the message in green */
		divID.innerHTML = "<strong><font color=\"red\">"+message+"</font></strong>";
		divID.htmlContent = "<strong><font color=\"red\">"+message+"</font></strong>";
		return true;
	}else{ // means the error is false, field accepted, use the green print
		divID.innerHTML = "<font color=\"green\">"+message+"</font>";
		divID.htmlContent = "<font color=\"green\">"+message+"</font>";
		return true;
	}
}
