








 /* JAVASCRIPT FUNCTIONS */
/* Set Cookies */
function setcook(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* Get cookie value by name */
function getcook(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
}
/* Check for cookies */
function chkcook(cookname) {
    var cook = getcook(cookname);
    if (cook != "") {
        return getcook(cookname);
    }
    else {
        return '';
    }
}

/* Check registration and pricing information via API */
function checkregprice(domain){

    /* Remove previous instance */
    if(document.getElementById("japi")){
        var element = document.getElementById("japi");
        element.parentNode.removeChild(element);
    }

    /* Call api via script include - data returned as Javascript*/
    var script = document.createElement('script');
    script.src = 'https://brownrice.com/api.php?order=chkdom&domain='+domain;
    script.id='japi';
    document.getElementsByTagName('body')[0].appendChild(script);

    $("#domainlist").html(' ');

}

/* Move domains back and forth between list and shopping basket */
function listswap(elem){

    document.getElementById('nodoms').style.display='none';

    var html = elem.innerHTML;

    var pid = elem.parentNode.id;

    var title = elem.title;
    var id = elem.id;

    elem.parentNode.removeChild(elem);

    var newelem = document.createElement('li');

    newelem.className='result available';

    newelem.innerHTML = html;

    newelem.title=title;
    newelem.id=id;

    newelem.onclick=function(){listswap(this);}

    if(pid == 'domainlist'){
        newelem.getElementsByTagName('div')[0].style.display='block';
        newelem.getElementsByTagName('div')[1].style.display='none';
        newelem.title='Click here to remove from your basket.';
        document.getElementById('domaincart').appendChild(newelem);
    }
    if(pid == 'domaincart'){
        newelem.getElementsByTagName('div')[0].style.display='none';
        newelem.getElementsByTagName('div')[1].style.display='block';
        newelem.title='Click here to add to your basket.';
        document.getElementById('domainlist').appendChild(newelem);
    }
    var items = document.getElementById('domaincart').getElementsByTagName('li');

    var total = '';

    var shoppinglist = '';

    for(i=0;i<items.length;i++){

        var item = items[i];
        //var price = parseFloat(item.getElementsByTagName('span')[0].innerText.replace(/\$/gi, ''));

        if(shoppinglist == ''){
            var sep = '';
        }
        else{
            var sep = '|';
        }

        shoppinglist = shoppinglist+sep+item.id;

        var data = item.id.split(' ');
        //console.log(data[0]+', '+data[1]);
        var price = data[1];
        total = total+','+price;

        setcook("bri-domains", shoppinglist, 3);
        setcook("bri-domains-total", total, 3);


        console.log(shoppinglist);



    }

    $("#regtotal").load("https://brownrice.com/api.php?order=add&nums="+total);
    $('#cart-popup-block').load('https://brownrice.com//api/?order=showcartcontents&requri=js/checkout.js.phtml?loc=404');

}




/* Check for existing customer accounts */
function checkemail(email){
    $("#checkemailresponse").load("https://brownrice.com/api.php?order=acctsearch&email="+email);
}
function unlockact(email, passwd){
    email = btoa(email);
    passwd = btoa(passwd);
    $("#unlocked").load("https://brownrice.com/api.php?order=unlock&email="+email+"&passwd="+passwd);
}
function populatecontactform(customerid){
    document.getElementById('accountinfo').style.display='block';
    $("#popcon").load("https://brownrice.com/api.php?order=populatecform&customerid="+customerid);

}

/* Formats a decimal number as currency, i.e. two decimal places. */
function formatCurrency(num)
{
    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));
    }

    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

/* Updates cookies and checkout form elements with new pricing when customer changes billing cycle */
function updateservicetotal(data){
    var cycle,price;
    data = data.split(' ');
    cycle = data[0];
    price = data[1];
    if(getcook('bri-addons-email') == 1){
        if(cycle == 'quarterly'){
            var emlttl = 3;
        }
        else{
            var emlttl = 12;
        }
        var price = parseFloat(price)+emlttl;
    }
    else{
        var price = parseFloat(price);
    }
    setcook('bri-planttl', price, 3);
    setcook('bri-cycle', cycle, 3);
    document.getElementById('billingcycle').innerHTML = cycle;
    document.getElementById('servicetotal').innerHTML = formatCurrency(price);
    updategrandtotal(getcook('bri-domttl'), price);
}

