function formEl(id, name, _default, _req, _errmsg, _origborder, loop) {
	this.id=id;
	this.name=name;
	this._default=_default;
	this._req=_req;
	this._errmsg = _errmsg;
	this._origborder=_origborder;
	this.errorborder = "1px solid red";
	this.value;
	this.clickRemove;
	this.loop=loop;
}

formEl.prototype.chkVal = function(ob) {
	this.value=ob.value;
	var pass;
	if (!this._req) pass=true;
	else pass=(ob.value === this._req) ? false : true;
	if (pass) ob.style.border=this._origborder;
	else ob.style.border=this.errorborder;
	return pass;
}

formEl.prototype.clickBlur = function(ob) {
	if (ob.value=="") ob.value=this.clickRemove;
}

formEl.prototype.clickFocus = function(ob) {
	if (ob.value.trim()==this.clickRemove) ob.value="";
}

formEl.prototype.listener = function(id, tp, ob) {
	if (tp==="onfocus") this.clickFocus(ob);
	else if (tp==="onblur") this.clickBlur(ob);
	ob=tp=id=null;
}

function formOb(id, name, ajax, err, del) {
	this.id=id;
	this.name=name;
	this.formels={};
	this.del=del;
	this.ajax=ajax;
	this.err=err;
}

formOb.prototype.chkDel = function() {
	var ok=confirm("Are you sure you want to delete this?\nClicking ok will permanently delete this from the database. If you do not want to delete this, click cancel");
	return ok;
}

formOb.prototype.chkValues = function() {
	var pass=true;
	var err="";
	for (var i in this.formels) {
		if (!this.formels[i].chkVal(this.formels[i].name)) {
			pass=false;
			err += this.formels[i]._errmsg + "\n";
		}
	}
	/*if (!pass) {
	    if (this.err) {
		   var el=document.getElementById(this.err);
		   el.firstChild.innerHTML = err.replace(/\n/g, "<br>");
		   set.shDiv(el, true);
		}  else {
			alert(err.replace(/<br>/g, "\n"));
		}
	    el=null;
	}*/
	return pass;
}

formOb.prototype.addLoop = function() {
	
}

formOb.prototype.ajaxSubmit = function() {
	var data = ["ajaxid=" + this.formid];
	//make an ajax object of the data
	for (var i in this.formels) {
		data[data.length] = this.formels[i].name + "=" + this.formels[i].value;
	}
    mAjax.run(this.name, data.join("&"));
    data=null;
}
//this needs to manage all the form eventd
var fms = {
  'obs' : {},  
  'regF' : function(id, name, ajax, err, del) {
	  fms.obs[id] = new formOb(id, name, ajax, err, del);
  },
  'reg' : function(id, formid, name, def, req, errmsg, loop) {
	 fms.obs[formid].formels[id] = new formEl(id, name, def, req, errmsg, loop);
  },
  '_chkForm' : function(id) {
	  fms.obs[id].chkValues();
  }
}

function changeOptions(id) {
	this.id=id;
	this.vals={};
	this.currentval;
}

changeOptions.prototype.addValue=function(value, id, type) {
	if (!this.vals[value]) this.vals[value] = [];
	this.vals[value][this.vals[value].length] = {'id':id,'type':type};
}

changeOptions.prototype.listener = function(id, tp, ob) {
	var cOb;
	if (this.currentval) {
	}
	if (this.vals[ob.value]) {
	    this.currentval=ob.value;
		for (var i=0,n=this.vals[ob.value].length; i<n; i++) {
			cOb=this.vals[ob.value][i];
			if (cOb.type==="show") set.shDiv(cOb.id, true);
			else if (cOb.type==="hide") set.shDiv(cOb.id, false);
			cOb=null;
		}
	}
}

function htmlOb(id, type, loop) {
	this.id=id;
	this.type=type;
	this.loop=loop
	this.el;
}
	
htmlOb.prototype.load = function() {
	this.el=document.getElementById(this.id);
	var dv=document.createElement("div");
	dv.style.cssText = this.el.style.cssText;
	dv.className = this.el.className;
	if (!dv.style.backgroundColor) dv.style.backgroundColor="#ffffff";
	this.el.parentNode.insertBefore(dv, this.el);
	this.el.style.position="absolute";
	this.el.className = "";
	this.el.style.visibility="hidden";
	dv.innerHTML = this.el.value;
	dv.id = this.el.id + "_div";
	this.el=dv;
	hands.dynamic_reg(this.id, "onclick", this, dv, this.loop);
	hands.dynamic_reg(this.id, "onfocus", this, dv.firstChild, this.loop);
	dv=null;
}

