// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/javascripts/popup-window.php
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as part of another product.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.

// USAGE
//
// function popup_show(id, drag_id, exit_id, position, x, y, position_id)
//
// id          - id of a popup window;
// drag_id     - id of an element within popup window intended for dragging it
// exit_id     - id of an element within popup window intended for hiding it
// position    - positioning type:
//               "element", "element-right", "element-bottom", "mouse",
//               "screen-top-left", "screen-center", "screen-bottom-right"
// x, y        - offset
// position_id - id of an element relative to which popup window will be positioned


// ***** Variables *************************************************************

var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;



// ***** custom *******************************************************
function getWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}


function getHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getScrollTop() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function getScrollLeft() {
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

function topzindex()
{
  var d = new Date();

  var curr_hour = d.getHours();
      curr_hour = curr_hour * 60 * 60;
  var curr_min = d.getMinutes();
      curr_min = curr_min * 60;
  var curr_second = d.getSeconds();
  
  curr_second = curr_hour + curr_min + curr_second
  return ("" + curr_second);  

}

function donepickiframe(objrf,objrf2,rowobj1,rowobj2,btnclose)
{
	var rf =  document.getElementById(objrf);
	var rf2 =  document.getElementById(objrf2);
	
	if (rf != null)
	{
	  if (rf.value!='')
        parent.document.getElementById(rf.value).value=document.getElementById(rowobj1).value;
    }
	if (rf2 != null)
	{
	  if (rf2.value!='')
	  {
	    if (rf2.value.indexOf('lbl')>0)
	    {
          parent.document.getElementById(rf2.value).innerHTML=document.getElementById(rowobj2).value;
	    }
	    else
	    {
          parent.document.getElementById(rf2.value).value=document.getElementById(rowobj2).value;
        }
      }
    }
    
    if (btnclose!='')
    {
      parent.document.getElementById(btnclose).click();
    }
  

}

       
function setIFRAME(iframeobj,hfobj) {
var block1 = document.getElementById(iframeobj);
var block2 = document.getElementById(hfobj);

block1.src=block2.value;

  return true;
}       

function iframe_close(targetid,iframeobj)
{
  var element = document.getElementById(targetid);
  var element2 = document.getElementById(iframeobj);
  element.style.display = 'none';
  if (element2!=null) element2.src='';
}

function iframenewwindow(iframeobj,objprefix)
{
  var element = document.getElementById(iframeobj);
  var element2 = document.getElementById(objprefix);
  
  //alert(element.contentWindow.location.href);
  window.open(element2.value+element.contentWindow.location.href,'_blank');


}

function popupinitial(id, drag_id, exit_id, position, x, y, position_id)
{
  var element      = document.getElementById(id);
  var drag_element = document.getElementById(drag_id);
  var exit_element = document.getElementById(exit_id);

  //var width        = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
  //var height       = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
  var width        = getWidth();
  var height       = getHeight();
  var xscrollTop        = getScrollTop();
  var xscrollLeft       = getScrollLeft();


  element.style.position = "absolute";
  //element.style.display  = "block";
  element.style.zIndex = topzindex();
  
  if (position == "element" || position == "element-right" || position == "element-bottom")
  {
    var position_element = document.getElementById(position_id);

    for (var p = position_element; p; p = p.offsetParent)
      if (p.style.position != 'absolute')
      {
        x += p.offsetLeft;
        y += p.offsetTop;
      }

    if (position == "element-right" ) x += position_element.clientWidth;
    if (position == "element-bottom") y += position_element.clientHeight;

    element.style.left = x+'px';
    element.style.top  = y+'px';
  }

  if (position == "mouse")
  {
    element.style.left = (document.documentElement.scrollLeft+popup_mouseposX+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +popup_mouseposY+y)+'px';
  }

  if (position == "screen-top-left")
  {
    element.style.left = (document.documentElement.scrollLeft+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +y)+'px';
  }

  ileft = (xscrollLeft+(width -element.clientWidth )/2+x);
  itop  = (xscrollTop +(height-element.clientHeight)/2+y);


  if (position == "screen-center")
  {
    if (ileft<0) ileft=0;
    if (itop<0) itop=0;
    element.style.left = ileft+'px';
    element.style.top  = itop+'px';
  }

  if (position == "screen-bottom-right")
  {
    element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )  +x)+'px';
    element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)  +y)+'px';
  }

  drag_element['target']   = id;
  drag_element.onmousedown = popup_mousedown_window;
 // exit_element.onclick     = popup_exit;
}

