//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// COMMON FUNCTIONS
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
// Detect Browser
//
//////////////////////////////////////////////////////////////////////////////

var agt = navigator.userAgent.toLowerCase ();
var is_major = parseInt (navigator.appVersion);
var is_minor = parseFloat (navigator.appVersion);

var is_safari = (agt.indexOf ("safari") != -1);

var is_nav  = ((agt.indexOf ('mozilla') != -1) && (agt.indexOf ('spoofer') == -1) && (agt.indexOf ('compatible') == -1) && (agt.indexOf ('opera') == -1) && (agt.indexOf ('webtv') == -1) && (agt.indexOf ('hotjava') == -1) && (agt.indexOf ('safari') == -1));

var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf (";nav") != -1) || (agt.indexOf ("; nav") != -1)));
var is_nav6 = (is_nav && (is_major == 5) && (agt.indexOf("netscape") != -1) && (agt.indexOf ("netscape/7") == -1));
var is_nav6up = ((is_nav && (is_major >= 5)) || (is_nav && (agt.indexOf ('netscape/7') != -1)));
var is_gecko = (agt.indexOf ('gecko') != -1);

var is_ie     = ((agt.indexOf ("msie") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.0") != -1));
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.5") != -1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 6.") != -1));
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5 && !is_safari);

var is_opera = (agt.indexOf ("opera") != -1);
var is_opera2 = (agt.indexOf ("opera 2") != -1 || agt.indexOf ("opera/2") != -1);
var is_opera3 = (agt.indexOf ("opera 3") != -1 || agt.indexOf("opera/3") != -1);
var is_opera4 = (agt.indexOf ("opera 4") != -1 || agt.indexOf("opera/4") != -1);
var is_opera5 = (agt.indexOf ("opera 5") != -1 || agt.indexOf("opera/5") != -1);
var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4 && !is_opera5);
var is_opera7 = ((agt.indexOf ("opera/7.") != -1) || (agt.indexOf ("opera 7.") != -1));
var is_opera8 = ((agt.indexOf ("opera/8.") != -1) || (agt.indexOf ("opera 8.") != -1));

var dom = ((document.getElementById) ? true : false);

var pathRoot = '';

var urlRoot = '';
urlRoot = document.location.href.split ('/');
urlRoot.length--;
urlRoot = urlRoot.join ('/') + '/';

//////////////////////////////////////////////////////////////////////////////
//
// Find Object
//
//////////////////////////////////////////////////////////////////////////////

