// Clears all span tags with id ending in: "_error"
function ResetValidation(tagName){
	var re = /_error$/;
	var elems = document.getElementsByTagName(tagName);

	for(m = 0; m < elems.length; m++){
		if(elems[m].id.match(re)){
			// _error span
			while(elems[m].childNodes.length){
				elems[m].removeChild(elems[m].firstChild);
			}
		}
	}
}

// Appends text node with specified text to specified element
function ShowInvalid(id, msg){
	var elem = document.getElementById(id);
	var span = document.createElement("span");
	var txt = document.createTextNode(msg);
	span.className = "invalid";
	span.appendChild(txt);
	elem.appendChild(span);
}
	
// validates that the entry has one or more characters in it
function IsNotEmpty(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /.+/;

	if(str.match(re)){
		return true;
	}else{		
		return false;
	}
}

// validates that the entry does not exceed a certain length
function IsMaxLength(id, n){
	var elem = document.getElementById(id);
	var str = elem.value;
	str = str.toString();

	if(str.length <= n){
		return true;
	}else{		
		return false;
	}
}

// validates that the entry is a positive or negative number
function IsNumeric(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^[-]?\d*\.?\d*$/;
	str = str.toString();
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}

// US Currency
function IsCurrency(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^\$?\d{1,3}(,?\d{3})*(\.\d\d)?$/;
	str = str.toString();
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}

// validates that the entry is alpha characters only
function IsAlpha(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^[a-zA-Z]*$/;
	str = str.toString();
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}

// validates that the entry is a positive integer
function IsPosInt(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^\d+$/;
	str = str.toString();
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}

// 3 to 20 alpha|numeric|underscore characters
function ValidUsername(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^[a-zA-Z][\w]{2,19}$/;
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}

// 6 to 10 alpha|numeric characters
function ValidPassword(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^([a-zA-Z]|\d){6,10}$/;
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}

// validates that two entries are equal
function IsEqual(id1, id2){
	var elem1 = document.getElementById(id1);
	var elem2 = document.getElementById(id2);
	var str1 = elem1.value;
	var str2 = elem2.value;

	if(str1 == str2){
		return true;
	}else{
		return false;
	}
}

// validates that the entry is formatted as an email address
function ValidEmail(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}

function ValidDate(id){
	var elem = document.getElementById(id);
	var str = elem.value;
	var re = /^(0?[1-9]|1[012])\/|-\d{1,2}\/|-\d{4}$/;
	if(str.match(re)){
		return true;
	}else{
		return false;
	}
}