function categoryCreate(parent, name, relation) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "category", action: "create", parent: parent, name: name, relation: relation},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function categoryRename(element, name) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "category", action: "rename", id: element, name: name},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function categoryMove(element, parent, relation) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "category", action: "move", id: element, parent: parent, relation: relation},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function categoryRemove(element) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "category", action: "delete", id: element},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function categoryChildren(element) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "category", action: "children", id: element},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function confirmDialog($message) {
    return confirm($message);
}

function productMove(id, target, relation) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "category", action: "move", id: element, parent: parent, relation: relation},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;    
}


function manufacturerCreate(name, position) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "manufacturer", action: "create", name: name, position: position},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function manufacturerMove(list) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?"+list+"&",
        data: { mode: "ajax", module: "manufacturer", action: "move"},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function manufacturerDelete(id, list) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?"+list+"&",
        data: { mode: "ajax", module: "manufacturer", action: "delete", id: id},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function manufacturerChange(id, name) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "manufacturer", action: "change", id: id, name: name},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function flushPicture() {
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "manufacturer", action: "flush"},
        success: function(msg){
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

function strReplace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

function validateNip(nip) {
    var weights = new Array(6, 5, 7, 2, 3, 4, 5, 6, 7);
    var result = 0;
    nip = strReplace(nip, "-", "");
    nip = strReplace(nip, " ", "");
  
    if (nip.length!=10) return false;
    
    for (var i=0; i<9; i++) {
      result += weights[i] * parseInt(nip.charAt(i));
    }
    result %= 11;
    if (result == 10) result = 0;
    return (result == parseInt(nip.charAt(9)))?true:false;
  
}


// http://snipplr.com/view.php?codeview&id=29911
function fancyAlert(msg) {
    jQuery.fancybox({
        'modal' : true,
        'content' : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input style=\"margin:3px;padding:0px;\" type=\"button\" onclick=\"jQuery.fancybox.close();\" value=\"Ok\"></div></div>"
    });
}

function fancyConfirm(msg,callback) {
    var ret;
    jQuery.fancybox({
        modal : true,
        content : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input id=\"fancyConfirm_cancel\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Cancel\"><input id=\"fancyConfirm_ok\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Ok\"></div></div>",
        onComplete : function() {
            jQuery("#fancyConfirm_cancel").click(function() {
                ret = false; 
                jQuery.fancybox.close();
            })
            jQuery("#fancyConfirm_ok").click(function() {
                ret = true; 
                jQuery.fancybox.close();
            })
        },
        onClosed : function() {
            callback.call(this,ret);
        }
    });
}

function fancyConfirm_text() {
    fancyConfirm("Ceci est un test", function(ret) {
        alert(ret)
    })
}


function productCreate(category) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "product", action: "create", category: category},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}


function productEdit(id, category, name, description, promotion_description, remarks, parameters, unit_type, unit_name) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "product", action: "edit", id: id, category: category, name: name, description: description, promotion_description: promotion_description, remarks: remarks, parameters: parameters, unit_type: unit_type, unit_name: unit_name},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}


function productDelete(id, category, list) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?"+list+"&",
        data: { mode: "ajax", module: "product", action: "delete", id: id, category: category},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}


function productGet(id, display) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "product", action: "get", id: id, display: display},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}



function pictureCreate(name, position, description, link) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "picture", action: "create", name: name, position: position, description: description, link: link},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function pictureMove(list) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?"+list+"&",
        data: { mode: "ajax", module: "picture", action: "move"},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function pictureDelete(id, list) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?"+list+"&",
        data: { mode: "ajax", module: "picture", action: "delete", id: id},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function pictureChange(id, name, description, link) {
    var result = 0;
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "picture", action: "change", id: id, name: name, description:description, link:link},
        success: function(msg){
            result = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            result = 0;
        }
    });
    return result;
}

function pictureFlush() {
    $.ajax({
        type: "POST", cache: false, async: false, url: "?",
        data: { mode: "ajax", module: "picture", action: "flush"},
        success: function(msg){
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}



