// Dynamic HTML POPUP ( d.roche@lectra.com ) 
//
// USAGE :
// - include this file in your HTML page
// - also include the associated css file in your page 
// - somewhere in your <body> add this statement :
// <script language="javascript">
// PKcreateContainer('template/velonew/popkiller')
// </script>
// - you may use the PKpop function associated to an html element ( <input> ) eg :
// <input id="FRM_ID" type="text" value="">
// <a href="javascript:PKpop('popup_url.html', 'show1', 'FRM_ID', 'value',  'pick a value')">
// <img src="somepicture.gif"></a>
//
// in the popup page, you can use the PKreturn(value) function to write back 
// data in the caller page.
//
// You may also use the PKxxxxNum function if you wish to use several popup at same time.
//

// Array for storing return value info
PKstore = new Array()

// popup the default (0)  window with following arguments :
//
// url     :  url to fill the popup ( if false , use the previously popep url ) 
// style   :  css class name to dsplay the popup ( if false, use "show" ) 
// residt  :  id of html element to return result
// restype :  type of result to return ( "value" or  "html" or function name )
//         :  - value set the value property of the caller  
//         :  - html set the innerHtml of the caller  
//         :  - function is called with 2 parameters: residt, returned_value
// title   :  title of popup banner ( "" if false ) 
// x, y    :  coordinate to pop the window, ( at mouse position if false ) 
//
function PKpop(url, style, resid, restype, title, x, y) {
  PKpopNum('0', url, style, resid, restype, title, x, y)
}

// popdown the default (0) window 
//
function PKclose() {
 PKcloseNum('0')
}

// popup the specified window with following arguments :
//
// num     :  window number  ( no default ) 
// url     :  url to fill the popup ( if false , use the previously popep url ) 
// style   :  css class name to dsplay the popup ( if false, use "show" ) 
// residt  :  id of html element to return result
// restype :  type of result to return ( "value" or  "html" or function name )
//         :  - value set the value property of the caller  
//         :  - html set the innerHtml of the caller  
//         :  - function is called with 2 parameters: residt, returned_value
// title   :  title of popup banner ( "" if false ) 
// x, y    :  coordinate to pop the window, ( at mouse position if false ) 
//
function PKpopNum(num, url, style, resid, restype, title, x, y) {

 var tmp = PKstore[num]
 if ( ! tmp ) {
  alert("Popup container not defined, use PKcreateContainer() in the page")
  return
 } 

 if ( url ) {
  if ( tmp['ctyp'] == 'ifrm' ) {
   ChangeElementSrc('popifrm'+num, url); 
  } else {
   FetchUrlInto(url, 'popdiv'+num, 'html')
  }
 }
 if (x == -1 && y == -1) {
	// center the window
 } else {
	 if ( x && y ) {
	  ChangeElementPosition('popwin'+num, x, y)
	 } else {
	  ChangeElementPosition('popwin'+num, tempX-300, tempY-200)
	 }
 }

 if ( title ) {
  ChangeElementHtml('poptit'+num, title)
 } else {
  ChangeElementHtml('poptit'+num, "")
 }

 if ( style ) {
  ChangeElementClass('popwin'+num, 'popup pop_'+style);
 } else {
  ChangeElementClass('popwin'+num, 'popup pop_show');
 }
 if (x == -1 && y == -1) {
 	var elem = document.getElementById('popwin'+num);
 	ChangeElementPosition('popwin'+num, (window.screen.width - elem.clientWidth) / 2, (window.screen.height - elem.clientHeight) / 2);
 	//window.screen.width/2
 }
 if ( resid && restype ) {
  tmp['id'] = resid
  tmp['rtyp'] = restype
  PKstore[num] = tmp
 }

}

// popdown the specified window 
//
function PKcloseNum(num) {
 ChangeElementClass('popwin'+num, 'popup pop_hidn')
}

// default popup return value methods with following arguments :
//
// value   :  the value to return
// keep    :  flag tho keep the popup open after returning value ( if false, close the popup ) 
//

function PKreturn(value, keep)  {
 PKreturnNum('0', value, keep)
}

// specified popup return value methods with following arguments :
//
// num    :  window number  ( no default ) 
// value  :  the value to return
// keep   :  flag tho keep the popup open after returning value ( if false, close the popup ) 
//

