/* Copyright 2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html
$Id: popup.js,v 1.5 2007/04/30 12:19:05 imc Exp $
*/

var isIE;

function AJAXInteraction(url) {
   AJAXInteraction(url,null);
}

function AJAXInteraction(url, callback) {

    var req = init();
    req.onreadystatechange = processRequest;
        
    function init() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        isIE = true;
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest (){
	      if (req.readyState == 4) {
	        if (req.status == 200) {
	          if (callback) callback(req);
	        }
	      }
    }
    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }
    
    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body+"t="+new Date());
      
    }
}

function getElementY(element){
	var targetTop = 0;
	if (element.offsetParent) {
		while (element.offsetParent) {
			targetTop += element.offsetTop;
            element = element.offsetParent;
		}
	} else if (element.y) {
		targetTop += element.y;
    }
	return targetTop;
}
 	function getQrystring(formid){
		var formElements=document.forms[formid].elements;
		var qstr='';
		for (var n=0; n<formElements.length; n++) {
			qstr +=formElements[n].name+'='+escape(formElements[n].value)+'&';
		}
		return qstr;
	}
	function setQrystring(){
		
		for (var n=0; n<formElements.length; n++) {
			document.getElementById(formElements[n].name)=formElements[n].value;
		
		}
		
	}
	function getPreQrystring(){
	var query = window.location.search.substring(1);
	alert(query);
	}
	
	function showPopup(obj,querystring){
			//var url='/cisms/views/objects/popup.jsf?'+querystring;
			var url=querystring;
	   		setVisible(obj);
		 	makeRequest(url,document.getElementById('popup_div'));
	        popupInit();
	}
	function hidePopup() {
		   	obj = document.getElementById('popup_div');
			obj.style.visibility = 'hidden';
	} 
	function popupCallback(xmlHttp){
			var popupdiv=document.getElementById('popup_div');
			popupdiv.innerHTML=xmlHttp.responseText;
    }
    function setId(id,name,formid,idfield,namefield){
     var f= document.forms[formid];
     if(idfield!=null){
		if(f.elements[formid+':'+idfield]){
			f.elements[formid+':'+idfield].value=id;
		}else{
			f.elements[idfield].value=id;
		}
	 }
	 if(namefield!=null && namefield!='null'){
	 	if(f.elements[formid+':'+namefield]){
		 	f.elements[formid+':'+namefield].value=name;
		}else{
			f.elements[namefield].value=name;
		}
	 }
  }
  function setReference(id,name,formid,idfield,namefield,rowIndex){
     var f= document.forms[formid];
     if(idfield!=null){
			f.elements[idfield][rowIndex].value=id;
		
	 }
	 if(namefield!=null && namefield!='null'){
			f.elements[namefield][rowIndex].value=name;
	 }
  }
        
	function setVisible(link_obj){
			obj = document.getElementById('popup_div');
			obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
			placeIt('popup_div',link_obj);
   }
   	function makeRequest(url,mydiv) {
	        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('Giving up :( Cannot create an XMLHTTP instance');
	            return false;
	        }
	        httpRequest.onreadystatechange = function() { flushContents(httpRequest,mydiv); };
	        //httpRequest.open('GET', url+"&t="+new Date(), true);
			httpRequest.open('GET', url, true);
	        httpRequest.send(null);
	
	    }
	    function flushContents(httpRequest,mydiv) {
	        if (httpRequest.readyState == 4) {
	            if (httpRequest.status == 200) {
	                 mydiv.innerHTML=httpRequest.responseText;
	                 mydiv.visibility="show";
	           }else{
	                alert('There was a problem with the request.');
	            }
	        }
	
	    }
	x = 250;
	y = 700;
	function findPosY(obj) {
		    var curtop = 0;
		    if(obj.offsetParent)
		        while(1){
		          curtop += obj.offsetTop;
		          if(!obj.offsetParent)
		            break;
		          obj = obj.offsetParent;
		        }
		    else if(obj.y)
		        curtop += obj.y;
		    return curtop;
	}
	 function placeIt(obj,link_obj){
			obj = document.getElementById(obj);
			if (document.documentElement){
				theLeft = document.documentElement.scrollLeft;
				theTop = document.documentElement.scrollTop;
			}
			else if (document.body){
				theLeft = document.body.scrollLeft;
				theTop = document.body.scrollTop;
			}
			theLeft += x;
			theTop += (findPosY(link_obj)-175);
			obj.style.left = theLeft + 'px' ;
			obj.style.top = theTop + 'px' ;
		} 
		
    /*** for draggable window **/
    function findPos(obj) {	
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
   }

	function popupInit(){
	
		if (!document.all){
		document.captureEvents(Event.MOUSEDOWN);
	
		}
		
		document.onmousemove = mouseMove;
		document.onmouseup = mouseUP;
	}
	function mouseMove (evt) {
		
		if (document.all) {
			currX = event.clientX;
			currY = event.clientY;
		}
		else {
			currX= evt.pageX;
			currY = evt.pageY;
		}
		if(DRAG_MODE){
			moveWin();
		}
	}
	function moveWin(){
		obj = document.getElementById("popup_div");
		obj.style.left = (obj_x+currX-m_start_x)+'px';
		obj.style.top = (obj_y+currY-m_start_y)+'px';
	}
	var DRAG_MODE = false;
	var currX, currY;
	var m_start_x;
	var m_start_y;
	var obj_x;
	var obj_y;
	
	function mouseDn(){
		
		DRAG_MODE=true;	
		m_start_x = currX;
		m_start_y = currY;
		
		obj = document.getElementById("popup_div");
		var arr = findPos(obj);
		obj_x = arr[0];
		obj_y = arr[1];
	}
	function mouseUP(){
		if(DRAG_MODE){
			DRAG_MODE=false;
		}
	}
		
