<!--
//=======================
//Author:		Dan Pettit
//Date Created:	02/08/01
//Description:	This page has all Javascript functions for the site
//========================

		
	function emailCheck (emailStr,msgStr) {
		/* Set up our error message */
		var errMsg="Please double check " + msgStr + " email address.\n";
		/* The following pattern is used to check if the entered e-mail address fits the user@domain format.  It also is used to separate the username from the domain. */
		var emailPat=/^(.+)@(.+)$/;
		/* The following string represents the pattern for matching all special characters.  We don't want to allow special characters in the address.  These characters include ( ) < > @ , ; : \ " . [ ] */
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
		/* The following string represents the range of characters allowed in a  username or domainname.  It really states which chars aren't allowed. */
		var validChars="\[^\\s" + specialChars + "\]";
		/* The following pattern represents the range of characters allowed as the first character in a valid username or domain. I just made it  the same as above, but if you want to add a different constraint, you would change it here. */
		var firstChars=validChars;
		/* The following pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com is a legal e-mail address. */
		/* var quotedUser="(\"[^\"]*\")"; */
		/* The following pattern applies for domains that are IP addresses,   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal e-mail address. NOTE: The square brackets are required. */
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		/* The following string represents at atom (basically a series of  non-special characters.) */
		var atom="(" + firstChars + validChars + "*" + ")";
		/* The following string represents one word in the typical username.  For example, in john.doe@somewhere.com, john and doe are words.  Basically, a word is either an atom or quoted string. */
		/* var word="(" + atom + "|" + quotedUser + ")"; */
		var word= atom;
		// The following pattern describes the structure of the user
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		/* The following pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom+")*$");
		/* Finally, let's start trying to figure out if the supplied address is valid. */
		
		/* Begin with the course pattern to simply break up user@domain into different pieces that are easy to analyze. */
		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
		  /* Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address. */
			alert(errMsg + "Email address seems incorrect (check @ and .\'s)");
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];

		// See if "user" is valid 
		if (user.match(userPat)==null) {
		    // user is not valid
		    alert(errMsg + "The e-mail username doesn't seem to be valid.");
		    return false;
		}
		/* if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
  		  // this is an IP address
  		for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        alert(errMsg + "Destination IP address is invalid!");
						return false;
				}
			}
			return true;
		}

		// Domain is symbolic name
		var domainArray=domain.match(domainPat);
		
		if(emailStr.indexOf("@.")>-1) {
			alert(errMsg + "The domain name doesn't seem to be valid.");
	    return false;
		}

		if (domainArray[0].length<4) {
			alert(errMsg + "The domain name doesn't seem to be valid.");
	    return false;
		}
		/* domain name seems valid, but now make sure that it ends in a three-letter word (like com, edu, gov) or a two-letter word,   representing country (uk, nl).   If there's a country code at the end of the address, the full domain  must include a hostname and category (e.g. host.co.uk or host.pub.nl).  If it ends in a .com or something, make sure there's a hostname.*/
		/* Now we need to break up the domain to get a count of how many atoms  it consists of. */
		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) {
		   // the address must end in a two letter or three letter word.
		   alert(errMsg + "The address must end in a three-letter domain, or two letter country.");
		   return false;
		}

		/* If it just ends in .com, .gov, etc., make sure
		there's a host name.
   This case can never actually happen because earlier
		checks take
		 care of this implicitly, but we'll do it anyway. */
		if (domArr[domArr.length-1].length==3 && len<2) {
			var errStr="This address is missing a hostname!";
			alert(errMsg + errStr);
			return false;
		}
		// If we've gotten this far, everything's valid!
		return true;
	}

	function validateZipCode(strZip) {
			var valid = "0123456789-";
			var hyphencount = 0;

			if (strZip.length!=5 && strZip.length!=9 && strZip.length!=10) {
				alert("Please enter a properly formatted 5 digit or 5 digit + 4 zip code, like '12345' or '12345-6789'.");
				return false;
			}
			
			for (var i=0; i < strZip.length; i++) {
				temp = "" + strZip.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") {
					alert("Please enter a properly formatted 5 digit or 5 digit + 4 zip code, like '12345' or '12345-6789'.");
					return false;
				}
				if ((hyphencount > 1) || (strZip.length==9) || ((strZip.length==10) && ""+strZip.charAt(5)!="-")) {
					alert("The hyphen character should be used with a properly formatted 5 digit+4 zip code, like '12345-6789'.   Please try again.");
					return false;
				}
			}
			return true;
		}

	function validateAge(month, day, year, minYears)	{
		var now = new Date();
		var birthday = new Date(year, (month-1), day);	// months go from 0-11
		
		if ((!month) || (!day) || (!year))	{
			return (false);
		}
		
		if ((month>12) || (day>31) || (year<1900) || (month<1) || (day<1))	{
			return (false);
		}
 
		// we don't have the hour/minute/second the user was born, so assume midnight for both
		now.setHours(0);
		now.setMinutes(0);
		now.setSeconds(0);
		
		// minAge is today minus x number of years
		// userAge is the user's current age
		var minAge	= now.getTime() - (minYears * 365 * 24 * 60 * 60 * 1000);
		var userAge	= birthday.getTime();
		
		// leap year nonsense
		// for some unknown reason we need to add all the missed feb. 29's back into the user's age
		var numLeapYears = Math.floor( (now.getFullYear() - birthday.getFullYear()) / 4 );
		userAge = userAge + (numLeapYears * 24 * 60 * 60 * 1000);
		
		// if the earliest allowed day comes after the user's birthday, they're OK
		if (( minAge > userAge ))	{
			return (true);
		}
		
		// sorry, kid
		return (false);
	}


