var xgAllEditBoxes = new Array();
//--------------------------------------------------------------------------------------
//
//
//
//                           XG_EDITBOX
//
//
//
//----------------------------------------------------------------------------------------
/*
						events

						onclick
						onchange
						ontype

*/
function xgEditBox()
{

	//function
	this.setFocus				=xgEB_SetFocus;

	this.id						="EditBox_"+GenerateID();
	this.dLeft					= 0;
	this.dTop					=0;
	this.dWidth					=100;
	this.dHeight				=20;
	this.dPosition				="absolute";
	this.Label					="";
	this.LabelWidth			=0;
	this.LabelClassName  ="xg_eb_label";
	this.RequiredClassName = "xg_required";
	this.EditClassName	= "xg_eb_input";
	this.FocusClassName	="xg_eb_input_focus";
	this.MouseOverClassName =  "xg_eb_input_mouseover";
	this.value					="";
	this.setValue				=xgEB_SetValue;
	this.getValue				=xgEB_GetValue;

	this.tabIndex				=null;
	this.ParentObject		=document.body;
	this.Draw					=xg_EB_Draw;
	this.EditBoxIndex		=xgAllEditBoxes.length;
	this.EditType				="text";
	this.EditLen				=-1;
	AutoTab ++ ;

	this.Type					="xgEditBox";
	this.ParamName			="";
	this.setParamValue   = xgEB_setParamValue;
	this.getParamValue		=xgEB_getParamValue;
	this.Required				=false;
	this.readOnly				=false;
	this.setReadOnly			=xgEB_setReadOnly;
	this.Active					=false;
	this.disable				=false;
	this.setDisable			=xgeditbox_setDisable;
	this.setVisible				=xgeditbox_setVisible;
	//for internal use only
	this.checkFor				=false;
	this.checkArg				;
	this._internaInput		;
	this.focusable				=true;
	this.ToolTip					="";
	this.Title							="";
	this.formatValue			=xgeditbox_formatValue;

			xgAllEditBoxes[xgAllEditBoxes.length] = this;
			xgDocument.currentObject = this;
	xg.tabs.add(this);
}

function xgEB_SetFocus()
{
	var xgObj = document.getElementById("edit_" + this.id);
	try{
	xgObj.focus();
	}catch(E){}
}