function popupmoveinitial(id, drag_id)
{
  var element      = document.getElementById(id);
  var drag_element = document.getElementById(drag_id);

  drag_element['target']   = id;
  drag_element.onmousedown = popup_mousedown_window;

}

// ***** popup_mousedown *******************************************************
        
function popup_mousedown(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  popup_mouseposX = ie ? window.event.clientX : e.clientX;
  popup_mouseposY = ie ? window.event.clientY : e.clientY;
}


// ***** popup_mousedown_window ************************************************

function popup_mousedown_window(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  if ( ie && window.event.button != 1) return;
  if (!ie && e.button            != 0) return;

  popup_dragging = true;
  popup_target   = this['target'];
  popup_mouseX   = ie ? window.event.clientX : e.clientX;
  popup_mouseY   = ie ? window.event.clientY : e.clientY;

  if (ie)
       popup_oldfunction = document.onselectstart;
  else popup_oldfunction = document.onmousedown;

  if (ie)
       document.onselectstart = new Function("return false;");
  else document.onmousedown   = new Function("return false;");
  
  var element = document.getElementById(popup_target);
  element.style.zIndex = topzindex();
  
}


// ***** popup_mousemove *******************************************************

function popup_mousemove(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;

  if (!popup_dragging) return;

  element.style.left = (element.offsetLeft+mouseX-popup_mouseX)+'px';
  element.style.top  = (element.offsetTop +mouseY-popup_mouseY)+'px';

  popup_mouseX = ie ? window.event.clientX : e.clientX;
  popup_mouseY = ie ? window.event.clientY : e.clientY;
}

// ***** popup_mouseup *********************************************************

function popup_mouseup(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  if (!popup_dragging) return;

  popup_dragging = false;

  if (ie)
       document.onselectstart = popup_oldfunction;
  else document.onmousedown   = popup_oldfunction;
}

// ***** popup_exit ************************************************************

function popup_exit(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  popup_mouseup(e);
  element.style.display = 'none';
}

