/**
 * @author anisrehan
 */

var NOClones = 1;

/**
 * Clone "CloneLI" and insert it Before the same.
 */
function addLi() {
    $('#CloneLI').clone(true).insertBefore($('#CloneLI')).css('display','block').attr('id','CloneLI'+ NOClones);
    NOClones++;
    return true;
}

function Remove(AddButton){
    AddButton.parentNode.parentNode.parentNode.removeChild(AddButton.parentNode.parentNode);
    return true;
}

function IndexLoadingImage(){
    var root = '../';
    insertLoadingImage(root);
}

function insertLoadingImage(root){
    var image = document.createElement('img');
    image.src = root + 'assets/images/ajax-loader.gif';
    image.alt = 'loading...';
    $('#loading').append(image);
}

function removeImage(){
    $('#loading').html('');
}

function AjaxRequest(root,query,Reciever){
    address = root + 'inc/itl/Ajax.php';
    insertLoadingImage(root);

    $.ajax({
        url: address,
        cache: true,
        data: query,
        success: function(response){
            //alert(response);
            if($('#'+Reciever).is('input')){
                $('#'+Reciever).val(response);
            }
            else{
                $('#'+Reciever).html(response);
            }
            removeImage();
        },
        error: function(response){
            //alert(response);
            if($('#'+Reciever).is('input')){
                $('#'+Reciever).val(response);
            }
            else{
                $('#'+Reciever).html(response);
            }
            removeImage();
        }
    });
    return true;
}

function Checkout(root){
    address = 'checkout.php';
    query = 'step=4';
    
    insertLoadingImage(root);
    $.ajax({
        url: address,
        data: query,
        success: function(response){
            //alert(response);
            if(response){
                //alert(response);
                if(document.MoneyBookerForm){
                    //alert('Submitting MoneyBooker Form');
                    document.MoneyBookerForm.submit();
                }
            }
            removeImage();
        }
    });
    return true;
}

function UpdateCartStatus(responseText, statusText){
    //alert(responseText);
    $.get('../inc/itl/Ajax.php', 'getCartTotal=1', function(response){
        //alert(response);
        $('#cartTotal').html(response);
    });
    $.get('../inc/itl/Ajax.php', 'getCartQty=1', function(response){
        //alert(response);
        $('#cartQty').html(response);
    });
    removeImage();
}

$.fn.clearSelect = function() {
    return this.each(function() {
        if (this.tagName == 'SELECT')
            this.options.length = 0;
    });
}

$.fn.fillSelect = function(data,years, months) {
    return this.clearSelect().each(function() {
        if (this.tagName == 'SELECT') {
            var dropdownList = this;
            $.each(data, function(index, optionData) {
                //alert('Value: '+optionData.value + '\nId: '+optionData.id);
                value = optionData.value;
                if(years == 1){
                    value = parseInt(optionData.value)/12;
                }else if(months == 1){
                    value = optionData.value + ' Months';
                }
                var option = new Option(value, optionData.id, optionData.superId);
                if ($.browser.msie) {
                    dropdownList.add(option);
                }
                else {
                    dropdownList.add(option, null);
                }
            });
        }
    });
}

function setPrice(data, id) {
    price = '';
    $.each(data, function(index, optionData) {
        //alert('Required id:' + id + '\nCurrent id: ' + optionData.id);
        if(parseInt(id) == parseInt(optionData.id)){
            price = optionData.price;
            //alert('Price in Function:' + price);
        }
    });
    //alert('Price Returning:' + price);
    return price;
}

function setIdNQty(Id){
    CheckBox = document.getElementById('Addons'+Id);
    QtyBox = document.getElementById('Qty'+Id);

    if(CheckBox.checked == true){
        CheckBox.value = Id;
        QtyBox.value = 1;

    }else{
        CheckBox.value = 0;
        QtyBox.value = '';
    }
    //alert(CheckBox.value + '\n' + QtyBox.value);
}

function getAddons(data){
    var addonsHTML = '';
    type = data[0].type;
    counter = 0;
    addonsHTML += '<div class=fontsize>' + data[0].type + '</div>';
    addonsHTML += '<div id="options' + counter +'">';
    addonsHTML += '<table cellpadding="0" cellpadding="0" width="86%">';
    addonsHTML += '<tr><td style="width:80%">Name</td><td>Price</td><td>Qty</td></tr>';
    $.each(data, function(index, optionData) {
        if(type != optionData.type){
            counter ++;
            addonsHTML += '</table></div><br >';
            addonsHTML += '<div class=fontsize>' + optionData.type + '</div>';
            addonsHTML += '<div id="options' + index + '">';
            addonsHTML += '<table cellpadding="0" cellpadding="0" width="86%">';
            addonsHTML += '<tr><td style="width:80%">Name</td><td>Price</td><td>Qty</td></tr>';
            type = optionData.type;
        }
        addonsHTML += '<tr>';
        addonsHTML += '<td>';
        addonsHTML += '<input type="checkbox" name="addons_id[]" id="Addons' + optionData.id + '" value="" onclick="setIdNQty(' + optionData.id + ');" />'; //this.value = this.checked==true ? ' + optionData.id + ' : 0;
        addonsHTML += '<label for="Addons' + optionData.id + '">' + optionData.value + '</label>';
        addonsHTML += '</td>';
        addonsHTML += '<td>&#36;<span class="addons_price">' + optionData.price + '</span></td>';
        addonsHTML += '<td>';
        addonsHTML += '<input type="text" id="Qty' + optionData.id + '" name="addons_qty[]" value="" class="element text" style="width:40px;" />';
        addonsHTML += '<input type="hidden" class="addons_price" name="addons_price[]" value="' + optionData.price + '" />';
        addonsHTML += '</td>';
        addonsHTML += '</tr>';
    });
    addonsHTML += '</table></div>';
    addonsHTML += '<script type="text/javascript">';
    for(i=0; i<counter; i++){
        addonsHTML += "animatedcollapse.addDiv('options"+i+"', 'fade=1,hide=1')" + i<counter-1 ? "," : "";
        addonsHTML += "animatedcollapse.init();";
    }
    addonsHTML += '</script>';
    //alert(addonsHTML);
    return addonsHTML;
}