/**
 * Shows and hides the direct children of a named element:
 * -parent_element_id: id of the parent element
 * -class_name: limit to elements having this calss (or '*' for all)
 * -visible: true/false
 * -collapse: also collapse element when not visible
 */
function csa_hideshow(parent_element_id, class_name, visible, collapse)
{
	var elements = document.getElementById(parent_element_id).childNodes;

  for (i=0; i<elements.length; i++)
  {
		if(elements[i].className==class_name || class_name=='*')
		{
    	if(visible)
    	{
    		elements[i].style.visibility='visible';
				if(collapse)
	    		elements[i].style.display="block";
    	}
    	else
    	{
    		elements[i].style.visibility='hidden';
				if(collapse)
	    		elements[i].style.display="none";
    	}
    }
  }
}

/*
 * Shows and hides all children of a named element:
 * -parent_element_id: id of the parent element
 * -class_name: limit to elements having this calss (or '*' for all)
 * -visible: true/false
 * -collapse: also collapse element when not visible
 *
 * -- This does not work in IE --
 */
function csa_hideshow_all(parent_element_id, class_name, visible, collapse)
{
	if(parent_element_id==null)
		var elements = document.getElementsByClassName(class_name);
	else
		var elements = document.getElementById(parent_element_id).getElementsByClassName(class_name);

  for (i=0; i<elements.length; i++)
  {
		if(visible)
		{
			elements[i].style.visibility='visible';
			if(collapse)
				elements[i].style.display="table-row";
		}
		else
		{
			elements[i].style.visibility='hidden';
			if(collapse)
				elements[i].style.display="none";
		}
	}
}


//
// // open_popup: Opens a popup window
//
function open_popup(url, title)
{
	window.open(url,title,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
}

function open_popup2(url, title, x, y, h, w)
{
	// If x is negative we'll allow scrollbars 
	if(x<0)
	{
		x = -x;
		var s = "yes";
		title = title + "x";
	}
	else
	{
		var s = "no";
	}

	// If y is negative we'll close the previous window
	if(y<0)
	{
		y = -y;
		parent.close();
	}
	
	var NewWindow = window.open(url,title,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + s + ',resizable=no,width='+ w +',height='+h);
	NewWindow.resizeTo(w,h);
	NewWindow.self.focus();
}

function open_popup3(url, x, y, h, w) {
popupWin=window.open(url,'remote','width='+w+',height='+h+',top='+x+',left='+y)
}

function close_popup()  
{
  parent.close();
}

// close_popup_go: Closes the current popup and navigates the main window 
// to the URL passed on in the parameter.
function close_popup_go(x)  
{
  top.opener.location.href = x;
  parent.close(); 
}

// keep_popup_go: Keeps the current popup and navigates the main window 
// to the URL passed on in the parameter.
function keep_popup_go(x)  
{
  top.opener.location.href = x;
}

// close_popup_reload: Closes the current popup and reloads the parent window 
// to the URL passed on in the parameter.
function close_popup_reload()
{
//  top.opener.location.href = top.opener.location.href;
  top.opener.location.reload();
  parent.close(); 
}

// used for onclick links
function GoThere2(target) 
{ 
	top.window.location.href=target; 
}

// used for onclick links
function GoThere3(target) 
{ 
	var NewWindow = window.open(target,'','');
}

function csa_toggle_div(element_id, collapse)
{
	element = document.getElementById(element_id)

	if(element!=null)
	{
		if(element.style.visibility=='visible')
		{
			element.style.visibility = 'hidden';
			if(collapse)
	    		element.style.display='none';
		}
		else
		{
			element.style.visibility = 'visible';
			if(collapse)
	    		element.style.display='block';
		}
	}
}

function csa_show_div(element)
{
	if(document.getElementById(element)!=null)
		document.getElementById(element).style.visibility = 'visible';
}

function csa_hide_div(element)
{
	if(document.getElementById(element)!=null)
		document.getElementById(element).style.visibility = 'hidden';
}

function csa_get_form_value(form_name, element_name)
{
	var element_value = '';

	element_object = document.forms[form_name].elements[element_name]
	
	if(element_object===undefined)
	{
		element_value = null;
	}
	else if(element_object.value===undefined)
	{
		for (var i=0; i < element_object.length; i++)
	  {
			if (element_object[i].checked)
			{
			 	element_value = element_object[i].value;
			}
	 	}
	}
	else if(element_object.type=='checkbox')
	{
		element_value = element_object.checked;
	}
	else
	{
		element_value = element_object.value;
	}

	return(element_value);
}
/* 
 * Cross-browser event handling
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 */

addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}
