// validates that the field value string has one or more characters in it
function isNotEmpty(elem, name) {
    var str = elem.value;
    if(str == null || str.length == 0) {
        alert("Please fill in " + name + ".");
        return false;
    } else {
        return true;
    }
}

function checkEmpty(elem) {
	var str = elem.value;
    if(str == null || str.length == 0) {
        return false;
    } else {
        return true;
    }
}

function isNotSpaces(elem, name) {
	var str = elem.value;
	for(count = 0; count < str.length; count++) {
		if(str.charAt(count) != ' ') {
			return true;
		}
	}
	alert("Invalid value for " + name + ".");
	return false;
}

function fieldMatch(name, elem1, elem2) {
	var pass1 = elem1.value;
	var pass2 = elem2.value;
	if(pass1 != pass2) {
		alert(name + " entered not identical.");
		return false;
	}
	return true;
}

function passwordMatch(elem1, elem2) {
	var pass1 = elem1.value;
	var pass2 = elem2.value;
	if(pass1 != pass2) {
		alert("Passwords entered not identical.");
		return false;
	}
	return true;
}

function isNumber(elem, name) {
    var str = elem.value;
    var oneDecimal = false;
    var oneChar = 0;
    str = str.toString( );
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
        if (oneChar < 48 || oneChar > 57) {
            alert("Enter only positive numbers into " + name + ".");
            return false;
        }
    }
    return true;
}

function checkNumber(elem) {
    var str = elem.value;
    var oneDecimal = false;
    var oneChar = 0;
    str = str.toString( );
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
        if (oneChar < 48 || oneChar > 57) {
            return false;
        }
    }
    return true;
}

function isFloat(elem, name) {
	
	var str = elem.value;
	str = str.toString();
	var fp = 0;
	var char = -1;
	
	for(var i = 0; i < str.length; i++) {
		char = str.charAt(i);
		if(char == '.') {
			fp++;
		}
	}
	if(fp > 1) {
		alert("Invalid entry for " + name + ".");
		return false;
	}
	for(var i = 0; i < str.length; i++) {
		char = str.charAt(i).charCodeAt(0);
		if (char != 46) {
			if(char < 48 || char > 57) {
           		alert("Invalid entry for " + name + ".");
           		return false;
			}
        }
	}
	return true;
}

function checkFloat(elem) {
	
	var str = elem.value;
	str = str.toString();
	var fp = 0;
	var char = -1;
	
	for(var i = 0; i < str.length; i++) {
		char = str.charAt(i);
		if(char == '.') {
			fp++;
		}
	}
	if(fp > 1) {
		return false;
	}
	for(var i = 0; i < str.length; i++) {
		char = str.charAt(i).charCodeAt(0);
		if (char != 46) {
			if(char < 48 || char > 57) {
           		return false;
			}
        }
	}
	return true;
}


function isPercentage(elem, name) {
	if(isFloat(elem, name)) {
		if(elem.value > 100) {
			alert("The value of " + name + " must be less than 100.");
	        return false;
		}
	} else {
		return false;
	}
	return true;
}

function isSecurePassword(elem) {
	var pw = elem.value;
	pw = pw.toLowerCase();
	var letters = false;
	var numbers = false;
	
	for(var i = 0; i < pw.length; i++) {		
		oneChar = pw.charAt(i).charCodeAt(0);
		if(oneChar > 47 && oneChar < 58) {
			if(!(numbers)) {
				numbers = true;
			}
		} else if(oneChar > 96 && oneChar < 123) {
			if(!(numbers)) {
				letters = true;
			}
		} else {
			alert("Only letters and numbers are accepted for password.");
    		return false;
		}
	}
	
	if(letters && numbers) {
		return true;
	}
	
	alert("Password has to be a combination of letters and numbers.");
    return false;
}

function isEMailAddr(elem) {
    var str = elem.value;
    str = str.toLowerCase( );
    if (str.indexOf("@") > 1) {
        var addr = str.substring(0, str.indexOf("@"));
        var domain = str.substring(str.indexOf("@") + 1, str.length);
        // at least one top level domain required
        if (domain.indexOf(".") == -1) {
            alert("Verify the domain portion of the email address.");
            return false;
        }
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) {
            oneChar = addr.charAt(i).charCodeAt(0);
            // dot or hyphen not allowed in first position; dot in last
            if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
                (i == addr.length - 1 && oneChar == 46)) {
                alert("Verify the user name portion of the email address.");
                return false;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Verify the user name portion of the email address.");
                return false;
            }
        }
        for (i = 0; i < domain.length; i++) {
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) || 
                ((i == domain.length - 1  || i == domain.length - 2) && oneChar == 46)) {
                alert("Verify the domain portion of the email address.");
                return false;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Verify the domain portion of the email address.");
                return false;
            }
        }
        return true;
    }
    alert("The email address may not be formatted correctly. Please verify.");
    return false;
}

// validate that the user made a selection other than default
function isChosen(sel, sName) {
    if (sel.selectedIndex < 0) {
        alert("Make a choice from the " + sName + " list.");
        return false;
    } else {
        return true;
    }
}