//----------------------------------------------\\

var zIndex=300;
function Control(objHtmlElement){

	if(!objHtmlElement) {

		alert("Control Object Error:can't construct control with a null htmlElement");

		return(null);

	}

	this.zIndex					=	zIndex;

	this.type						=	'GUIControl';

	this.errors						= '';

	this.enabled					=	true;

	this.focused					=	false;

	this.name						=	'anonymouse';

	this.parent					=	null;

	this.tabIndex					=	-1;

	this.text						=	'';

	this.visible						=	true;

	this.width						=	-1;

	this.height						=	-1;

	this.hasChildren				=	false;

	this.created					=	true;

	this.styleClassName		=	'';

	this.destroyed				=	false

	this.dSourse					=	null;

	this.hasSource				=	false;

	this.bound						=	false;

	this.html						=	objHtmlElement;

	this.controls					=	new IList(this);

	this.html.owner				=	this;

	

	this.setAtTop=function(){this.html.style.zIndex=zIndex++;};

	this.setSource=function(dSource){

		if(!dSource){alert("Control.setSource Error:the data source object is null, control type:"+this.type+" name:"+this.name);return false}

		this.dSourse=dSource;

		this.hasSource=true;

		this.onSourceChanged();

		return true;

	};

	this.controls.append=function(objControl){

		if(!objControl){alert('Control.append() Fetal Error:atempt to append a null object');return false;}

		objControl.parent=this.owner;

		objControl.construct();

		if(!objControl) {alert(this.name+'.append('+objControl+')');return;}

		this.add(objControl);

		this.owner.html.appendChild(objControl.html);

		return true;

	};

	this.controls.Delete=function(objControl,index){

		//alert("deleting "+objControl.name);

		if(!objControl && !index) return;

		if(objControl) index=this.indexOf(objControl);

		else		  objControl=this.valueOf(index);

		//alert(objControl.name+'-'+index);

		objControl.destruct();

		//alert("Control destroyed:"+objControl.destroyed);

		this.removeAt(index);

	};

	this.destruct=function(){

		//alert("Destroyeing "+this.name+","+this.type);

		this.controls.moveFirst();

		for(;!this.controls.atEnd();this.controls.moveNext()){

			if(this.controls.current().destruct)this.controls.current().destruct();

		}

		this.controls=null;

		this.Events=null;

		this.destroyed=true;

		this.html.parentNode.removeChild(this.html);

		return (this.destroyed);

		

	};

	this.focus=function(){

		this.html.focus();

	};

	this.show=function(){this.html.style.display="";};

	this.hide=function(){this.html.style.display="none";};

	this.toggleVisibility=function(){

		if(this.html.style.display=="none")

			this.show();

		else

			this.hide();

	};

	

	this.enable=function(){

		this.html.disabled="";

		this.enabled=true;

	};

	this.disable=function(){

		this.html.disabled="disabled";

		this.enabled=false;

	};

	this.validate=function(){

		

		//if(tb.uniqe){}

		if(this.maxLength){

			//alert(this.text.length+'>?'+this.maxLength);

			if(this.text.length>this.maxLength){

				this.errors+='The Length of '+this.parent.name+' is more than ('+this.maxLength+')';

			}

		}

		//if(this.defaultValue){}

		if(this.dataType){

			if(this.dataType=='Int')	{

				var re = new RegExp("([^0-9]+)","ig");re.exec(str);

  				if(RegExp.$1){this.errors+='you must enter only digits';};

			}

		}

		if(this.errors) return false;

		return true;

	};

	//abstruct functions

	this.update=function(){alert("Control Warning:update function should be overridden. this=(name:"+this.name+",type:"+this.type+")");};

	this.construct=function(){alert("Control Warning:construct function should be overridden. this=(name:"+this.name+",type:"+this.type+")");};

	this.updateHtml=function(){alert("Control Warning:updateHtml function should be overridden. this=(name:"+this.name+",type:"+this.type+")");};

	this.onSourceChanged=function(){alert("Control Warning:onSourceChanged function should be overridden. this=(name:"+this.name+",type:"+this.type+")");};

	

	//--------------------
};
//--------------------------------------------------------
function Holder(name,text,defaultWidth,fixed){

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

	this.control= Control;

	this.control(html);

	this.html.className='holder';

	this.controls.append=function(objControl){

		if(!objControl){alert('Control.append() Fetal Error:atempt to append a null object');return false;}

		objControl.parent=this.owner;

		objControl.construct();

		if(!objControl) {alert(this.name+'.append('+objControl+')');return;}

		this.add(objControl);

		this.owner.html.Body.appendChild(objControl.html);

		return true;

	}

	this.text=text;

	this.name=name;

	this.html.id=name;

	this.width=parseInt(defaultWidth)>349?parseInt(defaultWidth):800;

	this.html.style.width=this.width+'px';

	this.html.style.zIndex=zIndex++;

	this.html.style.position='relative';

	

	this.focused=function(){

		if(!this.html.style.zIndex||this.html.style.zIndex<(zIndex-1))

		this.html.style.zIndex=zIndex++;

	};

	this.html.onclick=this.focused.fasten(this);

	//create controls

	this.html.Cpanel=document.createElement("DIV");

	

	this.html.Cpanel.className='holderCpanel';

	this.html.appendChild(this.html.Cpanel);

	this.html.Cpanel.innerHTML="<table class='holderTitleBar' width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td nowrap='nowrap'></td><td bgcolor='#999999' width='100%'>&nbsp;</td><td></td></tr></table>";

	

	this.html.Title=document.createElement("SPAN");

	this.html.Cpanel.firstChild.rows[0].cells[0].appendChild(this.html.Title);

	this.html.Shutter=new Link(this.name+'Shutter','Close');

	this.html.Shutter.construct();

	this.html.Shutter.onClick=this.hide.fasten(this);

	this.html.Cpanel.firstChild.rows[0].cells[2].appendChild(this.html.Shutter.html);

	

	this.resetPosition=function()	{

		this.html.style.position='relative';

		this.html.style.top='';

		this.html.style.left='';

	}//resetPosition End;

	if(!fixed){

		this.html.Cpanel.firstChild.rows[0].cells[1].onmousedown=Drag.startDrag.fasten(Drag);

		this.html.Cpanel.firstChild.rows[0].cells[1].ondblclick=this.resetPosition.fasten(this);

		this.html.Cpanel.firstChild.rows[0].cells[1].style.cursor='move';

	}

	this.html.Body=document.createElement("DIV");

	this.html.Body.style.overflowX='scroll';

	this.html.Body.style.width=(this.width)+'px';

	this.html.Body.className='holderBody';

	this.html.appendChild(this.html.Body);

	

	this.html.Foot=document.createElement("DIV");

	this.html.Foot.className='holderFoot';

	this.html.appendChild(this.html.Foot);

	this.update=function(){

		this.controls.moveFirst();

		for(;this.controls.atEnd();this.controls.moveNext()){

			this.controls.current().update();

		}

	};

	this.construct=function(){

		this.updateHtml();

	};

	this.updateHtml=function(){

		this.html.Title.innerHTML=this.text;

	};	

	this.reposit=function(){

		var x=0;

		var y=0;

		currentNode=this.owner.html;

		while(currentNode && currentNode.tagName!='HTML'){

			x+=currentNode.offsetLeft;

			y+=currentNode.offsetTop;

			currentNode=currentNode.offsetParent;

		}

		this.html.style.left = (x+'px');

		this.html.style.top =((y+this.owner.html.offsetHeight)+'px');

	};

	this.updateHtml();

	this.setTitle=function(Sender){

		this.text=Sender.tableTitle;

		this.updateHtml();

	};//setTitle End;

	

};

