// JavaScript Document
//Cross Browser getElement function
function getElement(n, d) { // original: MM_findObj v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getElement(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function is_object(){
	if(arguments[0].toString().indexOf("[object Object]") != -1) return(true);
	else return(false);
}

function in_array(obj,targetarray){ 
    return new RegExp('(^|\,)'+obj+'(\,|$)','gi').test(targetarray);
  } 

/* Javascript Job Queue */
/*var currentjobQueueID = 1;
var jobList = ObjectList();
*/





//Object list handlers

function ObjectList(){
	this.pointer = 0;
	this.objectType = null;
	this.items = new Array();
	this.loaded = false;
	this.Next = Next;
	this.Clear = function(){
		this.items = new Array();
		}
	this.Previous = Previous;
	this.First = First;
	this.Last = Last;
	this.GetItem = getItem;
	this.getItem = getItem;
	this.findItem = findItem;
	this.FindItem = findItem;
	this.IsEmpty = IsEmpty;
	this.Count = Count;
//	this.LoadList = LoadList;
	this.Insert = Insert;
	this.Delete = DeleteItem;
	this.getPointer = function(){
		return(parseInt(this.pointer));
		}
	this.setPointer = function(){
		this.pointer=arguments[0];
		}
	this.Current = Current;
	
	function Insert(){
		this.items[this.items.length] = new Item(arguments);
		this.pointer = this.items.length-1;
		return(this.items[this.pointer]);
		}

	function DeleteItem(){
		/*Deletes item at pointer*/
		this.items.splice(this.pointer,1);
		if(this.pointer >= this.items.length) this.pointer--;
		}
	function findItem(att,val){
		var i = 0;
		if(this.items.length){
			for(i=0; i< this.items.length; i++){
				if(this.items[i].getProperty(att)==val){ 
					this.setPointer(i);
					return(this.items[i]);
					}
				}
			}
		return(false);
	}
	function getItem(itemNo){
		if(this.items[itemNo-1]){
			this.pointer = itemNo-1;
			return(this.items[this.pointer]);
			}
		else return(false);
		}
	function Current(){
		if(this.items[this.pointer])
		return(this.items[this.pointer]);
		else return(false);
		}
	function First(){
		this.pointer = 0;
		return(this.items[this.pointer]);
		}
	function Last(){
		this.pointer = this.items.length-1;
		return(this.items[this.pointer]);
		}
	function IsEmpty(){
		if(this.items.length>0) return(0);
		else return(1);
		}
	function Reset(){
		this.items = null;
		this.items = new Array();
		return(1);
	}
	function Next(){
		if(this.pointer < (this.items.length-1)){
			this.pointer++;
			return(this.items[this.pointer]);
			}
		else return(false);
		}
	
	function Previous(){
		if(this.pointer>0){
			this.pointer--;
			return(this.items[this.pointer]);
			}
		else return(false);
		}
	function Count(){
		return(this.items.length);
	}
} // End of class ObjectList


