﻿/* Validation functions for no pref ctr */
/*  Allows single letter domains 5/2009 */

var defaultMessage = "Please fill in or correct the items highlighted in red.";
var highlighted = Array();


/* ---------  Validate fields for registration ----------- */

function validateSignup() {

	var blnErrors = false;
	var blnEmailFormatError = false;
	
	// if errorlabels exist, de-highlight
	unhighlight(highlighted);

	var errorlabels = Array();

	if (isEmptyField(txtFirstName)) {
		errorlabels.push(lblFirstName);
		blnErrors = true;
	}

	if (isEmptyField(txtLastName)) {
		errorlabels.push(lblLastName);
		blnErrors = true;
}
	

	if (isEmptyField(txtZip) || !isValidZipField(txtZip)) {
		errorlabels.push(lblZip);
		blnErrors = true;
}


// check bday and checkbox
if (document.getElementById(ddlBirthdayMonth)) {
	if (document.getElementById(ddlBirthdayMonth).value == "" || document.getElementById(ddlBirthdayDay).value == "") {
			blnErrors = true;
			errorlabels.push(lblBirthday);
	}
}
if (!document.getElementById(chkOver13).checked) {
    blnErrors = true;
    errorlabels.push(lblOver13);
}

		if (isEmptyField(txtEmail) || !isValidEmailField(txtEmail)) {
		    errorlabels.push(lblEmail);

		    if (!isEmptyField(txtEmail) && !isValidEmailField(txtEmail)) {
		        blnEmailFormatError = true;
		   
		    }
			
			
			blnErrors = true;
}

if (isEmptyField(txtConfirmEmail) || !isValidEmailField(txtConfirmEmail)) {
		    errorlabels.push(lblConfirmEmail);
			blnErrors = true;
		}

		if (document.getElementById(txtEmail).value != document.getElementById(txtConfirmEmail).value) {
		    errorlabels.push(lblConfirmEmail);
			blnErrors = true;
		}


		if (isEmptyField(txtPassword)) {
		    errorlabels.push(lblPassword);
		    blnErrors = true;
		}

		if (isEmptyField(txtConfirmPassword)) {
		    errorlabels.push(lblConfirmPassword);
		    blnErrors = true;
		}

		if (document.getElementById(txtPassword).value != document.getElementById(txtConfirmPassword).value) {
		    errorlabels.push(lblConfirmPassword);
		    blnErrors = true;
		}

		if (document.getElementById(ddlState).value == "" || document.getElementById(ddlStore).value == "") {
		    blnErrors = true;
		    errorlabels.push(lblState);
		}
		

	// Update error msg and highlight errorfields

	var errorTop = document.getElementById(divError);
	
	if (blnErrors) {

	    highlight(errorlabels);

	    errorTop.innerHTML = "Please complete or correct the fields highlighted in red to continue.  "
	
	if (blnEmailFormatError) {
	    errorTop.innerHTML += "Sorry, invalid email format. Make sure your email address has an @ and domain like .com or .net. "
	
	}
		
		errorTop.style.display = "block";
	
		return false;
	}
	else {
	    errorTop.style.display = "none";
		return true;
	}

} // end function validateSignup()




/* ---------  Validate login ----------- */

function validateLogin() {
    var strErrmsg = "";
    var blnErrors = false;
    var errorlabels = Array();

    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

if (isEmptyField(txtLoginEmail) || !isValidEmailField(txtLoginEmail)) {
    errorlabels.push(lblLoginEmail);
    blnErrors = true;
    strErrmsg += isEmptyField(txtLoginEmail) ? "Please enter your email address.  " : "Sorry, invalid email format. Make sure your email address has an @ and domain like .com or .net. ";
}
    
    if (isEmptyField(txtLoginPassword)) {
        strErrmsg += "Please enter your password.";
        errorlabels.push(lblLoginPassword);
        blnErrors = true;
    }
   

    var errorTop = document.getElementById(divLoginError);

    if (blnErrors) {
        highlight(errorlabels);
       
        errorTop.style.display = "block";
        errorTop.innerHTML = strErrmsg;
        return false;
    }
    else {
        errorTop.style.display = "none";
        return true;
    }

} // end function validateLogin()




/* ---------  Validate Forgot Password ----------- */

