var VDW_FADE_IN = 0;
var VDW_FADE_OUT = 1;
/* 
   created by : Declercq Carl
   date : 23/06/2005
   purpose : convert a date object to some formating string see documentation of www.php.net
   remarks : the function is not terminated!! todo ;-)
*/
function VDW_ConvertDate(dt,convension)
{
   var len = convension.length; 
   var dum = "";
   var result = "";
   
   if(!dt)
   {
      dt = new Date();
   }
      
   
   for(i=0;i<len;i++)
   {
      switch(convension.charAt(i))
      {
         case "d":// Day of the month, 2 digits with leading zeros
            dum = dt.getDate();
            result+=((dum<10)?"0":"")+dum;         
            break;
         case "D":// A textual representation of a day, three letters
            break;
         case "j":// Day of the month without leading zeros
            result+=dum;
            break;
         case "l":// A full textual representation of the day of the week
            break;
         case "S":// English ordinal suffix for the day of the month, 2 characters
            break;
         case "w":// Numeric representation of the day of the week 0 (for Sunday)
            break;
         case "z":// The day of the year (starting from 0)
            break;
         case "W":// ISO-8601 week number of year, weeks starting on Monday
            break;
         case "F":// A full textual representation of a month, such as January or March
            break;
         case "m":// Numeric representation of a month, with leading zeros
            dum = dt.getMonth()+1;
            result+=((dum<10)?"0":"")+dum;
            break;
         case "M":// A short textual representation of a month, three letters
            break;
         case "n":// Numeric representation of a month, without leading zeros
            dum = dt.getMonth()+1;
            result+=dum;
            break;
         case "t":// Number of days in the given month
            break;
         case "L":// Whether it's a leap year
            break;
         case "Y":// A full numeric representation of a year, 4 digits
            result+=dt.getFullYear();
            break;
         case "y":// A two digit representation of a year
            dum = dt.getFullYear();
            result+=dum.substring(2,4);
            break;
         case "a":// Lowercase Ante meridiem and Post meridiem
            break;
         case "A":// Uppercase Ante meridiem and Post meridiem
            break;
         case "B":// Swatch Internet time
            break;
         case "g":// 12-hour format of an hour without leading zeros
            dum = dt.getHours();
            if(dum>12)
               dum=dum-12;
            result+=dum;
            break;
         case "G":// 24-hour format of an hour without leading zeros
            result+=dt.getHours();
            break;
         case "h":// 12-hour format of an hour with leading zeros
            dum = dt.getHours();
            if(dum>12)
               dum=dum-12;
            result+=((dum<10)?"0":"")+dum;
            break;
         case "H":// 24-hour format of an hour with leading zeros
            dum = dt.getHours();
            result+=((dum<10)?"0":"")+dum;
            break;
         case "i":// Minutes with leading zeros
            dum = dt.getMinutes();
            result+=((dum<10)?"0":"")+dum;
            break;
         case "s":// Seconds, with leading zeros
            dum = dt.getSeconds();
            result+=((dum<10)?"0":"")+dum;            
            break;
         case "I":// Whether or not the date is in daylights savings time
            break;
         case "O":// Difference to Greenwich time (GMT) in hours
            break;
         case "T":// Timezone setting of this browser
            break;
         case "Z":// Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
            break;
         case "c":// ISO 8601 date (added in PHP 5)
            break;
         case "r":// RFC 2822 formatted date
            break;
         case "U":// Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
            break;
         default:
            result+=convension.charAt(i);
            break;
      }
   }
   return result ;
}

