var siteDir = "/arkonline/";
var formsDir = siteDir + "forms/";
var d=document;
var NS = (!document.all) ? 1 : 0;
var IE = (document.all) ? 1 : 0;
var goodbrowser = false;

// One liners
var menus_hidden = false;

function deleteGridItem( page, grid, id ) {
	if (confirm("Are you sure you want to delete this item?")) {
		var r = new Request();
		r.url = "/arkonline/grids.php";
		r.params = { action: "deleteGridItem", page: page, grid: grid, id: id };
		actionframe.location.href = r.url + "?" + r.params.encode();	 
	}	
}

function userinfo() { var r = new Request(); r.params = { action: "currUserInfo" }; r.engage(); }
function Button(value, id) { return new Thing({id:id, type:"input", attrs: { type: "button", value: value }}); }
function nothing() { }
function today() { var x = new Date(); return x.getMonth()+1 + "/" + x.getDate() + "/" + x.getFullYear(); }
function _tag(tag) { return d.getElementsByTagName(tag); }
function done() {if (xmlhttp.readyState != 4) { return false; } return xmlhttp.responseText; }
function El(id) { this.me = $(id); }
function Reveal(obj) { if (typeof(obj) == "string") { obj = $(obj); } if (!obj) { return; } obj.style.display = ''; }
function Opacify(id) { var obj = $(id); if (!obj) return; obj.className += " transparent"; }
function Unopacify(id) { var obj = $(id); if (!obj) return; var x=$(id).className.replace(/(transparent|disabled)/g,"");  obj.className = x; }
function Hide(obj) { if (typeof(obj) == "string") {obj = $(obj);}  if (!obj) { return; }   obj.style.display = 'none';}
function isHidden(id) { var obj = $(id); if(!obj)return; (obj.style.display=='none') ? hidden=true: hidden=false; return hidden; }
function toggleAnything(id) { var obj = $(id); if (!obj) return; obj.style.display = (obj.style.display == 'none') ? '' : 'none'; }
function removeItem(id) { try { $(id).parentNode.removeChild($(id)); } catch (e) { } };
function h(id,px) { if ($(id)) { $(id).style.height = px + 'px'; } }
function max_h(id) {if ($(id)) {var obj = $(id);var off = obj.offsetTop;var ht = getDims().h;$(id).style.height = ht - 5 - off + 'px';}}
var dummy = function() { return new Thing({type:"div"}); } // return something empty to put junk in

El.prototype.top = function() { return this.me.offsetTop; }
El.prototype.left = function() { return this.me.offsetLeft; }
El.prototype.center = function() { var e = this.me; var m = getDims(); e.style.left = (m.w - e.offsetWidth)/2 + "px";  e.style.top = (m.h - e.offsetHeight)/2 + "px";}
El.prototype.addClass = function(cls) { this.obj.className += " " + cls; }
El.prototype.removeClass = function(cls) { this.obj.className = this.obj.className.replace(cls, ""); }

function Wink(def) { 
	if (arguments.length == 0) { 
		var def = { title: "details", width: "500px", height: "350px", id: "wink_window", position: { top: "300px", left: "300px" }, content: "" }; 
	} 
	this.id = (def.id) ? def.id : "wink_window"; 
	this.title = (def.title) ? def.title : "Information"; 
	this.width = (def.width) ? def.width : "500px"; 
	this.height = (def.height) ? def.height : "450px"; 
	this.content = (def.content) ? def.content : ""; 
	this.position = (def.position) ? def.position : { top: "300px", left: "400px" }; 
	this.toggleSelectsOnClose = true; 
	this.killOnClose = true;
};


function winks_open() {
	var count = 0;
	var winks = [];
	var divs = d.getElementsByTagName('div');
	for (i=0;i<divs.length;i++) {
		if (divs[i].className == "wink" || divs[i].className == "box") {
			++count;
			winks.push(divs[i].id);
		}
	}

	if (count==1) {
		if (winks[0] == "results" && $('results').style.display == 'none') count = 0;
	}
	
	return count;
}