function validateForgotPassword() {
    var strErrmsg = "";
    var blnErrors = false;
    var errorlabels = Array();

    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    if (isEmptyField(txtForgotPasswordEmail) || !isValidEmailField(txtForgotPasswordEmail)) {
        errorlabels.push(lblForgotPasswordEmail);
        blnErrors = true;
        strErrmsg += isEmptyField(txtForgotPasswordEmail) ? "Please enter your email address.  " : "Sorry, invalid email format. Make sure your email address has an @ and domain like .com or .net. ";
    }

  
    //  alert("TEST: \n" + strErrmsg + "\n" + errorlabels);

    var errorTop = document.getElementById(divForgotPasswordError);

    if (blnErrors) {
        highlight(errorlabels);

        errorTop.style.display = "block";
        errorTop.innerHTML = strErrmsg;
        return false;
    }
    else {
        errorTop.style.display = "none";
        return true;
    }

} // end function validateForgotPassword()



/* ---------  Validate Refer Friends ----------- */

function validateRAF() {
   var errMsg = "";
   errMsg += checkRAFline(txtFirstName1, txtEmail1, 1);
   errMsg += checkRAFline(txtFirstName2, txtEmail2, 2);
   errMsg += checkRAFline(txtFirstName3, txtEmail3, 3);

    if (errMsg != "") {
        $('#' + errRefer).text(errMsg);
        $('#' + errRefer).removeClass('hidden');
        return false;
    } else {
        $(errRefer).addClass('hidden');
        return true;
    }
} // end function validateRAF()



function checkRAFline(first, email, line) {
    var errMsg = "";

   // If any names entered, require valid email.
   // Validate any emails are entered without names.

   var first = document.getElementById(first);
   var email = document.getElementById(email);
 
// If either is entered, make sure *both* are entered,  and email is valid.

   if (!isEmpty(first.value) || !isEmpty(email.value)) {
       if (isEmpty(first.value)) {
           errMsg += "Please enter a first name on line " + line + ". ";
       }
       if (isEmpty(email.value)) {
           errMsg += "Please enter a valid email address on line " + line + ". ";
       } else {
       if (!isValidEmail(email.value)) {
           errMsg += "Email address on line " + line + " is not valid. ";
       }
       }
 } // if either entered

    return errMsg;

} // eof checkRAFline()



/* ----------------------------------------------------------------------- */
/*        Misc util functions used by validation routines above            */
/* ----------------------------------------------------------------------- */



// returns selected value of radio button group


function getradioSelection(group) {

	for (var k = 0; k < group.length; k++) {
		if (group[k].checked) {
			return group[k].value;
		}
	}
}

// Prevents user from editing past max length of field (use on textareas)

function checkMaxLength(fld, maxlen) {
	return (fld.value.length <= maxlen)
}

function unhide(div) {document.getElementById(div).style.display = "block"; }

function hide(div) { document.getElementById(div).style.display = "none"; }


// highlight() -- displays given labels with highlighted class
function highlight(fields) {

	for (var label in fields) {
		if (document.getElementById(fields[label])) {
			document.getElementById(fields[label]).className += " highlighted";
		}
	}
	highlighted = fields; // remember what fields you just highlighted
}


// unhighlight() turns highlighting off: revert to pre-highlighted CSS class 

function unhighlight(fields) {

	var currentClass;
	var unhighlightedClass;

	for (var label in fields) {
		if (document.getElementById(fields[label])){
			currentClass = document.getElementById(fields[label]).className;
			unhighlightedClass = currentClass.replace(/highlighted/g, "");
			document.getElementById(fields[label]).className = unhighlightedClass;
		}
	}

} // end function unhighlight


function showPanelReset(thisPanel, panelsArray, errDiv) {
   // clear previous errors and highlighted labels
   document.getElementById(errDiv).style.display = "none";
   unhighlight(highlighted);
   showPanel(thisPanel, panelsArray);
    
} // end function showPanelReset()



function showPanel(thisPanel, panelsArray) {

    
    // Toggle on thisPanel and toggle off other panels in panelsArray
    var x;

    for (x in panelsArray) {
        if (panelsArray[x] == thisPanel) {
            document.getElementById(panelsArray[x]).style.display = "block";
        }
        else {
            document.getElementById(panelsArray[x]).style.display = "none";
        }
    }
} // end function showPanel()



// Show div if checkbox is checked

