  /*
   File:             csgeneral.js
   Version:          1.0
   Last updated:     20-11-2003
   Last updated by:  NE
   Copyright:        © 2003 C-CONTENT b.v. 
   Description:      Module containing general functionality
   Required modules: -
   Output:           -
  */

  /*  stringReplace
  *  replaces all occurences of strOrg by strNew in aString
  *  returns 'new' string 
  */
  
  function ModuleCsGeneralStringReplace(aString, strOrg, strNew)
  {
    retString = new String('');
    
    if (strOrg.length > 0)
    {
      var index = aString.indexOf(strOrg);
      while(index>-1)
      {
        var begin = aString.substr(0, index);
        aString = aString.substr(index + strOrg.length, aString.length - index + strOrg.length);
        retString += begin + strNew;
        
        index = aString.indexOf(strOrg);
      }
    }
    retString += aString;
    
    return retString;
  }
  
  function ModuleCsGeneralAllnumbers(inp)
  {
    for (var i = 0; i < inp.length; i++)
      if (inp.charAt(i) >= '0' && inp.charAt(i) <= '9')
        ; //cc:skip
      else
        return false;
    return true;
  }

  function ModuleCsGeneralAddDelimiters(input, delimiter, sz)
  /*cc:adds leading delimters until size sz is reached*/
  {
    var prefix = new String();
    while ((input.length + prefix.length) < sz)
    {
      prefix += delimiter;
    }
    return (prefix + input); 
  }

  function GenerateEscapedCharCode(num)
  {
  	base = num / 16;
  	rem = num % 16;
  	base = base - (rem / 16);
  	baseS = MakeHex(base);
  	remS = MakeHex(rem);
  	return '%' + baseS + remS;
  }
  
  function MakeHex(x) 
  {
  	if((x >= 0) && (x <= 9))
  		return x;
  	else 
  	{
  		switch(x) 
  		{
  			case 10: return "A"; 
  			case 11: return "B";  
  			case 12: return "C";  
  			case 13: return "D";  
  			case 14: return "E";  
  			case 15: return "F";
  		}
  	}  
  }
  
  function ctEscape(aInput)
  {
  	var output = new String();
  
    if (aInput)
    { 
      // check if aInput is integer
      if (typeof aInput.length == "undefined")
        var input = "" + aInput;
      else
        var input = aInput;
    }
  
  	if (input)
  	  var len = input.length;
  	else
  	  var len = 0;
  	  
  	for (var i = 0; i < len; ++i)
  	{
  		switch (input.charCodeAt(i))
  		{
  			case 32 :
  			case 34 :
  			case 35 :
  			case 37 :
  			case 38 :
  			case 43 :
  			case 47 :
  			case 58 :
  			case 59 :
  			case 60 :
  			case 61 :
  			case 62 :
  			case 63 :
  			case 64 :
  			case 91 :
  			case 92 :
  			case 93 :
  			case 94 :
  			case 96 :
  			case 123:
  			case 124:
  			case 125:
  			case 126:
  			{
  				output += GenerateEscapedCharCode(input.charCodeAt(i));
  				break;
  			}
  			default :
  			{
  				var codeI = input.charCodeAt(i);
  				if ((0 <= codeI && codeI <=31) || (128 <= codeI && codeI <= 255))
  					output += GenerateEscapedCharCode(codeI);
  				else if (codeI>255)
  				  output += escape(input.charAt(i));
  				else
  					output += input.charAt(i);
  			}
  		}
  	}
  	return output;
  } 

  function ModuleCsGeneral_CcEscape(str)
  {
    return (ctEscape(str));
  }

  function ModuleCsGeneral_CcUnescape(str)
  {
    return (unescape(str));
  }
 
  /* csgeneral.ccEncodeURIComponent
  */
  function ModuleCsGeneralCcEncodeURIComponent(str)
  {
    return this._ccEscape(str);
  }

  /* csgeneral.ccDecodeURIComponent
  */
  function ModuleCsGeneralCcDecodeURIComponent(str)
  {
    return this._ccUnescape(str);
  }
  

  /* csgeneral.createButton(..) - Returns HTML code for a standard text or image 
       button.
  */
  function ModuleCsGeneralCreateButton(sButtonText, sButtonImg, sButtonAction, iType)
  {
    return(this.createButton2("", sButtonText, sButtonImg, sButtonAction, iType));
  }
  
  function ModuleCsGeneralCreateButton2(sBtnId, sButtonText, sButtonImg, sButtonAction, iType)
  {
    var sBtn, sBtnCap, sBtnAct;
    if (sButtonImg && (sButtonImg != ''))
    {
      sBtnCap = "<img src=\""+sButtonImg+"\" alt=\""+sButtonText+"\" title=\""+sButtonText+"\" border=\"no\"/>";
    }
    else
      sBtnCap = sButtonText;
    if (sButtonAction)
    {
      var i = sButtonAction.indexOf('(');
      if (i > -1)
        sBtnAct = sButtonAction.substring(0,i);
      else
        sBtnAct = sButtonAction;
      sBtnAct = "if(window."+sBtnAct+"){return(window."+sButtonAction+");}else{alert('function \\\'"+sBtnAct+"\\\' is not yet implemented.');return(false);}";
      
    }
    else
    {
      sBtnAct = "alert('no function defined');return(false);";
    }
    
    if ((typeof iType == "undefined") || ((typeof iType == "boolean") && iType)) //on
    {
      sBtn =  "<span class=\"button\" id=\""+sBtnId+"\"><div><a href=\"#\" onclick=\""+sBtnAct+"\">";
      sBtn += sBtnCap;
      sBtn += "</a></div></span>";
    }
    else //off
    {
      sBtn = "<span class=\"button disabled\" id=\""+sBtnId+"\"><div><a href=\"#\" onclick=\"return(false);\">";
      sBtn += sBtnCap;
      sBtn += "</a></div></span>";
    }
    return(sBtn);
  }

  /* csgeneral.enableButton - enables or disables the button specified  */
  function ModuleCsGeneralEnableButton(oWindow, sBtnId, bEnable)
  {
    if (oWindow)
    {
      oBtnSpan = oWindow.document.getElementById(sBtnId);
      if (oBtnSpan)
      {
        var sClass = oBtnSpan.className.replace(/ disabled/,"");
        if (!bEnable)
          sClass += " disabled";
        oBtnSpan.className = sClass;
        //form is <span><div><a>..</a></div></span>
        oBtnA = oBtnSpan.childNodes[0].childNodes[0];
        //alert(oBtnA.onclick);
      }
    }
  }
  
  /* csgeneral.joinPaths - joins the destination path to the source path
       relative paths are taken into account. */
  function ModuleCsGeneralJoinPaths(sSrc, sDst)
  {
    var sPath = '';
    if (sSrc != '')
    {
      //regular expression to find absolute path, find ('http://', 'ftp://', 
      //'file://', '\\' (UNC paths), '/' (unix style path), 'A:\' (dos style path)
      var reAbsPath = /(^file:\/\/|^http:\/\/|^ftp:\/\/|^\\\\|^\/|^[A-Z]:\\)(.+)/i
      //if sDst (destination) is absolute do nothing.
      if (reAbsPath.test(sDst))
        return(sDst)
        
      var sProt = '';
      //dived path in protocol (file://, \\, a:\, etc) and rest of path
      var a = reAbsPath.exec(sSrc)
      if (a && a.length > 2)
      {
        sSrc = a[2];
        sProt = a[1];
      }
      
      //if it's only an anchor add it to sSrc
      if (sDst.charAt(0) == '#')
        sPath = sSrc+sDst;
      else
      {
        //make all backward slashes forward slashes (unix style)
        sSrc = sSrc.replace(/\\/g,'/');
        sDst = sDst.replace(/\\/g,'/');

        //remove file name if present
        var iLastSlash = sSrc.lastIndexOf('/');
        var iLastDot = sSrc.lastIndexOf('.');
        if (iLastDot > iLastSlash)
          sSrc = sSrc.substring(0,iLastSlash);

        //join source and destination
        sSrc = sSrc+'/'+sDst;
        aSrc = sSrc.split('/');
        aDst = new Array();
        //now remove '.' and '..' items
        for (var i = 0;i<aSrc.length;i++)
        {
          if ((aSrc[i] == '..') && (aDst.length > 0))
            aDst.pop()
          else if ((aSrc[i] != '.') && (aSrc[i] != ''))
            aDst = aDst.concat(new Array(aSrc[i]))
        }
        sPath = aDst.join('/');
      }
    }
    //if sSrc was UNC or DOS style, make all slashes backward slashes
    if ((sProt == '\\\\') || (/^[A-Z]:\\/.test(sProt)))
      sPath = sPath.replace(/\//g, '\\');
    return(sProt+sPath);
  }  

  /* csgeneral.openDialog - Opens new window */
  function ModuleCsGeneralOpenDialog(url, wndname, height, width, options)
  {
    if(options && options != 'undefined')
      wnd = window.open("", wndname,"height="+height+",width="+width+","+options);
    else
      wnd = window.open("", wndname,"height="+height+",width="+width+",scrollbars=yes,resizable=yes");
    wnd.location = url.toString();
    
    return(wnd);
  }
  
  /* csgeneral.toArray - converts a string containing name/value pairs to 
       a named array. The value part of a name/value pair is decoded! */
  function ModuleCsGeneralToArray(s)
  {
    ain = s.split("&");
    aout = Array();
    for (var i = 0; i < ain.length; i++)
    {
      anm = ain[i].split("=",2);
      if (anm.length == 2)
        aout[anm[0]] = decodeURIComponent(anm[1]);
    }
    return(aout);
  }
  
  function ModuleCsGeneralInterrogate(what) 
  {
    var output = '';
    for (var i in what)
      output += i + ' ';
//    alert(output);
  }
  
  function ModuleCsGeneralArrayContainsString(oArray, sString)
  {
    var bFound = false;
    var i      = 0;
    
    if (oArray && sString)
    {
      while ((i<oArray.length) && !bFound)
      {
        bFound = (oArray[i] == sString);
        ++i;
      }
    }
    
    return bFound;
  }
  
  /* csgeneral - implements the module CsGeneral */
  function ModuleCsGeneral()
  {
    // private    
    this._ccEscape            = ModuleCsGeneral_CcEscape;
    this._ccUnescape          = ModuleCsGeneral_CcUnescape;
    
    this.createButton = ModuleCsGeneralCreateButton;
    this.createButton2 = ModuleCsGeneralCreateButton2;
    this.enableButton = ModuleCsGeneralEnableButton;
    this.joinPaths = ModuleCsGeneralJoinPaths;
    this.openDialog = ModuleCsGeneralOpenDialog;
    this.toArray = ModuleCsGeneralToArray;
    this.interrogate = ModuleCsGeneralInterrogate;

    this.stringReplace = ModuleCsGeneralStringReplace;
    this.allnumbers = ModuleCsGeneralAllnumbers;
    this.addDelimiters = ModuleCsGeneralAddDelimiters;
    this.ccEncodeURIComponent = ModuleCsGeneralCcEncodeURIComponent;
    this.ccDecodeURIComponent = ModuleCsGeneralCcDecodeURIComponent;
    
    this.arrayContainsString = ModuleCsGeneralArrayContainsString;
  }

  if (!csgeneral)
    var csgeneral = new ModuleCsGeneral();

