// this little function to clear the ONFOCUS cute thingie in a field
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
// DONE


// this function allows you to limit the number of character in a textarea or input
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

// the new censorship function - for covering up potentially offensive material
function censorshipsux(x,imgx){
 var divx = document.getElementById('div'+x)
 //var imgx = document.getElementById('img'+x)
   if (divx.style.display == 'none') {
      divx.style.display = 'block'
      document.getElementById(imgx).src = "/BEER-images/close-censorship.png";
   } else {
      divx.style.display = 'none'
      document.getElementById(imgx).src = "/BEER-images/open-censorship.png";
   }
}


  //can I delete the file if they hit cancel???  Working on it.
  //then clear out all the fields and show the bg again

  //how about allowing the users to manage their own pictures?

/*
function HideContent(d) {
  document.getElementById(d).style.display = "none";
  window.uploadedImage.document.body.innerHTML = "<center> <font face=arial>Click the Browse button above to add your picture.</font><br> <img src=camera_add.jpg border=0> </center> ";
  document.imageForm.reset();
}

function ShowContent(d) {
  document.getElementById(d).style.display = "block";
}

function ToggleContent(d) {
  if(document.getElementById(d).style.display == "none") { 
     document.getElementById(d).style.display = "block"; 
  } else { 
     document.getElementById(d).style.display = "none"; 
  }
}
*/

jQuery.noConflict();

jQuery(document).ready(function() {
  jQuery("#open-iframe").click(function(){
    jQuery("#dothings2iframe").slideDown("slow");
  });

  jQuery("#close-iframe").click(function(){
    jQuery("#dothings2iframe").slideUp("slow");
    clearmyform();
  });

  jQuery("#toggle-iframe a").click(function () {
    jQuery("#toggle-iframe a").toggle();
  });
});

function clearmyform() {
      document.getElementById("divResult").innerHTML = "";
      document.getElementById("posttext").innerHTML = "";
      window.uploadedImage.document.body.innerHTML = "";
      window.uploadedImage.imagePath = null;

      document.imageForm.reset();

      window.uploadedImage.document.body.innerHTML = 
           "<center> <font face=arial>Click the Browse button above to add your picture.</font><br> <img src=camera_add.jpg border=0> </center> ";
      document.getElementById("divResult").innerHTML = "";

      document.imageForm.reset();

      
      //window.uploadedImage.document.body.innerHTML = "";
      var uploadedImageFrame = window.uploadedImage;
      var imagePath = uploadedImageFrame.imagePath;
      document.new_post.oldImageToDelete.value = imagePath;
      //window.uploadedImage.imagePath = null;
      //document.getElementById('dothings2iframe').style.display = "none";
  //if(document.getElementById.style.display == "none") { document.getElementById.style.display = "block"; }
  //else { document.getElementById.style.display = "none"; }
}


//----------------------------------------------------------------
var loadingHtml = "<center><br><img src=/wp-admin/images/loading.gif width=30 height=30><br>Loading...</center>"; 
var imageLoadingHtml = "<b>Image loading...</b>";
var http = getXMLHTTPRequest();
//----------------------------------------------------------------
function uploadImage() {
 var uploadedImageFrame = window.uploadedImage;
 uploadedImageFrame.document.body.innerHTML = loadingHtml;
 // VALIDATE FILE
//var myExp = /\s/g; // I want to change spaces to - or _ but couldn't get it working!
// so I just accept the files with spaces, by putting qoutes around the filename...
var imagePath = uploadedImageFrame.imagePath;
//var imagePath = imagePath.replace(myExp,"_");


 if(imagePath == null){
  document.new_post.oldImageToDelete.value = "";
 }
 else {
  document.new_post.oldImageToDelete.value = imagePath;
 }
 document.imageForm.submit();
}

//-----------------------------------------------------------------
function showImageUploadStatus() {
  var uploadedImageFrame = window.uploadedImage;
  if(uploadedImageFrame.document.body.innerHTML == loadingHtml){
    document.getElementById("divResult").innerHTML = imageLoadingHtml;
  }
  else {
    var imagePath = uploadedImageFrame.imagePath;
    if(imagePath == null){
      document.getElementById("divResult").innerHTML = "No uploaded image.";
    }
    else {
      //document.getElementById("divResult").innerHTML = "Loaded image: /" + imagePath;
      // sending the "" will clear out the error - switch with above for troubleshooting
      document.getElementById("divResult").innerHTML = "";
      document.getElementById('posttext').value = "<img src=\"/" + imagePath + "\">";
      document.imageForm.reset();
      window.uploadedImage.document.body.innerHTML = "";
      window.uploadedImage.imagePath = null;
      //document.getElementById('dothings2iframe').style.display = "none";
    }
  }
}
      //----------------------------------------------------------------
function getXMLHTTPRequest() {
  try {
    xmlHttpRequest = new XMLHttpRequest();
  }
  catch(error1) {
    try {
      xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(error2) {
      try {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(error3) {
        xmlHttpRequest = false;
      }
    }
  }
  return xmlHttpRequest;
}
//----------------------------------------------------------------
function sendData() {
  var url = "submitForm.php";
  var parameters = "imageDescription=" + dataForm.imageDescription.value;
  var imagePath = window.uploadedImage.imagePath;
  if(imagePath != null){
   parameters += "&uploadedImagePath=" + imagePath;
  }

  http.open("POST", url, true);

  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", parameters.length);
  http.setRequestHeader("Connection", "close");

  http.onreadystatechange = useHttpResponse;
  http.send(parameters);
  document.getElementById('posttext').value = "<img src=/" + imagePath + ">";
}
//----------------------------------------------------------------
function submitFormIfNotImageLoading(maxLoadingTime, checkingIntervalTime) {
  if(window.uploadedImage.document.body.innerHTML == loadingHtml) {
    if(maxLoadingTime <= 0) {
      document.getElementById("divResult").innerHTML = "The image loading has timed out. "
      + "Please, try again when the image is loaded.";
    }
    else {
      document.getElementById("divResult").innerHTML = imageLoadingHtml;
      maxLoadingTime = maxLoadingTime - checkingIntervalTime;
      var recursiveCall = "submitFormIfNotImageLoading(" 
      + maxLoadingTime + ", " + checkingIntervalTime + ")";
      setTimeout(recursiveCall, checkingIntervalTime);
    }
  }
  else {
    sendData();
  }
}
//----------------------------------------------------------------
function submitForm() {
  var maxLoadingTime = 3000; // milliseconds
  var checkingIntervalTime = 500; // milliseconds
  submitFormIfNotImageLoading(maxLoadingTime, checkingIntervalTime);
}
//----------------------------------------------------------------
function useHttpResponse() {
  if (http.readyState == 4) {
    if (http.status == 200) {
      document.getElementById("divResult").innerHTML = http.responseText;
      dataForm.reset();
      document.imageForm.reset();
      window.uploadedImage.document.body.innerHTML = "";
      window.uploadedImage.imagePath = null;
    }
  }
}


    
