// initiates the functionStack

var functionStack = new Array();
functionStack[functionStack.length] = "spellCheck()";
sequences = new sequences();
JS_login_attempts = 0;

function sequences(){
	this.items = new Array();
}

var appStack = new Array();
var objectStack = new Array();

function main(){
	tM = check_for_gets(new Array("tX", "tY"));
	if(tM != false){
		window.moveTo(tM.tX, tM.tY);
	}
	var fStime = setInterval(doStack, 10);
	initObjStack();	
	// starts timer routine to execute each routine in the list every 10 ticks
}

function check_for_gets(T_targets){
	T_loc = window.location.href.toString();
	T_a = T_loc.split(/&/);
	c = 1;
	T_obj = new Object();
	got1=false;
	while(c < T_a.length){
		T_a2 = T_a[c].split(/=/);
		c2 = 0;
		while(T_targets[c2]){
			if(T_a2[0] == T_targets[c2]){
				T_obj[ T_a2[0] ] = T_a2[1];
				got1 = true;
			}	
			c2+=1;
		}
		c+=1;
	}
	if(got1 == true){
		return T_obj;
	}else{
		return false;
	}
}

function initObjStack(){
	c=0;

	while(c < objectStack.length){
		eval(objectStack[c]);
		//alert(objectStack[c]+"af");
		c+=1;
	}
}

function doStack(){
	c=0;
	while(c<functionStack.length){
		eval(functionStack[c]);
		c+=1;
	}
}


function getPos_static(evt){
	x1 = evt.clientX + Number(document.body.scrollLeft);
	y1 = evt.clientY + Number(document.body.scrollTop);
	mousePoint = {X:x1, Y:y1};
	return mousePoint;
}

function getPos_dynamic(e){
	if(e){
		x1 = document.getElementById('posX').value = e.pageX;
		y1 = document.getElementById('posY').value = e.pageY;
	}else{
		x1 = document.getElementById('posX').value = event.clientX + Number(document.body.scrollLeft);
		y1 = document.getElementById('posY').value = event.clientY + Number(document.body.scrollTop);
	}
	mousePoint = {X:x1, Y:y1};
	return mousePoint;
}


function getElementStats(obj){
	oW = obj.offsetWidth;
	oH = obj.offsetHeight;
	b1 = Number(obj.style.borderTopWidth.substring(0, obj.style.borderTopWidth.length - 2));
	b2 = Number(obj.style.borderRightWidth.substring(0, obj.style.borderRightWidth.length - 2));
	b3 = Number(obj.style.borderBottomWidth.substring(0, obj.style.borderBottomWidth.length - 2));
	b4 = Number(obj.style.borderLeftWidth.substring(0, obj.style.borderLeftWidth.length - 2));
	if(navigator.appName == "Microsoft Internet Explorer"){
		iW = oW;
		iH = oH;
		oW += (b2 + b4);
		oH += (b1 + b3);
	}else{
		iW = oW - (b2 + b4);
		iH = oH - (b1 + b3);
	}
	offsetX = 0;
	offsetY = 0;
	if(obj.offsetParent){
		offsetX = obj.offsetLeft;
		offsetY = obj.offsetTop;
		while(obj = obj.offsetParent){
			if(navigator.appName == "Microsoft Internet Explorer"){
				if(obj.style.position == "relative"){
					tL = Number(obj.style.left.substring(0, obj.style.left.length-2));
					tT = Number(obj.style.top.substring(0, obj.style.top.length-2));
					offsetX -= tL;
					offsetY -= tT;
				}
			}
			offsetX += obj.offsetLeft;
			offsetY += obj.offsetTop;
		}
	}
	tOffsets = {X:offsetX, Y:offsetY, width:oW, height:oH, innerWidth:iW, innerHeight:iH};
	return tOffsets;
}


function spellCheck(){
	//alert("aaddf");
}

function changeAction(newNo){
	document.getElementById('action_val').value = newNo;
}

function removeFromStack(inStr){
	r=0;
	while(r < functionStack.length){
		if(functionStack[r] == inStr){
			functionStack.splice(r,1);
		}
		r+=1;
	}
}

function selectionStart(obj, evt){
	if(evt.preventDefault){
		evt.preventDefault();
	}
	document.getElementById(obj.id).obj.JSON.onSelect(obj.id);
	me.compatibleDataTypes = false;
	me.selectionCount=0;
	me.selectedObj = obj.id;
	me.dataTypeHeld = document.getElementById(obj.id).obj.dataType;
	functionStack[functionStack.length] = "selectionTick()";

}

