function addToBasket( productid, jsessionid )
{
    var frm = document.forms["search"];
    var quantity = trim(frm.quantity.value) ;

    if ( quantity == 0 || isNaN (quantity ))
    {
        document.getElementById('pleaseenteranumber').style.display = 'block' ;
        setTimeout ( function()
        {
            var frm = document.forms["search"] ;
            var url = "product.jsp?productid=" + productid + "&jsessionid=" + jsessionid ;
            frm.method="post" ;
            frm.action = url ;
            frm.submit() ;
        }, 1000 );

        frm.quantity.value = 1 ;
        return false ;
    }
    else
    {
        document.getElementById('addedtobasket').style.display = 'block' ;
        setTimeout (
            function()
            {
                var frm = document.forms["search"] ;
                var url = "shop/ba?productid=" + productid + "&jsessionid=" + jsessionid ;
                frm.method="post" ;
                frm.action = url ;
                frm.submit() ;
            }, 1000 );
        return true ;
    }
}

function deleteFromBasket( productid, jsessionid )
{
    document.getElementById('deletedfrombasket').style.display = 'block' ;
    setTimeout (
        function()
        {
            var frm = document.forms["search"] ;
            var url = "shop/bd?productid=" + productid + "&jsessionid=" + jsessionid ;
            frm.method="post" ;
            frm.action = url ;
            frm.submit() ;
        }, 1000 );
}

function deleteAllFromBasket( jsessionid )
{
    document.getElementById('deleteallfrombasket').style.display = 'block' ;
    setTimeout (
        function()
        {
            var frm = document.forms["search"] ;
            var url = "shop/bda?jsessionid=" + jsessionid ;
            frm.method="post" ;
            frm.action = url ;
            frm.submit() ;
        }, 1000 );
}

function recalculate(jsessionid)
{
    var frm = document.forms["search"];
    var error = 0 ;
    
    // iterate through the elements
    for(i=0; i< frm.elements.length; i++)
    {
        var fieldName = frm.elements[i].name ;
        var fieldValue = frm.elements[i].value ;
        
        if (!fieldName.indexOf("quantity"))
        {
            if ( trim(fieldValue)=="" )
            {
                error = 1;
                break ;
            }
            if ( isNaN(trim(fieldValue)))
            {
                error = 2 ;
                break ;
            }
            if ( (trim(fieldValue) % 1)!= 0  )
            {
                error = 3 ;
                break ;
            }
        }
    }

    switch (error)
    {
        case 1:
            document.getElementById('pleaseenteranumber').style.display = 'block' ;
            setTimeout ( function()
            {
                var frm = document.forms["search"] ;
                var url = "basket.jsp?jsessionid=" + jsessionid ;
                frm.method="post" ;
                frm.action = url ;
                frm.submit() ;
            }, 1000 );
            break;
        case 2:
            document.getElementById('pleaseenteranumber').style.display = 'block' ;
            setTimeout ( function()
            {
                var frm = document.forms["search"] ;
                var url = "basket.jsp?jsessionid=" + jsessionid ;
                frm.method="post" ;
                frm.action = url ;
                frm.submit() ;
            }, 1000 );
            break;
        case 3:
            document.getElementById('onlywholenumbers').style.display = 'block' ;
            setTimeout ( function()
            {
                var frm = document.forms["search"] ;
                var url = "basket.jsp?jsessionid=" + jsessionid ;
                frm.method="post" ;
                frm.action = url ;
                frm.submit() ;
            }, 1000 );
            break;
        default :
            document.getElementById('recalculate').style.display = 'block' ;
            setTimeout (
                function()
                {
                    var frm = document.forms["search"] ;
                    var url = "shop/bu?jsessionid=" + jsessionid ;
                    frm.method="post" ;
                    frm.action = url ;
                    frm.submit() ;
                }, 1000 );
            break ;
            
    }
}

function goToCheckout(jsessionid)
{
    // var jsessionid = getQueryVariable('jsessionid') ;
    var frm = document.forms["search"] ;
    var url = "shop/ctg?jsessionid="+jsessionid ;
    frm.method="post" ;
    frm.action = url ;
    frm.submit() ;
}

function continueToAddDetails(jsessionid )
{
    var frm = document.forms["search"] ;
    var url = "shop/dac?jsessionid=" + jsessionid ;
    frm.method="post" ;
    frm.action = url ;
    frm.submit() ;
}

