// The follwoing forms currently use this javascript:
//    6630 - Store Order Form

//------------------------------------------------------------------------------------------------
//	Show/hide state/province dropdowns based on country
//------------------------------------------------------------------------------------------------

function clearPulldown( id )
{
	obj = document.getElementById(id);
	
	if (obj.selectedIndex)
	{
		obj.selectedIndex = 0;
	}
}

function showstar( id )
{
	document.getElementById(id).style.display = 'inline';
}

function hidestar( id )
{
	document.getElementById(id).style.display = 'none';
}

//---------------------------------------------------------------------------------------------------------
//	Based upon user action, show a particular part of the form
//---------------------------------------------------------------------------------------------------------
function showDiv( id )
{
	document.getElementById(id).style.display = 'block';
}

//---------------------------------------------------------------------------------------------------------
//	Basud upon user action, hide a particular part of the form
//---------------------------------------------------------------------------------------------------------
function hideDiv( id )
{
	document.getElementById(id).style.display = 'none';
}

//------------------------------------------------------------------------------------------------
//	Field Entry Validation
//------------------------------------------------------------------------------------------------
  
// onKeyUp - allow entry of numeric number only in the field
function validateNumericOnly(field)
{
    var valid		= "0123456789";
    var errors		= false;
    var temp		= "";
	var newString	= "";

    for (var i=0; i<field.value.length; i++)
    {
		temp = "" + field.value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")
		{
			errors = true;
		}

		else
		{
			newString += temp;
		}
	}

	if (errors == true)
	{
		field.value = newString;
		return;
	}
}

// onKeyUp - allow entry of numeric number and dashes only in a field
//         - used for sponsored child number fields and phone number fields
function validateNumericDash(field)
{
    var valid		= "0123456789-";
    var errors		= false;
    var temp		= "";
	var newString	= "";

    for (var i=0; i<field.value.length; i++)
    {
		temp = "" + field.value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")
		{
			errors = true;
		}

		else
		{
			newString += temp;
		}
	}

	if (errors == true)
	{
		field.value = newString;
		return;
	}
}

// onKeyUp - allow entry of numeric number and slashes / only in a field - used for date fields
function validateNumericSlash(field)
{
    var valid		= "0123456789/";
    var errors		= false;
    var temp		= "";
	var newString	= "";

    for (var i=0; i<field.value.length; i++)
    {
		temp = "" + field.value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")
		{
			errors = true;
		}

		else
		{
			newString += temp;
		}
	}

	if (errors == true)
	{
		field.value = newString;
		return;
	}
}

//------------------------------------------------------------------------------------------------
//	If source is current sponsor then a sponsor number must be entered on form
//------------------------------------------------------------------------------------------------
function validateSrc(src,sponsor)
{
	if (src.value == "cs" && sponsor.value.length == 0)
	{
		alert("You seleted current donor/sponsor but did not enter your sponsor number.");
		sponsor.focus();
		sponsor.select();
	}  
}

