var ctrlkey=false, shiftkey=false, altkey=false;
var curr_obj, curr_pos;
var isdrag;
var dragobj;


//************************
//       init()
//************************
function listen(){
	addListener(document,"keyup", keyup);
//	isIE() ? addListener(document,"keydown", keypress) : addListener(document,"keypress", keypress);
//	if (!isIE()) addListener(document,"keypress", keypress);
	addListener(document,"keydown", keypress);
	addListener(document,"mousedown", mousedown);
	addListener(document,"mouseup", mouseup);
	addListener(document,"mousemove", mousemove);
	addListener(document,"change", change);
	addListener(document,"help", help);
	addListener(document,"dblclick", doubleclick);
	addListener(document,"click", click);

//	isIE() ? addListener(document,"close", close) : addListener(document,"unload", close);
//	addListener(window,"beforeunload", close);
//	addListener(window,"oncontextmenu", unload);
}


//************************
//       click()
//************************
function click(e){
	e = e || window.event;
	//IE only for select boxes 
	if (isIE()){
		obj = event.srcElement;
		if (obj.tagName=="SELECT") {
			//Check to see if the attribute edit is set to N if so ignore this element as editable
			if(typeof(do_changed) != "undefined") {
				if (obj.getAttribute("edit") == 'No') return;
				do_changed(obj);
			}
		}
	}
}

//************************
//       close()
//************************
function close(){
	//Signout 
	AjaxCall("access", "logout");
//	alert("You have close HWOS you must re-sign on again");
//	return false;
}
//************************
//       doubleclick()
//************************
function doubleclick(e){
	e = e || window.event;
 	var obj = isIE() ? event.srcElement : e.target;
	if (obj.className=="calpick") do_getday();
	return false;
}
//************************
//       currobj()
//************************
function currobj(el){
	if (el.target) {
		return(el.target);
	}else{
		return(el.srcElement);
	}
}
/*
//************************
//       scroll()
//************************
function scroll(e) {
	e = e || window.event;
	obj = currobj(e);
	s=getobj("status");
	settext("status_msg", "W:"+window.pageYOffset+",S:"+screen.height)
	window.status=s.innerHTML;
}

//************************
//       getScrollHeight()
//************************
function getScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}
*/
//************************
//       change()
// Set a flag to indicate that a change has been made
// Add the attribute edit="No" if it does not matter if it is changed (ex: the list of sensors only the details is what is required)
//************************
// Check to see if it has already been set 
// Values would be N-New or C-Changed or none
function change(e) {
	e = e || window.event;
  	var obj = isIE() ? event.srcElement : e.target;
//	if (g_editmode != '') return; 
	if(typeof(do_changed) != "undefined") {
		if (obj.getAttribute("edit") == 'No') return;
		//Is it new if not set it to change
		if (g_editmode != 'N') g_editmode = 'C';
		do_changed(obj);
	}
}