function xg_EB_Draw(dLeft,dTop,dWidth,dHeight)
{
	if (dLeft) this.dLeft = dLeft;
	if (dTop) this.dTop = dTop;
	if (dWidth) this.dWidth = dWidth;
	if (dHeight) this.dHeight = dHeight;
	
	if ((dLeft == 0)&&(dTop == 0)) this.dPosition = "relative";


	var xgEBSurface = document.createElement("DIV");
	var xgEBEdit	= document.createElement("INPUT");
	var xgEBParam = document.createElement("INPUT");

	if (this.EditType.indexOf("(")>-1){
		this.EditLen	= this.EditType.substring(this.EditType.indexOf("(")+1,this.EditType.indexOf(")"));
		this.EditType = this.EditType.substring(0,this.EditType.indexOf("("));
	}
	if (this.EditType == "text")
			xgEBEdit.type = this.EditType;
	if (this.EditType == "password")
			xgEBEdit.type = this.EditType;


	if (this.EditType == "url")
	{
			xgEBEdit.type="text";
			this.checkFor=true;
			this.checkArg=".";
	}
	if (this.EditType == "hidden")
	{
		xgEBEdit.type = "hidden";
	}
	if (this.EditType == "e-mail")
		{
		xgEBEdit.type ="text";
		this.checkFor=true;
		this.checkArg = "@";
		}
	if (this.EditType == "file")
	{
		xgEBEdit.type = "text";

	}

	var xgEBLabel	= document.createElement("DIV");

	if (Browser.sName == "ie"){
							try{
							this.ParentObject.insertBefore(xgEBSurface);
							xgEBSurface.insertBefore(xgEBLabel);
							xgEBSurface.insertBefore(xgEBEdit);
							xgEBSurface.insertBefore(xgEBParam);
							}catch(e){
									info(e);
								}
							}
						else
							{
							this.ParentObject.appendChild(xgEBSurface);
							xgEBSurface.appendChild(xgEBLabel);
							xgEBSurface.appendChild(xgEBEdit);
							xgEBSurface.appendChild(xgEBParam);
							}

	xgEBSurface.id = "surface_" + this.id;
	xgEBEdit.id = "edit_" + this.id;
	xgEBEdit.readOnly=this.readOnly;
	xgEBLabel.id = "label_" + this.id;
	xgEBSurface.title = this.Title;
	xgEBEdit.title = this.Title;
	if (xgEBEdit.readOnly == true){
		xgEBEdit.style.cursor = "default";
		xgEBEdit.unselectable="on";
	}

xgEBEdit.title=this.ToolTip;

	this.ParentObject.style.overflow="hidden";
	this.Label = replaceStr(this.Label," ","&nbsp;");
		var _twidth="100%";
		if (this.LabelWidth >-1) _twidth = this.LabelWidth;
			var tempHTML ="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\""+_twidth+"\" height=\""+this.dHeight+"\">";
				tempHTML +="<tr>";
					tempHTML += "<td align=\"left\" valign=\"middle\">";
						tempHTML +="<div class=\""+this.LabelClassName+"\">";
							tempHTML +=this.Label;
						tempHTML +="</div>";
					tempHTML +="</td>";
				tempHTML += "</tr>";
			tempHTML += "</table>";
		
	xgEBSurface.style.position = this.dPosition;
	xgEBSurface.style.left = this.dLeft;
	xgEBSurface.style.top = this.dTop;
	xgEBSurface.style.width = this.dWidth;
	xgEBSurface.style.height = this.dHeight;

	xgEBParam.style.position = "absolute";
	xgEBParam.style.left = 0;
	xgEBParam.style.top = 0;
	xgEBParam.style.width=0;
	xgEBParam.style.height = 0;
	xgEBParam.style.backgroundColor = "transparent";
	xgEBParam.style.border = "none";

	this._param = xgEBParam;
	this._param.value = this.value;
	this.nValue = this.value;


	xgEBLabel.className = this.LabelClassName;
	xgEBLabel.style.position = "absolute";
	xgEBLabel.style.left = 0;
	xgEBLabel.style.top = 0;
	xgEBLabel.style.width = 0;
	xgEBLabel.style.height = 0;
	xgEBLabel.style.overflow = "visible";
	xgEBLabel.innerHTML = tempHTML; 

	xgEBLabel.style.zIndex = 100 - this.EditBoxIndex;



	if (this.Required == true){
		var xreq = document.createElement("DIV");
		if (Browser.sName == "ie"){
			xgEBSurface.insertBefore(xreq);
			}else{
				xgEBSurface.appendChild(xreq);
		}
		xreq.style.position="absolute";
		xreq.style.left=this.dWidth+2;
		xreq.style.top=0;
		xreq.style.height = this.dHeight;
		xreq.className = this.RequiredClassName;

		this.ParentObject.style.width = this.dWidth+10;
		xreq.innerHTML="*";
	}//this.Required

			var fileInput ;
			var fuContainer;
		if (this.EditType == "file"){
			fileInput = document.createElement("INPUT");
			fuContainer = document.createElement("DIV");

			fileInput.type="file";

			if (Browser.sName == "ie")
			{
				xgEBSurface.insertBefore(fuContainer);
				fuContainer.insertBefore(fileInput);
			}else{
				xgEBSurface.appendChild(fuContainer);
				fuContainer.appendChild(fileInput);
			}
			fuContainer.style.position="absolute";
			fuContainer.style.left=this.dWidth-28;
			fuContainer.style.top=0;
			fuContainer.style.width=30;
			fuContainer.style.height = this.dHeight;
			fuContainer.style.backgroundColor="transparent";
			fuContainer.style.overflow="hidden";
			fuContainer.style.visibility = "hidden";
			fileInput.style.position="absolute";
			fileInput.style.fontFamily="Tahoma";
			if (xg.browser.sName != "opera"){
			fileInput.style.fontSize=1;
			}else{
				fileInput.style.fontSize =9;
			}
			fileInput.style.left = -25;
			fileInput.style.top = 0;
			fileInput.style.width = 25;
			fileInput.style.height =this.dHeight;
		}


// setez numele parametrului
	if (this.ParamName != ""){
			this._param.name = this.ParamName;
	}

          var xgEBWidth = 0;
		  var xgEBHeight = 0;
		
		
		try{
		var d = xg.util.calculateTextSize(this.Label,this.LabelClassName);
	
		xgEBWidth = d.Width;
		xgEBHeight = d.Height;
		}catch(e){
		}

	//this.value = xgEBLabel.offsetWidth;

	if (this.LabelWidth>0) {
		xgEBWidth = this.LabelWidth;
	}else{
		this.LabelWidth = xgEBWidth;
	}

	xgEBLabel.style.width = xgEBWidth;
	xgEBLabel.style.height = xgEBHeight;
//	xgEBLabel.style.overflow = "hidden";

	xgEBEdit.className = this.EditClassName;

	xgEBEdit.style.position = "absolute";
	xgEBEdit.style.left = xgEBWidth+1;
	xgEBEdit.style.top=0;
	try{
	xgEBEdit.style.width = this.dWidth - xgEBWidth;
	}catch(E){
		alert(E);
	}
	xgEBEdit.style.height = this.dHeight;
	xgEBEdit.style.zIndex = 110- this.EditBoxIndex;

	this.allreadyDot = false;
	this._internalInput = xgEBEdit;
	this.setValue(this.nValue);



	xgEBEdit.tabIndex = this.tabIndex;

	var	xgCurrentEditBox = this.EditBoxIndex;

	xgEBEdit.onclick = function(){
		xgDocument.currentObject = xgAllEditBoxes[xgCurrentEditBox];
		try{
			eval(xgAllEditBoxes[xgCurrentEditBox].onclick());
		}
		catch (e){}
	}

	var myedit = this;

	xgEBEdit.onfocus = function(){
		try{
		this.value = myedit.nValue;
		if (myedit.nValue.indexOf(".") == -1){
			myedit.allreadyDot = false;
		}
		}catch(e){
		}

		if (myedit.focusable==false)	{
			return false;
		}
		xgEBEdit.select();
		if (myedit.disable == false){
		xgEBEdit.className = myedit.FocusClassName;
		myedit.Active = true;
		}
		try{
				myedit.onfocus();
		}catch(e){}
	}


	xgEBEdit.onselectstart = function(){
		if (myedit.readOnly){
					return false;
		}
	}

	xgEBEdit.onblur = function(){
		//this.value = myedit.formatValue(new String(myedit.nValue),myedit.EditType,myedit.EditLen);
		xgEBEdit.className = myedit.EditClassName;
		myedit.Active = false;
		try{
			myedit.onblur();
		}catch(E){
		}
	}
	
	xgEBEdit.onmouseover = function(){
		if (myedit.disable == false){
		if (myedit.Active == false)	xgEBEdit.className = myedit.MouseOverClassName;
		}
	}
	
	xgEBEdit.onmouseout = function(){
		if (myedit.disable == false){
		if (myedit.Active == false)	xgEBEdit.className = myedit.EditClassName;
		}
	}

	xgEBEdit.onkeyup = function(evt){
		if (xg.browser.sName == "ie") evt = event;
		if ((evt.keyCode == 8) || (evt.keyCode == 46)){
			if (myedit.allreadyDot == true){
				if (this.value.indexOf(".")==-1){
					myedit.allreadyDot = false;
				}
			}
		}

		try{
			myedit._param.value = this.value;
			myedit.onkeyup(evt);
		}catch(e){}
	}//on key up

	xgEBEdit.onkeydown = function(evt){
		if (xg.browser.sName != "ie") event = evt;
		if (xg.browser.sName == "ie"){
			if (event.keyCode == 13) xgEBEdit.onchange();
			}

			if (new String("8,46,37,39,38,40,36,35").indexOf(event.keyCode)>=0){
				return true;
			}

		if (myedit.EditType == "integer"){
			if (xg.util.isNumber(event.keyCode) == false)
				return false;
		}//integer

		if (myedit.EditType == "long"){
			if (new String("109,107").indexOf(event.keyCode)>=0){
				return true;
			}
			if ((event.keyCode == 189) && (event.shiftKey == false)){
				return true;
			}
			if ((event.keyCode == 187) && (event.shiftKey == true)){
				return true;
			}
			if ((event.keyCode == 190) && (event.shiftKey == false) && (myedit.allreadyDot == false)){
				myedit.allreadyDot = true;
				return true;
			}		

			if ((event.keyCode == 110) && (event.shiftKey == false) && (myedit.allreadyDot == false)){
				myedit.allreadyDot = true;
				return true;
			}		
				
			if (xg.util.isNumber(event.keyCode) == false){
				return false;
			}
		}// long

		if (myedit.EditType == "number"){
			if (new String("109,107").indexOf(event.keyCode)>=0){
				return true;
			}
			if ((event.keyCode == 189) && (event.shiftKey == false)){
				return true;
			}
			if ((event.keyCode == 187) && (event.shiftKey == true)){
				return true;
			}

			if (xg.util.isNumber(event.keyCode) == false){
				return false;
			}
		}// number

		if (myedit.EditType == "string"){
			if ((xg.util.isNumber(event.keyCode) == true) && (event.shiftKey == true)){
				return true;
			}
			if (xg.util.isLetter(event.keyCode) == true){
				return true;
			}else{
				return false;
			}
		}//string
		try{
				myedit.onkeydown();
		}catch(e){}
	}//on keydown

	xgEBEdit.onkeypress = function(){
		try{
			myedit.onkeypress();
		}catch(e){}
	}
	

	xgEBEdit.onchange = function()
	{
		var errMess = false;
		if (xgAllEditBoxes[xgCurrentEditBox].checkFor == true){
			var cond = xgAllEditBoxes[xgCurrentEditBox].checkArg;
			if (this.value.length>0){
				if (this.value.indexOf(cond)==-1){
					errMess = true;
			}else{
				if (cond=="@"){
					if (this.value.indexOf(".")==-1){ //daca nu exista punct		
							errMess = true;
							}else{
							 if (this.value.indexOf(".",this.value.indexOf("@"))==-1) //daca exista dar nu este dupa @
							errMess = true;						
							}
				}
			}
		}
		}		
		if (errMess == true)
			{
				alert("$res(xg.input.invalidAddress,$var(lang))");
				xgAllEditBoxes[xgCurrentEditBox].setFocus();
				if (xgAllEditBoxes[xgCurrentEditBox].Required == true)	{
					xgAllEditBoxes[xgCurrentEditBox].setFocus();
				}
				return false;
			}
			var meb = xgAllEditBoxes[xgCurrentEditBox];
			meb.nValue = this.value;
			meb.setValue(this.value);
		    meb._param.value = meb.nValue;
		try{
			eval(meb.onchange(this.value));
		}
		catch (e)	{	}

	}


	xgEBLabel.onselectstart =  function(){return false;}

}

	function xgEB_GetValue(){
		return this._param.value;
	}

	function xgEB_SetValue(val){
		val = replaceStr(val,"&nbsp;"," ");
		var xInput  = document.getElementById("edit_" + this.id);
		this.nValue = val;
		this._param.value = val;
		var xv =new String(this.formatValue(new String(val),this.EditType,this.EditLen));

		if ((xv.indexOf(".")>-1) && (this.EditType == "long")){
			this.allreadyDot = true;
		}
		xInput.value=xv;
	}//setvalue

	function xgeditbox_setDisable(disable){
	_surface = document.getElementById("surface_"+this.id);		
	_edit = document.getElementById("edit_" + this.id);
	if (disable == true){
			this.disable = disable;
			_edit.readOnly = true;
			if (xg.browser.sName != "opera")
			xg.util.setDisable(_surface);
	}else{
			this.disable = false;
			xg.util.setEnable(_surface);
			_edit.readOnly = false;
	}	

}

	function xgeditbox_setVisible(visible){
	_surface = document.getElementById("surface_"+this.id);		

	if (visible == true){
			_surface.style.visibility = "visible";
	}else{
			_surface.style.visibility = "hidden";
	}	
	}

	function xgEB_setParamValue(param){
		this._param.value = param;	
	}

	function xgEB_getParamValue(){
		return this._param.value;
	}

	function xgeditbox_formatValue(value,format,len){
		var intreg = "";
		var rest = "";
		if (format == "long"){
			if (value.indexOf(".")>-1){
					intreg = value.substring(0,value.indexOf("."));
					rest	= value.substring(value.indexOf(".")+1);
			}else{
				intreg = value;
				rest = "0";
			}
			intreg = __split(intreg);
			if (rest.length > len)	{
				rest = rest.substring(0,len);
			}
			if (intreg.length == 0)	{intreg = 0	}
			return intreg+"."+rest;
		}// long
		else if (format == "number"){
			return value;
		}else if(format == "integer"){
			return value;
		}
		else if (format == "string"){
			return value;
		}
		else if (format == "text"){
			return value;
		}else if (format == "password"){
			return value;
		}else if (format == "e-mail"){
			return value;
		}else if (format == "url"){
			return value;
		}else{
			return "-";
		}
	}

	function __split(str){
		var newstr ="";
		var k = str.length-1;
		var c = 0;
		while (k >= 0){
			newstr = str.substring(k,k+1)+newstr;
			c++;
			if (c == 3){
				newstr =","+newstr;
				c=0;
			}
			k--;
		}
		if (newstr.indexOf(",") == 0){
			newstr = newstr.substring(1);
		}

		return newstr;
	}

	function xgEB_setReadOnly(ro){
		this.readOnly = ro;
		this._internalInput.readOnly = ro;
	}