function getParentFormElement(elem) {
  form_obj = elem;
  while (form_obj.tagName!='FORM') {
    form_obj = form_obj.parentNode;
    if (!form_obj) {
      alert('Form not found! Please put the list control in a form!'); return 0;
    }
  }
  return form_obj;
}

function getForm(elem) {
	return getParentFormElement(elem);
}

function ADivButton(d){
  var col = d.getElementsByTagName('A');
  if(col.length>0){
    col[0].click();
  }
}


function fixIELabel(){
	var oLabs = document.getElementsByTagName('label');
	for(var i=0; i<oLabs.length; i++){
		var oLab = oLabs[i];
		
		if(!oLab.htmlFor || oLab.onclick) continue;
		var oSel = document.getElementById(oLab.htmlFor);
		if(!oSel) continue;
		if(oSel.tagName=='SELECT'){
			oLab.htmlFor = null;
			oLab.onclick = function(){
				oSel.focus();
			}
		}
		
	}

}


/* COOKIES   */

function SetCookie(sName, sValue)
{
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2020 23:59:59 GMT;";
}

function DelCookie(sName)
{
  var sValue='block';
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function GetCookie(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return null;
}

/* END COOKIES  */

function getScrollOffsets() {
	var i;
	var off = {x:0, y:0}
	var a=document.getElementsByTagName("html");
	var scy=document.body.scrollTop;
	var scx=document.body.scrollLeft;
	for(i=0;i<a.length;i++) {
		if(a[i].scrollTop) {
			scy+=a[i].scrollTop;
			
		}
		if(a[i].scrollLeft) {
			scx+=a[i].scrollLeft;
		}
	}
	
	if(scx==null||scx==undefined) {
		scx=document.body.scrollLeft;
	}
	if(scy==null||scy==undefined) {
		scy=document.body.scrollTop;
	}
	off={x:scx,y:scy};
	return off;
}

function getBoundingBox(el) {
	var off=getScrollOffsets();
	var scx=off.x;
	var scy=off.y;
	var box = {top:0, left:0, right:0, bottom:0, height:0, width:0}

	if (document.getBoxObjectFor) {
		var r = document.getBoxObjectFor(el);
		//box = {top:r.y - document.body.scrollTop, left:r.x - document.body.scrollLeft, right:r.x - document.body.scrollLeft + r.width, bottom:r.y - document.body.scrollTop + r.height, height:r.height, width:r.width};
		box = {top:r.y , left:r.x , right:r.x  + r.width, bottom:r.y  + r.height, height:r.height, width:r.width};
	} else if (el.getBoundingClientRect) {
		var r = el.getBoundingClientRect();
		box = {top:r.top+scy, left:r.left+scx, right:r.right+scx, bottom:r.bottom+scy, height:r.bottom-r.top, width:r.right-r.left};
		//box = {top:r.top, left:r.left, right:r.right, bottom:r.bottom, height:r.bottom-r.top, width:r.right-r.left};
	}
	else {
		var left = el.offsetLeft;
		var top = el.offsetTop;
		var r= { top: top, right: left + el.clientWidth, bottom: top + el.clientHeight, left: left }; 
		box = {top:r.y , left:r.x , right:r.x  + r.width, bottom:r.y  + r.height, height:r.height, width:r.width};
		
	}

	return box;
}
function getPrintLink(){
	var url = window.location.protocol +'//'+ window.location.hostname +  window.location.pathname;
	
	if(window.location.search){
		url += window.location.search + '&print=on';
	} else {
		url += '?print=on';
	}
	url += window.location.hash;
	return url;
}

function countAd(ad_id) {
	try {
	var httpRequest;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Cannot create an XMLHTTP instance');
            return false;
        }
        
        httpRequest.open("GET", "/adsClick.php?noredir=1&id="+ad_id, false)
		httpRequest.send(null);
	}catch(e) {		
	}
	
}
