﻿function handleEnterKey(e,funct){
    var key=e.keyCode || e.which;
    if (key==13){
        funct();            
    }
}
function getDocumentObject( id ){
    return document.getElementById( id );
}

function getCBSelectedValue( comboboxId ){
     var combobox = getDocumentObject(comboboxId);
     return combobox[combobox.selectedIndex].value;
}

function getCBSelectedText( comboboxId ){
     var combobox = getDocumentObject(comboboxId);
     return combobox[combobox.selectedIndex].text;
}

function getCheckboxStatus( checkboxId ){
    var checkbox = getDocumentObject( checkboxId );
    return checkbox.checked;
}

function getTextFieldText( txtId ){
    var txt = getDocumentObject( txtId );
    return txt.value;
}

function getLayerStatus( layerId ){
    var layer = getDocumentObject( layerId );
    return (layer.style.display != 'none');
}

function hide(id) {
    var obj = getDocumentObject(id);
    if(obj != null)
	    obj.style.display = "none";
}
function show(id) {	
    var obj = getDocumentObject(id);
    if(obj != null)
	    obj.style.display = "";
}
function addTagWithIdParent( parentId, tagName ){
    return addTagToParent( getDocumentObject(parentId), tagName );
}
function addTagToParent( parent, tagName ){
    var newEle = newTag( tagName );
    parent.appendChild( newEle );
    return newEle;
}
function addTextNodeToParent( parent, text ){
    var textNode = newTextNode(text);
    parent.appendChild( textNode );
    return textNode;
}

function setSelectedValue( comboId, selectValue ){
   var combo = getDocumentObject( comboId );
   
   if( combo == null ){
       return;
   }
   
   for( var i=0; i< combo.length; i++ ){
        var option = combo.options[i];
        if( option.value == selectValue ){
            combo.selectedIndex = i;
            break;
        }                
   }   
}

function getFirstChild( node ){
    var firstChild = node.childNodes[0];
    if( firstChild.nodeName == "#text" ){
        firstChild = node.childNodes[1];
    }
    return firstChild;
}

function getLastChild( node ){
    var lastChild = node.childNodes[node.childNodes.length-1];
    if( lastChild.nodeName == "#text" ){
        lastChild = node.childNodes[node.childNodes.length-2];
    }
    return lastChild;
}

function insertTagBefore( parent, nextNode, tagName ){
    var newNode = newTag( tagName );
    parent.insertBefore( newNode, nextNode );
    return newNode;
}

function insertTagAfter( parent, afterNode, tagName ){
    var newNode = newTag( tagName );
    if( (afterNode.nextSibling != undefined) && (afterNode.nextSibling != null)){
        parent.insertBefore( newNode, afterNode.nextSibling );
    }
    else{
        parent.appendChild(newNode);
    }
    return newNode;
}
function removeTag( tag ){
    tag.parentNode.removeChild( tag );
}
function removeAllChild(node){
    if ( node.hasChildNodes() )
    {
        while ( node.childNodes.length >= 1 )
        {
            node.removeChild( node.firstChild );       
        } 
    }
}
function newTag(tagName){
    return document.createElement(tagName);
}
function newTextNode(text){
    return document.createTextNode(text);
}

function getEventTagetTag(e){
    var targ;
	if (!e) {
	    var e = window.event;
	}
	if (e.target){
	    targ = e.target;
	}
	else if (e.srcElement){
	    targ = e.srcElement;
	}
	
	return targ;
}

function isEffectByEvent(e, tagID){
    var oEffectedRoot = getEventTagetTag( e );
    if( oEffectedRoot.id == tagID ){
        return true;
    }
    while( oEffectedRoot.parentNode != null ){
        if( oEffectedRoot.parentNode.id == tagID ){
            return true;
        }
        oEffectedRoot = oEffectedRoot.parentNode;
    }
    
    return false;
}

function isObjEffectByEvent(e, obj){
    var oEffectedRoot = getEventTagetTag( e );
    if( oEffectedRoot == obj){
        return true;
    }
    while( oEffectedRoot.parentNode != null ){
        if( oEffectedRoot.parentNode == obj){
            return true;
        }
        oEffectedRoot = oEffectedRoot.parentNode;
    }
    
    return false;
}