function continueToEnterCard(jsessionid)
{
    var isError = 0 ;
    var frm = document.forms["search"] ;
    
    if ( trim(frm.detailscontactnameinput.value) == "" )
    {
        document.getElementById('contactnameerror').style.display = 'block' ;
        isError = 1 ;
    }
    else
    {
        document.getElementById('contactnameerror').style.display = 'none' ;
    }
   
    if ( trim(frm.detailscontactphoneinput.value) == "" )
    {
        document.getElementById('contactphoneerror').style.display = 'block' ;
        isError = 1 ;
    }
    else
    {
        document.getElementById('contactphoneerror').style.display = 'none' ;
    }

    var contactphone = trim(frm.detailscontactphoneinput.value) ;

    if(contactphone.search(/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/)==-1)
    {
        document.getElementById('contactphoneerror').style.display = 'block' ;
        isError = 1 ;
    }

    else
    {
        document.getElementById('contactphoneerror').style.display = 'none' ;
    }

    if ( trim(frm.detailscontactemailinput.value) == "" )
    {
        document.getElementById('contactemailerror').style.display = 'block' ;
        isError = 1 ;
    }
    else
    {
        document.getElementById('contactemailerror').style.display = 'none' ;
    }

    if (!isValidEmail(trim(frm.detailscontactemailinput.value)))
    {
        document.getElementById('contactemailerror').style.display = 'block' ;
        isError = 2 ;
    }
    else
    {
        document.getElementById('contactemailerror').style.display = 'none' ;
    }
   
    if ( trim(frm.billingstreet1input.value) == "" )
    {
        document.getElementById('billingstreet1error').style.display = 'block' ;
        isError = 1 ;
    }
    else
    {
        document.getElementById('billingstreet1error').style.display = 'none' ;
    }

    if ( trim(frm.billingcityinput.value) == "" )
    {
        document.getElementById('billingcityerror').style.display = 'block' ;
        isError = 1 ;
    }
    else
    {
        document.getElementById('billingcityerror').style.display = 'none' ;
    }

    if ( trim(frm.billingpostcodeinput.value) == "" )
    {
        document.getElementById('billingpostcodeerror').style.display = 'block' ;
        isError = 1 ;
    }
    else
    {
        document.getElementById('billingpostcodeerror').style.display = 'none' ;
    }

    if ( trim(frm.cards.options[frm.cards.selectedIndex].value) == 0 )
    {
        document.getElementById('selectcarderror').style.display = 'block' ;
        isError = 1
    }

    else
    {
        document.getElementById('selectcarderror').style.display = 'none' ;
    }

    if ( isError == 1 || isError == 2 )
    {
        document.getElementById('detailsattention').style.display = 'block' ;
        return false ;
    }

    else
    {
        var url = "shop/cec?jsessionid=" + jsessionid ;
        frm.method="post" ;
        frm.action = url ;
        frm.submit() ;
        return true ;
    }
}

