//############################################################################
//# Javascript function[s]: Igaro WMS
//# Copyright: A. Charnley, Igaro
//############################################################################

// cache location to prevent ahash from ruining ajax calls
var a = window.location.href.split('#');
if (a[0].indexOf('?') == -1) a[0] += '?';
_winloc = a[0];

// better way for accessing elements
function $(i){return document.getElementById(i)}
function $$(e,p){p=p||document; return p.getElementsByTagName(e)}

// prototype a few useful functions
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g,"");  }
String.prototype.numComma = function () { var o = this.split('.',2); var t = ''; var i; for (i=o[0].length;i>3;i-=3) { t += ','+o[0].substr(i-3,3); } t = o[0].substr(0,i)+t; if (o.length == 2) t += '.' + o[1]; return t; }
Number.prototype.numComma = function() { var o = this+''; return o.numComma(); }
String.prototype.toHTML = function() { var o = this; o = o.replace(/\</g,"\&lt;"); o = o.replace(/\>/g,"\&gt;"); o = o.replace(/\|/g,"\&#124;"); o = o.replace(/  /g," \&nbsp;"); o = o.replace(/'/g,"\&#39;");o = o.replace(/"/gi,"\&quot;");o = o.replace(/\n\n/gi,"<p>");o = o.replace(/\n/gi,"<br>");return o; }
String.prototype.fromHTML  = function() { var o = this;o = o.replace(/\&lt;/g,"\<");o = o.replace(/\&gt;/g,"\>");o = o.replace(/\&#124;/g,"\|");o = o.replace(/ \&nbsp;/g,"  ");o = o.replace(/\&#39;/g,"\'");o = o.replace(/\&#039;/g,"\'");o = o.replace(/\&quot;/g,"\"");return o;}
String.prototype.stripHTML = function() { var n = this.replace(/(<([^>]+)>)/ig,"");n = n.replace(/\r\n/g,"");n = n.replace(/\n/g,"");n = n.replace(/\r/g,"");n = n.replace("&nbsp;", "");n = n.replace(/^\s+|\s+$/g,"");if (n == '<>' || n == '>' || n == '<') n = '';return n;}

// main igaro functions
igaro = {

	lastclicked : null,

	sections : {

		separator : '»',

		selformatw : function(obj) {
			for(var i=0; i < $(obj).options.length; i++) {
				if ($(obj).options[i].value.length == 0) continue;
				$(obj).options[i].text = igaro.sections.format($(obj).options[i].value);
				$(obj).options[i].value = $(obj).options[i].value.toUpperCase();
			}
		},

		selformat : function(obj) {
			for(var i=0; i < $(obj).options.length; i++) {
				if ($(obj).options[i].value.length == 0) continue;
				var u = $(obj).options[i].value.split(' > ');
				$(obj).options[i].style.paddingLeft = ((u.length-1)*20)+'px';
				$(obj).options[i].text = u[u.length-1];
				$(obj).options[i].value = $(obj).options[i].value.toUpperCase();
			}
		},

		format : function(s) { return s.replace(/ \> /g,' '+igaro.sections.separator+' '); }
        },

        jsFileLoad : function(obj) {
                var e =  document.createElement('script');
                e.type = 'text/javascript';
                e.src= obj.src;
                if (obj.runner) {
			e.onload = obj.runner;
			e.onreadystatechange = function() { if (e.readyState == 'loaded') e.onload(); }
		}
                 $$('head')[0].appendChild(e);
        },

        currency : {
                validate : function(s,o) {
                                if (o && o.allowneg) return RegExp(/^-?\d+(\.\d{2})?$/).test(String(s).trim());
                                return RegExp(/^\d+(\.\d{2})?$/).test(String(s).trim());
                },

		decimalise : function(o) {
			o = o+'';
			if (o.indexOf('.') == -1) return o+'.00';
			if (o.substr(o.length-2,1) == '.') return o+'0';
			return o;
		}
        },

        upload : {

                progress : {

			handle : null,
			stop:false,
			id:null,
			aj:new ajaxer,

			start : function(obj) {
					igaro.upload.progress.stop=false;
					igaro.upload.progress.id = igaro.loading.create();
					igaro.upload.progress.handle = setTimeout("igaro.upload.progress.retrieve()", 1000);
			},

			retrieve : function() {
					if (igaro.upload.progress.stop) return;
					if (! igaro.cookie.get('session')) return;
					var upl = igaro.upload.progress.aj;
					upl.requestFile = 'upload_status.pl';
					upl.onError =  function(e) { }
					upl.onCompletion = function() {
							var i = upl.response.split('|');
							if (upl.response.length == 0 || igaro.upload.progress.stop || i[0] == i[1]) { igaro.upload.progress.end(); return; }
							var j = ((i[1]/i[0])*100).toFixed(2);
							igaro.upload.progress.handle = setTimeout("igaro.upload.progress.retrieve()", 1500);
							igaro.loading.update(igaro.upload.progress.id,j);
					}
					upl.runAJAX();
			},

			end : function() {
					clearTimeout(igaro.upload.progress.handle);
					igaro.upload.progress.stop=true;
					igaro.loading.remove(igaro.upload.progress.id);
			}
                }
        },

	url : {
		getparam : function(name) {
			name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
			var regexS = "[\\?;]"+name+"=([^;#]*)";
			var regex = new RegExp(regexS);
			var results = regex.exec(window.location.href);
			return (results == null)? '':results[1];
		}
	},
	
	object : {
		clone : function(s) {
			for (i in s) {
				this[i] = (typeof s[i] == 'source')? new cloneObject(s[i]) : s[i];
			}
		}
	},

	// generates page index for navigation
	pages : function(currentpage,itemsperpage,items,u) {
		var pagestodisplay = 10; // probably wont need changing
		if (currentpage < 1) currentpage = 1;
		items--;
		var lastpage = Math.floor(items/itemsperpage) + 1;
		if (currentpage > lastpage) currentpage = 1;
		var buffer='';
		// for first page
		if (currentpage == 1) buffer += '<b>';
                var url = (u)? u : _winloc;
		if (! u || (u && u.indexOf(/\<INSERTPAGE\>/) == -1)) { url = url.replace(/\;pg\=\d\;/,';'); url = url.replace(/\;pg\=\d/,''); url += ';pg=<INSERTPAGE>'; }
		var t = url.replace(/\<INSERTPAGE\>/,1);
		buffer += (currentpage == 1)? 1 : '<a href="'+t+'">1</a>';
		if (currentpage == 1) buffer += '</b>';
		// where should we start and end?
		var start = currentpage - Math.floor(pagestodisplay/2);
		var end = currentpage + Math.floor(pagestodisplay/2);
		// start outside range?
		if (start < 2) { end += 2-start; start=2; }
		// end outside range?
		if (end >= lastpage) { start = lastpage - (pagestodisplay+1); end = lastpage-1; }
		// recheck start
		if (start < 2) start = 2;
		// current page long way from the first page
		if (start > 2) buffer += '&nbsp;...';
		//generate numbers
		for(var i=start; i <= end && i <= start + pagestodisplay; i++) {
			buffer += '&nbsp;';
			var t = url.replace(/\<INSERTPAGE\>/,i);
			buffer += (currentpage == i)? '<b>'+i+'</b>' : '<a href="'+t+'">'+i+'</a>';
		}
		// current page long way from the first page
		if (end < lastpage - 1) buffer += '&nbsp;...';
		// always show last page when greater than 1
		if (lastpage > 1) {
			buffer += '&nbsp;';
			if (currentpage == lastpage) buffer += '<b>';
			var t = url.replace(/\<INSERTPAGE\>/,lastpage);
			buffer += (currentpage == lastpage)? lastpage : '<a href="'+t+'">'+lastpage+'</a>';
			if (currentpage == lastpage) buffer += '</b>';
		}
		return buffer;
	},

	date : {
		strip : function(obj) { obj = obj.replace(/\:/g,""); obj = obj.replace(/\s/g,""); obj = obj.replace(/\-/g,""); return obj; },
		formattodayshort : 'Today, <HOUR_12>:<MINUTE><AM_PM>',
		formattodaylong : 'Today, <HOUR_12>:<MINUTE><AM_PM>',
		formattoday : 'Today, <HOUR_12>:<MINUTE><AM_PM>',
		formatyesterdayshort : 'Yesterday, <HOUR_12>:<MINUTE><AM_PM>',
		formatyesterdaylong : 'Yesterday, <HOUR_12>:<MINUTE><AM_PM>',
		formatyesterday : 'Yesterday, <HOUR_12>:<MINUTE><AM_PM>',
		formatnormallong : '<DAY> <MONTH_NAME> <YEAR>, <HOUR_12>:<MINUTE><AM_PM>',
		formatnormalshort : '<DAY> <MONTH_NAME_SHORT> <YEAR_SHORT>, <HOUR_12>:<MINUTE><AM_PM>',
		formatnormal : '<DAY> <MONTH_NAME_SHORT> <YEAR>, <HOUR_12>:<MINUTE><AM_PM>',
		formatpm : 'pm',
		formatam : 'am',
		formatmonths : new Array('January','Feburary','March','April','May','June','July','August','September','October','November','December'),
		formatmonthsshort : new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),
		now : function() {
			var d = new Date();
			var s = d.getUTCFullYear();
			if (d.getUTCMonth()+1 < 10) s += '0';
			s += (d.getUTCMonth()+1);
			if (d.getUTCDate() < 10) s += '0';
			s += d.getUTCDate();
			if (d.getUTCHours() < 10) s += '0';
			s += d.getUTCHours();
			if (d.getUTCMinutes() < 10) s += '0';
			s += d.getUTCMinutes();
			if (d.getUTCSeconds() < 10) s += '0';
			s += d.getUTCSeconds();
			return s;
		},
		format : function(obj) {
			if (obj.datetime.length < 5) return '???';
			obj.datetime = igaro.date.strip(obj.datetime);
			var year = parseInt(obj.datetime.substr(0,4));
			var month = parseInt(obj.datetime.substr(4,2),10);
			var day = parseInt(obj.datetime.substr(6,2),10);
			var hour = parseInt(obj.datetime.substr(8,2),10);
			var min = parseInt(obj.datetime.substr(10,2),10);
			var sec = parseInt(obj.datetime.substr(12,2),10);
			var today = new Date();
			var offset = today.getTimezoneOffset();
			if (! obj.nooffset && offset != 0) {
				if (offset < 0) { sec -= offset*60; }
				else { sec += offset*60; }
				if (sec < 0) { while (sec < 0) { sec += 60; min--; }}
				else { while (sec >= 60) { sec -= 60; min++; }}
				if (min < 0) { while (min < 0) { min += 60; hour--; }}
				else { while (min >= 60) { min -= 60; hour++; }}
				if (hour < 0) { hour += 24; day--; }
				else if (hour >= 24) { hour -= 24; day++; }
				//january, march, may, july, august, october, december :31 :: april, june, september, november: 30
				if (day == 0) {
					month--;
					if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 0) { day = 31; }
					else if (month == 4 || month == 6 || month == 9 || month == 11) { day = 30; }
					else if (igaro.date.isleapyear(year) && month == 2) { day = 29; }
					else { day = 28; }
				} else if (((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day == 32) || ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) || ((igaro.date.isleapyear(year) && month==2 && day == 30) || (! igaro.date.isleapyear(year) && month==2 && day == 29))) {
					month++; day=1;
				} else { day += 0; }
				if (month == 0) { year--; month= 12; }
				else if (month == 13) { year++; month=1; }
			}

			var t='';
			//if day is today display 'today' rather than date
			if (! obj.nospecday && year == today.getFullYear() && month == today.getMonth() && day == today.getDate()) {
				if (obj.type == 'short') { t = igaro.date.formattodayshort; }
				else if (obj.type == 'long') { t = igaro.date.formattodaylong; }
				else { t = igaro.date.formattoday; }
			//yesterday
			} else if (! obj.nospecday && (year == today.getFullYear() && month == today.getMonth() && day == today.getDate() - 1)  ||  (year == today.getFullYear() -1 && month == 12 && today.getMonth() == 1 && day == 31 && today.getDate() == 1)  ||  (year == today.getFullYear() && today.getDate() == 1 && (((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day == 32) || ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) || ((igaro.date.isleapyear(year) && month==2 && day == 30) || (! igaro.date.isleapyear(year) && month==2 && day == 29))))   ) {
				if (obj.type == 'short') { t = igaro.date.formatyesterdayshort; }
				else if (obj.type == 'long') { t = igaro.date.formatyesterdaylong; }
				else { t = igaro.date.formatyesterday; }
			//other day
			} else {
				if (obj.type == 'short') { t = igaro.date.formatnormalshort; }
				else if (obj.type == 'long') { t = igaro.date.formatnormallong; }
				else { t = igaro.date.formatnormal; }
			}
			// do AM or PM
			if (hour >= 12) {
				t = t.replace(/<AM_PM>/g, igaro.date.formatpm);
			} else {
				t = t.replace(/<AM_PM>/g, igaro.date.formatam);
			}
			// do day and place a zero when less than 10
			t = t.replace(/<DAY>/g, day);
			if (day < 10) day = '0' + day;
			t = t.replace(/<DAY_WITHERO>/g, day);
			// do month names and do a short 3 chr name as well
			t = t.replace(/<MONTH_NAME>/g, igaro.date.formatmonths[month-1]);
			t = t.replace(/<MONTH_NAME_SHORT>/g, igaro.date.formatmonthsshort[month-1]);
			t = t.replace(/<YEAR>/g, year);
			year = year+'';
			t = t.replace(/<YEAR_SHORT>/g, year.substr(2));
			if (min < 10) min = '0' + min;
			t = t.replace(/<MINUTE>/g, min);
			// do seconds
			if (sec < 10) sec = '0' + sec;
			t = t.replace(/<SECOND>/g, sec);
			// do hour and 24hr format
			if (hour < 10) hour = '0' + hour;
			t = t.replace(/<HOUR_24>/g, hour);
			if (hour >= 13) hour -= 12;
			t = t.replace(/<HOUR_12>/g, hour);
			return t;
		},

		isleapyear : function(obj) {
			var t0 = obj / 4; var t1  = obj / 100; var t2  = obj / 400;
			t0 += '';t1 += '';t2 += '';
			//Years evenly divisible by four are normally leap years, except...
			if (t0.match('.') == null) {
				// Years evenly divisible by 100 are not leap years, except...
				if (t1.match('.') == null) {
					//Years evenly divisible by 400 are leap years.
					if (t2.match('.') == null) return 1;
				}
			}
			return 0;
		},

		daysinmonth : function (cYear,cMonth) {
			// january, march, may, july, august, october, december :31
			if (cMonth == 1 || cMonth == 3 || cMonth == 5 || cMonth == 7 || cMonth == 8 || cMonth == 10 || cMonth == 12) {
				return 31;
			// april, june, september, november: 30
			} else if (cMonth == 4 || cMonth == 6 || cMonth == 9 || cMonth == 11) {
				return 30;
			// february :28/29
			} else if (cMonth == 2) {
				var tG = cYear / 4; //Years evenly divisible by four are normally leap years
				tG = tG.toString();
				var tF = cYear / 100; //Years evenly divisible by 100 are not leap years
				tF = tF.toString();
				var tE = cYear / 400; //Years evenly divisible by 400 are leap years.
				tE = tE.toString();
				if (tG.indexOf('.') == -1 && tF.indexOf('.') && tE.indexOf('.')) { return 29; }
				return 28;
			}
		},

		combo : {

			store : new Array(),

			update : function(grp) {
				var obj;
				for(var i=0; i<igaro.date.combo.store.length;i++) {
					if (grp == igaro.date.combo.store[i].group) {
						obj = igaro.date.combo.store[i];
						break;
					}
				}
				var isUpperBoundary = false; var isLowerBoundary = false;
				// write years
				if ($(obj.group+'_year')) {
					if ($(obj.group+'_year').selectedIndex > -1) obj.defaults.year = $(obj.group+'_year').options[$(obj.group+'_year').selectedIndex].value;
					if (obj.defaults.year == obj.from.year) isLowerBoundary = true;
					if (obj.defaults.year == obj.to.year) isUpperBoundary = true;
					$(obj.group+'_year').options.length = null;
					if (obj.space) $(obj.group+'_year').options[$(obj.group+'_year').options.length] = new Option('','');
					for(var i=obj.from.year;i<=obj.to.year;i++) {
						$(obj.group+'_year').options[$(obj.group+'_year').options.length] = new Option(i,i);
						if (i == obj.defaults.year) $(obj.group+'_year').options[$(obj.group+'_year').options.length-1].selected = true;
					}
					if (obj.from.year) $(obj.group+'_year').from = obj.from.year;
					if (obj.to.year) $(obj.group+'_year').from = obj.to.year;

				}
				// write months
				if ($(obj.group+'_month')) {
					if ($(obj.group+'_month').selectedIndex > -1) obj.defaults.month = $(obj.group+'_month').options[$(obj.group+'_month').selectedIndex].value;
					$(obj.group+'_month').options.length = null;
					if (obj.space) $(obj.group+'_month').options[$(obj.group+'_month').options.length] = new Option('','');
					var from = obj.from.month; var to = obj.to.month;
					if (! from || (from && ! isLowerBoundary)) from = 1;
					if (! to || (to && ! isUpperBoundary)) to = 12;
					if (isLowerBoundary && obj.defaults.month > from) isLowerBoundary = false;
					if (isUpperBoundary && obj.defaults.month < to) isUpperBoundary = false;
					for(var i=from;i<=to;i++) {
						$(obj.group+'_month').options[$(obj.group+'_month').options.length] = new Option(igaro.date.formatmonths[i-1],i);
						if (i == obj.defaults.month) $(obj.group+'_month').options[$(obj.group+'_month').options.length-1].selected = true;
					}
				}
				// write days
				if ($(obj.group+'_day')) {
					if ($(obj.group+'_day').selectedIndex > -1) obj.defaults.day = $(obj.group+'_day').options[$(obj.group+'_day').selectedIndex].value;
					$(obj.group+'_day').options.length = null;
					if (obj.space) $(obj.group+'_day').options[$(obj.group+'_day').options.length] = new Option('','');
					var from = obj.from.day; var to = obj.to.day;
					if (! from || (from && ! isLowerBoundary)) from = 1;
					if (! to || (to && ! isUpperBoundary)) to = ($(obj.group+'_month') && $(obj.group+'_month').options[$(obj.group+'_month').selectedIndex].value > 0 && $(obj.group+'_year') && $(obj.group+'_year').options[$(obj.group+'_year').selectedIndex].value > 0)? igaro.date.daysinmonth($(obj.group+'_year').options[$(obj.group+'_year').selectedIndex].value,$(obj.group+'_month').options[$(obj.group+'_month').selectedIndex].value) : 31;
					if (isLowerBoundary && obj.defaults.day > from) isLowerBoundary = false;
					if (isUpperBoundary && obj.defaults.day < to) isUpperBoundary = false;
					for(var i=from;i<=to;i++) {
						var j = (i < 10)?'0'+i:i;
						$(obj.group+'_day').options[$(obj.group+'_day').options.length] = new Option(j,i);
						if (i == obj.defaults.day) $(obj.group+'_day').options[$(obj.group+'_day').options.length-1].selected = true;
					}
				}
				// write hours
				if ($(obj.group+'_hour')) {
					if ($(obj.group+'_hour').selectedIndex > -1) obj.defaults.hour = $(obj.group+'_hour').options[$(obj.group+'_hour').selectedIndex].value;
					$(obj.group+'_hour').options.length = null;
					if (obj.space) $(obj.group+'_hour').options[$(obj.group+'_hour').options.length] = new Option('','');
					var from = obj.from.hour; var to = obj.to.hour;
					if (! from || (from && ! isLowerBoundary)) from = 0;
					if (! to || (to && ! isUpperBoundary)) to = 24;
					if (isLowerBoundary && obj.defaults.hour > from) isLowerBoundary = false;
					if (isUpperBoundary && obj.defaults.hour < to) isUpperBoundary = false;
					for(var i=from;i<=to;i++) {
						var j = (i < 10)?'0'+i:i;
						$(obj.group+'_hour').options[$(obj.group+'_hour').options.length] = new Option(j,i);
						if (i == obj.defaults.hour && obj.defaults.hour.length) $(obj.group+'_hour').options[$(obj.group+'_hour').options.length-1].selected = true;
					}
				}
				// write minutes
				if ($(obj.group+'_min')) {
					if ($(obj.group+'_min').selectedIndex > -1) obj.defaults.min = $(obj.group+'_min').options[$(obj.group+'_min').selectedIndex].value;
					$(obj.group+'_min').options.length = null;
					if (obj.space) $(obj.group+'_min').options[$(obj.group+'_min').options.length] = new Option('','');
					var from = obj.from.min; var to = obj.to.min;
					if (! from || (from && ! isLowerBoundary)) from = 0;
					if (! to || (to && ! isUpperBoundary)) to = 60;
					if (isLowerBoundary && obj.defaults.min > from) isLowerBoundary = false;
					if (isUpperBoundary && obj.defaults.min < to) isUpperBoundary = false;
					for(var i=from;i<=to;i++) {
						var j = (i < 10)?'0'+i:i;
						$(obj.group+'_min').options[$(obj.group+'_min').options.length] = new Option(j,i);
						if (i == obj.defaults.min && obj.defaults.min.length) $(obj.group+'_min').options[$(obj.group+'_min').options.length-1].selected = true;
					}
				}
				// write seconds
				if ($(obj.group+'_sec')) {
					if ($(obj.group+'_sec').selectedIndex > -1) obj.defaults.sec = $(obj.group+'_sec').options[$(obj.group+'_sec').selectedIndex].value;
					$(obj.group+'_sec').options.length = null;
					if (obj.space) $(obj.group+'_sec').options[$(obj.group+'_sec').options.length] = new Option('','');
					var from = obj.from.sec; var to = obj.to.sec;
					if (! from || (from && ! isLowerBoundary)) from = 0;
					if (! to || (to && ! isUpperBoundary)) to = 60;
					if (isLowerBoundary && obj.defaults.sec > from) isLowerBoundary = false;
					if (isUpperBoundary && obj.defaults.sec < to) isUpperBoundary = false;
					for(var i=from;i<=to;i++) {
						var j = (i < 10)?'0'+i:i;
						$(obj.group+'_sec').options[$(obj.group+'_sec').options.length] = new Option(j,i);
						if (i == obj.defaults.sec && obj.defaults.sec.length) $(obj.group+'_sec').options[$(obj.group+'_sec').options.length-1].selected = true;
					}
				}

			},

			check : function(obj) {
				var complete = 0; var controls = 0;
				var a = ['year','month','day','hour','min','sec']
				for(var i=0;i<a.length;i++) {
					if ($(obj+'_'+a[i])) {
						if ($(obj+'_'+a[i]).options[$(obj+'_'+a[i]).selectedIndex].value.length) complete++;
						controls++;
					}
				}
				if (complete == 0) return 1;
				if (complete == controls) return 2;
				return 0;
			},

			compare : function(start,finish) {
				var syear = ($(start+'_year'))?  $(start+'_year').options[$(start+'_year').selectedIndex].value:0;
				var smonth = ($(start+'_month'))?  $(start+'_month').options[$(start+'_month').selectedIndex].value:0;
				var sday = ($(start+'_day'))?  $(start+'_day').options[$(start+'_day').selectedIndex].value:0;
				var shour = ($(start+'_hour'))?  $(start+'_hour').options[$(start+'_hour').selectedIndex].value:0;
				var smin = ($(start+'_min'))?  $(start+'_min').options[$(start+'_min').selectedIndex].value:0;
				var ssec = ($(start+'_sec'))?  $(start+'_sec').options[$(start+'_sec').selectedIndex].value:0;
				var fyear = ($(finish+'_year'))?  $(finish+'_year').options[$(finish+'_year').selectedIndex].value:0;
				var fmonth = ($(finish+'_month'))?  $(finish+'_month').options[$(finish+'_month').selectedIndex].value:0;
				var fday = ($(finish+'_day'))?  $(finish+'_day').options[$(finish+'_day').selectedIndex].value:0;
				var fhour = ($(finish+'_hour'))?  $(finish+'_hour').options[$(finish+'_hour').selectedIndex].value:0;
				var fmin = ($(finish+'_min'))?  $(finish+'_min').options[$(finish+'_min').selectedIndex].value:0;
				var fsec = ($(finish+'_sec'))?  $(finish+'_sec').options[$(finish+'_sec').selectedIndex].value:0;
				if (syear == fyear && smonth == fmonth && sday == fday  && shour == fhour && smin == fmin  && ssec == fsec) return '=';
				if (syear < fyear || (syear == fyear && smonth < fmonth) || (syear == fyear && smonth == fmonth && sday < fday) || (syear == fyear && smonth == fmonth && sday == fday && shour < fhour) || (syear == fyear && smonth == fmonth && sday == fday && shour == fhour && smin < fmin) || (syear == fyear && smonth == fmonth && sday == fday && shour == fhour && smin == fmin && ssec < fsec)) return '<';
				return '>';
			},

			init : function(obj) {
				for (var i=0;i < igaro.date.combo.store.length;i++) {
					// remove previous stores by same name
					if (igaro.date.combo.store[i].group == obj.group) { igaro.date.combo.store.splice(i,1); i--; }
				}
				igaro.date.combo.store[igaro.date.combo.store.length] = obj;
				igaro.date.combo.update(obj.group);
			}
		}
	},

	cookie : {
		remove : function(name) { document.cookie = name + '=\'\';path=/;expires=Sun, 17-Jan-1980 00:00:00 GMT;\n'; },
		set : function (name, value, nopersist) {
			document.cookie = (nopersist)? name+'='+escape(value)+';path=/;\n' : name+'='+escape(value)+';path=/;expires=Sun, 17-Jan-2038 00:00:00 GMT;\n';
		},
		get : function (name,def) {
			name = name + '='; var j = -1; var done = false; var temp;
			while ((j < document.cookie.length) && done == false) { j++;
				if (document.cookie.substring(j, j + name.length) != name) continue;
				var k = 0; var x = 'a';
				while (x != '' && x != ';') { k++; x = document.cookie.substring(j + name.length + k, j + name.length + k - 1); }
				temp = unescape(document.cookie.substring(j + + name.length, j + name.length + k - 1));
				done = true;
			}
			if (temp) return temp; return def;
		}
	},

	loading : {

		n: 0,

		create : function(i) {
			igaro.loading.n++;
			var e = document.createElement('div');
			e.id = 'loadingprogress'+igaro.loading.n;
			e.className = 'igaro_ajax_progress';
			e.style.position = 'absolute';
			if (igaro.lastclicked == null) return null;
			var obj = igaro.lastclicked;
			var curleft = 0;
			var curtop = (obj.type=='select-one')? obj.offsetHeight : obj.clientHeight;
			if(obj.offsetParent) {
			    while(1) {
			      curleft += obj.offsetLeft;
			      curtop += obj.offsetTop;
			      if(!obj.offsetParent) break;
			      obj = obj.offsetParent;
			    }
			} else if(obj.x) {
			    curleft += obj.x; curtop += obj.y;
			}
			e.style.top = curtop+'px';
			e.style.left = curleft+'px';
			window.document.body.appendChild(e);
			igaro.loading.update(igaro.loading.n,0);
			return igaro.loading.n;
		},

		remove : function(i) { if (! $('loadingprogress'+i)) return; window.document.body.removeChild($('loadingprogress'+i)); },

		update : function(id,i) { if ($('loadingprogress'+id)) $('loadingprogress'+id).innerHTML = i+'%'; }
	},

	ajax : {

		evalscripts : function(id) {
			var lists = $$('SCRIPT',$(id));
			for (var i=0; i < lists.length; i++) {
                                try {
                                	eval(lists[i].innerHTML);
                                } catch (e) {
                                        var r = e;
                                        if (e.description) r += ': '+e.description;
                                        if (e.lineNumber) r += ' - '+e.lineNumber;
                			igaro.error(r);
                                }
                        }
		},

		loadurl : function(url) {
			igaro.frm.errorbox.off();
			var index = ajax.length;
			ajax[index] = new ajaxer();
			ajax[index].requestFile = url;
			ajax[index].onLoading = function() { }
			ajax[index].runAJAX();
			return false;
		},

		setContentHTML : function(html) {
			$('igaro_content').innerHTML = html;
			igaro.ajax.evalscripts('igaro_content');
                        igaro.credentials.hide();
                        igaro.captcha.hide();
		}
	},

	tabs : {
		toggle : function(tab) {
			if ($('tab_' + tab).className == 'disabledtab') return;
                        $('tab_' + tab).blur();
			var tabgrp = tab.substr(0,tab.lastIndexOf('_'));
			var lists = $$('DIV');
			igaro.frm.errorbox.off();
			igaro.frm.listhelper.off();
			if (document.activeElement) { document.activeElement.blur(); }
			// manage tabs
			for (var i=0; i < lists.length; i++) {
				// not part of this group
				if (lists[i].id.substr(0, 4+tabgrp.length) != 'tab_'+tabgrp) continue;
				// part of a sub group
				if (lists[i].id.substr(tabgrp.length+5).lastIndexOf('_') > -1) continue;
				// is the groups tab
				if (lists[i].id == 'tab_' + tabgrp) continue;
				// bling tab
				if (lists[i].id == 'tab_'+tab) { lists[i].className = 'activetab'; }
				else if (lists[i].className != 'disabledtab') { lists[i].className = 'inactivetab'; }
			}
			// manage containers
			var found=false;
			for (var i=0; i < lists.length; i++) {
				// not part of this group
				if (lists[i].id.substr(0, 8+tabgrp.length) != 'tabcont_'+tabgrp) continue;
				// part of a sub group
				if (lists[i].id.substr(tabgrp.length+9).lastIndexOf('_') > -1) continue;
				// is the groups container
				if (lists[i].id == 'tabcont_' + tabgrp) continue;
				// show/hide container
				if (lists[i].id == 'tabcont_'+tab) {
                                                $(lists[i].id).style.display = '';
                                                found = true;
                                                var obs = $$('*', $(lists[i].id));
                                                detectobj : for(var x=0; x < obs.length; x++) {
                                                                switch(obs[x].type) {
                                                                                case "text": case "textarea": case "button": case "reset": case "submit": case "file": case "password": case "radio":case "checkbox":case "select-one":case "select-multiple":obs[x].focus(); break detectobj;
                                                                }
                                                }
                                                continue;
                                } else {
                                                $(lists[i].id).style.display = 'none';
                                }
			}
			// when no container, show common tab if avail
			if ($('tabcont_'+tabgrp+'_common')) {
				if (found == false) {
                                                $('tabcont_'+tabgrp+'_common').style.display = '';
                                                var obs = $$('*', $('tabcont_'+tabgrp+'_common'));
                                                detectobj : for(var x=0; x < obs.length; x++) {
                                                                switch(obs[x].type) {
                                                                                case "text": case "textarea": case "button": case "reset": case "submit": case "file": case "password": case "radio":case "checkbox":case "select-one":case "select-multiple":obs[x].focus(); break detectobj;
                                                                }
                                                }
                                 } else {
                                                $('tabcont_'+tabgrp+'_common').style.display = 'none';
                                }
			}
			return false; // needed for url cancel
		},

		reset : function(tab) {
			var lists = $$('DIV');
			for (var i=0; i < lists.length; i++) {
				if (lists[i].id.substr(0, 5+tab.length) != 'tab_'+tab+'_') continue;
				if (lists[i].id.substr(tab.length+5).lastIndexOf('_') > -1) continue;
				if (lists[i].className == 'activetab') lists[i].className = 'inactivetab';
			}
			for (var i=0; i < lists.length; i++) {
				if (lists[i].id.substr(0, 9+tab.length) != 'tabcont_'+tab+'_') continue;
				if (lists[i].id.substr(tab.length+9).lastIndexOf('_') > -1) continue;
				lists[i].style.display = 'none';
			}
			if ($('tabcont_'+tab+'_welcome')) $('tabcont_'+tab+'_welcome').style.display = '';
		},

		update : function(tab, html) {
			$('tabcont_' + tab).innerHTML = html;
			igaro.ajax.evalscripts('tabcont_' + tab);
		},

		isDisabled : function(tab) { return ($('tab_' + tab).className == 'disabledtab')? true:false; },
		hasFocus : function(tab) { return ($('tab_' + tab).className == 'activetab')? true : false; },
		disable : function(tab) { $('tab_' + tab).className = 'disabledtab'; },
		enable : function(tab) { if ($('tab_' + tab).className == 'disabledtab') $('tab_' + tab).className = 'inactivetab'; }
	},

	confirm : function(txt) { txt = txt.replace(/\<p\>/g,"\n\n"); txt = txt.replace(/\<br\>/g,"\n"); return confirm(txt); },
	error : function(txt) { txt = txt.replace(/\<p\>/g,"\n\n");txt = txt.replace(/\<br\>/g,"\n"); alert(txt); },
	notice : function(txt) { txt = txt.replace(/\<p\>/g,"\n\n");txt = txt.replace(/\<br\>/g,"\n"); alert(txt); },

	credentials : {
                on : false,
		hide : function() {
			$('igaro_credentials').innerHTML = null;
			$('igaro_credentials').style.display = 'none';
			if (igaro.captcha.on) { $('igaro_captcha').style.display=''; } else { $('igaro_content').style.display = ''; }
                        igaro.credentials.on = false;
		},
		load : function(html) {
		        $('igaro_credentials').innerHTML = html;
                        $('igaro_credentials').style.display = '';
			$('igaro_captcha').style.display = $('igaro_content').style.display = 'none';
                        igaro.credentials.on = true;
                        igaro.ajax.evalscripts('igaro_credentials');
		}
	},

	captcha : {
                byajax : null,
                on : false,
		hide : function() {
			$('igaro_captcha').innerHTML = null;
			$('igaro_captcha').style.display = 'none';
			if (igaro.credentials.on) { $('igaro_credentials').style.display=''; } else { $('igaro_content').style.display = ''; }
                        igaro.captcha.on = false;
		},
		load : function(html) {
		        igaro.captcha.byajax = true;
		        $('igaro_captcha').innerHTML = html;
                        igaro.ajax.evalscripts('igaro_captcha');
                        igaro.captcha.show();
		},
                show : function() {
			$('igaro_captcha').style.display = '';
			$('igaro_credentials').style.display = $('igaro_content').style.display = 'none';
                        igaro.captcha.on = true;
                }
	},

	frm : {

		select : {
			all : function(obj) { for(var i=0; i < $(obj).options.length; i++) { $(obj).options[i].selected = true; } }
		},

		errorbox : {
			visible : false,
			on : function(obj,txt) {
				var curleft = 0;
				var curtop = (obj.type=='select-one')? obj.offsetHeight : obj.clientHeight;
				if(obj.offsetParent) {
				    while(1) {
				      curleft += obj.offsetLeft;
				      curtop += obj.offsetTop;
				      if(!obj.offsetParent) break;
				      obj = obj.offsetParent;
				    }
				} else if(obj.x) {
				    curleft += obj.x; curtop += obj.y;
				}
				$('igaro_form_errorbox').style.left=curleft + 'px';
				$('igaro_form_errorbox').style.top=curtop + 'px';
				$('igaro_form_errorbox').style.visibility='visible';
				$('igaro_form_errorbox').innerHTML = txt;
				igaro.frm.errorbox.visible=true;
			},
			off : function() {
				$('igaro_form_errorbox').style.visibility = 'hidden'; igaro.frm.errorbox.visible=false;
			}
		},

		listhelper : {
			visible : false,
			on : function(obj,arr,url) {
				var curleft = curtop = 0;
				if (obj.offsetParent) {
					curleft = obj.offsetLeft + obj.offsetWidth - obj.clientWidth;
					curtop = obj.offsetTop + obj.clientHeight;
					if (obj.type=='select-one') curtop -= obj.offsetHeight;
					while (obj == obj.offsetParent) { curleft += obj.offsetLeft; curtop += obj.offsetTop; }
				}
				for(var i=0; i < arr.length; i++) {
					var turl = url;
					turl = turl.replace(/\^\^\^/,arr[i]);
					arr[i] = '<a href="javascript:null" onclick="'+turl+';igaro.frm.listhelper.off();return false">'+arr[i]+'</a>';
				}
				$('igaro_form_listhelper').style.left=curleft + 'px';
				$('igaro_form_listhelper').style.top=curtop + 'px';
				$('igaro_form_listhelper').style.visibility='visible';
				$('igaro_form_listhelper').innerHTML = arr.join('<br>');
				igaro.frm.listhelper.visible=true;
			},
			slowoff : function() { setTimeout('igaro.frm.listhelper.off()',200); },
			off : function() {
				$('igaro_form_listhelper').style.visibility = 'hidden'; igaro.frm.listhelper.visible=false;
			}
		},

		savestate : function(FormName) {
			for(var i=0; i<FormName.elements.length; i++) {
				if(FormName.elements[i].type=="checkbox" || FormName.elements[i].type=="radio") {
					FormName.elements[i].origvalue=FormName.elements[i].checked;
				} else if (FormName.elements[i].type=="select-one") {
					FormName.elements[i].origvalue=FormName.elements[i].selectedIndex;
				} else if (FormName.elements[i].type=="file" || FormName.elements[i].type=="text" || FormName.elements[i].type=="password" || FormName.elements[i].type=="textarea") {
					FormName.elements[i].origvalue=FormName.elements[i].value.trim();
				} else if(FormName.elements[i].type=="select-multiple") {
					var txt='';
					for (var k=0; k < FormName.elements[i].options.length; k++) {
						if (! FormName.elements[i].options[k].selected) continue;
						if (txt.length) txt += '\n';
						txt += FormName.elements[i].options[k].value;
					}
					FormName.elements[i].origvalue=txt;
				}
			}
		},

		checkstate : function(FormName) {
			for(var i=0; i<FormName.elements.length; i++) {
				if( (FormName.elements[i].type=="checkbox" || FormName.elements[i].type=="radio") && FormName.elements[i].origvalue != FormName.elements[i].checked) {
					return true;
				} else if (FormName.elements[i].type=="select-one" && FormName.elements[i].origvalue != FormName.elements[i].selectedIndex) {
					return true;
				} else if ((FormName.elements[i].type=="file" || FormName.elements[i].type=="text" || FormName.elements[i].type=="password" || FormName.elements[i].type=="textarea") && FormName.elements[i].origvalue != FormName.elements[i].value.trim()) {
					return true;
				} else if (FormName.elements[i].type=="select-multiple") {
					var txt='';
					for (var k=0; k < FormName.elements[i].options.length; k++) {
						if (! FormName.elements[i].options[k].selected) continue;
						if (txt.length) txt += '\n';
						txt += FormName.elements[i].options[k].value;
					}
 					if (FormName.elements[i].origvalue != txt) return true;
				}
			}
		}
	},

	init : function() {
		// check meets requirements
		if ($ && typeof XMLHttpRequest == 'undefined') $('igaro_browserincompat').style.display='';
                // konqueror wont work
                if (navigator.userAgent.toLowerCase().indexOf('konqueror') > -1) $('igaro_browserincompat').style.display='';
                var e = document.createElement('div');
		e.id = 'igaro_form_errorbox';
		$$('body')[0].appendChild(e);
		e = document.createElement('div');
		e.id = 'igaro_form_listhelper';
		$$('body')[0].appendChild(e);
		e = document.createElement('div');
		e.id = 'igaro_loading_com';
		$$('body')[0].appendChild(e);
	},

	logout : function() {
		igaro.cookie.remove('user');
		igaro.cookie.remove('session');
		window.location.href= '?';
	}

}

var ajax = new Array();

function ajaxer(file) {
	this.xmlhttp = null;
	this.resetData = function() {
		this.URLString = "";
  		this.execute = true;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
		this.loading=false;
		this.withForm=false;
		this.progress=null;
  	};
	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { }
  		this.onError = function(e) {  igaro.error(e);};
	};
	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};
	this.createAJAX = function() { this.xmlhttp = new XMLHttpRequest(); };
	this.sendForm = function(name) {
		this.withForm = true;
		for(var i=0; i<name.elements.length; i++) {
			if(! name.elements[i].disabled) {
				if(name.elements[i].type=="checkbox" && name.elements[i].checked) {
					this.setVar(name.elements[i].name, name.elements[i].checked);
				} else if(name.elements[i].type=="select-one") {
					if (! name.elements[i].selectedIndex) name.elements[i].selectedIndex = 0;
					this.setVar(name.elements[i].name, name.elements[i].options[name.elements[i].selectedIndex].value);
				} else if(name.elements[i].type=="select-multiple") {
					var txt='';
					for (var k=0; k < name.elements[i].options.length; k++) {
						if (! name.elements[i].options[k].selected) continue;
						if (txt.length) txt += '\n';
						txt += name.elements[i].options[k].value;
					}
					if (txt.length) this.setVar(name.elements[i].name, txt);
				} else if (name.elements[i].type=="hidden" || name.elements[i].type=="password" || name.elements[i].type=="text" || name.elements[i].type=="radio" || name.elements[i].type=="textarea") {
					this.setVar(name.elements[i].name,name.elements[i].value.trim());
				}
			}
		}
	};
	this.setVar = function(name, value){ this.vars[name] = Array(value,false); };
	this.runResponse = function() { eval(this.response); }
	this.runAJAX = function() {
		// prevents caching of URLString when using GET
		if (! this.withForm) this.setVar("rndval", new Date().getTime());
		// build string
		var urlstringtemp = new Array();
		for (key in this.vars) {
			var encoded = Array(encodeURIComponent(key), encodeURIComponent(this.vars[key][0]));
			delete this.vars[key];
			this.vars[encoded[0]] = Array(encoded[1], true);
			key = encoded[0];
			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		this.URLString += urlstringtemp.join(';');
		var self = this;
		if (! this.withForm) {
			var t = this.requestFile;
                        t += (this.requestFile.indexOf('?') > -1)? ';' : '?';
                        t += this.URLString;
			this.xmlhttp.open('GET', t, true);
		} else {
			this.xmlhttp.open('POST', this.requestFile, true);
			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.xmlhttp.setRequestHeader("Content-length", this.URLString.length);
			this.xmlhttp.setRequestHeader("Connection", "close");
		}
		this.xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		this.xmlhttp.onreadystatechange = function() {
			switch (self.xmlhttp.readyState) {
			case 1:
				self.onLoading();
				self.loading=true;
				igaro.loading.update(self.progress,60);
				break;
			case 2:
				igaro.loading.update(self.progress,75);
				self.onLoaded();
				break;
			case 3:
				igaro.loading.update(self.progress,90);
				self.onInteractive();
				break;
			case 4:
				self.loading=false;
				self.response = self.xmlhttp.responseText;
				self.responseXML = self.xmlhttp.responseXML;
				self.responseStatus[0] = self.xmlhttp.status;
				self.responseStatus[1] = self.xmlhttp.statusText;
				if (self.responseStatus[0] == '200') { igaro.loading.update(self.progress,99); if (self.execute) self.runResponse(); self.onCompletion(); igaro.loading.remove(self.progress); }
				else { igaro.loading.remove(self.progress); self.onError(self.responseStatus[0] + ' - ' + self.responseStatus[1] + ' - ' + self.response); }
				self.URLString = "";
				break;
			}
		};
		if (! this.withForm) { this.xmlhttp.send(null); }
		else { this.xmlhttp.send(this.URLString); }
		self.progress = igaro.loading.create();
	};
	this.reset();
	this.createAJAX();
};


// allows I.E to impersonate Mozilla oninput via the propertyName prop
function iefrmfix(obj,e) {
	 if (e.propertyName != 'value') return;
	 if (obj.oninput) eval(obj.oninput);
	 if (obj.onInput) eval(obj.onInput);
}


function getel(e){
	var targ;
	if(!e) e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	//if (targ.nodeType == 3) // defeat Safari bug
	igaro.lastclicked = targ.parentNode;
}

document.onclick = getel;

