function Form(){    
    this.display = display;
    this.cancel = cancel;
    this.save = save;
    this.getForm = getForm;
    this.checkStatus = checkStatus;
    this.timer;    
    this.reload = reload;
}

function cancel(){
    id('over_content').className = 'ini';
    id('over_content_ajax').innerHTML = 'Loading...';
    document.body.removeChild(id('splash')); 
}

function display(data){ 
    var div = document.createElement('DIV');
    div.innerHTML = '';
    div.setAttribute("id", "splash");
    document.body.appendChild(div);
    id('over_content').className = 'fin';
    $("#over_content_ajax").css({ height:"auto" });
    $("#over_content_ajax").load("ajax.php?" + data, function(){
        if(id("over_content_ajax").offsetHeight > 633){
            $("#over_content_ajax").height(633);
            $("#over_content_ajax").css({ overflowY:"auto" });
        }
    });
    
}

function getForm(obj){
    while(obj.nodeName != 'FORM'){
        obj = obj.parentNode;
    } 
    if(obj.nodeName == 'FORM'){        
        return obj;
    } else {
        return null;
    }      
}

function save(obj, target_id, refresh, data, target){
    var form = this.getForm(obj);
    var update = false;
    //check for hidden id
    ln = $("input:hidden", $('#' + target_id));
    if(ln.attr('id') != 'form_new_id'){
        while(ln.next() && ln.attr('id') != 'form_new_id'){
            ln = ln.next();
        }
    }  
    if(ln.attr('id') == 'form_new_id'){
        nfid = ln;
    }
    //if update
    if(nfid){ 
        update = nfid.val() != "0" ? true : false;
    }
    if(form){ 
        var obj_id = form.id;
        $('#' + obj_id).ajaxForm();
        $('#' + obj_id).ajaxSubmit(
            function(response){
                $('#' + target_id).html(response);                                
                if(!update){
                    ln = $("input:hidden", $('#' + target_id));
                    if(ln.attr('id') != 'form_new_id'){
                        while(ln.next() && ln.attr('id') != 'form_new_id'){
                            ln = ln.next();
                        }
                    }                
                    if(ln.attr('id') == 'form_new_id' && ln.val() != "0"){
                        var s = new String(form.action);
                        s = s.replace('item=0', 'item='+ln.val());
                        Form.reload(target_id, s);                        
                    } else {
                        //alert('no id');
                    }
                } else {
                    clearTimeout(this.timer);
                    this.timer = setTimeout(clearScreenMessages, 3000);                    
                }
                if(refresh){
                    Grid.refresh(data, target);
                }
            }        
        );                                                       
    }
    return false;    
}

function reload(target_id, s){
    $("#" + target_id).load(s);
}

function clearScreenMessages(){
    $(".screen_message").html('');    
}

function checkStatus(data, msg){
    var st = id('form_status');
    if(st){
        if(st.value == 'saved'){
            Form.display(data);
        } else if (st.value == 'error') {
            $(".screen_message").addClass("failure");
            $(".screen_message").html(msg);
        } else {
            $(".screen_message").addClass("failure");
            $(".screen_message").html(msg);
        }
        clearTimeout(this.timer);
        this.timer = setTimeout(clearScreenMessages, 3000);        
    }
    return
}