//mouse over functions
function mouseOverBut(mOver, mOut) {
  this.mOver = mOver;
  this.mOut = mOut;
}

var mouseStore = new Object();
function mouseSetup(id, mOver, mOut) {
    mouseStore[id] = new mouseOverBut(mOver, mOut);
}

function mouseO(img) {
    _d(img.id).src = mouseStore[img.id].mOver;
}

function mouseOut(img) {
	_d(img.id).src = mouseStore[img.id].mOut;
}

//these are the personal window functions
var ops = new Object();

function winObj(pageName, width, height) {
  this.pageName = pageName;
  this.width = width;
  this.height = height;
}

function newObs(id, src, width, height) {
 if (src=='false') return false;
 ops[id] = new winObj(src, width, height);
 document.getElementById(id).onclick=openFunc;
}


function checkVal(res) {
   try {
  if (res.textContent != null) return res.textContent;
  else if (res.text != null) return res.text;
  else if (res.nodeValue!=null) return res.nodeValue
  else if (res.firstChild != null) return res.firstChild.nodeValue;
  else return "";
  } catch(e) {
    return "";
  }
}
 
function openFunc() {
     var id = this.id;
    window.open(ops[id].pageName, "view_photo", "toolbar=no,location=no,directories=no,status=yes,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width=" + ops[id].width + ",height=" + ops[id].height + ",top=100,left=50");
    return false;
}

var drop = new function() {
  this.drops = new Object();
  this.openMens = new Object();
  this.curLevel=0;

  this.shDiv = function(id, typ) {
    var el = document.getElementById(id);
    if (el==null) return false;
    el.style.display = (typ) ? "block" : "none";
    el.style.visibility = (typ) ? "visible" : "hidden";
    el=null;
  }

  this.addDrop = function(id, delay, dropId, level) {
    drop.drops[id] = new newDrop(delay, dropId, level);
  }

  this.setMouseO = function(el, type) {
	var func = el[type];
  }

  this.startDrop = function(el) {
    //if curmenu is not false then clear it out
    var id = el.id.replace(/dropdown/g, "");
    drop.curLevel = drop.drops[id].level;
    if (drop.drops[id].status==0) {
      drop.checkTime();
      drop.openMens[id] = true;
      drop.shDiv(drop.drops[id]._dropMenu, true);
    } 
	drop.drops[id].status=1;
    //finally start timer
  }

  this.unSet = function(el) {
	var id = el.id.replace(/dropdown/g, "");
	drop.drops[id].status=2;
	drop.drops[id].offsetTime = new Date();
	setTimeout("drop.checkTime();",200);
  }

  this.checkTime = function() {
    var recheck=false;
    var delArr=new Array();
    for (var i in drop.openMens) {
      if (drop.drops[i].status==2 && drop.drops[i].level >= drop.curLevel) {
      //this means to check whether we should be hiding this
    	var del = drop.drops[i]._delay;
    	var now = new Date();
    	var difference = now.getTime() - drop.drops[i].offsetTime.getTime();
    	if (difference > del) {
		  drop.shDiv(drop.drops[i]._dropMenu, false);
		  drop.drops[i].status=0;
		  delArr[delArr.length] = i;
    	} else recheck=true;
      } 
    }

    for (var i =0; i<delArr.length; i++) {
		delete drop.openMens[delArr[i]];
	}

    if (recheck) setTimeout("drop.checkTime()", 100);
    delArr=null;
  }
}

function newDrop(del, dropId, level) {
  this._delay = del;
  this._dropMenu = dropId;
  this.level = (isNaN(parseInt(level))) ? 0 : level;
  this.handle;
  this.status=0;
  this.offsetTime;
}

function _d(id) {
	return document.getElementById(id);
}

function fullmouseO(id, mover, mout) {
 //these will be applied first so dont need to worry about them having other event handlers
    mouseSetup(id, mover, mout);
	_d(id).onmouseover = function() {
		mouseO(this);
	}
	_d(id).onmouseout = function() {
		mouseOut(this);
	}
}

