
var zackAPI = {};
/*-----------------------------------------------------------
global parameters:
  zack_global_app_id
  zack_global_resturl

-------------------------------------------------------------*/

zackAPI.callMethod = function(APIMethod, params, listener, attempts, server) {
	if (typeof params != 'object') params = {}; 
	params.method = APIMethod; 
	
	var RESTURLROOT = zack_global_resturl;  
	if (server) RESTURLROOT = server + RESTURLROOT;
	//alert(params.title);
	params.rspm = 'xml';
	params.appKey = zack_global_app_id;
	//params.cb = new Date().getTime();

	var params_keyA = [];
	var RESTURL = '';
	for (var p in params) {
      if (p=='RESTURL') continue;
	  RESTURL+= '&'+p+'='+ /*escape_utf8*/(params[p]);
	}	
	params.RESTURL = RESTURL; // 
	
	var attempts = (attempts == undefined) ? 1 : attempts;
	var req = new XMLHttpRequest();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.responseText == '' && attempts<2) {
					attempts++;
					req.abort();
					zackAPI.callMethod(APIMethod, params, listener, attempts, server);
				} else {
					zackAPI.handleResponse(req.responseXML, APIMethod, params, req.responseText, listener);
				}
			}
		}
		req.open('POST', RESTURLROOT);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//alert(RESTURL.replace('&','\n&'))
		req.send(RESTURL);
	}
}

zackAPI.getCallBackName = function (dotted) {
	return dotted.split('.').join('_')+'_onLoad';
}

zackAPI.handleResponse = function(responseXML, APIMethod, params, responseText, listener) {
	if (!responseXML) { //OPERA!
		var success = (responseText.indexOf('stat="ok"') > -1) ? true : false;
	} else {
		var success = (responseXML.documentElement && responseXML.documentElement.getAttribute('stat') == 'ok') ? true : false;
	}
	listener = (listener) ? listener : this;
	listener[this.getCallBackName(APIMethod)](success, responseXML, responseText, params);
}

//----------------------------------------------------------
// Set or Category - Membership can be updated/display
// with this Class
//----------------------------------------------------------

// responseHandler for DIV on Load Call Back
divHandleResponse=function(success, responseXML){
	if(success){
	  this.innerHTML = this.newHTML;
	}
	else {this.innerHTML = '<b>?</b>';}
}

var CSCheckbox =function (method){
  this.method=method;
  this.updateMsg='<em>' +  '  updating ...</em>';
};

CSCheckbox.prototype.setOnHTML=function(html){
  this.onHTML=html;
};

CSCheckbox.prototype.setOffHTML=function(html){
  this.offHTML=html;
};

CSCheckbox.prototype.add2Set=function(id, cat){
  var div = document.getElementById('a2c_'+id+'_'+cat);
  div.innerHTML = this.updateMsg;
  this.method='zack.obj.add2Set';
  div.newHTML=this.onHTML.replace('{p1}', id); div.newHTML=div.newHTML.replace('{p2}', cat);
  div.zack_obj_add2Set_onLoad = divHandleResponse;
  zackAPI.callMethod(this.method, {ID:id, set_id:cat}, div);   
  
};

CSCheckbox.prototype.removeFromSet=function(id, cat){
  var div = $('a2c_'+id+'_'+cat);
  div.innerHTML = this.updateMsg;
  this.method='zack.obj.removeFromSet';
  div.newHTML=this.offHTML.replace('{p1}', id); div.newHTML=div.newHTML.replace('{p2}', cat);
  div.zack_obj_removeFromSet_onLoad=divHandleResponse;
  zackAPI.callMethod(this.method, {ID:id, set_id:cat}, div);   
  
};

CSCheckbox.prototype.add2Category=function(id, cat){
  var div = $('a2c_'+id+'_'+cat);
  div.innerHTML = this.updateMsg;
  this.method='zack.obj.add2Category';
  div.newHTML=this.onHTML.replace('{p1}', id); div.newHTML=div.newHTML.replace('{p2}', cat);
  div.zack_obj_add2Category_onLoad = divHandleResponse;
  zackAPI.callMethod(this.method, {ID:id, cid:cat}, div);   
};

CSCheckbox.prototype.removeFromCategory=function(id, cat){
  var div = $('a2c_'+id+'_'+cat);
  div.innerHTML = this.updateMsg;
  this.method='zack.obj.removeFromCategory';
  div.newHTML=this.offHTML.replace('{p1}', id); div.newHTML=div.newHTML.replace('{p2}', cat);
  div.zack_obj_removeFromCategory_onLoad=divHandleResponse;
  zackAPI.callMethod(this.method, {ID:id, cid:cat}, div);   
  
};
// property in triple tag form i.d. zack:chn=news
//
CSCheckbox.prototype.addProperty=function(id, prop){
  var $prop=prop.replace(':', '_');
  $prop=$prop.replace('=', '_');
  $prop=$prop.replace('/', '');
  var div = $('ap_'+id+'_'+$prop);
  div.innerHTML = this.updateMsg;
  this.method='zack.obj.addProperty';
  div.newHTML=this.onHTML.replace('{p1}', id); 
  div.newHTML=div.newHTML.replace('{p2}', prop);
  div.zack_obj_addProperty_onLoad = divHandleResponse;
  zackAPI.callMethod(this.method, {ID:id, property:prop}, div);   
};

CSCheckbox.prototype.removeProperty=function(id, prop){
  var $prop=prop.replace(':', '_');
  $prop=$prop.replace('=', '_');
  $prop=$prop.replace('/', '');
  var div = $('ap_'+id+'_'+$prop);
  div.innerHTML = this.updateMsg;
  this.method='zack.obj.removeProperty';
  div.newHTML=this.offHTML.replace('{p1}', id); 
  div.newHTML=div.newHTML.replace('{p2}', prop);
  div.zack_obj_removeProperty_onLoad=divHandleResponse;
  zackAPI.callMethod(this.method, {ID:id, property:prop}, div);   
  
};


