function CheckOutAccount(frm)
{
    /* check all required fields are complate */
    var boolValid = true;
    var strField = "";
    var boolDeliveryAddress = false;
    var charpos = 0; 
    var datToday = new Date()
    
    /* firstname */
    strField = ValueGet("txtfirstname");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide a first name");
        Select("txtfirstname");
    }
    
    /* lastname */
    strField = ValueGet("txtlastname");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide a last name");
        Select("txtlastname");
    }
    
    /* email */
    strField = ValueGet("txtemail");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide an email address");
        Select("txtemail");
    }
    if (boolValid == true && validateEmailv2(strField) == false)
    {
        boolValid = false;
        alert("You must provide a valid email address");
        Select("txtemail");
    }
        
    /* password */
    strField = ValueGet("txtpassword");
    if (boolValid == true && strField.length <= 6 )
    {
        boolValid = false;
        alert("Passwords must contain at least 6 characters");
        Select("txtpassword");
    }
    if (boolValid == true && ValueGet("txtpassword") != ValueGet("txtpasswordconfirm") )
    {
        boolValid = false;
        alert("The password you have entered does not match your confirmation password.");
        Select("txtpasswordconfirm");
    }
    
    /* telephone */
    strField = ValueGet("txttelephone");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide a telephone number");
        Select("txttelephone");
    }
    
    
    /* billingaddress1 */
    strField = ValueGet("txtbillingaddress1");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the first line of your billing address");
        Select("txtbillingaddress1");
    }
    
    /* billingtowncity */
    strField = ValueGet("txtbillingtowncity");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the town or city of your billing address");
        Select("txtbillingtowncity");
    }
    
    /* billingpostcode */
    strField = ValueGet("txtbillingpostcode");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the postcode of your billing address");
        Select("txtbillingpostcode");
    }
    
    /* check to see if there is a delivery address we need to validate */
    if (ValueGet("txtdeliveryaddress1") != "")
    {
        boolDeliveryAddress = true;
    }
    if (ValueGet("txtdeliveryaddress2") != "")
    {
        boolDeliveryAddress = true;
    }
    if (ValueGet("txtdeliverytowncity") != "")
    {
        boolDeliveryAddress = true;
    }
    if (ValueGet("txtdeliverypostcode") != "")
    {
        boolDeliveryAddress = true;
    }
    
    /* billingdeliveryaddress1 */
    strField = ValueGet("txtdeliveryaddress1");
    if (boolValid == true && boolDeliveryAddress == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the first line of your delivery address");
        Select("txtdeliveryaddress1");
    }
    
    /* billingdeliverytowncity */
    strField = ValueGet("txtdeliverytowncity");
    if (boolValid == true && boolDeliveryAddress == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the town or city of your delivery address");
        Select("txtdeliverytowncity");
    }
    
    /* billingdeliverypostcode */
    strField = ValueGet("txtdeliverypostcode");
    if (boolValid == true && boolDeliveryAddress == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the postcode of your delivery address");
        Select("txtdeliverypostcode");
    }
    
    /* delivery date */
    if (boolValid == true)
    {
        /* check day */
        strField = ValueGet("dtdeliverydateday");
        if (boolValid == true && strField.length == 0 )
        {
            boolValid = false;
            alert("Please enter the date you would prefer the delivery to be made");
            Select("dtdeliverydateday");
        }
        charpos = strField.search("[^0-9.]"); 
        if(strField.length > 0 &&  charpos >= 0)
        {
            boolValid = false;
            alert("The delivery date you have provided does not appear to be valid");
            Select("dtdeliverydateday");
        }
        /* check year */
        strField = ValueGet("dtdeliverydateyear");
        if (boolValid == true && strField.length == 0 )
        {
            boolValid = false;
            alert("Please enter the date you would prefer the delivery to be made");
            Select("dtdeliverydateyear");
        }
        charpos = strField.search("[^0-9.]"); 
        if(boolValid == true && strField.length > 0 &&  charpos >= 0)
        {
            boolValid = false;
            alert("The delivery date you have provided does not appear to be valid");
            Select("dtdeliverydateyear");
        }
        if (boolValid == true && ValidDate(ValueGet("dtdeliverydateday"), ValueGet("dtdeliverydatemonth"), ValueGet("dtdeliverydateyear")) == false)
        {
            boolValid = false;
            alert("The delivery date you have provided does not appear to be valid.");
            Select("dtdeliverydateday");
        }
        var datDate = new Date(ValueGet("dtdeliverydateyear"), ValueGet("dtdeliverydatemonth")-1, ValueGet("dtdeliverydateday"));
        if (boolValid == true && datToday > datDate)
        {
            boolValid = false;
            alert("Preferred delivery dates must be in the future, please try a delivery day from tomorrow onwards.\n\nUrgent deliveries can be made the same day, please call 0845 6 00 44 99.");
            Select("dtdeliverydateday");
        }
        if (boolValid == true && datDate.getDay() == 0)
        {
            boolValid = false;
            alert("It is not possible to delivery on Sundays, please choose a different date.");
            Select("dtdeliverydateday");
        }
    }

    if (boolValid == true)
    {
        frm.submit();
    }
}

