/*
Checks for a valid e-mail address
*/
function CheckEmail(checkStr)
{
	// test if valid email address, must have @ and .
	var checkEmail	= "@.";
	var EmailValid	= false;
	var EmailAt	= false;
	var EmailPeriod = false;
	var EmailSpace	= false;
	var error	= "";
	
	if (checkStr.indexOf(" ") > -1)
		EmailSpace = true;
	
	for (i = 0;i < checkStr.length;i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;j < checkEmail.length;j++)
		{
			if (ch == checkEmail.charAt(j) && ch == "@")
				EmailAt = true;
			
			if (ch == checkEmail.charAt(j) && ch == ".")
				EmailPeriod = true;
			
			if (EmailAt && EmailPeriod)
				break;
			
			if (j == checkEmail.length)
				break;
		}
		// if both the @ and . were in the string
		if ((EmailAt) && (EmailPeriod) && (!EmailSpace))
		{
			EmailValid = true
			break;
			error = "";
		}
	}
	
	if (!EmailValid)
	{
		error	= "";
		
		if ((!EmailAt) && (!EmailPeriod))
			error	+= " - Email must contain an \"@\" and a \".\"\n";
		
		if (!EmailAt)
			error += " - Email must contain an \"@\"\n";	
		
		if (!EmailPeriod)
			error += " - Email must contain an \".\"\n";	
		
		if (EmailSpace)
			error += " - Email must not contain a space\n";	
		else
			error = " - Email is invalid\n";	
	}
	return error;
}


/*--------------------------------------------------------*/
//  Check the send a friend form for required field values
/*--------------------------------------------------------*/
function CheckSendFriend()
{
	var alertsay = ""; 
	
	if (document.getElementById("SendFriendForm").YourName.value == "")
		alertsay += "- Please enter your Name\n";
	
	if (document.getElementById("SendFriendForm").YourEmail.value == "")
	{
		alertsay += "- Please enter your Email\n";
	}
	else
	{
		Error	= CheckEmail(document.getElementById("SendFriendForm").YourEmail.value);
		if (Error)
			alertsay += "Your Email: " + Error;
	}
	
	if (document.getElementById("SendFriendForm").FriendsName.value == "")
		alertsay += "- Please enter your Name\n";
	
	if (document.getElementById("SendFriendForm").FriendsEmail.value == "")
	{
		alertsay += "- Please enter your Email\n";
	}
	else
	{
		Error	= CheckEmail(document.getElementById("SendFriendForm").FriendsEmail.value);
		if (Error)
			alertsay += "Friends Email: " + Error;
	}
	if (!alertsay)
		return true;
	else
		ShowError(alertsay);
	
	return false;
}


/*---------------------------------------------------*/
//  Check the Contact form for required field values
/*---------------------------------------------------*/
function CheckContact()
{
	var alertsay = ""; 
	
	if (document.getElementById("ContactForm").YourName.value == "")
		alertsay += "- Please enter your Name\n";
	
	if (document.getElementById("ContactForm").YourEmail.value == "")
	{
		alertsay += "- Please enter your Email\n";
	}
	else if (document.getElementById("ContactForm").YourEmail.value != "")
	{
		alertsay += CheckEmail(document.getElementById("ContactForm").YourEmail.value);
	}
	
	if (!alertsay)
		return true;
	else
		ShowError(alertsay);
	
	return false;
}


