function clearInput(ctlName, text)
{
	if (document.getElementById(ctlName).value == text)
		document.getElementById(ctlName).value='';
}

function checkInputBlur(ctlName, text)
{
	if (document.getElementById(ctlName).value.length == 0){document.getElementById(ctlName).value=text;}
}

/* Apply Text box CSS and Error Text Box CSS */

function resetTextBoxCSS(object)
{
	//object.style.border='1px solid #7f9db9';
	object.style.backgroundColor='#ffffff';
}
function applyErrorTextBoxCSS(object)
{
	//object.style.border='1px solid red';
	object.style.backgroundColor='#FFDDDD';
}

function validateForm(check, txtName, txtEmail, dob, role)
{
    errorDetails = '';
	errorControl = '';
	
	txtName = document.getElementById(txtName);
	txtEmail = document.getElementById(txtEmail);
	dob = document.getElementById(dob);
	role = document.getElementById(role);
		
	resetTextBoxCSS(txtName);
	resetTextBoxCSS(txtEmail);
	resetTextBoxCSS(dob);
	resetTextBoxCSS(role);
	
	if (txtName.value == "" || txtName.value == "*Name")
	{
		applyErrorTextBoxCSS(txtName);
		if (errorDetails == '')
		{
			errorControl = txtName;
		}
		errorDetails = errorDetails + " - The Name field cannot be blank\n";		
	}	
	if (txtEmail.value == "" || txtEmail.value == "*Email")
	{
		applyErrorTextBoxCSS(txtEmail);
		if (errorDetails == '')
		{
			errorControl = txtEmail;
		}
		errorDetails = errorDetails + " - The Email field cannot be blank\n";
	}
	else if (isValidEmail(txtEmail.value)==false)
	{
		applyErrorTextBoxCSS(txtEmail);
		if (errorDetails == '')
		{
			errorControl = txtEmail;
		}
		errorDetails = errorDetails + " - Please enter a valid email address\n";
	}	
	if (dob.value == "" || dob.value == "*D.O.B (dd/mm/yyyy)")
	{
		applyErrorTextBoxCSS(dob);
		if (errorDetails == '')
		{
			errorControl = dob;
		}
		errorDetails = errorDetails + " - The Date of Birth field cannot be blank\n";		
	}
	else if (! (isValidDate(dob.value)))
	{
		applyErrorTextBoxCSS(dob);
		if (errorDetails == '')
		{
			errorControl = dob;
		}
		errorDetails = errorDetails + " - The Date of Birth is not valid\n";		
	}
	if (role.value == "")
	{
		applyErrorTextBoxCSS(role);
		if (errorDetails == '')
		{
			errorControl = role;
		}
		errorDetails = errorDetails + " - Select your role\n";		
	}
	
	
	if (errorDetails == '')
	{
		return true;
	}
	else
	{
		if (errorControl != '')
			errorControl.focus();
			
		alert('To submit the form you need to enter the following mandatory fields \n' + errorDetails);
		return false;
	}


}


/* is a Valid Email */
function isValidEmail (str)
{
    // Return immediately if an invalid value was passed in
    if (str+"" == "undefined" || str+"" == "null")
    return false;

    else
    {
    if (str+"" == "")
    return true;  // email is not required
    else

    str += "";
    namestr = str.substring(0, str.indexOf("@"));  // everything before the '@'
    domainstr = str.substring(str.indexOf("@")+1, str.length); // everything after the '@'

    // Rules:
    // namestr must contain something before the '@'
    // domainstr must contain a period that is not the first character (i.e. right after
    // the '@').
    if ((namestr.length == 0) || (domainstr.indexOf(".") <= 0) || (domainstr.indexOf("@") != -1))
    return false;
    }

    return true;
}


function isValidDate(dateStr) {
    // Checks for the following valid date formats:
    // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
    // Also separates date into month, day, and year variables

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

    // To require a 4 digit year entry, use this line instead:
    // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

    var matchArray = dateStr.match(datePat); // is the format ok?
    if (matchArray == null) {
        //return ("Date is not in a valid format.")
        return false;
    }
    month = matchArray[3]; // parse date into variables
    day = matchArray[1];
    year = matchArray[4];
    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }
    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        return false
    }
    if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap)) {
        //alert("February " + year + " doesn't have " + day + " days!");
        return false;
        }
    }
    return true;  // date is valid
}
