/*
Javascript base
*/

// Size object
function Size(width,height)
{
	this.width=new String(width).replace('px','');
	this.height=new String(height).replace('px','');
	this.setSize = function(w,h) { this.setWidth(w); this.setHeight(h); }
	this.getWidth = function() { return this.width; }
	this.getHeight = function() { return this.height; }
	this.setWidth = function(w) { this.width=w; }
	this.setHeight = function(h) { this.height=h; }
	this.substract = function(wx,hy) { return new Size(this.width-wx,this.height-hy); }
	this.toString=function() {return 'L: '+this.getWidth()+' T: '+this.getHeight();}
}

//const BROWSER_IEXLORE 1;
//const BROWSER_FIREFOX 2;
//const BROWSER_OPERA 3;
//const 

var browser='unknown';

function JSF()
{
	var t=navigator.userAgent.toLowerCase();
	
	if (t.indexOf('msie')!=-1)
		browser='msie';
	else if (t.indexOf('firefox')!=-1)
		browser='firefox';
	else if (t.indexOf('opera')!=-1)
		browser='opera';
	else browser='unknown';
}

// Option object
function ListOption(text,value)
{
	this.text=text;
	this.value=value;
	this.setText=function(txt) {this.text=txt;}
	this.getText=function() {return this.text;}
	this.setValue=function(val) {this.value=val;}
	this.getValue=function() {return this.value;}
}

function LanguageItem()
{
	var lng={};
	this.addString=function(lp,str)
		{
		lng[lp]=str;
		}
	this.getString=function(lp)
		{
		return lng[lp];
		}
}

function CreatedElementsArray()
{
	var arr=new Array();
	this.addElement=function(elem)
		{
		arr[arr.length]=elem;
		}
	this.getElementsCount=function()
		{
		return arr.length;
		}
	this.getElement=function(idx)
		{
		return arr[idx];
		}
}

var cea=new CreatedElementsArray();

// Get mouse position
function getGlobalMousePosition(ev)
{
	if (browser=='msie')
		return new Size(event.clientX,event.clientY);
	else
		return new Size(ev.pageX,ev.pageY);
}

