// IFRAME ONLOAD RESIZING TO ADJUST HEIGHT
function getElement(aID) {
  return (document.getElementById) ?
  $(aID) : document.all[aID];
}
function getIFrameDocument(aID){
  var rv = null; 
  var frame=getElement(aID);
  // if contentDocument exists, W3C compliant (e.g. Mozilla) 
  if (frame.contentDocument)
    rv = frame.contentDocument;
  else // bad IE  ;)
    rv = document.frames[aID].document;
  return rv;
}
function adjustiframe_containerHeight(elemId) {
  var frame = getElement(elemId);
  var frameDoc = getIFrameDocument(elemId);
  frame.height = (frameDoc.body.offsetHeight + 200); // pad by 200 extra pixels
}
// END IFRAME ONLOAD RESIZING

  
function myserialize(element, options) {
  var str = [];
  var key = encodeURIComponent(options.key || element.id);
  var elements = $A(element.getElements(options.tag || ''));
  elements.each(function(el, i) {
    str.push(key+'['+i+']='+el.id);
  });
  return str.join('&');
}

function RS_10_OUT() {
  $('RS_10_DIGIT').setStyle('display','none');
}
function RS_10_OFF() {
  $('RS_10_LED').setStyle('display','none');
  $('RS_10_DIGIT').setStyles({'display':'','background':'url(\'images/rs10_digits/000.gif\') no-repeat top right'});
}
function RS_10_ON(lf,tp,dgt) {
  $('RS_10_LED').setStyles({'display':'', 'top':tp+'px', 'left':lf+'px'});
  $('RS_10_DIGIT').setStyles({'display':'','background':'url(\'images/rs10_digits/'+dgt+'.gif\') top right no-repeat '});
}

form_box_height = 0;

function set_form_height(id) {
  if ($(id)) {
    gc = $(id).getCoordinates();
    ih = gc['height'];
    if (id == 'catform') {
      ih = (ih + 50);
    }
    form_box_height = ih;
    $(id).setStyles({'height':'0'});
  }
}

function open_form_box(id) {
  if ($(id)) {
    $(id).effect('height',{ duration: 300, transition: Fx.Transitions.quadInOut }).start(0,form_box_height);
    $('addbtn').effect('height',{ duration: 300, transition: Fx.Transitions.quadInOut }).start(28,0);
  }
}

function close_form_box(id,rt) {
  if ($(id)) {
    $(id).effect('height',{ duration: 300, transition: Fx.Transitions.quadInOut }).start(form_box_height,0);
    $('addbtn').effect('height',{ duration: 300, transition: Fx.Transitions.quadInOut }).start(0,28);
    if (rt != "") {
      setTimeout("window.location='?"+rt+"'",300);
    }
  }
}

function chkArtistForm() {
  ff = eval("document.form1");
  if (ff.name.value == "") {
    alert("Please enter an artist name.");
    ff.name.focus();
    return false;
  }
  else {
    ff.subtn.value = "saving...";
    ff.subtn.disabled = true;
    return true;
  }
}

function shrink_featured(fpl) {
  if ($(fpl)) {
    $(fpl).setStyles({'overflow':'hidden'});
    gc = $(fpl).getCoordinates();
    $(fpl).effect('opacity',{ duration: 300, transition: Fx.Transitions.quadInOut }).start(1,0);
    $(fpl).effect('height',{ duration: 300, transition: Fx.Transitions.quadInOut }).start(gc['height'],0);
    spl = "$('"+fpl+"').setStyles({'height':'"+gc['height']+"px', 'opacity':'1', 'display':'none'});"
    setTimeout("eval(spl)",300);
  }
}

function chkImgForm() {
  ff = eval("document.imgform");
  if (ff.myfile.value == "") {
    alert("Please select an image to upload.");
    ff.myfile.focus();
    return false;
  }
  else {
    ff.subtn.value = "wait...";
    ff.subtn.disabled = true;
  }
}

// PRODUCTS FUNCTIONS
function chkCatForm() {
  ff = eval("document.catform");
  if (ff.name.value == "") {
    alert("Please enter a category name.");
    ff.name.focus();
    return false;
  }
  else {
    ff.catbtn.value = "saving...";
    ff.catbtn.disabled = true;
    return true;
  }
}

