


function validatePhotoFoldForm() {

    return true;
}

function getNeededNumberOfImages() {
    var photoFoldType = document.getElementById("photoFoldTypeString");
    return getNumberOfImagesByType(photoFoldType.value);
}

function showHideImageFileControls() {

    var numImages = getNeededNumberOfImages();
    
    for (var i = 0; i < getMaxNumberImages(); i++) {
        var fileControlDiv = document.getElementById("imageDiv_" + i + "_");
        
        if (i >= numImages) {
            fileControlDiv.style.display = "none";
        }
        else {
            fileControlDiv.style.display = "block";
        }
    }
}

function validatePhotoFoldUploadForm(index) {
    var galleryImageElement = document.getElementById("galleryImageName_" + index + "_");
    var galleryImage = null;
    if (galleryImageElement) {
        galleryImage = galleryImageElement.value;
    }
    var file = document.getElementById("image_" + index + "_").value;
    
    if ((null == galleryImage || "" == galleryImage) && (null == file || "" == file)) {
        // Move this to an inline message or don't have it at all?
        // alert("Please select an image for upload or a gallery image.");
        // return false;
    }
    return true;
}

function submitImageFormForFileUpload(index, pool) {

  //alert("submitImageFormForFileUpload index=" + index + " pool=" + pool);

  var theForm = document.getElementById("fileForm_" + index + "_");
  
  if (!pool) {
    pool = '';
  }
  
  var galleryImageName =  document.getElementById("galleryImageName_" + index + "_");
  if (null != galleryImageName) {
    galleryImageName.value = "";
  }

  // Set crop field
  var cropMasterElement = document.getElementById("cropMaster");
  var cropMasterValue = null;
  if (null != cropMasterElement) {
      cropMasterValue = document.getElementById("cropMaster").checked;
	  if (cropMasterValue) {
	    document.getElementById("crop_" + index + "_").value = "true";
	  } else {
	    document.getElementById("crop_" + index + "_").value = "false";
	  }
  }
  
  hideCompletedImageLink();
  
  var fileName = document.getElementById("image_" + index + "_").value;
  //alert("submitting file image. fileName: " + fileName);
  
  if (validatePhotoFoldUploadForm(index)) {
      disablePhotoFoldSubmit();
      showLoadingLayer(index);
      //alert("submitting file image. index: " + index + " url: " + theForm.action);
      
dojo.io.iframe.send( {
    //contentType: "multipart/form-data",
    contentType: "text/html",
    form: theForm,
    error: function(error) {
        hideLoadingLayer(index);
        reenablePhotoFoldSubmit();
        alert ("whoa, can't load this file as an image...try a gif or jpeg smaller than 6 meg.\n"); 
    },
    method: "post",
    handleAs: "text",
    load: function(type, data, evt) {
	    //alert("Completed file image LOAD. index: " + index + " data: " + data);
	    //alert("Type: " +type);
	    hideLoadingLayer(index);
        reenablePhotoFoldSubmit();
	    if ('true' != type) {
	     alert ("whoa, can't load this file as an image...try a gif or jpeg smaller than 6 meg.\n"); 
	    } else {
	     dojo.byId("imageResult_" + index + "_").innerHTML = '<img onclick="bigImage(' + index + ', \'' + pool + '\');" src="/image.action?index=' 
	        + index + '&pool=' + pool + '&file=' + fileName + '&time=' + new Date() + '">';
	    }

	    return false;
    }
    });       
  }
}


/*

function submitImageFormForFileUpload(index) {
  var form = document.getElementById("fileForm_" + index + "_");
  document.getElementById("galleryImageName_" + index + "_").value = "";
  hideCompletedImageLink();
  
  if (validatePhotoFoldUploadForm(index)) {
      disablePhotoFoldSubmit();
      showLoadingLayer(index);
      alert("submitting file image. index: " + index + " url: " + form.action);
      
      // submit the form in the background    
    dojo.io.iframe.send({
            contentType: "multipart/form-data",
            form:     form,
            load:     function (data) { upload_file_helper (data); },
            error:    function (error) {alert ("Uknown error has occurred:\n" +
error);}
            handler: function(type, data, evt){
				dojo.debug("Type:", type);
				dojo.debug("Response:", data);
				alert("completed file image. index: " + index);
				dojo.byId("imageResult_" + index + "_").innerHTML = data; 
				hideLoadingLayer(index);
				reenablePhotoFoldSubmit();
            }
        }); 
  }
}
*/

function submitImageFormForGalleryImage(index, pool) {

  if (!pool) {
    pool = '';
  }
  
  var theForm = document.getElementById("galleryImageForm_" + index + "_");
  document.getElementById("image_" + index + "_").value = "";
  var galleryName = document.getElementById("galleryImageName_" + index + "_").value;
  hideCompletedImageLink();
  if (validatePhotoFoldUploadForm(index)) {
      disablePhotoFoldSubmit();
      showLoadingLayer(index);
      
      //alert("submitting gallery image. index: " + index + " url: " + theForm.action);
      
    // submit the form in the background    
    dojo.xhrPost({
        url: theForm.action,
        form: theForm,
        handleAs: "text",
        error: function(error) {alert ("Unknown error has occurred:\n" + error); },
        handle: function(data,args){
           //alert("completed gallery image. index: " + index);
           dojo.byId("imageResult_" + index + "_").innerHTML = '<img onclick="bigImage(' + index + ', \'' + pool + '\');" src="/image.action?index=' 
            + index + '&pool=' + pool + '&gallery=' + galleryName + '">';
           hideLoadingLayer(index); 
           reenablePhotoFoldSubmit();
        }
    });
  }
}

