var createDir;
var clientId;
var clientName;
var galleryId;

var voucherCode;
var voucherValue;
var voucherExpiry;

var orderNo;


function encryptPassword()
{
  var pass = document.getElementById('password');
  pass.value = hex_md5(pass.value);
}

function selectDir(dir, id, count, button, gallery)
{
  createDir = dir;
  dirCount = count;

  var selected = getElementsByClassName(document, 'div', 'dir_item_hot')
  for(var i=0; i<selected.length; i++)
    selected[i].className = 'dir_item';

  document.getElementById(id).className = 'dir_item_hot';

//  document.getElementById("createbutton").value    = button;
  document.getElementById("createbutton").disabled = button=="update"?true:false;

  if (button == "update")
  {
    document.getElementById("editbutton").disabled = false;
    galleryId = gallery;
  }
  else
    document.getElementById("editbutton").disabled = true;
}

function editGallery()
{
  window.location = "editgallery.php?gallery="+galleryId;
}

function printVouchers(_batch)
{
  var url = "printvouchers.php?batch="+_batch;
  window.open(url, 'vouchers', 'left=0,top=0,width=800,height='+screen.height+',location=no,toolbar=no,directories=no,status=no,menubar=no,history=no,scrollbars=yes');
}

function validateClient()
{
  canContinue = true;

  canContinue = canContinue && checkElement(document.getElementById('first_name'));
  canContinue = canContinue && checkElement(document.getElementById('last_name'));
  canContinue = canContinue && checkElement(document.getElementById('address_1'));
  canContinue = canContinue && checkElement(document.getElementById('city'));
  canContinue = canContinue && checkElement(document.getElementById('state'));
  canContinue = canContinue && checkElement(document.getElementById('postal'));
  canContinue = canContinue && checkElements(document.getElementById('email'));
  canContinue = canContinue && checkElementsOneEntered(document.getElementById('phone'), document.getElementById('mobile'));

  return canContinue;
}

function createClient()
{
  window.location = "addclient.php";
}

function selectClient(clientid, name, id)
{
  clientId = clientid;
  clientName = name;

  var selected = getElementsByClassName(document, 'div', 'client_item_hot')
  for(var i=0; i<selected.length; i++)
    selected[i].className = 'client_item';

	document.getElementById(id).className = 'client_item_hot';
  getClientFolders();
}

function setAccessCode(code)
{
  first = document.getElementById('first_name');
  last  = document.getElementById('last_name');
  if (first.value.length > 0 && last.value.length > 0)
  {
    document.getElementById('access_code').value = last.value.substr(0,1)+code+first.value.substr(0,1);
    document.getElementById('voucher_code').value = document.getElementById('access_code').value;
  }
}

function validateSettings()
{
  document.getElementById('password').value = hex_md5(document.getElementById('password').value);
}

function deleteSize(_code)
{
  if (confirm("Are you sure you want to delete '"+_code+"'?"))
  {
    window.location = "deletesize.php?code="+_code;
  }
}

function deleteFinish(_code)
{
  if (confirm("Are you sure you want to delete '"+_code+"'?"))
  {
    window.location = "deletefinish.php?code="+_code;
  }
}

function deleteMat(_code)
{
  if (confirm("Are you sure you want to delete '"+_code+"'?"))
  {
    window.location = "deletemat.php?code="+_code;
  }
}

function deleteFrame(_code)
{
  if (confirm("Are you sure you want to delete '"+_code+"'?"))
  {
    window.location = "deleteframe.php?code="+_code;
  }
}

function selectSize(_edit, _name)
{
  var num = parseFloat(_edit.value);
  if (num == 0 || _edit.value == '')
  {
    _edit.value = '0.00';
    document.getElementById(_name).checked = false;
  }
  else
  {
    _edit.value = num.toFixed(2);
    document.getElementById(_name).checked = true;
  }
}

function deleteGallery(_gallery)
{
  if (confirm("Are you sure you want to delete this gallery?"))
  {
    window.location = "deletegallery.php?gallery="+_gallery;
  }
}

function updateGallery(_gallery)
{
  if (confirm("Are you sure you want to update this gallery and add new images?"))
  {
    window.location = "updategallery.php?gallery="+_gallery;
  }
}

function deleteGalleryItem(_gallery, _item)
{
  if (confirm("Are you sure you want to delete this gallery item?"))
  {
    window.location = "deletegalleryitem.php?gallery="+_gallery+"&item="+_item;
  }
}

function updateGalleryItem(_gallery, _item)
{
  if (confirm("Are you sure you want to update this gallery item from the original image?"))
  {
    window.location = "updategalleryitem.php?gallery="+_gallery+"&item="+_item;
  }
}

function showAddClientVoucher(_code)
{
  document.getElementById('add_voucher').style.display = 'block';
  document.getElementById('add_voucher').style.visibility = 'visible';
}

function closeClientVoucher()
{
  document.getElementById('add_voucher').style.display = 'none';
  document.getElementById('add_voucher').style.visibility = 'hidden';
	createGallery();
}

function saveClientVoucher()
{
  voucherCode   = document.getElementById('voucher_code').value;
  voucherValue  = document.getElementById('voucher_value').value;
  voucherExpiry = document.getElementById('voucher_expiry').value;
	addClientVoucher();
}

function completeOrder(_order)
{
	orderNo = _order;
	doCompleteOrder();
}

function deleteOrder(_order)
{
  if (confirm("Are you sure you want to delete this order?"))
  {
    window.location = "deleteorder.php?order="+_order;
  }
}

function dashover(_title, _month)
{
	document.getElementById(_title).className = "dash_hot";
	document.getElementById(_month).className = "dash_hot";
}

function dashout(_title, _month)
{
	document.getElementById(_title).className = "dash_title";
	document.getElementById(_month).className = "";
}