/*****************************************************************************
 *
 *                   INDIGEN SOLUTIONS CODE PROPERTY
 *       The present javascript code is property of Indigen Solutions. This 
 *     code can only be used inside Internet/Intranet web sites located on 
 *  *web servers*, as the outcome of a licensed Indigen Solutions application 
 *  only. Any unauthorized use, reverse-engineering, alteration, transmission, 
 * transformation, facsimile, or copying of any means (electronic or not) is 
 *     strictly prohibited and will be prosecuted. Removal of the present 
 *              copyright notice is strictly prohibited
 *         Copyright (c) 2004 Indigen Solutions. All Rights Reserved.
 *
 * RCS Id                       $Id: util.js,v 1.19 2006/08/01 15:32:18 indigen Exp $
 * RCS Revision                 $Revision: 1.19 $
 * RCS Check in date            $Date: 2006/08/01 15:32:18 $
 * 
 ******************************************************************************/

/* Constants. */

var OK_CANCEL          = 0;
var YES_NO_CANCEL      = 1;
var OK_ONLY            = 2;
var YES_NO             = 3;
var ABORT_RETRY_IGNORE = 4;
var RETRY_CANCEL       = 5;

var CRITICAL           = 0;
var QUESTION           = 1;
var EXCLAMATION        = 2;
var INFORMATION        = 3;

var OK_BUTTON          = 1;
var CANCEL_BUTTON      = 2;
var ABORT_BUTTON       = 3;
var RETRY_BUTTON       = 4;
var IGNORE_BUTTON      = 5;
var YES_BUTTON         = 6;
var NO_BUTTON          = 7;

/* Globals. */

var MY_DICO = {};
var MY_OBJECT_REFERENCES = [];

/* Functions. */

function managePNG() {
  if ( !util_isIE() )
    return;
   for(var i=0; i<document.images.length; i++)
      {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	     {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
	     }
      }
}

function util_isIE() {
  return ( navigator.userAgent.toLowerCase().indexOf('msie') != -1 );
}

function messageBox(message, title, type, icon, handler){
  // We get VBType.
  var VBType = 0;
  switch ( type ) {
    case OK_CANCEL          : vbscript:getVBOkCancel(); VBType = VBResult; break;
    case YES_NO_CANCEL      : vbscript:getVBYesNoCancel(); VBType = VBResult; break;
    case OK_ONLY            : vbscript:getVBOkOnly(); VBType = VBResult; break;
    case YES_NO             : vbscript:getVBYesNo(); VBType = VBResult; break;
    case ABORT_RETRY_IGNORE : vbscript:getVBAbortRetryIgnore(); VBType = VBResult; break;
    case RETRY_CANCEL       : vbscript:getVBRetryCancel(); VBType = VBResult; break;
  }
  // We get icon.
  var VBIcon = 0;
  switch ( icon ) {
    case CRITICAL    : vbscript:getVBCritical(); VBIcon = VBResult; break;
    case QUESTION    : vbscript:getVBQuestion(); VBIcon = VBResult; break;
    case EXCLAMATION : vbscript:getVBExclamation(); VBIcon = VBResult; break;
    case INFORMATION : vbscript:getVBInformation(); VBIcon = VBResult; break;
  }
  // We display the message.
  a = message;
  b = VBType | VBIcon;
  c = title;
  vbscript:VBMessageBox();
  handler(VBResult);
}

function serializeForPHP(value) {
  switch ( typeof value ) {
    case "array"  :
    case "object" :
      if ( value == null )
	return "N;";
      var tmp = "";
      var nb = 0;
      for ( var key in value ) {
	tmp += serializeForPHP(key) + serializeForPHP(value[key]);
	nb++;
      }
      return "a:" + nb + ":{" + tmp + "}";
    case "string" :
      return "s:" + value.length + ":\"" + value + "\";";
    case "number" :
      return "i:" + value + ";";
    case "boolean" :
      return "b:" + (value ? 1 : 0) + ";";
  }
  return "N;";
}

