var DEBUG = null;
var IMG_PATH;
var MODE = ( !document.all && document.getElementById) ? 'NS6' : ( (document.layers) ? '' : ( (document.all) ? 'IE6' : '' ) );
function create_obj( o ) { return document.createElement(o); }
function doc_get( id ) { 
var obj = null;
//if( document.getElementById ) {
//obj = document.getElementById(n);
obj = document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
/*} else {
switch(MODE) {
case 'IE6' : obj = document.all[n]; break;
case 'NS6' : obj = document.getElementById(n);
}
} */
//if( !obj ) { alert( "missing obj: "+ n ); } 
return obj;
}

function doc_onload(callback) {
	
	var bSuccess = false;
			
	if(typeof window.addEventListener != 'undefined')
	{
		//.. gecko, safari, konqueror and standard
		bSuccess = window.addEventListener('load', callback, false);
	}
	else if(typeof document.addEventListener != 'undefined')
	{
		//.. opera 7
		bSuccess = document.addEventListener('load', callback, false);
			
	} 
	else if(typeof window.attachEvent != 'undefined')
	{
		//.. win/ie
		bSuccess = window.attachEvent("onload", callback);
	}
	else
	{
		if(typeof window.onload == 'function')
		{
			//store it
			var existing = onload;
		
			//add new onload handler
			window.onload = function()
			{
				//call existing onload function
				existing();
			
				//call generic onload function
				callback();
			};
		}
		else
		{
			//setup onload function
			window.onload = callback;
		}		
	}
	return bSuccess;
}

function doc_anchor(x)
{
 location=location.href.slice(0,location.href.length-location.hash.length)+'#'+x
}

function obj_attach_event(o, e, callback) {

	if(o.addEventListener){ // Mozilla, Netscape, Firefox
		o.addEventListener(e, callback, false);
		//object.myflag = "test";
		//object.mydata = "123";
	} else { // IE
		
		o.attachEvent(e, callback);
		//o.myflag = "test";
		//o.mydata = "123";
	}
}

function obj_set( o,n,v ) { o.setAttribute(n,v); }
function obj_get( o,n ) { return o.getAttribute(n); }
function obj_append( o,n ) { o.appendChild(n); }
function obj_remove( o,n ) { o.removeChild(n); }
function obj_remove_all( o ) { for (var i=o.childNodes.length; i>1;) o.removeChild(o.childNodes[--i]) }
function create_text( o ) { return document.createTextNode(o); }

function hide_obj(o) {switch(MODE) {
  case 'IE6' : case 'NS6' : o.style.visibility = "hidden";  o.style.zIndex = -10;
}}

function show_obj(o) { if(o.style){switch(MODE) {
  case 'IE6' : case 'NS6' : o.style.visibility = "visible"; o.style.zIndex = 10;
}}}


function event_getX(e) {
 switch(MODE) {
  case 'IE6': return event.clientX + (document.body.scrollLeft?document.body.scrollLeft:(document.documentElement?(document.documentElement.scrollLeft?document.documentElement.scrollLeft:0):0));  
  case 'NS6': return e.pageX;
}}

function event_gety(e) {

switch(MODE) {
  case 'IE6': return event.clientY + (document.body.scrollTop?document.body.scrollTop:(document.documentElement?(document.documentElement.scrollTop?document.documentElement.scrollTop:0):0));   
  case 'NS6': return e.pageY;
}}


// m2(X,l,t,w,h) = move object X to position (l,t) and optionally resize it to w x h
function obj_move( X,l,t,w,h ) {
  if( w ) {
    if( w<0 ) { l+=w;w=-w; }
    if( h<0 ) { t+=h;h=-h; }
  }
  switch(MODE) {
    case 'IE6': X.style.pixelLeft = l; X.style.pixelTop  = t; break;
    case 'NS6': X.style.setProperty( 'left', l+'px', 'important' );
                X.style.setProperty( 'top',  t+'px',  'important' );
  }
  if( w||h ) { obj_resize( X,w,h ); }
  return( 0 )
}

// rs(X,w,h) = resize X to w x h
function obj_resize( X,w,h ) {
switch(MODE) {
  case 'IE6': X.style.pixelHeight = h; X.style.pixelWidth = w; break;
  case 'NS6': X.style.setProperty( 'width',  w+'px',  'important' );
              X.style.setProperty( 'height', h+'px',  'important' );
}}

/**
 * Generic function to modify the HTML content of a div. 
 */
function updateDiv(divId,text,hide){  // div Name, the text to go into the div name and whether it should be hidden (true or false;
	
	if (hide=="true")
		document.getElementById(divId).style.visibility = 'hidden'; 	
	else 
		document.getElementById(divId).style.visibility = 'visible';
		
	document.getElementById(divId).innerHTML = text;	
}

