function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function confirmUserDelete(){
  return confirm("Are you sure you want to delete this user?");
}

function confirmPublish(published){
  if(published == 1){  
    msg = "Are you sure you want to unpublish this entry?";
  }else{
    msg = "Are you sure you want to publish this entry?";
  }

  return confirm(msg);
}

function publishNews(pid, elem){
  if(elem != null && (elem.value == 'yes' || elem.value == 'no'))
    document.location = "acp/acpnews.php?action=publish&pid="+pid+"&publish="+elem.value;
}

function hasSameValue(id1, id2){
  if(document.getElementById(id1) != null && document.getElementById(id2) != null)
  {
      if(document.getElementById(id1).value == document.getElementById(id2).value)
        return true;
  }

  return false;
}

function confirmProductDelete(){
  return confirm("Are you sure you want to delete this product item?");
}

function confirmProductPhotoDelete(){
  return confirm("Are you sure you want to delete this product's photo?");
}

function confirmProductVideoDelete(){
  return confirm("Are you sure you want to delete this product's video?");
}

function showCustomAttributeName(id){
  selectelem = document.getElementById('editproductattribname[' + id + ']');
  inputelem = document.getElementById('editproductattribcustomname[' + id + ']');

  if(selectelem != null && inputelem != null)
  {
    if(selectelem.value == "custom")
    {
      inputelem.style.display="";
      inputelem.value = "";
    }
    else
    {
      inputelem.style.display="none";
      inputelem.value = "";
    }
  }
}


function hideAttribute(id){
  elem = document.getElementById('editproductattrib[' + id + ']');
  if(elem != null)
  {
    elem.style.display = "none";
    elem.getElementsByTagName('select')[0].value = "";
    elem.getElementsByTagName('input')[0].value = "";
    elem.getElementsByTagName('input')[1].value = "";
  }
}

function addNewAttribute(){
  tableelem = document.getElementById('editproductsattributestable');
  if(tableelem == null)
    return;

  tablerows = tableelem.getElementsByTagName('tr');

  attributeCount = tablerows.length - 2;
  nextIndex = attributeCount;
  lastIndex = attributeCount - 1;

  nextelem = document.getElementById('editproductattrib[' + lastIndex + ']'); 
  nextelem.style.display="";
  nextelem.getElementsByTagName('input')[0].style.display="";


  // make hidden copy
  clone = nextelem.cloneNode(true);
  clone.id = 'editproductattrib[' + nextIndex + ']';
  clone.style.display="none";

  lastelem = tablerows[tablerows.length - 1];
  tablebodyelem = tableelem.getElementsByTagName('tbody')[0];

  if(tablebodyelem == null)
    tableelem.insertBefore(clone, lastelem);
  else
    tablebodyelem.insertBefore(clone, lastelem);

  nameelem = clone.getElementsByTagName('select')[0];
  if(nameelem != null)
  {
    nameelem.name = 'attributename[' + nextIndex + ']';
    nameelem.onchange = new Function("showCustomAttributeName(" + nextIndex+ ")");
    nameelem.id = 'editproductattribname[' + nextIndex + ']';
  }

  customnameelem = clone.getElementsByTagName('input')[0];
  if(customnameelem != null)
  {
    customnameelem.name = 'attributecustomname[' + nextIndex + ']';
    customnameelem.id = 'editproductattribcustomname[' + nextIndex + ']';
  }

  valueelem = clone.getElementsByTagName('input')[1];
  if(valueelem != null)
  {
    valueelem.name = 'attributevalue[' + nextIndex + ']';
    valueelem.id = 'editproductattribvalue[' + nextIndex + ']';
  }
  
  removelinkelem = clone.getElementsByTagName('a')[0];
  if(removelinkelem != null)
  {
    removelinkelem.onclick = new Function("removeAttribute("+nextIndex+")");
  }
}

function showUploadMediaLoading()
{
  if(document.getElementById('uloadmediaformdiv') != null)
    document.getElementById('uloadmediaformdiv').style.display="none";

  if(document.getElementById('uploadmedialoadingdiv') != null)
    document.getElementById('uploadmedialoadingdiv').style.display="";
}

function setUploadPhotoFlag(set)
{
  if(document.getElementById('editproductaddphoto') != null)
    document.getElementById('editproductaddphoto').value=set;
}

function showImagePopup(pid)
{
  width = (window.screen.width/2) - (300 + 10);
  height = (window.screen.height/2) - (300 + 50);

  var popup = window.open("productimage.php?pid=" + pid, "ProductImage", "status=1, toolbar=0, location=0, menubar=0, resizable=0, scrollbars=0, height=600, width=600, left=" + width + ", top="+ height +", screenX=" + width + ", screenY=" + height);
  popup.focus();
}

function getWindowWidth()
{
  width = -1;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    width = window.innerWidth;
  } 
  else if( document.documentElement && document.documentElement.clientWidth ) 
  {
    width = document.documentElement.clientWidth;
  } 
  else if( document.body && document.body.clientWidth ) 
  {
    width = document.body.clientWidth;
  }

  return width;
}

function getWindowHeight()
{
  height = -1;
  if( typeof( window.innerHeight ) == 'number' ) 
  {
    heigth = window.innerHeight;
  } 
  else if( document.documentElement && document.documentElement.clientHeight ) 
  {
    height = document.documentElement.clientHeight;
  } 
  else if( document.body && document.body.clientHeight ) 
  {
    height = document.body.clientHeight;
  }

  return height;
}


function showImageUploadPopup(url)
{
  width = (window.screen.width/2) - (150 + 10);
  height = (window.screen.height/2) - (50 + 50);

  var popup = window.open(url, "ImageUpload", "status=1, toolbar=0, location=0, menubar=0, resizable=0, scrollbars=0, height=100, width=300, left=" + width + ", top="+ height +", screenX=" + width + ", screenY=" + height);
  popup.focus();
}

function getBaseHref()
{
  if(document.getElementsByTagName('base') != null)
    return document.getElementsByTagName('base')[0].href;
  else
    return "";
}