

/**
 * This function is used to show a hidden area when a checkbox is selected.
 * 
 * @param id1 - The id of the checkbox which calls the function
 * @param id2 - The id of the area which is to be displayed
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-02
 * @version 2010-06-02
 */
function allowSubmit(checkboxID, submitID)
{
	var box = document.getElementById(checkboxID);
	if(box.checked)
	{
		$('#' + submitID).css('visibility', 'visible');
	}
	else
	{
		$('#' + submitID).css('visibility', 'hidden');
	}
}


/**
 * This function will check if all required fields were filled in.
 * It will display an appropriate message in a <div> container near the top 
 * of the page.
 * 
 * @return returnValue - boolean variable, if false then form is invalid and should not
 * be submitted, if true then it is ready to submit to the server.
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-03
 * @version 2010-06-03
 */
function ValidateSignUp()
{
	var firstname = document.getElementById('FirstName');
	var lastname = document.getElementById('LastName');
	var address = document.getElementById('Address');
	var city = document.getElementById('City');
	var zip = document.getElementById('Zip');
	var phone = document.getElementById('Phone');
	var dob = document.getElementById('DateOfBirth');
	var email = document.getElementById('Email');
	var password = document.getElementById('Password');
	var confirm = document.getElementById('Password2');
	var code = document.getElementById('DoctorCode');
	var membertype = document.getElementById('membertype');
	var agreebox = document.getElementById('agreebox');
	var messagebox = document.getElementById('messagebox');
	var returnValue = true;
	messagebox.innerHTML = '';
	
	if(firstname.value == '')
	{
		messagebox.innerHTML += 'Please enter your first name.<br />';
		returnValue = false;
	}
	if(lastname.value == '')
	{
		messagebox.innerHTML += 'Please enter your last name.<br />';
		returnValue = false;
	}
	if(address.value == '')
	{
		messagebox.innerHTML += 'Please enter your address.<br />';
		returnValue = false;
	}
	if(city.value == '')
	{
		messagebox.innerHTML += 'Please enter the city in which you reside.<br />';
		returnValue = false;
	}
	if(zip.value == '')
	{
		messagebox.innerHTML += 'Please enter your zip code.<br />';
		returnValue = false;
	}
	else if(!(/^\d{5}$/).test(zip.value))
	{
		messagebox.innerHTML += 'Invalid zip code, must be 5 digits.<br />';
		returnValue = false;
	}
	if(phone.value == '')
	{
		messagebox.innerHTML += 'Please enter your phone number.<br />';
		returnValue = false;
	}
	else if(!validatePhone())
	{
		messagebox.innerHTML += 'Phone number is invalid. Must be in the form (555) 555-5555.<br />';
		returnValue = false;
	}
	if(dob.value == '')
	{
		messagebox.innerHTML += 'Please enter your date of birth.<br />';
		returnValue = false;
	}
	else if(!validateDate())
	{
		messagebox.innerHTML += 'Date of birth must be in format "mm/dd/yyyy".<br />';
		returnValue = false;
	}
	if(email.value == '')
	{
		messagebox.innerHTML += 'Please enter an email address.<br />';
		returnValue = false;
	}
	else if(!validateEmail())
	{
		messagebox.innerHTML += 'Email is invalid. Please enter a valid email address.<br />';
		returnValue = false;
	}
	if(password.value == '')
	{
		messagebox.innerHTML += 'Please enter a valid password.<br />';
		returnValue = false;
	}
	if(password.value.length < 7)
	{
		messagebox.innerHTML += 'Password is not long enough.<br />';
		returnValue = false;
	}
	if(confirm.value != password.value)
	{
		messagebox.innerHTML += 'Confirm password does not match password.<br />';
		returnValue = false;
	}
	if(code.value == '')
	{
		messagebox.innerHTML += 'Please enter a valid referral code.<br />';
		returnValue = false;
	}
	if(!membertype.checked)
	{
		messagebox.innerHTML += 'Please select a membership type.<br />';
		returnValue = false;
	}
	if(!agreebox.checked)
	{
		messagebox.innerHTML += 'You must agree with the Terms &amp; Conditions in order to continue.<br />';
		returnValue = false;
	}
	return returnValue;
}



/**
 * This function will check if the phone number is valid
 * 
 * @return boolean, if true then number is valid,
 * if false it is invalid
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-03
 * @version 2010-06-03
 */
function validatePhone()
{
	var rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
	if(!rePhoneNumber.test(document.getElementById('Phone').value))
		return false;
	else
		return true;
}



/**
 * This function will check if the date of birth format is valid
 * 
 * @return boolean, if true then format is valid,
 * if false it is invalid
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-03
 * @version 2010-06-03
 */
function validateDate()
{
	var dateReg = new RegExp(/^[0-1][0-9](\/)[0-3][0-9]\1\d{4}$/);
	if(!dateReg.test(document.getElementById('DateOfBirth').value))
		return false;
	else
	{
		var splitDate = document.getElementById('DateOfBirth').value.split("/");
		var currentYear = new Date().getFullYear();
		if(splitDate[2] >= currentYear)
			return false;
		return true;
	}
}


/**
 * This function will check if the email address is valid
 * 
 * @return boolean, if true then email is valid,
 * if false it is invalid
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-03
 * @version 2010-06-03
 */
function validateEmail()
{
	var emailReg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;
	if(!emailReg.test(document.getElementById('Email').value))	
		return false;
	else
		return true;
}



/**
 * This function will check whether the password is at least 7 characters.
 * It will be called onkeyup.
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-03
 * @version 2010-06-03
 */
function checkPasswordChars()
{
	var charbox = document.getElementById('charcheck');
	var pw = document.getElementById('Password').value;
	charbox.style.fontSize = "12px";
	charbox.style.fontWeight = "lighter";
	if(pw.length < 7)
	{
		charbox.style.color = "#DD1111";
	}
	else
	{
		charbox.style.color = "green";
	}
	charbox.innerHTML = pw.length + "/7";
}



/**
 * This function will check whether the confirm password matches the 
 * original password.
 * It will be called onkeyup.
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-03
 * @version 2010-06-03
 */
function confirmPassword()
{
	var confirmbox = document.getElementById('confirmcheck');
	var cpw = document.getElementById('Password2').value;
	var pass = document.getElementById('Password').value;
	confirmbox.style.fontSize = "12px";
	confirmbox.style.fontWeight = "lighter";
	if(cpw == pass)
	{
		confirmbox.style.color = "green";
		confirmbox.innerHTML = "Matching";
	}
	else
	{
		confirmbox.style.color = "#DD1111";
		confirmbox.innerHTML = "Not Matching";
	}
}



/**
 * This function will prevent the enter key from submitting the form.
 * Requires you to check the "I agree" box in order to submit (in combination
 * with the function the shows the submit button when the box is checked.
 * 
 * @author Matthew A. Manspeaker <mamanspeaker@ethixsystems.com>
 * @since 2010-06-03
 * @version 2010-06-03
 */
function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     if(key == 13)
          return false;
     else
          return true;
}