//--------------------------\\

function CheckBox(name,text){

	var html=document.createElement("INPUT");

	html.type="checkbox";

	this.control= Control;

	this.control(html);

	

	this.type="CheckBox";

	if(name)this.name=name;

	if(text)this.text=text;

	

	this.construct=function(){

		

		this.html.value='1';

		this.html.name=this.name;

		this.html.className=this.styleClassName;

		this.html.onclick=this.preChange.fasten(this);

		this.updateHtml();

	}

	this.htmlUpdate=function(){

		this.text=this.html.checked?'1':'0';

	};

	this.updateHtml=function(){

		this.html.checked= (this.text=='1' || this.text==1 || this.text==true) ? 'checked':'';

	};

	this.enable=function(){

		this.html.disabled="";

		this.enabled=true;

	};

	this.disable=function(){

		this.html.disabled="disabled";

		this.enabled=false;

	};

	this.preChange=function(){this.htmlUpdate();this.onChange(this)};

	this.onChange=function(){};

};

//--------------------------\\

function DateBox(name,text,datePicker){

	this.control=TextBox;

	this.control(name,text);

	if(datePicker)this.datePicker=datePicker;

	this.type="DateBox";

	this.construct=function(){

		this.html.name=this.name;

		this.html.value=this.text;

		//this.html.readOnly="readonly";

		this.html.size="8";

		this.html.className=this.styleClassName;

		//this.html.onfocus=this.focused.fasten(this);

		this.html.onblur=this.blured.fasten(this);

		this.html.onclick=this.clicked.fasten(this);

	}

	this.setDatePicker=function(datePicker){

		this.datePicker=datePicker;

	};

	//event handlers

	this.focused=function(){this.datePicker.setOwner(this);this.datePicker.show();};

	this.blured=function(){this.text=this.html.value};

	this.clicked=function(evt){if(!evt)evt=event;this.datePicker.setOwner(this,evt);this.datePicker.toggleVisibility();};

};

