function numbersonly(myfield, e, dec)
{
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key==null) || (key==0) || (key==8) ||
        (key==9) || (key==13) || (key==27) )
        return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;

    // decimal point jump
    else if (dec && (keychar == "."))
    {
        myfield.form.elements[dec].focus();
        return false;
    }
    else
        return false;
}

function updateStatus(article_id, status){
    var params = 'article_id='+article_id+'&status='+status;

    new Ajax.Request(WEB_ROOT + '/index.php?module=pages&do=updateStatus_page',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function updateStatusProducts(product_id, status){
	
    var params = 'product_id='+product_id+'&status='+status;

    new Ajax.Request(WEB_ROOT + 'index.php?module=products&do=updateStatus_product',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function updateStateProducts(product_id, state){
	
    var params = 'product_id='+product_id+'&state='+state;

    new Ajax.Request(WEB_ROOT + 'index.php?module=products&do=updateState_product',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}

function updateStateSoldProducts(product_id, statesold){
	
    var params = 'product_id='+product_id+'&statesold='+statesold;

    new Ajax.Request(WEB_ROOT + 'index.php?module=products&do=updateStateSold_product',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function updateStatusCategory(categ_id, status){
	
    var params = 'categ_id='+categ_id+'&status='+status;
    new Ajax.Request(WEB_ROOT + 'index.php?module=products&do=updateStatus_category',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function updateFotoStatus(id, status){
    var params = 'id='+id+'&status='+status;

    new Ajax.Request(WEB_ROOT + 'index.php?module=photos&do=updateStatus_photo',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}

function redirect(id){
    window.location = WEB_ROOT + 'index.php?module=pages&do=view&parent_id=' + id;
    return false;
}
function editcateg(id){
    window.location = WEB_ROOT + 'index.php?module=products&do=editcateg&id=' + id;
    return false;
}
function editproduct(id){
    window.location = WEB_ROOT + 'index.php?module=products&do=edit&id=' + id;
    return false;
}


function deletePage(article_id){
    var params = 'article_id='+article_id;

    new Ajax.Request(WEB_ROOT + 'index.php?module=pages&do=delete_page',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function deleteProduct(product_id){
    var params = 'product_id='+product_id;

    new Ajax.Request(WEB_ROOT + 'index.php?module=products&do=delete_product',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function deleteCategory(categ_id){
    var params = 'categ_id='+categ_id;

    new Ajax.Request(WEB_ROOT + 'index.php?module=products&do=delete_category',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function deletePrice(price_id){
    var params = 'price_id='+price_id;

    new Ajax.Request(WEB_ROOT + 'index.php?module=products&do=delete_price',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}
function deleteFoto(id){
    var params = 'id='+id;

    new Ajax.Request(WEB_ROOT + 'index.php?module=photos&do=delete_photo',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
}

function prepareMenu() {
    // first lets make sure the browser understands the DOM methods we will be using
    if (!document.getElementsByTagName) return false;
    if (!document.getElementById) return false;

    // lets make sure the element exists
    if (!document.getElementById("submenu")) return false;
    var menu = document.getElementById("submenu");

    // for each of the li on the root level check if the element has any children
    // if so append a function that makes the element appear when hovered over
    var root_li = menu.getElementsByTagName("td");
    for (var i = 0; i < root_li.length; i++) {
        var li = root_li[i];
        // search for children
        var child_ul = li.getElementsByTagName("ul");
        if (child_ul.length >= 1) {
            // we have children - append hover function to the parent
            li.onmouseover = function () {
                if (!this.getElementsByTagName("ul")) return false;
                var ul = this.getElementsByTagName("ul");
                ul[0].style.display = "block";
                return true;
            }
            li.onmouseout = function () {
                if (!this.getElementsByTagName("ul")) return false;
                var ul = this.getElementsByTagName("ul");
                ul[0].style.display = "none";
                return true;
            }

            if (!li.getElementsByTagName("li")) return false;
            var child_li = li.getElementsByTagName("li");
            if(child_li.length > 1){
                for (var j = 0; j < child_li.length; j++) {
                    var li2 = child_li[j];
                    if (li2.getElementsByTagName("ul")) {
                        var child2_ul = li2.getElementsByTagName("ul");
                        if (child2_ul.length >= 1) {
                            var w = (li2.parentNode.getWidth() - 10)  +"px";
                            child2_ul[0].style.marginLeft  = w;
                        }
                    }
                }
            }
        }

    }

    return true;
}

function populateHiddenVars() {
    document.getElementById('articlesListOrder').value = Sortable.serialize('list');
    return true;
}
function populateHiddenProd() {
    document.getElementById('articlesListOrder').value = Sortable.serialize('list1');
    return true;
}
function savePageContent(text, id){
    var params = 'text='+text+'&article_id='+id;

    new Ajax.Request(WEB_ROOT + 'index.php?module=pages&do=updateContent_page',{
        method: 'post',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                alert(transport.responseText);
            }
        }
    });
    $('text').innerHTML = decodeURIComponent(text);
    $('content-white').toggle();
    $('edit_text').toggle();

    return false;
}

function openText(id) {
    window.open( WEB_ROOT + 'index.php?module=products&do=showtext&id='+id, "myWindow", 
        "status = 1, height = 500, width = 500,left=100, top=100, resizable = 0" )

}
function openSmallText(id) {
    window.open( WEB_ROOT + 'index.php?module=products&do=showsmalltext&id='+id, "myWindow", 
        "status = 1, height = 500, width = 500,left=100, top=100, resizable = 0" )

}

function openCategDetails(id) {
    window.open( WEB_ROOT + 'index.php?module=products&do=showcategdetails&id='+id, "myWindow", 
        "status = 1, height = 500, width = 500,left=100, top=100, resizable = 0" )

}

function getPage(module, do_action, params){
    new Ajax.Request(WEB_ROOT + 'index.php?module='+module+'&do='+do_action+'&ajax=1',{
        method: 'get',
        parameters: params,
        onSuccess: function(transport){
            if(transport.responseText != ''){
                $('mainContent').innerHTML = transport.responseText;
            }
        }
    });
}
