// JavaScript Document for /profiles/edit/security/index.asp

function checkPassword() {
	if (document.editUser.Password1.value != document.editUser.Password2.value) {
		document.getElementById('badPass').style.visibility="visible";
		}
	else if (document.editUser.Password1.value == document.editUser.Password2.value) {
		document.getElementById('badPass').style.visibility="hidden";
		document.editUser.Password.value = document.editUser.Password1.value;
		}
	}
	
// Check for empty form fields
function isblank(s) {
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '')) return false;
		}
	return true;
	}
	
// Form Verification
function verify(f) {
	var msg;
	var empty_fields = '';
	var errors = '';
	
	// Loop through all text & textarea fields of the form
	for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
		e.className = '';
                if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
                    // first check if the field is empty
                    if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                        empty_fields += "          " + e.name + "\n";
						e.className = 'highlight';
                        continue;
                    }
                }
        }
			
	if ((!isblank(f.Email.value)) && (f.Email != null)) {
		var str = f.Email.value;
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (!str.match(re)) {
        errors += "- Please verify your Email address.\n";
		f.Email.className = 'highlight';
			}
		}
	
	if ((!isblank(f.Day_Phone.value)) && (f.Day_Phone != null)) {
		var str = f.Day_Phone.value;
		var re = /^([\(]{1}[0-9]{3}[\)]{1}[ |\-]{0,1}|^[0-9]{3}[\-| ])?[0-9]{3}(\-| ){1}[0-9]{4}$/;
		if (!str.match(re)) {
        errors += "- Please verify your Daytime Phone Number.\n";
		f.Day_Phone.className = 'highlight';
			}
		}
		
	if ((!isblank(f.Night_Phone.value)) && (f.Night_Phone != null)) {
		var str = f.Night_Phone.value;
		var re = /^([\(]{1}[0-9]{3}[\)]{1}[ |\-]{0,1}|^[0-9]{3}[\-| ])?[0-9]{3}(\-| ){1}[0-9]{4}$/;
		if (!str.match(re)) {
        errors += "- Please verify your Nighttime Phone Number.\n";
		f.Night_Phone.className = 'highlight';
			}
		}
				
        // Now, if there were any errors, display the messages, and
            // return false to prevent the form from being submitted. 
            // Otherwise return true.
            if (!empty_fields && !errors) return true;
        
            msg  = "______________________________________________________\n\n";
            msg += "The form was not submitted because of the following error(s).\n";
            msg += "Please correct these error(s) and re-submit.\n";
            msg += "______________________________________________________\n\n";
        
            if (empty_fields) {
                msg += "- The following required field(s) are empty:\n" + empty_fields + "\n";
                if (errors) {
                        msg += "\n";
                        msg += "______________________________________________________\n\n";
                        msg += "The following fields have errors.  Please correct.\n";
                }
            }
            msg += errors;
            alert(msg);
            return false;
	}