/* 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 checkbox(field,f_value){
  var i;
  if(field.length){
    for (i = 0; i < field.length; i++){
      if(field[i].value == f_value){
        if(field[i].checked == true){field[i].checked = false;}
        else if(field[i].checked == false){field[i].checked = true;}
      }
    }
  }
  else{
    if(field.checked == true){field.checked = false;}
    else if(field.checked == false){field.checked = true;}
  }
}

function confirm_deletion (what_delete,url) {
     var response = confirm('Are you sure you want to Delete '+what_delete+'?');
     // OR var response = window.confirm('Confirm Test: Continue?');
     if (response) {
       url = url+"&confirm=1"
       location.href=url;
       return false;
     }
     else {return false;}
}

// confirm an action
function confirm_action (what_item,what_action,url) {
  var response = confirm('Are you sure you want to '+what_action+' '+what_item+'?');
  // OR var response = window.confirm('Confirm Test: Continue?');
  if (response) {
    url += '&confirm=1';
    location.href=url;
    return false;
  }
  else {return false;}
}

function toggleInputText(field_obj,initial_text,initial_class,highlighted_class){
        // reset the text
        if(field_obj.value.replace(/\s/g,'')==''){
          field_obj.value=initial_text;
          // note this will clear any classes set
          set_class(field_obj,initial_class);
        }
        // clear the text
	else if(field_obj.value == initial_text){
          field_obj.value = '';
          set_class(field_obj,highlighted_class);
        }
}

function setInnerHTML(id,text){
  var o;
  if(document.all){
    o = document.all[id];
  }
  else if(document.getElementById){
    o = document.getElementById(id);
  }
  if(o){o.innerHTML = text;}
}

function check_all(theForm,check){
  var i,n,a;
  for (i=0,n=theForm.elements.length;i<n;i++){
    a = theForm.elements[i];
    if((a.name.length>2) && (a.type == 'checkbox')){ // now worth checking
      if(a.checked !== check){a.checked = check;}
    }
  }
  return false;
}

/* 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;
}

// swaps an image source based on information given
function swap_image(img,id){
  if(document.getElementById){
    var o;
    if(o = document.getElementById(id)){
      o.src = img;
    }
  }
}

// set the classes for objects
function set_class(objectID,new_class){
  if (document.getElementById) {
//     alert("current classname (if already an object):"+objectID.className);
    // perhaps already an object passed?
    if(objectID.style){
      objectID.className = new_class;
//       alert(objectID.className);
    }
    else if(document.getElementById(objectID)){
      document.getElementById(objectID).className = new_class;
    }
  }
  else if (document.layers){
    if(document.layers[object]) {document.layers[objectID][className] = new_class;}
    // perhaps already an object passed?
    else if(objectID[style]){objectID[className] = new_class;}

  }
  else if (document.all) {
    if(document.all[objectID]){document.all[objectID].className = new_class;}
    else if(objectID.style){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 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){
    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;}
}

// lifted directly from http://lists.evolt.org/archive/Week-of-Mon-20070115/187466.html
// all credit where due to ben morrison
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="
+ opacity + ");";

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}