/*---------------------------------------------------------
 * Baseclass for all Dialogs
 * ------------------------------------------------------*/
ZACK.dialog=function(docType, tpl, timeout) {
  this.docType=docType;
  this.template=tpl;
  this.timeout=timeout || 4000;
  this.container=null;

};

ZACK.dialog.prototype= {

/*---------------------------------------------------------
 * rather abstract method
 * ------------------------------------------------------*/
handleSuccessPost: function(o){
  var obj=eval('('+o.responseText+')');
  if(obj.stat=='fail' && obj.error=='Bad Image Code!'){
    alert('update failed: bad image Code!');
  }
},

/*---------------------------------------------------------
 * rather abstract method
 * ------------------------------------------------------*/
handleSuccessGetInfo: function(o){
  var obj=eval('('+o.responseText+')');
  if(obj.stat=='fail'){
    alert('getInfo failed!');
  }
},

makePostString: function(params){
    var rv='';
	for (var p in params) {
	  rv+= p+'='+ params[p]+'&';
	}	
  return rv;
},

/*---------------------------------------------------------
 * postItem
 * ------------------------------------------------------*/
postItem: function(params) {
	
  params.method=params.method || 'zack.obj.addEvent';
  params.docType=this.docType;
  params.appKey=ZACK.appKey;
  params.mKey=ZACK.mKey;
  params.auth_token=ZACK.auth_token;
  params.ownerID=ZACK.ownerID;
  params.groupID= ZACK.groupID;
  params.UID=ZACK.UID;
  params.rspm= 'json';

  if($('vImageCodP'))params.vImageCodP=$F('vImageCodP');

  var request = YAHOO.util.Connect.asyncRequest('POST', ZACK.RESTURL,
     {success:this.handleSuccessPost, 
	  failure: function(o){alert('error: got no information');},
	  timeout: this.timeout, 
	  scope: this} , this.makePostString(params)); 
  return true;
},
  
editItem: function(ID) {
  //console.log(this);
  var working=' &nbsp;&nbsp;&nbsp;<b>loading!</b>&nbsp;&nbsp;&nbsp;<img src="http://w3-x.net/zkit/styles/default/assets/loading-small.gif" width="16" height="16" />';
  $('xloading_'+ID).innerHTML=working;
  
  var params={
    method:'zack.obj.getvalues',
    appKey:ZACK.appKey,
    mKey:ZACK.mKey,
    auth_token:ZACK.auth_token,
    ID: ID,
    UID:ZACK.UID,
    ownerID: ZACK.ownerID,
    rspm: 'json'
  }
  var request = YAHOO.util.Connect.asyncRequest('POST', ZACK.RESTURL,
     {success:this.handleSuccessGetInfo, 
	  failure: function(o){alert('error: got no information');},
	  timeout: this.timeout, 
	  scope: this} , this.makePostString(params)); 
},
  
/*---------------------------------------------------------
 * deleteItem
 * ------------------------------------------------------*/
deleteItem: function(ID, elemID) {
	
  if(!confirm('Wirklich loeschen?'))
    return;

  var params={
    method:'zack.obj.delete',
    appKey:ZACK.appKey,
    mKey:ZACK.mKey,
    auth_token:ZACK.auth_token,
    ID: ID,
    UID: ZACK.UID,
    ownerID: ZACK.ownerID,
    rspm: 'json'
  }
  var request = YAHOO.util.Connect.asyncRequest('POST', ZACK.RESTURL,
     {failure: function(o){alert('server error on delete');},
      success: function(o){
      	var obj=eval('('+o.responseText+')');
        if(obj.stat=='fail' )
          alert(obj.error);     	
      }, 
	  timeout: this.timeout, 
	  scope: this}, this.makePostString(params));
  try {
    var el=new YAHOO.util.Element('comment_list'); 
    el.removeChild($(elemID));
  }
  catch(e){}
  try {
    var el=new YAHOO.util.Element('event_list'); 
    el.removeChild($(elemID));
  }
  catch(e){}
}

} // -------------------------- end ZACK.dialog



