// JavaScript Document

var Basic = {};

//get OS///////////////////////////////////////
Basic.OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) Basic.OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) Basic.OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) Basic.OSName="Linux";
if (navigator.appVersion.indexOf("Linux")!=-1) Basic.OSName="Linux";



//get browser///////////////////////////////////
Basic.Browser = function () {
  var ua, s, i;
  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
};

///////////////////////////////////////////////////////////////////////////

///DRAG DIV////////////////////////////////////////////////////////////////
Basic.dragObj = new Object();
Basic.dragObj.zIndex = 0;
Basic.browser = new Basic.Browser();


/////////////////////////////////////////////////////////////////////
///find object coordinates

Basic.findPosX = function (obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
};

Basic.findPosY = function (obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
};



Basic.findCoordinates = function (object) {
	var top = Basic.findPosY(object);
	var left = Basic.findPosX(object);
	return new Array(left, top);
};

//WAIT////////////////////////////////////////////////////////////

Basic.wait = function (time) {
	{
	date = new Date();
	var curDate = null;
	do { var curDate = new Date(); } 
	while(curDate-date < time);
	}
};


///////GET BROWSER SIZE////////////////////////////////////////////
Basic.getBrowserSize = function () {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return new Array(myWidth, myHeight);
  
};
//////////////////////////////////////////////////////////////////////

Basic.removeObj = function (obj) {
	document.body.removeChild(obj);
};



///////////////////////////////////////////////////////////////////////


Basic.__max__buttons = new Array();



Basic.Button = function (parent, id) {
	this.parent = parent;
	this.label = id;
	this.id = id;
	Basic.__max__buttons[this.id] = this;
	this.top = 0;
	this.left = 0;
	
	
	this.Show = function() {
		this.__render();
	}
	
	this.setOver = function() {
		document.getElementById('button' + this.id + 'text').className = "label_over";
		document.getElementById('button' + this.id + 'right').className = "right_over";
		document.getElementById('button' + this.id + 'left').className = "left_over";
	}
	
	this.setDown = function() {
		document.getElementById('button' + this.id + 'text').className = "label_over";
		document.getElementById('button' + this.id + 'right').className = "right_over";
		document.getElementById('button' + this.id + 'left').className = "left_over";
		if (this.onclick) {
			this.onclick();
		}
	}
	this.setIdle = function() {
		try {
			document.getElementById('button' + this.id + 'text').className = "label";
			document.getElementById('button' + this.id + 'right').className = "right";
			document.getElementById('button' + this.id + 'left').className = "left";
		}
		catch(event){
		}
	}
	
	this.keyDown = function(e) {
		if (!e) e = window.event;
		code = e.keyCode;
		if (code ==13 || code ==32) {
			this.setDown();
		}
	}
		
	
	this.__render = function() {
		var s = "";
		/*s += '<div class="button" style = "top:' + this.top + 'px; left: ' + this.left + 'px;" tabindex = "0" onkeydown="Basic.__max__buttons[\'' + this.id + '\'].keyDown(event)" onblur="Basic.__max__buttons[\'' + this.id + '\'].setIdle()" >';
			s += '<div id = "button' + this.id + 'left" class ="button_left" onmouseover="Basic.__max__buttons[\'' + this.id + '\'].setOver()" onmouseout="Basic.__max__buttons[\'' + this.id + '\'].setIdle()" onclick="Basic.__max__buttons[\'' + this.id + '\'].setDown()">&nbsp;</div>';
				s += '<div class = "button_center></div>';
				//s += '<span id = "button' + this.id + 'left" class ="button_label" onmouseover="Basic.__max__buttons[\'' + this.id + '\'].setOver()" onmouseout="Basic.__max__buttons[\'' + this.id + '\'].setIdle()" onclick="Basic.__max__buttons[\'' + this.id + '\'].setDown()">';
					s += this.label;
				//s += '</span>';
				s += '<div id = "button' + this.id + 'right" class ="button_right" class ="button" onmouseover="Basic.__max__buttons[\'' + this.id + '\'].setOver()" onmouseout="Basic.__max__buttons[\'' + this.id + '\'].setIdle()" onclick="Basic.__max__buttons[\'' + this.id + '\'].setDown()">&nbsp;</div>';
		s += '</div>';*/
		s += '<table border = "0" cellspacing = "0" cellpadding ="0"  class ="button" style = "top:' + this.top + 'px; left: ' + this.left + 'px;" tabindex = "0">';
			s += '<tr>';
				s += '<td id = "button' + this.id + 'left" class = "left"  onmouseover="Basic.__max__buttons[\'' + this.id + '\'].setOver()" onmouseout="Basic.__max__buttons[\'' + this.id + '\'].setIdle()" onclick="Basic.__max__buttons[\'' + this.id + '\'].setDown()"></td>';
				s += '<td id = "button' + this.id + 'text" class = "label" onmouseover="Basic.__max__buttons[\'' + this.id + '\'].setOver()" onmouseout="Basic.__max__buttons[\'' + this.id + '\'].setIdle()" onclick="Basic.__max__buttons[\'' + this.id + '\'].setDown()"><nobr>' + this.label +'</nobr></td>';
				s += '<td id = "button' + this.id + 'right" class = "right" onmouseover="Basic.__max__buttons[\'' + this.id + '\'].setOver()" onmouseout="Basic.__max__buttons[\'' + this.id + '\'].setIdle()" onclick="Basic.__max__buttons[\'' + this.id + '\'].setDown()"></td>';
			s += '</tr>';
		s += '</table>';
		document.getElementById(this.parent).innerHTML +=s;
		
	}
}