//**********************************
// This function opens the contest popup window (used on Contest Window Popup)
	function Popup(page) {
	OpenWin = this.open(page, "Contest", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=570,height=600,screenX=100,screenY=100,top=100,left=100");
	}

//Script is for popup windows - (Bug Report Page)
	var newWin;
	function ShowDoc(url) {
		if (url != "") {
			if (newWin) {
				if (!newWin.closed) {
					newWin.location=url;
				} else {
					newWin=window.open(url,"newWin","statusbar,resizable,height=400,width=585,scrollbars");
				}	
				newWin.focus();
			} else {
				newWin=window.open(url,"newWin","statusbar,resizable,height=400,width=585,scrollbars");
				newWin.focus();
			}
		}
		return (true);
	}


//******************************************************
function isBlank(s) {
	for (var i=0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function isNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
   
//****************************************************

//**This Function validates form on postcardmain.asp

//*********************************
function checkForm(frmMain) {
	var errs = ''
	var checkedoff = ''
	if (isBlank(frmMain.elements.name1.value)) errs += 'Your Name\r'
	if (isBlank(frmMain.elements.emailaddress.value)) errs += 'Your E-Mail\r'
	if (isBlank(frmMain.elements.RName1.value)) errs += 'Recipient\'s Name\r'
	if (isBlank(frmMain.elements.RemailAddress.value)) errs += 'Recipient\'s E-Mail\r'
	//Check to see if the R1BrandID field is represented as a drop-list
	if (frmMain.elements.R1BrandID.options) {
		if (frmMain.elements.R1BrandID.options[frmMain.elements.R1BrandID.selectedIndex].value == "0") errs += 'Recipient\'s State of Residence\r'	
	}	
	 if (emailCheck(frmMain.emailaddress.value," your ") == false) {
	  frmMain.emailaddress.focus();
	  return (false);
	 }
	
	 if (emailCheck(frmMain.RemailAddress.value," your friend\'s ") == false) {
	  frmMain.RemailAddress.focus();
	  return (false);
	 }

	
	  //Set the postcard template id
	  frmMain.postcardTemplateID.value=arrPostcardTemplates[iImgIndex].id;
	  //Set the postcard image tag (only used by the preview page)
	  frmMain.postcardImageTag.value=arrPostcardTemplates[iImgIndex].imageTag;

	
	if (errs!='') errs = 'You left the following required fields blank:\r' + errs
	if (errs!='') { alert(errs); return false } else return true
}


//****************************************	  

//**This Function validates form on contact.asp

//*********************************
function checkRadio(button) {
	var radioValue = null;
	
	for (var i=0; i <button.length ; i++) {
		if (button[i].checked) {
			radioValue = button[i].value;
			break;
		}
	}

	if (radioValue == null) {
		return false;
	} else {
		return true;
	}	
}


//****************************************************

//**This Function validates form on flavorsurvey.asp

//*********************************
function checkSurvey(frmMain) {
	var errs = ''
	var checkedoff = ''
	if (checkRadio(frmMain.elements.flavor1) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor2) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor3) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor4) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor5) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor6) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor7) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor8) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor9) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor10) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor11) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor12) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor13) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor14) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor15) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor16) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor17) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor18) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor19) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor20) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor21) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor22) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor23) == false) errs += '1'
	if (checkRadio(frmMain.elements.flavor24) == false) errs += '1'


	if (errs!='') {
		alert('Please give us your opinion of all of the flavors.');
		return (false);
	}


	if (frmMain.elements.fave1.options[frmMain.elements.fave1.selectedIndex].value == '0') {
		alert('Please tell us your favorite flavor.');
		frmMain.fave1.focus();
		return (false);
	}

	if (frmMain.elements.fave2.options[frmMain.elements.fave2.selectedIndex].value == '0') {
		alert('Please tell us your second favorite flavor.');
		frmMain.fave2.focus();
		return (false);
	}

	if (frmMain.elements.fave3.options[frmMain.elements.fave3.selectedIndex].value == '0') {
		alert('Please tell us your third favorite flavor.');
		frmMain.fave3.focus();
		return (false);
	}

	if (frmMain.elements.fave4.options[frmMain.elements.fave4.selectedIndex].value == '0') {
		alert('Please tell us your fourth favorite flavor.');
		frmMain.fave4.focus();
		return (false);
	}

	if (frmMain.elements.fave1.options[frmMain.elements.fave1.selectedIndex].value == frmMain.elements.fave2.options[frmMain.elements.fave2.selectedIndex].value) {
		alert('Your favorite and second favorite flavors can\'t be the same.');
		frmMain.fave1.focus();
		return (false);
	}

	if (frmMain.elements.fave1.options[frmMain.elements.fave1.selectedIndex].value == frmMain.elements.fave3.options[frmMain.elements.fave3.selectedIndex].value) {
		alert('Your favorite and third favorite flavors can\'t be the same.');
		frmMain.fave1.focus();
		return (false);
	}

	if (frmMain.elements.fave1.options[frmMain.elements.fave1.selectedIndex].value == frmMain.elements.fave4.options[frmMain.elements.fave4.selectedIndex].value) {
		alert('Your favorite and fourth favorite flavors can\'t be the same.');
		frmMain.fave1.focus();
		return (false);
	}

	if (frmMain.elements.fave2.options[frmMain.elements.fave2.selectedIndex].value == frmMain.elements.fave3.options[frmMain.elements.fave3.selectedIndex].value) {
		alert('Your second and third favorite flavors can\'t be the same.');
		frmMain.fave2.focus();
		return (false);
	}

	if (frmMain.elements.fave2.options[frmMain.elements.fave2.selectedIndex].value == frmMain.elements.fave4.options[frmMain.elements.fave4.selectedIndex].value) {
		alert('Your second and fourth favorite flavors can\'t be the same.');
		frmMain.fave2.focus();
		return (false);
	}

	if (frmMain.elements.fave3.options[frmMain.elements.fave3.selectedIndex].value == frmMain.elements.fave4.options[frmMain.elements.fave4.selectedIndex].value) {
		alert('Your third and fourth favorite flavors can\'t be the same.');
		frmMain.fave3.focus();
		return (false);
	}

	if (frmMain.elements.gender.options[frmMain.elements.gender.selectedIndex].value == '') {
		alert('Please tell us your gender.');
		frmMain.gender.focus();
		return (false);
	}

	if (frmMain.elements.age.options[frmMain.elements.age.selectedIndex].value == '') {
		alert('Please tell us your age.');
		frmMain.age.focus();
		return (false);
	}

	if (frmMain.elements.state.options[frmMain.elements.state.selectedIndex].value == '') {
		alert('Please tell us your state.');
		frmMain.state.focus();
		return (false);
	}

	if (frmMain.elements.household.options[frmMain.elements.household.selectedIndex].value == '') {
		alert('Please tell us how many are in your household.');
		frmMain.household.focus();
		return (false);
	}

	return true
}


