function wordcount(input, span)
 {
   var v=input.value;
   var count = 0;
   var inword=false;
   for (var i = 0; i < v.length; ++i)
    {
      var c = v[i];
      if (c==' ' || c=='\t' || c=='\n')
       {
         if (inword == true)
          {
            inword=false;
          }
       }
      else if (inword == false)
       {
         inword=true;
         ++count;
       }
    }
   document.getElementById(span).innerHTML = ""+count;
 }

// Pass in an array of [FieldId, Msg] where FieldId is the id of the field to mark as an error and Msg is the message to print below the field
// FieldClass is the CSS class for the fiels in error
// MessageClass is the CSS class for the message
function HandleErrors(FormId, DefaultDiv, Errors, FieldClass, MessageClass)
 {
   var e;
   var p;
   var f = document.forms[FormId];
   var DD = document.getElementById(DefaultDiv);
   for (var i = 0; i < Errors.length; ++i)
    {
      var Msg = Errors[i];
      e = f == null ? null : document.f[Msg[0]];
      if (e == null)
       {
//         alert("Error looking for element '"+Msg[0]+"'");
         DD.innerHTML = DD.innerHTML+"<BR>"+Msg[0]+": "+Msg[1];
         continue;
       }
      e.className=FieldClass;
      p = document.createElement("div");
      p.id=Msg[0]+"_err";
      p.className=MessageClass;
      p.innerHTML="<BR>"+(e.type.toLowerCase()=="hidden"?Msg[0]+": ":"")+Msg[1];
      e.parentNode.insertBefore(p, e.nextSibling);
      var tag = e.tagName.toLowerCase();
      if (tag=="input" || tag=="textarea") // || tag=="select")
       {
         if (e.type.toLowerCase()!="hidden")
          {
            e.onkeyup = function() { RemoveErrorMessage(this, FieldClass); };
          }
       }
      else if (tag=="select")
       e.onchange = function() { RemoveErrorMessage(this, FieldClass); };
      else if (tag=="img" || tag=="a")
       e.onClick = function() { RemoveErrorMessage(this, FieldClass); };
   }
 }
 
function RemoveErrorMessage(Element, FieldClass)
 {
   if (Element.className==FieldClass)
    {
      Element.parentNode.removeChild(Element.nextSibling);
      Element.className="";
    }
 }

