
var bSubmitted = false;

function resetForm( strForm )
{
	eval( "document." + strForm + ".reset()" );
}

function checkDetails( strForm, strField, strFieldType )
{
	// Take input and Reformat to give bRequired Status string
	var strTemp = new String() ;
	strTemp = strField.replace( "form_", "bRequired_" );
	
	var nIncrement = 0;
	if ( eval( strTemp ) )
	{
		switch ( strFieldType )
		{
			case "textfield":
				strField = eval( "document." + strForm + "." + strField + ".value" );
				if ( strField == "" )
				{
					nIncrement = 1;
				}
				break;
			
			case "checkbox":
				strField = eval( "document." + strForm + "." + strField + ".checked" );
				if ( strField == false )
				{
					nIncrement = 1;
				}
				break;
		}
	}
	return nIncrement;
}

function submitForm( strForm )
{
	var strErrorMsg = "";
	var nFieldsNotValid = 0;
	switch( strForm )
	{
		case "billingDetails_form":
			if ( !bCorrectDetails )
			{
				nFieldsNotValid += checkDetails( strForm, "form_ContactTitle", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_ContactFirstName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_ContactLastName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_CompanyName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Address1", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Address2", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_City", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_County", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Country", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_PostCode", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_TelNo", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_FaxNo", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Email", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Newsletter", "checkbox" );
				nFieldsNotValid += checkDetails( strForm, "form_Over18", "checkbox" );
			}
			break;
		
		case "deliveryDetails_form":
			if ( !bCorrectDetails )
			{
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryFirstName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryLastName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryCompanyName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryAddress1", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryAddress2", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryCity", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryCounty", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryCountry", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryPostCode", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryTelNo", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryFaxNo", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_DeliveryEmail", "textfield" );
			}
			break;
		
		case "paymentDetails_form":
			if ( nTotalToPay > 0 )
			{
				nFieldsNotValid += checkDetails( strForm, "form_CardType", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_CardHolderName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_CardNumber", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_SecurityNumber", "textfield" );
			}
			break;

		case "billingPaymentDetails_form":
			if ( !bCorrectDetails )
			{
				nFieldsNotValid += checkDetails( strForm, "form_ContactTitle", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_ContactFirstName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_ContactLastName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_CompanyName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Address1", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Address2", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_City", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_County", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Country", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_PostCode", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_TelNo", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_FaxNo", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Email", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_Newsletter", "checkbox" );
				nFieldsNotValid += checkDetails( strForm, "form_Over18", "checkbox" );
				nFieldsNotValid += checkDetails( strForm, "form_CardType", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_CardHolderName", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_CardNumber", "textfield" );
				nFieldsNotValid += checkDetails( strForm, "form_SecurityNumber", "textfield" );
			}
			break;
		
		case "bankDetails_form":
			nFieldsNotValid += checkDetails( strForm, "form_BankName", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankAddress1", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankAddress2", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankCity", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankCounty", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankCountry", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankPostCode", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankSortCode", "textfield" );
			nFieldsNotValid += checkDetails( strForm, "form_BankAccountNumber", "textfield" );
			break;
	}

	if ( nFieldsNotValid > 0 )
	{
		strErrorMsg += "Please ensure that fields marked with an asterisk (*) are completed.";
	}
	
	if ( strErrorMsg == "" )
	{
		if ( !bSubmitted )
		{
			//document.getElementById( strForm ).submit()
			eval( "document." + strForm + ".submit()" );
		}
		bSubmitted = true;
		document.getElementById("processButtons").innerHTML = '<img src="/eshop/images/buttons/processing.gif" alt=":: Processing Details ::" width="73" height="21" border="0">';
	}
	else
	{
		alert( strErrorMsg );
	}
}


function isEmailAddr( strEmail )
{
	var result = false
	var theStr = new String( strEmail )
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}

function isValidPassword( strPassword )
{
	var result = false
	var theStr = new String( strPassword )
	var PasswordLength = theStr.length;
	if ( PasswordLength >= 6 ) result = true;
	return result;
}

function checkDeliveryDate( strField )
{
	bDateValid = true;
	arrShippingBlackoutDates = strShippingBlackoutDates.split(";");

	strDate = document.getElementById( strField ).value;
	arrDate = strDate.split("/");
	var datDate = new Date( parseInt( arrDate[2] ), parseInt( arrDate[1] ) - 1, parseInt( arrDate[0] ) );
	arrEarliestShippingDate = strEarliestShippingDate.split("/");
	//datEarliestShippingDate = new Date( parseInt( arrEarliestShippingDate[2] ), parseInt( arrEarliestShippingDate[1] ) -1, parseInt( arrEarliestShippingDate[0] ) );
	datEarliestShippingDate = new Date( arrEarliestShippingDate[2] , arrEarliestShippingDate[1] -1, arrEarliestShippingDate[0] );

	if ( datDate < datEarliestShippingDate )
	{
		bDateValid = false;
	}
	else
	{
		for ( i = 0; i < arrShippingBlackoutDates.length - 1; i++ )
		{
			strBlackoutDate = arrShippingBlackoutDates[i];
			arrBlackOutDate = strBlackoutDate.split("/");
			//var datBlackoutDate = new Date( parseInt( arrBlackOutDate[2] ), parseInt( arrBlackOutDate[1] ) - 1, parseInt( arrBlackOutDate[0] ) );
			var datBlackoutDate = new Date( arrBlackOutDate[2], arrBlackOutDate[1] - 1, arrBlackOutDate[0] );
			//if ( datDate == datBlackoutDate )
			if ( datDate.toString() == datBlackoutDate.toString() )
			{
				bDateValid = false;
			}
		}
		var arrDayNames = new Array("Sunday", "Monday", "Tuesday","Wednesday","Thursday", "Friday","Saturday");
		var bValidDeliveryDay = eval( "bDeliver" + arrDayNames[ datDate.getDay() ] )
		if ( bValidDeliveryDay == false )
		{
			bDateValid = false;
		}
	}
	
	if ( !bDateValid )
	{
		alert("Sorry, we are unable to ship on the date you have chosen.\n\nPlease select an alternative date.");
		document.getElementById( strField ).value = "";
		winCal.focus();
	}
	else
	{
		winCal.window.close();
	}
}

function checkGiftMessageLength( objMessage )
{
	strMessage = objMessage.value
	if ( strMessage.length > 255 )
	{
		alert("The maximum length for the Gift Message must be no more than 255 characters");
		objMessage.value = strMessage.substring( 0, 255 );
	}
	else
	{
		if ( strMessage.length > 0 )
		{
			document.getElementById("form_CardAndGiftWrap").checked = true;
		}
		else
		{
			document.getElementById("form_CardAndGiftWrap").checked = false;
		}
	}
}

function checkDeliveryInstructionsLength( objDeliveryInstructions )
{
	strDeliveryInstructions = objDeliveryInstructions.value
	if ( strDeliveryInstructions.length > 255 )
	{
		alert("The maximum length for the Delivery Instructions must be no more than 255 characters");
		objDeliveryInstructions.value = strDeliveryInstructions.substring( 0, 255 );
	}
}

