/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var getData = function(page,tujuan,data,lanjut,pop,obj) {
    if(obj!= undefined) {
        var objVal = $(obj).value;
    }
    showProses(pop,tujuan,obj);
    reqPost = new Request({
        url:page,
        method:'get',
        data:data,
        onSuccess:function(x) {
            var cekErr = x.substring(0, 5);
            if(cekErr == 'Error') {
                alert(x);
                hideProses(pop,tujuan,obj,objVal);
            } else if(cekErr == 'Info ') {
                alert(x);
                hideProses(pop,tujuan,obj,objVal);
            } else {
                switch(x) {
                    case 'Login time out !':
                        alert('Info \nSession time out, silahkan login kembali !');
                        closePopDiv();
                        break;
                    case 'Not implemented yet !':
                        alert(x);
                        hideProses(pop,tujuan,obj,objVal);
                        break;
                    case '&nbsp;':
                        alert('Data berhasil diproses !');
                        if($type(lanjut)=="function") {
                            hideProses(pop,tujuan,obj,objVal);
                            lanjut();
                        } else {
                            hideProses(pop,tujuan,obj,objVal);
                        }
                        break;
                    default :
                        hideProses(pop,tujuan,obj,objVal);
                        $(tujuan).set('html',x);
                        this.response.text.stripScripts(true);
                        if($type(lanjut)=="function") {
                            lanjut();
                        }
                }
            }
        }
    }).send();
}

var showProses = function(pop,tujuan,obj) {
    if(pop == undefined) {
        $('loading').setStyle('display','block');
    } else if(pop == 'load') {
        $(tujuan).set('html','<img src="var/loading.gif" style="width:16px;"> Loading please wait...');
    } else {
        if(obj!= undefined) {
            $(obj).value = 'Processing...';
            $(obj).disabled = true;
        }
    }
}

var hideProses = function(pop,tujuan,obj,objVal) {
    if(pop == undefined) {
        $('loading').setStyle('display','none');
    } else if(pop == 'load') {
        $(tujuan).set('html','');
    } else {
        if(obj!= undefined) {
            $(obj).disabled = false;
            $(obj).value = objVal;
        }
    }
}

var reqPost = null;
var postData = function(page,tujuan,data,lanjut,pop,obj) {
    if(obj!= undefined) {
        var objVal = $(obj).value;
    }
    showProses(pop,tujuan,obj);
    reqPost = new Request({
        url:page,
        method:'post',
        data:data,
        onSuccess:function(x) {
            var cekErr = x.substring(0, 5);
            if(cekErr == 'Error') {
                alert(x);
                hideProses(pop,tujuan,obj,objVal);
            } else if(cekErr == 'Info ') {
                alert(x);
                hideProses(pop,tujuan,obj,objVal);
            } else {
                switch(x) {
                    case 'Login time out !':
                        alert('Info \nSession time out, silahkan login kembali !');
                        closePopDiv();
                        break;
                    case 'Not implemented yet !':
                        alert(x);
                        hideProses(pop,tujuan,obj,objVal);
                        break;
                    case '&nbsp;':
                        alert('Data berhasil diproses !');
                        if($type(lanjut)=="function") {
                            hideProses(pop,tujuan,obj,objVal);
                            lanjut();
                        } else {
                            hideProses(pop,tujuan,obj,objVal);
                        }
                        break;
                    default :
                        hideProses(pop,tujuan,obj,objVal);
                        $(tujuan).set('html',x);
                        this.response.text.stripScripts(true);
                        if($type(lanjut)=="function") {
                            lanjut();
                        }
                }
            }
        }
    }).send();
}