function ConvertDateConvention(convension,type)
{
   var result="";
   var len = convension.length;
   
   for(i=0;i<len;i++)
   {
      switch(convension.charAt(i))
      {
         case "d":// Day of the month, 2 digits with leading zeros
            if(type==1)
            {
               result += "%d";
            }
            else
               result += "dd";         
            break;
         case "D":// A textual representation of a day, three letters
            break;
         case "j":// Day of the month without leading zeros
            if(type==1)
            {
               result += "%e";
            }
            else
               result += "d";
            break;
         case "l":// A full textual representation of the day of the week
            break;
         case "S":// English ordinal suffix for the day of the month, 2 characters
            if(type==1)
            {
               result += "%D";
            }
            break;
         case "w":// Numeric representation of the day of the week 0 (for Sunday)
            if(type==1)
            {
               result += "%w";
            }
            break;
         case "z":// The day of the year (starting from 0)
            if(type==1)
            {
               result += "%j";
            }
            break;
         case "W":// ISO-8601 week number of year, weeks starting on Monday
            if(type==1)
            {
               result += "%u";
            }            
            break;
         case "F":// A full textual representation of a month, such as January or March
            if(type==1)
            {
               result += "%M";
            }
            break;
         case "m":// Numeric representation of a month, with leading zeros
            if(type==1)
            {
               result += "%m";
            }
            else
               result += "mm";
            break;
         case "M":// A short textual representation of a month, three letters
            if(type==1)
            {
               result += "%b";
            }
            break;
         case "n":// Numeric representation of a month, without leading zeros
            if(type==1)
            {
               result += "%c";
            }
            else            
               result += "m";
            break;
         case "t":// Number of days in the given month
            break;
         case "L":// Whether it's a leap year
            break;
         case "Y":// A full numeric representation of a year, 4 digits
            if(type==1)
            {
               result += "%Y";
            }
            else         
               result += "yyyy";
            break;
         case "y":// A two digit representation of a year
            if(type==1)
            {
               result += "%y";
            }
            else                     
               result += "yy";
            break;
         case "a":// Lowercase Ante meridiem and Post meridiem
            break;
         case "A":// Uppercase Ante meridiem and Post meridiem
            break;
         case "B":// Swatch Internet time
            break;
         case "g":// 12-hour format of an hour without leading zeros
            if(type==1)
            {
               result += "%l";
            }
            else
               result += "h";
            break;
         case "G":// 24-hour format of an hour without leading zeros
            if(type==1)
            {
               result += "%k";
            }
            else            
               result += "h";            
            break;
         case "h":// 12-hour format of an hour with leading zeros
            if(type==1)
            {
               result += "%h";
            }
            else
               result += "hh";            
            break;
         case "H":// 24-hour format of an hour with leading zeros
            if(type==1)
            {
               result += "%H";
            }
            else            
               result += "hh";
            break;
         case "i":// Minutes with leading zeros
            if(type==1)
            {
               result += "%i";
            }
            else            
               result += "ii";
            break;
         case "s":// Seconds, with leading zeros
            if(type==1)
            {
               result += "%s";
            }
            else            
               result += "ss";
            break;
         case "I":// Whether or not the date is in daylights savings time
            break;
         case "O":// Difference to Greenwich time (GMT) in hours
            break;
         case "T":// Timezone setting of this browser
            break;
         case "Z":// Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
            break;
         case "c":// ISO 8601 date (added in PHP 5)
            break;
         case "r":// RFC 2822 formatted date
            break;
         case "U":// Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
            break;
         default:
            result += convension.charAt(i);
            break;
      }
   }
   
   return result;
}

function vdw_fade(direction,id,step,maxstep)
{
   var obj = document.getElementById(id);
   if(!obj)
      return;

   var i=0;
   if(obj.getAttribute('opacityindex'))
   {
      if(direction==VDW_FADE_IN)
         i = parseInt(obj.getAttribute('opacityindex'))+step;
      else
         i = parseInt(obj.getAttribute('opacityindex'))-step;
   }
   
   if(direction==VDW_FADE_IN)
   {
      if(i>maxstep)
         i=maxstep;
   }
   else
   {
      if(i<maxstep)
         i=maxstep;
   }
   
   if(i>99) /* firefox bug */
      i=99;
   
   if(i<0)
      i=0;
   
   obj.style.MozOpacity=i/100;
   obj.setAttribute('opacityindex',i);
   
   if(i>0)
   {
      obj.style.visibility = 'visible';
   }
   
   if(i==0)
      obj.style.visibility = 'hidden';
   
   if(((i>maxstep&&direction!=VDW_FADE_IN) || (i<maxstep&&direction==VDW_FADE_IN)) && (i<99 && i>0))
   {
      setTimeout('vdw_fade('+direction+',"'+id+'",'+step+','+maxstep+')',40);
   }
}

function vdw_debug(id,str,x,y)
{
   if(!document.getElementById(id))
   {
      var oDiv = VDW_CreateElement("div",id,"");
      oDiv.style.backgroundColor='#EFEFEF';
      oDiv.style.border='1px solid #C0C0C0';
      oDiv.style.zIndex='1000';
      oDiv.style.display = 'block';
      document.body.appendChild(oDiv);
   }
   if(x || y)
   {
      var obj = document.getElementById(id);
      obj.style.position = "absolute";
      obj.style.top = (y)?y:0;
      obj.style.left = (x)?x:0;      
   }
   document.getElementById(id).innerText=str;
}