function showIfChecked(showDiv, checkBox) {
    var thisDiv = document.getElementById(showDiv);
    if (checkBox.checked) {
        thisDiv.style.display = "block";
    }
    else {
        thisDiv.style.display = "none";
    }
} // end function


// Show div if checkbox is NOT checked

function showIfUnchecked(showDiv, checkBox) {
    var thisDiv = document.getElementById(showDiv);
    if (checkBox.checked) {
        thisDiv.style.display = "none";
    }
    else {
        thisDiv.style.display = "block";
    }
} // end function




/* --------- Return hidden fields based on response (reg step 2) ----------- */

function getHiddenField(selectedResponse) {
    var hiddenField = "";

    switch (selectedResponse) {

        case "resp1a":
            hiddenField = "hdnOptLikeBestAboutUno";
            break;
        case "resp1b":
            hiddenField = "hdnOptLikeBestAboutUno";
            break;
        case "resp1c":
            hiddenField = "hdnOptLikeBestAboutUno";
            break;
        case "resp2a":
            hiddenField = "hdnOptHaveKids";
            break;
        case "resp2b":
            hiddenField = "hdnOptHaveKids";
            break;
        case "resp2c":
            hiddenField = "hdnOptHaveKids";
            break;
        case "resp3a":
            hiddenField = "hdnChkLunch";
            break;
        case "resp3b":
            hiddenField = "hdnChkDinner";
            break;
        case "resp3c":
            hiddenField = "hdnChkLateNight";
            break;
        case "resp4a":
            hiddenField = "hdnChkRestaurant";
            break;
        case "resp4b":
            hiddenField = "hdnChkBar";
            break;
        case "resp4c":
            hiddenField = "hdnChkToGo";
            break;
    }

    return (hiddenField);

}


/* ----------------------- Misc utils ----------------------- */

// takes OBJ as param
function isEmptyField(field) {

	chkfield = document.getElementById(field);
	
	if (isEmpty(chkfield.value)) {
		return true;
	}
	else {
		return false;
	}
}

// takes OBJ as param
function isNumericField(field) {
	chkfield = document.getElementById(field);
	if (isNaN(chkfield.value)) {
		return true;
	}
	else {
		return false;
	}
}



// takes STRING as parameter
function isEmpty(str) {
	if (str.length == 0) {
		return true;
	}
	else {
		return false;
	}
}


function isValidEmailOrPhone(str) {
	if (isValidEmail(str) || isValidPhone(str)) {
		return true;
	} else {
		return false;
	}
}

function isValidPhone(fld) {
	var phoneFilter = /^\+?1?[\( \-\.]*[0-9]{3}[\) \-\.]*[0-9]{3}[ \-\.]?[0-9]{4}$/;
	return (fld.match(phoneFilter) != null);
}

function isValidPhoneField(fld) {
	var str = document.getElementById(fld).value;

	var phoneFilter = /^\+?1?[\( \-\.]*[0-9]{3}[\) \-\.]*[0-9]{3}[ \-\.]?[0-9]{4}$/;
	return (str.match(phoneFilter) != null);
}

function isValidEmail(fld) {
	var emailFilter = /^[^+][+\-.\w]*@([\w\-+]+\.)+[\w]{2,}$/;
	return (fld.match(emailFilter) != null);
}

function isValidEmailField(fld) {
	var str = document.getElementById(fld).value;
	var emailFilter = /^[^+][+\-.\w]*@([\w\-+]+\.)+[\w]{2,}$/;
	return (str.match(emailFilter) != null);
}

function isValidZipField(fld) {
	var str = document.getElementById(fld).value;
	var emailFilter = /^[0-9]{5}$/;
	return (str.match(emailFilter) != null);
}

// Checks if return key was pressed
function checkreturn(e) {

	var characterCode;

	if (e && e.which) {
		e = e;
		characterCode = e.which; //NN4 which property
	}
	else {
		e = event;
		characterCode = e.keyCode //IE keyCode property
	}

	if (characterCode == 13) {   // if hit return/enter, submit form
		document.forms[0].submit();
		return false;
	}
	else {
		return true;
	}

}


/* ---------  display email options ----------- */

function openInstructions() {
    window.open('http://tools.89degrees.com/antispam/index.asp?app=unos', 'antispam', 'width=300,height=400,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no,titlebar=0,');
}