function Item(){
	this.properties = new Array();
	this.setProperty = setProperty;
	this.addListProperty = addListProperty;
	this.getProperty = getProperty;	
	this.ajaxSetProperties = ajaxSetProperties;
	this.populate = populateItem;
	this.printProperties = printProperties;
	this.displayProperty = displayProperty;
	this.display = displayItem;
	this.Display = displayItem;
	this.Hide = hideElement;
	function populateItem(iProperties){
		if(iProperties.length>0){
			this.properties = iProperties;
			return(0);
			}
			else return(-1);
		}	
	function setProperty(pName,pValue){
		var oPropertyIndex = null;
		var i = 0;
		//alert('setting property '+pName+', current property count is '+properties.length);
		if(this.properties && this.properties.length){
			for(i=0;((i < this.properties.length) && (oPropertyIndex==null));i++){
				if(this.properties[i][0] == pName){
					oPropertyIndex = i;
					}
				}
			}
		else if(this.properties == null || this.properties =='undefined') this.properties = new Array();
		if(oPropertyIndex==null)	this.properties[this.properties.length] = new Array(pName,pValue);
		else this.properties[oPropertyIndex][1] = pValue;
	
	}
	function addListProperty(listName){
		var oPropertyIndex = null;
		var i = 0;
		//alert('setting property '+pName+', current property count is '+properties.length);
		if(this.properties && this.properties.length){
			for(i=0;((i < this.properties.length) && (oPropertyIndex==null));i++){
				if(this.properties[i][0] == listName){
					oPropertyIndex = i;
					}
				}
			}
		else if(this.properties == null || this.properties =='undefined') this.properties = new Array();
		if(oPropertyIndex==null){
			this.properties[this.properties.length] = new Array(listName,new ObjectList());
			oPropertyIndex = this.properties.length-1;
			}
		else this.properties[oPropertyIndex][1] = new ObjectList();
		return(this.properties[oPropertyIndex][1]);
	}
	function findProperty(pName,pValue){
		/*var propertyFound = false;
		var i = 0;
		
		if(this.properties[0]){
			do	{
				if(this.properties[i][0] == pName)
					if(this[properties[i][1] == pValue){
						propertyFound = true;
						return(t
				}while(this.properties[++i] && propertyFound == false);
			}
		*/	
		}
	function getProperty(pName){
		//alert('getting property ' + pName);
		var oProperty = null;
			var i = 0;
			if(this.properties[0])
			do{
				//alert('property '+i+':'+this.properties[i][0]);
				if(this.properties[i].length)
				if(this.properties[i][0] == pName){
				//	alert('found property' +pName);
					return(this.properties[i][1]);
				}
			}while(this.properties[++i]);
		return(-1);
	}
	function ajaxSetProperties(){
		if(arguments[0]){
			for(arg in arguments[0]){
			/*	if (JSON.parse(arguments[0][arg]))
				this.setProperty(arg,JSON.parse(arguments[0][arg]));
				else*/ this.setProperty(arg,arguments[0][arg]);
				}
			}
	}
	function printProperties(){
	var aText = '';
	var i = 0;
	if(this.properties[0])
			do{
				if(this.properties[i].length)
				 aText = aText + this.properties[i][0]+' = '+this.properties[i][1]+'\n';
				
			}while(this.properties[++i]);
		alert(aText);
		return(1);
	}
	
	function displayProperty(pName,dField){
		if(getElement(dField)){
			if((arguments[2]) && (this.getProperty(pName).length>arguments[2]))getElement(dField).innerHTML = this.getProperty(pName).substring(0,arguments[2])+'..';
			else getElement(dField).innerHTML = this.getProperty(pName);
		}
		}	
	
	function displayItem(){
		var sibling = false;
		if(this.getProperty('objectID')==-1 && this.getProperty('target')!=-1) this.setProperty('objectID',this.getProperty('target'));
		if(this.getProperty('objectID')!=-1 && getElement(this.getProperty('objectID'))){
			//alert('displaying element'+this.getProperty('objectID'));
			
			if(sibling = firstSibling(getElement(this.getProperty('objectID')))){
				sibling = firstSibling(getElement(this.getProperty('objectID')));
				do{
				
					if(sibling != null){
						if(sibling.id != this.getProperty('objectID')){
							sibling.style.display='none';
							sibling.style.visibility='hidden';
							}
						}
					}while(sibling = nextSibling(sibling));
					
				}
			displayElement(this.getProperty('objectID'));
			}
		else statusmessages.Add('could not display item, no objectID found');
		}
}// END of class Item