function selectionTick(){
	if(me.selectionCount < 40){
		me.selectionCount +=1;
	}else{
		removeFromStack("selectionTick()");
		dragStart(me.selectedObj, document.getElementById(me.selectedObj).obj.mode);

	}
}

function mouseUpChooser(obj){
	if(me.dragObj == ""){
		selectionFinalise(obj)
	}else{
		dragDrop(obj);
	}
}
		
function selectionFinalise(obj){
	me.selectionCount=0;
	removeFromStack("selectionTick()");
}

dragFiles = {image:"icon2"};

function dragStart(idIn, mode){
	me.selectedObj = "";
	me.dragObj = idIn;
	me.draggingData = document.getElementById(idIn).obj.JSON.doDrag(document.getElementById(idIn));

	if(mode == "cut"){
		me.cutObj = obj.id;
		document.getElementById(obj.id).removeChild(obj.id);
	}
	var dragIcon = document.createElement("img");
	dragIcon.id="dragIcon";
	dragIcon.src="./static/IMG/IMG_dataTypes/" + dragFiles[me.dataTypeHeld] + ".jpg";
	dragIcon.style.position = "absolute";
	dragIcon.style.left = me.posX +5;
	dragIcon.style.top = me.posY +5;
	document.body.appendChild(dragIcon);
	functionStack[functionStack.length] = "dragMove()";
}

function getPos(evt){
	if(me.browser == "IE"){
		me.posX = evt.clientX + document.body.scrollLeft;
		me.posY = evt.clientY + document.body.scrollTop;
	}else{
		me.posX = evt.pageX;
		me.posY = evt.pageY;
	}
	me.posX += 5;
	me.posY += 5;

}

function dragMove(){
	document.getElementById('dragIcon').style.left=me.posX;
	document.getElementById('dragIcon').style.top=me.posY;
}

function dragDrop(obj){
	if((me.dragObj != obj.id) && (me.compatibleDataTypes == true)){
		document.getElementById(obj.id).obj.JSON.doDrop(obj, me.draggingData);
	}
	me.dragObj = "";
	document.body.removeChild(document.getElementById('dragIcon'));
	removeFromStack("dragMove()");
}

function dataConvert(){

}

function removeSelection(evt){
	return false;
}

function dataCheck(obj){
	if(me.dragObj != ""){
		c=0;
		checkedVar = false;
		while((c < document.getElementById(obj.id).obj.dataAccepted.length) && (checkedVar == false)){
			if(document.getElementById(obj.id).obj.dataAccepted[c] == me.dataTypeHeld){
				checkedVar = true;
				me.compatibleDataTypes = true;
			}
			c+=1;
		}
	}
}

function selectControl(){

}