function replaceAll(baseStr, oldStr, newStr) {
  var str="";
  var idx=baseStr.indexOf(oldStr);
  while(idx>=0) {
    str=str+baseStr.substring(0,idx);
    str=str+newStr;
    baseStr=baseStr.substring(idx+oldStr.length);
    idx=baseStr.indexOf(oldStr);
  }
  if(baseStr.length>0)
    str=str+baseStr;
  return str;
}

function util_serialize(mixed) {
  if ( mixed == null ) {
    return "null";
  } else if ( mixed instanceof Array ) {
    return util_serializeArray(mixed);
  } else if ( mixed instanceof Object ) {
    return util_serializeObject(mixed);
  } else {
    switch ( typeof mixed ) {
      case "string" :
	return util_serializeString(mixed);
      case "boolean" :
	return util_serializeBoolean(mixed);
      case "number" :
	return util_serializeNumber(mixed);
      default :
	return "";
    }
  }
}

function util_serializeArray(array) {
  var str = "[";
  var first = true;
  for( var i in array ) {
    if ( first )
      first = false;
    else
      str += ",";
    str += util_serialize(array[i]);
  }
  return str + "]";
}

function util_serializeObject(object) {
  var str = "{";
  var first = true;
  for ( var i in object ) {
    if ( first )
      first = false;
    else
      str += ",";
    str += i + ":" + util_serialize(object[i]);
  }
  return str + "}";
}

function util_serializeString(string) {
  //string = replaceAll(string, "\\", "\\\\");
  string = replaceAll(string, "\b", "\\b"); 
  string = replaceAll(string, "\t", "\\t"); 
  string = replaceAll(string, "\n", "\\n"); 
  string = replaceAll(string, "\f", "\\f"); 
  string = replaceAll(string, "\r", "\\r"); 
  //string = replaceAll(string, "\'", "\\\'"); 
  string = replaceAll(string, "\"", "\\\""); 
  return "\"" + string + "\"";
}

function util_serializeBoolean(bool) {
  return ( ( !bool ) ? "false" : "true" ); 
}

function util_serializeNumber(number) {
  return "" + number;
}

function util_implode(sep, arr) {
  var str = "";
  var first = true;
  for ( var i in arr )
    if ( first ) {
      str = "" + arr[i];
      first = false;
    } else
      str += sep + arr[i];
  return str;
}

function util_keys(map) {
  var keys = [];
  for ( var i in map )
    keys.push(i);
  return keys;
}

function util_attachEventHandlerToElement(element, event, handler) {
  if ( document.addEventListener )
    element.addEventListener(event, handler, false);
  else if ( document.attachEvent )
    element.attachEvent("on" + event, handler);
}

function util_getStrRef(obj) {
  if ( obj.__id_to_str_ref__ == null ) {
    obj.__id_to_str_ref__ = MY_OBJECT_REFERENCES.length;
    MY_OBJECT_REFERENCES.push(obj);
  }
  return "MY_OBJECT_REFERENCES[" + obj.__id_to_str_ref__ + "]";
}

function util_isIE() {
  return ( document.all != null );
}

function util_isMoz() {
  return ( document.all == null );
}

function getClassname(object) {
  var constructor = "" + object.constructor;
  var re = /function[ \t\n]+([^\)]+)\(/;
  var array = constructor.match(re);
  return array[1];
}

// This class is used for getting some variables from server.
function Variables(element) {
  // Creation of an iframe to get a set of variables from server.
  var iframe = document.createElement("iframe");
  iframe.style.display = "none";
  if ( element == null )
    document.body.appendChild(iframe);
  else
    element.appendChild(iframe);
  // Get the variables from an url.
  // handler is function pointer which takes a parameter.
  // This function is called when the variables has been loaded.
  // The parameter of function is a hashmap with the variables.
  this.get = function(url, handler) {
    iframe.src=url;
    iframe.onload=function() {
      var variables = new Array();
      var body = iframe.contentWindow.document.body;
      for ( var node = body.firstChild; node != null; node = node.nextSibling ) {
	if ( node.nodeType == 8 ) {
	  // Content is coded : \bname\nvalue\n
	  var content = node.nodeValue;
	  var i = 1;
	  for ( ; content.charAt(i) != '\n'; i++ );
	  var name = content.substring(1, i);
	  var value = content.substring(i + 1, content.length - 1);
	  value = value.replace("</*!--*/", "<!--");
	  value = value.replace("/*--*/>", "-->");
	  variables[name] = value;
	}
      }
      if ( typeof handler == "string" )
        eval(handler + "(variables)");
      else
        handler(variables);
    };
  };
}