function vdw_convert_RGBToHex(col) {
	var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");

	var rgb = col.replace(re, "$1,$2,$3").split(',');
	if (rgb.length == 3) 
   {
		r = parseInt(rgb[0]).toString(16);
		g = parseInt(rgb[1]).toString(16);
		b = parseInt(rgb[2]).toString(16);

		r = r.length == 1 ? '0' + r : r;
		g = g.length == 1 ? '0' + g : g;
		b = b.length == 1 ? '0' + b : b;

		return "#" + r + g + b;
	}

	return col;
}

function vdw_convert_HexToRGB(col) {
	if (col.indexOf('#') != -1) 
   {
		col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');

		r = parseInt(col.substring(0, 2), 16);
		g = parseInt(col.substring(2, 4), 16);
		b = parseInt(col.substring(4, 6), 16);

		return "rgb(" + r + "," + g + "," + b + ")";
	}

	return col;
}

/* 
   function : CheckDate(date,convention)
   purpose :  check if correct date format and if date exist.
   return  :  must be 0 (1 = invalid convention format, 2 = invalid date format, 3 = invalid date)
*/
function CheckDate(date,convention)
{
   
   var error = 0;
   var Y = "((19|20)\\d\\d){1,4}";
   var m = "(0[1-9]|1[012]){1,2}";
   var d = "(0[1-9]|[12][0-9]|3[01]){1,2}";
   var sep = "[-\\s/\.:]";
   
   if(!convention || convention=="")
   {
      return error=1;
   }

   if(!date || date=="")
   {
      return error = 2;
   }

   var pos_d = convention.indexOf('d');
   var pos_m = convention.indexOf('m');
   var pos_y = convention.indexOf('Y');
   
   var day=0,month=0,year=0,convention="";

   //d-m-Y
   if(pos_d<pos_m && pos_d<pos_y && pos_m < pos_y)
   {
      day=1
      month=2;
      year=3;
      convention = "^"+d+sep+m+sep+Y+"$";
   }
   else
   
   //d-Y-m
   if(pos_d<pos_m && pos_d<pos_y && pos_y < pos_m)
   {
      day=1
      year=2;
      month=3;
      convention = "^"+d+sep+m+sep+Y+"$";
   }
   else

   //m-d-Y
   if(pos_m<pos_d && pos_m<pos_y && pos_d<pos_y)
   {
      month=1;
      day=2;
      year=3;
      convention = "^"+m+sep+d+sep+Y+"$";            
   }
   else

   //m-Y-d
   if(pos_m<pos_y && pos_m<pos_d && pos_y<pos_d)
   {
      month=1;
      year=2;
      day=3;
      convention = "^"+m+sep+Y+sep+d+"$";            
   }
   else

   //Y-d-m
   if(pos_y<pos_d && pos_y<pos_m && pos_d<pos_m)
   {
      year=1;
      day=2;
      month=3;
      convention = "^"+Y+sep+d+sep+m+"$";            
   }
   else
   //Y-m-d
   if(pos_y<pos_m && pos_y<pos_d && pos_m<pos_d)
   {
      year=1;
      month=2;
      day=3;
      convention = "^"+Y+sep+m+sep+d+"$";            
   }
   else
   {
      return error = 1;
   }
   
   var r_date = new RegExp(convention,"");
   var result = date.match(r_date);

   //'invlaid string'
   if(!result)
   {
      error = 2;
   }
   else //'31st of a month with 30 days'
   if(result[day]==31 && (result[month]==4 || result[month] == 6 || result[month]==9 || result[month]==11))
   {
      error = 3;
   }
   else //'February 30th or 31st'
   if(result[day] >= 30 && result[month] == 2)
   {
      error = 3;
   }
   else //'February 29th outside a leap year'
   if(result[month] == 2 && result[day] == 29 && !(result[year] % 4 == 0 && (result[year] % 100 != 0 || result[year] % 400 == 0)))
   {
      error = 3;
   }
   else //every thing ok
   {
      error = 0;
   }

   return error;
}

