//
// user-login.js
//
// requires :
//   trader.js
//   

var plugModuleInstance = new PlugModule();
plugModuleInstance.pageLoad();

function PlugModule () {

    this.URL_XML = "./";
    this.MENU_CONTAINER_ID = "menu";
    this.PAGE_CONTAINER_ID = "page";
    this.BLOCK_CONTAINER_ID = "block";
    this.COMPONENT_CONTAINER_ID = "plug_list";
    
    this.pageLoad = function() {
        this.initVars();
    }
    
    this.initVars = function() {
        this.plug_id = null;
        this.action = null;
        this.type = null;
        this.payment = null;
        this.filters = null;
        this.search = null;
    }
    
    this.target;
    
    this.fetchXmlData = function() {
        var xmlGet = this.generateGet();
        var xmlPost = this.generatePost();
        fetchXmlData(this.URL_XML, xmlGet, xmlPost);
        
        this.initVars();
    }
    
    this.module;
    this.method;
    
    this.generateGet = function() {
        var params = "";

        params = appendUrlParam(params, "m", this.module);
        params = appendUrlParam(params, "method", this.method);
        params = appendUrlParam(params, "target", this.target);
        
        if(this.filters)
            params += "&" + this.filters;
        if(this.search)
            params = appendUrlParam(params, "search", this.search);

        if(this.action) {
            params = appendUrlParam(params, "action", this.action);
        } else {
            if(this.type && this.payment_method) {
                params = appendUrlParam(params, "type", this.type);
                params = appendUrlParam(params, "payment", this.payment_method);
            }
        }
        if(this.plug_id)
            params = appendUrlParam(params, "plug_id", this.plug_id);

        return params;
    }
    
    this.generatePost = function() {
        var params = "";

        if(this.action=="plug-new") {
            params = appendUrlParam(params, "url", this.url);
            params = appendUrlParam(params, "title", this.title);
            params = appendUrlParam(params, "description", this.description);
            params = appendUrlParam(params, "payment", this.payment_method);
            params = appendUrlParam(params, "type", this.type);
            if(this.domain != "")
                params += '&'+this.domain;
            if(this.category != "")
                params += '&'+this.category;
            if(this.thumb != "")
                params += '&'+this.thumb;
        } else if(this.action=="plug-edit") {
            params = appendUrlParam(params, "category", this.category);
            params = appendUrlParam(params, "url", this.url);
            params = appendUrlParam(params, "title", this.title);
            params = appendUrlParam(params, "description", this.description);
            params = appendUrlParam(params, "thumb", this.thumb);
        }

        return params;
    }
    
    this.action;
    this.plug_id;
    this.url;
    this.title;
    this.description;
    this.type;
    this.payment_method;
    this.domain;
    this.category;
    this.thumb;
    
    this.viewPlugs = function() {
        this.module = "plugs";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.editPlug = function(id) {
        this.plug_id = id;
        
        this.module = "plug-edit";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.sendEditPlug = function(form) {
        this.action = "plug-edit";
        this.plug_id = form.plug_id.value;
        this.category = 0;
        if(form.category)
            this.category = form.category.value;
        this.url = form.url.value;
        this.title = form.title.value;
        this.description = form.description.value;
        this.thumb = form.thumb.value;
        
        this.module = "plug-edit";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
        
        return false;
    }
    
    this.deletePlug = function(id) {
        if(!confirm('Are you sure you want to delete this plug ?')) return;
        this.plug_id = id;
        this.action = "plug-delete";
        
        this.module = "plugs";
        this.method = "ajax";
        this.target = this.PAGE_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.newPlug = function() {
        this.module = "plug-new";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.sendNewPlug = function(form) {
        this.action = "plug-new";
        this.url = form.url.value;
        this.title = form.title.value;
        this.description = form.description.value;
        this.payment_method = undefined;
        for(i=form.payment.length-1; i>-1; i--) {
            if(form.payment[i].checked) {
                this.payment_method = form.payment[i].value;
            }
        }
        this.type = form.type.value;
        this.domain = "";
        this.category = "";
        this.thumb = "";
        e = form.elements;
        for(var i=0;i<e.length;i++) {
            if(form[i].name.indexOf('domain[]')==0) {
                if(this.domain != "") {
                    this.domain += "&";
                }
                this.domain += form[i].name+"="+form[i].value;
            } else if(form[i].name.indexOf('category[')==0) {
                if(this.category != "") {
                    this.category += "&";
                }
                this.category += form[i].name+"="+form[i].value;
            } else if(form[i].name.indexOf('thumb[')==0) {
                if(this.thumb != "") {
                    this.thumb += "&";
                }
                this.thumb += form[i].name+"="+form[i].value;
            }
        }
        
        this.module = "plug-new";
        this.method = "ajax";
        this.target = this.PAGE_CONTAINER_ID;
        this.fetchXmlData();
        
        return false;
    }
    
    this.buyPlug = function() {
        this.module = "plug-buy";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.confirmBuyPlug = function() {
        this.module = "plug-buy-confirm";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.confirmOrderPlug = function() {
        this.module = "plug-buy-order-confirm";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.pushPlug = function(id) {
        this.plug_id = id;
        
        this.module = "plug-push";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.newBoughtPlug = function() {
        this.module = "plug-submit-bought";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.viewPlugStats = function(id) {
        this.plug_id = id;
        
        this.module = "plug-stats";
        this.method = "ajax";
        this.target = this.BLOCK_CONTAINER_ID;
        this.fetchXmlData();
    }
    
    this.previousType = 1;
    this.previousPayment = 1;
    this.userCredits = 0;
    this.userPurchasedPlugs = 0;
    this.userPurchasedChosen = 0;
    
    this.updateDomainLists = function(form) {
        payment = null;
        for(i=form.payment.length-1; i>-1; i--) {
            if(form.payment[i].checked) {
                payment = form.payment[i].value;
            }
        }
        if(this.previousType==form.type.value && this.previousPayment==payment) {
            return false;
        }
        if(this.selectedDomains.length>0 && !confirm('Changing the plug type or the payment method will force a reset on your selected domains list.\n Do you wish to continue ?')) {
            form.type.value=this.previousType;
            for(i=form.payment.length-1; i>-1; i--) {
                if(form.payment[i].value == this.previousPayment) {
                    form.payment[i].checked = true;
                }
            }
            return false;
        }
        this.previousType = form.type.value;
        this.previousPayment = payment;
        if(form.type.value && payment) {
            if(form.type.value != '' && payment!=null) {
                document.getElementById('submit_to_results').innerHTML = '\
            <table width="100%" height="42px">\
                <tr>\
                    <td valign="middle" colspan="2" class="test_normal">\
                        Updating list...\
                    </td>\
                </tr>\
            </table>';
                
                this.type = form.type.value;
                this.payment_method = payment;
                
                this.module = "plug-new";
                this.method = "ajax";
                this.target = "submit_to_results";
                this.fetchXmlData();
                for(var i=this.selectedDomains.length-1; i>=0; i--) {
                    this.unselectDomain('domain_'+this.selectedDomains[i].id);
                }
            }
        }
    }
    
    this.selectedDomains = [];
    
    this.selectDomain = function(obj) {
        var el_id = obj.id;
        var found = false;
        for(var i=0; i<plugModuleInstance.selectedDomains.length; i++) {
            if(plugModuleInstance.selectedDomains[i].id == el_id) {
                found = true;
            }
        }
        if(!found) {
            var el = document.createElement('div');
            el.setAttribute('id', 'domain_'+el_id);
            var html = '<table id="" width="100%" height="42px">\
    <tr>\
        <td valign="middle" width="130px">\
            <input type="hidden" name="domain[]" value="'+el_id+'" />\
            <input type="hidden" name="thumb['+el_id+']" id="thumb[domain_'+el_id+']" />\
            <a href="javascript:void(0)" onclick="open_thumb('+obj.thumb.width+', '+obj.thumb.height+', \'thumb[domain_'+el_id+']\', \'thumb_preview[domain_'+el_id+']\')" alt="Edit Thumb" title="Edit Thumb">\
                <img id="thumb_preview[domain_'+el_id+']" src="./images/thumb_needed.jpg" width="60px" height="60px" border="0" style="float: left; padding-right: 4px" />\
            </a>\
            <a href="javascript:void(0)" onclick="open_thumb('+obj.thumb.width+', '+obj.thumb.height+', \'thumb[domain_'+el_id+']\', \'thumb_preview[domain_'+el_id+']\')">\
                <img src="./images/icons/selection_edit.png" border="0" alt="Edit Thumb" title="Edit Thumb" />\
                Edit\
            </a><br/>\
            <a href="javascript:void(0)" onmouseover="\
                if(document.getElementById(\'thumb_preview[domain_'+el_id+']\').src!=\'\') { \
                    document.getElementById(\'preview_img[domain_'+el_id+']\').src=document.getElementById(\'thumb_preview[domain_'+el_id+']\').src;\
                }\
                TagToTip(\'preview_div[domain_'+el_id+']\', SHADOW, true, BGCOLOR, \'#ffffe1\', BORDERCOLOR, \'#000000\');\
                " onmouseout="UnTip()">\
                <img src="./images/icons/selection_view.png" border="0" style="padding-top: 8px" />\
                Preview\
            </a>\
            <div id="preview_div[domain_'+el_id+']" style="display: none;">\
                <img id="preview_img[domain_'+el_id+']" src="" border="0" />\
            </div>\
        </td>\
        <td valign="top" align="left">\
            <b>'+obj.name+'</b>';
            if(obj.method==1) {
                html += ' - '+obj.credits+' credits';
            }
            html += '<br/>';
            if(obj.type==1) {
                html = html + '\
                <select name="category['+obj.id+']">\
                    <option value=""></option>';
                for(var i=0; i<obj.categories.length; i++) {
                    var cat = obj.categories[i];
                    html = html + '                <option value="' + cat.id + '">' + cat.name + '</option>';
                }
                html = html + '            </select>';
            }
            html = html +'\
        </td>\
        <td valign="top" align="right" width="20px">\
            <a href="javascript:void(0)" onclick="plugModuleInstance.unselectDomain(\'domain_\'+'+el_id+')">\
                <img src="./images/icons/na.png" border="0" style="float: left; padding-right: 3px" alt="Remove this domain" title="Remove this domain" />\
            </a>\
        </td>\
    </tr>\
    <tr>\
        <td colspan="3">\
            <hr />\
        </td>\
    </tr>\
</table>';
            el.innerHTML = html;
            Dom.add(el, 'submit_to_list');
            plugModuleInstance.selectedDomains[plugModuleInstance.selectedDomains.length] = obj;
        }
        this.updateTotal();
    }
    
    this.unselectDomain = function(id) {
        Dom.remove(id);
        var newArr = [];
        for(var i = 0; i < plugModuleInstance.selectedDomains.length; i++) {
            if('domain_'+plugModuleInstance.selectedDomains[i].id != id) {
                newArr[newArr.length] = plugModuleInstance.selectedDomains[i];
            }
        }
        plugModuleInstance.selectedDomains = newArr;
        this.updateTotal();
    }
    
    this.updateTotal = function() {
        total_credits = 0;
        total_purchased_p = 0;
        total_purchased_c = 0;
        for(var i=0; i<this.selectedDomains.length; i++) {
            switch(this.selectedDomains[i].method) {
                case "1":
                    total_credits += parseInt(this.selectedDomains[i].credits);
                break;
                
                case "2":
                    switch(this.selectedDomains[i].type) {
                        case "1":
                            total_purchased_p ++;
                        break;
                        
                        case "2":
                            total_purchased_c ++;
                        break;
                    }
                break;
            }
        }
        text = "";
        if(total_credits > 0) {
            credits = total_credits + " Credits";
            if(total_credits > this.userCredits) {
                msg = 'Not enough credits. Send us more traffic.';
                credits = '<div><font color="#AA0000"><img src="./images/icons/warning.png" style="float: right" border="0" alt="'+msg+'" title="'+msg+'" />'+credits+'</font></div>';
            }
            text += credits;
        }
        if(total_purchased_p > 0) {
            pp = total_purchased_p + " Purchased Plugs";
            if(total_purchased_p > this.userPurchasedPlugs) {
                msg = 'Not enough purchased plugs. Contact us to buy some.';
                pp = '<div><font color="#AA0000"><img src="./images/icons/warning.png" style="float: right" border="0" alt="'+msg+'" title="'+msg+'" />'+pp+'</font></div>';
            }
            text += pp
        }
        if(total_purchased_c > 0) {
            pc = total_purchased_c + " Purchased Chosens";
            if(total_purchased_c > this.userPurchasedChosen) {
                msg = 'Not enough purchased chosen ones. Contact us to buy some.';
                pc = '<div><font color="#AA0000"><img src="./images/icons/warning.png" style="float: right" border="0" alt="'+msg+'" title="'+msg+'" />'+pc+'</font></div>';
            }
            text += pc
        }
        if(text == "") {
            text = "---";
        }
        text = '<table width="100%" border="0" cellpadding="0" cellspacing="2" align="right" bgcolor="#AAAAAA">\
    <tr>\
        <td align="right" width="50%">Total expenses : </td>\
        <td align="left" width="50%">'+text+'</td>\
    </tr>\
</table>';
        document.getElementById('total_cost').innerHTML = text;
    }

    var filters;
    var search;
    
    this.applyFilters = function(form) {
        var e=form.getElementsByTagName('input');
        this.filters = '';
        for(var i=0;i<e.length;i++) {
            if(e[i].type=='checkbox' && e[i].checked) {
                if(e[i].value != undefined) {
                    this.filters = appendUrlParam(this.filters, e[i].name, e[i].value);
                } else {
                    this.filters = appendUrlParam(this.filters, e[i].name, '');
                }
            } else if(e[i].type=='radio' && e[i].checked) {
                this.filters = appendUrlParam(this.filters, e[i].name, e[i].value);
            }
        }
        this.search = form.search.value;
    
        this.module = "plugs";
        this.method = "ajax";
        this.target = this.COMPONENT_CONTAINER_ID;
        
        this.fetchXmlData();
        
        switch_blocks('filters_opened', 'filters_closed');
        
        return false;
    }
    
    this.clearFilters = function(form) {
        var e=form.getElementsByTagName('input');
        for(var i=0;i<e.length;i++) {
            if(e[i].type=='checkbox') {
                e[i].checked = false;
            } else if(e[i].type=='text') {
                e[i].value = '';
            }
        }
        
        return false;
    }    
}

