﻿function checkData(theform)
{
    if (checkBooking(theform))
    {
        if (checkCC(theform))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

function checkCC(theform) 
{
    thisMonth = new Date().getMonth() + 1;
    thisYear = new Date().getYear();

    //if (!theform.ctl00_cp_mainright_chkAgreed.checked) 
    //{
        //alert("Please click the Box to Agree to the Terms and Conditions")
        //theform.ctl00_cp_mainright_chkAgreed.focus()
        //return false 
    //}
    //else 
    
    if (theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value == "") 
    {
        alert("Please choose a Payment Type")
        theform.ctl00_cp_mainright_txtCardName.focus()
        return false 
    }
    else if (validCardTypes[theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value] == null) 
    {
        return true;
    }
    else if (theform.ctl00_cp_mainright_txtCardName.value.length == 0 && theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value != "PF") 
    {
        alert("Please enter the name on your Credit Card")
        theform.ctl00_cp_mainright_txtCardName.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtCardNum.value.length == 0 && theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value != "PF") 
    {
        alert("Please enter a Card number")
        theform.ctl00_cp_mainright_txtCardNum.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_ddlCardExpMonth.options[theform.ctl00_cp_mainright_ddlCardExpMonth.selectedIndex].value < thisMonth && theform.ctl00_cp_mainright_ddlCardExpYear.options[theform.ctl00_cp_mainright_ddlCardExpYear.selectedIndex].value == thisYear && theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value != "PF") 
    {
        alert("The Expiry Month is not Valid")
        theform.ctl00_cp_mainright_txtCardNum.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_ddlCardExpYear.options[theform.ctl00_cp_mainright_ddlCardExpYear.selectedIndex].value < thisYear && theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value != "PF") 
    {
        alert("The Expiry Year is not Valid")
        theform.ctl00_cp_mainright_txtCardNum.focus()
        return false 
    }
    else if (checkCard(theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value,theform.ctl00_cp_mainright_txtCardNum.value) == 0) 
    {
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtCardCVVCode.value.length == 0 && theform.ctl00_cp_mainright_ddlCCType[theform.ctl00_cp_mainright_ddlCCType.selectedIndex].value != "PF") 
    {
        alert("Please enter a Card ID number")
        theform.ctl00_cp_mainright_txtCardCVVCode.focus()
        return false 
    }
    else  
    {
        return true
    }  	
}


function checkCardNumWithMod10(card_num) 
{
    var i;
    var cc = new Array(16);
    var checksum = 0;
    var validcc;

    // assign each digit of the card number to a space in the array
    for (i = 0; i < card_num.length; i++) 
    {
        cc[i] = Math.floor(card_num.substring(i, i+1));
    }

     // walk through every other digit doing our magic
     // if the card number is sixteen digits then start at the
     // first digit (position 0), otherwise start from the
     // second (position 1)
    for (i = (card_num.length % 2); i < card_num.length; i +=2) 
    {
        var a = cc[i] * 2;
        if (a >= 10) 
        {
            var aStr = a.toString();
            var b = aStr.substring(0,1);
            var c = aStr.substring(1,2);
            cc[i] = Math.floor(b) + Math.floor(c);
        } 
        else 
        {
            cc[i] = a;
        }
    }

    // add up all of the digits in the array
    for (i = 0; i < card_num.length; i++) 
    {
        checksum += Math.floor(cc[i]);
    }

    // if the checksum is evenly divisble by 10
    // then this is a valid card number
    validcc = ((checksum % 10) == 0);
    return validcc;
}

function cleanCardNum(card_num) 
{

    var i;
    var ch;
    var newCard = "";

    // walk through the string character by character to build
    // a new string with numbers only
    i = 0;
    while (i < card_num.length) 
    {
        // get the current character
        ch = card_num.substring(i, i+1);
        if ((ch >= "0") && (ch <= "9")) 
        {
            // if the current character is a digit then add it
            // to the numbers-only string we're building
            newCard += ch;
        } 
        else 
        {
            // not a digit, so check if its a dash or a space
            if ((ch != " ") && (ch != "-")) 
            {
                // not a dash or a space so fail
                alert("The card number contains invalid characters.");
                return 0;
            }
         }
         i++;
    }
    // we got here if we didn't fail, so return what we built
    return newCard;
}

validCardTypes = new Object();
 
validCardTypes['VI'] = true;    
validCardTypes['CA'] = true;
validCardTypes['AX'] = true;    
validCardTypes['DS'] = true;
validCardTypes['A3'] = true;    
validCardTypes['CB'] = true;
validCardTypes['DC'] = true;    
validCardTypes['EC'] = true;
validCardTypes['ER'] = true;    
validCardTypes['JC'] = true;
validCardTypes['O1'] = true;
 
function checkCard(cardType, card_num) 
{

    var validCard;
    var cardLength;
    var cardLengthOK;
    var cardStart;
    var cardStartOK;

    if (cardType == "PF") 
    { // there's nothing to check here
        return 1;
    }

    // check if the card type is valid
    if (validCardTypes[cardType] == null) {
        alert("Please select a card type.");
        return 0;
    }

    if (cardType != "VI" && cardType != "CA" && cardType != "AX" && cardType != "DS") 
        return 1; // the above codes are the only ones we can verify
    
    // clean up any spaces or dashes in the card number
    validCard = cleanCardNum(card_num);
    if (validCard != "") 
    {
        // check the first digit to see if it matches the card type
        cardStart = validCard.substring(0,1);
        cardStartOK = ( ((cardType == "VI") && (cardStart == "4")) ||
            ((cardType == "CA") && (cardStart == "5")) ||
            ((cardType == "AX") && (cardStart == "3")) ||
            ((cardType == "DS") && (cardStart == "6")) );
        if (!(cardStartOK)) {
            // card number's first digit doesn't match card type
            alert("Please make sure the card number you've entered matched the card type you selected.");
            return 0;
        }

        // the card number is good now, so check to make sure
        // it's a the right length
        cardLength = validCard.length;
        cardLengthOK = ( ((cardType == "VI") && ((cardLength == 13) || (cardLength == 16))) ||
            ((cardType == "CA") && (cardLength == 16)) ||
            ((cardType == "AX") && (cardLength == 15)) ||
            ((cardType == "DS") && (cardLength == 16)) );
        if (!(cardLengthOK)) 
        {
            // not the right length
            alert("Please make sure you've entered all of the digits on your card.");
            return 0;
        }

        // card number seems OK so do the Mod10
        if (checkCardNumWithMod10(validCard)) 
        {
            return 1;
        } 
        else 
        {
            alert("Please make sure you've entered your card number correctly.");
            return 0;
        }
    } 
    else 
    {
        return 0;
    }
 }



function checkBooking(theform)
{
    //just checking that the mandatory fields are completed.
    if (theform.ctl00_cp_mainright_txtFirstName.value.length == 0) 
    {
        alert("Please enter a First name")
        theform.ctl00_cp_mainright_txtFirstName.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtSurname.value.length == 0) 
    {
        alert("Please enter a Surname")
        theform.ctl00_cp_mainright_txtSurname.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtEmail.value.length == 0) 
    {
        alert("Please enter an Email address")
        theform.ctl00_cp_mainright_txtEmail.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtPhone.value.length == 0) 
    {
        alert("Please enter a Phone Number")
        theform.ctl00_cp_mainright_txtPhone.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtAddress.value.length == 0) 
    {
        alert("Please enter an Address")
        theform.ctl00_cp_mainright_txtAddress.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtCity.value.length == 0) 
    {
        alert("Please enter a City")
        theform.ctl00_cp_mainright_txtCity.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtPostcode.value.length == 0) 
    {
        alert("Please enter a Zip/Post Code")
        theform.ctl00_cp_mainright_txtPostcode.focus()
        return false 
    }
    else if (theform.ctl00_cp_mainright_txtCountry.value.length == 0) 
    {
        alert("Please enter a Country")
        theform.ctl00_cp_mainright_txtCountry.focus()
        return false 
    }
    else  
    { 
        return true
    }  	
}