Wink.prototype.display = function() { if (!this.styled()) { this.addStyle(); } var w = document.createElement("div"); w.id = this.id; w.style.left = this.position.left; w.style.top = this.position.top; w.className = "wink"; w.style.width = this.width; w.style.height = this.height; w.appendChild( this.titleBar() ); var contentDiv = document.createElement("div"); contentDiv.className = "winkcontent"; if (this.content){ contentDiv.innerHTML = this.content; } w.appendChild(contentDiv); document.body.appendChild(w); this.makeDraggable();};
Wink.prototype.titleBar = function() { 
	var t = document.createElement("div");
	t.className = "titlebar"; 
	if (this.title) { t.innerHTML = this.title; } 
	var closeBtn = document.createElement("span");
	closeBtn.style.fontSize = "13pt"; 
	
	closeBtn.onclick = function() {
		this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
		if (typeof(Ark.Handy.toggleSelects) != "undefined") { Ark.Handy.toggleSelects('visible'); }
	}
		
	/*
	if (this.killOnClose) { 
		closeBtn.onclick = function() { 
			this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
			if (this.toggleSelectsOnClose) {
				
			}
		};
	} else { 
		closeBtn.onclick = function() { 			
			Hide(this.parentNode.parentNode.id);
			if (this.toggleSelectsOnClose) { if (typeof(Ark.Handy.toggleSelects) != "undefined") { Ark.Handy.toggleSelects('visible'); } } 
		}; 
	} 
	*/
	closeBtn.innerHTML = "x"; 
	closeBtn.className = "closeBtn"; 
	t.appendChild(closeBtn); 
	return t;
};
Wink.prototype.styled = function() { var L = _tag('link'); for(i=0;i<L.length;i++) { if (L[i].getAttribute('href').indexOf('wink.css') != -1) { return true; } } return false; };
Wink.prototype.addStyle = function() { var h = _tag('head')[0]; var l = new Thing({type:"link", attrs: { href: "/style/wink.css", type: "text/css", rel: "stylesheet"} }); h.appendChild(l); };
Wink.prototype.makeDraggable = function() { try { var me = document.getElementById(this.id); var handle = me.getElementsByTagName('div')[0]; Drag.init(handle, me); } catch(e) { } };
Wink.prototype.addContent = function(chunk) { var wc = $(this.id).childNodes[1]; wc.appendChild(chunk); };

Array.prototype.indexOf=function(n){for(var i=0;i<this.length;i++){if(this[i]===n){return i;}}return -1;}
Object.prototype.altIndexOf=function(n){for(var i=0;i<this.length;i++){if(this[i]===n){return i;}}return -1;}
Array.prototype.lastIndexOf=function(n){var i=this.length;while(i--){if(this[i]===n){return i;}}return -1;}
Array.prototype.forEach=function(f){var i=this.length,j,l=this.length;for(i=0;i<l;i++){if((j=this[i])){f(j);}}};
Array.prototype.insert=function(i,v){if(i>=0){var a=this.slice(),b=a.splice(i);a[i]=value;return a.concat(b);}}
Array.prototype.unique=function(){var a=[],i;this.sort();for(i=0;i<this.length;i++){if(this[i]!==this[i+1]){a[a.length]=this[i];}}return a;}
if(typeof Array.prototype.push==='undefined'){Array.prototype.push=function(){for(var i=0,b=this.length,a=arguments;i<a.length;i++){this[b+i]=a[i];}return this.length;};}
Array.prototype.removeItem = function(val) {var temp = []; for(i=0;i<this.length;i++) { if (this[i] != val) { temp.push(this[i]); } } return temp;}

