var default_term = '1';
var default_private = '0';



function cartitem(domain, type, price, isprivate, term){

    multidomain = 1;//may not need
    price = ( typeof price != "undefined" ? price : "12.95" );
    this.name = domain;
    this.price = price;
    this.private = (isprivate ? isprivate : default_private);
    this.term = (term ? term : default_term);
    this.type = type;

    this.makeTerm = function(){ 
        var mysel = "<select name='term' onchange='updateTerm(\""+this.name+"\",this.value)'>";
        for ( var x=1; x<=3; x++ ) {
            mysel += "<option value='"+x+"' "+ ( x == this.term ? "selected='selected'": "" ) +" >" + x+" year";
            switch(x){
                case 1: mysel += " - standard price</option>"; break;
                case 2: mysel += "s - 4% discount</option>"; break;
                case 3: mysel += "s - 8% discount</option>"; break;
            }
        }
        mysel += "</select>";
        return mysel;
    };

    this.getPrice = function(){ 
        var price = 0.0;
        switch(this.term){
            case '1': price += 12.95; break;
            case '2': price += 24.95; break;
            case '3': price += 35.95; break;
        }
        //var price = 12.95 * parseFloat(this.term);
        //var discount = (this.term-1) * (4/100);
        //price *= (1 - discount);
        //price = parseInt(price) + parseFloat('0.95');
        if (this.private == "1") { 
            switch(this.term){
                case '1': price += 9.95; break;
                case '2': price += 19.10; break;
                case '3': price += 27.50; break;
            }
        }
        if (this.type == "premium"){
            price += parseFloat(this.price); 
        }
        return roundVal(price);
    };


    this.getTooltip = function(price){ 
        if (price == 0) {price = this.getPrice();}
        if (this.type == "premium"){
            var tooltip = "The $" + roundVal(parseFloat(this.price) - 12.95) +" premium cost plus $12.95/yr renewal fee";
        } else {
            var tooltip = "$12.95/yr";
        }
        if (this.private == "1") { tooltip += " plus $9.95/yr for privatization" ; }
        tooltip += " costs $" + price + " for "+ this.term +" year";
        if (this.term > 1) { tooltip += "s with the "+ ((this.term-1) * 4 )  +"% discount"; }
        return tooltip;
    };
}

tmp = new Object();