//------------------------------------------------------------------------------------------------
//	validate the form fields afer the user hits the submit button
//------------------------------------------------------------------------------------------------
function formValidate()
{
	form = document.details;

	//---------------------------------------------------------------------------------------------
	//	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("Please select a State");
			//control.focus();
			return false;
		}

		control = form["fields[Postal Code]"];
		value	= str_trim(control.value);
		
		if ( !value )
		{
			alert("Please enter a valid US 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("US Postal Code is not the proper length.  Must be 5 or 9.");
			control.focus();
			return false;
		}
		
		if (!checkZipNumeric(value))
		{
			alert("US postal codes may only be numeric");
			control.focus();
			return false;
		}
	}

	//---------------------------------------------------------------------------------------------
	//	Test for valid email format
	//---------------------------------------------------------------------------------------------
	control = form["fields[Email]"];
	if ( str_trim(control.value) )
	{
		if ( !IsEmail( control.value ) )
		{
			alert("Email format is invalid.");
			control.focus();
			return false;
		}
	}
	
	//-------------------------------------------------------------------------------------------
	//	Validate CC Payment fields
	//-------------------------------------------------------------------------------------------
	if ( form["fields[Form]"].value != "ManageAccount" && form["fields[Form]"].value != "PostalPref" )
	{
		control = form["fields[CC Number]"];
		value	= str_trim(control.value);

		if ( !value )
		{
			alert("Please enter a Credit Card Number");
			control.focus();
			return false;
		}
		
		control = form["fields[CC Type]"];
		value	= str_trim(control.value);

		if ( !value )
		{
			alert("Please enter a Credit Card Type");
			control.focus();
			return false;
		}
	}

	if ( ( form["fields[Form]"].value != "ManageAccount" && form["fields[Form]"].value != "PostalPref" ) || (  form["fields[Form]"].value == "ManageAccount" && ( form["fields[TransType]"].value == "One time CC Donation" || form["fields[TransType]"].value == "Change Method to CC" || form["fields[TransType]"].value == "Change CC Info" ) ))
	{
		control = form["fields[CC Exp Month]"];
		value	= str_trim(control.value);

		if ( !value )
		{
			alert("Please select an Expiration Month");
			control.focus();
			return false;
		}

		control = form["fields[CC Exp Year]"];
		value	= str_trim(control.value);

		if ( !value )
		{
			alert("Please select an Expiration Year");
			control.focus();
			return false;
		}

		control = form["fields[Name on Account]"];
		value	= str_trim(control.value);

		if ( !value )
		{
			alert("Please enter the name on the Credit Card");
			control.focus();
			return false;
		}

		if ( form["fields[Form]"].value == "ManageAccount" )
		{
			ccnum  = form["fields[CCEFTAccount]"];
			ccname = form["fields[CCTypeBankName]"];
		}
		else
		{
			ccnum  = form["fields[CC Number]"];
			ccname = form["fields[CC Type]"];
		}
		
		if ( !validateCCnum( 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[CC Exp Month]"].options;
		selected_month	= parseInt( dropdown[dropdown.selectedIndex].value,10 );
		
		dropdown		= form["fields[CC Exp 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 ("The date for the 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 ("The date for the credit card has expired.");
			return false;
		}
	}

	// Verify the routing number on EFT forms
	if ( form["fields[Form]"].value == "ManageAccount" && ( form["fields[TransType]"].value == "Change Method to EFT" || form["fields[TransType]"].value == "Change EFT Info" ) )
	{
		control = form["fields[Routing Number]"];
		value	= str_trim(control.value);
		if ( value.length != 9)
		{
			alert ("Please enter 9 digits for the routing number.\nSee link above for sample check information.");
			control.focus();
			return false;
		}
	}

    // Verify the dollar amount entered is correct on the Make a Donation Forms
	if ( form["fields[Form]"].value == "Donation" )
	{
		control = form["fields[Dollar Amt]"]; 
		if ( !confirm("You have entered a donation amount of $" + str_trim(control.value) + ".00.\nPlease click cancel if this is not correct.\nOtherwise, click the OK button to continue.") )
		{
			control.focus();
			return false;
		}
	}

	// Verify the dollar amounts entered is correct on the Manage Account Forms
	if ( form["fields[Form]"].value == "ManageAccount" )
	{
		controlA = form["fields[Child or OneTime]"];		
		valueA	= str_trim(controlA.value);
		controlB = form["fields[Project or Continue]"];		
		valueB	= str_trim(controlB.value);
		if ( valueA && !valueB ) 
		{
			if ( !confirm("You have entered an amount of $" + valueA + ".00.\nPlease click cancel if this is not correct.\nOtherwise, click the OK button to continue.") )
			{
				controlA.focus();
				return false;
			}
		}
		if ( !valueA && valueB ) 
		{
			if ( !confirm("You have entered an amount of $" + valueB + ".00.\nPlease click cancel if this is not correct.\nOtherwise, click the OK button to continue.") )
			{
				controlB.focus();
				return false;
			}
		}
		if ( valueA && valueB ) 
		{
			if ( !confirm("You have entered amounts of \n$" + valueA + ".00 and $" + valueB + ".00.\nPlease click cancel if this is not correct.\nOtherwise, click the OK button to continue.") )
			{
				controlA.focus();
				return false;
			}
		}
	}
		

	//---------------------------------------------------------------------------------------------
	//	Everything ok
	//---------------------------------------------------------------------------------------------
	return true;
}


//--------------------------------------------------------------------------------------------------
//	Editing routines for formValidate()
//--------------------------------------------------------------------------------------------------

function IsEmail( fieldvalue )
{
// Note: The next expression must be all on one line...
//       allow no spaces, linefeeds, or carriage returns!
	return fieldvalue.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.int)|(\.name)|(\.info)|(\.biz)|(\..{2,2}))$)\b/gi);
}

function validateCCnum(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 == 16) && (firstdig == "4");
			cardName = "Visa";
			cardSize = "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;
		case "DC":
			var validNums = "068";
			result = (cardLen == 14  && firstdig == "3") && (validNums.indexOf(seconddig) >= 0)
			cardName = "Diners Club";
			cardSize = "14";
			cardBegin = "30, 36, or 38";
			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 checkNumeric(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;
}

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;
}