/*
 *  Jesse Charbneau  Copyright 2007
 *  
 *  Use is hereby granted to use any of this code however you like.  No warranty for any code here.
 *
 *
 *
 *
 */
 

/* 
    Mouse hover tricks for images - Also see the stylesheet for the classes
    
*/

function mOver(oElement) {
   oElement.className = 'mOver';
}

function mOut(oElement) {
   oElement.className = 'mOut';
}


var ie=document.all;
var firefox=document.getElementById&&!document.all;

var isDrag=false;
var x,y;
var dobj;

function mouseMove(e)
{
  if (isDrag)	//only move the box if the user is dragging it
  {	
	if (firefox){
		dobj.style.left = e.clientX + (tx - x);
		dobj.style.top  = e.clientY + (ty - y);
	}
	else{		//IE
		dobj.style.left = event.clientX + (tx - x);
		dobj.style.top  = event.clientY + (ty - y);
	}    
  }
}

//handle click on widget title bar
function selectMouse(e) 
{	
	if (firefox){
		//verify that user clicked the 'titlebar' element 
		var p=e.target;	
		if (p.attributes['id'] && p.attributes['id'].value.indexOf("titlebar")!=-1){
			isDrag=true;
			x=e.clientX;
			y=e.clientY;
			//get the widget coordinates from the widget root
			var pstr=p.attributes['id'].value;
			var temp=pstr.substring(pstr.indexOf('_')+1,pstr.length);
//			dobj=document.getElementById('widget_'+temp);
			dobj=document.getElementById('small_cal1');
			//record widget location
			tx=parseInt(dobj.style.left);
			ty=parseInt(dobj.style.top);
			//change zIndex
			dobj.style.zIndex=z; z++;
		}
	}
	else{		//IE
		//verify that user clicked the 'titlebar' element
		var p=event.srcElement;		
		if (p.attributes['id'] && p.attributes['id'].value.indexOf("titlebar")!=-1){
			isDrag=true;
			x=event.clientX;
			y=event.clientY;
			//get the widget coordinates from the widget root
			var pstr=p.attributes['id'].value;
			var temp=pstr.substring(pstr.indexOf('_')+1,pstr.length);			
//			dobj=document.getElementById('widget_'+temp);
			dobj=document.getElementById('small_cal1');
			//record widget location
			tx=parseInt(dobj.style.left);
			ty=parseInt(dobj.style.top);
			//change zIndex
			dobj.style.zIndex=z; z++;
		}
	}
}
document.onmousemove=mouseMove;
document.onmousedown=selectMouse;
document.onmouseup=function(){
	isDrag=false;
	dobj=null;
}

/****************************************************************************/
/*****************************  AJAX CODE ***********************************/
/****************************************************************************/
function createRequestObject() { 
   var req; 
   if(window.XMLHttpRequest){ 
      // Firefox, Safari, Opera... 
      req = new XMLHttpRequest(); 
   } else if(window.ActiveXObject) { 
      // Internet Explorer 5+ 
      req = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
      // There is an error creating the object, 
      // just as an old browser is being used. 
      alert('Problem creating the XMLHttpRequest object'); 
   } 
   return req; 
} 
function handleDivTag(divtag) 
{ 
   var divtag; 
   return divtag; 
} 
// Make the XMLHttpRequest object 
var http = createRequestObject(); 

// Create the Divtag Handler -- Mainly an IE 6 Fix 
var divhandler = new handleDivTag(null); 

function handleResponse() { 
   
   if(http.readyState == 4 && http.status == 200){ 
      // Text returned FROM the PHP script 
      var response = http.responseText; 
      if(response) { 
// alert(response);
         // UPDATE ajaxTest content 
        sethtml(divhandler.divtag,response);
      } 
   } 
}


function sethtml(div,content)
{
    var search = content;
    var script;
         
    while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i))
    {
      search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length);
      
      if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break;
      
      block = search.substr(0, search.indexOf(RegExp.$1));
      search = search.substring(block.length + RegExp.$1.length);
      
      var oScript = document.createElement('script');
      oScript.text = block;
      document.getElementsByTagName("head").item(0).appendChild(oScript);
    }
   
    document.getElementById(div).innerHTML=content;
} 

/****************************************************************************/
/************************* CALENDAR AJAX CODE *******************************/
/****************************************************************************/
function newCalRequest(lid, day, month, year) { 
   // Open PHP script for requests 
   http.open('get', 'ajax/ajcal.phtml?id='+ lid + '&day=' + day + '&month=' + month + '&year=' + year); 
   http.onreadystatechange = handleResponse; 
   divhandler.divtag = lid; 
   http.send(null); 

} 

function newDayViewerRequest(day, month, year) { 
   // Open PHP script for requests 
   id = "day_viewer";
   http.open('get', 'ajax/ajDV.phtml?day='+ day + '&month=' + month + '&year=' + year); 
   http.onreadystatechange = handleResponse; 
   divhandler.divtag = id; 
   http.send(null); 
} 


<!--
function openPopup( pageToLoad, winName, width, height, center) {
    xposition=0; yposition=0;

    if ((parseInt(navigator.appVersion) >= 4 ) && (center))
    {
        xposition = (screen.width - width) / 2;
        yposition = (screen.height - height) / 2;
    }
    
    args = "width=" + width + ","     + "height=" + height + "," 
    + "location=0,"     + "menubar=0,"    + "resizable=1,"    + "resize=1,"    + "scrollbars=0,"
    + "status=0,"     + "titlebar=0,"    + "toolbar=0,"    + "hotkeys=0,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only
    window.open( pageToLoad,winName,args );

} 
// -->