function getFlashHTML(url, name, params) {
  if (params = null)
    params = {};
  var html = '';
  html += '<object name="' + name  + '" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,42,0" >';
  for (var i in params)
    html += '<param name="' + i  + '" value="' + params[name] + '">';
  html += '<param name="quality" value="high">';
  html += '<param name="allowscriptaccess" value="samedomain">';
  html += '<param name="movie" value="' + url + '">';
  html += '<embed type="application/x-shockwave-flash" ';
  html += 'name="' + name + '" ';
  html += 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
  html += 'swLiveConnect="true" ';
  for (var i in params)
    html += ' ' + i + '="' + params[name] + '" ';
  html += 'src="' + url + '" quality="high" ';
  html += 'allowscriptaccess="samedomain" ';
  html += 'movie="' + url + '" ></embed>';
  html += '</object> ';
  return html;
}

function Includer() {
  // Include a javascript.
  this.includeJS = function(js) {
    var scripts = document.getElementsByTagName("script");
    for ( var i = 0; i < scripts.length; i++ ) {
      var script = scripts[i];
      if ( script.src == js ) {
	alert("script " + js + " found");
	return;
      }
    }
    var script = document.createElement("script");
    script.src = js;
    document.body.appendChild(script);
  };
}

// Allow to get a element from its id and from a root element.
function getElementById(elem, id) {
  if ( elem.nodeType != 1 /*Node.ELEMENT_NODE*/ ) {
    return null;
  }
  if ( elem.getAttribute("id") == id )
    return elem;
  var node = elem.firstChild;
  while ( node != null ) {
    var node0 = this.getElementById(node, id);
    if ( node0 != null )
      return node0;
    node = node.nextSibling;
  }
  return null;
}

function mapDOMElementsRecursively(elem, handler) {
  if ( elem.nodeType != 1 /*Node.ELEMENT_NODE*/ )
    return;
  for ( var child = elem.firstChild; child; child = child.nextSibling )
    mapDOMElementsRecursively(child, handler);
  handler(elem);
}

function vsprintf(string, args) {
  if (args == "")
    return sprintf(string);
  else
    return eval("sprintf(\"" + string + "\", " + args + ");");
}

function sprintf() {
  if (!arguments || arguments.length < 1 || !RegExp) {
    return "";
  }
  var str = arguments[0];
  var re = new RegExp("([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)");
  var a = b = [], numSubstitutions = 0, numMatches = 0;
  while (a = re.exec(str)) {
    var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
    var pPrecision = a[5], pType = a[6], rightPart = a[7];      
    numMatches++;
    if (pType == '%') {
      subst = '%';
    } else {
      numSubstitutions++;
      if (numSubstitutions >= arguments.length) {
	alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
      }
      var param = arguments[numSubstitutions];
      var pad = '';
      if (pPad && pPad.substr(0,1) == "'") 
	pad = leftpart.substr(1,1);
      else if (pPad) 
	pad = pPad;
      var justifyRight = true;
      if (pJustify && pJustify === "-") 
	justifyRight = false;
      var minLength = -1;
      if (pMinLength) 
	minLength = parseInt(pMinLength);
      var precision = -1;
      if (pPrecision && pType == 'f') 
	precision = parseInt(pPrecision.substring(1));
      var subst = param;
      if (pType == 'b') 
	subst = parseInt(param).toString(2);
      else if (pType == 'c') 
	subst = String.fromCharCode(parseInt(param));
      else if (pType == 'd') 
	subst = parseInt(param) ? parseInt(param) : 0;
      else if (pType == 'u') 
	subst = Math.abs(param);
      else if (pType == 'f') 
	subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
      else if (pType == 'o') 
	subst = parseInt(param).toString(8);
      else if (pType == 's') 
	subst = param;
      else if (pType == 'x') 
	subst = ('' + parseInt(param).toString(16)).toLowerCase();
      else if (pType == 'X') 
	subst = ('' + parseInt(param).toString(16)).toUpperCase();
    }
    str = leftpart + subst + rightPart;
  }
  return str;
}