Basic.frame_zindex = 0;
Basic.__frames = {};
Basic.Frame = function (id, title, top, left) {
	if (top == null) {
		top = 0;
	}
	if (left == null) {
		left = 0;
	}
	this.resizable = true;
	this.minHeight = 10;
	this.minWidth = 150;
	this.width = this.minWidth;
	this.height = this.minHeight;
	this.__width = this.minWidth;
	this.__height =this.minHeight;
	this.title = title;
	this.top = top;
	this.left = left;
	this.align = false;
	this.id = id;
	this.panel = null;
	this.setSize = function (width, height) {
		if (this.minWidth <width) {
			this.width = width;
			this.__width = width;
		}
		if (this.minHeight , height) {
			this.height = height;
			this.__height = height;
		}
	}

	this.getClientHeight = function() {
		return this.height - 38;
	}
	this.getClientWidth = function() {
		return this.width -2;
	}

	this.Destroy = function() {
		Basic.removeObj(document.getElementById("_formEl" + this.id));
		Basic.__frames[id] = null;
	};				
	this.setPos = function(top, left) {
		this.top = top;
		this.left = left;
	}
	this.setTitle = function(title) {
		this.title = title;
		document.getElementById(this.id+"_topcenter").innerHTML = this.title;
	}
	this.__render = function() {
		Basic.__frames[id] = this;
		var s = "";
		s += '<div class ="b_form" id = "_form' + this.id +'" style="position:absolute;top:' + this.top +'px;left:'+ this.left +'px;width:'+ this.__width +'px;height:auto;visibility:hidden">';
			s += '<div id = "' + this.id + '_topbar" class = "b_form_topBar" onmousedown="Basic.__frames[\'' + this.id + '\'].dragStart(event, \'_form' + this.id +'\')">';
				s += "<table border = \"0\" cellspacing = \"0\" cellpadding = \"0\" width = \"100%\">";
				s += "<tr>";
					s += "<td class = \"left\"></td>";
				
					s +='<td id = "' + this.id + '_topcenter" class = "center">' +this.title +'</td>';
					s += '<td class = "close" onmousedown ="javascript:Basic.__frames[\''+ this.id + '\'].Destroy();" onmouseover="javascript:Basic.__frames[\''+ this.id + '\']._changeClass(this,\'close_over\');" onmouseout="javascript:Basic.__frames[\''+ this.id + '\']._changeClass(this,\'close\');"></td>';
				s += "</tr>";
				s += "</table>";
					
			s += '</div>';

			s += '<div class = "content" id = "' + this.id +'" style="height:' + this.__height +'px; width:' + (this.__width -2) + 'px"">';
			s += '</div>';
			s += '<div id = "' + this.id + '_statusbar" class = "b_form_status_bar">'
				s +='<div class = "text"></div>';
				s +='<div class = "drag" onmousedown="Basic.__frames[\''+ this.id + '\'].resize(event)"><\/div>';	
			s += '</div>'
		s += '</div>';
		t = document.createElement("t");
		t.setAttribute("id", "_formEl"+ this.id);
		t.innerHTML = s;
		document.body.appendChild(t);
		var obj = document.getElementById("_form" + this.id);
		obj.style.visibility = "visible";
		this.__position(true);
		this.panel = document.getElementById(this.id);

		return obj;

	};

	this._changeClass = function(elem, class_name) {
		if (elem) {
			elem.className = class_name;
		}
	};

	this.__position = function(firstTime) {
		var f = document.getElementById("_form" + this.id);

		if (firstTime && this.align == "center") {
				var size = Basic.getBrowserSize();
				var top = Math.floor(size[1]/2);
				var left = Math.floor(size[0]/2);
				this.top = (top - (f.clientHeight/2));
				this.left = (left -(f.clientWidth/2));
				if (this.top < 0 ) {
					this.top = 0;
				}
				if (this.left < 0) {
					this.left = 0;
				}
				f.style.top = this.top +"px";
				f.style.left = (left -(f.clientWidth/2)) +"px";
		}
		else {
				f.style.top = this.top + "px";
				f.style.left = this.left + "px";
		}
	}

	this.Show = function(flag) {
		//console.debug(flag);
		if (flag == null) flag = true;
		var f = document.getElementById("_form" + this.id);
		if (f == null) {
			f = this.__render();
		}
		else {
			//f.style.top = this.top + "px";
			//f.style.left = this.left + "px";
			//console.debug(flag);
			if (flag == true) {
				this.__position(false);
				f.style.zIndex = ++Basic.frame_zindex;
				f.style.visibility = "visible";
			}
			else {
				f.style.visibility = "hidden";
			}
		}
			
	};

	this.release_size = function() {
		Basic._tosize = false;
		Basic.__frames[Basic.currentFrame].onResize;
		if (Basic.browser.isIE) {
			document.detachEvent("onmousemove", Basic.__frames[Basic.currentFrame].onResize);
			document.detachEvent("onmouseup",   Basic.__frames[Basic.currentFrame].release_size);
		}
		if (Basic.browser.isNS) {
			document.removeEventListener("mousemove",  Basic.__frames[Basic.currentFrame].onResize,   true);
			document.removeEventListener("mouseup",   Basic.__frames[Basic.currentFrame].release_size, true);
		}
		mouseX = null;
		mouseY = null;
			
	}
	
	
	this.onResize = function(e) {
		if (!Basic._tosize){
			return;
		}
		if (!e) e = window.event
		var x= e.screenX;
		var y = e.screenY;
		var n = Number(x) - Number(mouseX);
		var p = Number(y) - Number(mouseY);
		mouseX = e.screenX;
		mouseY = e.screenY;
		f = document.getElementById("_form" + Basic.__frames[Basic.currentFrame].id)
		content = document.getElementById(Basic.__frames[Basic.currentFrame].id)
		//topbar = document.getElementById(Basic.__frames[Basic.currentFrame].id + "_topbar")
		//topcenter = document.getElementById(Basic.__frames[Basic.currentFrame].id + "_topcenter")
		//statusbar = document.getElementById(Basic.__frames[Basic.currentFrame].id + "_statusbar")

		oldWidth = f.style.width;
		oldHeight = content.style.height;
		width = (Number(oldWidth.split("p")[0]) + n);
		height = (Number(oldHeight.split("p")[0]) + p);
		Basic.__frames[Basic.currentFrame].width = width;
		Basic.__frames[Basic.currentFrame].height = height;
		if (width >  Basic.__frames[Basic.currentFrame].minWidth && height >  Basic.__frames[Basic.currentFrame].minHeight) {
			//console.debug(oldHeight +","+ p + ","+ height);
			f.style.width = width + "px";
			content.style.height = height +"px";
			content.style.width = (width-2) +"px";
			//topbar.style.width = (width) + "px";
			//topcenter.style.width = (width-44) +"px";
		//f.style.width = width + "px";
		}

		if (Basic.browser.isIE) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (Basic.browser.isNS)
			e.preventDefault();
	}
	
	this.resize = function(eve) {
		Basic._tosize = true;
		if (this.resizable) {
			Basic.currentFrame = this.id
			mouseX = eve.screenX;
			mouseY = eve.screenY;
			if (Basic.browser.isIE) {
				document.attachEvent("onmousemove", Basic.__frames[Basic.currentFrame].onResize);
				document.attachEvent("onmouseup",   Basic.__frames[Basic.currentFrame].release_size);
				window.event.cancelBubble = true;
				window.event.returnValue = false;
			}
			if (Basic.browser.isNS) {
				document.addEventListener("mousemove", Basic.__frames[Basic.currentFrame].onResize,   true);
				document.addEventListener("mouseup",   Basic.__frames[Basic.currentFrame].release_size, true);
				eve.preventDefault();
			}
		}	
	}

	this.dragStart = function (event, id) {
		var el;
  		var x, y;
		Basic._todrag = true;
		Basic. currentFrame = this.id
		Basic.dragObj.elNode = document.getElementById(id);
 
  // Get cursor position with respect to the page.

  		if (Basic.browser.isIE) {
    			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
    			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
  		}
  		if (Basic.browser.isNS) {
    			x = event.clientX + window.scrollX;
    			y = event.clientY + window.scrollY;
  		}

  // Save starting positions of cursor and element.

  		Basic.dragObj.cursorStartX = x;
  		Basic.dragObj.cursorStartY = y; 
  		Basic.dragObj.elStartLeft  = parseInt(Basic.dragObj.elNode.style.left, 10);
  		Basic.dragObj.elStartTop   = parseInt(Basic.dragObj.elNode.style.top,  10);

  		if (isNaN(Basic.dragObj.elStartLeft)) Basic.dragObj.elStartLeft = 0;
  		if (isNaN(Basic.dragObj.elStartTop))  Basic.dragObj.elStartTop  = 0;

  		// Update element's z-index.

  		Basic.dragObj.elNode.style.zIndex = ++Basic.dragObj.zIndex;

  		// Capture mousemove and mouseup events on the page.

  		if (Basic.browser.isIE) {
    			document.attachEvent("onmousemove",  Basic.__frames[Basic.currentFrame].dragGo);
    			document.attachEvent("onmouseup",    Basic.__frames[Basic.currentFrame].dragStop);
    			window.event.cancelBubble = true;
    			window.event.returnValue = false;
  		}
  		if (Basic.browser.isNS) {
    			document.addEventListener("mousemove",  Basic.__frames[Basic.currentFrame].dragGo,   true);
    			document.addEventListener("mouseup",    Basic.__frames[Basic.currentFrame].dragStop, true);
    			event.preventDefault();
  		}
	};

	this.dragGo = function (event) {
		if (! Basic._todrag) { return };

  		var x, y;

  		// Get cursor position with respect to the page.

  		if (Basic.browser.isIE) {
    			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
    			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
  		}
  		if (Basic.browser.isNS) {
    			x = event.clientX + window.scrollX;
    			y = event.clientY + window.scrollY;
  		}

  		// Move drag element by the same amount the cursor has moved.
  		left = (Basic.dragObj.elStartLeft + x - Basic.dragObj.cursorStartX);
  		top =  (Basic.dragObj.elStartTop  + y - Basic.dragObj.cursorStartY);
		if (top > 0) {
			Basic.dragObj.elNode.style.top  = top + "px";
			Basic.__frames[Basic.currentFrame].top = top;
  		}
		if (left > 0) {
			Basic.dragObj.elNode.style.left = left + "px";
			Basic.__frames[Basic.currentFrame].left = left;
		
		}
  		if (Basic.browser.isIE) {
    			window.event.cancelBubble = true;
    			window.event.returnValue = false;
  		}
  		if (Basic.browser.isNS)
    			event.preventDefault();
		};

	this.dragStop = function (event) {
		Basic._todrag = false;

  		// Stop capturing mousemove and mouseup events.

  		if (Basic.browser.isIE) {
    			document.detachEvent("onmousemove",  Basic.__frames[Basic.currentFrame].dragGo);
    			document.detachEvent("onmouseup",    Basic.__frames[Basic.currentFrame].dragStop);
  		}
  		if (Basic.browser.isNS) {
    			document.removeEventListener("mousemove",  Basic.__frames[Basic.currentFrame].dragGo,   true);
    			document.removeEventListener("mouseup",    Basic.__frames[Basic.currentFrame].dragStop, true);
  		}
	};


};


Basic.messageBox = function(header, text) {
	if(!header) header = "Alert";
	this.id = "__message" + Math.round(1000*Math.random());
	var size = Basic.getBrowserSize();
	var lines = text.split("<br/>")
	var longestLine = 0;
	for (var i in lines) {
		if (lines[i].length > longestLine) {
			longestLine = lines[i].length;
		}
	}
	var height = Math.min((lines.length*18) + 35 , size[1]);
	var width = Math.min(longestLine*7, size[0]);
	//console.debug(width);
	var f = new Basic.Frame(this.id, header);
	f.align = "center";
	//f.resizable = false;
	f.setSize(width, height);
	f.Show();
	var button = new Basic.Button(this.id + "_ok", this.id +'_ok1');
	//button.top = 0;
	//button.left = width/2;
	button.label = "OK";
	messageBox_id = this.id
	button.onclick = function() {
		f.Destroy();
	};
	f.panel.innerHTML += ('<table style = "text-align:center; margin:auto" border = "0" callspacing = "0" cellpadding = "2"><tr><td>' + text + '</td></tr><tr><td align = "center" id = "' + this.id + '_ok"></td></tr></table>');
	button.Show();
};