function checkContactForm(contactfrm) {
	var errs = ''
	var checkedoff = ''
	
	if (contactfrm.elements.subjectid.options[contactfrm.elements.subjectid.selectedIndex].value == "0") errs += 'This message is in reference to\n'
	if (isBlank(contactfrm.elements.Name1.value)) errs += 'First Name\n'
	if (isBlank(contactfrm.elements.Name2.value)) errs += 'Last Name\n'
	if (isBlank(contactfrm.elements.Address1.value)) errs += 'Address\n'
	if (contactfrm.elements.State.options[contactfrm.elements.State.selectedIndex].value == "0") errs += 'State\n'
	if (isBlank(contactfrm.elements.City.value)) errs += 'City\n'
	if (isBlank(contactfrm.elements.ZipCode.value)) errs += 'Zip/Postal Code\n'
	if (isBlank(contactfrm.elements.EmailAddress.value)) errs += 'E-Mail\n'
	if (isBlank(contactfrm.elements.Message.value)) errs += 'Type your question or comment here\n'
	//if (checkRadio(contactfrm.elements.over18)==false) errs += 'Please indicate whether you are over or under 18 years old.\n'		
		
	 if (emailCheck(contactfrm.EmailAddress.value,"") == false) {
	  contactfrm.EmailAddress.focus();
	  return (false);
	 }
	 
	var commentText = contactfrm.elements.Message.value;
	if (commentText.length > 1000)
	{
		alert('Your question or comment is too long.\n Please limit your response to 1000 characters.');
		contactfrm.Message.focus()
		return (false);
	}

	if (errs!='') errs = 'You left the following field(s) blank:\n' + errs + 'Please complete the form and resubmit.'
	if (errs!='') { alert(errs); return false } else return true
}