htmlOb.prototype.editOn = function() {
	tEmenu.text=this.el.innerHTML;
	tEmenu.ob=this;
	sM.cob={'htmltext':this.el.innerHTML,'style':{}};
	sM.open("texteditor");
}

htmlOb.prototype.save = function() {
	var id=this.el.id.replace(/_div/, "");
	document.getElementById(id).innerHTML = this.el.innerHTML;
	id=null;
}

htmlOb.prototype.listener = function() {
	this.editOn();
}

var tEmenu = {
		'text' : '',
		'ob' : null,
	    'app' : function() {
	       tEmenu.ob.el.innerHTML = tE.eT(); 
	       tEmenu.ob.save();	  
	       alert("Saved");
	       sM.close();
	       document.onkeydown=null;
	    },
	   'htmlstart' : function() {
			 sM.fillOb.prop.htmltext=tE.eT();
			 document.getElementById("prop_htmltext").value=sM.fillOb.prop.htmltext;
	   },
	   'contentstart' : function() {
		set.shDiv("texteditorhint", false);
		var dv=document.getElementById("texteditordiv");
		var wid=tEmenu.ob.el.offsetWidth + 30;
		if (wid >= 742) wid = 742;
		dv.style.width= wid + "px";
		dv.style.height=(wM.she - 200) + "px";
		dv.style.overflow="scroll";
		dv.innerHTML = sM.fillOb.prop.htmltext ? sM.fillOb.prop.htmltext : sM.cob.htmltext;
		sM.fillOb.prop.htmltext="";
		tE.setup(dv);
		dv=e=id=elin=null;
	   }
}

var fhtml = {
	'obs' : {},
	'reg' : function(id, type) {
		fhtml.obs[id] = new htmlOb(id, type);
		_loadOb['html'] = fhtml.load;
	},
	'regDynamic' : function(id, type) {
		fhtml.obs[id] = new htmlOb(id, type);
		_loadOb['html'] = fhtml.load;
	},
	'load' : function() {
		for (var i in fhtml.obs) {
			fhtml.obs[i].load();
		}
	}
}

function _cal(date) {
	this.date=new Date();
	this.day=86400000;
	this.curmonth=null;
	this.month=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    this.setDateId=null;
}

_cal.prototype.runCal = function(ob) {
	this.close
	this.setDateId = ob.id;
	var off=set.offset(ob);
    var dv = document.createElement("div");
    dv.id="calendar";
    dv.style.position="absolute";
    dv.style.top=off[0] + "px";
    dv.style.left=(off[1] + off[2] + 15 ) + "px";
    dv.setAttribute("id", "calendar");
    dv.style.backgroundColor="#ffffff";
    dv.style.border="1px solid #000000";
    dv.style.zIndex="400";
    document.body.appendChild(dv);
    this.setCal(dv);
    listener.addHandle("calendar", this, "onclick");
    dv.onclick=listener.onclick;
    dv.style.cursor="pointer";
    dv=off=null;
}

_cal.prototype.setCal = function(el) {
  if (!el) el = document.getElementById("calendar");
  if (el==null) return false;
  var mon, htm, st, day, dt;
  this.curmonth = this.date.getMonth()
  mon = this.month[this.curmonth];
  htm = "<table style='font-size:12px;font-family:Arial;'><tr><td colspan='7' style='text-align:right;cursor:pointer;' id='closecal'>close</td></tr>";
  htm += "<tr><td colspan='7' style='text-align:center;'><span style='position:relative;float:left;'><a id='backyear' style='cursor:pointer;'>&lt;&lt;</a>&nbsp;|&nbsp;<a id='back' style='cursor:pointer;'>&lt;</a></span>&nbsp;|&nbsp;<span>" + mon + " - " + this.date.getFullYear() + "</span>&nbsp;|&nbsp;<span id='nextmonth' style='position:relative;float:right;'><a id='next' style='cursor:pointer;'>&gt;</a>&nbsp;|&nbsp;<a id='nextyear' style='cursor:pointer;'>&gt;&gt;</a></span></td></tr>";
  htm += "<tr><td id='sun'>Sun</td><td id='mon'>Mon</td><td id='tue'>Tue</td><td id='wed'>Wed</td><td id='thu'>Thu</td><td id='fri'>Fri</td><td id='sat'>Sat</td></tr>";
  var dto = new Date();
  dto.setDate(1);
  dto.setFullYear(this.date.getFullYear());
  dto.setMonth(this.date.getMonth());
  day = dto.getDay();
  st=0;
  while (dto.getMonth() == this.curmonth) {
		  for (var i=0; i<7; i++) {
		    if (i==0) htm += "<tr>";
		    if (i==day && st==0) st=1;
		    if (st==1) {
		     dt = dto.getDate();
		     htm += "<td id='date/" + dt + "' style='text-align:center;cursor:pointer;'>" + dt + "</td>";
		      dto.setTime(dto.getTime() + this.day);
			} else {
			 htm += "<td>&nbsp;</td>";
			}
		    if (i==6) htm += "<tr>";
		    if (dto.getMonth() != this.curmonth) st=2;
		  }
		}	 
	   el.innerHTML = htm;
	  mon=htm=st=day=dt=dto=tgs=el=null;
}	

