String.prototype.replaceAll = function(strSearch,strReplace){
	var tmp=this
	if(typeof strSearch == "undefined"){return this;}

	while(tmp.indexOf(strSearch) != -1){
			tmp=tmp.replace(strSearch,strReplace);
	}
	return tmp;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

String.prototype.toFloat = function(){
	var tmp = this.replace(",",".");
	if(tmp == ""){
		tmp = 0.00;
	}
	var n = parseFloat(tmp);
	
	if(isNaN(n)){
		return null;
	}else{
		return n;
	}
	
}

/**
 * hex2rgb
 *
 * Convert hexadecimal color triplets to RGB
 *
 * Expects an hexadecimal color triplet (case insensitive)
 * Returns an array containing the decimal values
 * for r, g and b.
 *
 * example:
 *   test = 'ff0033'
 *   test.hex2rgb() //returns the array (255,00,51)
 */

String.prototype.hex2rgb = function()
{
  var red, green, blue;
  var triplet = this.toLowerCase().replace(/#/, '');
  var rgbArr  = new Array();

  if(triplet.length == 6)
  {
    rgbArr[0] = parseInt(triplet.substr(0,2), 16)
    rgbArr[1] = parseInt(triplet.substr(2,2), 16)
    rgbArr[2] = parseInt(triplet.substr(4,2), 16)
    return rgbArr;
  }
  else if(triplet.length == 3)
  {
    rgbArr[0] = parseInt((triplet.substr(0,1) + triplet.substr(0,1)), 16);
    rgbArr[1] = parseInt((triplet.substr(1,1) + triplet.substr(1,1)), 16);
    rgbArr[2] = parseInt((triplet.substr(2,2) + triplet.substr(2,2)), 16);
    return rgbArr;
  }
  else
  {
    throw triplet + ' is not a valid color triplet.';
  }
}


/**
 * ucfirst
 *
 * Returns a string with the first character capitalized,
 * if that character is alphabetic.
 *
 * object string
 * return string
 *
 * example:
 *   test = 'john'
 *   test.ucfirst() //returns 'John'
 */

String.prototype.ucfirst = function()
{
  firstLetter = this.charCodeAt(0);
  if((firstLetter >= 97 && firstLetter <= 122)
     || (firstLetter >= 224 && firstLetter <= 246)
     || (firstLetter >= 249 && firstLetter <= 254))
  {
    firstLetter = firstLetter - 32;
  }
  alert(firstLetter)
  return String.fromCharCode(firstLetter) + this.substr(1,this.length -1)
}


/**
 * strPad
 *
 * Pad a string to a certain length with another string
 *
 * This functions returns the input string padded on the left, the right, or both sides
 * to the specified padding length. If the optional argument pad_string is not supplied,
 * the output is padded with spaces, otherwise it is padded with characters from pad_string
 * up to the limit.
 *
 * The optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH.
 * If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
 *
 * If the value of pad_length is negative or less than the length of the input string,
 * no padding takes place.
 *
 * object string
 * return string
 *
 * examples:
 *   var input = 'foo';
 *   input.strPad(9);                      // returns "foo      "
 *   input.strPad(9, "*+", STR_PAD_LEFT);  // returns "*+*+*+foo"
 *   input.strPad(9, "*", STR_PAD_BOTH);   // returns "***foo***"
 *   input.strPad(9 , "*********");        // returns "foo******"
 */

var STR_PAD_LEFT  = 0;
var STR_PAD_RIGHT = 1;
var STR_PAD_BOTH  = 2;

String.prototype.strPad = function(pad_length, pad_string, pad_type)
{
  /* Helper variables */
  var num_pad_chars   = pad_length - this.length;/* Number of padding characters */
  var result          = '';                       /* Resulting string */
  var pad_str_val     = ' ';
  var pad_str_len     = 1;                        /* Length of the padding string */
  var pad_type_val    = STR_PAD_RIGHT;            /* The padding type value */
  var i               = 0;
  var left_pad        = 0;
  var right_pad       = 0;
  var error           = false;
  var error_msg       = '';
  var output           = this;

  if (arguments.length < 2 || arguments.length > 4)
  {
    error     = true;
    error_msg = "Wrong parameter count.";
  }


  else if(isNaN(arguments[0]) == true)
  {
    error     = true;
    error_msg = "Padding length must be an integer.";
  }
  /* Setup the padding string values if specified. */
  if (arguments.length > 2)
  {
    if (pad_string.length == 0)
    {
      error     = true;
      error_msg = "Padding string cannot be empty.";
    }
    pad_str_val = pad_string;
    pad_str_len = pad_string.length;

    if (arguments.length > 3)
    {
      pad_type_val = pad_type;
      if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH)
      {
        error     = true;
        error_msg = "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH."
      }
    }
  }

  if(error) throw error_msg;

  if(num_pad_chars > 0 && !error)
  {
    /* We need to figure out the left/right padding lengths. */
    switch (pad_type_val)
    {
      case STR_PAD_RIGHT:
        left_pad  = 0;
        right_pad = num_pad_chars;
        break;

      case STR_PAD_LEFT:
        left_pad  = num_pad_chars;
        right_pad = 0;
        break;

      case STR_PAD_BOTH:
        left_pad  = Math.floor(num_pad_chars / 2);
        right_pad = num_pad_chars - left_pad;
        break;
    }

    for(i = 0; i < left_pad; i++)
    {
      output = pad_str_val.substr(0,num_pad_chars) + output;
    }

    for(i = 0; i < right_pad; i++)
    {
      output += pad_str_val.substr(0,num_pad_chars);
    }
  }

  return output;
}



NT = {
	debug:null,
	debugWindowId:'Debug',
	init:function(){},

	initDebug:function(){
		if(this.debug){this.stopDebug();}
		this.debug=document.createElement('div');
		this.debug.setAttribute('id',this.debugWindowId);
		document.body.insertBefore(this.debug,document.body.firstChild);
	},
	setDebug:function(bug){
		if(!this.debug){this.initDebug();}
		this.debug.innerHTML+=bug+'<br>';
	},
	stopDebug:function(){
		if(this.debug){
			this.debug.parentNode.removeChild(this.debug);
			this.debug=null;
		}
	},

	addLoadEvent:function(func){
		var oldonload = window.onload;
		if(typeof window.onload != 'function'){
			window.onload = func;
		}else{
			window.onload = function(){
				oldonload();
				func();
			}
		}
	},

	addEvent:function(element,evType,fn,useCapture){
		if(element.addEventListener){
			element.addEventListener(evType,fn,useCapture);
		}else if(element.attachEvent){
			element.attachEvent('on' + evType,fn);
		}else{
			element['on'+evType] = fn;
		}
	},
	getTarget:function(e){
		var target = window.event ? window.event.srcElement :
		e ? e.target : null;
		if (!target){return false;}
		return target;
	},
	stopDefault:function(e){
		if(window.event && window.event.returnValue){
			window.event.cancelBubble = true;
		}
		if (e && e.preventDefault){
			e.preventDefault();
		}
	},
	stopBubble:function(e){
		if(window.event && window.event.cancelBubble){
			window.event.cancelBubble = true;
		}
		if (e && e.stopPropagation){
			e.stopPropagation();
		}
	},
	cancelClick:function(e){
		if (window.event && window.event.cancelBubble && window.event.returnValue){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if (e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
		}
	},





	isIE:function(){
		return (document.all ? true : false);
	},
	isOPERA:function(){
		return (navigator.userAgent.indexOf('Opera') != -1 ? true : false);
	},
	isDOM:function(){
		return (document.getElementById && document.createTextNode ? true : false);
	},
	isEmpty:function(str){
		if(str && (typeof str == "string")){
			str = str.trim();
			return ( (str == "") || (str == null) ) ? true : false;
		}else if (typeof str == "number"){
			return false;
		}else{
			return true;
		}
	},
	inRange:function(iValue,lo,hi){
		try{
			var num = parseInt(iValue,10);
			if( (isNaN(num)) || (num < lo) || (num > hi) ){
				return false;
			}
			return true;
		}catch(e){
			return false;
		}
	},

	/* Cookies */

	//
	// "Internal" function to return the decoded value of a cookie
	getCookieVal:function(offset) {
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1) {
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	},

	//
	// Function to correct for 2.x Mac date bug. Call this function to
	// fix a date object prior to passing it to SetCookie.
	fixCookieDate:function(date) {
		var base = new Date(0);
		var skew = base.getTime(); // dawn of (Unix) time - should be 0
		if (skew > 0) { // Except on the Mac - ahead of its time
			date.setTime (date.getTime() - skew);
		}
	},

	// Function to return the value of the cookie specified by "name".
	//   name - String object containing the cookie name.
	//   returns - String object containing the cookie value, or null if
	//     the cookie does not exist.
	getCookie:function(name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg) {
				return NT.getCookieVal (j);
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) {
				break;
			}
		}
		return null;
	},

	setCookie:function(name,value,expires,path,domain,secure) {
   		document.cookie = name + "=" + escape (value) +
      	((expires) ? "; expires=" + expires.toGMTString() : "") +
      	((path) ? "; path=" + path : "") +
      	((domain) ? "; domain=" + domain : "") +
      	((secure) ? "; secure" : "");
	},

	deleteCookie:function(name,path,domain){
		if (NT.getCookie(name)) {
			document.cookie = name + "=" +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	},



	// Allow only use numbers into an input
	// use into onkeydown and onkeyup event
	// <input type="text" onkeydown="return NT.onlyNumbers(event);"  onkeyup="return NT.onlyNumbers(event);" name="test" />
	onlyNumbers:function(e){
		if(window.event){
			var key = window.event.keyCode;
		}else if(e){
			var key = e.keyCode;
		}

		if( ( (key<48) || (key>57)) && (key != 8) && (key != 46) && (key != 127) && (key != 9) && (key != 110) && ( (key<37) || (key>40)) && ( (key<96) || (key>105)) ){
			return false;
		}
	},

	isValidDate:function(sDate,sFormat){
		var tmp 	= sDate.toString();

		var vDate 	= null;
		var iDay  	= 0;
		var iMonth 	= 0;
		var iYear 	= 0;
		var dt 		= new Date();


		var vMaxDays = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31);
		var vMaxDaysBi = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31);

		tmp	= tmp.replaceAll("/","-");

		if ( (typeof sFormat == "undefined") || (sFormat == null)){
			sFormat = "ddmmyyyy";
		}

		switch(sFormat){
			case "ddmmyyyy":
				vDate = tmp.split("-");
				iDay = 0;
				iMonth = 1;
				iYear = 2;
				break;
			case "yyyymmdd":
				vDate = tmp.split("-");
				iDay = 2;
				iMonth = 1;
				iYear = 0;
				break;
			case "yyyyddmm":
				vDate = tmp.split("-");
				iDay = 1;
				iMonth = 2;
				iYear = 0;
				break;
			case "mmddyyyy":
				vDate = tmp.split("-");
				iDay = 1;
				iMonth = 0;
				iYear = 2;
				break;
		}
		try{
			for(i=0;i<vDate.length;i++){
				vDate[i] = parseInt(vDate[i],10);
				if(isNaN(vDate[i])){
					return false;
				}
			}
			var bYear	= this.inRange(vDate[iYear],1900,2100);
			var bMonth 	= this.inRange(vDate[iMonth]);
			if((vDate[iYear] % 4) == 0){
				var bDay	= this.inRange(vDate[iDay],1,vMaxDaysBi[vDate[iMonth]]);
			}else{
				var bDay	= this.inRange(vDate[iDay],1,vMaxDays[vDate[iMonth]]);
			}


		}catch(e){
			return false
		}
		return bMonth && bDay && bYear;
	},

	lastSibling:function(node){
		var tempObj=node.parentNode.lastChild;
		while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
			tempObj=tempObj.previousSibling;
		}
		return (tempObj.nodeType==1)?tempObj:false;
	},
	firstSibling:function(node){
		var tempObj=node.parentNode.firstChild;
		while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
			tempObj=tempObj.nextSibling;
		}
		return (tempObj.nodeType==1)?tempObj:false;
	},
	getText:function(node){
		if(!node.hasChildNodes()){return false;}
		var reg=/^\s+$/;
		var tempObj=node.firstChild;
		while(tempObj.nodeType!=3 && tempObj.nextSibling!=null ||    reg.test(tempObj.nodeValue)){
			tempObj=tempObj.nextSibling;
		}
		return tempObj.nodeType==3?tempObj.nodeValue:false;
	},

	closestSibling:function(node,direction){
		var tempObj;
		if(direction==-1 && node.previousSibling!=null){
			tempObj=node.previousSibling;
			while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
				tempObj=tempObj.previousSibling;
			}
		}else if(direction==1 && node.nextSibling!=null){
			tempObj=node.nextSibling;
			while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
				tempObj=tempObj.nextSibling;
			}
		}
		return ((typeof tempObj != 'undefined') && (tempObj.nodeType==1))?tempObj:false;
	},
	winPopup:function(Url,Width,Height){
		var opt = "width=" + Width +",height="+Height+",menubar=no,toolbar=no,location=no,status=no";
		var win = window.open(Url,"NewWin",opt);
		return false;
	
	},getUrlParam:function(name ){
  		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  		var regexS = "[\\?&]"+name+"=([^&#]*)";
  		var regex = new RegExp( regexS );
  		var results = regex.exec( window.location.href );
  		if( results == null ){
    		return "";
    	}else{
    		return results[1];
    	}
	}
	
}

/**
*
* UTF-8 data encode / decode
* http://www.webtoolkit.info/
*
**/

var UTF8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

