
function redirectLink(o) 
{
    var subscriptionChannel = document.getElementById('scontent').options[document.getElementById('scontent').selectedIndex].value;
    var subscriptionPlan = document.getElementById('subscriptions').options[document.getElementById('subscriptions').selectedIndex].value;
    window.location = 'index.php?module=userAccount&action=buyCredits&page=buySubscription&subscriptionChannel=' + subscriptionChannel + "&subscriptionPlan=" + subscriptionPlan;
}

function redirectLink_credit(o) 
{
    var creditAmount =  document.getElementById('amount').value;
    window.location = 'index.php?module=userAccount&action=buyCredits&page=buyCredit&creditAmount=' + creditAmount; 
}

function CashUButtonClicked1()
{
    if(check()) 
    {
        x = document.cashuform.amount.value;
        y = document.BuyForm.amount.value;                     
        
        if (x != y) document.cashuform.amount.value = y;
        
        document.cashuform.submit();        
    }
}

function payPalButtonClicked1() 
{
    if(check()) 
    {
        x = document.paypalform.amount.value;
        y = document.BuyForm.amount.value;
        
        if (x!=y) document.paypalform.amount.value=y;
        
        document.paypalform.submit();		
	}
}		


function payWizardButtonClicked1() 
{
    if(check()) 
    {
        x = document.paywizardform.retailerItemCost.value;
        y = document.BuyForm.amount.value;
        
        if (x!=y) document.paywizardform.retailerItemCost.value = y;
        
        document.paywizardform.submit();        
    }
}

function payPalButtonClicked2()  
{
      payPalButtonClicked1();
}

function clickandbuyClicked() 
{


    var price = document.getElementById("price");
    var amount = document.getElementById("amount");
    
    if(amount.value.indexOf(',')) {
//	alert("Please Specify Top-up Amount");
//	return 1;
    }
    
    var tmp = parseFloat(amount.value) * 100;
    if(tmp) {
	amount.value = Math.round(parseFloat(tmp)) / 100;
    }

    if(!(amount.value > 0)) {
	alert("Please Specify Top-up Amount \n Only numbers and decimal point characters are allowed.");
	return 1;
    }

    if((amount.value > maxPayment)) {
	alert("Sorry, maximum Top-Up Amount is " + maxPayment + ".");
	return 1;
    }
    
    if((amount.value < minPayment)) {
	alert("Sorry, minimum payment is " + minPayment + ".");
	return 1;
    }
    
    try {
        currency = document.getElementById('currency').selectedIndex;
        c = document.getElementById('cb_currency');
        if(currency == 0) {
            c.value = "GBP";
        }
        if(currency == 1) {
            c.value = "EUR";
        }
        if(currency == 2) {
            c.value = "USD";
        }
    } catch(e) {
    }
    var cx = document.forms[1].currency_code;
    if(cx.value != '' && cx.value != 'GBP') {
	//alert('Sorry, we do not support selected currency with selected payment gateway,\nbut we might enable it in future.\nPlease select GBP currency.');
	//return 1;
    }
    

    try {
        if(amount) {
            if(amount.value < 0.1) {
                alert("Specified amount should be grater then 0.1");
                return;
            }
            price.value = amount.value * 100;
            document.clickandbuyForm.submit();
        }
    } catch(e) {
    }
    document.clickandbuyForm.submit();
}

var func;

function check() 
{
    if(document.BuyForm.amount.value > 0) 
    {
        var amount = document.getElementById("amount");
        
        if((amount.value > maxPayment)) 
        {
            alert("Sorry, maximum Top-Up Amount is " + maxPayment + ".");
            return false;
        }
        
        if((amount.value < minPayment)) 
        {
            alert("Sorry, minimum payment is " + minPayment + ".");
            return false;
        }
        
        return true;
        
    } else 
    {
        alert("Specified amount should be greater then 0");
        return false;
    }
}

function checkWorldpay() {
    var price = document.getElementById("price");
    var amount = document.getElementById("amount");
    
    var tmp = parseFloat(amount.value) * 100;
    if(tmp) {
        amount.value = Math.round(parseFloat(tmp)) / 100;
    }

    if(!(amount.value > 0)) {
        alert("Please Specify Top-up Amount \n Only numbers and decimal point characters are allowed.");
        return 1;
    }

    if(amount.value < minPayment) {
        alert("Sorry, minimum payment is " + minPayment + ".");
        return 1;
    }

    if((amount.value > maxPayment)) {
        alert("Sorry, maximum Top-Up Amount is " + maxPayment + ".");
        return 1;
    }
    
    

    try {
        if(amount) {
            price.value = amount.value * 100;
            document.clickandbuyForm.submit();
        }
    } catch(e) {
    }

    return 0;   
    
}

function check2() {
    return true;
}
 
function calc1() {
    if(func == "calculate1") {
        calculate1();
    }
    if(func == "calculate2") {
        calculate2();
    }
    if(func == "calculate3") {
        calculate3();
    }
    if(func == "calculate4") {
        calculate4();
    }
}

function calculate1() {
    document.BuyForm.credits.value = Math.round(document.BuyForm.amount.value * 1000 * 100)/100;
    document.BuyForm.tim.value = Math.round(document.BuyForm.credits.value / 1000 * 100)/100;
}

function calculate2() {
    var val = Math.round(document.BuyForm.credits.value / 1000 * 100) / 100;
    document.BuyForm.amount.value = val.toString();
    var val2 = Math.round(document.BuyForm.credits.value / 1000 * 100) / 100;
    document.BuyForm.tim.value = val2.toString();
}

function calculate3() {
    var val = Math.round(document.BuyForm.tim.value * 1000 * 100) / 100;
    document.BuyForm.credits.value = val.toString();
    val2 = Math.round(document.BuyForm.amount.value * 100)/100;
    document.BuyForm.amount.value = Math.round(document.BuyForm.credits.value / 1000 * 100)/100;
}

function calculate4() {
    var val = document.BuyForm2.month;
    var val2 = document.getElementById("flatAmount");
    var cartsid = new Array(1);
    var a = document.BuyForm2.amount;
    var b = a.value;
    var currency2 = document.forms[2].currency2;
    var currency3 = document.forms[1].currency3;
    a.value = prices[val.value * currency3.value].toString();
    val2.innerHTML = formatCurrency(prices[val.value + currency3.value], "");
    document.BuyForm2.cartId.value = cartsid[val.value];
    document.BuyForm2.desc.value = "Subscription";	
}

function buy1() {
    var d;
    d = document.getElementById("buy1Div");
    d.style.visibility = "";
    d = document.getElementById("buy2Div");
    d.style.visibility = "hidden";
}

function buy2() {
    var d;
    d = document.getElementById("buy1Div");
    d.style.visibility = "hidden";
    d = document.getElementById("buy2Div");
    d.style.visibility = "";
}

 
function changeCurrency(sel) {
    var currency = document.forms[1].currency_code;
    currency.value = document.getElementById('currency').options[document.getElementById('currency').selectedIndex].value;
}

function formatCurrency(num, currencySymbol) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    if(currencySymbol != "Credits") {
        return (((sign)?'':'-') + currencySymbol + num + '.' + cents);
    } else {
        return (((sign)?'':'-') + num + '.' + cents + " Credits");
    }
}

