// ---- START REUSABLE GENERIC CODE ----  
// License: copyright (C) 2008, codeserver.net/wiki1.net, Open Source 2008/12/10


function getSubValue (inString, inName) {
    var inDefault = '';
    if (inString.length > 0) {
      var myRe = new RegExp(inName + "=([^;]+)");  
      var myValue = myRe.exec(inString);
      return (myValue != null) ? unescape(myValue[1]) : inDefault;
    } else {
      return inDefault;
    }
  }


function PopupMenu(name, items, images, pointers) {
// License: copyright (C) 2008, codeserver.net/wiki1.net, Open Source 2008/12/10 

  // Properties
  this.name = name;
  this.items = items;
  this.images = images;
  this.pointers = pointers;
  this.selected = 0;
  this.zIndex = 400;
  
  // Methods
  this.init = function() {
  // Draw Menu Objects
    document.write("<div class='menu' id='" + this.name + "' style='z-index:" + this.zIndex + ";' >");
    for(var i=0; i<this.items.length; i++) {
      document.write("<div id='" + this.name + "Item" + i + "' class='menuItem' >");
      if (images[i].length > 0) {
        document.write("<img id='" + this.name + "Img" + i + "' src='" + images[i] + "' />");
      }
      document.write(" " + items[i] + " ");       
      if (pointers[i]==1) {
        document.write("<img src='images/more.gif' />");
      }
      document.write("</div>");       
    }
    document.write("</div>");
  }


  this.setImageBackGrounds = function(backgrounds) {
  // Set Image Backcolor
    for(var i=0; i<items.length; i++) {
      if (backgrounds[i].length > 0) {
        var domMenuImg=document.getElementById(this.name + 'Img' + i);
        domMenuImg.style.background = backgrounds[i];
      }
    }
  }


  this.select = function(item) {
  // Select Item
    this.selected = item;
    for(var i=0; i<this.items.length; i++) {
      var domMenuItem=document.getElementById(this.name + 'Item' + i);
      if (i == this.selected) {
        domMenuItem.style.background = '#D3D3D3';
      } else {
        domMenuItem.style.background = '#F5F5F5';
      }
    }
  }


  this.show = function(x,y) {
  // Show Menu
    var domMenu=document.getElementById(this.name);
    domMenu.style.left = x;
    domMenu.style.top = y;
    for(var i=0; i<this.items.length; i++) {
      var domMenuItem=document.getElementById(this.name + 'Item' + i);
      if (i == this.selected) {
        domMenuItem.style.background = '#D3D3D3';
      } else {
        domMenuItem.style.background = '#F5F5F5';
      }
    }
  }


  this.hide = function(x,y) {
  // Hide Menu
    var domMenu=document.getElementById(this.name);
    domMenu.style.top = this.items.length * -25;
  }
}


//digit2
function get2Digit(i) {
// Return i as 2 digits
  if (i<10) {i="0" + i;} 
  return i;
}


function getwindowx() {
// Get window width
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}


function getwindowy() {
// Get Window Height
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}


function myDebug(inValue, inAction) {
// Example: myDebug('a1', 'TITLE'); myDebug('a2'); myDebug('a3', 'BREAK');  

  // DISABLE:   return false;
  // INIT Static Vars
  myDebug.Index = myDebug.Index || 0;
  myDebug.Line = myDebug.Line || '0: ';
  myDebug.Body = myDebug.Body || '';
  switch(inAction) {
  case 'NEW':
    myDebug.Body = myDebug.Line + '</BR>' + myDebug.Body; 
    // myDebugIndx = myDebugIndx + 1;
    myDebug.Index = myDebug.Index + 1;
    myDebug.Line = myDebug.Index + ': ';
    if (inValue.length > 0) { myDebug.Line = myDebug.Line + inValue + '; '; }
    myShowMessage('myDebugDiv', myDebug.Line + '</BR>' + myDebug.Body);
    break;
  case 'BREAK':
    if (inValue.length > 0) { myDebug.Line = myDebug.Line + inValue; }
    myDebug.Body = myDebug.Line + '<BR/>' + myDebug.Body; 
    // myDebugIndx = myDebugIndx + 1;
    myDebug.Index = myDebug.Index + 1;
    myDebug.Line = myDebug.Index + ': ';
    myShowMessage('myDebugDiv', myDebug.Body);
    break;
  case 'LINE':
    myDebug('', 'NEW');
    myDebug(inValue, 'BREAK');
    break;
  case 'BOLD':
    myDebug('', 'NEW');
    myDebug('<b>' + inValue + '</b>', 'BREAK');
    break;
  default:
    if (inValue.length > 0) { myDebug.Line = myDebug.Line + inValue + '; '; }
    myShowMessage('myDebugDiv', myDebug.Line + '</BR>' + myDebug.Body);
  }
}

function myShowMessage(inContainer, inMessage) {
// Example: myShowMessage('div1', 'val=1');
  if (inContainer=='alert') { alert(inMessage); } 
  else {
    var oElem = document.getElementById(inContainer);
    if (oElem) { oElem.innerHTML=inMessage; } 
    else { document.write("<div id='" + inContainer + "' style='z-index:2000'>" + inMessage + "</div>"); }
  }
}



function Cookie(name, expireDays) {
// example: var c = new Cookie('test',90); c.set('data');  var data = c.get();

  // Properties
  this.name = name;
  this.expireDays = expireDays;


  // Methods

  this.get = function () {
  // Get
    var c_start;
    var c_end;
    if (document.cookie.length>0) {
      c_start=document.cookie.indexOf(this.name + "=");
      if (c_start!=-1) { 
        c_start=c_start + this.name.length+1; 
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
      } 
    }
    return "";
  }


  this.set = function (value) {
  // Set
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+this.expireDays);
    document.cookie=this.name+ "=" +escape(value)+
    ((this.expireDays==null) ? "" : ";expires="+exdate.toGMTString());
  }


  this.cut = function () {
  // Cut
    var exdate=new Date();
    exdate.setDate(exdate.getDate()-1);
    document.cookie=this.name+"=;expires="+exdate.toGMTString();
  }


  this.getSub = function (c_sub) {
  // Get Sub
    var inDefault = '';
    var inCookie = this.get();
    if (inCookie.length > 0) {
      var myRe = new RegExp(c_sub + "=([^;]+)");  
      var myValue = myRe.exec(inCookie);
      return (myValue != null) ? unescape(myValue[1]) : inDefault;
    } else {
      return inDefault;
    }
  }


  this.setSub = function (c_sub,c_value) {
  // Set Sub (expire applies to cookie, not sub cookie...)
    var inCookie = this.get();
    var outValue = c_sub + "=" + escape(c_value);
    var outCookie = "";
    outCookie = inCookie;
    if (outCookie.length > 0) {
      var myRe = new RegExp(c_sub + "=([^;]+)");  
      var myValue = myRe.exec(outCookie);
      outValue = (myValue != null) ? outCookie.replace(myRe, outValue) : outCookie + outValue + ";";
    } else {
      outValue += ";"
    }
    this.set(outValue);
  }
}


// ---- END REUSABLE GENERIC CODE ---- Open Source, Copyright 2007 wiki1.net 
