// actually part of the exit pop-up strategy but needed since coded for all pages.
function set_hold()
{
	hold=true;
}
function release_hold()
{
	hold=false;
}

function resetCC()
{
	form = document.details;
	
	control = form["fields[Payment Method]"];
	control.selectedIndex = 1;
	
	control = form["fields[CC Num]"];
	control.value = '';
	
	control = form["fields[Name On Card]"];
	control.value = '';
	
	control = form["fields[Credit Card Type]"];
	control.selectedIndex = 0;
	
	control = form["fields[Expiration Date Month]"];
	control.selectedIndex = 0;
	
	control = form["fields[Expiration Date Year]"];
	control.selectedIndex = 0;
}

function setHearValue( value )
{
	form = document.details;
	
	control = form["fields[How did you hear about us]"];
	control.value = value;
}

function setGiftPaymentInfo()
{
	form			= document.details;
	
	showDiv('paymentdiv');
	hideDiv('amountdiv');

	showDiv('cconlydiv');
	hideDiv('otherpaymentdiv');

	control			= form["fields[gift_payment]"];
	control.value	= true;

	control			= form["fields[Payment Method]"];
	control.value	= 'Card';

	control			= form["fields[Payment Amount]"];
	control.value	= '$288/annually';
}

function resetGiftPaymentInfo()
{
	form			= document.details;
	
	hideDiv('paymentdiv');
	
	showDiv('otherpaymentdiv');
	hideDiv('cconlydiv');
	
	control			= form["fields[gift_payment]"];
	control.value	= 0;

	control			= form["fields[Payment Method]"];
	control.selectedIndex = 0;

	control			= form["fields[Payment Amount]"];
	control.value	= '';
}

function resetGift()
{
	form = document.details;
	
	document.getElementById('gift').selectedIndex = 0;
	document.getElementById('writingdiv').style.display = 'block';
}

// LBB 07/16/2009 - Change the matchkey value based on the how_did_you_hear_about_us dropdown
function MKChange() 
{
	var dropdownValue = document.getElementById('howdidyouhearaboutus').value;
	var mk = document.getElementById('matchkey');
	
	if  (dropdownValue == "tv") {
         mk.value = "2010D0045";
	}
	else {
         mk.value = "2010Y0007";
    }
}

// Credit card validation
function validateCCnum2(ccnbr,ccname)
{
	var result = true;
	var firstdig = ccnbr.value.substring(0,1);
	var seconddig = ccnbr.value.substring(1,2);
	var first4digs = ccnbr.value.substring(0,4);
	var cardName = "";
	var cardSize = "";
	var cardBegin = "";	
	var cardLen = ccnbr.value.length;

	switch (ccname.value)
	{
		case "V":
			result = ((cardLen == 13 || cardLen == 16) && (firstdig == "4"));
			cardName = "Visa";
			cardSize = "13 or 16";
			cardBegin = "4";
			break;
		case "D":
			result = (cardLen == 16  && first4digs == "6011");
			cardName = "Discover";
			cardSize = "16";
			cardBegin = "6011";	
			break;
		case "AE":
			var validNums = "47";
			result = (cardLen == 15  && firstdig == "3") && (validNums.indexOf(seconddig) >= 0);
			cardName = "American Express";
			cardSize = "15";
			cardBegin = "34 or 37";
			break;
		case "MC":
			var validNums = "12345";
			result = (cardLen == 16  && firstdig == "5") && (validNums.indexOf(seconddig) >= 0);
			cardName = "MasterCard";
			cardSize = "16";
			cardBegin = "51, 52, 53, 54, or 55";
			break;
	}
	if (!result)
	{
		alert("Please correct your credit card number.\nCard number or length is incorrect for type selected.\n" + cardName + " must be " + cardSize + " digits and begin with " + cardBegin + ".");
		ccname.focus();
		return false;
	}
	else
	{
		var ckDigit = ccnbr.value.substr(cardLen - 1);
		var sum = parseInt(ckDigit);
		var x = 2;

		for (i = 1; i < cardLen; i++ )
		{
			var digit = ccnbr.value.substr(cardLen - i - 1, 1);
			if ((x % 2) != 0)
				var tproduct = parseInt(digit);
				else 
					tproduct = parseInt(digit) * 2;
					if (tproduct >= 10)
						sum += (tproduct % 10) + 1;
		  			else
						sum += tproduct;
				x++;
		}
		if ((sum % 10) != 0)
		{
			alert("Please enter a valid credit card number.");
			ccnbr.focus();
			ccnbr.select();
			return false;
		}
	}
	
	return true;
}