// check all the checkboxes from a given form field
function checkAll(event, field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

// uncheck all the checkboxes from a given form field
function uncheckAll(event, field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

// escape Single quote or apostrophe ('), and Double quote (")
function escapeQuotes(str) {
	str = str.replace(/\'/g,"\\'");
	str = str.replace(/\"/g,'\\"');
	return str;
}

// escape Extra Quotes
function removeEscapedQuotes(str) {
	str = str.replace(/\\\'/g,"\'");
	return str;
}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function ajax_addLoadCursor(contextPath, o) {

  	loaderImg = create_obj('img');
  	obj_set( loaderImg, 'id', 'loaderImg' );
  	loaderImg.style.borderWidth = 0;
  	loaderImg.src = contextPath + "/icons/ajax-loader.gif";
	obj_append(o, loaderImg);
}

function ajax_showLoadCursor(contextPath, panelId) {

	var div = doc_get(panelId);
	if (div) {
		// keep the size of the panel
		
		var w = div.offsetWidth;
		var h = div.offsetHeight;
		div.innerHTML = '';
		div.style.width = "400px";
		//div.style.height = ((h>32) ? h : 32) + "px" ;
		div.style.textalign = "center";
		ajax_addLoadCursor(contextPath, div);
  		show_obj(div);
	}
}

function ajax_displayLoadCursor(contextPath, panelId) {

	ajax_displayStatusIconHTML(contextPath, panelId, "/icons/ajax-loader.gif")	
}

function ajax_displayBugIcon(contextPath, panelId, message) {

	ajax_displayStatusIconHTML(contextPath, panelId, "/icons/bug.png", message);

}

function ajax_displayStatusIcon(contextPath, panelId, imageName) {
	
	var div = doc_get(panelId);
	var ajax_div = doc_get(panelId + "_statusIcon");
	
	if (div) {
		
		// we want to display a load icon relative to the top-left corner of
		// the parent div.
		// keep the size of the panel
		
		// the size of the panel is relative or absolute
		
		var w = div.offsetWidth;
		var h = div.offsetHeight;
		var x = div.offsetLeft;
		var y = div.offsetTop;
		
		//alert(panelId + "[ x=" + x + ";y=" + y + ";w=" + w + ";h=" + h + " ]");
		
		var ajax_div = create_obj('div');
		//position:absolute; margin-left:10px; margin-top:10px; visibility:visible; z-index:2; width:500px; height:32px;

		/* for IE */
  		//ajax_div.style.filter = "alpha(opacity=75)";
  		//ajax_div.filters.alpha.opacity=75;
  		/* CSS3 standard */
  		ajax_div.style.opacity = 0.75;
  		ajax_div.style.border = "1px solid black";
		ajax_div.style.background = "white";
		ajax_div.zIndex = 10;
		
		loaderImg = create_obj('img');
  		obj_set( loaderImg, 'id', 'loaderImg' );
  		loaderImg.style.borderWidth = 0;
  		loaderImg.src = contextPath + imageName;
		obj_append(ajax_div, loaderImg);
		
		var nbNodes = div.childNodes.length;
		var bNotEmpty = nbNodes > 0 || div.innerHTML.length > 0;
		

  		
  		if (bNotEmpty == true) {
  			ajax_div.style.left = x;
  			ajax_div.style.top = y;
  			ajax_div.style.position = "absolute";
  			
  		} else {
  			ajax_div.style.position = "relative";
  		}
		
		ajax_div.style.marginTop = "10px";
		ajax_div.style.marginLeft = "10px";
		ajax_div.style.visibility = "visible";
		
		
		
		obj_append(div, ajax_div);
		
  		
  		alert('showing ajax load icon');
	}
}

function ajax_displayStatusIconHTML(contextPath, panelId, imageName, message) {

	var div = doc_get(panelId);
	
	if (div) {
		
		// we want to display a load icon relative to the top-left corner of
		// the parent div.
		// keep the size of the panel
		
		// the size of the panel is relative or absolute
		
		var x = div.offsetLeft;
		var y = div.offsetTop;
		
		/* CSS3 standard */
		var opacity = "opacity:0.75;filter:alpha(opacity=75);";
  		
  		var nbNodes = div.childNodes.length;
		var bNotEmpty = nbNodes > 0 || div.innerHTML.length > 0;
		
		var leftTop = (bNotEmpty == true) ? "top:" + y + "px;position:absolute;" : 'position:relative;';
			
  			
  		
  		
  		var ajax_div = '<div style="'+leftTop + 'margin-left:10px; margin-top:10px; background-color:white; visibility:visible; z-index:10; border:1px solid black;'+opacity+'"';
  		
  		ajax_div+='<img style="border:0px;" src="'+ contextPath + '/' + imageName + '"/>';
		
  		if (message != null) {
  			ajax_div+='<span style="font-size:smaller;color:white;background-color:Red">' + message + '</span>';
  		}
  		
		ajax_div+='</div>';
		
  		div.innerHTML = ajax_div + div.innerHTML;

	}
}



