
function app_check_fields() {

    actionControl = document.getElementById('appAction');
    senderControl = document.getElementById('appSender');
    actionParam = document.getElementById('appActionParam');
    actionTarget = document.getElementById('appTarget');

    if (actionControl == null) {
        alert('The hidden input "appAction" (id) is missing!');
        return;
    }

    if (actionParam == null) {
        alert('The hidden input "appActionParam" (id) is missing!');
        return;
    }

    if (senderControl == null) {
        alert('The hidden input "appSender" (id) is missing!');
        return;
    }

    if (actionTarget == null) {
        alert('The hidden input "appTarget" (id) is missing!');
        return;
    }
}

function app_postback_ajax(target, control, action, param) {
    app_check_fields();

    action = action.substring(12);

    // wrapper for post
    $.postJSON = function(url, data, callback) {
        $.post(url, data, callback, "json");
    };

    // get all select and input tags inside a div
    var selector = "select, input";
    // serialize 
    var serialized = $(selector).serializeArray();
 
    // add action param to post
    var appActionObj = { name: 'appAction', value: action };
    serialized.push(appActionObj); 
    appActionObj = { name: 'appSender', value: control };
    serialized.push(appActionObj); 
    appActionObj = { name: 'appActionParam', value: param };
    serialized.push(appActionObj); 
    appActionObj = { name: 'appTarget', value: target };
    serialized.push(appActionObj); 
    appActionObj = { name: 'appAjax', value: '1' };
    serialized.push(appActionObj); 

    // post page & handle return
    $.post(document.location.href,  serialized,
    function(data){
        jQuery.each(data, function(key, val){
        if (key == 'viewStateContainer')
            $("#"+key).html(val); 
        else {
            //alert(val);
            $("#"+key).html(val); 
           /* var doc = document.getElementById(key);
            if (doc == null)
                alert('Div not found with id '+key);
            else
                doc.innerHTML = val;
        */
        }
//        $("#"+key).html(val);       
      });
    },'json');    

}

function app_postback(target, control, action, param) {
	app_check_fields();
	
	action = action.substring(7);
	
	form = document.getElementById('appForm');
	if (form == null) {
		alert('The main form "appForm" (id) is missing!');
		return;
	}

// target = page or user control
// control = control in actoion
// action = method to call
// param = just some param
    document.getElementById('appAction').value = action; // control (Button etc..)
    document.getElementById('appSender').value = control; // sender page or usercontrol
    document.getElementById('appActionParam').value = param; // action method
    document.getElementById('appTarget').value = target;  // param
    $('#ajaxDialog').jqmShow(); 

	form.submit();	
}


/*    appActionObj.name = 'appAction';
    appActionObj.value = 'add'; */
    


 //{"name":"mytest", "value":"mytestvalue"]}); //["mytest","mytestval"]);
 /*     jQuery.each(arr, function(i, field){
          jQuery.each(field, function(i, field2){
             // jQuery.each(arr, function(i, field){
                alert(field2+' '+i);
             // });
          });
      });
*/
