//<!--
// expandContractContent.js

//global variable for remembering the visibility state of the email to a friend form
var isEmailToAFriendFormShowing = false;

function validateEmailToAFriendSubmission1()
{
	if(document.forms['emailToAFriendForm'].emailTextField.value == "" || document.forms['emailToAFriendForm'].yourEmailTextField.value == "")
	{
			document.getElementById("emailToAFriendErrorMessage2").style.display = "block";
			document.getElementById("emailToAFriendErrorMessage3").style.display = "none";
			document.getElementById("emailToAFriendErrorMessage").style.display = "none";
			document.forms['emailToAFriendForm'].reset();
			document.forms['emailToAFriendForm'].emailTextField.focus();
	}
	else if(document.forms['emailToAFriendForm'].yourEmailTextField.value != "")
	{
			var str = document.forms['emailToAFriendForm'].yourEmailTextField.value;
			var matches = str.match(/@/g); 
			var count ;
			if(matches != null){
				count = matches.length;
			}
			if(count > 1)
			{
				document.getElementById("emailToAFriendErrorMessage2").style.display = "none";
				document.getElementById("emailToAFriendErrorMessage").style.display = "none";
				document.getElementById("emailToAFriendErrorMessage3").style.display = "block";
				document.forms['emailToAFriendForm'].reset();
				document.forms['emailToAFriendForm'].emailTextField.focus();
			}
			else if (isEmailToAFriendFormValid())
			{
				document.getElementById("emailToAFriendSuccessMessage").style.display = "block";
				document.getElementById("emailToAFriendErrorMessage").style.display = "none";
				document.getElementById("emailToAFriendFormContainer").style.display = "none";
				isEmailToAFriendFormShowing = false;
			
				var urlAndQueryString = document.forms['emailToAFriendForm'].action;
				urlAndQueryString = urlAndQueryString + "?emailTextField=";
				urlAndQueryString = urlAndQueryString + escape(document.forms['emailToAFriendForm'].emailTextField.value);
				urlAndQueryString = urlAndQueryString + "&yourEmailTextField=";
				urlAndQueryString = urlAndQueryString + escape(document.forms['emailToAFriendForm'].yourEmailTextField.value);
				urlAndQueryString = urlAndQueryString + "&offerId=";
				urlAndQueryString = urlAndQueryString + escape(document.forms['emailToAFriendForm'].offerId.value);
				document.getElementById("emailToAFriendSubmitImage").src = urlAndQueryString;
			}
			else
			{
				document.getElementById("emailToAFriendErrorMessage3").style.display = "none";
				document.getElementById("emailToAFriendErrorMessage2").style.display = "none";
				document.getElementById("emailToAFriendErrorMessage").style.display = "block";
				document.forms['emailToAFriendForm'].reset();
				document.forms['emailToAFriendForm'].emailTextField.focus();
			}
	}
	// always return false; the form never really submits
	return false;
}

function toggleEmailToAFriendFormVisibility()
{
	if (!isEmailToAFriendFormShowing)
	{
		//if the form is not showing, show it and reset it's contents
		document.getElementById("emailToAFriendFormContainer").style.display = "block";
		document.forms['emailToAFriendForm'].reset();
		document.forms['emailToAFriendForm'].emailTextField.focus();
		isEmailToAFriendFormShowing = true;
	}
	else
	{
		//if the form is showing, hid it
		document.getElementById("emailToAFriendFormContainer").style.display = "none";
		isEmailToAFriendFormShowing = false;
	}
	
	//whether showing or hiding, hide success and error messages
	document.getElementById("emailToAFriendSuccessMessage").style.display = "none";
	document.getElementById("emailToAFriendErrorMessage").style.display = "none";
}
	
function isEmailToAFriendFormValid()
{
	var emailAddresses = new Array();
	
	emailAddresses = document.forms['emailToAFriendForm'].emailTextField.value.split(",");
	var validationFailedCount = 0;
	for(var i = 0; i < emailAddresses.length; i++)
	{
		//trim off leading and trailing spaces of each email address
		emailAddresses[i] = trim(emailAddresses[i]);
		//test each email address
		if (!isEmail(emailAddresses[i]))
		{
			++validationFailedCount;
		}
	}
	
	//if validation failed count is zero, then all passed validation
	if (validationFailedCount == 0)
	{
		//side effect: reset form to cleansed list of email addresses (no spaces)
		document.forms['emailToAFriendForm'].emailTextField.value = emailAddresses.join(",");
		
		//indicates success
		return true;
	}
	else
	{
		//indicates one or more bad emails are present
		return false;
	}
}

// Trim whitespace from left and right sides of s.
function trim(s) {
    return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

// Determines if string is a valid looking Email Address. This is comprised
// of looking for the "@" and "." characters...
function isEmail(sEmailAddress)
{
	// do basic edits then extend if javascript 1.2 or later is supported 
	if (	sEmailAddress.indexOf('@') == -1 || sEmailAddress.indexOf('.') == -1 || 
			spaceInField(sEmailAddress) || chkInvalidEmailFormat(sEmailAddress) || 
			CheckForDupChar('@',sEmailAddress)
		)
	{
		return false;
	}
	else 
		return true;
}	

function CheckForDupChar (checkChar,varString)
{
	var charPos;
	var parsedString;
	charPos = varString.indexOf(checkChar);
	parsedString = varString.substring(charPos + 1);
	charPos = parsedString.indexOf(checkChar);
	if (charPos == -1)
	{
		return false;
	}
	else
		return true;
}		

// Embedded space function
function spaceInField(s)
{
	var iSpace = s.indexOf(' ');
	var iLen = s.length;
	if ((iSpace > 0 && iSpace < iLen - 1) || ( iSpace == 0 ))
	{
		// Spaces in string - embedded or 1st char is space
		return true;
	}
	else 
		return false;
}

function chkInvalidEmailFormat(strAddress)
{
	// check for a dot after the @
	{
		var aAt = strAddress.indexOf('@');
		var aLastDot = strAddress.lastIndexOf('.');
		
		if (aAt > aLastDot) 
		{
	 		return true; 	
		}
	}
	// check for invalid characters
	{
		var aBang = strAddress.indexOf('!');
		var aPound = strAddress.indexOf('#');   
		var aDollar = strAddress.indexOf('$');   
		var aPercent = strAddress.indexOf('%');   
		var aCarat = strAddress.indexOf('^');   
		var aApersand = strAddress.indexOf('&');        
		var aAsterisk = strAddress.indexOf('*');   
		var aLparen = strAddress.indexOf('(');   
		var aRparen = strAddress.indexOf(')');   
		var aPlus = strAddress.indexOf('+');   
		var aEqual = strAddress.indexOf('=');   
		var aSlash = strAddress.indexOf('/');
		var aQuestion = strAddress.indexOf('?');   
		var aLcurly = strAddress.indexOf('{');   
		var aRcurly = strAddress.indexOf('}');   
		var aLbracket = strAddress.indexOf('[');   
		var aRbracket = strAddress.indexOf(']');   
		var aParenthesis = strAddress.indexOf('"');
		var aSum = ( aBang + aPound + aDollar + aPercent + aCarat + aApersand + aAsterisk + aLparen + aRparen + aPlus +	aEqual + aSlash + aQuestion + aLcurly + aRcurly + aLbracket + aRbracket + aParenthesis);
		
		if ( aSum > -18 ) 
		{
			return true; 	
		}
	}
}
// -->