function checkZipNumeric(value)
{
    var valid		= "0123456789";
    var errors		= false;
    var temp		= "";
	
    for (var i=0; i<value.length; i++)
    {
		temp = "" + value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")
		{
			errors = true;
		}
	}

	if (errors == true)
	{
		return false;
	}
	
	return true;
}

//---------------------------------------------------------------------------------------------------------
//	Validation the Addition form
//---------------------------------------------------------------------------------------------------------
function formValidate()
{
	form = document.details;

//---------------------------------------------------------------------------------------------------------
//	Test for valid email format
//---------------------------------------------------------------------------------------------------------
	control = form["fields[Email]"];
	if ( !IsEmail( control.value ) )
	{
		alert("Email format is invalid.");
		control.focus();
		return false;
	}

//---------------------------------------------------------------------------------------------------------
//	Replacement Field Testing
//---------------------------------------------------------------------------------------------------------
	if (form["fields[AddReplace]"])
	{
		if( !form["fields[AddReplace]"].value )
		{
			alert("Please select add or replace.");
			form["fields[AddReplace]"].focus();
			return false; 
		}
		if( form["fields[AddReplace]"].value == 'Replace' )
		{
			control = form["fields[Previous Child Name]"];
			value	= str_trim(control.value);
		
			if ( !value )
			{
				alert("Enter a Previous Child Name");
				control.focus();
				return false;
			}
			
			control = form["fields[Previous Child Number]"];
			value	= str_trim(control.value);
		
			if ( !value )
			{
				alert("Enter a Previous Child Number");
				control.focus();
				return false;
			}

			control = form["fields[Sponsor Number]"];
			value	= str_trim(control.value);
		
			if ( !value )
			{
				alert("Enter a Sponsor Number");
				control.focus();
				return false;
			}
		}
	}

//---------------------------------------------------------------------------------------------------------
//	Gift Field Testing
//---------------------------------------------------------------------------------------------------------
	if(( form["fields[Gift]"].value == 1 ) || (form["fields[Other Writer]"].value == 1 ))
	{
		control			= form["fields[Gift Comment]"];
		value			= str_trim(control.value);
		commentLength	= value.length;
	
		if ( (commentLength > 1000) )
		{
			alert("Please limit your Comment/Note to 1000 characters");
			control.focus();
			return false;
		}
		
		control = form["fields[Gift First Name]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Recipient First Name");
			control.focus();
			return false;
		}
		
		control = form["fields[Gift Last Name]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Recipient Last Name");
			control.focus();
			return false;
		}
		
		control = form["fields[Gift Country]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Recipient Country");
			control.focus();
			return false;
		}
		
		control = form["fields[Gift Address 1]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Recipient Address 1");
			control.focus();
			return false;
		}
		
		control = form["fields[Gift City]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Recipient City");
			control.focus();
			return false;
		}
		
		control = form["fields[Gift Country]"];
		value	= str_trim(control.value);
	
		if (value == 'CANADA')
		{
			control = form["fields[Gift Province]"];
			value	= str_trim(control.value);

			if ( !value )
			{
				alert("Enter a Recipient Province");
				control.focus();
				return false;
			}
			
			control = form["fields[Gift Postal Code]"];
			value	= str_trim(control.value);
		
			if ( !value )
			{
				alert("Enter a Recipient Postal Code");
				control.focus();
				return false;
			}			
		}

		else if (value == 'UNITED STATES')
		{
			control = form["fields[Gift State]"];
			value	= str_trim(control.value);
	
			if ( !value )
			{
				alert("Enter a Recipient State");
				control.focus();
				return false;
			}

			control = form["fields[Gift Postal Code]"];
			value	= str_trim(control.value);
		
			if ( !value )
			{
				alert("Enter a Recipient Postal Code");
				control.focus();
				return false;
			}
		
		
			control = form["fields[Gift Postal Code]"];
			value	= str_trim(control.value);
			strLength	= value.length;

			if ( (strLength != 5) && (strLength != 9) )
			{
				alert("Recipient Postal Code is not the proper length");
				control.focus();
				return false;
			}
			
			if (!checkZipNumeric(value))
			{
				alert("US postal codes may only be numeric");
				control.focus();
				return false;
			}
		}

		control = form["fields[Gift Email]"];
		if ( control.value && !IsEmail( control.value ) )
		{
			alert("Recipient Email format is invalid.");
			control.focus();
			return false;
		}
	}