_cal.prototype.setDate = function(time) {
	this.date.setTime(time);
}

_cal.prototype.postDate = function(id) {
   var day = id.split("/");
	var mon = this.date.getMonth() + 1;
	if (mon < 10) mon = "0" + mon;
	if (day[1] < 10) day[1] = "0" + day[1];
	document.getElementById(this.setDateId).value = day[1] + "/" + mon + "/" + this.date.getFullYear();
	day=mon=null;
	this.close();
}	

_cal.prototype.nextMonth = function() {
		var mon = this.date.getMonth() + 1;
		if (mon>11) {
			this.date.setFullYear(this.date.getFullYear() + 1);
			this.date.setMonth(0);
		} else {
		    this.date.setMonth(mon);
	    }
	  this.setCal();
}

_cal.prototype.nextYear = function() {
		this.date.setFullYear(this.date.getFullYear() + 1);
		this.setCal();
}

_cal.prototype.prevMonth = function() {
		var mon = this.date.getMonth() - 1;
		if (mon<0) {
		   this.date.setFullYear(this.date.getFullYear() - 1);
		   this.date.setMonth(11);
		} else {
		   this.date.setMonth(mon);
	    }
	  this.setCal();
}

_cal.prototype.prevYear = function() {
	this.date.setFullYear(this.date.getFullYear() - 1);
	this.setCal();
}

_cal.prototype.close = function () {
	var el=document.getElementById("calendar");
	el.onclick=null;
	listener.remListen("calendar");
	el.parentNode.removeChild(el);
}

_cal.prototype.listener = function(id, tp, ob, origob) {
	if (origob.id=="back") this.prevMonth();
	else if (origob.id=="next") this.nextMonth();
	else if (origob.id=="backyear") this.prevYear();
	else if (origob.id=="nextyear") this.nextYear();
	else if (origob.id=="closecal") this.close();
	else if (origob.id.indexOf("date") > -1) this.postDate(origob.id);
	else {
	  set.remEl("calendar");
	  this.runCal(ob);
	}
}

var cal = {
		'obs' : {},
		'reg' : function(id, loop) {
			cal.obs[id] = new _cal();
			hands.reg(id, "onclick", cal.obs[id], loop);
		},
		'regDynamic' : function(id) {
			cal.obs[id] = new _cal();
			hands.dynamic_reg(id, "onclick", cal.obs[id]);
		}
}

function clearClick(origvalue) {
	this.origvalue=origvalue;
}

clearClick.prototype.clear=function(el) {
	if (el.value==this.origvalue) el.value="";
}

clearClick.prototype.restore=function(el) {
	if (!el.value) el.value=this.origvalue;
}

clearClick.prototype.listener=function(id, tp, ob, origob) {
	if (tp==="onfocus") this.clear(ob);
	else if (tp==="onblur") this.restore(ob);
}

var clC = {
		'obs' : {},
		'reg' : function(id, origvalue, loop) {
			clC.obs[id] = new clearClick(origvalue);
			hands.reg(id, "onfocus", clC.obs[id], loop);
			hands.reg(id, "onblur", clC.obs[id], loop);
		},
		'regDynamic' : function(id, origvalue, loop) {
			clC.obs[id] = new clearClick(origvalue);
			hands.dynamic_reg(id, "onfocus", clC.obs[id]);
			hands.dynamic_reg(id, "onblur", clC.obs[id]);
		}
}