function chkProdForm() {
  ff = eval("document.prodform");
  if (ff.name.value == "") {
    alert("Please enter a product name.");
    ff.name.focus();
    return false;
  }
  else {
    ff.prodbtn.value = "saving...";
    ff.prodbtn.disabled = true;
    return true;
  }
}

var mvr;
  
function alert_default_image_change() {
  alert("Your default image for this product has been updated. Refresh page to see change.");
}
  
function save_cat_order(ri,trg,tbl) {
  ri.remove();
  idstr = myserialize(trg, {key: 'sid', tag: 'li'});
  new Ajax('ajax/save_order', { postBody:'&tbl='+tbl+'&'+idstr, update:'ajax_save' } ).request();
}

function editcat(id) {
  window.location = "?action=editcat&cat_id="+id;
}
  
function deleteCat(id) {
  if ($(id+'_tp').value != "0") {
    alert("This category has "+$(id+'_tp').value+" products assigned to it. Please re-assign these products to other categories before deleting.");
  }
  else {
    kk = confirm("Are you sure you want to delete this category?");
    if (kk == true) {
      window.location = "?action=deletecat&cat_id="+id;
    }
  }
}
  
function editProd(id) {
  window.location = "?view=product&prod_id="+id;
}
  
function deleteProd(id) {
  kk = confirm("Are you sure you want to delete this product?");
  if (kk == true) {
    window.location = "?action=deleteprod&cat_id="+$('cat_id').value+"&prod_id="+id;
  }
}
  
function featProdToggle(id) {
  fpl = 'feat_'+id;
  if ($('feature'+id).checked) {
    // buggy, doesn't come back up sometimes
    // if ($(fpl)) { $(fpl).setStyle('display',''); }
    ff = 1;
  }
  else {
    shrink_featured(fpl);
    ff = 0;
  }
  new Ajax('ajax/save_featured', { postBody:'&tbl=products&id='+id+'&featured='+ff, update:'ajax_save' } ).request();
}

function spotlightProdToggle(id) {
  if ($('spotlight_product'+id).checked) {
    ff = 1;
  }
  else {
    ff = 0;
  }
  new Ajax('ajax/save_spotlight', { postBody:'&tbl=products&id='+id+'&spotlight='+ff, update:'ajax_save' } ).request();
}

function spotlightArtistToggle(id) {
  if ($('spotlight_artist'+id).checked) {
    ff = 1;
  }
  else {
    ff = 0;
  }
  new Ajax('ajax/save_spotlight', { postBody:'&tbl=artists&id='+id+'&spotlight='+ff, update:'ajax_save' } ).request();
}

function unfeatureProduct(id) {
  new Ajax('ajax/save_featured', { postBody:'&tbl=products&id='+id+'&featured=0', update:'ajax_save' } ).request();
  shrink_featured(id);
}

function unfeatureSpotlight(id) {
  new Ajax('ajax/remove_spotlight', { postBody:'&id='+id, update:'spotlight_ajax' } ).request();
  shrink_featured(id);
}

function pop_lightbox(isrc,tit) {
  Lightbox.show(isrc, tit);
}

function deleteImage(id,prod_id) {
  kk = confirm("Are you sure you want to delete this image?");
  if (kk == true) {
    window.location = "?view=product&prod_id="+prod_id+"&action=deleteimage&img_id="+id;
  }
}
// END PRODUCTS FUNCTIONS

  function chkContactForm() {
    if (document.cform.name.value == "") {
      alert("Please enter your name");
      document.cform.name.focus();
      return false;
    }
    else if (document.cform.email.value.indexOf('@') <= 0) {
      alert("Please enter a valid email address");
      document.cform.email.focus();
      document.cform.email.select();
      return false;
    }
    else if (document.cform.subject.value == "") {
      alert("Please enter a subject");
      document.cform.subject.focus();
      return false;
    }
    else if (document.cform.message.value == "") {
      alert("Please enter a message");
      document.cform.message.focus();
      return false;
    }
    else if (document.cform.recaptcha_response_field.value == "") {
      alert("Please enter the words you see.");
      document.cform.recaptcha_response_field.focus();
      return false;
    }
    else {
      document.cform.submit.value = "please wait...";
      document.cform.submit.disabled = true;
      return true;
    }
  }
