﻿function GE(id) {
    return document.getElementById(id);
}

function find(id,tagName,o){
	if(!o){o=document;}
	var e=o.getElementsByTagName(tagName);
	var bestMatch=null;
	for(var i=0;i<e.length;i++){
		var pos=e[i].id.indexOf('_'+id);
		if(pos>-1){
			if(e[i].id.indexOf('_'+id)==e[i].id.length-id.length-1){return e[i];}
			if(e[i].id.indexOf(id)==e[i].id.length-id.length){
				bestMatch=e[i];
			}else if(!bestMatch&&e[i].id.indexOf(id)>-1){
				bestMatch=e[i];
			}
		}else if(!bestMatch&&e[i].id.indexOf(id)>-1){
			bestMatch=e[i];
		}
	}
	return bestMatch;
}

function findParentByAttribute(el,attrName,attrVal){
    if(el.getAttribute(attrName)==attrVal){return el;}
    el=el.parentNode;
    while(el&&el.tagName!='BODY'){
        if(el.getAttribute(attrName)==attrVal){return el;}
        el=el.parentNode;
    }
    return null;
}

function getChildNodes(el,tagName){
	var els=new Array();
	for(var i=0;i<el.childNodes.length;i++){
		if(el.childNodes[i].tagName==tagName.toUpperCase()){
			els[els.length]=el.childNodes[i];
		}
	}
	return els;
}

var _uniqueIDIndex = 0;
function getUniqueID(el){
    if(el.id==''){el.id='__jsUniqueID'+_uniqueIDIndex++;}
}

function removeElement(el,timeout){
    if(timeout){
        getUniqueID(el);
        removeElementById(el.id,timeout);
    }else{
        el.parentNode.removeChild(el);
    }
}

function removeElementById(id,timeout){
    if(timeout){
        setTimeout('removeElementById("'+id+'")',timeout);
    }else{
        removeElement($get(id));
    }
}

function search(btnGO){
    var divSearchBar=btnGO.parentNode;
    var txtSearch=find('txtSearch','INPUT',divSearchBar);
    var ddlCategory=find('ddlCategory','SELECT',divSearchBar);
    var url='/Listing/Search.aspx?s='+escape(txtSearch.value);
    var categoryID=ddlCategory.options[ddlCategory.selectedIndex].value;
    if(categoryID>0){
        url+="&cat="+categoryID;
    }
    document.location=url;
}

function UpdateCount(textBoxId, charCountId, limit, limitErrorMessage) {
    var charCount = GE(charCountId);
    var textBox = GE(textBoxId);
    
    var charLength = textBox.value.length;
    
    charCount.innerText = charLength;
    
    if (charLength > limit) {
        var string = textBox.value.substring(0, limit);
        textBox.value = string;
        alert(limitErrorMessage);
        charCount.innerText = string.length;
    }
}