// more global variables /////////////////////////////////
var arDragObj;  // the drag object for sequence modules
var arDragYMax; // the maximum y drag coordinate
var editFrStatus;

// showing/hiding divs  /////////////////////////////////

function showPop(event,id,text) {
  var x, y;

  el = document.getElementById(id);

	if (el.firstChild) { // browser supporting el.firstChild
		el.firstChild.nodeValue = text;

		// step 3
		if (document.all) {
			x = event.clientX + document.body.scrollLeft;
			y = event.clientY + document.body.scrollTop;
		} else {
			x = event.pageX;
			y = event.pageY;
		}

// 		if (window.event) {
// 				  x = window.event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft);
// 				  y = window.event.clientY + (document.documentElement.scrollTop  || document.body.scrollTop);
// 		}
// 		else {
// 		  x = event.clientX; // + window.scrollX;
// 		  y = event.clientY; // + window.scrollY;
// 		}	
		x += 10; y += 15;
		el.style.left = x + "px";
		el.style.top  = y + "px";
		el.style.visibility = "visible";
	} else { // browser not supporting el.firstChild
		window.status = text;
		return true;
	}
}

function hideDiv(id) {
	var el;
  el = document.getElementById(id);
  el.style.visibility = "hidden";
  el.style.left = "-500px";
  el.style.top  = "-100px";
}


////////////////// function for a popup window ////////////////

function popWin(url,name,x,y,toolbar,resizable,scrollbars,menubar,status) {
	if (!name) name = "popWin";
	if (!x) x = "400";
	if (!y) y = "300";
	if (!toolbar) toolbar = "no";
	if (!resizable) resizable = "no";
	if (!scrollbars) scrollbars = "no";
	if (!menubar) menubar = "no";
	options = "width="+x+",height="+y+",toolbar="+toolbar+",resizable="+resizable+",scrollbars="+scrollbars+",menubar="+menubar+",status="+status+""
	window.open(url,name,options);
}


// open a window with a specific size
function openWin(url,winName,wW,wH,opts) {

	if(!opts) opts = "toolbar=1,location=1,directories=0,status=1,menuBar=1,resizable=1,scrollBars=1";
			
	if (wW && wH) {
		sW = screen.availWidth;
		sH = screen.availHeight;
		wW = parseInt(wW) + 20; // add marginright
		wH = parseInt(wH) + 30; // add marginbottom
		
		// compare the req. window size with screen size (minus margin of 20 px).
		sbW=0;
		sbH=0;
	
		if (sW < wW-60) { // screen-width is smaller than requested window-width
			wW = sW - 60; // set window-width to screen-width minus a small tolerance-margin
			sbH = 40; // window is smaller than requested, so most likely a  vertical scrollbar will appear, add space for it to height
		}
	
		if (sH < wH-60) { // screen-height is smaller than requested window-height
			wH = sH - 60; // set window-height to screen-height minus a small tolerance-margin
			sbW = 40; // window is smaller than requested, so most likely a  vertical scrollbar will appear, add space for it to width;
		}
		
		// if window is smaller than requested show scrollbars
		// if (sbW || sbH) opts = opts + ",scrollBars=1";
		// else opts = opts + ",scrollBars=0";
		
		// add scrollbars to width and height if they appear
		wW = wW+sbW;
		wH = wH+sbH;
		wleft = ((screen.width-wW)/2)-15;
		wtop = ((screen.height-wH)/2)-15;
		opts = "width="+wW+",height="+wH+",left="+wleft+",top="+wtop+opts;
	}

	var win = window.open(url,"_blank",opts); // use this if you want a special window look
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
	
}	