function newdrop(id, delay, dropdowndiv, level) {
   id = id.replace("/", "");
   drop.addDrop(id, delay, dropdowndiv, level);
   var el = _d(id);
	el.onmouseover = function() {
	 //first reset id 
     if (mouseStore[id] != null) mouseO(this);
	 drop.startDrop(this);
   }

	el.onmouseout = function() {
	   if (mouseStore[id] != null) mouseOut(this);
		drop.unSet(this);
	}

	el=_d(dropdowndiv);
	el.onmouseover = function() {
	 drop.startDrop(this);
   }

	el.onmouseout = function() {
		drop.unSet(this);
	}
   el=null;	
}

var formObj = new Array();
function addChk() {
	this.id;
	this.type;
	this.name;
}

function checkOb() {
 var err=false;
 var errMsg = "";
 var obj,errobj, form;
	for (var i =0; i<formObj.length; i++) {
	 obj = _d(formObj[i].id);
	 errobj=_d(formObj[i].id + "error");
	 if (errobj!=null) errobj.style.visibility="hidden";
		if (formObj[i].type=="text") {
			if (obj.value=="") {
			  err=true;
		     if (errobj != null) {
				errobj.style.visibility="visible";
				errMsg += errobj.firstChild.innerHTML + "\n";
			  }
			} 
		} else if (formObj[i].type=="checkbox") {
			if (!obj.checked) {
				err=true;
				if (errobj != null) {
				  errobj.style.visibility="visible";
				  errMsg += errobj.firstChild.innerHTML + "\n";
				}
			}
		} else if (formObj[i].type=="radio") {
		}
  }
	formObj=form=errobj=null;
	if (errMsg != "") alert(errMsg);
	if (!err) return true;
	else return false;
}

function radioChk(id, name) {
 var num = formObj.length;
	formObj[num] = new Object();
	formObj[num].type="radio";
	formObj[num].name = name;
	formObj[num].id = id;
	num=0;	
}

function boxChk(id) {
	 var num = formObj.length;
	formObj[num] = new Object();
	formObj[num].type="checkbox";
	formObj[num].id = id;
	num=0;
}

function textChk(id) {
	 var num = formObj.length;
	formObj[num] = new Object();
	formObj[num].type="text";
	formObj[num].id = id;
	num=0;
}

var _ajox = new Object();
var _curAjaxId=false;

function addAjOb(id, action, cont, method, formid) {
 var el = document.getElementById(id);
 if (el != null) {
  _ajox[id] = new ajaxObject(id, action, cont, method, formid);
  el.style.cursor="pointer";
  el.onclick=startAj;
  el=null;
  }
}

function ajaxObject(id, action, cont, method, formid, name) {
	this.id = id;
	this.action = action;
	this.cont = cont;
	this.method = method;
	this.formid=formid;
	this.name=name;
}

function startAjMan(id) {
	_curAjaxId = id;
	startAjWork();
}

function startAj(e) {
	if (!e) var e = window.event;
	if (e.stopPropogation) e.stopPropogation();
	if (e.bubble) e.bubble=false;
	_curAjaxId = this.id;
	startAjWork();
}

function startAjWork() {
	var curOb = _ajox[_curAjaxId];
	var fm = document.getElementById(curOb.formid);
	var data = "submit=true";
	var tgs = fm.getElementsByTagName("input");
	for (var i=0; i<tgs.length; i++) {
		if (tgs[i].type=="text" || tgs[i].type=="hidden") {
			data += "&" + tgs[i].name + "=" + tgs[i].value; 
		} else if ((tgs[i].type=="checkbox" || tgs[i].type=="radio") && tgs[i].checked==true) {
			data += "&" + tgs[i].name + "=" + tgs[i].value;
		}
	}
	tgs=fm.getElementsByTagName("select");
	for (var i=0; i<tgs.length; i++) {
	   data += "&" + tgs[i].name + "=" + tgs[i].value;	
	}
	tgs=fm.getElementsByTagName("textarea");
	for (var i=0; i<tgs.length; i++) {
		data += "&" + tgs[i].name + "=" + tgs[i].value;
	}
	tgs=null;
	var ajax = new Ajax();
	if (curOb.method.toUpperCase()=="POST") {
		ajax.method="POST";
		ajax.postData = data;
		ajax.doGet(curOb.action, startAjDisp);
	} else {
		ajax.method="GET";
		ajax.doGet(curOb.action + "?" + data, startAjDisp);
	}
	ajax=curOb=data=null;
}

