var detached = new Object;
var lastAutocompleteSearch = '';
var searchEventId = 0;
var autoListSelection = 0;
function showPopUp(id,offset,absX, absY, hideSelect) {
	if (!offset) 
		offset = 0;
	linkBox = document.getElementById(id);
	if(linkBox) {
		linkBox.style.display = "block";
		if (absX && absX > -1)
			linkBox.style.left = (absX)+"px";
		else
			linkBox.style.left = (mouseX+offset)+"px";
	
		if (absY && absY > -1) 
			linkBox.style.top = absY+"px";
		else
			linkBox.style.top = mouseY+"px";
		
		if (hideSelect)
			showHideSelectControls('hide',mouseX+offset,mouseY,linkBox.offsetWidth,linkBox.offsetHeight);
			
	} else 
		alert("PopUp "+id+" not found!");
}

function showPopUpInline(id,offset) {
	if (!offset) 
		offset = 0;
	linkBox = document.getElementById(id);
	linkBox.style.display = "block";
	linkBox.style.position = "absolute";
	showHideSelectControls('hide',mouseX,mouseY,linkBox.offsetWidth,linkBox.offsetHeight);
}

function hidePopUp(id) {
	box = document.getElementById(id);
	box.style.display = 'none';
	showHideSelectControls('show',mouseX,mouseY,box.offsetWidth,box.offsetHeight);
}

function expand(obj) {
	document.getElementById("collapseIcon").style.display='inline';
	document.getElementById("expandIcon").style.display='none';		
	obj.style.display="inline";
}

function collapse(obj) {
	expandElement = document.getElementById("expandIcon");
	expandElement.style.display="inline";
	obj.style.display='none';
	collapsedElement = document.createElement('div');
	collapsedElement.appendChild(expandElement);
	obj.parentNode.insertBefore(collapsedElement,obj);	
}


function detatch(obj, target){
	if (!detached[target.id]) {
		detached[target.id] = true;
		target.style.position = 'absolute';
		document.getElementById("attachIcon").style.display='inline';
		document.getElementById("detatchIcon").style.display='none';		
	} else {
		target.style.position = 'static';
		detached[target.id] = false;	
		document.getElementById("attachIcon").style.display='none';
		document.getElementById("detatchIcon").style.display='inline';		
	}
}

function changePage(pageNum) {
	document.getElementById('pageNumber').value = pageNum;
	document.getElementById('pagingSubmitButton').click();
	//document.getElementById('pagingForm').submit();
}

function st_changePage(pageNum) {
	document.getElementById('pageNumber').value = pageNum;
	document.getElementById('pagingSubmitButton').click();
	//document.getElementById('pagingForm').submit();
}

function absoluteY(node) {
	var total = 0;
	if (node.offsetParent) {
		total += (node.offsetTop && !isNaN(node.offsetTop)?node.offsetTop:0) + absoluteY(node.offsetParent);
	}
	else 
		total += (node.offsetTop && !isNaN(node.offsetTop)?node.offsetTop:0);
	return total;
}

function absoluteX(node) {
	var total = 0;
	if ( node.offsetParent) {
		total += (node.offsetLeft && !isNaN(node.offsetLeft)?node.offsetLeft:0)  + absoluteX(node.offsetParent);
	}
	else
		total += (node.offsetLeft && !isNaN(node.offsetLeft)?node.offsetLeft:0) ;
	return total;
}

//////////////////////////////
// Ajax Autocomplete Text Box
//////////////////////////////
function autocomplete(evt ,input, minlen, url,formName,processFunction) {

	if (__DEBUG) 
		addDebugMessage("Input Length at "+input.value.length+" chars.");
	
	if (evt.keyCode == 40 || evt.keyCode == 38) {	
		list = document.getElementById(input.name+'_list');
		list.normalize();
		
		if (evt.keyCode == 40) 
			if (autoListSelection >= list.childNodes.length)
				autoListSelection = 1;
			else 
				autoListSelection++;	
		
		if (evt.keyCode == 38) 
			if (autoListSelection == 1)
				autoListSelection = list.childNodes.length;
			else 
				autoListSelection--;	
		
		
		if (list.hasChildNodes()) {
			for(i = 0; i < list.childNodes.length; i++) {
				list.childNodes[i].className = input.name+'_deselected';	
			}
			if(__DEBUG)
				addDebugMessage("Highlighting Item #"+autoListSelection+'/'+list.childNodes.length);
			list.childNodes[autoListSelection].className = input.name+'_selected';


		}
	}
	
	// If a search is scheduled, cancel because additional characters have been typed.
	clearTimeout(searchEventId);  
	evt.stopPropagation();	// Stop Return from submitting form
	
	// If the search has changed and is of the minimum length to autocomplete, search
	if (input.value != lastAutocompleteSearch && input.value.length >= minlen) {
		dropDown = document.getElementById(input.name + '_drop');
		dropDown.style.display = 'block';
		dropDown.innerHTML = "Searching...";
		searchEventId = setTimeout(doAutocompeteSearch,500,evt ,input, minlen, url,formName,processFunction);
	}
}