function gmap(id) {
 var text = ""; 
 var url = "http://maps.google.com/maps?f=q&hl=en&q="; 
 if (!$(id)) {
  text = arguments[0];  
  window.open(url+text, '', 'width=600,height=500');  
  return;   
  } 
  var r = new Request(); 
  r.url = "/arkonline/remote.php"; 
  r.params = { action: "getMap", id: $(id).value }; 
  r.method = "GET"; 
  r.todo = function() { 
	  if (!done()){ return;}  
	  var data = [];  
	  try { eval("data = (" + done() + ");"); } catch (e) {  }  
	  var text = data[0].adr_line1 + ", " + data[0].city + ", " + data[0].state + " " + data[0].zip;  
	  var url = "http://maps.google.com/maps?f=q&hl=en&q=";  
	  var a = data[0];  
	  var addr = a.adr_line1 + ", " + a.city + ", " + a.state + " " + a.zip;  
	  window.open(url+addr, '', ''); 
  } 
  r.engage();
}

function Request(details) {
 if (arguments.length==0) { var details = {url:"",method:"GET",params:{},todo:nothing}; }
 this.url = (details.url) ? details.url : "/arkonline/remote.php";
 this.method = (details.method) ? details.method : "GET";
 this.params = details.params;
 this.todo = (details.todo) ? details.todo : function() { if (xmlhttp.readyState != 4) { return; } alert(xmlhttp.responseText); }
}

Request.prototype.engage = function() {
 if (this.method == "GET" && this.params) { this.url += "?" + this.params.encode(); }
 var envelope = (this.method=="POST") ? this.params.encode() : null; 
 try { xmlhttp.open( this.method, this.url, true); } catch (e) { } 
 if (this.method == "POST") { 
  xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
 } 
 xmlhttp.onreadystatechange = this.todo; 
 xmlhttp.send(envelope);
}

function Thing(obj) {
 if (!obj.type) return;
 this.type = obj.type;
 this.me = document.createElement(this.type);
 if (obj.id) this.me.id = obj.id;
 if (obj.className) this.me.className = obj.className;
 if (obj.content) this.me.innerHTML = obj.content;
 if (obj.attrs) {
  for(key in obj.attrs) {
   if (typeof(obj.attrs[key]) == "function") continue;
   this.me.setAttribute(key, obj.attrs[key]);
  }
 }
 return this.me;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      try { func(); } catch(e) { if (debug) { alert("Debug Info: Error in onload function. " + e.message); } }
    }
  }
}

function bytag(tag) {
 var array = [];
 var collection = d.getElementsByTagName(tag);
 for(i=0;i<collection.length;i++) {array.push( collection[i] );}
 return array;
}

function $() {
    if (arguments.length==1) {
        var obj = d.getElementById(arguments[0]);
        if (!obj) return;
        return obj;
    } else if (arguments.length > 1) {
        var items = [];
        for(i=0;i<arguments.length;i++) {
            var obj = d.getElementById(arguments[i]);
            if (obj) items.push(obj);
        }
        return items;
    }
}

// This is not really commented out, it only looks like it is. Don't ask.
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

function xh() { if (xmlhttp.readyState != 4) return; alert(xmlhttp.responseText); }

function getDims() {
  if (window.innerHeight) 
   return {h:window.innerHeight,w:window.innerWidth};
  if (document.documentElement && document.documentElement.clientHeight != 0) 
   return {h:document.documentElement.clientHeight,w:document.documentElement.clientWidth};
  if (typeof(document.body.clientHeight) == "number") 
   return {h:document.body.clientHeight,w:document.body.clientWidth};
}
// Does exact same thing as Request(), can probably be rid of
function ajax(query) {  
  var url = query.url;
  var ondataload = query.onload;
  var todo = query.todo;
  var params = query.params;
  var method = (query.method) ? query.method : "GET";  
  if (method=="GET") {
    if (params) url += "?" + params.encode();
  } 
  var envelope = (method=="POST") ? params.encode() : null; 
  try { xmlhttp.open(method,url,true); } catch(e) {}  
  if (method == "POST") xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xmlhttp.onreadystatechange = todo;
  xmlhttp.send(envelope);
}

function node(nodetype,id,classname,content) {
  var n = document.createElement(nodetype);
  if (id!="") n.id = id;
  if (classname != "") n.className = classname;
  if (content != "") n.innerHTML = content;
  return n;
}

