/**
 * This function shows a poup and gives focus to it.
 */
function popitup(url) {
	newwindow=window.open(url,'name','height=350,width=350,scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}

/**
 * This function displays pdf document and gives focus to it.
 */
    function popit_pdf(tmp_url,nameArray, numItems) {
	     var itm = false;
         var close_tag = 0;/* tag to hold user input*/
    	 for (var count=0; count < numItems; count++)
    	 {
             itm = getItem(nameArray[count]);
             if(itm)
             {
                 if(itm.style.display == 'none')
                     close_tag =close_tag | (1<<(count+2));
                 else
                     close_tag = close_tag & (~(1<<(count+2)));
             }
    	 }
        // var url = "list_products.php?pdf=1&close_tag=" + close_tag;
        var url = tmp_url + "&close_tag=" + close_tag;

         newwindow=window.open(url, 'name','height=600,width=800,scrollbars=yes,resizable=yes');
         if (window.focus) {newwindow.focus()}
            return false;
         
    }
    
/**
 * This function shows a poup and gives focus to it.
 */
function popitupprice(url) {
	newwindow=window.open(url,'name','height=400,width=500,scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}

/**
 * This function return an element from its id.
 */
function getItem(id)
{
    var itm = false;
    if(document.getElementById)
        itm = document.getElementById(id);
    else if(document.all)
        itm = document.all[id];
    else if(document.layers)
        itm = document.layers[id];

    return itm;
}

/**
 * This function finds an absolute position of an object.
 */
function findAbsolutePosition(obj){
    this.X = obj.offsetLeft;
    this.Y = obj.offsetTop;
    while(obj.offsetParent){
        this.X=this.X+obj.offsetParent.offsetLeft;
        this.Y=this.Y+obj.offsetParent.offsetTop;
        if(obj==document.getElementsByTagName('body')[0]){break}
        else{obj=obj.offsetParent;}
    }
    return this
}

/**
 * This function returns the current scrolling
 */
function getScrollXY() {
  this.scrOfX = 0;
  this.scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    this.scrOfY = window.pageYOffset;
    this.scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    this.scrOfY = document.body.scrollTop;
    this.scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    this.scrOfY = document.documentElement.scrollTop;
    this.scrOfX = document.documentElement.scrollLeft;
  }
  return this;
}

/**
 * this function aligns "floating_div_id" acording to "locator_id" when "locator_id" is not shown. "locator_id" should refer a table cell.
 */
function ShowFloatingDiv(floating_div_id,locator_id)
{
    var d = document;
	var el = d.getElementById?d.getElementById(floating_div_id):d.all?d.all[floating_div_id]:d.layers[floating_div_id];
    var scr = getScrollXY();

    var locator = d.getElementById?d.getElementById(locator_id):d.all?d.all[locator_id]:d.layers[locator_id];

    var locator_position = findAbsolutePosition(locator);

    // isn't the locator visible?
    if (parseInt(scr.scrOfY)>parseInt(locator_position.Y)) {
        el.style.visibility = 'visible';
        el.style.left =  parseInt(locator_position.X)+"px";
        el.style.top  = (parseInt(scr.scrOfY)+1)+"px";

        // fixing cell width
        var i = parseInt(locator.offsetWidth)-15;
        el.style.width = (i)+"px";
    } else {
        el.style.visibility = 'hidden';
        el.style.left =  parseInt(10)+"px";
        el.style.top  =  parseInt(10)+"px";
    }
}

/*
 * This function aligns a DIV (and probably other elements too) according to a
 * left side element.
 */
function AlignDivAccordingLeftElement(content_div_id,locator_id,loffset,roffset) {
    var d = document;
	var el = d.getElementById?d.getElementById(content_div_id):d.all?d.all[content_div_id]:d.layers[content_div_id];
    var locator = d.getElementById?d.getElementById(locator_id):d.all?d.all[locator_id]:d.layers[locator_id];
    var locator_position = findAbsolutePosition(locator);

    var local_top = 0;//locator_position.Y;
    
    var locatorWidth = 0;//parseInt(locator.style.pixelWidth);
    var local_left = parseInt(locator_position.X) + locatorWidth + parseInt(loffset);
    var local_width = parseInt(d.body.clientWidth) - locatorWidth - parseInt(loffset) - parseInt(roffset);

    el.style.left  = parseInt(local_left)  + "px";
    el.style.top   = parseInt(local_top)   + "px";
    el.style.width = parseInt(local_width) + "px";
}

// Fix content's width using screen width
function FixDivWidth(content_div_id, woffset, minWidth, maxWidth) {
    var d = document;
	var el = d.getElementById?d.getElementById(content_div_id):d.all?d.all[content_div_id]:d.layers[content_div_id];
 
    var local_width = parseInt(d.body.clientWidth) - parseInt(woffset);

    if (local_width<minWidth){
        local_width = minWidth;
    }

    if (local_width>maxWidth){
        local_width = maxWidth;
    }

    el.style.width = parseInt(local_width) + "px";
}