function continueToOrderSummary(jsessionid)
{
    var frm = document.forms["search"] ;
    var isError = 0 ;

    /*
     *
     * Checks for the credit card number
     *
     */
    var cardtype = trim(frm.cardtype.value) ;
    var cardnumber = trim(frm.cardnumberinput.value) ;

    if (!checkCreditCard (cardnumber,cardtype))
    {
        document.getElementById('cardnumberformaterror').style.display = 'block' ;
        isError = 1 ;
    }
    else
    {
        document.getElementById('cardnumberformaterror').style.display = 'none' ;
    }

    /*
     *
     * Checks for the issuenumber, start month and year.  Only do these if they are present on the form
     *
     */

    /* if ( typeof(frm.cardissuenumberinput) != 'undefined' )
    {
        // check that the issue number is a positive integer
        var issueNumber = trim(frm.cardissuenumberinput.value) ;
        var regexIssueNumber = /^\d*$/;

        if (!( regexIssueNumber.test(issueNumber) && issueNumber >  0 ))
        {
            isError = 1 ;
            document.getElementById('cardissuenumbererror').style.display = 'block' ;
        }
        else
        {
            document.getElementById('cardissuenumbererror').style.display = 'none' ;
        }
    } */ 

    // Check the format of the from month
    if ( typeof(frm.cardvalidfrommonth) != 'undefined' )
    {
        var cardvalidfrommonth = trim(frm.cardvalidfrommonth.value) ;

        // test to see if it's blank
        if ( cardvalidfrommonth == "" )
        {
            isError = 2 ;
        }
        
        // add a leading zero if the length is < 2
        if ( cardvalidfrommonth.length < 2)
        {
            cardvalidfrommonth = '0'+cardvalidfrommonth ;
        }

        var regexCardFromMonth = /01|02|03|04|05|06|07|08|09|10|11|12/ ;
        if (!regexCardFromMonth.test(cardvalidfrommonth))
        {
            isError = 2 ;
        }
    }

    // Checks for the valid from year
    if ( typeof(frm.cardvalidfromyear) != 'undefined' )
    {
        var cardvalidfromyear = trim(frm.cardvalidfromyear.value) ;
        // check it's not blank
        if ( cardvalidfromyear == "" )
        {
            isError = 2 ;
        }
   
        //check it's an integer
        // check that the issue number is a positive integer
        var regexValidFromYear = /^\d*$/;

        if (!( regexValidFromYear.test(cardvalidfromyear) && cardvalidfromyear >  0 ))
        {
            isError = 2 ;
        }
   
        //check it's four digits
        if ( cardvalidfromyear.length < 4 )
        {
            isError = 2 ;
        }

        if ( isError == 2 )
        {
            document.getElementById('cardvalidfromerror').style.display = 'block' ;
        }
        else
        {
            document.getElementById('cardvalidfromerror').style.display = 'none' ;
        }
    }

   

    /*
     *
     *  Checks for the expiry month and year - must always be on the form
     *
     */
    // Check the format of the expiry month
    var cardexpirymonth = trim(frm.cardexpirymonth.value);

    // test to see if it's blank
    if ( cardexpirymonth == "" )
    {
        isError = 3 ;
    }
    
    // add a leading zero if the length is < 2
    if ( cardexpirymonth.length < 2)
    {
        cardexpirymonth = '0'+cardexpirymonth ;
    }

    // now compare it to the regex
    var regexCardExpiry = /01|02|03|04|05|06|07|08|09|10|11|12/ ;
    if (!regexCardExpiry.test(cardexpirymonth))
    {
        isError = 3 ;
    }
   
    // Checks for the expiry year
    var cardexpiryyear = trim(frm.cardexpiryyear.value) ;
    // check it's not blank
    if ( cardexpiryyear == "" )
    {
        isError = 3 ;
    }
   
    //check it's an integer
    // check that the issue number is a positive integer
    var regexExpiryYear = /^\d*$/;

    if (!( regexExpiryYear.test(cardexpiryyear) && cardexpiryyear >  0 ))
    {
        isError = 3 ;
    }
   
    //check it's four digits
    if ( cardexpiryyear.length < 4 )
    {
        isError = 3 ;
    }
   
    // if all the above pass, then check that the start date is not greater than the expiry
    var startdate = cardvalidfromyear + cardvalidfrommonth ;
    var expirydate = cardexpiryyear + cardexpirymonth ;

    var datediff = expirydate - startdate ;

    if ( datediff <= 0 )
    {
        isError = 3 ;
    }
    
    if ( isError == 3 )
    {
        document.getElementById('cardexpiryerror').style.display = 'block' ;
    }
    else
    {
        document.getElementById('cardexpiryerror').style.display = 'none' ;
    }

    
    // Check name on card isn't blank
    var cardholdername = trim(frm.cardholdernameinput.value) ;
    //check it's four digits
    if ( cardholdername == "" )
    {
        isError = 4 ;
    }


    if ( isError == 4 )
    {
        document.getElementById('cardholdernameerror').style.display = 'block' ;
    }
    else
    {
        document.getElementById('cardholdernameerror').style.display = 'none' ;
    }

    //checks for the CVV
    var cardcvv = trim(frm.cardcvvinput.value) ;
    //check it's four digits
    if ( cardcvv == "" )
    {
        isError = 5 ;
    }
    
    if ( cardcvv.length != 3 )
    {
        isError = 5 ;
    }
    
    //check it's an integer
    // check that the issue number is a positive integer
    var regexCVV = /^\d*$/;
    if (!( regexCVV.test(cardcvv) && cardcvv >  0 ))
    {
        isError = 5 ;
    }
    
    if ( isError == 5 )
    {
        document.getElementById('cardcvverror').style.display = 'block' ;
    }
    else
    {
        document.getElementById('cardcvverror').style.display = 'none' ;
    }

    if ( isError == 1 || isError == 2 || isError == 3 || isError == 4 || isError == 5 )
    {
        document.getElementById('detailsattention').style.display = 'block' ;
        return false ;
    }
    else
    {
        var url = "shop/osg?jsessionid=" + jsessionid ;
        frm.method="post" ;
        frm.action = url ;
        frm.submit() ;
        return true ;
    }
}

function copyBillingDetails(jsessionid)
{
    var frm = document.forms["search"] ;
    var url = "shop/dbc?jsessionid=" + jsessionid ;
    frm.method="post" ;
    frm.action = url ;
    frm.submit() ;
}

function populateSessionVisa(jsessionid)
{
    var frm = document.forms["search"] ;
    var url = "shop/psv?jsessionid=" + jsessionid ;
    frm.method="post" ;
    frm.action = url ;
    frm.submit() ;
}

function continueToMakePayment( jsessionid )
{
    var isError = 0 ;
    var frm = document.forms["search"] ;
 
    if ( frm.tsandcs.checked )
    {
        document.getElementById('tsandcsattention').style.display = 'none' ;
        document.getElementById('processingpayment').style.display = 'block' ;
        document.getElementById('makepayment').style.display = 'none' ;
        setTimeout (
            function()
            {
                var frm = document.forms["search"] ;
                var url = "shop/ctp?jsessionid=" + jsessionid ;
                frm.method="post" ;
                frm.action = url ;
                frm.submit() ;
            }, 1000 ) ;
        return true ;
    }
    else
    {
        document.getElementById('tsandcsattention').style.display = 'block' ;
        return false ;
    }
}

function continueToAuthentication( jsessionid )
{
    var frm = document.forms["search"] ;

    if ( frm.tsandcs.checked )
    {
        document.getElementById('tsandcsattention').style.display = 'none' ;
        document.getElementById('processingpayment').style.display = 'block' ;
        document.getElementById('makepayment').style.display = 'none' ;
       
        var url = "auth?jsessionid=" + jsessionid ;
        frm.method="post" ;
        frm.action = url ;
        frm.submit() ;
        
        return true ;
    }
    else
    {
        document.getElementById('tsandcsattention').style.display = 'block' ;
        return false ;
    }

}

function isValidEmail(str)
{
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}