/* //////////////////////////// edit frame functions //////////////////////////////
                                                                                   
The editFrame consists of:                                                         
                                                                                   
DIV "editFrame" with an iFrame "editIFrame" inside.                                
Source of the iFrame is arEditIFrame.html by default.                              
Edit-Modules replace that default source of the iFrame.                            
They have a DIV "boxEditFrame" as the main container.                              
                                                                                   
To open the editFrame call initEditFrame() with the url of the editModule.         
Put showEditFrame() in the body.onLoad of the editFrame to scale the iFrame to     
the correct size.                                                                  
To hide the editFrame call parent.hideEditFrame() from the edit modules.           
To keep the eF where it is vertically (e.g. for feedback) set editFrameNoCenter=1  
To set the x position of editFrame define a var editFrXPosition in the php document
                                                                                   
onunload in body tag: Why is it necessary?                                         
IE Mac refuses to load new content into an already opened editFrame when there is  
no "onunload" in the body of the parent document. negg discovered this on          
09.06.2005 20:37 but has no idea why this is so. See /_templates/js for a          
testcase of this behaviour. Anyway: put this in every cms or w3 script that can    
open editFrames. Or better: simply put it in EVERY script!                         
                                                                                   
//////////////////////////////////////////////////////////////////////////////// */

// trigger to show the editFrame //////////////////////////

// note: this function was obviously invented by akim for /arPfizerMB/ar.w3/editFrMediabaseDownload.ar
// but: this is not the right name and place for this functionality. please consult negg to clear this.
// negg 29.09.06
function initEditFrameParent(hrf) {
	parent.frames.editIFrame.location.href = hrf;
	return true;
}

function initEditFrame(hrf) {
	frames.editIFrame.location.href = hrf;
	return true;
}

// Show the editFrame /////////////////////////////////////
// Info: this function is only used by edit modules from body.onLoad()
function showEditFrame() {
	var p = parent.window.document;
	var eF = p.getElementById('editFrame');

	// get window dimensions
	if (is_ie && !MAC) {
		winH = p.documentElement.clientHeight || p.body.clientHeight;
		scrY = p.documentElement.scrollTop || p.body.scrollTop;
	} else if (is_ie && MAC) {
		 winH = p.body.clientHeight;
		 scrY = p.body.scrollTop;
	} else {
		p = parent.window;
		winH = p.innerHeight;
		scrY = p.pageYOffset;
	}
	
	// IE Mac seems he can't get the editFrame dimensions fast enough: 
	// so we wait a little before scaling the window
	if (editFrStatus != 'hidden') {
		if (is_ie && MAC) setTimeout("scaleEditFrame()",300);
		else scaleEditFrame();
	}
}


// Do the scaling and positioning of the editFrame and editIFrame.
// This function is only called by showEditFrame after a short timeout
function scaleEditFrame() {
	var p = parent.window.document;
	var eF = p.getElementById('editFrame');
	var eIF = p.getElementById('editIFrame');

	eF.style.visibility = "visible";

	// measure boxEditFrame inside the editFrame to get the desired height 
	eFX = document.getElementById('boxEditFrame').offsetWidth;
	eFY = document.getElementById('boxEditFrame').offsetHeight;
	
	// sometimes the editFrame is too small because the content 
	// is not rendered completely (IE Mac)                      
	if (eFX < 100) eFX = 732;
	if (eFY < 50) eFY = 50;

	// calculate the position 
	if (!editFrXPosition) x = 117;
	else x = editFrXPosition;
	if (!editFrYPosition)
	{ y = scrY + (winH / 2) - (eFY / 2);
		if (y < scrY) y = scrY + 10;
	}
	else {
		y = editFrYPosition;
	}

	// only center the editFrame if its not a feedback
	if(!editFrameNoCenter) {	
	 	eF.style.left = x + "px";
		eF.style.top  = Math.max(0,y) + "px";
	}
	
	eF.style.width = eFX + "px";
	eF.style.height = eFY + "px";

	eIF.setAttribute('width',eFX);
	eIF.setAttribute('height',eFY);
	// alert("scaleEditFrame to "+x + "px "+Math.max(0,y) + "px height: "+eFX + "px "+ eFY + "px");

	return true;
}


// Hide the editFrame ///////////////////////////////////////////
// Call this as parent.hideEditFrame() from the edit module.
function hideEditFrame() {
	var eF = document.getElementById('editFrame');
	eF.style.visibility = "hidden";
	eF.style.left = "-1000px";
}