function CheckOutDeliveryWarning()
{
    if (ValidDate(ValueGet("dtdeliverydateday"), ValueGet("dtdeliverydatemonth"), ValueGet("dtdeliverydateyear")) == true)
    {
        var datDate = new Date(ValueGet("dtdeliverydateyear"), ValueGet("dtdeliverydatemonth")-1, ValueGet("dtdeliverydateday"));
        switch (datDate.getDay())
        {
            case 6 : /* saturday */
                ObjectReveal("deliverywarningsat");
                ObjectConceal("deliverywarningsun");
                ObjectConceal("deliverywarningdate");
                ValueSet("hidsaturdaydelivery", 1);
                break;
            case 0 : /* sunday */
                ObjectConceal("deliverywarningsat");
                ObjectReveal("deliverywarningsun");
                ObjectConceal("deliverywarningdate");
                ValueSet("hidsaturdaydelivery", 0);
                break;
            default :
                ObjectConceal("deliverywarningsat");
                ObjectConceal("deliverywarningsun");
                ObjectConceal("deliverywarningdate");
                ValueSet("hidsaturdaydelivery", 0);
        }
    } else {
        ObjectConceal("deliverywarningsat");
        ObjectConceal("deliverywarningsun");
        ObjectReveal("deliverywarningdate");
        ValueSet("hidsaturdaydelivery", 0);
    }
}