function JobQueue(){
	//this.queueID = currentjobQueueID++;
	this.jobs = new ObjectList();
	this.execute = runJob;
	this.stop = stopQueue;
	this.wait = pauseJob;
	this.pause = pauseJob;
	this.start = startQueue; 
	this.timeoutID = null;
	this.addJob = addJob;
	this.iCounter = 0;
	function pauseJob(){
		if(!this.jobs.IsEmpty()){	
			var currentJob = this.jobs.First();
			if(this.timeoutID != null){
				if(currentJob.waitStatus == true )setTimeout("jobqueue.pause()",currentJob.getProperty('interval'));
				else setTimeout("jobqueue.execute()",currentJob.getProperty('interval'));
			}
		}
	}
	function addJob(functionName,waitForEnd,maxIteration,interval){
		var job = this.jobs.Insert();
		job.waitStatus = false;
		job.setProperty('jobName',functionName ? functionName : false);
		job.setProperty('interval',interval ? interval : 50);
		job.setProperty('maxIteration',maxIteration ? maxIteration : 50);
		job.setProperty('waitForEnd',waitForEnd ? waitForEnd : false); 
		//job.printProperties();
		if(this.timeoutID == null) this.start();
	}

	function runJob(){
	//	alert('counter:'+iCounter);
		if(this.timeoutID!=null){
			if(!this.jobs.IsEmpty()){	
				var currentJob = this.jobs.First();
			
				var result = eval(currentJob.getProperty('jobName'));
				
				if(currentJob.getProperty('waitForEnd')==true){
					currentJob.waitStatus = true;
					++this.iCounter;
					}
				if((currentJob.getProperty('waitForEnd')==false) || (this.iCounter >= currentJob.getProperty('maxIteration')) || (result==true)) {
					//Delete job from queue
					this.jobs.Delete();
					this.iCounter = 0;
					}
				if(!this.jobs.IsEmpty()){
					currentJob = this.jobs.First(); 
					if(this.timeoutID != null) setTimeout("jobqueue.execute()",currentJob.getProperty('interval'));
					}
				else this.stop();
				}
			else{
				this.stop();
				}
			}
		}
	function stopQueue(){
		this.jobStatus = false;
		this.timeoutID = null;
		}
		
	function startQueue(){
		if(!this.jobs.IsEmpty()){	
			var currentJob = this.jobs.First();
			
			if(this.timeoutID==null){
				this.jobStatus = true;
				//alert('starting job queue');
				this.timeoutID = setTimeout("jobqueue.execute()",currentJob.getProperty('interval'));
			}
		}
	}
}//end of class jobqueue
var jobqueue = new JobQueue();


	function firstChild(startParent){	
		var tempObj = startParent.firstChild;
		while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){		
			tempObj = tempObj.nextSibling;	
			}	
		return (tempObj.nodeType==1)?tempObj:false;
		}
	function nextSibling(startBrother){  
		var tempObj = startBrother.nextSibling;  
		if(tempObj){
			while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
				if(tempObj)
				tempObj = tempObj.nextSibling;	  
				}  
			return (tempObj.nodeType==1)?tempObj:false;
			}
		else return false;
		}
	function firstSibling(node){  
		var tempObj=node.parentNode.firstChild;  
		while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){    
			tempObj=tempObj.nextSibling;  
			}  
		return (tempObj.nodeType==1)?tempObj:false;
		}
	function lastSibling(node){  
		var tempObj=node.parentNode.lastChild; 
		while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
			tempObj=tempObj.previousSibling;  
			}
		return (tempObj.nodeType==1)?tempObj:false;
		}
		
/* DOM functions */
function addTR(targetTable,pos){
	var tPos = 0;
	if(pos!='BEGINNING') tPos = -1;
	if(!getElement(targetTable)) return(false);
	var target = getElement(targetTable);
	return(target.insertRow(tPos));
	}
function addTD(targetTable,content,pos,tr){
	var tPos = 0;
	if(pos!='BEGINNING') tPos = -1;
	if(!getElement(targetTable)) return(false);
	var target = getElement(targetTable);
	var row = tr ? tr : target.insertRow(tPos);
	var cell = row.insertCell(-1);
	$(cell).attr('className','alignTop');
	cell.innerHTML = content;
	return(cell);
	
	
}
function deleteTD(targetTable,pos){
 var rows = getElement(targetTable).rows;
 var tPos = 0;
 if(pos!='BEGINNING' && pos !='START'){
	 tPos = rows.length-1;
 	}
 	getElement(targetTable).deleteRow(tPos);
}
function trimTable(targetTable,trimlength,pos){
	if(!getElement(targetTable)) return(false);
	var	rows = getElement(targetTable).rows;
 	while(rows.length > trimlength)
		deleteTD(targetTable,pos);
	}
	
	
function checkEmail(val) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(val)){
		return (true);
		}
	else return (false);
	}
function hideElementContents(element){
	var target = getElement(element);
	var coll = target.childNodes;
	var i;
	if(coll.length){
		var currentNode = target.firstChild;
		do{	var attribs = currentNode.attributes;
			
			
			if(currentNode.style){
			currentNode.style.display='none';
			currentNode.style.visibility='hidden';
			}
			}while(currentNode = currentNode.nextSibling);
		
		}
}
function hideElement(element){
	var target = getElement(element);
	if(target){
		target.style.display='none';
		target.style.visibility='hidden';
		}
}
function displayElement(element){
	
	var target = getElement(element);
	if(target){
		target.style.display='block';
		target.style.visibility='visible';
		}
	}
function SQL_TO_JS_Date(sqlDate) {
return sqlDate.replace(/^(....).(..).(.{11}).*$/, "$1/$2/$3") }
