
/**
 * Common utilities for EasyTalk
 * @author Raj
 * (c) Impiger Technologies, 2009
 */
 
var WSURL = "ws/endpoint.php";

var sucess = function(response, opts) {
      
         if ( typeof response != 'undefined' && typeof response.responseText != 'undefined' )
      	{
      		resp = Ext.util.JSON.decode(response.responseText);
      		if ( resp.success == true )
      		{
      			Ext.Msg.alert('Success', resp.result);
      		}
      		else
      		{
      			Ext.Msg.alert('Error', resp.result);
      		}
      	}
      	else
      	{
      		Ext.Msg.alert('Error', "couldn't get server response");
      	}
         /*if (respObj.success) {
            if (successCallback) {
                successCallback(respObj.result);
            };
         } else {
            // failed!
            if (failCallback) { 
               failCallback(respObj.result);
            } else {
               alert("Error! - " + respObj.result);
            };            
         }*/

      }
     
	/* onSuccess handler for Ajax.Request
      var sucessProto = function(transport) {
      
         if ( typeof transport != 'undefined' && typeof transport.responseText != 'undefined' )
      	{
      		eval("resp = "+transport.responseText);
      		if ( resp.success == true )
      		{
      			alert(resp.result);
      		}
      		else
      		{
      			alert(resp.result);
      		}
      	}
      	else
      	{
      		alert("couldn't get server response");
      	}
      }

/**
 * Invoke a webservice.
 * @param service Name of service
 * @param method 
 * @param data Data object. This will be serialized as JSON and sent out.
 */
/**
 * Invoke a webservice.
 * @param service Name of service
 * @param method 
 * @param data Data object. This will be serialized as JSON and sent out.
 */
function invokeWS(method, params, successCallback, scope, url) {
   success = successCallback || sucess;
   url     = url || WSURL;
   Ext.Ajax.request({
					url:url,
					methos: method || 'POST',
					params: params || {},
					success: success,
					failure: function(response) {
			      	if ( typeof response != 'undefined' && typeof response.responseText != 'undefined' )
			      	{
			      		resp = Ext.util.JSON.decode();
			      		Ext.Msg.alert('Error', resp.result);
			      	}
			      	else
			      	{
			      		Ext.Msg.alert('Error', "There was a problem communicating with server!");
			      	}
			      },
					scope: scope || this
				});
   
   //return request;
}

/**
 * Invoke a webservice using prototype lib.
 * @param service Name of service
 * @param method 
 * @param data Data object. This will be serialized as JSON and sent out.
 */
function invokeProtoWS(method, params, successCallback, scope, url) {
   if ( (typeof scope == 'object') && (typeof successCallback == 'function' || typeof successCallback == 'object') )
   {
   	eval("sucessProto = successCallback.bind(scope)");
   }
   url     = url || WSURL;
   new Ajax.Request(url,{
					methos: method || 'POST',
					parameters: params || {},
					onSuccess: sucessProto,
					onFailure: function(transport) {
			      	if ( typeof transport != 'undefined' && typeof transport.responseText != 'undefined' )
			      	{
			      		eval("resp = "+transport.responseText);
			      		alert(resp.result);
			      	}
			      	else
			      	{
			      		alert("There was a problem communicating with server!");
			      	}
			      }
				});
}

	var ajax_loading_callback = function() {
	    Ext.MessageBox.wait('Processing Data...', 'Please wait');
	}
	
	var ajax_loaded_callback = function() {
	    Ext.MessageBox.hide();
	}
	
	
    //namespace
    var ECN = {};
    
    ECN.getEl = function(el){
 		if ( typeof el == 'string' )
 		{
 			el = $(el);
 		}
 		return el;
    }

	//Template obj
    ECN.Template = function(html){
    	this.html = html;
    	this.template = new Template(html);
    }
    
    ECN.Template.prototype = {
    	insertFirst: function(el, values)
    	{
    		el = ECN.getEl(el);
    		htmlFragments = this.template.evaluate(values);
    		el.insert({top:htmlFragments});
    	}
    }
    
    /**
	 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
	 * @param {String/HTMLElement} el A DOM element or its id
	 * @static
	 */
	ECN.Template.from = function(el){
	    el = ECN.getEl(el);
	    return new ECN.Template(el.value || el.innerHTML);
	};