/*---------------------------------------------------*/
//  Check the Order form for required field values
/*---------------------------------------------------*/
function CheckOrder()
{

	var alertsay = ""; 
	
	if (document.getElementById("OrderForm").Prefer.value == "")
		alertsay += "- Please select the name your prefer to use\n";
	
	if (document.getElementById("OrderForm").ChildName.value == "")
		alertsay += "- Please enter the Childs Name\n";
	
	if (document.getElementById("OrderForm").ChildSurname.value == "")
		alertsay += "- Please enter the Childs Surname\n";
	
	if (document.getElementById("OrderForm").ChildGender.value == "")
		alertsay += "- Please select the Childs Gender\n";
	
	if ((document.getElementById("OrderForm").ChildYear.value == "") && (document.getElementById("OrderForm").ChildQuarter.value == ""))
		alertsay += "- Please select the Childs Age\n";
	
	if (document.getElementById("OrderForm").ChildTown.value == "")
		alertsay += "- Please enter the town Santa/Father Christmas will visit\n";
	
	if (document.getElementById("OrderForm").ChildLeft.value == "")
		alertsay += "- Please enter the what was left for the reindeers last year\n";
	
	if (document.getElementById("OrderForm").ChildFriend.value == "")
		alertsay += "- Please enter the Childs Friends Name\n";
	
	if (document.getElementById("OrderForm").ChildPresent.value == "")
		alertsay += "- Please enter the Childs Wanted Present\n";
	
	if (document.getElementById("OrderForm").YourName.value == "")
		alertsay += "- Please enter the Your Name\n";
	
	if (document.getElementById("OrderForm").YourEmail.value == "")
	{
		alertsay += "- Please enter Your Email\n";
	}
	else if (document.getElementById("OrderForm").YourEmail.value != "")
	{
		alertsay += CheckEmail(document.getElementById("OrderForm").YourEmail.value);
	}
	// BACK UP IF ADDRESS 3 & 4 in .. if ((document.getElementById("OrderForm").Address1.value == "") && (document.getElementById("OrderForm").Address2.value == "") && (document.getElementById("OrderForm").Address3.value == "") && (document.getElementById("OrderForm").Address4.value == ""))
	if ((document.getElementById("OrderForm").Address1.value == "") && (document.getElementById("OrderForm").Address2.value == ""))
	alertsay += "- Please enter the Delivery Address\n";
	
	if (document.getElementById("OrderForm").Town.value == "")
	alertsay += "- Please enter the Delivery Town\n";
	
	if (document.getElementById("OrderForm").County.value == "")
	alertsay += "- Please enter the Delivery County\n";
	
	if (document.getElementById("OrderForm").PostCode.value == "")
	alertsay += "- Please enter the Delivery Postcode\n";
	
	if (document.getElementById("OrderForm").ChildCountry.value == "")
	alertsay += "- Please select the Childs Country\n";
	
	if (!alertsay)
		return true;
	else
		ShowError(alertsay);
	
	return false;
}


/*---------------------------------------------------*/
//  Generic show error function to display an alert box
/*---------------------------------------------------*/
function ShowError(Errors)
{
	alert("The following error(s) occurred:\n" + Errors.substring(Errors,Errors.length-1) + "\n\nSorry can not Process the form");
}


/*--------------------------------*/
//  Sets the value of a selectbox
/*--------------------------------*/
function setselectbyvalue(theSelect, avalue)
{
	if (theSelect)
	{
		for ( var i=0,n=theSelect.options.length; i<n; i++ )
		{
			if (theSelect.options[i].value == avalue )
				return( theSelect.selectedIndex = i );
		}
		return(-1);
	}
}


/*---------------------------------------*/
//  Checks for a valid credit card number
/*---------------------------------------*/
function CheckPayment()
{
	var alertsay	= "";
	
	alertsay	+= CheckCC("MakePayment");
	
	if (alertsay)
	{
		ShowError(alertsay);
		return false;
	}
	else
	{
		return true;
	}
	return false;
}


/*----------------------------------------------------------------------------*/
//  Copies the payment details from one section to another on the payment form
/*----------------------------------------------------------------------------*/
function SameDetailsAs()
{
	if (document.getElementById("MakePayment").SameAs.checked == true)
	{
		document.getElementById("MakePayment").SameAs.disabled	= true;
		document.getElementById("MakePayment").CCHolder.value	= document.getElementById("MakePayment").ContactName.value;
		document.getElementById("MakePayment").CCAddress.value	= document.getElementById("MakePayment").Address.value;
		document.getElementById("MakePayment").CCPostCode.value	= document.getElementById("MakePayment").PostCode.value;
		document.getElementById("MakePayment").CCCountry.value	= document.getElementById("MakePayment").Country.value;
	}
}


/*----------------------------------------------------*/
//  Copies the payment details from the previous order
/*----------------------------------------------------*/
function SameLast()
{
	if (document.getElementById("OrderForm").SameAs.checked == true)
	{
		document.getElementById("OrderForm").SameAs.disabled	= true;
		document.getElementById("OrderForm").YourName.value	= document.getElementById("OrderForm").SYourName.value;
		document.getElementById("OrderForm").YourEmail.value	= document.getElementById("OrderForm").SYourEmail.value;
		document.getElementById("OrderForm").Address1.value	= document.getElementById("OrderForm").SAddress1.value;
		document.getElementById("OrderForm").Address2.value	= document.getElementById("OrderForm").SAddress2.value;
		// document.getElementById("OrderForm").Address3.value	= document.getElementById("OrderForm").SAddress3.value;
		// document.getElementById("OrderForm").Address4.value	= document.getElementById("OrderForm").SAddress4.value;
		document.getElementById("OrderForm").Town.value	= document.getElementById("OrderForm").STown.value;
		document.getElementById("OrderForm").County.value	= document.getElementById("OrderForm").SCounty.value;
		document.getElementById("OrderForm").PostCode.value	= document.getElementById("OrderForm").SPostCode.value;
		document.getElementById("OrderForm").ChildCountry.value	= document.getElementById("OrderForm").SCountry.value;
	}
}