function doAutocompeteSearch(evt ,input, minlen, url,formName,processFunction) {
		lastAutocompleteSearch = input.value;
		addDebugMessage("Searching...");
		submitForm(url,formName,null,false,processFunction);
}

function initDropdown(name,parent) {
	return;
	dropDown = document.createElement('div');
	dropDown.id = name;
	dropDown.className = name;
	dropDown.style.position = 'absolute';
	dropDown.style.display = 'none';
	dropDown.style.left =  absoluteX(parent)+'px';
	//dropDown.style.top = '2px';
	dropDown.style.top = (absoluteY(parent)+parent.clientHeight+3)+'px';	
	//alert(dropDown.style.left+' x '+dropDown.style.top);
	parent.parentNode.appendChild(dropDown);
}

function destroyDropdown(name,parent) {
	parent.parentNode.removeChild(document.getElementById(name));
}

function getParentRow(e) {
	if (e.tagName && e.tagName != 'TR' && e.parentNode) { 
		return getParentRow(e.parentNode);
	} else
		return e;
	
}

function getEle(id) {
	return document.getElementById(id);
}

function deleteElementsLike(tagName, name) {
	tags = document.getElementsByTagName(tagName);
	if(tags) {
		count = tags.length;
		for(i = count; i > 0; i--) {
			if (tags[i] && tags[i].id.indexOf(name) == 0)
				tags[i].parentNode.removeChild(tags[i]);
		}
	}
}

function showPopupBox(errId,popupId,x,y) {
	box = document.getElementById(errId);
	if (box) {
		showPopUp(popupId,0,x,y);
	}	
}

if (typeof  showHideSelectControls != 'function') {
	function showHideSelectControls(action,x,y,width,height) {
		//alert(y);//+"--------"+height+"---------"+x+"----------"+y+"---------"+action);
		var right=x+width-1;
		var bottom=y+height-1;
		var selectControls=document.getElementsByTagName("SELECT");
		//alert("showHideselectcontrol"+x+","+y+"action="+action);
		for (var i=0; i<selectControls.length; i++) {
			if (action=='hide') {
				var sx=selectControls[i].offsetLeft;
				var sy=selectControls[i].offsetTop;
		
				// sx and sy are relative to parent. Compute absolute values 
				var parent=selectControls[i].offsetParent;
					if (parent)
					  do {
					sx += parent.offsetLeft;
					sy += parent.offsetTop;
				} while ( parent = parent.offsetParent );
		
				var sRight=selectControls[i].offsetWidth; //sx+selectControls[i].offsetWidth-1; 
				var sBottom=selectControls[i].offsetHeight; //sy+selectControls[i].offsetHeight-1; 
				t =    (x+width < sx || sx+sRight < x || y + height < sy || sy+sBottom < y);
				nt = !(x+width < sx || sx+sRight < x || y + height < sy || sy+sBottom < y);
				if(!(x+width < sx || sx+sRight < x || y + height < sy || sy+sBottom < y))  {                                                                                                                                    
					//alert("x="+x+",y="+y+" right="+right+" bottom="+bottom+" sx="+sx+" sy="+sy+" sRight="+sRight+" sBottom="+sBottom);                                                                                        
					selectControls[i].style.visibility='hidden';                                                                                                                                                                    
				} else 
					selectControls[i].style.visibility='visible';                                                                                                                                                                                                                                                                                                                     
			} else if(action=='show') selectControls[i].style.visibility='visible';                                                                                                                                               
		} 
	}
}

function toggleBox(lnk,elementId) {
        e = document.getElementById(elementId);
        if (e.style.display == 'none') {
                sign = document.createTextNode('-');                                                                                                                                                                                        
                e.style.display = 'block';                                                                                                                                                                                                  
                lnk.replaceChild(sign,lnk.childNodes[0]);                                                                                                                                                                                   
        }                                                                                                                                                                                                                                   
        else {                                                                                                                                                                                                                              
                sign = document.createTextNode('+');                                                                                                                                                                                        
                e.style.display = 'none';                                                                                                                                                                                                   
                lnk.replaceChild(sign,lnk.childNodes[0]);                                                                                                                                                                                   
        }                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                            
} 
function swapEle(ele1Id,ele2Id,mode) {
		ele1 = document.getElementById(ele1Id);
		ele2 = document.getElementById(ele2Id);

        if (ele1.style.display == 'none') {
                ele1.style.display = mode;
				ele2.style.display = 'none';
        }                                                                                                                                                                                                                                   
        else {                                                                                                                                                                                                                              
                ele2.style.display = mode;
				ele1.style.display = 'none';                                                                                                                                                                                   
        }		
}
/*
function showElementById(id,show) {
	e = document.getElementById(id);
	if(e) {
		if(show)
			e.style.display = '';
		else
			e.style.display = 'none';
	}
}
*/

function setLangCookie() {
	selBox = document.getElementById("LANG_NAME");
	value = selBox[selBox.selectedIndex].value;
	var exdate=new Date();
 	exdate.setDate(exdate.getDate() + 1);
 	var cvalue=escape(value) + "; expires="+exdate.toUTCString();
 	document.cookie = "language=" + cvalue;
	return;
}
