function newsInit()
{
  var publishButton = document.getElementById("publishbutton");
  if(publishButton)
  {
    publishButton.onclick = publishArticle;
  }
  
  var uploads = document.getElementById("fileupload");
  if (uploads)
  {
     uploadEvents();
  }
  var uploadedFiles = document.getElementById("afterupload");
  if (uploadedFiles)
  {
    removeHandlers();
  }   
}

function publishArticle()
{
   var publishElement = document.getElementById("publish");
   if(publishElement)
   {
     publishElement.value = "1";
     var articleForm = document.getElementById("articleform");
     if(articleForm)
     {
       articleForm.submit();
     }   
   }
   
}
  
function uploadEvents()
{
  var uploadForm = document.getElementById("fileupload");
  if (uploadForm)
  {
   var inputElements = uploadForm.getElementsByTagName("input");
   if (inputElements)
   {
     for (var i=0; i < inputElements.length; i++)
     {
       if (inputElements[i].type == "button")
       {
         inputElements[i].onclick = fileUpload;
       }  // Checked for buttons      
     } // Looped through all the input elements
   } // Made sure there are input elements  
  }// Made sure the form exists
  
  
}
function removeHandlers()
{
  var uploadedDiv = document.getElementById("afterupload");
  if (uploadedDiv)
  {
   var inputTypeElements = uploadedDiv.getElementsByTagName("input");
   if (inputTypeElements)
   {
    for (var x=0; x < inputTypeElements.length; x++)
    {
      if(inputTypeElements[x].type == "button")
        inputTypeElements[x].onclick = removeFile;           
    }    
   }
  }
}


function fileUpload()
{
  var uploadForm = document.getElementById("fileupload");
  var uploadInput = document.getElementById("fileinput");
  if (uploadForm && uploadInput.value != "")
  {
   showStatusDiv();
   uploadForm.target="fileframe";
   uploadForm.action="/news/process/uploadme.php";
   uploadForm.submit();  
   uploadForm.reset();
  }
  else if(uploadInput.value == "")
    alert("No file was selected");

}
function showFile(fileName, tempLocation)
{
 var tr = document.createElement("tr"); 
 var nameTd = document.createElement("td");
 var buttonTd = document.createElement("td");
  
 var fileNameSpan = document.createElement("span");
 fileNameSpan.innerHTML = fileName;
 
 var fileLocationInput = document.createElement("input");
 fileLocationInput.name = "imageuploads[]";
 fileLocationInput.type = "hidden";
 fileLocationInput.value = tempLocation;

  
 var removeButton = document.createElement("input");
 removeButton.type = "button";
 removeButton.value = "Remove File";
 
 nameTd.appendChild(fileNameSpan);
 tr.appendChild(nameTd);
 
 buttonTd.appendChild(fileLocationInput);
 buttonTd.appendChild(removeButton);
 tr.appendChild(buttonTd);
 
 var mainContainer = document.getElementById("afterupload");
 if (mainContainer && mainContainer != "")
 {
   mainContainer.appendChild(tr);
 }
 

}

function removeFile()
{
  var fileId = this.previousSibling.value;
  
  new Ajax.Updater("afterupload", '/news/process/deleteFile.php?fid='+fileId, {asynchronous:true, onFailure: Ajax_Failure, onException: Ajax_Exception});
  
  var parentContainer = this.parentNode.parentNode;
  var mainUploadDiv = document.getElementById("afterupload")
  if (mainUploadDiv)
    mainUploadDiv.removeChild(parentContainer);     
} 

function showStatusDiv()
{
 var statusDiv = document.createElement("div");
 statusDiv.id = "statusdiv";   

 var subDiv = document.createElement("div");        
 subDiv.innerHTML = "PLEASE WAIT!"; 
 
 statusDiv.appendChild(subDiv);
 document.body.appendChild(statusDiv);
}
function hideStatusDiv()
{
  var statusDiv = document.getElementById("statusdiv");
  document.body.removeChild(statusDiv); 
}

function showError( error )
{
	alert(error);
}

function Ajax_Failure( xmlHttpRequest, xJSONResponse )
{
	alert("Ajax call failed...");
}  


function Ajax_Exception( ajaxRequest, exception )
{
	alert(exception);
}

function invalidType()
{
 alert("That file type is not allowed");
}