function getObj (_objID, _document)
{
	var _obj;

	if (!_document) 
		_document = document; 

	if (dom)
		_obj = _document.getElementById (_objID);

	return _obj;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function createObj (_document, _parent, _child, _id, _className)
{
	var obj = null;

	obj = _document.createElement (_child);
	_parent.appendChild (obj);
	obj.id = _id;
	obj.name = _id;
	obj.className = ((_className != null) ? _className : '');

	return obj;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function setID (_obj)
{
	if (!_obj.id && _obj)
	{
		_obj.id = _obj.uniqueID;
	}

	return _obj.id;
}

//////////////////////////////////////////////////////////////////////////////
//
// Get layer Y position
//
//////////////////////////////////////////////////////////////////////////////

function getY (_element)
{
	var y = 0;
	
	while (_element != null && _element.style.position != 'absolute')
	{
		if (_element.nodeName != 'BODY' && _element.nodeName != 'IMG')
		{
			y += _element.offsetTop ;//- _element.scrollTop;
		
		}
		else
		{
			y += _element.offsetTop;
		}

		_element = _element.offsetParent;
	}

	return y;
}

//////////////////////////////////////////////////////////////////////////////
//
// Get layer X position
//
//////////////////////////////////////////////////////////////////////////////

function getX (_element)
{
	var x = 0;

	while (_element != null)
	{
		if (_element.nodeName != 'BODY' && _element.nodeName != 'IMG')
		{
			x += _element.offsetLeft - _element.scrollLeft;
		}
		else
		{
			x += _element.offsetLeft;
		}

		_element = _element.offsetParent;
	}

	return x;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function isMouseOver (_obj, _event)
{
	var toElement = null;

	if (_event == null)
	{
		_event = window.event;
	}

	toElement = (is_ie ? _event.toElement : _event.target);

	return (isChild (_obj, toElement) == null ? false : true);
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function windowClose ()
{
	window.close;

	return true;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function showPopup (url, name, title, W, H, X, Y)
{
//	W = (W ? W : 300);
//	W = (H ? H : 400);

	X = (window.screen.availWidth / 2) - (W / 2);

	if (window.screen.availHeight - 24 < H)
	{
		Y = 0;
		W += 17;
		H = window.screen.availHeight - 24;
	}
	else
	{
		Y = (window.screen.availHeight / 2) - (H / 2 + 12);
	}

	params = "scrollbars=yes,status=yes,resizable=1,width=" + W + ",height=" + H + ",left=" + X + ",top=" + Y;

	wID = window.open (url, name, params);

	return wID;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function isChild (_child, _parent)
{
	do
	{
		if (_child != _parent)
		{
			_child = _child.parentNode;
		}
		else
		{
			setID (_child);

			return _child;
		}
	} while (_child != null);

	return null;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getParentElementByTag (_child, _parentTagName)
{
	do
	{
		if (_child.tagName.toUpperCase () != _parentTagName.toUpperCase ())
		{
			_child = _child.parentNode;
		}
		else
		{
			setID (_child);

			return _child;
		}
	} while (_child != null);

	return false;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getChildElementByTag (_parent, _childTagName)
{
	var currentNode = getFirstChild (_parent);
	
	while (currentNode != _parent)
	{
	
		if ( currentNode.tagName.toUpperCase () == _childTagName.toUpperCase () )
		{
			return currentNode;
		}		
		
		//go to first child node
		if (getFirstChild(currentNode) != null) 
			currentNode = getFirstChild(currentNode);
		//else go to next node
		else if ((getNextSibling(currentNode) != null) && (getNextSibling(currentNode).tagName != "IFRAME"))
			currentNode = getNextSibling(currentNode); 
		//else go up by tree
	 	else 
		{
			while ((currentNode != _parent) && (getNextSibling(currentNode) == null) ) 
			{
				currentNode = currentNode.parentNode ;			
			}
				
			if (currentNode != _parent) 
			{
				currentNode = getNextSibling (currentNode);
			}
		}
	}
	
	return false;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getChildElements (_parent)
{
	var controls = new Array (),
		controlsNeedle = new Array (),
		parentObj = getObj (_parent);
		
	for (var index = 0; index < document.forms.length; index++)
	{
		for (var index2 = 0; index2 < document.forms[index].elements.length; index2++)
		{
			controls[controls.length] = document.forms[index].elements[index2];
		}
	}

	for (index = 0; index < controls.length; index++)
	{
		if (isChild(controls[index], parentObj))
		{
			controlsNeedle[controlsNeedle.length] = controls[index];
		}
	}

	return controlsNeedle;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getChildElementsFromForm (_parent)
{
	var controls = new Array (),
		controlsNeedle = new Array (),
		parentObj = (typeof (_parent) == 'object' ? _parent : getObj (_parent));

	for (var index = 0; index < document.forms.length; index++)
	{
		for (index2 = 0; index2 < document.forms[index].elements.length; index2++)
		{
			controls[controls.length] = document.forms[index].elements[index2];
		}
	}

	for (index = 0; index < controls.length; index++)
	{
		if (getParentElementByTag (controls[index], parentObj.tagName) == parentObj)
		{
			controlsNeedle[controlsNeedle.length] = controls[index];
		}
	}

	return controlsNeedle;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getElementCollectionByName (_name, _tags)
{
	var count = 0,
		objCollection = new Array (),
		tagsCollection = new Array (),
		namesCollection;

	if (_tags != null)
	{
		tagsCollection = _tags.split (',');
	}
	else
	{
		return null;
	}

	for (var indexTags = 0; indexTags < tagsCollection.length; indexTags++)
	{
		namesCollection = document.all.tags (tagsCollection[indexTags]);

		for (var indexNames = 0; indexNames < namesCollection.length; indexNames++)
		{
			if (namesCollection[indexNames].name == _name)
			{
				objCollection[count++] = namesCollection[indexNames];
			}
		}
	}

	if (count > 0)
	{
		return objCollection;
	}

	return null;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function hideBlock (_obj)
{
	_obj.style.display = 'none';
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function showBlock (_obj)
{
	_obj.style.display = 'block';
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function setBlockVisibilityControl (_objControl, _title, _mode)
{
	var imgCollapse = '',
		imgExpand = '';

	switch (_title.toLowerCase ())
	{
		case "group":
			imgCollapse = 'img/icons/general/group-collapse.gif';
			imgExpand = 'img/icons/general/group-expand.gif';
			break;

		case "item":
		default:
			imgCollapse = 'img/icons/general/item-collapse.gif';
			imgExpand = 'img/icons/general/item-expand.gif';
			break;
	}

	if (_mode == null)
	{
		_mode = (_objControl.src.indexOf ('collapse') == -1 ? true : false);
	}

	if (_mode)
	{
		_objControl.src = pathRoot + imgCollapse;
		_objControl.alt = 'Collapse ' + _title;
		_objControl.title = 'Collapse ' + _title;
	}
	else
	{
		_objControl.src = pathRoot + imgExpand;
		_objControl.alt = 'Expand ' + _title;
		_objControl.title = 'Expand ' + _title;
	}
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function setCheckboxes (_objName, _isChecked, _isTree)
{
	var arrCheckboxes = new Array ();

	arrCheckboxes = document.getElementsByName (_objName);

	for (var indexCheckbox = 0; indexCheckbox < arrCheckboxes.length; indexCheckbox++)
	{
		if (!(arrCheckboxes[indexCheckbox].disabled))
		{
			arrCheckboxes[indexCheckbox].checked = (_isChecked ? false : true);
			arrCheckboxes[indexCheckbox].click();
		}
		
		if (_isTree)
		{
			setCheckboxes (arrCheckboxes[indexCheckbox].id, _isChecked);
		}
	}
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getItemsSelected (_objName)
{
	var arrCheckboxes = new Array (),
		arrCheckboxesSelected = new Array ();

	arrCheckboxes = document.getElementsByName (_objName);

	for (var indexCheckbox = 0; indexCheckbox < arrCheckboxes.length; indexCheckbox++)
	{
		if (arrCheckboxes[indexCheckbox].checked)
		{
			arrCheckboxesSelected[arrCheckboxesSelected.length] = arrCheckboxes[indexCheckbox].id;
		}
	}

	return arrCheckboxesSelected.join (',');
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getItemsSelectedValues (_objName, _isFilterEmpty)
{
	var arrCheckboxes = new Array (),
		arrCheckboxesValues = new Array ();

	arrCheckboxes = document.getElementsByName (_objName);

	for (var indexCheckbox = 0; indexCheckbox < arrCheckboxes.length; indexCheckbox++)
	{
		if (arrCheckboxes[indexCheckbox].checked)
		{
			if (_isFilterEmpty && arrCheckboxes[indexCheckbox].value == '')
			{

			}
			else
			{
				arrCheckboxesValues[arrCheckboxesValues.length] = arrCheckboxes[indexCheckbox].value;
			}
		}
	}

	return arrCheckboxesValues.join (',');
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getItemsSelectedSelectboxValues (_prefixSelectbox, _prefixCheckbox)
{
	var arrCheckbox = new Array (),
		arrSelectbox = new Array (),
		indexSelectbox = 1,
		objCheckbox = null,
		objSelectbox = null;

	while ((objSelectbox = getObj (_prefixSelectbox + indexSelectbox)) && (objCheckbox = getObj (_prefixCheckbox + indexSelectbox)))
	{
		if (objCheckbox.checked)
		{
			arrSelectbox[arrSelectbox.length] = objSelectbox.value;
		}

		indexSelectbox++;
	}

	return arrSelectbox.join (',');
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function selectOption (_objSelect, _optionValue)
{
	for (indexOptions = 0; indexOptions < _objSelect.length; indexOptions++)
	{
		if (_objSelect.options[indexOptions].value == _optionValue)
		{
			_objSelect.options[indexOptions].selected = true;

			return true;
		}
	}

	return false;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function setClassName (_element, _arrClassOld, _classNew)
{
	var objClass = null,
	    objElement = null;

	objElement = ((typeof (_element) != 'object') ? getObj (_element) : _element);
	objClass = objElement.className.split (' ');

	for (var indexClass = 0; indexClass < objClass.length; indexClass++) {
		if (isInArray (_arrClassOld, objClass[indexClass])) {
			objClass[indexClass] = _classNew;
			objElement.className = objClass.join (' ');
			return true;
		}
	}

	objElement.className = objClass.join (' ') + ' ' + _classNew;

	return true;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function switchClassName (_element, _className)
{
	var classReplace = '/ ' + _className + '/ig';

	if (_element.className.indexOf (_className) == -1)
	{
		_element.className += ' ' + _className;
	}
	else
	{
		_element.className = _element.className.replace (eval (classReplace), '');
	}
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function addClassName (_element, _className)
{
	var classReplace = '/ ' + _className + '/ig';

	if (_element.className.indexOf (_className) == -1)
	{
		_element.className += ' ' + _className;
	}
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function removeClassName (_element, _className)
{
	var classReplace = '/ ' + _className + '/ig';

	_element.className = _element.className.replace (eval (classReplace), '');
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function removeClassNameFull (_element, _className)
{
	var classReplace = '/' + _className + '/ig';

	_element.className = _element.className.replace (eval (classReplace), '');
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function isHaveClassName (_element, _className)
{
	var classReplace = '/' + _className + '/ig';

	return _element.className.search (eval (classReplace));
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function isHaveClassName (_element, _className)
{
	var classReplace = '/' + _className + '/ig';

	return _element.className.match (eval (classReplace)) != null;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function isInArray (_array, _needle)
{
	for (indexArray = 0; indexArray < _array.length; indexArray++)
	{
		if (_array[indexArray] == _needle)
		{
			return true;
		}
	}

	return false;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function documentWrite (_window, _content, _isClear)
{
	if (_isClear)
	{
		_window.document.clear ();
	}

	_window.document.open ();
	_window.document.write (_content);
	_window.document.close ();
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getLayer (_objLayerID, _objLayerCode, _objLayerClass, _isRewrite, _tag, _document)
{
	var objLayer = null;

	if (objLayer = getObj (_objLayerID))
	{
		if (_isRewrite)
		{
			objLayer.innerHTML = _objLayerCode;
		}
	}
	else
	{
		objLayer = createObj ((_document != null ? _document : document), (_document != null ? _document.body : document.body), (_tag != null ? _tag : 'div'), _objLayerID, _objLayerClass);

		if (_objLayerCode != '' & _objLayerCode != null)
		{
			objLayer.innerHTML = _objLayerCode;
		}
	}

	return objLayer;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function getArrayItemIndex (_arrArray, _needle)
{
	for (indexArray = 0; indexArray < _arrArray.length; indexArray++)
	{
		if (_arrArray[indexArray] == _needle)
		{
			return indexArray;
		}
	}

	return -1;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function findFirstInput (_document)
{
	var arrForms = (_document != null ? _document.forms : document.forms),
		objItem = null;
	
	for (var indexForm = 0; indexForm < arrForms.length; indexForm++)
	{
		for (var indexItem = 0; indexItem < arrForms[indexForm].length; indexItem++)
		{
			if (arrForms[indexForm].elements[indexItem].tagName == "TEXTAREA" || (arrForms[indexForm].elements[indexItem].tagName == "INPUT" && arrForms[indexForm].elements[indexItem].type == "text" && !arrForms[indexForm].elements[indexItem].disabled && !arrForms[indexForm].elements[indexItem].readonly))
			{
				objItem = arrForms[indexForm].elements[indexItem];

				return objItem;
			}
		}
	}

	return false;
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
 
function gotoURL (_url)
{
	if (_url)
	{
		document.location.href = _url;
	}
}

//////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////	

function attachEvent (_element, _event, _function)
{
	if (!is_ie)
	{
		_element.addEventListener (_event, _function, false);
	}
	else
	{
		_element.attachEvent ("on" + _event, _function);
	}
}

//////////////////////////////////////////////////////////////////////////////
//
// Get first child node of element
//
// _obj - element,  
//
// return child node if exist, else return null
//
//////////////////////////////////////////////////////////////////////////////

function getFirstChild (_obj) 
{
	var kids = _obj.childNodes;
	
	for (var i = 0; i < kids.length; i++) 
	{
		if (kids[i].nodeType == 1)
			return kids[i];
	}
	
	return null;
}

//////////////////////////////////////////////////////////////////////////////
//
// Get next sibling node of element
//
// _obj - element,  
//
// return next sibling node if exist, else return null
//
//////////////////////////////////////////////////////////////////////////////


function getNextSibling (_obj) 
{
	var obj = _obj;

	try 
	{
		while (obj.nextSibling != null) 
		{
			obj = obj.nextSibling;
			if (obj.nodeType == 1)
				return obj;
		}
	} 
	catch (e) 
	{
		return null;
	}
	
	return null;
}

//////////////////////////////////////////////////////////////////////////////
//
// Get previous sibling node of element
//
// _obj - element,  
//
// return previous sibling node if exist, else return null
//
//////////////////////////////////////////////////////////////////////////////


function getPreviousSibling (_obj) 
{
	var obj = _obj;
	
	while (obj.previousSibling != null) 
	{
		obj = obj.previousSibling;
		if (obj.nodeType == 1)
			return obj;
	}
	
	return null;
}
//////////////////////////////////////////////////////////////////////////////
//
// Trim string
//
// _s - string,  
//
// return trimmed string
//
//////////////////////////////////////////////////////////////////////////////	
	
function trim (_s) 
{
    return _s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}	

//////////////////////////////////////////////////////////////////////////////
//
//
//
//
//////////////////////////////////////////////////////////////////////////////	
	
function removeTags (_s) 
{
    return _s.replace(/(<([^>]+)>)/ig,"");
}	


//////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////	

function expandTreeNode (_img, _obj) 
{


	var obj = document.getElementById(_obj);
	var img = document.getElementById(_img);
	if (obj.style.display == 'none') 
	{
		obj.style.display = 'block';
		img.src="img/icons/general/item-collapse.gif";
	}
	else 
	{
		obj.style.display = 'none';
		img.src="img/icons/general/item-expand.gif";
	}
}

//////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////	

function underline (_obj) 
{
	_obj.style.textDecoration='underline';
}

//////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////	

function ununderline (_obj) 
{
	_obj.style.textDecoration='none';
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function showPaging (_caller, _obj, _input)
{
	hideBlock (getObj (_caller));
	getObj (_obj).style.display = '';
	getObj (_input).focus();
}