function FindXYPos(obj)
 {
   var curleft = 0;
   var curtop = 0;
   if (obj.offsetParent)
    {
      while (obj.offsetParent)
       {
         curleft += obj.offsetLeft
         curtop += obj.offsetTop
         obj = obj.offsetParent;
       }
    }
   else
    {
      if (obj.x) curleft += obj.x;
      if (obj.y) curtop  += obj.y;
    }
   return {x:curleft, y:curtop};
 } 
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Date Picker
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function DatePicker(InputId, ClassName)
 {
   this.InputId = InputId;
   this.DivName = InputId+"_DIV";
   this.ClassName = ClassName == null ? "DatePicker" : ClassName;
   
   this.show = function()
    {
      this.InputElement = document.getElementById(InputId);
      this.Pos = FindXYPos(this.InputElement);
      
      // if not rendered yet, do so
      if ( !this.oSpan )
       this.render();
 
      // set coordinates
      this.oSpan.style.left = this.Pos.x;
      this.oSpan.style.top= this.Pos.y;

      var val = this.InputElement.value.split(".");
      this.dt = new Date(parseInt(val[0]),parseInt(val[1])-1, parseInt(val[2][0]=='0'?val[2][1]:val[2]));
      
      this.fill();

      this.oSpan.style.visibility = "visible";
      this.oMonth.focus();
    }

   this.hide = function()
    {
      if ( this.oSpan ) this.oSpan.style.visibility = "hidden";
    }
   
   this.render = function()
    {
      var oT1, oTR1, oTD1, oTH1;
      var oT2, oTR2, oTD2;

      this.oSpan = document.getElementById(this.DivName);
      oT1 = document.createElement("table");
      this.oSpan.appendChild(oT1);
      oT1.className=this.ClassName+'_Table'
      oT1.border=0;
      oT1.cellPadding=0;
      oT1.cellSpacing=0;

      oTR1 = oT1.insertRow(oT1.rows.length);
      oTD1 = oTR1.insertCell(oTR1.cells.length);
      oTD1.colSpan = 7;
      oTD1.className = this.ClassName+'_Hdr';

      oT2 = document.createElement("table");
      oTD1.appendChild(oT2);
      oT2.border=0;

      oTR2 = oT2.insertRow(oT2.rows.length);

      oTD2 = oTR2.insertCell(oTR2.cells.length);
      oTD2.title = this.texts.prevMonth;
      oTD2.onclick = function() { this.oDatePicker.onPrev(); }
      oTD2.oDatePicker = this;
      oTD2.innerHTML = "&lt;&lt;";
      oTD2.className = this.ClassName+'_PrevNext';

      oTD2 = oTR2.insertCell(oTR2.cells.length);
      this.oMonth = document.createElement("select");
      oTD2.appendChild(this.oMonth);
      this.oMonth.oDatePicker = this;
      this.oMonth.onchange = this.oMonth.onkeyup = function() { this.oDatePicker.onMonth(); }
      for ( var i = 0; i < 12; i++ )
       {
         this.oMonth.add(new Option(this.texts.months[i], i),undefined);
       }
//      this.oMonth.focus();  // IE failes here.

      this.oYear = oTR2.insertCell(oTR2.cells.length);
      this.oYear.title = this.texts.yearTitle;
      this.oYear.oDatePicker = this;
      this.oYear.className = this.ClassName+'_PrevNext';

      oTD2 = oTR2.insertCell(oTR2.cells.length);
      oTD2.title = this.texts.nextMonth;
      oTD2.onclick = function() { this.oDatePicker.onNext(); }
      oTD2.oDatePicker = this;
      oTD2.innerHTML = "&gt;&gt;";
      oTD2.className = this.ClassName+'_PrevNext';

      oTR1 = oT1.insertRow(oT1.rows.length);
      for ( i = 0; i < 7; i++ )
       {
         oTH1 = document.createElement("th");
         oTR1.appendChild(oTH1);
         oTH1.innerHTML = this.texts.days[i];
         oTH1.className = this.ClassName+"_Week";
       }

      this.aCells = new Array;
      for ( var j = 0; j < 6; j++ )
       {
         this.aCells.push(new Array);
         oTR1 = oT1.insertRow(oT1.rows.length);
         oTR1.className = this.ClassName+"_Days";
         for ( i = 0; i < 7; i++ )
          {
            this.aCells[j][i] = oTR1.insertCell(oTR1.cells.length);
            this.aCells[j][i].oDatePicker = this;
            this.aCells[j][i].onclick = function() { this.oDatePicker.onDay(this); }
          }
       }
    }
    
   this.fill = function()
    {
      // first clear all
      this.clear();

      // place the dates in the calendar
      var nRow = 0;
      var d = new Date(this.dt.getTime());
      var m = d.getMonth();
      for ( d.setDate(1); d.getMonth() == m; d.setTime(d.getTime() + 86400000) )
       {
         var nCol = d.getDay();
         this.aCells[nRow][nCol].innerHTML = d.getDate();

         if ( d.getDate() == this.dt.getDate() )
          {
            this.aCells[nRow][nCol].className = this.ClassName+'_SelectedDay';
          }
         if ( nCol == 6 ) nRow++;
       }
      // set the month combo
      this.oMonth.value = m;
      // set the year text
      this.oYear.innerHTML = this.dt.getFullYear();
    }

   this.clear = function()
    {
      for ( var j = 0; j < 6; j++ )
       for ( var i = 0; i < 7; i++ )
        {
          this.aCells[j][i].innerHTML = "&nbsp;"
          this.aCells[j][i].className = this.ClassName+'_Days';
        }
    }
    
   this.onPrev = function()
    {
      if ( this.dt.getMonth() == 0 )
       {
         this.dt.setFullYear(this.dt.getFullYear() - 1);
         this.dt.setMonth(11);
       }
      else
       {
         this.dt.setMonth(this.dt.getMonth() - 1);
       }
      this.fill();
    }

   this.onNext = function()
    {
      if ( this.dt.getMonth() == 11 )
       {
         this.dt.setFullYear(this.dt.getFullYear() + 1);
         this.dt.setMonth(0);
       }
      else
       {
         this.dt.setMonth(this.dt.getMonth() + 1);
       }
      this.fill();
    }

   this.onMonth = function()
    {
      this.dt.setMonth(this.oMonth.value);
      this.fill();
    }

   this.onDay = function(oCell)
    {
      var d = parseInt(oCell.innerHTML);
      if ( d > 0 )
       {
         this.dt.setDate(d);
         this.hide();
         var m = (this.dt.getMonth() + 1); if (m < 10) m = "0"+m;
         var d = this.dt.getDate();        if (d < 10) d = "0"+d;
         this.InputElement.value = this.dt.getFullYear() + "." + m + "." + d;
       }
     }

    this.texts = { months   : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], 
                   days     : ["S", "M", "T", "W", "T", "F", "S"],
                   prevMonth: "Previous Month",
                   nextMonth: "Next Month",
                   yearTitle: "Year. Click to modify.",
                   yearQuestion: "Enter a new year:"
                 };
 }



