//Common Javascript functions

function checkKey(e) {
	var keynum;
	var keychar;
	var numcheck;

	if (e.keyCode) {
		keynum = e.keyCode;
	} else if (e.which) {
		keynum = e.which;
	}
	return keynum;
}

/*
     @name		isIESix - Checks to see if the current browser is IE 6 or below
     @author        ssloan
     @param 
     @return
     @note          Check to see if the user is using IE 6  as the schedule resize heights 
                    will differ due to how borders are drawn
*/
function isIESix(){
 
     if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
          var version=new Number(RegExp.$1) // capture x.x portion and store as a number
          
          if (version <= 6){
               return true;
          }
     }
     
     return false;
}

/* Browser Fixes */

// Extend Array for Internet Explorer to add support for indexOf
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i < this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

if(!Array.in_array){
     Array.prototype.in_array = function ( obj ) {
          var len = this.length;
          
          for ( var x = 0 ; x <= len ; x++ ) {
               if ( this[x] == obj ) return true;
          }
          
          return false;
     }
}

// Add the trim functions for strings 
// These functions are built in Firefox 3.1+
if(!String.trim){
     String.prototype.trim = function trim() {
          return this.replace(/^\s+|\s+$/g,"");
     }
}

if(!String.trimLeft){
     String.prototype.trimLeft = function trimLeft() {
          return this.replace(/^\s+/,"");
     }
}

if(!String.trimRight){
     String.prototype.trimRight = function trimRight() {
          return this.replace(/\s+$/,"");
     }
}

if(!String.empty){
     String.prototype.empty = function empty(){
          if(this.length > 0)
               return false;
          
          if(this == null)
               return true;
               
          return true;
     }
}

if(!String.parseTime){
     String.prototype.parseTime= function parseTime(){
          var time = this.match(/^([0-1]?[0-9]{1}):([0-5]{1}[0-9]{1})(\s[p]{1}[m]{1})?/i);
          var h = parseInt(time[1], 10);
          var m = parseInt(time[2], 10);
          
          if(time[3] && h != 12){
               h+= 12;
          }
          
          var d = new Date();
          d.setHours(h);
          d.setMinutes(m);
          
          return d.getTime();
     }
}

if(!String.parseDate){
     String.prototype.parseDate = function parseDate(delimiter){
          if(!delimiter || delimiter.empty())
               delimiter = '/';
          
          var parts = this.split(delimiter);
		var d = new Date(parts[2], parts[0], parts[1],0,0,0);

          return d.getTime();
     }
}

if(!Date.format){
     Date.prototype.format = function format(formatStr){
          var re = /(%[a-zA-Z])/g;
          var values = formatStr.match(re);
          
          for(var i=0; i < values.length; i++){
               switch(values[i]){
                    case "%a":
                         var txt = (this.getHours() > 12 )? "pm" : "am";                         
                         formatStr = formatStr.replace("%a", txt, "g");
                         break;
                    case "%A":
                         var txt = (this.getHours() > 12 )? "PM" : "AM";                         
                         formatStr = formatStr.replace("%A", txt, "g");
                         break;
                    case "%d":
                         var txt = (this.getDate() < 10)? "0"+this.getDate() : this.getDate();
                         formatStr =  formatStr.replace("%d", txt, "g");
                         break;
                    case "%g":
                         var txt = (this.getHours() > 12 )? this.getHours()-12 : this.getHours();                         
                         formatStr = formatStr.replace("%g", txt, "g");
                         break;
                    case "%G":
                         formatStr = formatStr.replace("%G", this.getHours(), "g");
                         break;
                    case "%h":
                         var txt = (this.getHours() > 12 )? this.getHours()-12 : this.getHours(); 
                         txt = (txt < 10)? "0"+ txt : txt;
                         formatStr = formatStr.replace("%h", txt, "g");
                         break;
                    case "%H":
                         var txt = (this.getHours() < 10 )? "0"+ this.getHours() : this.getHours();
                         formatStr = formatStr.replace("%H", txt, "g");
                         break;
                    case "%i":
                         var txt = (this.getMinutes() < 10)? "0"+this.getMinutes() : this.getMinutes();
                         formatStr =  formatStr.replace("%i", txt, "g");
                         break;
                    case "%L":
                         var txt = (((this.getFullYear() % 4  == 0) && (this.getFullYear() % 100 != 0)) || (this.getFullYear() % 400  == 0))? 1 : 0
                         formatStr =  formatStr.replace("%L", txt, "g");
                         break;
                    case "%m":
                         var txt = ((this.getMonth()+1) < 10)? "0"+ (this.getMonth()+1) : (this.getMonth()+1);
                         formatStr =  formatStr.replace("%m", txt, "g");
                         break;
                    case "%n":
                         formatStr =  formatStr.replace("%n", (this.getMonth()+1), "g");
                         break;
                    case "%s":
                         var txt = (this.getSeconds() < 10)? "0"+ this.getSeconds() : this.getSeconds();
                         formatStr = formatStr.replace("%s", this.getSeconds(), "g");
                         break;
                    case "%w":
                         formatStr =  formatStr.replace("%w", this.getDay(), "g");
                         break;
                    case "%Y":
                         formatStr =  formatStr.replace("%Y", this.getFullYear(), "g");
                         break;
               }
          }

          return formatStr;
     }
}

function initWWB() {
     var c = o = document.getElementById("wwb");
     var pp = co = lp = tp = 0;
     var cg = function(){return Math.floor(Math.random()* 256)};
     do { lp += o.offsetLeft; tp += o.offsetTop; } while (o = o.offsetParent);
     
     if(c.getContext){
          var ctx = c.getContext("2d");   
          c.onmousemove = function(e) {if(pp){
                                        ctx.save();
                                        ctx.fillStyle = co;
                                        ctx.fillRect((e.pageX - lp), (e.pageY - tp), 4, 4);
                                        ctx.restore();}};
                                        
          c.onmousedown = function(){pp = true; co = "rgb("+ cg() +","+ cg() +","+ cg() +")";};
          c.onmouseup = function(){pp = false;};
     }
}

if(!Array.hasElement){
     Array.prototype.hasElement = function hasElement(needle, ignoreCase){
          var re = (ignoreCase)? new RegExp(needle ,"i") : new RegExp(needle);
          
          for(var i=0; i < this.length; i++){
               if(re.test(this[i])){
                    return true;
               }
          }
          
          return false;
     }
}

function hideSelectIE6() {
     if (isIESix()) {
          $("select").css("visibility", "hidden");
     }
}

function showSelectIE6() {
     if (isIESix()) {
          $("select").css("visibility", "visible");
     }
}

function cleanNameForURL(name){
     var str ="";
    
     if(name){
          name = name.split(" ");
               
          for(var i=0; i < name.length; i++){
               var part = name[i].replace(/[^a-zA-Z- ]+/, "");
               
               if(!part.empty()){  
                    str += "-"+ part;
               }
          }
     }
     return str;
}