function newWindow(mypage,myname,w,h,features) {
  if(screen.width){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  }else{winl = 0;wint =0;}
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}

function popup(mypage,myname,features) {
  if(screen.width){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  }else{winl = 0;wint =0;}
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}

function modal_popup(url){
	newWindow(url,'modal',"toolbar=no,menubar=no,personalbar=no,width=10,height=10,scrollbars=no,resizable=yes,modal=yes,dependable=yes");
	return(false);
}

function submitenter(myfield,e){

    var keycode;
	var onSubmit=false;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

	onSubmit = (submitenter.arguments.length > 2)?submitenter.arguments[2]:'showList()';


    if (keycode == 13){
		if (!myfield.form.onsubmit){
			eval(onSubmit);
		}else{
			myfield.form.submit();
		}
	   
	   return false;       
       }
    else
       return true;
}

function showLoading(){	
	document.body.style.cursor = 'wait';
	document.getElementById('loading').style.top=document.body.scrollTop + 10;
	document.getElementById('loading').style.display = 'block';
};

function hiddenLoading(){
	document.body.style.cursor = '';
	document.getElementById('loading').style.display = 'none';
};

// calendars for ajax
var activeCalendarElement= null;

function closeCalendar(cal){
	if(cal.dateClicked)
		activeCalendarElement.value=cal.date.print("%d/%m/%Y");

	cal.hide();
}

function showCalendar(element){
	var calendar =  new Calendar(0, null, null, closeCalendar);
	activeCalendarElement = element;
	calendar.create(null);
	if (element.value)
		calendar.parseDate(activeCalendarElement.value,"%d/%m/%Y");

	calendar.showAtElement(activeCalendarElement);
}

function updateEditors(){
	
	if (typeof xinha_editors != "undefined"){
		for(var i in xinha_editors){
			xinha_editors[i]._textArea.value = xinha_editors[i].outwardHtml(xinha_editors[i].getHTML());
		}
	}
	
}

function formReset(id){
	document.getElementById(id).reset();
}

function formSubmit(id){
	document.getElementById(id).submit();
}

function previewImg(id,resize,source){
	wbsPopup('/core/proc.popupImage.php?id='+id+'&resize='+resize+'&source='+source);
}

/* WBSPOPUP */

wbsPopup._return = null;

// constant, the currently opened wbsPopup
wbsPopup._modal = null;

// the wbsPopup will read it's args from this variable
wbsPopup._arguments = null;

wbsPopup.agt = navigator.userAgent.toLowerCase();

wbsPopup.is_ie	   = ((wbsPopup.agt.indexOf("msie") != -1) && (wbsPopup.agt.indexOf("opera") == -1));

// ------------------------------------------------------------------

// Though "wbsPopup" looks like an object, it isn't really an object.  Instead
// it's just namespace for protecting global symbols.

function wbsPopup(url, action, init) 
  {
	if (typeof init == "undefined") 
	  {
		init = window;	// pass this window object by default
	  }

	// optional editor instance parameter that can be propagated through to 
	// wbsPopups.

	if (typeof wbsPopup.arguments[3] != "undefined") 
	  {
		wbsPopup._editor = wbsPopup.arguments[3];
	  }

	wbsPopup._geckoOpenModal(url, action, init);

  }; // end of wbsPopup()

// ----------------------------------------------------------

/**
* wbsPopup._parentEvent()
*/

wbsPopup._parentEvent = function(ev) 
  {
	setTimeout( 
	  function() 
	    { 
			if (wbsPopup._modal && !wbsPopup._modal.closed) 
			  { 
				wbsPopup._modal.focus() 
				} 
			}, 50); // end of setTimeout() call.
		
	if (wbsPopup._modal && !wbsPopup._modal.closed) 
	  {
		wbsPopup._stopEvent(ev);
	  }

  }; // end of _parentEvent();

// ----------------------------------------------------------

/**
* wbsPopup._geckoOpenModal()
*/

wbsPopup._geckoOpenModal = function(url, action, init) 
  {
	//var urlLink = "hadialog"+url.toString();

	var myURL = "hadialog"+url;
	var regObj = /\W/g;

	myURL = myURL.replace(regObj,'_');

	var dlg = window.open(url, myURL,
			      "toolbar=no,menubar=no,personalbar=no,width=10,height=10," +
			      "scrollbars=no,resizable=no,titlebar=no,modal=yes,dependable=yes");

	wbsPopup._modal = dlg;
	wbsPopup._arguments = init;

	// capture some window's events
	function capwin(w) 
	  {
		wbsPopup._addEvent(w, "click", wbsPopup._parentEvent);
		wbsPopup._addEvent(w, "mousedown", wbsPopup._parentEvent);
		wbsPopup._addEvent(w, "focus", wbsPopup._parentEvent);
	  };

	// release the captured events
	function relwin(w) 
	  {
		wbsPopup._removeEvent(w, "click", wbsPopup._parentEvent);
		wbsPopup._removeEvent(w, "mousedown", wbsPopup._parentEvent);
		wbsPopup._removeEvent(w, "focus", wbsPopup._parentEvent);
	  };

	capwin(window);

	// capture other frames
	for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));

	// make up a function to be called when the wbsPopup ends, using the function
	// passed to us.

	wbsPopup._return = function (val) 
	  {
		if (val && action) 
		  {
			action(val);
		  }

		relwin(window);
		// capture other frames

		for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));

		wbsPopup._modal = null;

    };
  }; // end of wbsPopup._geckoOpenModal

/**
* event handling
*/

wbsPopup._addEvent = function(el, evname, func) 
  {
	if (wbsPopup.is_ie) 
	  {
		el.attachEvent("on" + evname, func);
	  } 
	else 
	  {
		el.addEventListener(evname, func, true);
    }
  };

/**
* _removeEvent()
*/

wbsPopup._removeEvent = function(el, evname, func) 
  {
	if (wbsPopup.is_ie) 
	  {
		el.detachEvent("on" + evname, func);
	  } 
	else 
	  {
		el.removeEventListener(evname, func, true);
   	}
  };

/**
* _stopEvent()
*/

wbsPopup._stopEvent = function(ev) 
  {
	if (wbsPopup.is_ie) 
	  {
		ev.cancelBubble = true;
		ev.returnValue = false;
	  } 
	else 
	  {
		ev.preventDefault();
		ev.stopPropagation();
	  }
  };

function getAbsolutePos(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};

function showAtElement(el,elAt) {
	var p = getAbsolutePos(elAt);
	showAt(el,p.x+elAt.offsetWidth, p.y);
	return true;
}

function showAt(el,x, y) {
	el.style.left = x + "px";
	el.style.top = y + "px";
	el.style.display = 'block';
};