//--------------------------\\

function TextBox(name,text,size){

	var html=document.createElement("INPUT");

	this.control= Control;

	this.control(html);

	this.type='TextBox';

	if(name)this.name=name;

	if(text)this.text=text;

	if(size && size>1)this.html.size=size;

	

	this.construct=function(){

		this.html.value=this.text;

		this.html.name=this.name;

		this.html.className=this.styleClassName;

		this.html.onblur=this.htmlUpdate.fasten(this);

	}

	this.htmlUpdate=function(){

		this.text=this.html.value;

	};

	this.updateHtml=function(){

		this.html.value=this.text;

	};

	this.enable=function(){

		this.html.disabled="";

		this.enabled=true;

	};

	this.disable=function(){

		this.html.disabled="disabled";

		this.enabled=false;

	};

};

//----------------------------

function Lable(name,text){

	var html=document.createElement("Span");

	this.control= Control;

	this.control(html);

	this.type="Lable";

	if(name)this.name=name;

	if(text)this.text=text;

	this.construct=function(){

		this.html.innerHTML=this.text;

		//this.html.name=this.name;

		this.html.className=this.styleClassName;

	}

	this.updateHtml=function(){

		this.html.innerHTML=this.text;

	};

	

	this.enable=function(){

		this.html.disabled="";

		this.enabled=true;

	};

	this.disable=function(){

		this.html.disabled="disabled";

		this.enabled=false;

	};

	

};

//------------------------------------

