
//main function for old Ajax call Loic Calvy Trademark :)
function VM_RemoteScriptSSOConnexionInfo(urlSite,urlSiteSSL,callbackFunction)
{
	remoteExecute((VM_GetTypeHttp('self')=='https' ? urlSiteSSL : urlSite )+ "/site/layout/tools/SSOConnexionInfo.aspx", null, callbackFunction,false);
}

//get protocol of page	
function VM_GetTypeHttp(Target)
{
    ChaineLoc=""+eval(Target+'.location')+""
    ChaineLoc=ChaineLoc.toLowerCase();
    if(ChaineLoc.substr(0,5)=="https")
        {
        return "https";
        }
    else
        {
        return "http";
        }
}	

//get cookie by name
function VM_GetCookie(name){var ToExec = new RegExp(name + "=([^;]+)");var valeur = ToExec.exec(document.cookie);return (valeur != null) ? valeur[1] : null;}

//set cookie name/value
function VM_SetCookie(name, value) {document.cookie=  name + "=" + value + "; path=/";}

//function d'encodage des url essayant d'utiliser la methode conseillée pour gerer l'utf8 , escape etant ascii uniquement	
function VM_EncodeUrl(stringToEncode)	
{
	try
	{ 
		return encodeURIComponent(stringToEncode);
	}
	catch(exception)
	{
		return escape(stringToEncode);
	}
}
	
///////////////////////////////////////MOTEUR/////////////////////////////
var nbRemoteConnection = 0;
var nbMaxRemoteConnection = 10;
var RemoteConnection = new Array();
var curentBrowser = getBrowser();
var containerName;


function remoteExecute( remoteUrlPage, parms, callbackFunction, debug ){

    var contextObj = RemoteConnection[ piGetContextID() ];

	contextObj.callback = callbackFunction;

	var vis = (debug == null)? false : true;
	
	contextObj.setVisibility( vis );
	contextObj.GET( remoteUrlPage,  parms , null );

    return contextObj.id;
}

function piContextObj( contextID ){

  this.id = contextID;
  this.busy = true;
  this.callback = null;
  this.container = contextCreateContainer( contextID );
  this.GET = contextGET;
  this.getPayload = contextGetPayload;
  this.setVisibility = contextSetVisibility;
}

function contextCreateContainer( containerName ){
  var container;
  
      
  switch( curentBrowser ) {
 
        case 'NS':
      container = new Layer(100);
      container.name = containerName;
      container.visibility = 'hidden';
      container.clip.width = 100;
      container.clip.height = 100;
      break;

   
    case 'MOZ':  
      var span = document.createElement('SPAN');
      span.id = "SPAN" + containerName;
      document.body.appendChild( span );
      var iframe = document.createElement('IFRAME');
      iframe.name = containerName;
      iframe.id = containerName;
      try
      {
        iframe.style.width=0;
        iframe.style.height=0;
      }
      catch(exception)
      {
      }
      span.appendChild( iframe );
      container = iframe;
      break;

    case 'OPR':  
      var span = document.createElement('SPAN');
      span.id = "SPAN" + containerName;
      document.body.appendChild( span );
      var iframe = document.createElement('IFRAME');
      iframe.name = containerName;
      iframe.id = containerName;
      span.appendChild( iframe );
      container = iframe;
      break;

    case 'KONQ':  
      var span = document.createElement('SPAN');
      span.id = "SPAN" + containerName;
      document.body.appendChild( span );
      var iframe = document.createElement('IFRAME');
      iframe.name = containerName;
      iframe.id = containerName;
      span.appendChild( iframe );
      container = iframe;

      // Needs to be hidden for Konqueror, otherwise it'll appear on the page
      span.style.display = none;
      iframe.style.display = none;
      iframe.style.visibility = hidden;
      iframe.height = 0;
      iframe.width = 0;

      break;
    
    case 'IE':
    
	  document.body.insertAdjacentHTML( "afterBegin", '<span id="SPAN' + containerName + '"></span>' );
      var span = document.all( "SPAN" + containerName );
      var html = '<iframe name="' + containerName + '" src="' + (VM_GetTypeHttp('self')=='https' ? VM_UrlSiteSSL : VM_UrlSite )  +'/blank.htm" width="0" height="0"></iframe>';
	  span.innerHTML = html;
	  span.style.display = 'none';
      container = window.frames[ containerName ];

    break;
      
  }
  return container;
}