function ConvertStringDateToArray(str,convention)
{
   var error = 0;
   var array = new Array();
   var Y = "(19|20\\d\\d){1,4}";
   var m = "(0[1-9]|1[012]){1,2}";
   var d = "(0[1-9]|[12][0-9]|3[01]){1,2}";
   var h = "([01][0-9]|2[0-4]){1,2}";
   var g = "([0-5][0-9]){1,2}";
   var s = "([0-5][0-9]){1,2}";
   
   var sep = "[-\\s/\.:]";
   var fl_hours = true;
   var fl_date = true;   
   
   if(!convention || convention=="")
   {
      return null;
   }

   if(!str || str=="")
   {
      return null;
   }

   var pos_d = convention.indexOf('d');
   var pos_m = convention.indexOf('m');
   var pos_y = convention.indexOf('Y');
   
   if(pos_d<0 || pos_m<0 || pos_y<0)
   {
      fl_date = false;
   }

   var pos_h = convention.indexOf('h');
   var pos_g = convention.indexOf('g');
   var pos_s = convention.indexOf('s');
   
   if(pos_h<0 || pos_g<0 || pos_s<0)
   {
      fl_hours = false;
   }
   
   var day=0,month=0,year=0,hour=0,minutes=0,secondes=0;
   var rConvention="",rDateConvention="",rTimeConvention="";

   if(fl_date)
   {
      //d-m-Y
      if(pos_d<pos_m && pos_d<pos_y && pos_m < pos_y)
      {
         day=1
         month=2;
         year=3;
         rDateConvention = d+sep+m+sep+Y;
      }
      else
      
      //d-Y-m
      if(pos_d<pos_m && pos_d<pos_y && pos_y < pos_m)
      {
         day=1
         year=2;
         month=3;
         rDateConvention = d+sep+m+sep+Y;
      }
      else

      //m-d-Y
      if(pos_m<pos_d && pos_m<pos_y && pos_d<pos_y)
      {
         month=1;
         day=2;
         year=3;
         rDateConvention = m+sep+d+sep+Y;
      }
      else

      //m-Y-d
      if(pos_m<pos_y && pos_m<pos_d && pos_y<pos_d)
      {
         month=1;
         year=2;
         day=3;
         rDateConvention = m+sep+Y+sep+d;
      }
      else

      //Y-d-m
      if(pos_y<pos_d && pos_y<pos_m && pos_d<pos_m)
      {
         year=1;
         day=2;
         month=3;
         rDateConvention = Y+sep+d+sep+m;
      }
      else
      //Y-m-d
      if(pos_y<pos_m && pos_y<pos_d && pos_m<pos_d)
      {
         year=1;
         month=2;
         day=3;
         rDateConvention = Y+sep+m+sep+d;
      }
      else
      {
         return null;
      }
   }

   if(fl_hours)
   {
      //h:g:s
      if(pos_h<pos_g && pos_h<pos_s && pos_g<pos_s)
      {
         hour=1;
         minutes=2;
         seconds=3;
         rTimeConvention = h+sep+g+sep+s;            
      }
      else
      //h:s:g
      if(pos_h<pos_g && pos_h<pos_s && pos_g<pos_s)
      {
         hour=1;
         minutes=3;
         seconds=2;
         rTimeConvention = h+sep+s+sep+g;
      }
      else
      //g:h:s
      if(pos_g<pos_h && pos_g<pos_s && pos_h<pos_s)
      {
         hour=2;
         minutes=1;
         seconds=3;
         rTimeConvention = g+sep+h+sep+s;
      }
      else
      //g:s:h
      if(pos_g<pos_h && pos_g<pos_s && pos_s<pos_h)
      {
         hour=3;
         minutes=1;
         seconds=2;
         rTimeConvention = g+sep+s+sep+h;
      }
      else
      //s:h:g
      if(pos_s<pos_h && pos_s<pos_g && pos_h<pos_g)
      {
         hour=2;
         minutes=3;
         seconds=1;
         rTimeConvention = s+sep+h+sep+g;
      }
      else
      //s:g:h
      if(pos_s<pos_h && pos_s<pos_g && pos_g<pos_h)
      {
         hour=3;
         minutes=1;
         seconds=2;
         rTimeConvention = s+sep+g+sep+h;
      }
      else
      {
         return null;
      }
   }
   
   
   if(fl_date && fl_hours)
   {
      if(pos_h<pos_y)
      {
         year = year+3;
         date = date+3;
         month = month+3;         
         rConvention = "^"+(rTimeConvention+sep+rDateConvention)+"$";
      }
      else
      {
         hour = hour+3;
         minutes = minutes+3;
         seconds = seconds+3;         
         rConvention = "^"+(rDateConvention+sep+rTimeConvention)+"$";
      }
   }
   else
   if(fl_date)
   {
      rConvention = "^"+rDateConvention+"$";
   }
   else
   if(fl_hours)
   {
      rConvention = "^"+rTimeConvention+"$";
   }

   var r_date = new RegExp(rConvention,"");
   var result = str.match(r_date);
   
   if(!result)
   {
      return null;
   }
   
   if(fl_date)
   {
      array['year']  = parseInt(result[year],10);
      array['month'] = parseInt(result[month],10);
      array['day']   = parseInt(result[day],10);
   }
   else
   {
      array['year']  = 0;
      array['month'] = 0;
      array['day']   = 0;
   }
   
   if(fl_hours)
   {
      array['hour']     = parseInt(result[hour],10);
      array['minutes']  = parseInt(result[minutes],10);
      array['seconds']  = parseInt(result[seconds],10);
   }
   else
   {
      array['hour']     = 0;
      array['minutes']  = 0;
      array['seconds']  = 0;
   }

   return array;
}

