function showText(boxname, text, errors) {
	if(errors){
		document.getElementById(boxname).innerHTML = "<img src='img/spacer.gif' width='1' height='20'>"+text+"";
	}else{
		document.getElementById(boxname).innerHTML = "";
	}												
}

function validateForm(f) {
var zipValid = true;
zipValid = isZip(f.elements['zipCode'].value);

if(!zipValid){ 
	showText("ziperror", "Please re-enter your zip, it must have 5 digits!", true);
	zipValid=false;
}
if(zipValid){
	showText("ziperror", "", false);
}

var emailValid = checkEmail(f.elements['emailAddress'].value);
if(!emailValid){
	showText("emailerror", "Please re-enter your email address, it must be valid!!", true);
}else{
	showText("emailerror", "", false);
}

var ageValid = f.elements['age'].checked;
if(!ageValid){
	showText("ageerror", "You must be 18 to receive notifications!!", true);
}else{
	showText("ageerror", "", false);
}

return (zipValid && emailValid && ageValid);
}

function validateFormZipCode(f) {
var zipValid = true;

zipValid = isZip(f.elements['zipCode'].value);
if(!zipValid){ 
	showText("ziperror", "Please enter a 5-digit zip code, such as 12345.", true);
	zipValid=false;
}
if(zipValid){
	showText("ziperror", "", false);
}

return (zipValid);
}

function validateFormZipCodeProdIndex(f) {
var zipValid = true;

zipValid = isZip(f.elements['zipCode'].value);
if(!zipValid){ 
	alert("Please enter a 5-digit zip code, such as 12345.");
	showText("ziperror", "", false);
	zipValid=false;
}
if(zipValid){
	showText("ziperror", "", false);
}

return (zipValid);
}


function validateFormEmailIs18(f) {

var emailValid = checkEmail(f.elements['emailAddress'].value);
if(!emailValid){
	showText("emailerror", "Please enter a valid email address.", true);
	showText("emailerror2", "Please enter a valid email address.", true);
}else{
	showText("emailerror", "", false);
	showText("emailerror2", "", false);	
}

var ageValid = f.elements['is18'].checked;
if(!ageValid){
	showText("ageerror", "Sorry, you must be over 18 to receive email notifications.", true);
	showText("ageerror2", "Sorry, you must be over 18 to receive email notifications.", true);	
}else{
	showText("ageerror", "", false);
	showText("ageerror2", "", false);
}

var tooMany = validateNoMoreThanThree();

return (emailValid && ageValid && tooMany);
}


function validateFormEmail(f) {

var emailValid = checkEmail(f.elements['emailAddress'].value);
if(!emailValid){
	showText("emailerror", "Please enter a valid email address.", true);
}else{
	showText("emailerror", "", false);
}

return (emailValid);
}

function validateNoMoreThanSix() {

var elements = document.forms[0].elements.length;
var count = 0;
var totalChecked = 0;
var tooMany = false;
var tooFew = false;
while(count < elements){
	var element = document.forms[0].elements[count];
	if(element.type == 'checkbox'){
		if(element.checked){
			totalChecked++;
		}
	}
	count++;
}

if(totalChecked > 6){
	showText("toomany", "Please select no more than 6 flavors at a time.", true);
	showText("toomany2", "Please select no more than 6 flavors at a time.", true);
	tooMany = false;
}else{
	showText("toomany", "", false);
	showText("toomany2", "", false);
	tooMany = true;
}
if(totalChecked < 1){
	showText("toofew", "Please select at least one flavor.", true);
	showText("toofew2", "Please select at least one flavor.", true);
	tooFew = false;
}else{
	showText("toofew", "", false);
	showText("toofew2", "", false);
	tooFew = true;
}


return (tooMany && tooFew);
}

function validateNoMoreThanThree() {
var a = new Array();

var elements = document.forms[0].elements.length;
var count = 0;
var count2 = 0;
var totalChecked = 0;
var tooMany = false;
var tooFew = false;
var foundIt = false;
var lastProdIdSub = "";
while(count < elements){
	var element = document.forms[0].elements[count];
	if(element.type == 'checkbox'){
		if(element.name != 'is18'){
			if(element.checked){
				var prodName = element.name;
				var secUnder = prodName.lastIndexOf("_");
				var prodIdSub = prodName.substring(1, secUnder);
				if(prodIdSub != lastProdIdSub){
					count2 = 1;
				}else{
					count2++;
				}
				if(count2 > 3){
					foundIt = true;
				}
				lastProdIdSub = prodIdSub;
				totalChecked++;
			}		
		}
	}
	count++;
}

if(foundIt){
	showText("toomany", "Please select no more than 3 stores per flavor at a time.", true);
	showText("toomany2", "Please select no more than 3 stores per flavor at a time.", true);
	tooMany = false;
}else{
	showText("toomany", "", false);
	showText("toomany2", "", false);
	tooMany = true;
}
if(totalChecked < 1){
	showText("toofew", "Please select at least one store.", true);
	showText("toofew2", "Please select at least one store.", true);
	tooFew = false;
}else{
	showText("toofew", "", false);
	showText("toofew2", "", false);
	tooFew = true;
}


return (tooMany && tooFew);
}


function validateFormCancelEmailAtLeast1() {

var elements = document.forms[0].elements.length;
var count = 0;
var totalChecked = 0;
var tooMany = false;
while(count < elements){
	var element = document.forms[0].elements[count];
	if(element.type == 'checkbox'){
		if(element.checked){
			totalChecked++;
		}		
	}
	count++;
}

if(totalChecked < 1){
	showText("toofew", "Please select at least one notification to cancel.", true);
	tooMany = false;
}else{
	showText("toofew", "", false);
	tooMany = true;
}

return tooMany;
}