var base = function(){
	this.tSite = window.location.href.split(/\//);
	this.site = "";
	this.folder = "";
	c=0;
	while(c < (this.tSite.length-1)){
		if(c > 1){
			this.site += "/";
		}else if (c == 1){
			this.site += "//";
		}
		this.site +=this.tSite[c];
		c+=1;
	}
	this.site += "/";
	this.browser = navigator.appName;
	if(this.browser == "Microsoft Internet Explorer"){
		this.browser = "IE";
	}
	this.posX = 0;
	this.posY = 0;
	this.draggingData = null;
	this.moving_control = false;
	this.compatibleDataTypes = false;
	this.dragObj = "";
	this.selectedObj = "";
	this.cutObj = "";
	this.dataTypeHeld = null;
}

function validateForm(items, columns, types){
	errMsg = "";
	c = 0;
	while(c < items.length){
		if(document.getElementById(items[c]).value == ""){
			errMsg += columns[c];
			if(types[c]){
				if(types[c] == "password2"){
					errMsg += " must match \n";
				}else if(types[c] == "above"){
					errMsg += " is too low\n";			
				}else{
					errMsg += " must have a value\n";
				}
			}else{
				errMsg += " must have a value\n";
			}
		}
		c += 1;
	}
	if(errMsg.length > 0){
		alert(errMsg);
		return false;
	}else{
		document.getElementById('news_item_method').value = 2;
		return true;
	}
}


function setProperty(target, obj, val){
	if(target.length > 2){
		obj[target] = val;
	}else{
		setProperty(target[1], obj[target[0]], val);
	}
}

var me = new base();


function createNewWindow(T_name, T_redirect, T_inherits, T_width, T_height, T_obj, T_src, T_pa){
	// possible params - T_menuBar, T_resizeable, scrollbars, status, toolbar
	params = "width=" + T_width + ",height=" + T_height;
	c = 0;
	while(T_obj[c]){
		params += T_obj[c];
		c+=1;
	}

	T_src += "?action_val=" + T_redirect;
	c = 0;
	while(T_pa[c]){
		T_src += "&" + T_pa[c];
		c+=1;
	}

	if(T_inherits == true){
		if(document.getElementById('action_method')){
			if(document.getElementById('action_method').value.length >0){
				T_src += "&action_method=" +document.getElementById('action_method').value;
			}
		}
		if(document.getElementById('selected_id')){
			if(document.getElementById('selected_id').value.length >0){
				T_src += "&selected_id=" +document.getElementById('selected_id').value;
			}
		}
		if(document.getElementById('search_string')){
			if(document.getElementById('search_string').value.length >0){
				T_src += "&search_string=" +document.getElementById('search_string').value;
			}
		}
		if(document.getElementById('return_sId')){
			if(document.getElementById('return_sId').value.length >0){
				T_src += "&return_sId=" +document.getElementById('return_sId').value;
			}
		}
		if(document.getElementById('pro_lo')){
			if(document.getElementById('pro_lo').value.length >0){
				T_src += "&pro_lo=" +document.getElementById('pro_lo').value;
			}
		}
	}
	var tWin = window.open(T_src, T_name, params);
}

var overlay = function(tex, bgCol, contObj){
	tO = getElementStats(document.body);
	dd3 = document.createElement("table");
	dd3.id = "overlayPane";
	c1 = dd3;
	dd3.style.textAlign = "center";
	dd3.style.position = "absolute";
	dd3.style.left = "0px";
	dd3.style.top = "0px";
	dd3.style.zIndex = 500;
	dd3.style.width = document.body.scrollWidth;
	if(navigator.appName == "Microsoft Internet Explorer"){
		dd3.style.height = 800;
	}else{
		dd3.style.height = 800;
	}
	dd4 = document.createElement("table");
	dd4.align="center";
	r2 = dd4.insertRow(-1);
	c3 = r2.insertCell(-1);
	c3.id = "overlayContent";
	c3.style.verticalAlign = "top";
	c3.style.width = "400";
	c3.style.height = "200";
	c3.style.backgroundColor = "white";
	dd4.style.textAlign = "left";
	dd4.style.backgroundColor = "white";
	dd3r1 = c1.insertRow(-1);
	dd3c1 = dd3r1.insertCell(-1);

	dd3c1.appendChild(dd4);
	document.forms[0].appendChild(dd3);
	var dd = document.createElement("div");
	dd.id = "overlayPane1";
	dd.style.verticalAlign="top";
	dd.style.position = "absolute";
	dd.style.left = "0px";
	dd.style.zIndex = 50;
	dd.style.top = "0px";
	dd.style.backgroundColor = bgCol;
	dd.style.width = document.body.scrollWidth;
	dd.style.opacity = "0.5";
	dd.style.filter = "alpha(opacity=50)"; 

	if(navigator.appName == "Microsoft Internet Explorer"){
		dd.style.height = document.body.scrollHeight + 20;
	}else{
		dd.style.height = tO.height + 20;
	}
	document.forms[0].appendChild(dd);
	window.scrollTo(0, 0);

	var in1 = document.createElement("div");
	in1.id = "overlayClose";
	in1.innerHTML = "<div id=\"zx\" onClick=\"overlay_close()\" style=\"font-family:arial, helvetica, sans-serif;font-weight:bold;cursor:pointer;\">CLOSE</div>";
	document.getElementById('overlayContent').appendChild(in1);
	document.getElementById('overlayContent').appendChild(contObj);

};



//

var base_control = function(T_obj, T_properties){
	if(T_properties.width){
		T_obj.width = T_properties.width;
	}
	if(T_properties.controls){
		T_obj.controls = T_properties.controls;
	}
	if(T_properties.id){
		T_obj.id = T_properties.id;
	}
	if(T_properties.X){
		T_obj.X = T_properties.X;
	}
	if(T_properties.Y){
		T_obj.Y = T_properties.Y;
	}
	if(T_properties.clickEvt){
		T_obj.clickEvt = T_properties.clickEvt;
	}
	if(T_properties.parameters){
		T_obj.parameters = T_properties.parameters;
	}
};


var context_menu = function(T_properties){
	var tcontrol = new base_control(this, T_properties);
	this.init = function(T_obj){
		var d1 = document.createElement("div");
		d1.style.left = T_obj.X + "px";
		d1.style.top = T_obj.Y + "px";
		d1.id= T_obj.id ;
		d1.className = "context_menu_container";
		var d1_1 = document.createElement("div");
		d1_1.id= T_obj.id + "_1";
		d1_1.className = "context_menu_shadow";
		var d1_1_1 = document.createElement("div");
		d1_1_1.id= T_obj.id + "_1_1";
		d1_1_1.className = "context_menu_shadow_inner";
		var d1_2 = document.createElement("div");
		d1_2.id= T_obj.id + "_2";
		d1_2.className = "context_menu_panel";
		d1.appendChild(d1_1);
		d1_1.appendChild(d1_1_1);

		d1.appendChild(d1_2);
		document.body.appendChild(d1);
		c = 0;
		tHeight = 0;
		while(T_obj.controls[c]){
			if(T_obj.controls[c].type == "item"){
				var d1_2_1 = document.createElement("div");
				d1_2_1.id= T_obj.id + "_2_" + c;
				d1_2_1.innerHTML = T_obj.controls[c].text;
				d1_2_1.onmouseover = T_obj.mouseOver;
				d1_2_1.onmouseout = T_obj.mouseOut;
				d1_2_1.onclick = T_obj.click;
				d1_2_1.setAttribute("clickEvt", T_obj.controls[c].clickEvt);

				d1_2_1.setAttribute("clickEvt", T_obj.controls[c].clickEvt);
				d1_2_1.setAttribute("params", T_obj.controls[c].parameters);

				d1_2_1.className = "context_menu_item";
				d1_2.appendChild(d1_2_1);

				tHeight+=20;
			}else if(T_obj.controls[c].type == "divider"){
				tHeight+=5;
			}
			c+=1;
		}
		d1_1_1.style.height = (tHeight + 6) + "px";
		d1_2.style.height = tHeight + "px";

	};
	this.mouseOver = function(){
		this.style.backgroundColor="#316ac5";
	};
	this.mouseOut = function(){
		this.style.backgroundColor="";
	};

	this.click = function(){
		contextActions[ this.getAttribute("clickEvt") ](this.parentNode.parentNode.id, this);
	};
};

function oo(evt){
	var te = getPos_static(evt);
	var testcontext = new context_menu({X:te.X, Y:te.Y, id:"context1", width:24, height:100, controls:new Array({type:"item", text:"Open", clickEvt:"contextTest1", parameters:new Array(12, "hi")}, {type:"item", text:"Save", clickEvt:"contextTest2", parameters:new Array(34, "it got there!")}) } );
	testcontext.init(testcontext);
	return false;
}

var context_menu_actions = function(){
	this.contextTest1 = function (T_id, T_ref){
		document.body.removeChild(document.getElementById(T_id));
	};
};

var contextActions = new context_menu_actions();

//-----------------------------------------------------------------------
// these functions are important: they deal with moving standard controls
//

function move_start(T_id, T_move_type, T_type, T_evt){
	var tP = getPos_static(T_evt);
	var eS = getElementStats(document.getElementById(T_id));
	//alert(tP.X + "   " + eS.X);
	offsetX = tP.X - eS.X;
	offsetY = tP.Y - eS.Y;
	me.moving_control = true;
	me.moving_object = T_id;
}

function update_move(evt){
	if(me.moving_control == true){
		var tP = getPos_static(evt);
		document.getElementById(me.moving_object).style.left = tP.X - offsetX;
		document.getElementById(me.moving_object).style.top = tP.Y - offsetY;
	}
}

function move_end(){
	me.moving_control = false;
	me.moving_object = "";

}

//
//---------------------------------------------------------------------

function overlay_close(){
	var ee = document.getElementsByTagName("body");
	document.forms[0].removeChild(document.getElementById("overlayPane"));
	document.forms[0].removeChild(document.getElementById("overlayPane1"));
}