function VDWInnerHTML(oObj,sHtml)
{
   //first of all add the html code to the object
   oObj.innerHTML = sHtml;

   //evaluate the javascript codes
   var reg1 = new RegExp("<script.*>.*</script>", "ig");
   var reg2 = new RegExp("<script.*>(.*)</script>", "i");

   var res  = sHtml.match(reg1);
   var res2 = null,i,len;
   if(res)
   {
      len  = res.length;
      for(i = 0 ; i<len ; i++)
      {
         res2 = res[i].match(reg2);
         if(!res2 || res2.length<=1)
            continue;
         eval(res2[1]);
      }
   }
}

function VDWGenerateName(oReq, user, convention, name, variables, sArgs, fResult, bSyncronous)
{
   var xmlstring = '<PAGE name="generate_name"><DECLARATIONS><VARIABLE type="integer"><NAME>error</NAME></VARIABLE><VARIABLE type="string"><NAME>lang</NAME></VARIABLE><VARIABLE type="string"><NAME>user</NAME></VARIABLE><VARIABLE type="string"><NAME>convention</NAME></VARIABLE><VARIABLE type="string"><NAME>name</NAME></VARIABLE><VARIABLE type="string"><NAME>value</NAME></VARIABLE><VARIABLE type="string"><NAME>variables</NAME></VARIABLE></DECLARATIONS><CONTENTS><#FUNCTION>GenerateName($error,$user,$convention,$name,$value,$variables)</#FUNCTION><#IF expression="$error!=0"><#THEN><MESSAGE type="error">__TEXT:$lang:Intranet298__ $error</MESSAGE></#THEN><#ELSE><MESSAGE type="info">__TEXT:$lang:Intranet348__</MESSAGE></#ELSE></#IF><RESULT>$value</RESULT></CONTENTS></PAGE>';
   
   if(oReq.IsBusy())
   {
      AddMessage(new vdwmessage('error',messages[language][20]),'error_message_line');
      return ;
   }

   oReq.cleanRequest();
   oReq.methode = "POST";   

   if(bSyncronous)
      oReq.setSynchronous();

   oReq.server = 'content.php';
   if(document.forms[0].elements['lang'])
      oReq.add('lang', document.forms[0].elements['lang'].value);
   oReq.add('xml_string',        escape(xmlstring));
   oReq.add('name', name);
   oReq.add('user', user);   
   oReq.add('convention', convention);
   oReq.add('variables', variables);
   oReq.add('layout', 'nolayout');
   
   oReq.setIndicatorFunction(SetIndicator, 'loading');
  
   if(fResult)
      oReq.setCallFunction(fResult,sArgs);
   
   if(!oReq.execute())
   {
      AddMessage(new vdwmessage('error',messages[language][21]),'error_message_line');
   }

   if(bSyncronous)
     return oReq; 
}

function VDWIsHidden(oElm)
{
   if(oElm && oElm.style && oElm.style.display=='none')
   {
      return true;
   }

   var oParent = oElm.parentNode
   if(!oParent)
   {
      return false;
   }

   return VDWIsHidden(oParent);
}