function PKreturnNum(num, value, keep)  {
 // try different window to retrieve popup caller
 // - window.parent ( iframed popup ) 
 // - window.parent.parent ( double iframed popup ( explorer ) 
 // - window ( div based popup ) 
 try {
  var tmp = window.parent.PKstore[num]
  var type = tmp['rtyp']
  var base = window.parent
 } catch ( e1 ) {
  try {
   var tmp = window.parent.parent.PKstore[num]
   var type = tmp['rtyp']
   var base = window.parent.parent
  } catch ( e2 ) {
   try {
    var tmp = window.parent.parent.PKstore[num]
    var type = tmp['rtyp']
    var base = window.parent.parent
   } catch ( e3 ) {
    alert( "no popup caller found ");
    return   
   }
  }
 }
 if ( type == 'value') {
  base.ChangeElementValue(tmp['id'], value) 
 } else if ( type == 'html' ) {
  base.ChangeElementHtml(tmp['id'], value)
 } else if ( typeof(type) == "function" ) {
  type(tmp['id'],value)
 } else {
  alert("return type unknown : "+type + " " + typeof(type))  
 }

 if ( ! keep ) {
  base.PKcloseNum(num) 
 }
}

// create default popup container
//
// imgpath :  path for the images  ( no default ) 
//

function PKcreateContainer(imgpath) {
 // default create a Iframe container
 PKcreateIfrmContainerNum('0', imgpath)
}

// create specific popup container
//
// num     :  window number  ( no default ) 
// imgpath :  path for the images  ( no default ) 
//

function PKcreateIfrmContainerNum(num, imgpath) {
 document.writeln('<div id="popwin'+num+'" class="popup pop_hidn" style="top:200px;left:200px">')
 document.writeln('<img src="'+imgpath+'/vide.gif" class="popdrag" /><a href="javascript:PKcloseNum(\''+num+'\')"><img src="'+imgpath+'/close_popup.gif" border="0" class="popupclose" /></a>')
 document.writeln('<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">')
 document.writeln('<tr>')
 document.writeln(' <td><!-- <img src="'+imgpath+'/coin_pop.gif" /> --></td>')
 document.writeln(' <td class="TopPagepop" width="100%" id="poptit'+num+'">  POPTITLE</td>')
 document.writeln(' <td class="TopPagepop"><a href="javascript:PKcloseNum(\''+num+'\')"><img src="'+imgpath+'/close_popup.gif" border="0" class="popupclose" /></a></td>')
 document.writeln('</tr>')
 document.writeln('<tr><td colspan="3" class="pourtourpop" align="right">')
 document.writeln('<iframe src="about:blank" id="popifrm'+num+'" class="popifrm" frameborder="no" scrolling="yes" width="100%" height="100%">')
 document.writeln('iframe not supported , use a decent web browser !')
 document.writeln('</iframe>')
 document.writeln('</td></tr>')
 document.writeln('</table>')
 document.writeln('</div>')

 var tmp = new Array()
 tmp['ctyp'] = 'ifrm'
 PKstore[num] = tmp
}

function PKcreateDivContainerNum(num, imgpath) {
 document.writeln('<div id="popwin'+num+'" class="popup pop_hidn" style="top:200px;left:200px">')
 document.writeln('<img src="'+imgpath+'/vide.gif" class="popdrag" />')
 document.writeln('<table border="0" cellpadding="0" cellspacing="0" width="99%" height="99%">')
 document.writeln('<tr>')
 document.writeln(' <td><img src="'+imgpath+'/coin_pop.gif" /></td>')
 document.writeln(' <td class="TopPagepop" width="100%" id="poptit'+num+'">POPTITLE</td>')
 document.writeln(' <td class="TopPagepop"><a href="javascript:PKcloseNum(\''+num+'\')"><img src="'+imgpath+'/close_popup.gif" border="0" class="popupclose" /></a></td>')
 document.writeln('</tr>')
 document.writeln('<tr><td colspan="3" class="pourtourpop" align="right">')
 document.writeln('<div id="popdiv'+num+'" class="popdiv" >')
 document.writeln('</div>')
 document.writeln('</td></tr>')
 document.writeln('</table>')
 document.writeln('</div>')

 var tmp = new Array()
 tmp['ctyp'] = 'div'
 PKstore[num] = tmp
}