// ***** popup_showup ************************************************************
//this function will replace popup_show
function popup_showup(boxid,urlid,xtitle,xheight,xwidth,hfprefixurl)
{
  var element      = document.getElementById(boxid);
  var eurl      = document.getElementById(urlid);
  
  var btnclose = boxid+'btnclose';
  var drag_id = boxid+'tbltopbar';
  var iframe_id = boxid+'iframe';
  
  var ss

  ss="<table cellpadding='0' cellspacing='0'>";
  ss=ss+"<tr><td class='groupboxc1_1'>...</td><td class='groupboxc1_2'></td><td class='groupboxc1_3'>...</td></tr>";

  ss=ss+"<tr><td class='groupboxc2_1'>&nbsp;</td><td class='groupboxc2_4'>";
  ss=ss+"<table cellpadding='0' cellspacing ='0' width='100%' id='"+drag_id+"' style='cursor: move'>";
  ss=ss+"<tr><td style='height: 22px' class='groupboxc2_4'>"+xtitle+"</td>";
  ss=ss+"<td align='right' style='height: 22px'>";
  ss=ss+"<input id='btnnewwindow' onclick=\"iframenewwindow('" + iframe_id + "','" + hfprefixurl + "');\" type='button' value='++' Class='normalbtn' style='width: 24px' />&nbsp;";
  ss=ss+"<input id='"+btnclose+"' onclick=\"iframe_close('" + boxid + "','" + iframe_id + "');\" type='button' value='X' Class='normalbtn' style='width: 24px' />";
  ss=ss+"</td></tr></table>";
  ss=ss+"</td><td class='groupboxc2_3'>&nbsp;</td></tr><tr><td class='groupboxc2_1'>..</td><td class='groupboxc2_2'>";
  ss=ss+"<iframe style='width: "+xwidth+"; height: "+xheight+"' scrolling='auto' id='"+iframe_id+"' frameborder='no' src='"+eurl.value+"'></iframe> ";
  ss=ss+"</td><td class='groupboxc2_3'>&nbsp;.</td></tr>";
  ss=ss+"<tr><td class='groupboxc3_1'></td><td class='groupboxc3_2'></td><td class='groupboxc3_3'></td></tr></table> ";

  element.innerHTML=ss;
  element.style.display  = "block";
/*

<table cellpadding="0" cellspacing="0">
<tr><td class="groupboxc1_1_1">...</td><td class="groupboxc1_2_1">
<div id="tbltopbar" runat="server" style="width:100%; height:100%; vertical-align: middle;">
<asp:LinkButton ID="lbnewwindow" CssClass="groupboxc1_2_2" runat="server">[New Window]</asp:LinkButton>
<asp:LinkButton ID="lbclose" CssClass="groupboxc1_2_2" runat="server">[Close]</asp:LinkButton>
</div>
</td><td class="groupboxc1_3_1">...</td></tr>


<tr><td class="groupboxc2_1"></td><td class="groupboxc2_2">
 <iframe style="width: 200px; height: 100px;" id="IFRAME1" runat="server" frameborder="no"></iframe>    
</td><td class="groupboxc2_3">&nbsp;.</td></tr>
<tr><td class="groupboxc3_1"></td><td class="groupboxc3_2"></td><td class="groupboxc3_3"></td></tr>
</table>   
*/



 var drag_element = document.getElementById(drag_id);
 var iframe_element = document.getElementById(iframe_id);

   var width        = getWidth();
  var height       = getHeight();
  
  
  if (element.clientHeight>height)
  {
     iframe_element.style.height=(height-40)+'px';
  }
    // iframe_element.style.height=height+'px';
  
  var xscrollTop        = getScrollTop();
  var xscrollLeft       = getScrollLeft();


  element.style.position = "absolute";
  element.style.display  = "block";
  element.style.zIndex = topzindex();

 
  ileft = (xscrollLeft+(width -element.clientWidth )/2);
  itop  = (xscrollTop +(height-element.clientHeight)/2);


  //if (position == "screen-center")
  //{
    if (ileft<0) ileft=0;
    if (itop<0) itop=0;
    element.style.left = ileft+'px';
    element.style.top  = itop+'px';
 // }


  drag_element['target']   = boxid;
  drag_element.onmousedown = popup_mousedown_window;
}

