//  PC-Kiim2008-Engine.js Release 1.00
//  For KIIM2008 engine (http://www.kiim.info/)
//  To be used under a commercial license only.
//  Copyright (c) 1999-2008 Les éditions plaisirs de la connaissance inc.

var cData = "";
var ua = navigator.userAgent.toLowerCase();
var xmlhttp = "";
var kF = new Array();

function KiimAjaxMessage(obj,args){
   if(kF.length > 0){
      kF.splice(0,kF.length);
   }
   var kmAjaxMsg = new Object();
   kmAjaxMsg.grid = "9";
   kmAjaxMsg.datakey = kmGetListViewDataKey(kmCache.cId);
   kmAjaxMsg.sourceid = kmCache.cId;
   kmAjaxMsg.controlid = obj.id;
   kmAjaxMsg.oldvalue = "<![CDATA["+kmCache.cValue+"]]>";
   kmAjaxMsg.newvalue= "<![CDATA["+args+"]]>";
   kF.push('<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"no\"?>');
   kF.push('<arguments xmlns:tns=\"http:\/\/www.kiim.info\/ajax\/">');
   kF.push(KiimAjaxXml(kmAjaxMsg));
   kF.push('</arguments>');
   return kF.join('');
}
function KiimAjaxXml(data){
   var str = "";
   var i;
   for(i in data){
      str += "<"+ i +">" + kmStripUnicode(data[i]) +  "</"+ i +">";
   }
  return str;
}
function KiimRPC(method,httpmethod,sdata,mode,loadtype,ctx){
   //method = methodname
   //httpmethod = GET/POST/SOAP
   //sdata = String/Soap Message
   //mode  = XML/TEXT
   //loadtype = 0 synchronous / 1 asynchronous
   //ctx = Any data pertinent to the call
   
   xmlhttp = KiimRPCInit();
   switch(httpmethod.toUpperCase()){
   case "GET" :
      if(loadtype == "0") {
	     xmlhttp.open("GET", soapHref, false);
	  }
	  else{
	     xmlhttp.open("GET", soapHref, true);
	  }
	  xmlhttp.setRequestHeader ("Content-Type", "text/plain");
	  xmlhttp.setRequestHeader("Charset", "ISO-8859-1");
	  xmlhttp.setRequestHeader("User-Agent","XMLHTTP/1.0");
   break;
   case "SOAP" :
      if(loadtype == "0"){
	     xmlhttp.open("POST", soapHref, false);
	  }
	  else{
	     xmlhttp.open("POST", soapHref, true);
	  }
	  if(ua.indexOf("mac os x") > -1) {
         xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  }
	  else{
	     xmlhttp.setRequestHeader("Content-Type", "text/XML; Charset=utf-8");
	  }
	  xmlhttp.setRequestHeader("SOAPAction", baseSoapXMLns + method);
	  xmlhttp.setRequestHeader("User-Agent","XMLHTTP/1.0");
   break;
   case "POST" :
      if(loadtype == "0") {
	     xmlhttp.open("POST", soapHref, false);
	  }
	   else{
	     xmlhttp.open("POST", soapHref, true);
	  }
	  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  xmlhttp.setRequestHeader("Content-Type", "text/XML; Charset=utf-8");
   break;
   default :
      xmlhttp.open("GET", soapHref, true);
      xmlhttp.setRequestHeader ("Content-Type", "text/plain; charset=iso-8859-1");
	  xmlhttp.setRequestHeader("User-Agent","XMLHTTP/1.0");
   }
   xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState==4) {
      
         if(xmlhttp.status == "200"){
         
          	if(mode.toUpperCase() == "XML") {
			 
			   top.KiimRPCXml(xmlhttp.responseXML,ctx);
			}
	        else{
			   top.KiimRPCText(xmlhttp.responseText,sdata,ctx);
			}
	     }
	     else{
            top.KiimRPCAlert("There was a problem retrieving the XML data: " + xmlhttp.statusText);
	     }
    }  
   };
   try{
      if(httpmethod.toUpperCase() == "GET"){
	     if(ua.indexOf("mac os x") > -1) {
            xmlhttp.send(null);
		 }
		 else{
		    xmlhttp.send();
		 }
	  }
	  else{
         if(ua.indexOf("mac os x") > -1) {
		    sdata = 'message='+escape(sdata);
		 }
	     xmlhttp.send(sdata);
	  }
   }
   catch (e) {
        top.KiimRPCAlert("There was a problem sending the data...");
   }
   return;
}
function KiimRPCAlert(msg){
   alert(msg);
   return true;
}
function KiimRPCDelete(){
   delete xmlhttp;
   delete RPCobj;
   return true;
}
function KiimRPCCallback(dat,str,ctx){

   if(ua.indexOf("windows") > -1) {
      var scriptTag = document.getElementById('JSCallback');
      var head = document.getElementsByTagName('head').item(0);
      if(scriptTag){
	     head.removeChild(scriptTag);
	  }
      script = document.createElement('script');
	  script.setAttribute('id','JSCallback');
	   if(ua.indexOf("windows") > -1) {
		 script.setAttribute('type','text/javascript');
		 if(dat != "null") {
		    script.text = dat + str;
		 }
		 else{
		    script.text = str;
		 }
	  }
	  head.appendChild(script);
  }
  if(ua.indexOf("mac os x") > -1){
     if(typeof window.frames["macCache"] == "undefined"){
        KiimMacIFrame();
		macIFrame = window.frames["macCache"];
     }
     if(dat == "null"){
	    dat = "";
	 }
	 if(str == "null"){
	     str = "";
	 }
	 var objMacDoc = (macIFrame.contentDocument) ? macIFrame.contentDocument : (macIFrame.contentWindow) ? macIFrame.contentWindow.document : (window.frames && window.frames['macCache']) ? window.frames['macCache'].document : (macIFrame.document) ? macIFrame.document  : null;
	 objMacDoc.clear();
	 objMacDoc.open();
     objMacDoc.write('<scr'+'ipt defer="defer" type="text/javascript" id="JSCallback">');
	 objMacDoc.write(dat+str);
     objMacDoc.write('<\/scr'+'ipt>\n');
     objMacDoc.close();
   }
   return;
}
function KiimMacIFrame(){
   var tempIFrame = document.createElement('iframe');
   tempIFrame.setAttribute('id','macCache');
   tempIFrame.setAttribute('name','macCache');
   tempIFrame.setAttribute('src','blank.asp?id=newframe');
   tempIFrame.style.border='0px';
   tempIFrame.style.width='0px';
   tempIFrame.style.height='0px';
   document.body.appendChild(tempIFrame);
   return;
}
function KiimRPCInit(){
   if (window.XMLHttpRequest) {
      RPCobj = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      try{
         RPCobj = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e){}
   }
   return RPCobj;
}
function KiimRPCXml(xmlDoc,ctx){
   var callback = "";
   method = xmlDoc.documentElement.childNodes;
   if (method.item(0).firstChild.nodeName == "SOAP-ENV:Fault"){ 
	  var mFaultCode = xmlDoc.getElementsByTagName("faultcode")[0].firstChild.nodeValue;
	  var mFaultString = xmlDoc.getElementsByTagName("faultstring")[0].firstChild.nodeValue;
	  callback = xmlDoc.getElementsByTagName("callback")[0].firstChild.nodeValue;
	  if(mFaultString != "null"){
         KiimRPCAlert(mFaultString);
	  }
	  if(callback != "null"){
		 KiimRPCCallback("null",callback,ctx);
	  }
   }
   else{
      var js = xmlDoc.getElementsByTagName("js")[0].firstChild.nodeValue;
	  var data = xmlDoc.getElementsByTagName("data")[0].firstChild.nodeValue;
      callback = xmlDoc.getElementsByTagName("callback")[0].firstChild.nodeValue;
	  var cData = xmlDoc.documentElement.firstChild.firstChild.lastChild.lastChild.firstChild;
      if(callback != "null"){
		 KiimRPCCallback(js,callback,ctx);
      }
   }
   KiimRPCDelete();
   return true;
}
function KiimRPCText(str,sdiv,ctx){
   try{
      document.getElementById(""+sdiv+"").innerHTML = str;
   }
   catch(e){}
   KiimRPCDelete();
   return true;
}
// Utils
function KiimStripUnicode(s) {
   o = "";
   for(m=0;s.charAt(m);++m){
      if ( (c=s.charCodeAt(m))<128&&c!=38){
	    o+=s.charAt(m);
      }
      else if (c==38){
	     o+="&";
      }
      else{
	     o+="&#"+c+";";
      }     
    }
    return o;
}
// Soap Objects
function KiimSoapCredentials(licence,userdomain,userenv,username,userpassword,timestamp,usertoken){
  this.licence = licence || 0 ;
  this.userdomain = userdomain ||  "Kiim";
  this.userenv = userenv;
  this.username = userpassword;
  this.userpassword = userpassword;
  this.timestamp = timestamp || Date();
  this.usertoken = usertoken;
  return this;
}
function KiimSoapMessage(methodname){
  this.method = methodname||"listMethods";
  this.params = [];
  return this;
}
KiimSoapMessage.doStructXML = function(data){
  var str = "";
  var i;
  for (i in data){
     str += "<tns:"+ i +">" + KiimStripUnicode(data[i]) +  "</tns:"+ i +">";
  }
  return str;
};
KiimSoapMessage.prototype.addParameter = function(data){
  if (arguments.length===0){
     return;
  }
  this.params[this.params.length] = data;
};
KiimSoapMessage.prototype.setMethod = function(methodName){
  if (!methodName){
     return;
  }
  this.method = methodName;
};
KiimSoapMessage.prototype.xml = function(){
  var method = this.method;
  var xml = "";
  var i;
  var stri;
  xml += '<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"no\"?>';
  xml += '<!--Request From KIIM 2008 SOAP Client Version 12.0 - Revision 2008-03-18 Copyright 1999-2008 Les &Eacute;ditions Plaisirs De La Connaissance Inc.-->';
  xml += '<soap:Envelope xmlns:soap="http:\/\/schemas.xmlsoap.org\/soap/envelope\/" xmlns:xsi="http:\/\/www.w3.org\/2001\/XMLSchema-instance" xmlns:xsd="http:\/\/www.w3.org\/2001\/XMLSchema" xmlns:tns="http:\/\/www.kiim.info\/soap\/">';
  xml += '<soap:Header>';
  xml += '<tns:KiimSoapCredentials>';
  xml += KiimSoapMessage.doStructXML(this.params[0]);
  xml += '<\/tns:KiimSoapCredentials>';
  xml += '<\/soap:Header>';
  xml += '<soap:Body>';
  xml += '<tns:'+method+'>';
  xml += KiimSoapMessage.doStructXML(this.params[1]);
  xml += '<\/tns:'+method+'>';
  xml += '<\/soap:Body>';
  xml += '<\/soap:Envelope>';
  return xml;
};