function Link(name,text){

	var html=document.createElement("A");

	//this.linkHolder=document.createElement("SPAN");

	//html.href='#';

	this.control= Control;

	this.control(html);

	this.html.style.paddingLeft=this.html.style.paddingRight='4px';
	//this.html.style.color='blue';
	this.html.style.cursor='pointer';

	//this.linkHolder.appendChild(this.html);

	if(name)this.name=name;

	if(text)this.text=text;

	

	this.construct=function(){

		this.html.innerHTML=this.text;

		this.html.onclick=this.invokeOnClick.fasten(this);

	}

	this.updateHtml=function(){

		this.html.innerHTML=this.text;

	};

	

	this.enable=function(){

		this.html.style.color='#0000FF';

		this.html.style.textDecoration="underline ";

		this.html.onclick=this.onClick;

		this.enabled=true;

	};

	this.disable=function(){

		//this.html.setAttribute("style","color:#999999;text-decoration:none;");

		this.html.style.color='#999999';

		this.html.style.textDecoration="none";

		this.html.onclick='';

		this.enabled=false;

	};

	this.invokeOnClick=function(){

		this.onClick(this);

	}

	this.onClick=null;

};

//------------------------------------

function Button(name,text){

	var html=document.createElement("INPUT");

	html.type='button';

	this.control= Control;

	this.control(html);

	if(name)this.name;

	if(text)this.text=text;

	this.construct=function(){

		this.update();

	}

	this.update=function(){

		this.html.value=this.text;

		this.html.name=this.name;

		this.html.className=this.styleClassName;

		this.html.onclick=this.onClick;

		

	};

	this.enable=function(){

		this.html.disabled="";

		this.enabled=true;

	};

	this.disable=function(){

		this.html.disabled="disabled";

		this.enabled=false;

	};

	this.onClick;

};

//-------------------------------------------------

function ListBox(name,text,dTable,isListener,subscription,indActivePassiveCol,anyOption,showPassive){

	var html=document.createElement("Select");

	this.control= Control;

	this.control(html);

	this.type='ListBox';

	this.anyOption=anyOption;

	this.showPassive=showPassive;

	if(name)	this.name=name;

	if(text)		this.text=text;

	this.update=function(){

		this.updateHtml();

		this.html.name=this.name;

	};

	this.bind=function(dTable,isListener,subscription,indActivePassiveCol){

		if(!dTable){alert("ListBox.bind Error:can't bind a null dTable");return false}

		this.dTable=dTable;

		this.indActivePassiveCol=indActivePassiveCol;

		this.bound=true;

		if(isListener&&this.dTable.Events){

			var subscr=subscription?subscription:this.name;

			this.dTable.Events.addListener('DataRowInserted',subscr,this.update.fasten(this));

			this.dTable.Events.addListener('DataRowDeleted',subscr,this.update.fasten(this));

			this.dTable.Events.addListener('DataRowUpdated',subscr,this.update.fasten(this));

		}

		return true;

	};

	if(dTable){

		this.bind(dTable,isListener,subscription,indActivePassiveCol);

	}

		

	this.construct=function(){

		if(!this.dTable){alert("ListBox.construct Error:can't construct with a null dTable");return false}

		this.updateHtml();

		this.html.name=this.name;

		this.html.onchange=this.preChange.fasten(this);

		if(this.text===null)this.htmlUpdate;

		//alert("this.update exist ? :"+this.hasOwnProperty('update'));

	};

	this.updateHtml=function(){

		//alert(this.name+" is ubdating html");

		if(!this.dTable){alert("ListBox.updateHtml Error:can't construct with a null dTable");return false}

		indText=this.dTable.indListText;

		indValue=this.dTable.indListValue;

		

		this.html.options.length=0;

		if(this.anyOption)this.html.options[0]=new Option('any','any');

		for(var dx in this.dTable.dRows){

			if(!this.showPassive&&this.indActivePassiveCol&& !parseInt(this.dTable.dRows[dx].dItems[this.indActivePassiveCol].value)) continue;

			this.html.options[this.html.options.length]=new Option(this.dTable.dRows[dx].dItems[indText].value,this.dTable.dRows[dx].dItems[indValue].value);	

			if(this.html.options[this.html.options.length-1].value==this.text)this.html.options[this.html.options.length-1].selected='SELECTED';

		}

		//this.html.options[this.html.options.length]=new Option('addNew','Add New');

		//if(this.html.options[this.html.selectedIndex])alert(this.html.options[this.html.selectedIndex].text);

	};

	this.selectValue=function(val){

		var value=val?val:this.text;

		this.html.value=value;

	};

	this.htmlUpdate=function(){

		this.text=this.html.options[this.html.selectedIndex].value;

	};

	this.preChange=function(){this.htmlUpdate();this.onChange()};

	this.onChange=function(){};

};

