function addImage2Item(nid, path) {
  if (document.getElementById('edit-image').value == '') {
    alert('Please specify file to upload');
    return;
  }
  createIframe();
  if (document.all) {
  	iframeDocument = window.top.document.getElementById('redirect-target').contentWindow.document;
  }
  else {
  	iframeDocument = window.top.document.getElementById('redirect-target').contentDocument;
  }
  //alert(window.top.document.domain);
  //alert(iframeDocument.domain);
  iframeDocument.write('<body></body>');
  form = iframeDocument.getElementsByTagName('body').item(0).appendChild(iframeDocument.createElement('form'));
  form.setAttribute('enctype', 'multipart/form-data');
  form.setAttribute('method', 'post');
  form.setAttribute('action', path + 'Ajax/addItemImage/' + nid);
  form.setAttribute('id', 'add-image');
  //fileInput = ;
  //alert(fileInput);
  //alert(fileInput.value);
  fileInput = form.appendChild(window.top.document.getElementById('edit-image').cloneNode(true));
  fileInput.removeAttribute('id');
  //alert(file.value);
  form.appendChild(fileInput);
  //alert(iframeDocument);
  iframeDocument.getElementsByTagName('form').item(0).submit()
}

function sendSearchQuery() {
  if (document.getElementById('item_search').value == '') {
  	alert('Enter search keywords');
  	return;
  }
  if (document.getElementById('item_exhibitor').disabled || document.getElementById('item_exhibitor').value == '') {
  	window.location.href = siteUrl + 'Items-Search/' + document.getElementById('item_client').value + '/' + encodeURIComponent(document.getElementById('item_search').value) + '/' + document.getElementById('item_sort').value;
  	return;
  }
  window.location.href = siteUrl + 'Items-Search/' + document.getElementById('item_client').value + '/' + document.getElementById('item_exhibitor').value + '/' + encodeURIComponent(document.getElementById('item_search').value)  + '/' + document.getElementById('item_sort').value;
}

function add2Lightbox(nid) {
  request = HTTPGet(siteUrl + 'Ajax/add2Lightbox/' + nid, add2LighboxCallback, nid);
}

function removeFromLightbox(nid) {
  request = HTTPGet(siteUrl + 'Ajax/removeFromLightBox/' + nid, removeFromLighboxCallback, nid);
}

function removeItemFromRequest(nid, rid) {
  request = HTTPGet(siteUrl + 'Ajax/removeFromRequest/' + nid + '/' + rid, removeItemFromRequestCallback, nid);
}

function add2LighboxCallback(responseText, request, nid) {
  switch(request.responseXML.documentElement.getAttribute('status')) {
  	case '0':
  	  window.location.href = siteUrl + 'user?destination=' + window.location.href.substr(siteUrl.length, window.location.href.length);
  	  break;
  	case '1': 
  	  var button = document.getElementById('item_lightbox_button_' + nid);
  	  button.setAttribute('class', 'lightboxremove');
	    button.className = 'lightboxremove';
  	  button.onclick = function() {removeFromLightbox(nid);};
	  if (link2L = document.getElementById('lightbox_link')) {
	  	  link2L.innerHTML = 'My current Lightbox (' + request.responseXML.documentElement.getAttribute('items') + ')';
	  }
	  if (buttonL = document.getElementById('gotoyourlightbox')) {
	  	  if (document.all) {
		  	buttonL.style.display = 'block';
		  }
		  else {
		  	buttonL.style.display = 'inline';
		  }
	  }
  	  
  	  break;
  }
}

function removeFromLighboxCallback(responseText, request, nid) {
  switch(request.responseXML.documentElement.getAttribute('status')) {
  	case '0':
  	  alert('Item has NOT been removed. Please try again.');
  	  break;
  	case '1':
  	  if (div = document.getElementById('lightbox_' + nid)) {
  	  	document.getElementById('imagelibrary').removeChild(div);
		document.getElementById('lightbox_link').innerHTML = 'My current Lightbox (' + request.responseXML.documentElement.getAttribute('items') + ')';
  	  }
  	  else {
		var button = document.getElementById('item_lightbox_button_' + nid);
  	    button.setAttribute('class', 'lightboxadd');
		button.className = 'lightboxadd';
  	    button.onclick = function() {add2Lightbox(nid);};
		if (link2L = document.getElementById('lightbox_link')) {
	  	  link2L.innerHTML = 'My current Lightbox (' + request.responseXML.documentElement.getAttribute('items') + ')';
	    }
		if ((buttonL = document.getElementById('gotoyourlightbox')) && request.responseXML.documentElement.getAttribute('items') == 0) {
	  	  buttonL.style.display = 'none';
	    }
  	   }
  	  break;
  }
}

function removeItemFromRequestCallback(responseText, request, nid) {
  switch(request.responseXML.documentElement.getAttribute('status')) {
  	case '0':
  	  alert('Item has NOT been removed. Please try again.');
  	  break;
  	case '1':
  	  document.getElementById('imagelibrary').removeChild(document.getElementById('request_' + nid));
  	  break;
  }
}

function submitReleaseForm() {
  if (document.getElementById('date').value == '') {
  	alert('Please specify the date of publication.');
  	document.getElementById('date').focus();
  	return false;
  }
  if (document.getElementById('publication').value == '') {
  	alert('Please specify where the images are going to be published.');
  	document.getElementById('publication').focus();
  	return false;
  }
  if (document.getElementById('details').value == '') {
  	alert('Please provide your comments and some details about publication.');
  	document.getElementById('details').focus();
  	return false;
  }  
  return true;
}

function openPopUp(nid, url) {
  var popUp = window.open(siteUrl + 'Image/' + nid, 'popUp' + nid, 'menubar=no;toolbar=no;location=no;resizable=yes;');
}

function resizeToImage() {
  var NS = (navigator.appName == 'Netscape')?true:false;
  var iWidth = (NS)?window.innerWidth:document.body.clientWidth;
  var iHeight = (NS)?window.innerHeight:document.body.clientHeight;
  iWidth = document.images[0].width - iWidth;
  iHeight = document.images[0].height - iHeight;
  if (document.all) {
  	window.resizeTo(document.body.clientWidth, document.body.clientHeight + 35);
  }
  else {
  	window.resizeBy(iWidth, iHeight);
  }
  self.focus(); 
}

function sortReleases(sort, path) {
  window.location.href = siteUrl + 'Press-Item/' + path + '/' + sort;
}

function changeExhibitor(selected) {
  if (selected) {
    window.location.href = siteUrl + 'Items/' + document.getElementById('item_client').value + '/' + selected  + '/Alphabetically';
  }
  else {
    window.location.href = siteUrl + 'Items/' + document.getElementById('item_client').value + '/Alphabetically';
  }
}

function showSheet(key) {
  elem = document.getElementById('sh_' + key);
  if (elem.style.display == 'none') {
  	elem.style.display = 'block';
  }
  else {
  	elem.style.display = 'none';
  }
}

function show(id) {
  if (document.getElementById(id).style.display == 'none') {
  	document.getElementById(id).style.display = 'block';
  }
  else {
  	document.getElementById(id).style.display = 'none';
  }
}