function startAjDisp(str) {
	document.getElementById(_ajox[_curAjaxId].cont).innerHTML = str;
	if (window.ajaxHandler) ajaxHandler(_curAjaxId);
	else _curAjaxId=false;
}


var cal = new function() {
    this.date;
	this.day = ((1000 * 60) *60) * 24;
	this.curmonth;
	this.month=new Array(12);
	this.month[0]="January";
	this.month[1]="February";
	this.month[2]="March";
	this.month[3]="April";
	this.month[4]="May";
	this.month[5]="June";
	this.month[6]="July";
	this.month[7]="August";
	this.month[8]="September";
	this.month[9]="October";
	this.month[10]="November";
	this.month[11]="December";
	this.setDateId;
	this.origFunc=false;
	
	this.stopBub = function(e) {
		if (!e) var e = window.event;
       e.cancelBubble=true;
       if (e.stopPropogation) e.stopPropogation();
       return e;
	}
	this.runCal = function(e) {
		//get offset left and top
	  cal.stopBub(e);
	  cal.close();
	  cal.setDateId = this.id;
	  cal.date = new Date();
	  var curleft=curtop=0;
	  var obj=this;
	  curleft = this.offsetWidth;
	   if (obj.offsetParent) {
		 do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		 } while (obj = obj.offsetParent);
	   }
      var dv = document.createElement("div");
      dv.style.position="absolute";
      curleft += 15;
      curtop += 15;
      dv.style.top=curtop + "px";
      dv.style.left=curleft + "px";
      dv.setAttribute("id", "calendar");
      dv.onclick=cal.stopBub;
      dv.style.backgroundColor="#ffffff";
      dv.style.border="1px solid #000000";
      dv.style.zIndex="400";
      document.body.appendChild(dv);
      cal.setCal();
      cal.origFunc = document.body.onclick;
      dv=null;
	}
	
	this.setCal = function() {
	  var 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;' onclick='cal.close();'>close</td></tr>";
		htm += "<tr><td colspan='7' style='text-align:center;'><span id='back' style='cursor:pointer;position:absolute;left:0px;'>&lt;&lt;</span><span>" + mon + " - " + cal.date.getFullYear() + "</span><span id='next' style='cursor:pointer;position:absolute;right:0px;'>&gt;&gt;</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;
	   var tgs =el.getElementsByTagName("td");
       for (var i=0; i<tgs.length; i++) {
		if (tgs[i].id!=null && tgs[i].id!="") {
		   if (i==0) alert(tgs[i].id);
		    tgs[i].onclick=cal.postDate;
		}
	  } 
	  document.getElementById("back").onclick=cal.prevMonth;
	  document.getElementById("next").onclick=cal.nextMonth;
	  mon=htm=st=day=dt=dto=tgs=el=null;
	}
	
	this.setDate = function(time) {
		this.date.setTime(time);
	}
	 

    this.postDate = function(e) {
       cal.stopBub(e);
		var day = this.id.split("/");
		var mon = cal.date.getMonth() + 1;
		if (mon < 10) mon = "0" + mon;
		if (day[1] < 10) day[1] = "0" + day[1];
		document.getElementById(cal.setDateId).value = day[1] + "/" + mon + "/" + cal.date.getFullYear();
		day=mon=null;
	}
	
	this.nextMonth = function() {
		var mon = cal.date.getMonth() + 1;
		if (mon>=12) {
			cal.date.setFullYear(cal.date.getFullYear() + 1);
			cal.date.setMonth(0);
		} else {
		    cal.date.setMonth(mon);
	    }
	  cal.setCal();
	}
	
	this.prevMonth = function() {
		var mon = cal.date.getMonth() - 1;
		if (mon==-1) {
		   cal.date.setFullYear(cal.date.getFullYear() - 1);
		   cal.date.setMonth(11);
		} else {
		    cal.date.setMonth(mon);
	    }
	  cal.setCal();
	}
	
	this.close = function() {
	   var el = document.getElementById("calendar");
	   if (el!=null) document.body.removeChild(el);
	   el=null;
	   if (cal.origFunc != false) {
	       document.body.onclick=cal.origFunc;
	   	   cal.origFunc=null;
	   }
	}
}

