/* open a popup window */
function open_window(url,width,height,name,status,toolbar,resize){
   // set defaults if none passed
   if(name === ''){name ='new_win';}
   if(status === ''){status ='no';}
   if(toolbar === ''){toolbar ='no';}
   if(resize === ''){resize ='yes';}
   // check for correct values (could have a tiny screen)
   var padding = 50; // in pixels
   if(width>screen.width){width = screen.width-padding;}
   if(height>screen.height){height = screen.height-padding;}
   // get the position of the window (always centred)
   var x = screen.width/2-width/2;
   var y = screen.height/2-height/2;
   var settings="toolbar="+toolbar+",location=no,directories=no,"+"status="+status+",menubar=no,scrollbars=yes,"+"resizable="+resize+",width="+width+",height="+height+',screenX='+x+',screenY='+y/* +',top='+x+',left='+x */;
//    alert(settings);
   var window_handle = window.open(""+url,name,settings);
   window_handle.focus();
   if (window_handle.opener === null) {window_handle.opener = self;}
   return false;
}

function toggleInputText(field_obj,initial_text){
        // reset the text
        if(field_obj.value.replace(/\s/g,'')==''){field_obj.value=initial_text;}
        // clear the text
	else if(field_obj.value == initial_text){field_obj.value = '';}
}

// set the classes for objects
function set_class(objectID,new_class){
  if (document.getElementById) {
    if(document.getElementById(objectID)){
      document.getElementById(objectID).className = new_class;
    }
  }
  else if (document.layers && document.layers[object]) {
    document.layers[objectID][className] = new_class;
  }
  else if (document.all) {
    document.all[objectID].className = new_class;
  }
  ;
}

// set the styles for objects
function set_style(objectID,attribute,new_style){
  // not sure how this affects items
//   if(objectID.replace('[','\[')){objectID.replace(']','\]');}
//   alert('setting style "'+attribute+":"+new_style+'" for "'+objectID+'"');
  if (document.getElementById) {
    if(document.getElementById(objectID)){
      document.getElementById(objectID).style[attribute] = new_style;
//       alert(document.getElementById(objectID).style[attribute]);
    }
//     else{alert('The object \''+objectID+'\' cannot be found');;}
  }
  else if (document.layers && document.layers[object]) {
    document.layers[objectID][attribute] = new_style;
  }
  else if (document.all) {
    document.all[objectID].style[attribute] = new_style;
  }
  ;
}

// script to show areas
function show(vars){
  if(isArray(vars)){
    // process it, and hid the vars
    for (var i=0; i<(vars.length); i++) {
      if(vars[i].length>0){
        set_style(vars[i],'visibility','');
        set_style(vars[i],'display','');
      }
    }
    return true;
  }
  else if(vars.length){
//     alert('vars is a string: '+vars)
    if(vars.length>0){ // a string
     set_style(vars,'visibility','');
     set_style(vars,'display','');
     return true;
    }else{return false;}  // nothing to show
  }
  else{return false;}
}

function show_hide(element,show_or_not){
	if (typeof show_or_not == "undefined") {
	  show_or_not = "0";
	}
	// check we've not got an array
	if(!isArray(element)){
	  if (document.getElementById(element)) {
		var obj = document.getElementById(element);
		if(obj.style){
		  if((obj.style.visibility == "visible") || (obj.style.visibility == "")){hide(element);}
		  else{
		    show(element);
		  }
	  	}
	  }
	}
	else{
	  for (var i=0; i<(element.length); i++) {
	    if(show_or_not == 'show'){
	      // could well be an array
              show(element);
	    }
	    // could well be an array
	    else{hide(element);}
	  }
	}
  ;
}

/*  script to hide areas */
function hide(vars){
  if(isArray(vars)){ // an array of stuff to hide
    /*  process it, and hid the vars */
    for (var i=0; i<(vars.length); i++) {
      if(vars[i].length>0){
        set_style(vars[i],'visibility','hidden');
        set_style(vars[i],'display','none');
      }
    }
    return true;
  }
  else if(vars.length){
    if(vars.length>0){ // a string
      set_style(vars,'visibility','hidden');
      set_style(vars,'display','none');
      return true;
    }else{return false;}  // nothing to show
  }
  else{return false;}
}

/* checks if the passed variable is an array ... such a function does not exist in Javascript */
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function show_hide(element,show_or_not){
	if (typeof show_or_not == "undefined") {
	  show_or_not = "0";
	}
	// check we've not got an array
	if(!isArray(element)){
	  if (document.getElementById(element)) {
		var obj = document.getElementById(element);
	  	if(obj.style){
		  if((obj.style.visibility == "visible") || (obj.style.visibility == "")){hide(element);}
	  	  else{
		    show(element);

    		  }
	  	}
	  }
	}
	else{
	  for (var i=0; i<(element.length); i++) {
	    if(show_or_not == 'show'){
	      // could well be an array
              show(element);
      	    }
	    // could well be an array
	    else{hide(element);}
	  }
	}
  ;
}
