/*
 * Project     : Building Blocks
 * Block       : WebForms
 * Created on  : 12/06/2003
 * CVS Id      : $Id: iRemoteScripting.js,v 1.1 2004/11/18 10:40:46 miki Exp $
 *
 * (C) 2002 Polymita Technologies, S.L
 *          All rights reserved.
 */

/**
 * Library javascript for remote scripting.
 * A collection of javascript methods to do remote scripting techniques; only
 * include the implementqations to connect with server and receive the data
 * associates with the service resolution.
 *
 * @author <a href="mailto:javier.alperte@polymita.com">Javier Alperte Pérez-Rejón</a>
 * @version $Revision: 1.1 $, $Date: 2004/11/18 10:40:46 $
 */

/** our IFrame object for remote scripting
 */
var IFrameObj; 


/**
 * Call the remote service.
 * Make a new call to a specific remote service.
 *
 * @param urlBase   URL of remote server
 * @param idGroup   Group identifier of service
 * @param idService Identifier of service
 * @param params    String with params and values
 * @param handler   Handler of remote service
 */    
function callRemoteService(urlBase,idGroup,idService,params,handler) {

    var url =   "";

    if( urlBase.indexOf('?') >= 0 ) {
        url = urlBase+'&groupId='+idGroup+'&serviceId='+idService;
    } else {
        url = urlBase+'?groupId='+idGroup+'&serviceId='+idService;
    }

    if (params!=null && params!='')
        url+='&'+params;

    url+="&handler="+handler;

    setTimeout('callServer("'+url+'")',10);
}
       
/**
 * Call the remote server.
 *
 * @param url URL of remote server
 */    
function callServer(url) {     

  if (!document.createElement) {return true};            
    
    
  if (!IFrameObj && document.createElement) {                
    
    try {
        /** Create the IFrame and store a global reference.
         * This will only happen the first time callViewService() is called.
         */
        tempIFrame = document.createElement('iframe');
        tempIFrame.setAttribute('id','RSIFrame');
        tempIFrame.style.border='0px';
        tempIFrame.style.width='0px';
        tempIFrame.style.height='0px';
        IFrameObj = document.body.appendChild(tempIFrame);

        if (document.frames) {

            /** This is for IE5 Mac, because it will only
             * allow access to the document object
             * of the IFrame if we access it through
             * the document.frames array
             */
            IFrameObj = document.frames['RSIFrame'];
        }
    } catch(exception) {
      // This is for IE5 PC, which does not allow dynamic creation
      // and manipulation of an iframe object. Instead, we'll fake
      // it up by creating our own objects.
      iframeHTML='\<iframe id="RSIFrame" style="';
      iframeHTML+='border:0px;';
      iframeHTML+='width:0px;';
      iframeHTML+='height:0px;';
      iframeHTML+='"><\/iframe>';
      document.body.innerHTML+=iframeHTML;
      IFrameObj = new Object();
      IFrameObj.document = new Object();
      IFrameObj.document.location = new Object();
      IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
      IFrameObj.document.location.replace = function(location) {
      this.iframe.src = location;}
    }	
  }

  if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {

        /** We have to give NS6 a fraction of a second
         * to recognize the new IFrame
         */
        setTimeout('callServer("'+url+'")',10);		
        return false;
    }

    if (IFrameObj.contentDocument) {
        // For NS6
        IFrameDoc = IFrameObj.contentDocument; 
    } else if (IFrameObj.contentWindow) {
        // For IE5.5 and IE6
        IFrameDoc = IFrameObj.contentWindow.document;
    } else if (IFrameObj.document) {
        // For IE5
        IFrameDoc = IFrameObj.document;
    } else {
        return true;
    }

    IFrameDoc.location.replace(url);

    return false;
    
}

/** Manage the handler invocation from the onload
 *  event of the pages loaded in the hidden IFRAME.
 * 
 * @param doc     Document of loaded page un the hidden IFRAME.
 * @param idData  Identifier of div data tag
 * @param handler Name of handler to invoke.
 */
function handlerManager(doc,idData,handler) {
    var data    =   doc.getElementById(idData);
    method  =  handler+'(data)';        
    eval(method);        
}

/**
 * Timer for execute a task sometimes.
 *
 * @author <a href="mailto:javier.alperte@polymita.com">Javier Alperte Pérez-Rejón</a>
 */

/** Create a new instance.
 * @param nameVar Name of variable that contain the created object.
 */
function Timer(nameVar){
  
  this.nameVar      =   nameVar;
  this.loop         =   false;
  this.time         =   0;  
  this.timer        =   null;
  this.invocation   =   null;

  this.setTime          = Timer_setTime;
  this.getTime          = Timer_getTime;
  this.setLoop          = Timer_setLoop;
  this.isLoop           = Timer_isLoop;
  this.start            = Timer_start;
  this.stop             = Timer_stop;
  this.setHandler       = Timer_setHandler;
  this.getHandler       = Timer_getHandler;
  this.setTimeout       = Timer_setTimeout;
}

/** Change the interval between invocation to the task.
 * @param t New interval value.
 */
function Timer_setTime(t) {
    this.time   =   t;
}

/** Obtain the interval between invocation to the task.
 * @return Interval value.
 */
function Timer_getTime() {
    return this.time;
}

/** Change the repetition property of the timer.
 * @param l New repetition value.
 */
function Timer_setLoop(l) {
    this.loop   =   l;
}

/** Indicate if the task is repetitive. 
 * @return Repetitive property.
 */
function Timer_isLoop() {
    return this.loop;
}

/** Start the timer.
 * Do the task one time and then if the loop property if true
 * start a timer. 
 */
function Timer_start() {
    this.timeout    =   Timer_startTimer(this.nameVar);
}

/** Stop the timer.
 */
function Timer_stop() {    
    clearTimeout(this.timer);
}

/** Change the task/handler of timer.
 * @param h New handler of timer.
 */
function Timer_setHandler(h) {
    this.invocation =   h;
}

/** Obtain the task/handler of timer.
 * @return Handler of timer.
 */
function Timer_getHandler() {
    return this.invocation;
}

/** Change the actual timeout.
 * @param t New timeout value.
 */
function Timer_setTimeout(t) {
    this.timer  =   t;
}

/** Start a timeout with the params of timer.
 * This method is necessary because timeout in javascript
 * only functions with methods.
 *
 * @param timer Timer.
 */
function Timer_startTimer(nameVar) {
        
    
    eval(eval(nameVar+".getHandler()"));
            
    if (eval(nameVar+".isLoop()")) {                        
        var reInvocation  =   "Timer_startTimer('"+nameVar+"')";        
        var t             =   eval(nameVar+".getTime()");
        var tOut          =   setTimeout(reInvocation,t);
        eval(nameVar+".setTimeout("+tOut+")");
    }

}
