/**
 * @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){
        if(QtyBox.value<=0){
            QtyBox.value = 1;
        }
    }else{
        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>' + '\n';
    addonsHTML += '<div>' + '\n';
    addonsHTML += '<table cellpadding="0" cellpadding="0" width="86%">' + '\n';
    addonsHTML += '<tr><td style="width:80%">Name</td><td>Price</td><td>Qty</td></tr>' + '\n';
    $.each(data, function(index, optionData) {
        if(type != optionData.type){
            counter ++;
            addonsHTML += '</table></div><br >' + '\n';
            addonsHTML += '<div class=fontsize>' + optionData.type + '</div>' + '\n';
            addonsHTML += '<div>' + '\n';
            addonsHTML += '<table cellpadding="0" cellpadding="0" width="86%">' + '\n';
            addonsHTML += '<tr><td style="width:80%">Name</td><td>Price</td><td>Qty</td></tr>' + '\n';
            type = optionData.type;
        }
        addonsHTML += '<tr>' + '\n';
        addonsHTML += '<td>' + '\n';
        addonsHTML += '<input type="checkbox" name="addons_id[' + index + ']" id="Addons' + optionData.id + '" value="' + optionData.id + '" onclick="setIdNQty(' + optionData.id + ');" />' + '\n';
        addonsHTML += '<label for="Addons' + optionData.id + '">' + optionData.value + '</label>' + '\n';
        addonsHTML += '</td>' + '\n';
        addonsHTML += '<td>&#36;<span class="addons_price">' + optionData.price + '</span></td>' + '\n';
        addonsHTML += '<td>' + '\n';
        addonsHTML += '<input type="text" id="Qty' + optionData.id + '" name="addons_qty[' + index + ']" value="" class="element text" style="width:40px;" />' + '\n';
        addonsHTML += '<input type="hidden" id="Price' + optionData.id + '" class="addons_price" name="addons_price[' + index + ']" value="' + optionData.price + '" />' + '\n';
        addonsHTML += '</td>' + '\n';
        addonsHTML += '</tr>' + '\n';
    });
    addonsHTML += '</table></div>' + '\n';
    //alert(addonsHTML);
    return addonsHTML;
}