// Reload main window ///////////////////////////////////////////
// After feedback for the user reload the main window.           
// Call as parent.reloadWindow() from the feedback module        
// Parameters:                                                   
// string aUrl if not null the given is relocated. If aUrl starts
//   with a # it is interpreted as an anchor and simply added to 
//   the current url.                                            
function reloadWindow(aUrl) {
	// if a url is provided and its not just a named anchor use it!
	if (aUrl && aUrl.charAt(0) != '#') this.location.replace(aUrl);
	// no url or just a named anchor are given
	else {
		var loc = String(this.location);
		// if a named anchor is provided strip the old one
		if (aUrl && aUrl.charAt(0) == "#") {
			loc = loc.split("#");
			loc = loc[0];
		}
 /*
	 //
	 // deactivated by math. doesn't seam to be necessary anymore.
	 //
	 // this serves as a Safari editFrame reload protection: adding a ? or & to the url every time we reload the window
	 if (loc.indexOf("?") == -1) loc = loc+"?1=1";
	 else loc = loc+"&1=1";
	 // adding a named anchor to the url 
	 if (aUrl && aUrl.charAt(0) == "#") loc = loc+' '+aUrl;
*/
		this.location.replace(loc);
	}
}

function setEditFrStatus (status) {
	editFrStatus = status;
}

//                                   
// acFormDate                        
//                                   
function onDateChange(trigger,fdKey, hiddenFdName, flag) {

	if(!initOk) return false;

	var day,month,year,dateReset;
	var daysOfMonth = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31); // first array entry is for '--' in month list
	
	var hiddenFd = document.getElementById(hiddenFdName);
	var dayFd    = document.getElementById(fdKey+'_d');
	var monthFd  = document.getElementById(fdKey+'_m');
	var yearFd   = document.getElementById(fdKey+'_y');

	// read date fields
	day = parseInt(dayFd[dayFd.selectedIndex].value);
	month = parseInt(monthFd.options[monthFd.selectedIndex].value);
	year = parseInt(yearFd.options[yearFd.selectedIndex].value);

	// leap-year? then month[2] (Feb) has 29 days
	if ((year % 4)==0) daysOfMonth[2]=29;

	// initializtion
	if (flag=="init") {
		monthNames = eval("arr_"+fdKey+"_m");
		monthFd.options.length=0;
		monthFd.options[0] = new Option('--',0);
		for (i=1;i<=12;i++) monthFd.options[i]  = new Option(monthNames[i],i);
		monthFd.options[month].selected=true;
	}

	// check if the trigger field was set to '--'
	if (trigger && trigger.selectedIndex == 0) {
		day = 0;
		monthFd.options[0].selected=true;
		yearFd.options[0].selected=true;
		dateReset = 1;
	// if the fields were not reset check if one of them is still --
	} else if (trigger) {
		var now = new Date();
		if (!year) yearFd.options[11].selected=true; // 11 is the current year...it does not work to set this to 2004, because the index is always 0,1,2,3...
		if (!month) monthFd.options[now.getMonth()+1].selected=true;
		if (day <1) day = now.getDate();
	}

	// set days according to month	
	dayFd.options.length = 0;
	dayFd.options[0]  = new Option('--',0);
	for (i=1;i<=31;i++) dayFd.options[i]  = new Option(i,i);
	dayFd.options.length = daysOfMonth[month]+1;
	if (day > daysOfMonth[month]) day = daysOfMonth[month];
	dayFd.options[day].selected=true;

	
	// write date to hidden field
	if (dateReset || yearFd.value == "0000") hiddenFd.value = "";
	else hiddenFd.value = yearFd.value+'-'+monthFd.value+'-'+dayFd.value;
} 

// simulating the spam safe function, just in case it is
// used in w3 (admins would not see their entered emails
// if we don't have this here, too                      
function ssafe(ssafe0,ssafe1,ssafe2) {
	document.write('<a href=\"mailto:'+ssafe0+'@'+ssafe1+'.'+ssafe2+'\">');
	document.write(ssafe0+'@'+ssafe1+'.'+ssafe2+'</a>');
}	
