// Copyright (C) 2006-2008 Daniel James
// All rights reserved
// Phone: 1800 888 981
// Email: info@netbreeze.com.au

function form2ele() {
}
form2ele.prototype.run = function() {
}
form2ele.prototype.abspos = function(ele) {
	var node = ele;
	var t=0,l=0;
	while (node) {
		t += node.offsetTop;
		l += node.offsetLeft;
		node = node.offsetParent;
	}
	return [t,l];
}
form2ele.prototype.cancelbubble = function(e) {
	if (!e) e = window.event;
	if (e.stopPropagation) e.stopPropagation();
	e.cancelBubble = true;
	e.returnValue = false;
	return false;
}
function form2dateinit(ele) {
	this.ele = ele;
	this.popup = false;
}
form2dateinit.prototype = new form2ele;

form2dateinit.prototype.go = function(months) {
	this.monthnumber = this.monthnumber + months;
	while (this.monthnumber < 1) {
		this.monthnumber += 12;
		this.yearnumber --;
	}
	while (this.monthnumber > 12) {
		this.monthnumber -= 12;
		this.yearnumber ++;
	}
	this.refresh();
}
form2dateinit.prototype.initpopup = function() {
	var obj = this;
	var button = function(jmp, txt) {
		var td = document.createElement('td');
		var div = document.createElement('div');
		div.className='btn';
		div.onclick=function(e) {
			obj.go(jmp);
			return obj.cancelbubble(e);
		}
		div.ondblclick=function() {
			e = window.event;
			if (e) {
				obj.go(jmp);
				return obj.cancelbubble(e);
			}
		}
		div.onselectstart = obj.cancelbubble;
		div.onmouseover = function() {
			this.className = 'btn over';
		}
		div.onmouseout = function() {
			this.className = 'btn';
		}
		div.onmousedown = function(e) {
			this.className = 'btn down';
			return obj.cancelbubble(e);
		}
		div.onmouseup = function() {
			this.className = 'btn over';
		}
		div.innerHTML = txt;
		td.appendChild(div);
		return td;
	}
	this.popup = document.createElement('div');
	this.popup.className = 'calendarpopup';
	this.popup.style.top = (this.t - 1) + 'px';
	this.popup.style.left = this.l + 'px';
	this.popup.style.width = (this.w - 1) + 'px';
	var tab = document.createElement('table');
	tab.className = 'nav';
	this.popup.appendChild(tab);
	tab.border = 0;
	tab.cellSpacing = 0;
	tab.cellPadding = 0;
	var tb = document.createElement('tbody');
	tab.appendChild(tb);
	var tr = document.createElement('tr');
	tb.appendChild(tr);
	tr.appendChild(button(-12, '◄◄'));
	tr.appendChild(button(-1, '◄'));
	var td = document.createElement('td');
	tr.appendChild(td);
	td.width = '50%';
	this.navlabel = document.createElement('div');
	this.navlabel.onselectstart = this.cancelbubble;
	this.navlabel.onmousedown = this.cancelbubble;
	td.appendChild(this.navlabel);
	this.navlabel.className = 'navlabel';
	this.navlabel.onclick=this.cancelbubble;
	tr.appendChild(button(1, '►'));
	tr.appendChild(button(12, '►►'));
	this.month = document.createElement('table');
	this.popup.appendChild(this.month);
	tab.border = 0;
	tab.cellSpacing = 0;
	tab.cellPadding = 0;
	this.month.className="month";
	tb = document.createElement('tbody');
	this.month.appendChild(tb);
	if (navigator.userAgent.indexOf("Firefox") != -1) {
		this.month.style.width = (this.w - 2) + 'px';
		this.month.style.margin = '1px 0px 0px 1px';
	}
	document.body.appendChild(this.popup);
}