/* Adds price of domains in cart to service total. Generates grand total. */
function updategrandtotal(domttl, srvttl){
    var ttl = parseFloat(domttl)+parseFloat(srvttl);
    setcook('bri-gttl', ttl, 3);
    document.getElementById('totalcharges').innerHTML = formatCurrency(ttl);
}

/* Javascript equivalent of PHP's empty() function */
function empty(val){
    if(val == ''){
        return true;
    }
    else{
        return false;
    }
}

/* Processes the final signup form, places all information in cookies. */
function processform(){
    var suform    = document.getElementById('signupform');
    var inputs    = suform.getElementsByTagName('input');
    var selects   = suform.getElementsByTagName('select');
    var textareas = suform.getElementsByTagName('textarea');
    var halt      = 0;
    /* Loop through INPUT type fields */
    for(i=0;i<inputs.length;i++){
        var ID    = inputs[i].id;
        var NAME  = inputs[i].name;
        var VALUE = (ID =='phone' ? +phone.getNumber():inputs[i].value);
        /* Browsers have different values when box is checked. Fix this so we know what the value will be. */
        if(inputs[i].checked || inputs[i].value == 'on'){
            VALUE = 'checked';
        }
        /* Check for empty fields which are required. Stop if found. */
        if(ID == 'firstname' || ID == 'lastname' || ID == 'phone' || ID == 'myemail' || ID == 'addressline1' || ID == 'city' || ID == 'state' || ID == 'zip'){
            if(empty(VALUE)){
                /* TODO: Replace this with some nicer validation. */
                //alert('Field '+ID+' is required.');
                inputs[i].style.border='2px solid #ff0000';
                document.getElementById('errordiv').style.display='block';
                var _top = document.getElementById('errordiv').offsetTop;
                window.scrollTo(0, _top);
                setTimeout(function(){document.getElementById('errordiv').style.display='none';}, 5000);
                halt = 1;
            }
            /* extra check  for valid phone format */
            if(ID == 'phone') {
                if(phone.getValidationError() > 0){
                    halt = 1;
                    $("#phone").css("border", "2px solid #ff0000");
                    document.getElementById('errordiv').style.display='block';
                    var _top = document.getElementById('errordiv').offsetTop;
                    window.scrollTo(0, _top);
                } else {
                    setcook(ID, phone.getNumber(), 3);
                }
            }
        }
        /* We don't want this field value in a cookie... */
        if(ID == 'password'){
            VALUE = '*****';
        }
        /* Update / create cookie if not exists */
        if(ID && !getcook(ID) && ID != 'phone'){
            setcook(ID, VALUE, 3);
        }
    }
    /* Loop through SELECT type fields */
    for(i=0;i<selects.length;i++){
        var ID    = selects[i].id;
        var NAME  = selects[i].name;
        var VALUE = selects[i].value;
        if(ID && !getcook(ID)){
            setcook(ID, VALUE, 3);
        }
    }
    /* Loop through TEXTAREA type fields */
    for(i=0;i<textareas.length;i++){
        var ID    = textareas[i].id;
        var NAME  = textareas[i].name;
        var VALUE = textareas[i].value;
        if(ID && !getcook(ID)){
            setcook(ID, VALUE, 3);
        }
    }
    /* Make sure they've accepted the terms */
    if(getcook('terms')!='checked'){
        /* TODO: Nicer validation messages. */
        alert('You must accept Terms.');
        halt == 1;
    }
    /* die if halt has been set. */
    if(halt == 1){
        return false;
    }
    /* Or send the form. We're not actually using any POST data, we'll be getting data from cookies. */
    else{
        document.forms['signupform'].submit();
    }
}