function RatingClick(Id, i, func)
 {
   var e = document.getElementById("i"+Id);
   if (e == null)
    {
      alert("ERROR: Rating image '"+Id+"' not found.");
    }
   e.src = e.src.replace(/\.[0-9]+\.gif/, "."+i+".gif");
   
   var e = document.getElementById(Id);
   if (e == null)
    {
      alert("ERROR: Rating input field '"+Id+"' not found.");
    }
   else
    {
      e.value=i;
      if (func != null)
       func(Id, i);
    }
 }



function PromptBeforeProceeding(Text, Url)
 {
   if (confirm(Text) == true)
    {
      window.location=Url;
    }
 }
 
function ProjectPathCrumbs(ProjectName, PathName)
 {
   var Str = "<A HREF=\"/ProjectProcess/"+ProjectName+"?path=\">"+ProjectName+"</A>";
   var i = PathName.indexOf('\\');
   var j = 0;
   var ParentPath = "";
   while (i != -1)
    { 
      ParentPath = PathName.substring(0, i);
      Str+= "\\ <A HREF=\"/ProjectProcess/"+ProjectName+"?path="+escape(ParentPath)+"\">"+PathName.substring(j, i)+"</A> ";
      j = i+1;
      i = PathName.indexOf('\\', j);
    }
   if (PathName.length > 1)
    {
      Str+= "\\ <A HREF=\"/ProjectProcess/"+ProjectName+"?path="+escape(PathName)+"\">"+PathName.substring(j)+"</A> ";
    }
   return Str;
 }
 
function EMAIL(email)
 {
   var str = "";
   for (var i = email.length-1; i>= 0; --i)
    str+=email.charAt(i);
   document.writeln("<A HREF='mailto:"+str+"'>"+str+"</A>");
 }
 
function GetFolderTreePath(message)
 {
    var CleanNameRegEx = new RegExp(/ \(\d+\)/g);
    var folderName = message.source.title.replace(CleanNameRegEx, "");
    var parent = message.source.parent;
    while (parent != undefined && parent.title != undefined)
     {
       folderName = parent.title.replace(CleanNameRegEx, "")+"\\"+folderName;
       parent = parent.parent;
     }
    return folderName;
 } 