form2dateinit.prototype.days = function(month, year) {
	var counts = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month == 2) {
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
			return 29;
		} else {
			return 28;
		}
	} else {
		return counts[month - 1];
	}
}
form2dateinit.prototype.monthof = function(month, year) {
	if (month < 1) {
		month += 12;
	} else if (month > 12) {
		month -= 12;
	}
	return month;
}
form2dateinit.prototype.yearof = function(month, year) {
	if (month < 1) {
		year --;
	} else if (month > 12) {
		year ++;
	}
	return year;
}
form2dateinit.prototype.didtodate = function(did) {
	var day = '0' + (did - Math.floor(did / 100) * 100);
	var mon = '0' + (Math.floor((did - Math.floor(did / 10000) * 10000) / 100));
	var yea = Math.floor(did / 10000);
	return day.substr(day.length - 2)+'/'+mon.substr(mon.length - 2)+'/'+yea;
}
form2dateinit.prototype.selectdate = function(did) {
	this.ele.value = this.didtodate(did);
	this.seldid = did;
	this.select.options[0] = null;
	this.select.options[0] = new Option(this.ele.value,0);
	this.select.selectedIndex = 0;
}
form2dateinit.prototype.killclick = function() {
	var obj = this;
	setTimeout(function() {
		document.body.onclick = obj.oldbodyclick;
		obj.popup.style.visibility='hidden';
		if (obj.hidfields) {
			var x,s;
			s = document.getElementsByTagName('select');
			for (x = 0; x < s.length; x ++) {
				s[x].style.visibility = 'visible';
			}
		}
	}, 50);
}
form2dateinit.prototype.createtd = function(num, month, year) {
	var obj = this;
	function over() {
		this.className = this.baseclassname + ' over';
	}
	function out() {
		this.className = this.baseclassname;
	}
	function click(e) {
		obj.killclick();
		obj.selectdate(this.did);
		return obj.cancelbubble(e);
	}
	var td = document.createElement('td');
	td.innerHTML = num;
	if ((month == this.monthnumber) && (year == this.yearnumber)) {
		td.onmouseover = over;
		td.onmouseout = out;
		td.onclick = click;
		td.did = year * 10000 + month * 100 + num;
		if (td.did == this.seldid) {
			td.className='c';
		} else {
			td.className='';
		}
	} else {
		td.className='nc';
		td.onclick = this.cancelbubble;
	}

	td.baseclassname = td.className;

	return td;
}
form2dateinit.prototype.refresh = function() {
	var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	var days = ['S','M','T','W','T','F','S'];
	var x;
	var td, tr, tb;
	this.navlabel.innerHTML = months[this.monthnumber-1] + ' ' + this.yearnumber;

	tb = document.createElement('tbody');
	tr = document.createElement('tr');
	for (x = 0; x < 7; x ++) {
		td = document.createElement('th');
		td.innerHTML=days[x];
		td.onclick=this.cancelbubble;
		td.onmousedown=this.cancelbubble;
		td.onselectstart=this.cancelbubble;
		tr.appendChild (td);
	}
	tb.appendChild(tr);

	tr = document.createElement('tr');
	var lastprevcount = this.days(this.monthof(this.monthnumber-1,this.yearnumber),this.yearof(this.monthnumber-1,this.yearnumber));
	var lastprev = new Date(this.yearof(this.monthnumber-1,this.yearnumber),this.monthof(this.monthnumber-1,this.yearnumber)-1,lastprevcount);
	var lastprevval = lastprev.getDay();
	var d, pd;
	var mm = this.monthof(this.monthnumber-1,this.yearnumber);
	var yy = this.yearof(this.monthnumber-1,this.yearnumber);
	for (d = 0; d <= lastprevval; d ++) {
		pd = lastprevcount - lastprevval + d;
		td = this.createtd(pd, mm, yy);
		tr.appendChild(td);
	}
	var lastday = this.days(this.monthof(this.monthnumber,this.yearnumber),this.yearof(this.monthnumber,this.yearnumber));
	var y = 0;

	for (x = 0; x < lastday; x ++) {
		if (d > 6) {
			d = 0;
			tb.appendChild(tr);
			tr = document.createElement('tr');
			y ++;
		}
		td = this.createtd(x + 1, this.monthnumber, this.yearnumber);
		tr.appendChild(td);
		d ++;
	}
	var end = 13;
	if (y == 5) end = 6;
	mm = this.monthof(this.monthnumber+1,this.yearnumber);
	yy = this.yearof(this.monthnumber+1,this.yearnumber);
	for (x = d; x <= end; x ++ ) {
		if (x == 7) {
			tb.appendChild(tr);
			tr = document.createElement('tr');
		}
		pd = x - d + 1;
		td = this.createtd(pd, mm, yy);
		tr.appendChild(td);
	}
	tb.appendChild(tr);

	td = this.month.getElementsByTagName('tbody');
	this.month.replaceChild(tb, td[0]);
}
form2dateinit.prototype.showpopup = function() {
	var obj = this;
	if (!this.popup) {
		this.initpopup();
	}
	if (this.popup.style.visibility != 'visible') {
		this.popup.style.visibility='visible';
		this.refresh();
		setTimeout(function() {
			var totalheight = typeof(window.scrollY) != 'undefined'?window.scrollY + window.innerHeight:(document.body.parentNode.clientHeight == 0?document.body.clientHeight + document.body.scrollTop:document.body.parentNode.clientHeight + document.body.parentNode.scrollTop);
			var totaltop = obj.abspos(obj.popup);
			totaltop = totaltop[0];
			var offby = obj.popup.offsetHeight + totaltop - totalheight;
			if (offby > 0) {
				window.scrollBy(0, offby);
			}
		}, 100);
		var obj = this;
		setTimeout(function() {
			obj.oldbodyclick = document.body.onclick;
			document.body.onclick = function() {
				obj.killclick();
			}
		}, 100);
	}
}
form2dateinit.prototype.selectclick = function() {
	this.hidfields = true;
	var x, s;
	s = document.getElementsByTagName('select');
	for (x = 0; x < s.length; x ++) {
		if (s[x] != this.select) {
			s[x].style.visibility = 'hidden';
		}
	}
	this.ele.style.visibility='visible';
	this.ele.focus();
	this.ele.style.visibility='hidden';
	this.showpopup();
}
form2dateinit.prototype.maskclick = function() {
	if (this.select.style.visibility == 'hidden') {
		return;
	}	
	this.hidfields = false;
	this.showpopup();
}
form2dateinit.prototype.run1 = function() {
	var tl,w,h;
	w = this.ele.offsetWidth;
	h = this.ele.offsetHeight;
	tl = this.abspos(this.ele);
	this.t = tl[0];
	this.l = tl[1];
	this.ele.style.visibility = 'hidden';
	this.select = document.createElement('select');
	this.mask = document.createElement('div');
	this.select.className = 'calendar';
	this.mask.className = 'calendarmask';
	this.select.style.top = tl[0]+'px';
	this.mask.style.top = tl[0]+'px';
	this.select.style.left = tl[1]+'px';
	this.mask.style.left = tl[1]+'px';
	this.select.style.width = w+'px';
	this.mask.style.width = w+'px';
	this.select.style.height = h+'px';
	this.mask.style.height = h+'px';
	document.body.appendChild(this.select);
	document.body.appendChild(this.mask);
	this.select.options[0] = new Option(this.ele.value,0);
	this.select.selectedIndex = 0;
	var obj = this;
	setTimeout(function() {
		obj.run2(w,h);
	},50);
}
form2dateinit.prototype.run2 = function(w,h) {
	this.w = this.select.offsetWidth;
	this.t += this.select.offsetHeight;
	this.mask.style.width = this.w + 'px';
	this.mask.style.height = this.select.offsetHeight + 'px';
	var obj = this;
	this.select.onclick = function() {
		return obj.selectclick();
	}
	this.mask.onclick = function() {
		return obj.maskclick();
	}
}
form2dateinit.prototype.run = function() {
	this.daynumber = Number(this.ele.value.substr(0,2));
	this.monthnumber = Number(this.ele.value.substr(3,2));
	this.yearnumber = Number(this.ele.value.substr(6));
	if (!(this.daynumber && this.monthnumber && this.yearnumber)) {
		var d = new Date();
		this.daynumber = d.getDate();
		this.monthnumber = 1 + d.getMonth();
		this.yearnumber = d.getFullYear();
	}
	this.seldid = this.yearnumber * 10000 + this.monthnumber * 100 + this.daynumber;
	var obj = this;
	setTimeout(function() {
		obj.run1();
	}, 300);
}