// ***** popup_showupv2 ************************************************************
//this function will replace popup_show
function popup_showupv2(boxid,urlid,xheight,xwidth,hfprefixurl)
{
  var element      = document.getElementById(boxid);
  var eurl      = document.getElementById(urlid);
  
  var btnclose = boxid+'btnclose';
  var drag_id = boxid+'tbltopbar';
  var iframe_id = boxid+'iframe';
  
  var ss


  ss="<table cellpadding='0' cellspacing='0'>"
  ss=ss+"<tr><td class='groupboxc1_1_1'>...</td><td class='groupboxc1_2_1'>"
  ss=ss+"<div id='"+drag_id+"' style='width:100%; height:100%; vertical-align: middle;'>"
  ss=ss+"<a onclick=\"iframenewwindow('" + iframe_id + "','" + hfprefixurl + "');\" Class='groupboxc1_2_2'>[New Window]</a>"
  ss=ss+"<a ID='"+btnclose+"' Class='groupboxc1_2_2' onclick=\"iframe_close('" + boxid + "','" + iframe_id + "');\">[Close]</a></div>"
  ss=ss+"</td><td class='groupboxc1_3_1'>...</td></tr>"
  ss=ss+"<tr><td class='groupboxc2_1'></td><td class='groupboxc2_2'>"
  ss=ss+"<iframe style='width: "+xwidth+"; height: "+xheight+"' scrolling='auto' id='"+iframe_id+"' frameborder='no' src='"+eurl.value+"'></iframe> ";
  ss=ss+"</td><td class='groupboxc2_3'>&nbsp;.</td></tr>"
  ss=ss+"<tr><td class='groupboxc3_1'></td><td class='groupboxc3_2'></td><td class='groupboxc3_3'></td></tr></table>"  


  element.innerHTML=ss;
  element.style.display  = "block";
  
/*
<table cellpadding="0" cellspacing="0">
<tr><td class="groupboxc1_1_1">...</td><td class="groupboxc1_2_1">
<div id="tbltopbar" style="width:100%; height:100%; vertical-align: middle;">
<asp:LinkButton ID="lbnewwindow" CssClass="groupboxc1_2_2" runat="server">[New Window]</asp:LinkButton>
<asp:LinkButton ID="lbclose" CssClass="groupboxc1_2_2" runat="server">[Close]</asp:LinkButton></div>
</td><td class="groupboxc1_3_1">...</td></tr>
<tr><td class="groupboxc2_1"></td><td class="groupboxc2_2">
 <iframe style="width: 200px; height: 100px;" id="IFRAME1" runat="server" frameborder="no"></iframe>    
</td><td class="groupboxc2_3">&nbsp;.</td></tr>
<tr><td class="groupboxc3_1"></td><td class="groupboxc3_2"></td><td class="groupboxc3_3"></td></tr></table>     
*/



 var drag_element = document.getElementById(drag_id);
 var iframe_element = document.getElementById(iframe_id);

   var width        = getWidth();
  var height       = getHeight();
  
  
  if (element.clientHeight>height)
  {
     iframe_element.style.height=(height-40)+'px';
  }
    // iframe_element.style.height=height+'px';
  
  var xscrollTop        = getScrollTop();
  var xscrollLeft       = getScrollLeft();


  element.style.position = "absolute";
  element.style.display  = "block";
  element.style.zIndex = topzindex();

 
  ileft = (xscrollLeft+(width -element.clientWidth )/2);
  itop  = (xscrollTop +(height-element.clientHeight)/2);


  //if (position == "screen-center")
  //{
    if (ileft<0) ileft=0;
    if (itop<0) itop=0;
    element.style.left = ileft+'px';
    element.style.top  = itop+'px';
 // }


  drag_element['target']   = boxid;
  drag_element.onmousedown = popup_mousedown_window;
}
// ***** popup_show ************************************************************

function popup_show(id, drag_id, exit_id, position, x, y, position_id)
{
  var element      = document.getElementById(id);
  var drag_element = document.getElementById(drag_id);
  var exit_element = document.getElementById(exit_id);

  //var width        = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
  //var height       = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
  var width        = getWidth();
  var height       = getHeight();
  
  
  var xscrollTop        = getScrollTop();
  var xscrollLeft       = getScrollLeft();


  element.style.position = "absolute";
  element.style.display  = "block";
  element.style.zIndex = topzindex();

  if (position == "element" || position == "element-right" || position == "element-bottom")
  {
    var position_element = document.getElementById(position_id);

    for (var p = position_element; p; p = p.offsetParent)
      if (p.style.position != 'absolute')
      {
        x += p.offsetLeft;
        y += p.offsetTop;
      }

    if (position == "element-right" ) x += position_element.clientWidth;
    if (position == "element-bottom") y += position_element.clientHeight;

    element.style.left = x+'px';
    element.style.top  = y+'px';
  }

  if (position == "mouse")
  {
    element.style.left = (document.documentElement.scrollLeft+popup_mouseposX+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +popup_mouseposY+y)+'px';
  }

  if (position == "screen-top-left")
  {
    element.style.left = (document.documentElement.scrollLeft+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +y)+'px';
  }

 
  ileft = (xscrollLeft+(width -element.clientWidth )/2+x);
  itop  = (xscrollTop +(height-element.clientHeight)/2+y);


  if (position == "screen-center")
  {
    if (ileft<0) ileft=0;
    if (itop<0) itop=0;
    element.style.left = ileft+'px';
    element.style.top  = itop+'px';
  }

  if (position == "screen-bottom-right")
  {
    element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )  +x)+'px';
    element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)  +y)+'px';
  }

  drag_element['target']   = id;
  drag_element.onmousedown = popup_mousedown_window;
  //exit_element.onclick     = popup_exit;
}


// ***** Attach Events *********************************************************

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousedown', popup_mousedown);
else document.addEventListener('mousedown', popup_mousedown, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousemove', popup_mousemove);
else document.addEventListener('mousemove', popup_mousemove, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmouseup', popup_mouseup);
else document.addEventListener('mouseup', popup_mouseup, false);