function closeWin() {
	window.close();
}

var formVal = new function() {
 this.reqVals = new Object();
 this.err="";
 
 this.addVal = function(name, errid, def) {
	formVal.reqVals[name] = new formReq(errid, def);
 }

 this.checkOb = function(id) {
   formVal.err="";
   formVal.hideErrs();
   var ob = _d(id);
   var name;
    for (var i=0; i<ob.elements.length; i++) {
      name = ob.elements[i].name;
		if (formVal.reqVals[name] != null && !formVal.reqVals[name].state) {
			if (ob.elements[i].type=="radio") formVal.chkRadio(ob.elements[i], formVal.reqVals[name]);
			else if (ob.elements[i].type=="checkbox") formVal.chkCheck(ob.elements[i], formVal.reqVals[name]);
			else formVal.chkTextSel(ob.elements[i], formVal.reqVals[name]);
		}
	}
	ob=name=null;
	if (formVal.err!= "") {
		alert(formVal.err);
		return false;
	} else {
		return true;
	}
 }
 
 this.hideErrs = function() {
   var ob;
	for (var i in formVal.reqVals) {
	 formVal.reqVals[i].state=false;
		if (formVal.reqVals[i].showErr) {
			ob=document.getElementById(formVal.reqVals[i].errid);
			ob.style.visibility="hidden";
			ob.style.display="none";
			formVal.reqVals[i].showErr=false;
		}
	}
  ob=null;
}
 this.chkTextSel = function(ob, formOb) {
   var errob;
	if (ob.value==formOb.def || ob.value=='') {
	  errob=document.getElementById(formOb.errid);
	  formVal.err += errob.firstChild.innerHTML + "\n";
	  errob.style.visibility="visible";
	  errob.style.display="block";
	  formOb.showErr=true;
	}
	errob=null;
  }
  
  this.chkCheck = function(ob, formOb) {
	var errob;
	if (!ob.checked) {
	  errob=document.getElementById(formOb.errid);
	  formVal.err += errob.firstChild.innerHTML + "\n";
	  errob.style.visibility="visible";
	  errob.style.display="block";
	  formOb.showErr=true;
	}
	errob=null;
   }
   
   this.chkRadio = function(ob, formOb) {
	var errob;
	var chk=false;
	for (var i =0; i<ob.length; i++) {
		if (ob[i].checked && ob[i].value != formOb.def) {
		  chk=true;
		  break;
		}
	}
	if (!chk) {
	  errob=document.getElementById(formOb.errid);
	  formVal.err += errob.firstChild.innerHTML + "\n";
	  errob.style.visibility="visible";
	  errob.style.display="block";
	  formOb.showErr=true;
	 }
	 errob=chk=null;
    }
 	
}

function formReq(errid, def) {
	this.errid=errid;
	this.def=def;
	this.showErr=false;
	this.state=false;
}

_clickRemove = new Object();
function _clickFocus() {
	if (this.value==_clickRemove[this.id]) this.value="";
}
function _clickBlur() {
	if (this.value=="") this.value=_clickRemove[this.id];
}
