// 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')	// Is Microsoft Internet Explorer
		return new Size(event.clientX,event.clientY);
	else
		return new Size(ev.pageX,ev.pageY);
}

function validation(sender, type, ttarget, disable, bound_min, bound_max, chars) {
	switch(type) {
		case "numeric":
			var val=sender.getValue();
			var condition=val>=bound_min && val<=bound_max; // Numarul se afla intre bounds
			document.getElementById(ttarget).innerHTML=(condition==true)?"":'Introduceti o valoare cuprinsa intre '+bound_min+' si '+bound_max;
			var arr=new Array();
			arr=disable.split(':');
			for (i=0;i<arr.length;i++)
				document.getElementById(arr[i]).style.display=(condition==true)?'inline':'none';

			break;
			
		case "string":
			
			break;
			
		case "regex":
			
			break;
	}
	
}

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()+'px';
		t.height=sz.getHeight()+'px';
		}
	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);
		}     
	// GS Enabled
	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;
		}
	
	// Checks
	this.setChecked=function(b) {
		this.element.checked=(b==true?"checked":"");
	}
	this.isChecked=function() {
		return this.element.checked
	}
	
	// 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()
{

}


function URLEncode(txt)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = txt;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(txt)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = txt;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