function shoppingcart(item, domain){
    this.items = new Object();
    this.count = '0';


    var mycookie = $.cookie('domaincart');
    if (mycookie && mycookie != "{}"){
        var myitems = eval('('+ $.cookie('domaincart') +')');
        $.each(myitems, function() {
            tmp[this.name] = new cartitem(this.name, this.type, this.price, this.private, this.term); 
        });
        this.items = tmp;
    }
    
    this.addItem = function(domain, type, price){
        this.items[domain] = new cartitem(domain, type, price); 
        
        $.getJSON("/bin/ajax/search", { domain:domain, type:"exact" }, function(data){
        //alert(data.domain);
            if (data.domain == domain){
                update("<p>" + "Added " + domain + " to cart!" + "</p>");
            } else {
                removeFromCart(domain,"error"); 
                update("<p>" + "Error adding " + domain + " to cart!" + "</p>");
            }
        });
    };

    this.removeItem = function(domain){ 
        delete this.items[domain];
    };

    this.privatize = function(domain, checked) {
        if (domain == "all") {
            default_private = ( checked ? "1" : "0" );
            for (var item in this.items){
                this.items[item]['private'] = default_private;
            }
        } else {
            this.items[domain]['private'] = ( checked ? "1" : "0" );
        }
    };

    this.updateTerm = function(domain, term) {
        if (domain == "all") {
            default_term = term;
            $.each(this.items, function(i, item) {
                this.items[i]['term'] = term;
            });
        } else {
            this.items[domain]['term'] = term;
        }
    };

    this.clearCart = function(){
        manage_ind = 1;
        $.cookie("manageind",null);
        this.items = new Object();
        $.cookie('domaincart',null);
    };

    this.renderCart = function(){
        var myhtml = "";
        $.each(this.items, function(i, item) {
            var mysel = item.makeTerm();
            var price = item.getPrice();
            var tooltip = item.getTooltip(price);
            myhtml += "<tr id='"+i+"'>\
  <td class='domain'>"+i+"</td>\
  <td class='price' title='"+tooltip+"'>$"+ price + "</td>\
  <td class='term optional'>"+ mysel + "</td>\
  <td class='private optional'>\
    <input type='checkbox' name='private' value='"+item['private']+"' "+ (item['private']=="1" ? "checked='checked'":"") +" onclick='privatize(\""+i+"\",this.checked)'/>\
  </td>\
  <td class='remove' >\
    <a href='javascript:removeFromCart(\""+i+"\",\""+item['type']+"\")'><img src='images/icon_delete.png'/></a>\
  </td>\
</tr>";
        });
        return myhtml;
    };

    this.renderSum = function(){
        var myhtml = "";
        var sum = 0.0;
        var count = 0;
        $.each(this.items, function(i, item) {
            sum += parseFloat(item.getPrice());
            count++;
        });
        myhtml += "<tr id='sum'><td class='domain'>Total ("+count+" domains)</td><td class='price'>$"+ roundVal(sum) + "</td>\
  <td class='term optional'>&nbsp;</td>\
  <td class='private optional'>&nbsp;</td>\
  <td class='remove' >&nbsp;</td>\
</tr>";
        return myhtml;
    };


    this.renderBuyOnly = function(domain){
        var myhtml = "";

        var item = this.items[domain]; 
        var mysel = item.makeTerm();
        var price = item.getPrice();
        var tooltip = item.getTooltip(price);
        myhtml += "\
<h1 class='options'>Purchase "+domain+" for <span title='"+tooltip+"'>$"+ price + "</span></h1>\
<div class='options'>\
  <p>\
    <input type='checkbox' name='private' value='"+item['private']+"' "+ (item['private']=="1" ? "checked='checked'":"") +" onclick='privatize(\""+domain+"\",this.checked)'/>\
Add <span class=\"bold\">Domain Privacy</span> (<a href='javascript:help(\"private\")'>?</a>) to this domain for $9.95 per year. <span class=\"red\">(Recommended)</span><br/>This feature helps to prevent scams and identity theft by not listing your personal information in the public domain name \"WHOIS\" directory. This does not affect search engines such as Google finding your site. </p><p>&nbsp;</p>\
<p><label for='term'>Term:</label>&nbsp;"+ mysel + "</p>\
</div>\
<div class='options'>\
<input type='button' value='Buy Now' onclick='checkOut()' />\
<input type='button' class='simplemodal-close' onclick='$.modal.close()' value='Cancel'/>\
</div>" ;
        return myhtml;
    };


    this.save = function(){
        var myJSONText = JSON.stringify(this.items, replacer);
        $.cookie('domaincart',myJSONText);
    }



    this.checkOut = function() {
        var myJSONText = JSON.stringify(this, replacer);
        $.getJSON("/bin/ajax/checkout", { json: myJSONText }, function(data){

            if (data.error == 1){
                alert("Unfortunately some domains in your cart are no longer available.  They have been removed from your cart.");
                for (var dom in data.bad){
                    oocart.removeItem(data.bad[dom]);
                    oocart.save();
                    redraw_cart();
                }
            }
            
            if (data.url != ""){
                window.location = data.url
            }

        }, "text");
    };


    this.itemCount = function() {
        var size = 0, key;
        for (key in this.items) {
            if (this.items.hasOwnProperty(key)) size++;
        }
        return size;
    };

}


//subdomain.prototype.clone = function() {
//  var newObj = (this instanceof Array) ? [] : {};
//  for (i in this) {
//    if (i == 'clone') continue;
//    if (this[i] && typeof this[i] == "object") {
//      newObj[i] = this[i].clone();
//    } else newObj[i] = this[i]
//  } return newObj;
//};