function validateFormZipCodeAndTooMany(f) {
var zipValid = true;
zipValid = isZip(f.elements['zipCode'].value);
if(!zipValid){ 
	showText("ziperror", "Please enter a 5-digit zip code, such as 12345.", true);
	showText("ziperror2", "Please enter a 5-digit zip code, such as 12345.", true);
	zipValid=false;
}
if(zipValid){
	showText("ziperror", "", false);
	showText("ziperror2", "", false);
}

var tooMany = validateNoMoreThanSix();
return (zipValid && tooMany);
}




function validateFormZipCodeAndOneFlavor(f) {
var zipValid = true;
var iValue = parseInt(f.elements['zipCode'].value);

zipValid = isZip(f.elements['zipCode'].value);
if(!zipValid){ 
	showText("ziperror", "Please enter a 5-digit zip code, such as 12345.", true);
	zipValid=false;
}
if(zipValid){
	showText("ziperror", "", false);
}

var flavorValid = true;
if(f.elements['flavor'].value.length < 2)
{
	showText("flavorerror", "Please select a flavor.", true);
	flavorValid = false;
}else{
	showText("flavorerror", "", false);
	flavorValid = true;
}

return (zipValid&&flavorValid);
}

function isZip(s) 
{
 
     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
 
     if (!reZip.test(s)) {
          return false;
     }
 
return true;
}



    /**
     * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
     * http://javascript.internet.com
     */
    function checkEmail(emailStr) {

       if (emailStr.length == 0) {
           return false;
       }
       var emailPat=/^(.+)@(.+)$/;
       var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
       var validChars="\[^\\s" + specialChars + "\]";
       var quotedUser="(\"[^\"]*\")";
       var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
       var atom=validChars + '+';
       var word="(" + atom + "|" + quotedUser + ")";
       var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
       var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
       var matchArray=emailStr.match(emailPat);
       if (matchArray == null) {
           return false;
       }
       var user=matchArray[1];
       var domain=matchArray[2];
       if (user.match(userPat) == null) {
           return false;
       }
       var IPArray = domain.match(ipDomainPat);
       if (IPArray != null) {
           for (var i = 1; i <= 4; i++) {
              if (IPArray[i] > 255) {
                 return false;
              }
           }
           return true;
       }
       var domainArray=domain.match(domainPat);
       if (domainArray == null) {
           return false;
       }
       var atomPat=new RegExp(atom,"g");
       var domArr=domain.match(atomPat);
       var len=domArr.length;
       if ((domArr[domArr.length-1].length < 2) ||
           (domArr[domArr.length-1].length > 3)) {
           return false;
       }
       if (len < 2) {
           return false;
       }
       return true;
    }
    
function valNumBoxesChecked(inCheckboxname, inMin, inMax, inErrorDiv, inNoun, inNounPlural) {
        var selCount = 0;
        var errMsg = null;
        var noun ="flavor";
        var nounPlural ="flavors";
        if (inNoun != null) noun = inNoun;
        if (inNounPlural != null) nounPlural = inNounPlural;
        if (typeof(inCheckboxname) == 'object') {
            for (var i=0; i<inCheckboxname.length; i++) {
                if (inCheckboxname[i].checked == true) { selCount++; }
            }
        } else {
            if (theForm.elements[inCheckboxname].length) {
                for (var i=0; i<theForm.elements[inCheckboxname].length; i++) {
                    if (theForm.elements[inCheckboxname][i].checked == true) { selCount++; }
                }
            } else {
                if (theForm.elements[inCheckboxname].checked == true) { selCount++; }
            }
        }
        if (selCount < inMin) { 
            errMsg = 'Please select at least '+inMin+' '+noun+'.'; 
        }
        if (selCount > inMax) {
            errMsg = 'Please select no more than '+inMax+' '+nounPlural+'.'; 
        }
        if (errMsg) {
            addErrorToASPNETForm(inErrorDiv, errMsg);
            return false;
        }
        return true;
    }
    
function findDiv_IDEndingWith(inSearchString, inTagType) {
    var tagType = inTagType;
    if (tagType == null) { tagType = "div"; }
    var divCollection = document.getElementsByTagName(tagType);
    for (var i=0; i<divCollection.length; i++) {
        if(divCollection[i].getAttribute("id") && divCollection[i].getAttribute("id").indexOf(inSearchString) ==  divCollection[i].getAttribute("id").length - inSearchString.length) {
            return divCollection[i];
        } 
    }
}

function findTags_IDEndingWith(inSearchString, inTagType) {
    var tagType = inTagType;
    if (tagType == null) { tagType = "div"; }
    var divCollection = document.getElementsByTagName(tagType);
    var resultCollection = new Array();
    for (var i=0; i<divCollection.length; i++) {
        if(divCollection[i].getAttribute("id") && divCollection[i].getAttribute("id").indexOf(inSearchString) ==  divCollection[i].getAttribute("id").length - inSearchString.length) {
            resultCollection.push(divCollection[i]);
        } 
    }
    return resultCollection;
}

function addErrorToASPNETForm(inErrorDiv, inMsg) {
    var errDiv = findDiv_IDEndingWith(inErrorDiv);
    if (errDiv) {
        if (errDiv.innerHTML.indexOf(inMsg)<0) { errDiv.innerHTML += inMsg+'<br />'; }
        if (errDiv.style.display) errDiv.style.display = 'block';
    }
}