function bigImage(index, pool) {
    //alert("bigImage: " + index);
    
    if (!pool) {
        pool = '';
    }
    var imageDiv = dojo.byId("imageLayer_" + index);
    
    // If big image is already up, then clear it.
    if (imageDiv.style.display == "block") {
          imageDiv.style.display = "none"; 
          return;
    }
        
    imageDiv.style.display = "block";
    imageDiv.style.cursor = "pointer";
    
    var galleryElement = document.getElementById("galleryImageName_" + index + "_");
    var galleryName = null;
    if (galleryElement) {
        galleryName = galleryElement.value;
    }
    var fileName = document.getElementById("image_" + index + "_").value;
    
    imageDiv.innerHTML = '<img onclick="clearImage(' + index + ');" src="/image.action?tn=false&index=' 
            + index + '&gallery=' + galleryName + '&pool=' + pool + '&file=' + fileName + '&time=' + new Date() + '">';
    
}

function clearImage(index) {
    //alert("clearImage: " + index);
    var imageDiv = dojo.byId("imageLayer_" + index);
    imageDiv.style.display = "none";   
}

function hideCompletedImageLink() {
  var completedImageDiv = document.getElementById("completedImageDiv");
  if (null != completedImageDiv) {
    completedImageDiv.style.display = "none";
  }
  
  completedImageDiv = document.getElementById("kaleidocycle_result_div");
  if (null != completedImageDiv) {
    completedImageDiv.style.display = "none";
  }
  
}

function showLoadingLayer(index) {
    var loading = parent.document.getElementById("loadingLayer_" + index);
    loading.style.display = "block";
    loading = null;             
}

function hideLoadingLayer(index) {
    var loading = parent.document.getElementById("loadingLayer_" + index);
    loading.style.display = "none";
    loading = null; 
}
            

function disablePhotoFoldSubmit() {
  // Disable submit button
  try {
    document.getElementById("submitButton").disabled = true;
  }
  catch(ignore) { } 
}

function reenablePhotoFoldSubmit() {
  // Reenable submit button from iFrame
  try {
    document.getElementById("submitButton").disabled = false;
    document.getElementById("errors").innerHTML = document.getElementById("errors").innerHTML;
    var valid = document.getElementById("valid").innerHTML;
    var index = document.getElementById("index").innerHTML;
    
    // if there is an error, then clear drop down.
    if ("true" != valid) {
        document.getElementById("galleryImageName_" + index + "_").value = "";
    }
  } catch(ignore) { } 
  
  try {
 // alert("hiding loading layer");
   var index = document.getElementById("index").innerHTML;
     //alert("hiding loading layer for index: " + index);
     if ("" == index) {
     // clear all layers
         try {
        for (var i = 0; i < 10; i++) {
            //alert("clearing i=: " + i);
            hideLoadingLayer(i);
        }
      } catch (e) {
        // ignore
      }
     } else {
        hideLoadingLayer(index);
   }
  } catch(ignore) { } 
}

function checkFileInputs() {

    try {
	    for (var i = 0; i < 10; i++) {
	        //alert("clearing i=: " + i);
	        parent.frames['imageiframe_' + i + '_'].location.reload(true);
	    }
    } catch (e) {
        // ignore
    }
}

function addLoadEvent(func) { 
 var oldonload = window.onload; 
 if (typeof window.onload != 'function') { 
  window.onload = func; 
 } else { 
  window.onload = function() { 
   if (oldonload) { 
    oldonload(); 
   } 
   func(); 
  }  
 } 
}

function createHeaderImages() {
// Active images
f1_on = new Image();
o1_on = new Image();
l1_on = new Image();
d1_on = new Image();
p1_on = new Image();
l2_on = new Image();
a1_on = new Image();
y1_on = new Image();
f1_on.src = "img/f1_b_head.gif";
o1_on.src = "img/o1_b_head.gif";
l1_on.src = "img/l1_b_head.gif";
d1_on.src = "img/d1_b_head.gif";
p1_on.src = "img/p1_b_head.gif";
l2_on.src = "img/l2_b_head.gif";
a1_on.src = "img/a1_b_head.gif";
y1_on.src = "img/y1_b_head.gif";

// Inactive images
f1_off = new Image();
o1_off = new Image();
l1_off = new Image();
d1_off = new Image();
p1_off = new Image();
l2_off = new Image();
a1_off = new Image();
y1_off = new Image();
f1_off.src = "img/f1_a_head.gif";
o1_off.src = "img/o1_a_head.gif";
l1_off.src = "img/l1_a_head.gif";
d1_off.src = "img/d1_a_head.gif";
p1_off.src = "img/p1_a_head.gif";
l2_off.src = "img/l2_a_head.gif";
a1_off.src = "img/a1_a_head.gif";
y1_off.src = "img/y1_a_head.gif";
}

function getCurrentPageSafe() {
 var currentPage = "";
 try {
  currentPage = getCurrentPage();
 } catch(e) { /* ignore */ }
 return currentPage;
}
  
   
function imgOn(imgName, override) {
 var currentPage = getCurrentPageSafe();
 if (imgName != currentPage || (override != null && override == true)) {
  if (document.images && null != imgName && "" != imgName) {
   document[imgName].src = eval(imgName + "_on.src");
  }
 }
}

function imgOff(imgName) {
 var currentPage = getCurrentPageSafe();
 if (imgName != currentPage) {
  if (document.images && null != imgName && "" != imgName) {
   document[imgName].src = eval(imgName + "_off.src");
  }
 }
}

/** Dojo. */

function loadFromQuery(url){
   var get = dojo.xhrGet({ url: url });
   return function(node){
     get.addCallback(function(data){
       node.innerHTML = data;
       return data;
     });
   }
 }