function form2obj(frm,foc) {
	this.form = document[frm];
	if (this.form[foc] && this.form[foc].focus) {
		this.form[foc].focus();
	}
	if (document.body.getAttribute) {
		this.form.form2 = this;
		this.form.onsubmit = function() {
			return this.form2.submithandle();
		}
		this.initele();
		this.formula();
	}
}
form2obj.prototype.initele = function() {
	var ele = this.form.elements;
	var x,ct,o;
	for (x = 0,ct = ele.length; x < ct; x ++) {
		if (init = ele[x].getAttribute('init')) {
			init = eval('form2'+init+'init');
			o = new init(ele[x]);
			o.run();
		}
	}
}
form2obj.prototype.formula = function() {
	var form = this.form;
	function number_format(num) {
		var str = new String(Math.round(num*100)/100);
		if (str.indexOf('.') == -1) {
			str+='.0';
		}
		str+='0';
		return '$'+str.substr(0, str.indexOf('.') + 3);
	}
	function number_unformat(str) {
		var stro = new String(str);
		return Number(stro.replace(/[^0-9.]/, ''));
	}
	form.updateelement = function(ele) {
		var formula = ele.getAttribute('formula');
		var depends = formula.split(/\s*(\+|-|\*|\/|\(|\)|=|\?|:|>|<)\s*/);
		var y;
		for (y = 0; y < depends.length; y++) {
			if (depends[y].match(/^[a-zA-Z]/)) {
				if (ele.form[depends[y]]) {
					var number1 = '';
					var number2 = '';
					var name = depends[y];
					if (ele.form[depends[y]][0] && (ele.form[depends[y]].nodeName != 'SELECT')) {
						var z;
						for (z = 0; z < ele.form[depends[y]].length; z ++) {
							if (ele.form[depends[y]][z].checked) {
								actual = ele.form[depends[y]][z];
							}
						}
					} else {
						actual = ele.form[depends[y]];
					}

					if (typeof(actual) == 'undefined') {
						return;
					}

					name = name.replace(/(\[|\])/g, "\\$1");
					var input = new RegExp(name);
					var myvalue;

					if (actual.nodeName == 'SELECT') {
						myvalue = actual.options[actual.selectedIndex].value;
					} else {
						myvalue = actual.value;
					}

					if (myvalue.match(/^\d+(\.\d+)?$/)) {
						number1 = 'Number(';
						number2 = ')';
					} else {
						myvalue = "'"+actual.value+"'";
					}

					if (ele.form[depends[y]].nodeName == 'SELECT') {
						formula = formula.replace(input, myvalue);
					} else if (ele.form[depends[y]][0]) { // collection
						formula = formula.replace(input, myvalue);
					} else if (actual.getAttribute('type') == 'checkbox') {
						formula = formula.replace(input, actual.checked?1:0);
					} else {
						formula = formula.replace(input, myvalue);
					}
				}
			}
		}
		if (ele.nodeName == 'DIV') {
			if (eval(formula)) {
				ele.style.display='block';
			} else {
				ele.style.display='none';
			}
		} else {
			ele.value = eval(formula);
			if (ele.updatecall) {
				if (ele.getAttribute('type') == 'checkbox') {
					ele.onclick();
				} else {
					ele.onchange();
				}
			}
		}
	}
	function updatecaller() {
		var x, extra;
		if (extra = this.getAttribute('onchangeextra')) {
			eval(extra);
		}
		for (x = 0; x < this.updatecall.length; x++) {
			this.form.updateelement(this.updatecall[x]);
		}
	}

	function hookup(target) {
		var formula = target.getAttribute('formula');
		if (formula) {
			var depends = formula.split(/\s*(\+|-|\*|\/|\(|\)|=|\?|:|>|<)\s*/);
			var y;
			for (y = 0; y < depends.length; y++) {
				if (depends[y].match(/^[a-zA-Z]/) && !depends[y].match(/^(number_format|number_unformat)$/)) {
					if (form[depends[y]]) {
						if (form[depends[y]][0] && (form[depends[y]].nodeName != 'SELECT')) {
							if (!form[depends[y]][0].updatecall) {
								var z;
								for (z = 0; z < form[depends[y]].length; z ++) {
									form[depends[y]][z].onclick = updatecaller;
									form[depends[y]][z].updatecall = new Array();
								}
							}
						} else {
							if (!form[depends[y]].updatecall) {
								if (form[depends[y]].getAttribute('type') == 'checkbox') {
									form[depends[y]].onclick = updatecaller;
								} else {
									form[depends[y]].onchange = updatecaller;
								}
								form[depends[y]].updatecall = new Array();
							}
						}
						if (form[depends[y]][0] && (form[depends[y]].nodeName != 'SELECT')) {
							var z;
							for (z = 0; z < form[depends[y]].length; z ++) {
								form[depends[y]][z].updatecall[form[depends[y]][z].updatecall.length] = target;
							}
						} else {
							form[depends[y]].updatecall[form[depends[y]].updatecall.length] = target;
						}
					} else {
						alert("Error. The element you referred to '"+depends[y]+"', does not exist");
					}
				}
			}
		}
	}

	var target = form.elements;
	var x;
	for (x = 0; x < target.length; x ++) {
		hookup(target[x]);
	}
	target = form.getElementsByTagName('div');
	for (x = 0; x < target.length; x ++) {
		if (target[x].getAttribute('formula')) {
			target[x].form = form;
			hookup(target[x]);
		}
	}

	target = form.elements;
	for (x = 0; x < target.length; x ++) {
		if (target[x].getAttribute('formula')) {
			form.updateelement(target[x]);
		}
	}
	target = form.getElementsByTagName('div');
	for (x = 0; x < target.length; x ++) {
		if (target[x].getAttribute('formula')) {
			form.updateelement(target[x]);
		}
	}
}
form2obj.prototype.submithandle = function() {
	var ele = this.form.elements;
	var ret = true, msg = '', foc = false;
	var len,x,vtype;
	var vali = {
		'text':[/.?/,'Please enter some text'],
		'number':[/^[0-9]*$/,'Please enter a number'],
		'spacenumber':[/^[0-9 ]*$/,'Please enter a number'],
		'phone':[/^(\+?[0-9 ]*(\(\d+\))?[0-9 ]+)?$/,'Please enter a phone number'],
		'money':[/^(\$?(\d{1,3}(\,\d\d\d)+|\d*)(\.\d\d)?)?$/,'Please enter a dollar amount'],
		'email':[/^([-a-zA-Z0-9_.+]+@[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)+)?$/,'Please enter a valid email address'],
		'url':[/^((https?\:\/\/|ftp\:\/\/|mailto\:)?[a-zA-Z0-9][-_a-zA-Z0-9]+(\.[a-zA-Z0-9][-_a-zA-Z0-9]+)+(\/[-_a-zA-Z0-9\&\%\?\:\/\=]*)?)?$/,'Please enter a valid URL (eg http://www.google.com/)'],
		'time':[/^((1[0-2]|0?[1-9]):[0-6][0-9] ?[apAP][mM]?)?$/,'Please enter a time (eg 7:00 pm)'],
		'date':[/^((0?[1-9]|[1-2]\d|3[0-1])[-\/\.](1[0-2]|0?[1-9])[-\/\.](19|20)?\d\d)?$/,'Please enter a date (eg 25/12/2010)'],
		'lat':[/^(-?([5-9]|[1-2]\d|3[0-4])\.\d+)?$/,'Please enter a decimal latitude and make sure it is in range. (eg -19.257106)'],
		'lng':[/^(1(3[5-9]|4\d|5[0-5])\.\d+)?$/,'Please enter a decimal longitude and make sure that it is in range. (eg 146.818371)']
	}
	for (x=0,len=ele.length; x<len; x++) {
		if (ele[x].getAttribute('compulsory') && !ele[x].value) {
			ret = false;
			msg += "\n"+ele[x].getAttribute('vlabel')+" Please fill in this field";
			if ((foc === false) && ((ele[x].style.visibility !== 'hidden') || (ele[x].hasAttribute('foc')))) {
				if(ele[x].getAttribute('foc')) {
					foc = this.form[ele[x].getAttribute('foc')];
				} else {
					foc = ele[x];
				}
			}
		} else {
			vtype = ele[x].getAttribute('vtype');
			if (vtype && vali[vtype] && !vali[vtype][0].test(ele[x].value)) {
				ret = false;
				msg += "\n"+ele[x].getAttribute('vlabel')+" "+vali[vtype][1];
				if ((foc === false) && (ele[x].style.visibility !== 'hidden')) {
					foc = ele[x];
				}
			}
		}
	}
	if (ret == false) {
		foc.focus();
		alert("There were errors on your form.\n"+msg);
	}
	return ret;
}