function finish(){

    var flds = new Array();
    var pass = 1;
    flds[0] = document.getElementById('fn');
    flds[1] = document.getElementById('ln');
    flds[2] = document.getElementById('ba');
    flds[3] = document.getElementById('cy');
    flds[4] = document.getElementById('st');
    flds[5] = document.getElementById('zc');
    flds[6] = document.getElementById('em');
    for(i=0;i<flds.length;i++){
        if(flds[i].value == ''){
            pass = 0;
        }


    }
    if(pass == 1){
        get_stripe_token();
    }
    else{
        alert('You must fill out ALL the fields in this form.');
    }
}

function removecartitem(elem, item){
    if(item == 'bri-plan'){
        setcook('bri-plan', 'none', 3);
        var bripttl = getcook('bri-planttl');
        var brigttl = getcook('bri-gttl');
        ngttl = brigttl-bripttl;
        setcook('bri-planttl', '0', 3);
        setcook('bri-gttl', ngttl, 3);
        location.reload();
    }
    if(item == 'bri-addons-domain'){
        setcook('bri-addons-domain', '0', 3);
        var bridttl = getcook('bri-domttl');
        var brigttl = getcook('bri-gttl');
        ngttl = brigttl-bridttl;
        setcook('bri-domttl', '0', 3);
        setcook('bri-domains', '', 3);
        setcook('bri-domains-total', ',0', 3);
        setcook('bri-gttl', ngttl, 3);
        location.reload();
    }
    if(item == 'bri-addons-email'){
        setcook('bri-addons-email', '0', 3);
        var planttl = getcook('bri-planttl');
        nplanttl = planttl-3;
        setcook('bri-planttl', nplanttl, 3);
        setcook('bri-gttl', nplanttl, 3);
        location.reload();
    }

    elem.parentNode.style.display='none';
}


function term(cycle){

    var plan = getcook('bri-plan');
    if(plan == 'vps-sm'){
        var monthly = 7.95;
    }
    if(plan == 'vps-me'){
        var monthly = 17.95;
    }
    if(plan == 'vps-lg'){
        var monthly = 27.95;
    }
    if(getcook('bri-addons-domain') == '1'){
        var domains = getcook('bri-domttl');
    }
    else{
        var domains = 0;
    }
    if(cycle == 'cycley'){
        setcook('bri-cycle', 'annually', 3);
        document.getElementById('bri-cycle').value='annually';
        if(getcook('bri-addons-email') == '1'){
            var email = 12;
        }
        else{
            var email = 0;
        }
        var planttl = parseFloat(monthly*12)+parseFloat(email);
        var gttl = parseFloat(monthly*12)+parseFloat(email)+parseFloat(domains);
    }
    if(cycle == 'cycleq'){
        setcook('bri-cycle', 'quarterly', 3);
        document.getElementById('bri-cycle').value='quarterly';
        if(getcook('bri-addons-email') == '1'){
            var email = 3;
        }
        else{
            var email = 0;
        }
        monthly = monthly+2;
        var planttl = parseFloat(monthly*3)+parseFloat(email);
        var gttl = parseFloat(monthly*3)+parseFloat(email)+parseFloat(domains);
    }
    setcook('bri-planttl', parseFloat(planttl.toFixed(2)), 3);
    setcook('bri-gttl', parseFloat(gttl.toFixed(2)), 3);

    /* Update planttl, and total elements */
    if(document.getElementById('bri-planttl')){document.getElementById('bri-planttl').value=formatCurrency(planttl);}
    if(document.getElementById('bri-gttl')){document.getElementById('bri-gttl').value = formatCurrency(gttl);}
    if(document.getElementById('emlttl')){document.getElementById('emlttl').innerHTML = formatCurrency(email);}
    if(document.getElementById('planttl')){document.getElementById('planttl').innerHTML = formatCurrency(planttl-email);}
    if(document.getElementById('total')){document.getElementById('total').innerHTML = formatCurrency(gttl);}

}



function ac(sw){
   console.log(sw);
    if(sw == 'checked' || sw == true){
        setcook('autocharge', 'checked', 3);
    }
    else{
        setcook('autocharge', '', 3);
    }


}
console.log("- - - - - - - - - \r\nLoaded includes...\r\n- - - - - - - - - \r\n");console.log("- - - - - - - \r\nLocation: 404\r\n- - - - - - - \r\n");