function CheckOutPayment(frm)
{
    /* check all required fields are complate */
    var boolValid = true;
    var strField = "";
    var lngMonth = 0;
    var lngYear = 0;
    var dblDate = new Date() ;
    
    /* cardholdername */
    strField = ValueGet("txtcardholdername");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the name as shown on the card");
        Select("txtcardholdername");
    }
    
    /* cardnumber */
    strField = ValueGet("txtcardnumber");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the long number shown on the front of the card");
        Select("txtcardnumber");
    }
    if (boolValid == true && ValidateCardNumber(strField) == false )
    {
        boolValid = false;
        alert("The card number you provided is not valid, please check the number and try again");
        Select("txtcardnumber");
    }
    
    /* has the right card type been selected */
    strField = ValueGet("txtcardnumber");
    
    if (boolValid == true)
    {
        switch (ValidateCardType(strField))
        {
            case "American Express" :
                if (CheckedGet("optcardtype5") != true)
                {
                    alert("The card number and card type you have selected do not appear to match.\nPlease check both and try again");
                    boolValid = false;
                }
                break;
            case "Visa" :
                if (CheckedGet("optcardtype1") != true && CheckedGet("optcardtype4") != true)
                {
                    alert("The card number and card type you have selected do not appear to match.\nPlease check both and try again");
                    boolValid = false;
                }
                break;
            case "Visa Electron" :
                if (CheckedGet("optcardtype6") != true)
                {
                    alert("The card number and card type you have selected do not appear to match.\nPlease check both and try again");
                    boolValid = false;
                }
                break;
            case "Mastercard" :
                if (CheckedGet("optcardtype2") != true)
                {
                    alert("The card number and card type you have selected do not appear to match.\nPlease check both and try again");
                    boolValid = false;
                }
                break;
            case "Maestro" :
                if (CheckedGet("optcardtype7") != true)
                {
                    alert("The card number and card type you have selected do not appear to match.\nPlease check both and try again");
                    boolValid = false;
                }
                break;
            case "Switch" :
                if (CheckedGet("optcardtype3") != true)
                {
                    alert("The card number and card type you have selected do not appear to match.\nPlease check both and try again");
                    boolValid = false;
                }
                break;
            default :
                alert("Card Type: " + ValidateCardType(strField));
        }
    }
    
    /* valid from (switch only) */
    if (boolValid == true && CheckedGet("optcardtype3") == true)
    {
        /* validmonth */
        strField = document.getElementById("cmbvalidmonth");
        if (boolValid == true && strField.selectedIndex == 0 )
        {
            boolValid = false;
            alert("You must select the month in which this card became valid");
            Focus("cmbvalidmonth");
        }
        /* validyear */
        strField = document.getElementById("cmbvalidyear");
        if (boolValid == true && strField.selectedIndex == 0 )
        {
            boolValid = false;
            alert("You must select the year in which this card became valid");
            Focus("cmbvalidyear");
        }
        /* check valid date is not after this month and year */
        if (boolValid == true)
        {
            lngMonth = ValueGet("cmbvalidmonth");
            lngYear = ValueGet("cmbvalidyear");
            if (Number(lngYear) == Number(dblDate.getFullYear()) && Number(lngMonth) > Number(dblDate.getMonth() + 1))
            {
                boolValid = false;
                alert("The valid from date of this card shows the card is not yet valid");
                Focus("cmbvalidmonth");
            }
        }
    }

    /* expiresmonth */
    strField = document.getElementById("cmbexpiresmonth");
    if (boolValid == true && strField.selectedIndex == 0 )
    {
        boolValid = false;
        alert("You must select the month in which this card expires");
        Focus("cmbexpiresmonth");
    }
    
    /* expiresyear */
    strField = document.getElementById("cmbexpiresyear");
    if (boolValid == true && strField.selectedIndex == 0 )
    {
        boolValid = false;
        alert("You must select the year in which this card expires");
        Focus("cmbexpiresyear");
    }
    /* check expiry date is not before this month and year */
    if (boolValid == true)
    {
        lngMonth = ValueGet("cmbexpiresmonth");
        lngYear = ValueGet("cmbexpiresyear");
        if (Number(lngYear) == Number(dblDate.getFullYear()) && Number(lngMonth) < Number(dblDate.getMonth() + 1))
        {
            boolValid = false;
            alert("The expiry date for this card must not have already expired");
            Focus("cmbexpiresmonth");
        }
    }
    
    /* is issue number required and if so is it valid (switch only) */
    strField = ValueGet("txtissuenumber");
    if (boolValid == true && strField.length == 0 && CheckedGet("optcardtype3") == true)
    {
        boolValid = false;
        alert("You must provide the issue number from your card");
        Select("txtissuenumber");
    }
    if (boolValid == true && CheckedGet("optcardtype3") == true)
    {
        var charpos = strField.search("[^0-9.]"); 
        if(strField.length > 0 &&  charpos >= 0)
        {
            boolValid = false;
            alert("The issue number you have provided does not appear to be valid");
            Select("txtissuenumber");
        }
    }
        
    /* securitycode */
    strField = ValueGet("txtsecuritycode");
    if (boolValid == true && strField.length == 0 )
    {
        boolValid = false;
        alert("You must provide the security code from the card");
        ObjectReveal("securitycode");
        Select("txtsecuritycode");
    }
    if (boolValid == true && Number(strField) == false )
    {
        boolValid = false;
        alert("You must provide the security code from the card");
        ObjectReveal("securitycode");
        Select("txtsecuritycode");
    }
    /* is this an american express card, if so then must have four characters for the security code */
    if (boolValid == true && strField.length != 4 && CheckedGet("optcardtype5") == true)
    {
        boolValid = false;
        alert("You must provide a valid security code from the card");
        ObjectReveal("securitycode");
        Select("txtsecuritycode");        
    }
    /* any other cards must have three characters for the security code */
    if (boolValid == true && strField.length != 3 && CheckedGet("optcardtype5") != true)
    {
        boolValid = false;
        alert("You must provide a valid security code from the card");
        ObjectReveal("securitycode");
        Select("txtsecuritycode");        
    }
    
    if (boolValid == true)
    {
        frm.submit();
    }
}

function CheckOutSecurityCode()
{
    if (document.getElementById("securitycode").style.display == "none" || document.getElementById("securitycode").style.display == "")
    {
        ObjectReveal("securitycode");
    } else {
        ObjectConceal("securitycode");
    }
}