/*----------------------------------------*/
//  Removes an item from the shopping cart
/*----------------------------------------*/
function Remove(CartItem)
{
	QuanBox	= "Quantity" + CartItem;
	document.getElementById(QuanBox).value = "0";
	UpdateCart();
}

/*----------------------------*/
//  Updates the shopping cart
/*----------------------------*/
function UpdateCart()
{
	document.getElementById("MakePayment").submit();
}

/*--------------------------------------------------------*/
//  Calls the process file and displays the thank you page
/*--------------------------------------------------------*/
function MakePayment()
{
	URL	= "process.php";
	window.open(URL);
	window.location.href = "payment_thanks.htm";
}

/*------------------------*/
//  Calls the process file 
/*------------------------*/
function MakePaymentNew()
{
	URL	= "process.php";
	window.location.href = URL;
}

/*------------------------------*/
//  Calls the processPostal file 
/*------------------------------*/
function MakePostalPaymentNew(id, total)
{
	window.location.href ="processPostal.php?id=" + id + "&total=" + total;
}

/*------------------------------*/
//  Calls the processPostal file 
/*------------------------------*/
function MakePostalPayment()
{
	window.location.href ="processPostal.php";
}

/*---------------------------------------------------*/
//  Check the Callback form for required field values
/*---------------------------------------------------*/
function CheckCallBack()
{
	var alertsay = ""; 
	
	if (document.getElementById("CallBackForm").Name.value == "")
		alertsay += "- Please enter your Name\n";
	
	if (document.getElementById("CallBackForm").Telephone.value == "")
		alertsay += "- Please enter your Telephone Number\n";
	
	if (!alertsay)
		return true;
	else
		ShowError(alertsay);
	
	return false;
}

/*----------------------------------------------------------*/
//  Runs the specific function below based on the childs age
/*----------------------------------------------------------*/
function LetterPicker()
{
	if ((document.getElementById("Letter1")) && (document.getElementById("Letter2")) && (document.getElementById("Letter3")))
	{
		document.getElementById("Letter1").onclick	= new Function("LetterText('1')");
		document.getElementById("Letter2").onclick	= new Function("LetterText('2')");
		document.getElementById("Letter3").onclick	= new Function("LetterText('3')");
		document.getElementById("Letter4").onclick	= new Function("LetterText('4')");
	}
}

/*------------------------------------------------------------------------------*/
//  Amends the available text and select boxes depending on the age of the child
/*------------------------------------------------------------------------------*/
function LetterText(Letter)
{
	if (Letter == "4")
	{
		CPresent	= "What present would the baby like for Christmas:";
		CFriend		= "Baby's best friend:";
		CName		= "Baby's First Name:";
		CSurname	= "Baby's Surname:";
		CGender		= "Baby's Gender:";
		CTown		= "Baby's City/Town:";
		ChildText	= "Baby's Age:";
		LeftText	= "What will you leave out for the reindeers to eat this Christmas?";
		MakeAges("Month", "ChildYear");
		MakeAges("Week", "ChildQuarter");
	}
	else
	{
		CPresent	= "What present would the child like for Christmas:";
		CFriend		= "Child's best friend:";
		CName		= "Child's First Name:";
		CSurname	= "Child's Surname:";
		CGender		= "Child's Gender:";
		CTown		= "Child's City/Town:";
		ChildText	= "Child's Age:";
		LeftText	= "What was left out for the reindeers to eat last Christmas?";
		MakeAges("Year", "ChildYear");
		MakeAges("Fraction", "ChildQuarter");
	}
	document.getElementById("ChPresent").innerHTML	= CPresent;
	document.getElementById("ChFriend").innerHTML	= CFriend;
	document.getElementById("ChName").innerHTML		= CName;
	document.getElementById("ChSurname").innerHTML	= CSurname;
	document.getElementById("ChGender").innerHTML	= CGender;
	document.getElementById("ChTown").innerHTML		= CTown;
	document.getElementById("ChAgeText").innerHTML	= ChildText;
	document.getElementById("ChLeftText").innerHTML	= LeftText;
}