// JScript source code
//From old event module code
function form_validator(theForm){
    var objRegExpNames  = /[><]/;    
    var objRegExpZip  = /(^\d{5}$)/;
    
    if (trimField(theForm.FName.value) == '') {
        alert('Please enter your First Name.');
        theForm.FName.value = '';
        theForm.FName.focus();
        return(false);
    }  
    
    if (objRegExpNames.test(theForm.FName.value)) {
        alert('The characters ">" and "<" are not permitted. Please re-enter your First Name.');
        theForm.FName.value = '';
        theForm.FName.focus();
        return(false);
    }  

    if (trimField(theForm.LName.value) == '') {
        alert('Please enter your Last Name.');
        theForm.LName.value = '';
        theForm.LName.focus();
        return(false);
    }   
    
    if (objRegExpNames.test(theForm.LName.value)) {
        alert('The characters ">" and "<" are not permitted. Please re-enter your Last Name.');
        theForm.LName.value = '';
        theForm.LName.focus();
        return(false);
    }   
    
    if (trimField(theForm.Email.value) == '') {
        alert('Please enter your Email Address.');
        theForm.Email.value = '';
        theForm.Email.focus();
        return(false);
    }  
    
    if (!CheckEmail(theForm.Email.value.toLowerCase())) {
        alert('Please enter a valid Email Address.');
        theForm.Email.value = '';
        theForm.Email.focus();
        return(false);
    }  
    
    if (!objRegExpZip.test(theForm.PostalCode.value)) {
        alert('Please enter a valid 5-digit ZIP code.');     
        theForm.PostalCode.value = '';
        theForm.PostalCode.focus();
        return(false);
    }  

    //birthdate validation start
    if (theForm.yyyy.value == '') {
        alert('Please enter your birth year.');
        theForm.yyyy.focus();
        return(false);
    }  
    if (theForm.mm.value == '') {
        alert('Please enter your birth month.');
        theForm.mm.focus();
        return(false);
    }  
    if (theForm.dd.value == '') {
        alert('Please enter your birth day.');
        theForm.dd.focus();
        return(false);
    }  
    thedate = new Date();
    cur_mm = thedate.getMonth() + 1;
    cur_dd = thedate.getDate();
    cur_yyyy = thedate.getYear();
    if (cur_yyyy < 200)
        cur_yyyy += 1900;
    yourage = cur_yyyy - theForm.yyyy.value;
    if (cur_mm < theForm.mm.value) 
        yourage--;
    if ((cur_mm == theForm.mm.value) && (cur_dd < theForm.dd.value)) 
        yourage--;
    if (yourage < 13) {
        alert('You must be at least 13 years old to participate');
        return(false);
    }  
    if ((theForm.mm.value == 4 || theForm.mm.value == 6 || theForm.mm.value == 9 || theForm.mm.value == 11) && theForm.dd.value == 31) {
        alert('Invalid date. (only 30 days in that month)');
        theForm.dd.focus();
        return(false);
    }  
    if (theForm.mm.value == 2 && theForm.dd.value > 29) {
        alert('Invalid date. (only 28 or 29 days in Feb)');
        theForm.dd.focus();
        return(false);
    }  
    if (theForm.mm.value == 2 && theForm.dd.value == 29 && !(theForm.yyyy.value % 4 == 0 && (theForm.yyyy.value % 100 != 0 || theForm.yyyy.value % 400 == 0))) {
        alert('Invalid date. (not a leap year)');
        theForm.dd.focus();
        return(false);
    }  
    //birthdate validation end

    //check all questions (in select menus)
    var allAnswered = true;
    for (var i=0; i < theForm.elements.length; i++ ) {
        if ((theForm.elements[i].type == 'select-one') && (theForm.elements[i].name != "Gender")) {
            if (theForm.elements[i].value == '') {
                allAnswered = false;
                break;
            }
        }
    }
    if (!allAnswered) {
        alert('Please answer all questions.');
        theForm.elements[i].focus();
        return(false);
    } 

    if (!checkCheckboxes(theForm,"BrandsUsed",1)) {
        alert('Please select the brands you use.');
        theForm.BrandsUsed[0].focus();
        return(false); 
    }           

    // if Jergens is checked, find out which brands they use
    //if (theForm.BrandsUsed[3].checked) {
        if (!checkCheckboxes(theForm,"ProductsUsed",1)) {
            alert('Please indicate which JergensŪ product(s) you use.');
            theForm.ProductsUsed[0].focus();
            return(false);        
        }           
    //}
    
    // this brand's optin box needs to be checked
    if (!theForm.optin.checked) {
        alert('If you want to become a Jergens Brand Member, please check Yes!');
        return(false);
    }
}
    
// multi-brand opt-in checkboxes
window.onload = function() {
	var theOptionCheckboxes = [];
	var theInputs = document.getElementsByTagName('input');
	document.getElementById('multioptin').checked = 0;
	for (var j = 0; j < theInputs.length; j++){
		if (theInputs[j].className == 'optin'){
			theInputs[j].checked = 0;
			theInputs[j].disabled = 1;	}}}
function turnOffAll() {
if (this.checked == 1) {
var allOff = 1;
var theInputs = document.getElementsByTagName('input');	
for (var i=0; i<theInputs.length; i++){
if(theInputs[i].className == 'optin' && theInputs[i].checked == 1) {allOff = 0;}}
if (allOff == 1) {document.getElementById('multioptin').checked = 0;toggleCheckboxes();document.getElementById('multioptin').className = 'click';}}}
function toggleCheckboxes() {
	var theOptionCheckboxes = [];
	var theInputs = document.getElementsByTagName('input');
	var k = 0;
	for (var j = 0; j < theInputs.length; j++){
		if (theInputs[j].className == 'optin'){
			theOptionCheckboxes[k] = theInputs[j];
			k++; }	}
	if (this.checked == 1) {
		for(var i = 0; i<theOptionCheckboxes.length; i++){
			theOptionCheckboxes[i].disabled = 1;}
		this.checked = 0;}
	else {
		for(var i = 0; i<theOptionCheckboxes.length; i++){
			theOptionCheckboxes[i].disabled = 0;
			if (this.className != 'click' || document.getElementById('multioptin').className == 'click') {theOptionCheckboxes[i].checked = 1;}	}
		this.checked = 1;
		this.className = 'click';document.getElementById('multioptin').className = '';}
}

function checkCheckboxes (f,name,require) {
    var checked = 0, e, i = 0;
    while (e = f.elements[i++]) {
        if (e.type == 'checkbox' && e.name == name && e.checked) 
            checked++;
    }
    return require <= checked
}

function trimField(fld) {
    // removes leading and trailing blanks from fld
    var res = "";
    var c = 0;
    for (i=0; i < fld.length; i++) {
      if (fld.charAt(i) != " " || c > 0) {
        res += fld.charAt(i);
        if (fld.charAt(i) != " ") c = res.length;
      }
    }
    return res.substr(0,c);
}
