if(!this.JSON){this.JSON={}}(function(){function f(n){return n<10?"0"+n:n}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+string+'"'}function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==="object"&&typeof value.toJSON==="function"){value=value.toJSON(key)}if(typeof rep==="function"){value=rep.call(holder,key,value)}switch(typeof value){case"string":return quote(value);case"number":return isFinite(value)?String(value):"null";case"boolean":case"null":return String(value);case"object":if(!value){return"null"}gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==="[object Array]"){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||"null"}v=partial.length===0?"[]":gap?"[\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"]":"["+partial.join(",")+"]";gap=mind;return v}if(rep&&typeof rep==="object"){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==="string"){v=str(k,value);if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}v=partial.length===0?"{}":gap?"{\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"}":"{"+partial.join(",")+"}";gap=mind;return v}}if(typeof JSON.stringify!=="function"){JSON.stringify=function(value,replacer,space){var i;gap="";indent="";if(typeof space==="number"){for(i=0;i<space;i+=1){indent+=" "}}else{if(typeof space==="string"){indent=space}}rep=replacer;if(replacer&&typeof replacer!=="function"&&(typeof replacer!=="object"||typeof replacer.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":value})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==="object"){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v}else{delete value[k]}}}}return reviver.call(holder,key,value)}cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}}());


/*
*
* Copyright (c) 2009 C. F., Wong (<a href="http://cloudgen.w0ng.hk">Cloudgen Examplet Store</a>)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
* 
* See details in: <a href="http://cloudgen.w0ng.hk/javascript/javascript.php">Javascript Examplet</a>
*
*/
(function(a){a.fn.caret=function(n,j){var b,f,l=this[0];if(typeof n==="object"&&typeof n.start==="number"&&typeof n.end==="number"){b=n.start;f=n.end}else{if(typeof n==="number"&&typeof j==="number"){b=n;f=j}else{if(typeof n==="string"){if((b=l.value.indexOf(n))>-1){f=b+n.length-1}else{b=null}}else{if(Object.prototype.toString.call(n)==="[object RegExp]"){var k=n.exec(l.value);if(k!=null){b=k.index;f=b+k[0].length-1}}}}}if(typeof b!="undefined"){if(a.browser.msie){var h=this[0].createTextRange();h.collapse(true);h.moveStart("character",b);h.moveEnd("character",f-b);h.select()}else{this[0].selectionStart=b;this[0].selectionEnd=f}this[0].focus();return this}else{if(a.browser.msie){var c=this.val();var g=document.selection.createRange().duplicate();g.moveEnd("character",c.length);var m=(g.text==""?c.length:c.lastIndexOf(g.text));g=document.selection.createRange().duplicate();g.moveStart("character",-c.length);var i=g.text.length}else{var m=l.selectionStart,i=l.selectionEnd}var d=l.value.substring(m,i);return{start:m,end:i,text:d,replace:function(e){return l.value.substring(0,m)+e+l.value.substring(i,l.value.length)}}}return this}})(jQuery);

(function($) {

jQuery.fn.multibutton = function() {
	
	var check = function(el) {
		var el = $(el);
		
		var checked = el.find('input[type=checkbox],input[type=radio]').attr('checked');
		
		if (checked)
			el.addClass('active');
		else
			el.removeClass('active');
	} 
	return this.each(function() {
		check(this);

		var that = this;
		var handle = $(this).find('input[type=checkbox], input[type=radio]').eq(0);
		var name = handle.attr('name');
		var type = handle.attr('type');
		
		var fn = function(event){
			if (type == 'radio') {
				$(this).closest('form').find('.active input[type=checkbox], .active input[type=radio]').each(function() {
					if ($(this).attr('name') == name)
						$(this).closest('.active').removeClass('active');
				});
				$(that).addClass('active');
			} else {
				check(that);
			}
		};
		
		if (type == 'radio') {
			$(this).find('input[type=text],input[type=password],input[type=submit],input[type=button],select,textarea').focus(function() {
				handle.click();
				handle.change();
			});
		} else if (type == 'checkbox') {
			$(this).find('a,input[type=text],input[type=password],input[type=submit],input[type=button],select,textarea').click(function(event) {
				event.preventDefault();
			});
		}
		
		handle.click(fn);
	});
};

var phtml_suggestive_objs = [];

$(function() {
	$('input[type=text]').focus(function() {
		this.hasTextFocus = true;
	}).blur(function() {
		this.hasTextFocus = false;	
	});	
})

jQuery.fn.phtml_suggestive = function(options, value, show) {

	var getValue = function(dropdown) {
		var value = dropdown.attr('value');
		if (value)
			return value;
		var index = dropdown.attr('selectedIndex');
		var opts = dropdown.attr('options');
		if (index < 0 || index >= opts.length) {
			return null;
		}
		var opt = $(dropdown.attr('options')[dropdown.attr('selectedIndex')]);
		return opt.attr('value') ? opt.attr('value') : opt.text();
	};
	
	return this.each(function() {
		
		if (typeof phtml_suggestive_objs[$(this).attr('id')] != 'undefined') {
			
			var o = phtml_suggestive_objs[$(this).attr('id')];
			
			o.setOptions(options);

			if (show || o.hasTextFocus()) {
				o.f();
				o.focus();
			}
						
			return;
		}
		var dropDown = null;
		var that = $(this);
		
		var PHtml_suggestive = function() {
			that.attr('autocomplete','off');
			
			var self = this;
			
			this.options = options;
			
			this.setOptions = function(o) {
				var open = false;
				if (dropDown != null) {
					self.removeDropDown();
					open = true;
				}
				self.options = o;
				
				if (open) {
					self.f();
				}
			}
			
			this.dropDownSelected = function() {
				that.focus();
				that.val(getValue(dropDown.find('select')));
				that.change();
				that.removeClass('label');
				self.selContainer.remove();
				setTimeout(function() {dropDown = null;},200);	
			}
			
			this.createDropDown = function() {
				
				if (self.options.length <= 0) {
					self.removeDropDown();						
					return;
				}
				
				self.selContainer = $('<div/>');
				self.selContainer.css({
					'position': 'absolute',
					'z-index': 10000
				});
				
				self.positionDropDown(self.selContainer);
				
				var sel = $('<select size="'+(self.options.length > 7 ? 7 : self.options.length < 2 ? 2 : self.options.length)+'"/>');
				
				phtml_suggestive_objs[that[0]] = sel;

				sel.css({
					'border': '1px solid #888',
					'overflow': 'auto'
				});
				if ($.support.style)
					sel.css('min-width', that.outerWidth());
				else
					sel.css('width', that.outerWidth()+100);
				self.selContainer.append(sel);
				
				for(var i in self.options) {
					sel.append('<option>'+self.options[i]+'</option>');
				};
				
				$(document.body).prepend(this.selContainer);
				
				sel.change(self.dropDownSelected);
				sel.click(self.dropDownSelected);
				
				setTimeout(function() {self.updateDropDownSelection(sel);}, 0);			
				
				return self.selContainer;
			};
			
			this.positionDropDown = function(cont) {
				var pos = that.offset();
				cont.css({
					'top': pos.top + that.outerHeight(),
					'left': pos.left
				});
			};
			
			var setSelectionTimer = null;
			this.updateDropDownSelection = function(cont, appendsuggestion) {
				var val = that.attr('value');
				var opts = cont.find('option');
				var maxMatch = -1;
				var lastMatch = null;
				opts.each(function(j) {
					var opt = $(this).text();
					for(var i = 0; i < opt.length || i < val.length; i++) {
						if (opt.charAt(i).toLowerCase() == val.charAt(i).toLowerCase()) {
							if (maxMatch <= i) {
								if (maxMatch != i)
									lastMatch = this;
								maxMatch = i
							}
						} else {
							return;
						}
					}
				});
				if (lastMatch) {
					
					clearTimeout(setSelectionTimer);
					
					setSelectionTimer = setTimeout(function() {
						if (appendsuggestion) {
							var pos = that.caret().start;
							if (pos-1 <= maxMatch) {
								var match = $(lastMatch).text();
								var val = that.val();
								var newVal = val + match.substr(val.length);
								that.val(newVal);
								that.caret({start:val.length, end:newVal.length});
							}
						}
						
						var na = $(lastMatch).nextAll();
						na.eq(na.length >= 7 ? 6 : na.length - 1).attr('selected','selected');
						$(lastMatch).attr('selected','selected');
					},100);
				}
			};
			
			this.f = function() {
				if (dropDown == null) {
					dropDown = self.createDropDown();
				}
			}; 
			
			that.focus(this.f);		
			that.click(this.f);
			
			that.keyup(function(e) {
				if (dropDown != null) {
					
					var notcontrol = e.keyCode < 8 || e.keyCode > 46 || e.keyCode == 32;
					
					if (jQuery.inArray(e.keyCode,[38,40,33,34]) == -1)					
						self.updateDropDownSelection(dropDown, notcontrol);
				}
			});
			
			this.moveDropDownSelection = function(by) {
				if (dropDown == null)
					return;
				
				var sel = dropDown.find('select');
				var opts = sel.find('option[selected]');
				var val;
				if (opts.length > 0) {
					var all;
					if (by > 0) {
						all = opts.nextAll();
					} else {
						all = opts.prevAll();
					}
					val = all.eq(Math.abs(by)-1).attr('selected', 'selected').val();
				} else {
					val = sel.find('option:first').attr('selected','selected').val();
				}
				
				that.val(val);				
			}
			
			that.keydown(function(e) {
				if (dropDown != null) {
					if (e.keyCode == 13) {
						e.preventDefault();
						self.dropDownSelected();
					} else if (e.keyCode == 9) {
						self.doBlur();
					} else if (e.keyCode == 38) {
						self.moveDropDownSelection(-1);
					} else if (e.keyCode == 40) {
						self.moveDropDownSelection(1);
					} else if (e.keyCode == 33) {
						self.moveDropDownSelection(-7);
					} else if (e.keyCode == 34) {
						self.moveDropDownSelection(7);
					}
				}
			});
			
			/* if window is resized, reposition dropDown */
			$(window).resize(function() {
				if (dropDown != null) {
					self.positionDropDown(dropDown);
				}
			});
			
			/* add a scroll handler to window scroll */
			$(window).scroll(function() {
				if (dropDown != null) {
					self.positionDropDown(dropDown);
				}
			});

			/* search for overflows and add a scroll handler */
			var overflow;
			for(var p = that.parent(); p.length > 0; p = p.parent()) {
				if (p[0] == document.body)
					break;
				overflow = p.css('overflow');
				if (overflow == 'hidden' || overflow == 'scroll' || overflow == 'auto') {
					p.scroll(function() {
						if (dropDown != null) {
							dropDown.remove();
							dropDown = null;
						}
					});
				}
			}
			
			this.removeDropDown = function() {
				if (dropDown != null) {
					dropDown.remove();
					dropDown = null;
				}				
			};
			
			this.focus = function() {
				that.focus();
			};
			
			this.hasTextFocus = function() {
				return that[0].hasTextFocus;
			}
			
			$(document).click(function(e) {
				if (typeof e.originalEvent == 'undefined')
					return;
				if (dropDown != null && e.target != that[0]) {
					self.removeDropDown();
				}
			});
			
			this.doBlur = function() {
				if (dropDown != null) {
					var dropval = getValue(dropDown.find('select'));
			
					if (dropval && dropval != that.val() && dropval.toLowerCase() == that.val().toLowerCase()) {
						that.val(dropval);
					}
					that.change();
					
					setTimeout(function() {self.removeDropDown();},100);					
				}					
			};
			
			if (show) {
				self.f();
				self.focus();
			}
		}

		
		phtml_suggestive_objs[$(this).attr('id')] = new PHtml_suggestive();
		

	});
};

jQuery.fn.infodialog = function() {
	return this.each(function() {
		var that = $(this);
		var info = that.attr('title');
		that.removeAttr('title');
		
		var div = $('<div title="Information" style="display: none"></div>').html(info.replace(/\r?\n/g,'<br/>')).appendTo($(document.body));
		
		var title;
		if(that.parents('span').prev('span').children('h2').length > 0) {
			title = that.parents('span').prev('span').children('h2').html();
		} else {
			title = 'Information';
		}
		
		div.dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			resizable: false,
			width: 700,			
			title: title,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});
		
		that.click(function() {
			div.dialog('open');
		});
	});
};

jQuery.fn.inputNumbersOnly = function() {
	return this.each(function() {
		var that = $(this);
		that.keypress(function(event) {
			var charCode = (event.which) ? event.which : event.keyCode
			if (charCode > 31 && (charCode < 48 || charCode > 57))
	            event.preventDefault();
		});
	});
};


})(jQuery);

function addToDate(date, addstr) {
	var regex = /([+-])([0-9]+)([a-z])/gi;
	var result;
	while (result = regex.exec(addstr)) {
		var val = parseInt(result[2]);
		if (result[1] == '-')
			val = -val;
		var f = result[3].toLowerCase();
		if (f == 'd') {
			date.setDate(date.getDate()+val);
		} else if (f == 'w') {
			date.setDate(date.getDate()+val*7);
		} else if (f == 'm') {
			date.setMonth(date.getMonth()+val);
		} else if (f == 'y') {
			date.setFullYear(date.getFullYear()+val);
		}
	}
	return date;
}

$(document).ready(function() {
	$('.multibutton').multibutton();
	$('.info.tooltip').infodialog();
	$('.inputNumbersOnly').inputNumbersOnly();
});