function checkETContactForm(contactfrm) {
	var errs = ''
	var checkedoff = ''
	if (isBlank(contactfrm.elements.Name1.value)) errs += 'First Name\r'
	if (isBlank(contactfrm.elements.Name2.value)) errs += 'Last Name\r'
	if (isBlank(contactfrm.elements.Address1.value)) errs += 'Address\r'
	if (isBlank(contactfrm.elements.City.value)) errs += 'City\r'
	if (isBlank(contactfrm.elements.ZipCode.value)) errs += 'Postal Code\r'
	if (isBlank(contactfrm.elements.EmailAddress.value)) errs += 'E-Mail\r'
	if (isBlank(contactfrm.elements.Message.value)) errs += 'Type your question or comment here\r'
	if (checkRadio(contactfrm.elements.over18)==false) errs += 'Please indicate whether you are over or under 18 years old.\r'		
		
	 if (emailCheck(contactfrm.EmailAddress.value,"") == false) {
	  contactfrm.EmailAddress.focus();
	  return (false);
	 }
	 
	 if (validateZipCode(contactfrm.ZipCode.value) == false) {
	  contactfrm.ZipCode.focus();
	  return (false);
	 }

	if (errs!='') errs = 'You left the following field(s) blank:\r' + errs + 'Please complete the form and resubmit.'
	if (errs!='') { alert(errs); return false } else return true
}

function checkETOptinForm(contactfrm) {
	//var sMonth = contactfrm.elements.month.value
	//var sDay = contactfrm.elements.day.value
	//var sYear = contactfrm.elements.year.value
	
	if (isBlank(contactfrm.elements.Name1.value)) {
	  alert('Please enter your first name.');
	  contactfrm.Name1.focus();
	  return (false);
	}
	 
	if (validateZipCode(contactfrm.zip.value) == false) {
	  contactfrm.zip.focus();
	  return (false);
	}
		
	if (emailCheck(contactfrm.EmailAddress.value,"") == false) {
	  contactfrm.EmailAddress.focus();
	  return (false);
	}
	 
//	if (sMonth.length!==2|sDay.length!==2|sYear.length!==4|isNumeric(sMonth)==false|isNumeric(sDay)==false|isNumeric(sYear)==false) {
//		alert('Please enter your birthdate in MM/DD/YYYY format.');
//		contactfrm.month.focus();
//		return (false);
//	}
	
//	if (validateAge(sMonth,sDay,sYear,18)==false) {
//		alert('You must be over 18 to sign up for the Good Stuff newsletter.');
//		contactfrm.month.focus();
//		return (false);
//	}
	
	return true
}

//****************************************	  

	//-- This function opens the contest popup window.
	<!-- Begin
	function Popup(page) {
	OpenWin = this.open(page, "Contest", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=685,height=500,screenX=5,screenY=5,top=5,left=5");
	}
	
	//Trim Functions
	function AllTrim(myString) 
		{ 
		//This JavaScript function performs the same task as the 
		//Trim() function in VB. It will trim the spaces on 
		//both sides of the string that was passed. 
		while (myString.charAt(0)==' ') myString=myString.substring(1); 
		while (myString.charAt(myString.length - 1) ==' ') 
		myString=myString.substring(0,myString.length - 1); 
		return myString; 
	} 
	function LeftTrim(myString) 
		{ 
		//This JavaScript function performs the same task as the 
		//LTrim() function in VB. It will trim the spaces on 
		//the left side of the string that was passed. 
		while (myString.charAt(0)==' ') myString=myString.substring(1); 
		return myString; 
	} 
	function RightTrim(myString) 
		{ 
		//This JavaScript function performs the same task as the 
		//RTrim() function in VB. It will trim the spaces on 
		//the right side of the string that was passed. 
		while (myString.charAt(myString.length - 1) ==' ') 
		myString=myString.substring(0,myString.length - 1); 
		return myString; 
	} 
	
	
	// End -->

//-->
