/* HTML Element: 
    - getElement(el, toggleBy)
    - getChildElement(parentEl, childTag, childID)
    - getChildElementByIndex(parentEl, childTag, index)
    - getOwnerParent(srcItem, parentTagName)
    - getFisrtRow(tableItem)
    - getFirstChild(parentItem, childTagName)
    - getNextChild(oItem, childTagName)
*/
function getElement(el, toggleBy){
	if (!(browseris.dom || browseris.ie || browseris.nav4)) return null;
	if(browseris.nav4 && (toggleBy == "tag")) return null;
	
	switch(toggleBy) {
		case "tag":
        	return ((browseris.dom) ? document.getElementsByTagName(el) : document.all.tags(el));
		case "id":
			return ((browseris.dom) ? document.getElementById(el) : (browseris.ie) ? document.all(el) : document.layers[el]);
		case "name":
		    return document.getElementsByTagName(el);
	}
	
	return null;
}

function getElement2(el, toggleBy){
	return getElement(el, toggleBy);
}

function getChildElement(parentEl, childTag, childID){
	if (!(browseris.dom || browseris.ie || browseris.nav4)) return null;
	
	if (browseris.ie){
		return parentEl.all.item(childID)
	}else{
		try{
			var children = parentEl.getElementsByTagName(childTag);
			var length = children.length;
			if (length > 0){
				for (var i=0; i<length; i++){
					child = children.item(i);
					if (child.id == childID)
						return child;
				}
			}
		}catch(e){}
	}
	
	return null;
}

function getChildElementByIndex(parentEl, childTag, index){
	if (!(browseris.dom || browseris.ie || browseris.nav4)) return null;
	
	try{
		var child = parentEl.firstChild;
		var i = 0;
		
		if (child){
			if ((index == 0) && (child.tagName == childTag)) return child;
			while (child && (i < index)){
				child = child.nextSibling;
				if (child && (child.tagName == childTag)) i++;
			}
			return child;
		}
	}catch(e){}
	
	return null;
}

function getOwnerParent(srcItem, parentTagName){
	var parentItem = srcItem.parentNode;
	while (parentItem && !FIStringEquals(parentItem.tagName, parentTagName))
		parentItem = parentItem.parentNode;
	
	return parentItem;
}

function getFisrtRow(tableItem){
	var trItem = tableItem.firstChild;
	var tagName = trItem.tagName;
	
	while (trItem && !FIStringEquals(tagName, "tr")){
		if (!FIStringEquals(tagName, "tbody") && !FIStringEquals(tagName, "td"))
			trItem = trItem.nextSibling;
		else
			trItem = trItem.firstChild;
		if (trItem) tagName = trItem.tagName;
	}
	
	return trItem;
}

function getFirstChild(parentItem, childTagName){
	var child = parentItem.firstChild;
	while (child && !FIStringEquals(child.tagName, childTagName)){
		child = child.nextSibling;
	}
	
	return child;
}

function getNextChild(oItem, childTagName){
	var oNextItem = oItem.nextSibling;
	
	while (oNextItem && !FIStringEquals(oNextItem.tagName, childTagName)){
		oNextItem = oNextItem.nextSibling;
	}
	
	return oNextItem;
}

/* HTML Element Attribute:
    - getAttribute(theElement, attrName)
    - setAttribute(theElement, attrName, attrValue)
    - mergeAttributes(srcItem, descItem)
    - getInnerText(elt)
*/
function getAttribute(theElement, attrName){
	try{
		if ((attrName == "disabled") && browseris.ie4up && browseris.win32)
			return "" + theElement.isDisabled;
		else
			return theElement.getAttribute(attrName);
	}catch(e){}
	
	return null;
}

function setAttribute(theElement, attrName, attrValue){
	try{
		if ((attrName == "disabled") && browseris.ie4up && browseris.win32)
			theElement.disabled = ((attrValue == "true") ? true : false);
		else
			theElement.setAttribute(attrName, attrValue);
	}catch(e){
	    return false;
	}

    return true;	
}

function mergeAttributes(srcItem, descItem){
	if (browseris.ie4up){
		descItem.mergeAttributes(srcItem);
	}else{
		try{
			var atts = srcItem.attributes;
			for (var i=0; i<atts.length; i++){
				setAttribute(descItem, atts.item(i).name, atts.item(i).value);
			}
		}catch(e){
		    return false;
		}
	}
	
	return true;
}

function getInnerText(elt){
	if (elt){
		if(browseris.ie) {
			return elt.innerText;
		}else{
    		var value = elt.innerHTML;
			if (value){
				var reg = /\<([^<,^>]*)\>/g;
				return value.replace(reg, "");
			}
		}
	}
	
	return "";
}
/* HTML Element Display Style:
    - toggleElementList(show, elList, toggleBy)
    - setVisibility(theElement, show)
    - isVisibility(theElement)
*/
function toggleElementList(show, elList, toggleBy){
	if (!(browseris.dom||browseris.ie||browseris.nav4)) return true;
	if(browseris.nav4&&(toggleBy == "tag")) return true;
	
	for(var i=0; i<elList.length; i++) {
    	var ElementsToToggle = [];
        switch(toggleBy) {
        	case "tag":
            	ElementsToToggle = (browseris.dom) ? document.getElementsByTagName(elList[i]) : document.all.tags(elList[i]);
               	break;
			case "id":
            	ElementsToToggle[0] = (browseris.dom) ? document.getElementById(elList[i]) : (browseris.ie) ? document.all(elList[i]) : document.layers[elList[i]];
               	break;
        }
        for(var j=0; j<ElementsToToggle.length; j++) {
        	var theElement = ElementsToToggle[j];
            if(!theElement) continue;
			
			setVisibility(theElement, show);
        }
	}
	
    return true;
}

function setVisibility(theElement, show){
	if(!browseris.ie)
    	theElement.visibility = ((show) ? "inherit" : "hide");
	
	theElement.style.display = ((show) ? "block" : "none");
}

function isVisibility(theElement){
	if(browseris.ie) {
		return (theElement.style.display != "none");
	}else{
    	return (theElement.visibility != "hide");
	}
}

/* Cookie Function: 
    - setCookie(c_name, value, expiredays)
    - getCookie(c_name)
    - delCookie(c_name)
*/
function setCookie(c_name, value, expiredays){
    var exdate = new Date();
    
    if (expiredays != null){
        exdate.setDate(exdate.getDate() + expiredays);
    }
    
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name){
    if (document.cookie.length > 0){
        var c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1){ 
            c_start = c_start + c_name.length + 1;
            var c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1)
                c_end = document.cookie.length;
    
            return unescape(document.cookie.substring(c_start, c_end));
        } 
    }
    
    return null;
}

function delCookie(c_name){
    if (getCookie(c_name)){
        document.cookie = c_name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* String Function:
    - FIStringEquals(wzX, wzY)
*/
function FIStringEquals(wzX, wzY){
	return wzX != null && wzY != null && wzX.toLowerCase() == wzY.toLowerCase();
}