function myalert(what) {
  what = what.replace(/\n/g, "<br />");
  var w = new Wink();
  w.content = what;
  w.title = "Info";
  w.display();
  (new El(w.id)).center();
}

// Prototypes
String.prototype.stripHTML = function() {return this.replace(/(<([^>]+)>)/ig," ");}
String.prototype.trim = function() { if (this == "") {  return this;  } else {  return this.replace(/^\s*|\s*$/g,""); }}
String.prototype.cap = function() { if (this=="") { return this; } else { return this.substr(0,1).toUpperCase() + this.substr(1, this.length - 1); } }
String.prototype.contains = function(toSearchFor) {
	if (this=="") return false;
	if (this.toLowerCase().indexOf(toSearchFor.toLowerCase()) != -1) {
		return true;
	} else {
		return false;
	}
}
Object.prototype.findItem = function(searchkey,searchval,returnkey) {
 for(z=0;z<this.length;z++){
  if (this[z][searchkey] == searchval) return this[z][returnkey];
 }
 return -1;
}
Object.prototype.encode = function() {
  var str = "";
  for (key in this) {
    if (typeof(this[key]) != "function" && this[key] != "undefined") str += key + "=" + this[key] + "&";
  }
  str = str.substr(0,str.length-1);
  return str;
}

Object.prototype.outputDetails = function() {
  var str = "";
  var len = this.length;
  var obj = (typeof(len) == "number") ? this : [this];
  for (i = 0;i< obj.length; i++) {
    for (key in obj[i]) {
      if (typeof(obj[i][key]) != "function") str += "<strong style='font-weight:bold'>" + key.toUpperCase() + "</strong>: " + obj[i][key] + ";<hr> ";
    }
    str += "\n\n";
  }
  myalert(str);
}

Array.prototype.each = function(func) { for (i=0;i<this.length;i++) { func(this[i]); } }

Object.prototype.findByNum = function(searchKey, val, returnKey) {
 for (i=0;i<this.length;i++) {
  if (this[searchKey] == val) return this[returnKey];
 }
 return -1;
}

Array.prototype.drop = function(val) {
 var temp = [];
 for(i=0;i<this.length;i++){
  if (this[i] != val) {
   temp.push(this[i]);
  }
 }
 return temp;
}

function fixDate(strDate) {
  if(strDate.indexOf("-")==-1) return strDate;
  try {
 var parts = strDate.split("-");
 var y = parts[0];
 var m = parts[1];
 if (m.charAt(0) == "0") m = m.charAt(1);
 var day = parts[2];
 if (day.charAt(0) == "0") day = day.charAt(1);
   return m + "/" + day + "/" + y;
  } catch(e) { return strDate; }
    
}

function returnFirstNonTextElement(parent) {
  var obj = (typeof(parent)=="string") ? document.getElementById(parent) : parent;
  if(!obj) return;
  var test = obj.firstChild;
  while (test.nodeName == "#text") {
    test = test.nextSibling;
  }
  return test; 
}

Renamer = {
	changeID: function(type) {
		var p = prompt("Please enter a new id for this item.", "", "");
		if (p != "" && p != null ) {
			var r = new Request(); 
			var oldid = "";
			
			switch(type) {
				case "animal": 
					oldid =  $('id_num').value;
					break;
				case "person": 
					oldid = $('id').value;
					break;					
			}
			
			if (oldid != "") {
				r.params = { action: "changeItemID", type: type, oldid: oldid, newid: p };

				r.method = "POST";
				r.todo = function() {
					if (!done()) return;
					var data = {};
					try {
						eval("data = " + done() + ";");	
					} catch (e) {
						
					}
					
					if (data.message != '') {
						alert(data.message);
					} else {
						switch(data.type) {
							case "animal": $('id_num').value = data.newid;
							case "person": $('id').value = data.newid;
						}
					}
					
					
					
				}
			
				r.engage();
			} else {
				alert("Old ID is blank.");	
			}
		}	
	}
}


