function getElement(id)
{
	this.id=id;
	this.element=document.getElementById(id);
	cea.addElement(this);
	
	this.getDOMReference=function() {
		return this.element;	
	}
	
	// GS Language
	this.setLanguage=function(lng)
		{
		this.lng=lng;
		}
	this.getLanguage=function()
		{
		return this.lng;
		}
	
	// GS Caption
	this.getCaption=function() {return this.element.innerHTML;}
	this.setCaption=function(cap) {this.element.innerHTML=cap;}
	// GS Visibility
	this.getVisible=function() {var t=this.element.style.display;if (t=='' || t==undefined || t==null)return true;if (t=='block' || t=='inline')	return true;if (t=='none')	return false;}
	this.setVisible=function(flag) {if (flag==true)	this.element.style.display='block'; else this.element.style.display='none';}
	// GS Size
	this.setSize=function(sz) 	
		{
		var t=this.element.style;
		t.width=sz.getWidth();
		t.height=sz.getHeight();
		}
	this.getSize=function()		
		{
		var t=this.element.style;
		return new Size(t.width,t.height);
		}
	// GS Position
	this.setPosition=function(sz)
		{
		var t=this.element.style;
		t.top=sz.getHeight();
		t.left=sz.getWidth();
		}
	this.getPosition=function()
		{
		var t=this.element.style;
		return new Size(t.left,t.top);
		}
	this.setEnabled=function(flag)
		{
		this.element.disabled=!flag;
		}
	this.getEnabled=function()
		{
		return !this.element.disabled;
		}
		
	this.setFocus=function()
		{
		this.element.focus();
		}
		
	// Get mouse position
	this.getMousePosition=function(ev)
		{
		var s=getGlobalMousePosition(ev);
		var ns=s.substract(this.getPosition().getWidth(),this.getPosition().getHeight());
		return ns;
		}
	
	// Get element type
	this.getElementType=function()
		{
		return this.element.tagName;
		}
	
	// GS Image (src)
	this.getImage=function()
		{
		return this.element.src;
		}
	this.setImage=function(img)
		{
		this.element.src=img;
		}
	
	// Styles
	this.getStyles=function()
		{
		return this.element.style;
		}
	this.getStyle=function(sid)
		{
		return this.element.style[sid];
		}
	this.setStyle=function(prop,val)
		{
		this.element.style[prop]=val;
		}
	this.deleteStyle=function(prop)
		{
		this.element.style[prop]='';
		}
		
	// Listbox/Combobox only!
	this.addItem=function(opt)
		{	
		var i_opt=new Option(opt.getText(),opt.getValue());
		try
			{
			this.element.add(i_opt,null);
			}
		catch(ex)
			{
			this.element.add(i_opt);
			}
		}	
	this.removeItem=function(idx)
		{
		try {
			this.element.remove(idx);
			return true;
			}
		catch(ex)
			{
			return false;
			}
		}
	this.removeSelected=function()
		{
		this.removeItem(this.element.selectedIndex);
		}
	this.getItem=function(idx)
		{
		var t=this.element.options[idx];
		return new ListOption(t.text,t.value);
		}
	this.getSelected=function()
		{
		var t=this.element.options[this.element.selectedIndex];
		return new ListOption(t.text,t.value);
		}
	this.setSelectedValue=function(val)
		{
		var t=this.element.options[this.element.selectedIndex];
		this.element.options[this.element.selectedIndex].value=val;
		}
	this.setSelectedText=function(txt)
		{
		var t=this.element.options[this.element.selectedIndex];
		this.element.options[this.element.selectedIndex].text=txt;		
		}
	this.getSelectedIndex=function()
		{
		return this.element.selectedIndex;
		}
	this.getItemsCount=function()
		{
		return this.element.length;
		}
	this.setSelectedIndex=function(idx)
		{
		this.element.selectedIndex=idx;
		}
	this.clear=function()
		{
		this.element.options.length=0;		
		}
		
	// TextBox/Input 
	this.setValue=function(val)
		{
		this.element.value=val;
		}
	this.getValue=function()
		{
		return this.element.value;
		}
	this.selectAll=function()
		{
		this.element.select();
		}
		
	// Forms
	this.setAction=function(str)
		{
		this.element.action=str;
		}
	this.getAction=function()
		{
		return this.element.action;
		}
	this.submit=function()
		{
		this.element.submit();
		}
		
	// Links
	this.setLink=function(str)
		{
		this.element.href=str;
		}
	this.getLink=function()
		{
		return this.element.href;
		}
		
	// GS Events
	this.setEvent_Click=function(evs) {this.element.onclick=evs;}
	this.setEvent_DoubleClick=function(evs) {this.element.ondblclick=evs; }
	this.setEvent_MouseOver=function(evs) {this.element.onmouseover=evs; }
	this.setEvent_MouseOut=function(evs) {this.element.onmouseout=evs; }
	this.setEvent_MouseDown=function(evs) {this.element.onmousedown=evs; }
	this.setEvent_MouseUp=function(evs) {this.element.onmouseup=evs; }
	this.setEvent_MouseMove=function(evs) {this.element.onmousemove=evs; }
	this.setEvent_Change=function(evs) {this.element.onchange=evs; }
	this.setEvent_KeyUp=function(evs) {this.element.onkeyup=evs;}
	this.setEvent_KeyDown=function(evs) {this.element.onykeydown=evs;}
	this.setEvent_Select=function(evs) {this.element.onselect=evs;}
	
}

function Timer(fp,itv)
{
	var tim;
	var fp=fp;
	var itv=itv;
	this.start=function()
		{
		tim=setInterval(fp,itv);
		}
	this.stop=function()
		{
		clearTimeout(tim);
		}
}

function JSArray()
{
	var iarr=new Array();
	this.add=function()
		{
		if (arguments.length==0) return false;	
		for (i=0;i<arguments.length;i++)
			iarr[iarr.length]=arguments[i];
		}
	this.removeAt=function(idx)
		{
		for (i=idx;i<iarr.length-1;i++)
			{
			iarr[i]=iarr[i+1];
			}
		iarr[iarr.length-1]=null;
		}
	this.remove=function(obj)
		{
		for (i=0;i<iarr.length;i++)
			if (obj==iarr[i])
				this.removeAt(i);
		}
	this.toString=function()
		{
		var s='{';
		for (i=0;i<iarr.length;i++)
			if (iarr[i]!=null) s+=iarr[i]+' ';
		s+='}';
		return s;
		}
}

function Color()
{

}



