//Created by Marv Rubenstein 3/2/06 for the CCC membership information system.
// Added password check 4/19/06 and strengthened it on 4/25/06 with two additional checks.
// 10/9/06: Added openMBHelp function for all of the message board pages.
// 9/4/10: Added formIsDirty to determine if any field on a form has been updated.

	function openMBHelp() {
	  window.open("help_msgboard.php", "helpWindow", "top=40,left=50,width=740,height=630")
	}


// Checks Password length and makes sure there are only letters and numbers and at least one of each.
function badPassword(strPW) {
	var illegalChars = /[\W_]/;
	var oneNumber = /[0-9]/;
	var oneLCase = /[a-z]/;
	var oneUCase = /[A-Z]/;
	if(isBadLen(chngpw.newpw.value, chngpw.newpw, "New Password", 8)) {
	  	return true
	}
	else if(illegalChars.test(strPW)) {
	  	alert("The new password can only contain letters and numbers. Spaces and other characters are not allowed.");
	  	chngpw.newpw.focus()
	  	chngpw.newpw.select()
		return true
 	}
	else if (!oneNumber.test(strPW)) {
		alert("The new password must contain at least one number.");
	  	chngpw.newpw.focus()
	  	chngpw.newpw.select()
		return true
	}
	else if (!((oneLCase.test(strPW)) || (oneUCase.test(strPW)))){
		alert("The new password must contain at least one letter, which can be either upper or lower case.");
	  	chngpw.newpw.focus()
	  	chngpw.newpw.select()
		return true
	}
	return false
}

// Checks for null values.
function isEmpty(itemValue, item, itemDesc) {
	var strMsg
	if (item.name == "email") {
		strMsg = "Email is for club e-newsletter and messages. If not wanted, enter x@x to avoid this message."
	} else {
	  	strMsg = "The " + itemDesc + " item can't be blank."
	}
	if (itemValue == "") {
	   alert(strMsg) 
		item.focus()
		return true
	}
	return false
}
// Checks for both length of string and null value
function isBadLen(itemVal, item, itemDesc, itemLen) {
 	var strInput, strLength, strMsg
	strValue = itemVal.toString()
 	strLength = itemLen.toString()
	if (item.name == "phone") {
	   strMsg = "The Phone item is either blank or not formatted 'xxx-xxx-xxxx'" }
	else if (item.name == "newpw") {
	   strMsg = "The New Password must contain eight numbers and letters."	
	} else {
	  	strMsg = "The " + itemDesc + " item is either blank or has less than " + itemLen + " characters."
	}
	if (strValue.length < strLength) {
 		alert(strMsg)
		item.focus()
		item.select()
 		return true
 		}
 	return false
 }
 //Outputs current date in format yyyy-mm-dd for input to mySQL table.
 function getToday() {
	var now = new Date();
	var months = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
   var year = now.getYear();
	var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
   return(year + "-" + months[now.getMonth()] + "-" + date);
}

// Checks if at least one field has been updated on the form. Called by validate function.
function formIsDirty(form)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        var element = form.elements[i];
        var type = element.type;
        if (type == "checkbox" || type == "radio")
        {
            if (element.checked != element.defaultChecked)
            {
                return true;
            }
        }
        else if (type == "hidden" || type == "password" || type == "text" ||
                 type == "textarea")
        {
            if (element.value != element.defaultValue)
            {
                return true;
            }
        }
        else if (type == "select-one" || type == "select-multiple")
        {
            for (var j = 0; j < element.options.length; j++)
            {
                if (element.options[j].selected !=
                    element.options[j].defaultSelected)
                {
                    return true;
                }
            }
        }
    }
    return false;
}