//************************
//       keypress()
//************************
function keypress(e) {
	e = e || window.event;

  	var obj = isIE() ? event.srcElement : e.target;
//	if (e.keyCode == 46) {
//		//Delete
//		if(typeof(do_delete) != "undefined") do_delete();
//	}
//	if (e.keyCode == 45) {
//		//Delete
//		if(typeof(do_insert) != "undefined") do_insert();
//	}

	switch(e.keyCode) {
		case 13:
			//Enter
			if (obj.id == "search_data") search();
			else if (obj.id == "signup") signup();
			break;
		case 112:
			//F1
			if (!isIE()) {
				if (obj.id) {
					JObj = new Object;
					JObj.curr_tab = g_curr_tab;
					JObj.curr_obj = obj.id;
					AjaxCall("status", "show_help", JObj);
				}
				e.stopPropagation();
				e.preventDefault();
			}
			return false;
			break;
		case 113:
			//F2
			debug();
			return;
			break;
		case 120:
			//F9
		  	var obj = isIE() ? event.srcElement : e.target;
			if (obj.id) {
				JObj = new Object;
				JObj.curr_tab = g_curr_tab;
				JObj.curr_obj = obj.id;
				AjaxCall("status", "show_help", JObj);
			}
			return;
			break;
		case 123:
			//F12
			close_window("buddy");
			close_window("correct");
			close_window("status_reject");
			close_window("errors");
			close_window("status_late");
			close_window("status_hourly");
			close_window("status_speci");
			return;
			break;
		case 40:
			//Down arrow
			if(typeof(do_goDown) != "undefined") {
				if (isIE()) {
					obj=obj.parentNode.parentNode.parentNode.parentNode;
				}
				if (obj.className.toLowerCase() == "grid") do_goDown(obj);
			}
			break;
		case 38:
			//Up arrow
			if(typeof(do_goUp) != "undefined") {
				if (isIE()) {
					obj=obj.parentNode.parentNode.parentNode.parentNode;
				}
				if (obj.className.toLowerCase() == "grid") do_goUp(obj);
			}
			break;
		case 16:
			shiftkey=true;
			break;
		case 17:
			ctrlkey=true;
			break;
		case 18:
			altkey=true;
			break;
	}
	if (e.shiftKey && e.keyCode==222) {
		alert('You cannot use a " in your text!');
		obj.value = obj.value.replace(/\"/g, "");;
		return false;
	}
	if (e.keyCode < 32) return true;
	if (obj.className == "std_edit") {
		//Check to see if the attribute edit is set to N if so ignore this element as editable
		if(typeof(do_changed) != "undefined") {
			if (obj.getAttribute("edit") == 'No') return;
			do_changed(obj);
		}
	}
	return true;
}
//************************
//       help() IE only
//************************
function help(e) {
	e = e || window.event;
  	var obj = isIE() ? event.srcElement : e.target;
	if (obj.id) {
		JObj = new Object;
		JObj.curr_tab = g_curr_tab;
		JObj.curr_obj = obj.id;
		AjaxCall("status", "show_help", JObj);
	}
	return false;
}
//************************
//       keyup()
//************************
function keyup(e) {
	e = e || window.event;
  	var obj = isIE() ? event.srcElement : e.target;
	if (obj.className == "std_edit") {
		if (obj.getAttribute("limit")) {
			if (obj.value.length >= obj.getAttribute("limit")) {
				alert("Text limit reached!");
				obj.value=obj.value.substr(0,obj.getAttribute("limit"));
				return false;
			}
		}
	}else{
		if (e.keyCode == 38 || e.keyCode == 40) {
			if (isIE()) {
				obj=obj.parentNode.parentNode.parentNode.parentNode;
			}
			if (obj.className.toLowerCase() == "grid")  if(typeof(do_keyUp) != "undefined") do_keyUp(obj);
		}
	}
	switch(e.keyCode) {
		case 16:
			shiftkey=false;
			break;
		case 17:
			ctltkey=false;
			break;
		case 18:
			altkey=false;
			break;
	}
	return true;
}
//************************
//       Unload()
//************************
function unload(e) {
//	AjaxCall("access","logout");
	if (window.event.clientX < 0 && window.event.clientY < 0) {
		alert("Window is closing...");
	}
}

//************************
//       mousedown()
//************************
function mousedown(e) {
	var i, sel;
  	var obj = isIE() ? event.srcElement : e.target;

//alert(getobj("container").offsetHeight);
//alert(getobj("center").offsetLeft);

	if (obj.id == "winclose") {
		// Get the Div id
		var did=obj.parentNode.parentNode.parentNode.parentNode.parentNode;
		if (did) close_window(did.id);
		return false;
	}

	if (obj.id == "wintitle") {
		// Get the DIV obj
		isdrag = true;
		dragobj = obj.parentNode.parentNode.parentNode.parentNode;
		tx = dragobj.offsetLeft;
		ty = dragobj.offsetTop;
		lastx = isIE() ? event.clientX : e.clientX;
		lasty = isIE() ? event.clientY : e.clientY;
		return;
	}
    if (obj.tagName == "BODY" || obj.tagName == "HTML") {
 		return;
	}
	// If it's a table cell
	if (obj.tagName == "TD") {
		if (obj.parentNode.parentNode.parentNode.className=="calndr"){
			calpick(obj);
		}else{
			if (isGridPickable(obj)) {
				pick(obj);
			}
		}
	}
	if(typeof(do_changed) != "undefined") {
		if ((obj.getAttribute("type") == 'checkbox') || (obj.getAttribute("type") == 'radio')) {
			//Is it new if not set it to change
			if (g_editmode != 'No') g_editmode = 'C';
			do_changed(obj);
		}
	}
	return;
}
    
//************************
//       mouseup()
//************************
function mouseup(e) {
	e = e || window.event;
	isdrag=false;
	if (dhtmlwindow.resizing == 'Y') {
		d=document.getElementById("dhtmlframe");
		if (d) {
			d.style.width=(d.parentNode.offsetWidth-20)+"px"
			d.style.height=(d.parentNode.offsetHeight-20)+"px"
		}
	}
	return true;
}

//************************
//       mousemove()
//************************
function mousemove(e) {
	if (isdrag)  {
		if (isIE()) {
			dragobj.style.left = tx + event.clientX - lastx+"px";
			if (event.clientY > 24) dragobj.style.top  = ty + event.clientY - lasty+"px";
		}else{
			dragobj.style.left = tx + e.clientX - lastx+"px";
			if (e.clientY > 24) dragobj.style.top  = ty + e.clientY - lasty+"px";
		}
	}

//	settext("status_msg", e.target.id);
}

//************************
//       addListener()
//************************
function addListener(element, type, expression, bubbling) {

	bubbling = bubbling || false;
	if(window.addEventListener) { // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else return false;
}
listen();