//---------------------------------------------------------------------------------------------------------
//	State Province Testing
//---------------------------------------------------------------------------------------------------------
	control = form["fields[Country]"];
	value	= str_trim(control.value);
	
	if (value == 'CANADA')
	{
		control = form["fields[Province]"];
		value	= str_trim(control.value);

		if ( !value )
		{
			alert("Enter a Province");
			control.focus();
			return false;
		}
		
		control = form["fields[Postal Code]"];
		value	= str_trim(control.value);
		
		if ( !value )
		{
			alert("Enter a Postal Code");
			control.focus();
			return false;
		}
	}

	else if (value == 'UNITED STATES')
	{
		control = form["fields[State]"];
		value	= str_trim(control.value);

		if ( !value )
		{
			alert("Enter a State");
			//control.focus();
			return false;
		}

		control = form["fields[Postal Code]"];
		value	= str_trim(control.value);
		
		if ( !value )
		{
			alert("Enter a Postal Code");
			control.focus();
			return false;
		}
		
		control = form["fields[Postal Code]"];
		value	= str_trim(control.value);
		strLength	= value.length;

		if ( (strLength != 5) && (strLength != 9) )
		{
			alert("Postal Code is not the proper length");
			control.focus();
			return false;
		}
		
		if (!checkZipNumeric(value))
		{
			alert("US postal codes may only be numeric");
			control.focus();
			return false;
		}
	}

//---------------------------------------------------------------------------------------------------------
//	Validate CC Payment fields
//---------------------------------------------------------------------------------------------------------
	if( form["fields[Payment Method]"].value == 'Card' || form["fields[gift_payment]"].value == 'true' )
	{
		control = form["fields[CC Num]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Credit Card Number");
			control.focus();
			return false;
		}
		
		control = form["fields[Credit Card Type]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Credit Card Type");
			control.focus();
			return false;
		}

		control = form["fields[Expiration Date Month]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter an Expiration Date Month");
			control.focus();
			return false;
		}

		control = form["fields[Expiration Date Year]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter an Expiration Date Year");
			control.focus();
			return false;
		}


		control = form["fields[Name On Card]"];
		value	= str_trim(control.value);
	
		if ( !value )
		{
			alert("Enter a Name On Card");
			control.focus();
			return false;
		}
		
		ccnum  = form["fields[CC Num]"];
		ccname = form["fields[Credit Card Type]"];
		
		if ( !validateCCnum2( ccnum, ccname ) )
		{
			ccnum.focus();
			return false;
		}
	
	//---------------------------------------------------------------------------------------------------------
	//	Check expiration date
	//---------------------------------------------------------------------------------------------------------
		//	Get the current month and year
		date_obj		= new Date();
		current_month	= date_obj.getMonth() + 1;
	
		//	Get the two digit year part only
		current_year	= date_obj.getFullYear() + '';
		current_year	= parseInt( current_year.substr(2,2),10 );
	
		//	Get the selected month and year
		dropdown		= form["fields[Expiration Date Month]"].options;
		selected_month	= parseInt( dropdown[dropdown.selectedIndex].value,10 );
		
		dropdown		= form["fields[Expiration Date Year]"].options;
		selected_year	= parseInt( dropdown[dropdown.selectedIndex].value,10 );
	
		//	Has the exp. date passed? 
		if ( selected_year < current_year )
		{
			//	Yep, not valid card
			alert("Credit card has expired");
			return false;
		}
	
		//	Is the exp. year this year and the month is the current
		//	month or has already passed?
		if ( selected_year == current_year && 
			 selected_month < current_month )
		{
			//	Yep, invalid card
			alert("Credit card has expired");
			return false;
		}
	}

//---------------------------------------------------------------------------------------------------------
//	Everything ok
//---------------------------------------------------------------------------------------------------------
	return true;
}