var recent_images_expanded;

/* 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 += '&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;}
}


/* 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('window.open(""+'+url+','+name+','+settings+')');
   var window_handle = window.open(""+url,name,settings);
   window_handle.focus();
   if (window_handle.opener === null) {window_handle.opener = self;}
   return false;
}

// check all checkboxes
function check_all(theForm,check,attr,style,attr2,style2){
  var i,n,a;
//   alert(theForm.elements.length);
  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;}
      if(attr.length>1 && (a.checked === true)){set_style(a.name,attr,style);}
      if(attr2.length>1 && (a.checked === false)){set_style(a.name,attr2,style2);}
    }
  }
}

// if check === true, then we style it.  if not, we remove the specified style
function style_all(theForm,check,attribute,style,prefix){
  var i,n;
  for (i=0,n=theForm.elements.length;i<n;i++){
    if(theForm.elements[i].checked == check){
//       alert('setting the style \''+attribute+':'+style+'\' for \''+theForm.elements[i].name+'\' :: checked?'+theForm.elements[i].checked);
      // only for elements with names that can be sent via a form
      if(theForm.elements[i].name.length>2){set_style(theForm.elements[i].name,attribute,style);}
    }
  }
}

// from the mailing list, add users into the send option
function ml_add_user(insert_to,second_id){
  var i,n,r,c;r = '<span id=\"ml_user_list\">';c=0;
  var i_1 = '<input type=hidden name="';var i_2 = '" value="'; var i_3 = "\">\n";
  for (i=0,n=document.add.elements.length;i<n;i++){
    if((document.add.elements[i].checked === true) && (document.add.elements[i].type == 'checkbox') && (document.add.elements[i].name.length>2)){
      r += i_1+document.add.elements[i].name+i_2+document.add.elements[i].value+i_3;c++;
    }
  }

  r += "<b>"+c+" user";if(c>1){r+='s';}r+=" total</b>";
  r += " <span id=\""+second_id+"\">"+window.opener.get_html('add_user_link')+"</span>";
//   alert(r);
//   alert('trying to send to "'+insert_to+'"');
  window.opener.replace_html(insert_to,r);
  self.close();
}

// can insert to anywhere in a document (not just the RTE)
function replace_html(objectID,html){
//   alert('replacing for id="'+objectID+'"');
  if (document.getElementById) {
    document.getElementById(objectID).innerHTML = html;
  }
//   else if (document.layers && document.layers[object]) {
//     document.layers[objectID][attribute] = new_style;
//   }
  else if (document.all) {
    document.all[objectID].innerHTML = html;
  }

}
// can retrieve HTML from anywhere in a document (esp. from remote window!)
function get_html(objectID){
//   alert('retrieving for id="'+objectID+'"');
  if (document.getElementById) {
//     alert(document.getElementById(objectID).outerHTML);
    return(document.getElementById(objectID).innerHTML);
  }
//   else if (document.layers && document.layers[object]) {
//     document.layers[objectID][attribute] = new_style;
//   }
  else if (document.all) {
    return(document.all[objectID].innerHTML);
  }
  else{return false;}

}

function ml_add_all_users(form_name,element_replace,element_keep){
  if(document.getElementById){
    var o = document.getElementById(element_replace);
    var tmp = document.getElementById(element_keep).innerHTML;
    o.innerHTML = '<input type=hidden name="user[]" value="all"><b>all users added</b><span id="'+element_keep+'">'+tmp+'</span>';
  }
}

function ml_remove_users(form_name,element_replace,element_keep){
  if(document.getElementById){
    var o = document.getElementById(element_replace);
    var tmp = document.getElementById(element_keep).innerHTML;
    o.innerHTML = '<b>0 users added</b><span id="'+element_keep+'">'+tmp+'</span>';
  }
}

// a generic select an image function.  Expects to be given an id into which it can place all image information, including the hidden field for the image
function select_image_from_db(url,id_to_work_with,hidden_field_name){
	url+="&id_replace="+id_to_work_with+"&field="+hidden_field_name;
        open_window(url,800,600,id_to_work_with);
}

// for use especially with the menus and selecting images
function return_image_from_db(document_element_id,hidden_field_name,image_id){
  if(document.getElementById){
    var o = document.getElementById(document_element_id);
    o.innerHTML = "<input type=hidden name=\""+hidden_field_name+"\" value=\""+image_id+"\"><img src=\"images.php?id="+image_id+"&tn=1\">";
  }
}

// generic function to zap anything's innerHTML based on an object ID
function clear_element_value(element_id){
  if(document.getElementById){
    var o = document.getElementById(element_id);
    o.innerHTML = '';
  }
}