Step1Message = "Credit card is not known.\nCredit card number is invalid.";
Step2Message = "Credit card number is invalid.";
Step3Message = "Credit card is not known.";
DisplayAllOkayMessage = "y";


ccType = '';
ccNumberOkay = 0;
ccNumberChecked = '';

function StripNonDigits(number)
{
    var Re = /\d+/g;
    if(Re.lastIndex > 1) { Re.lastIndex = 0; }
    var Array = Re.exec(number);
    if(Re.lastIndex < 1) { return 'X'; }
    var ss = Array.join();
    while(Re.lastIndex > 0 && Re.lastIndex < number.length)
    {
        Array = Re.exec(number);
        if(Array) { ss += Array.join(); }
    }
    return ss;
} // StripNonDigits()


function GetType(number)
{
var len = number.length;
var Re = /^5[1-5]/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		var Array = Re.exec(number);
			if(Array && len == 16) { return 'Mastercard'; }
Re = /^(4903|4905|4911|4936|564182|633110|6333|6759)/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 16 || len == 18 || len == 19)) { return 'Switch'; }
Re = /^(417500|4917|4913)/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 16)) { return 'Visa Electron'; }
Re = /^4/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 13 || len == 16)) { return 'Visa'; }
Re = /^3[47]/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 15) { return 'American Express'; }
Re = /^(3|1800|2131)/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 15 || len == 16)) { return 'JCB'; }
Re = /^(5020|5038|6304|6759)/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 16 || len == 18)) { return 'Maestro'; }
return '';
} // GetType()


function Reverse(number) {
var n = '';
for(i = number.length; i >= 0; i--) { n += number.substr(i,1); }
return n;
} // Reverse()


function AddedTogether(number) {
var n = 0;
for(i = 0; i < number.length; i++) {
	var s = number.substr(i,1);
	var si = parseInt(s,10);
	if(i % 2 > 0) {
		var ii = si * 2;
		if(ii < 10) { n += ii; }
		else {
			var ss = ' ' + ii;
			for(xi = 1; xi < ss.length; xi++) {
				var xs = ss.substr(xi,1);
				var xsi = parseInt(xs,10);
				n += xsi;
				} // for
			} // else
		} // if
	else { n += si; }
	} // for
return n;
} // AddedTogether()


function Mod10(n) {
var reversed = Reverse(n);
var total = AddedTogether(reversed);
if(total % 10 > 0) { return 0; }
return 1;
} // Mod10()


function Validate(n)
{
    ccNumberChecked = StripNonDigits(n);
    if(ccNumberChecked == 'X') {
        alert('Please supply a credit card number.');
        return false;
        }
    ccType = GetType(ccNumberChecked);
    ccNumberOkay = Mod10(ccNumberChecked);
    var fourth = '-- Credit Card --\n' + ccType + '\n' + ccNumberChecked;
    if(     Step1Message.length > 0 && ccType.length < 2 && ccNumberOkay == 0) { alert(Step1Message); }
    else if(Step2Message.length > 0 && ccNumberOkay == 0                     ) { alert(Step2Message); }
    else if(Step3Message.length > 0 && ccType.length < 2                     ) { alert(Step3Message); }
    else if(DisplayAllOkayMessage == "y" || DisplayAllOkayMessage == "Y"     ) { alert(fourth);       }
    return false;
} 
function ValidateCardNumber(n)
{
    ccNumberChecked = StripNonDigits(n);
    if(ccNumberChecked == 'X')
    {
        return false;
    } else {
        ccNumberOkay = Mod10(ccNumberChecked);
        if (ccNumberOkay != 0)
        {
            return true;
        } else {
            return false;
        }
    }
} 
function ValidateCardType(n)
{
    ccNumberChecked = StripNonDigits(n);
    if(ccNumberChecked == 'X')
    {
        return false;
    } else {
        ccType = GetType(ccNumberChecked);
        if (ccType.length >= 2)
        {
            return ccType;
        } else {
            return false;
        }
    }
} 



function ValidDate(day, month, year)
{
    var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    
	if (!day || !month || !year)
    {
        return false
    }
	if (year/4 == parseInt(year/4))
    {
        monthLength[1] = 29;
    }
	if (day > monthLength[month-1])
    {
        return false;
    }
    return true;
}