﻿$(document).ready(function() {
            
    $("a[rel='fbHTML']").click(function(){
        jQuery.facebox("<h2>Redirecting to WorldPay...</h2>");
    });
    
});

function PayQuarterly()
{
    Pay('Membership Quarterly Subscription', oConfig.FirstPriceQuart, oConfig.NormalPriceQuart, 'Q', oConfig);
}

function PayYearly()
{
    Pay('Membership Yearly Subscription', oConfig.FirstPriceYear, oConfig.NormalPriceYear, 'Y', oConfig);
}

function PayFixed(cartID, instID, currency, description, amount, orderId, config)
{
    var frmCO = document.createElement('form');
    frmCO.setAttribute("method", "post");
    frmCO.setAttribute("action", "https://select.worldpay.com/wcc/purchase");
    frmCO.setAttribute("display", "none");
    
    frmCO.appendChild(CreateInput("instId", '213793'));
    frmCO.appendChild(CreateInput("cartId", cartID));
    frmCO.appendChild(CreateInput("currency", currency));
    frmCO.appendChild(CreateInput("description", description));
    frmCO.appendChild(CreateInput("amount", amount));
    frmCO.appendChild(CreateInput("M_order", orderId));
    frmCO.appendChild(CreateInput("M_cart", cartID));

    if (config.Type > '')
        frmCO.appendChild(CreateInput("M_type", config.Type));
    else
        frmCO.appendChild(CreateInput("M_type", "6"));

    frmCO.appendChild(CreateInput("M_ReffererId", config.ReffererId));
    frmCO.appendChild(CreateInput("M_RefUserId", config.RefUserId));
    frmCO.appendChild(CreateInput("M_RefConversionType", config.RefConversionType));
    frmCO.appendChild(CreateInput("M_SignUpJobSeekerID", config.SignUpJobSeekerID));
    
    document.getElementsByTagName("body")[0].appendChild(frmCO);
    frmCO.submit();
}


function PayOnceOff(cartID, instID, currency, description, amount, orderId, config)
{
    var frmCO = document.createElement('form');
    frmCO.setAttribute("method", "post");
    frmCO.setAttribute("action", "https://select.worldpay.com/wcc/purchase");
    frmCO.setAttribute("display", "none");
    
    frmCO.appendChild(CreateInput("instId", '213793'));
    frmCO.appendChild(CreateInput("cartId", cartID));
    frmCO.appendChild(CreateInput("currency", currency));
    frmCO.appendChild(CreateInput("description", description));
    frmCO.appendChild(CreateInput("amount", amount));
    frmCO.appendChild(CreateInput("M_order", orderId));
    frmCO.appendChild(CreateInput("M_cart", cartID));
    frmCO.appendChild(CreateInput("M_type", "C"));
    frmCO.appendChild(CreateInput("M_ReffererId", config.ReffererId));
    frmCO.appendChild(CreateInput("M_RefUserId", config.RefUserId));
    frmCO.appendChild(CreateInput("M_RefConversionType", config.RefConversionType));
    
    document.getElementsByTagName("body")[0].appendChild(frmCO);
    frmCO.submit();
}

function Pay(description, firstAmount, normalAmount, type, config)
{    
    var frmCO = document.createElement('form');
    frmCO.setAttribute("method", "post");
    frmCO.setAttribute("action", "https://select.worldpay.com/wcc/purchase");
    frmCO.setAttribute("display", "none");
    
    frmCO.appendChild(CreateInput("instId", '213793'));
    frmCO.appendChild(CreateInput("cartId", config.CartID));
    frmCO.appendChild(CreateInput("authMode", 'A'));
    frmCO.appendChild(CreateInput("M_order", config.OrderID));
    frmCO.appendChild(CreateInput("M_cart", config.Cart));
    frmCO.appendChild(CreateInput("M_type", type));
    frmCO.appendChild(CreateInput("amount", firstAmount));
    frmCO.appendChild(CreateInput("currency", 'GBP'));
    frmCO.appendChild(CreateInput("desc", description));
    frmCO.appendChild(CreateInput("testMode", config.TestMode));
    frmCO.appendChild(CreateInput("country", config.Country));
    frmCO.appendChild(CreateInput("postcode", config.PostCode));
    frmCO.appendChild(CreateInput("tel", config.Tel));
    frmCO.appendChild(CreateInput("name", config.Name));
    frmCO.appendChild(CreateInput("M_ReffererId", config.ReffererId));
    frmCO.appendChild(CreateInput("M_RefUserId", config.RefUserId));
    frmCO.appendChild(CreateInput("M_RefConversionType", config.RefConversionType));
    frmCO.appendChild(CreateInput("M_SignUpJobSeekerID", config.SignUpJobSeekerID));
                
    frmCO.appendChild(CreateInput("futurePayType", "regular"));
    frmCO.appendChild(CreateInput("noOfPayments", 0));
    
    switch(type)
    {
        case "Q":
            frmCO.appendChild(CreateInput("intervalUnit", 3));
            frmCO.appendChild(CreateInput("intervalMult", 3));
            frmCO.appendChild(CreateInput("startDate", config.StartDateQuart));
            break;                
            
        case "Y":
            frmCO.appendChild(CreateInput("intervalUnit", 4));
            frmCO.appendChild(CreateInput("intervalMult", 1));
            frmCO.appendChild(CreateInput("startDate", config.StartDateYear));
            break;
            
        case "M":
            frmCO.appendChild(CreateInput("intervalUnit", 3));
            frmCO.appendChild(CreateInput("intervalMult", 1));
            frmCO.appendChild(CreateInput("startDate", config.StartDateYear));
            break;
    }
    
    frmCO.appendChild(CreateInput("normalAmount", normalAmount));
    frmCO.appendChild(CreateInput("option", 0));
    
    document.getElementsByTagName("body")[0].appendChild(frmCO);
    frmCO.submit();
}

function CreateInput(keyName, keyValue)
{
    var ipt = document.createElement("input");
    ipt.setAttribute("name", keyName);
    ipt.setAttribute("type", "hidden");
    ipt.setAttribute("value", keyValue);
    return ipt;
}