function alertException(exception, rethrow) {
  var str = "";
  for ( var i in exception ) {
    str += i + ": " + exception[i] + "\n";
  }
  alert(str);
  if ( rethrow == null )
    throw exception;
}

function basename(uri) {
  if (uri == null)
    return "";
  if (uri.indexOf("/") != -1) {
    var split = uri.split("/");
    return split[split.length - 1];
  }
  if (uri.indexOf("\\") != -1) {
    var split = uri.split("\\");
    return split[split.length - 1];
  }
  return "";
}

function locale_get() {
  if (!arguments || arguments.length < 1) {
    return;
  }
  var args = new Array();
  for (var i = 0; i < arguments.length; i++)
      args[i] = arguments[i];
  var code = args.shift();
  if (!MY_DICO[code])
    return code;
  var string = MY_DICO[code];
  return vsprintf(string, args); 
}

function locale_vget(code, args) {
  var string = MY_DICO[code];
  return vsprintf(string, args); 
}

function setJavascript() {
  var sender = new Sender(MAIN_URL, alert);
  sender.addParam("m", 6);
  sender.addParam("x", "javascript");
  sender.send();
}

function util_cleanURLSlashes(url) {
  var split = url.split("http://");
  var http = "";
  if (split[1]) {
    url = split[1];
    http = "http://";
  } else {
    split = url.split("https://");    
    if (split[1]) {
      url = split[1];
      http = "https://";
    }
  }
  var regexp1 = new RegExp("\\\\+", "g");
  url = url.replace(regexp1, "/");
  var regexp2 = new RegExp("//+", "g");
  url = url.replace(regexp2, "/");
  return http + url;
}

var s = "";
s = "<s" + "c" + "r" + "i" + "p" + "t" + " language=\"VBScript\">\n";
s += "dim a, b, c\n";
s += "dim VBResult\n";
s += "Function getVBOkCancel()\n";
s += "VBResult = vbOkCancel\n";
s += "End Function\n";
s += "\n";
s += "Function getVBOkOnly()\n";
s += "VBResult = vbOkOnly\n";
s += "End Function\n";
s += "\n";
s += "Function getVBAbortRetryIgnore()\n";
s += "VBResult = vbAbortRetryIgnore\n";
s += "End Function\n";
s += "\n";
s += "Function getVBRetryCancel()\n";
s += "VBResult = vbRetryCancel\n";
s += "End Function\n";
s += "\n";
s += "Function getVBYesNo()\n";
s += "VBResult = vbYesNo\n";
s += "End Function\n";
s += "\n";
s += "Function getVBYesNoCancel()\n";
s += "VBResult = vbYesNoCancel\n";
s += "End Function\n";
s += "\n";
s += "Function getVBCritical()\n";
s += "VBResult = vbCritical\n";
s += "End Function\n";
s += "\n";
s += "Function getVBQuestion()\n";
s += "VBResult = vbQuestion\n";
s += "End Function\n";
s += "\n";
s += "Function getVBExclamation()\n";
s += "VBResult = vbExclamation\n";
s += "End Function\n";
s += "\n";
s += "Function getVBInformation()\n";
s += "VBResult = vbInformation\n";
s += "End Function\n";
s += "\n";
s += "Function VBMessageBox()\n";
s += "VBResult = MsgBox(a, b, c)\n";
s += "End Function\n";
s += "</s" + "c" + "r" + "i" + "p" + "t>\n";

document.write(s);