//-------------------------------------------------

function GridNavigator(){

	this.recordsPerBlock=null;

	this.recordsTotal=null;

	this.blocksTotal=null;

	this.currentBlock=1;

	this.additionalData=[{'name':'PHPSESSID','value':Server.sessionId},{'name':'passcode','value':Server.passcode}];

	

	this.html=document.createElement("DIV");

	this.html.className='gridNav';

	this.html.innerHTML="<table width='100%'><tr><td nowrap='nowrap'></td><td align='center' width='100%'></td><td nowrap='nowrap'></td></tr></table>";

	

	this.construct=function(){

		this.setup();

					

		this.lbRecordsPerBlock=new ListBox('RecordsPerBlock',this.recordsPerBlock);

		this.lbRecordsPerBlock.onChange=this.aplaySetting.fasten(this);

		tmpArray=[];

		for(var i=1;i<(this.recordsPerBlock+5);i++)	tmpArray.push(i);

		

		this.lbCurrentBlock=new ListBox('SelectedBlock');

		this.lbRecordsPerBlock.dTable=this.createDTable(tmpArray,tmpArray);

		this.lbRecordsPerBlock.construct();

		this.html.firstChild.rows[0].cells[0].appendChild(this.lbRecordsPerBlock.html);

		this.html.firstChild.rows[0].cells[0].appendChild(document.createTextNode(" records per table."));

				

		this.lnkNext=new Link('Next','next');this.lnkNext.onClick=this.goNext.fasten(this);this.lnkNext.construct();

		this.lnkPrevious=new Link('Previous','previous');this.lnkPrevious.onClick=this.goPrevious.fasten(this);this.lnkPrevious.construct();

		

		this.lnkReload=new Link('Reload','Reload');this.lnkReload.onClick=this.fetchData.fasten(this);this.lnkReload.construct();

				

		tmpArray=new Array();

		for(var i=1;i<=this.blocksTotal;i++)	tmpArray.push(i);

		

		this.lbCurrentBlock=new ListBox('SelectedBlock','');

		this.lbCurrentBlock.dTable=this.createDTable(tmpArray,tmpArray);

		this.lbCurrentBlock.onChange=this.goToBlock.fasten(this);

		this.lbCurrentBlock.construct();

		

		this.html.firstChild.rows[0].cells[1].appendChild(this.lnkPrevious.html);

		this.html.firstChild.rows[0].cells[1].appendChild(this.lbCurrentBlock.html);

		this.html.firstChild.rows[0].cells[1].appendChild(this.lnkNext.html);

		

		this.html.firstChild.rows[0].cells[2].appendChild(this.lnkReload.html);

		

		this.updateHtml();

	};

	this.bind=function(dTable){

		if(!dTable){alert("GridNavigator.bind Error:can't bind a null dTable");return false}

		this.dTable=dTable;

		this.name=this.dTable.tableName+'Navigator';

		if(this.dTable.Events)

			this.dTable.Events.addListener('DataTableChanged',this.name,this.update.fasten(this));

		return true;

	};

	this.createDTable=function(arrText,arrValue){

		if(arrText.length!=arrValue.length){alert("GridNavigator.creatDTable Error:texts array doesn't match the values array");return false}

		var dTable=new Object();

		dTable.tableName='ListBoxData';dTable.indListText=1;dTable.indListValue=0;dTable.dRows=new Array();

		for(var dx in arrText){	

			dTable.dRows.push({'ordinal':dx,'dItems':[{'ordinal':0, 'value':arrValue[dx]},{'ordinal':1, 'value':arrText[dx]}]});

		}

		return(dTable);

	};

	this.htmlUpdate=function(){

		this.lbRecordsPerBlock.text=this.lbRecordsPerBlock.html.value;

		this.lbCurrentBlock.text=this.lbCurrentBlock.html.value;

	};

	this.updateHtml=function(){

		tmpArray=[];

		for(var i=1;i<=this.blocksTotal;i++)	tmpArray.push(i);

		this.lbCurrentBlock.dTable=this.createDTable(tmpArray,tmpArray);

		this.lbCurrentBlock.text=this.currentBlock;

		this.lbCurrentBlock.updateHtml();

		this.lnkReload.updateHtml();

		(this.blocksTotal<2)?this.lbCurrentBlock.hide():this.lbCurrentBlock.show();

		(this.currentBlock>=this.blocksTotal)?this.lnkNext.hide():this.lnkNext.show();

		(this.currentBlock<=1)?this.lnkPrevious.hide():this.lnkPrevious.show();

	};

	this.update=function(Sender){

		if(Sender.tableName != this.dTable.tableName)return;

		this.lbRecordsPerBlock.enable();

		this.lbCurrentBlock.enable();

		this.lnkNext.enable();

		this.lnkPrevious.enable();

		this.setup();

		this.updateHtml();

	};

	this.setup=function(){

		this.recordsTotal=this.dTable.recordsTotal;

		if(this.recordsPerBlock===null)this.recordsPerBlock=this.dTable.recordsPerBlock;

		this.blocksTotal=(this.recordsPerBlock===0)?0:Math.ceil(this.recordsTotal/this.recordsPerBlock);

	};

	this.fetchData=function(arrAdditionalData){

		

		if(arrAdditionalData && (typeof arrAdditionalData).toLowerCase()=="object" && arrAdditionalData.type=='RequestParameters')this.additionalData=arrAdditionalData;

		this.lbRecordsPerBlock.disable();

		this.lbCurrentBlock.disable();

		this.lnkNext.disable();

		this.lnkPrevious.disable();

		var SR=(this.currentBlock*this.recordsPerBlock)-this.recordsPerBlock;

		data=new Array({name:'startingRecord',value:SR},{name:'recordsNumber',value:this.recordsPerBlock});

		data=data.concat(this.additionalData);

		var op=this.dTable.readOnly?'View':'Manage';

		objR=new Request(dSet.Update.fasten(dSet),viewErrors,data,Server.address,'POST',-1,this.dTable.tableName+op);

		Server.handleRequest(objR);
		document.Loader.show();

	};

	this.goNext=function(){

		this.currentBlock++;

		this.fetchData();

	};

	this.goPrevious=function(){

		this.currentBlock--;

		this.fetchData();

	};

	this.goToBlock=function(){

		this.htmlUpdate();

		this.currentBlock=this.lbCurrentBlock.text;

		this.fetchData();

	};

	this.setupAutoRefresh=function(){

		if(this.cbRefresh.text=='1'){

			if(!this.interval) this.startAutoRefresh();

		}

		else this.stopAutoRefresh();

	};

	this.startAutoRefresh=function(){	this.interval=setInterval(this.fetchData.fasten(this),1000);};

	this.stopAutoRefresh=function(){clearInterval(this.interval);this.interval=null};

	this.aplaySetting=function(){

		this.htmlUpdate();

		this.currentBlock=1;

		this.recordsPerBlock=this.lbRecordsPerBlock.text;

		this.fetchData();

	};

}
//----------------------------
function Loader(){
	var html=document.createElement("DIV");
	this.control= Control;
	this.control(html);
	this.html.className='loader';
	this.html.innerHTML="<image src='GUI/loader.gif' />";
	this.html.style.zIndex=90000;
	this.hide();
}//Loader End;