function contextGET( rsPage, parms ,cacheParamName){

  var URL = rsPage;
  URL += "?C=" + this.id;

    if (parms != null){
      if (typeof(parms) == "string")
      {
        URL += "&P0=" + encodeUrl(parms+'') + "";
        if(cacheParamName!="" && cacheParamName!=null)
        {
			URL += "&" + cacheParamName + "=" +  encodeUrl(parms+'');
        }
      } 
      else 
      {
      
        for( var i=0; i < parms.length; i++ )
        {
          URL += "&P" + i + "=" + encodeUrl(parms[i]+'') + "";
           if(i==0 && cacheParamName!="" && cacheParamName!=null)
			{
				URL += "&" + cacheParamName + "=" +  encodeUrl(parms[i]+'');
			}
        }
      } 
    } 
  var d = new Date();
  URL += "&U=" + d.getTime();

  switch( curentBrowser ) {
    case 'NS':
      this.container.src = URL;
      break;
    case 'IE':
      this.container.document.location.replace(URL);
      break;
    case 'MOZ':
      this.container.src = '';
      this.container.src = URL; 
      break;
    case 'OPR':
      this.container.src = '';
      this.container.src = URL; 
      break;
    case 'KONQ':
      this.container.src = '';
      this.container.src = URL; 
      break;
  }  
 }

function contextGetPayload(){
  switch( curentBrowser ) {
    case 'NS':
      return this.container.document.forms['Form1'].elements['remoteTextArea1'].value;
    case 'IE':
      return this.container.document.forms['Form1']['remoteTextArea1'].value;
    case 'MOZ':
      return window.frames[this.container.name].document.forms['Form1']['remoteTextArea1'].value; 
    case 'OPR':
      var textElement = window.frames[this.container.name].document.getElementById("remoteTextArea1");
    case 'KONQ':
      var textElement = window.frames[this.container.name].document.getElementById("remoteTextArea1");
      return textElement.value;
  }  
}

function contextSetVisibility( vis ){
  switch( curentBrowser ) {
    case 'NS':
      this.container.visibility = (vis)? 'show' : 'hidden';
      break;
    case 'IE':
      document.all("SPAN" + this.id ).style.display = (vis)? '' : 'none';
      break;
    case 'MOZ':
      document.getElementById("SPAN" + this.id).style.visibility = (vis)? '' : 'hidden';
    case 'OPR':
      document.getElementById("SPAN" + this.id).style.visibility = (vis)? '' : 'hidden';
      this.container.width = (vis)? 250 : 0;
      this.container.height = (vis)? 100 : 0;
      break;
  }  
}

function piGetContextID()
{

    var contextObj;
    for (var i = 1; i <= nbRemoteConnection; i++)
    {
        contextObj = RemoteConnection[ 'jsrs' + i ];
        if ( !contextObj.busy )
        {
            contextObj.busy = true;      
            return contextObj.id;
        }
    }
    
    if ( nbRemoteConnection <= nbMaxRemoteConnection )
    {
        var contextID = "jsrs" + (nbRemoteConnection + 1);
        RemoteConnection[ contextID ] = new piContextObj( contextID );
	    nbRemoteConnection++;
	    return contextID;
    }
    else
    {
        return null;
    }
}


function remoteLoaded( contextID ){
 var contextObj = RemoteConnection[ contextID ];
 if( contextObj.callback != null)
  {
   contextObj.busy = false;
   contextObj.callback( remoteUnescape( contextObj.getPayload() ), contextID );
  }
  contextObj.callback = null;
  contextObj.busy = false;
}


function remoteUnescape( str ){
  return str.replace( /\\\//g, "/" );
}

function getBrowser(){

  if (document.layers) return "NS";
  if (document.all) {
		var agt=navigator.userAgent.toLowerCase();
		var is_opera = (agt.indexOf("opera") != -1);
		var is_konq = (agt.indexOf("konqueror") != -1);
		if(is_opera) {
			return "OPR";
		} else {
			if(is_konq) {
				return "KONQ";
			} else {
				return "IE";
			}
		}
  }
  if (document.getElementById) return "MOZ";
  return "OTHER";
}
