/* METHOD for FileObject
 *
 * disable, hide, and rename the file input with a value
 */
function DisableInput()
{
  /* Do not disable the element.  Disabled inputs do not get submitted */
  //this.file_input.setAttribute("disabled", "");
  //this.file_input.style.display = "block";	
	this.file_input.style.position = "absolute";
	this.file_input.style.left = "-10000px";
	this.file_input.setAttribute("name", "file_" + this.file_id);
}

/* METHOD for FileObject
 *
 * set id and name attributes for all visual elements.
 * set the class attribute to odd or even.
 * set the argument to the remove file method.
 */
function ResetFileId(file_id)
{
  this.file_id = file_id;
  
  this.file_input.id = "file_input_" + this.file_id + "_" + this.id;
  this.file_input.setAttribute("name", "file_" + this.file_id);
  
  this.file_display.id = "file_" + this.file_id + "_" + this.id;
  if(this.file_id % 2)
    {
      //this.file_display.setAttribute("class", "upload_file_odd");
      this.file_display.className = "upload_file_odd";
    }
  else
    {
      //this.file_display.setAttribute("class", "upload_file_even");
      this.file_display.className = "upload_file_even";
    }

  var button = this.file_display.getElementsByTagName("input").item(0);
  button.id = "file_" + this.file_id + "_remove_" + this.id;
  button.file_id = file_id;
  button.onclick = function (e)
  {
    this.file_upload.RemoveFile(this.file_id);
  };
}

/* FileObject constructor
 *
 */
function FileObject(id, file_id)
{
  this.file_input   = null;
  this.file_display = null;
  this.file_id      = file_id;
  this.id           = id;

  this.DisableInput = DisableInput;
  this.ResetFileId  = ResetFileId;
}


/* METHOD for FileUpload
 *
 * create a new active file input element
 */
function CreateFileInput()
{
  var file_tmp = this.file;
  
  this.file = this.doc.createElement("input");
  this.file.id = "file_" + this.id;
  this.file.setAttribute("type", "file");
  this.file.setAttribute("class", "upload_file");
  this.file.className = "upload_file";
  this.file.onchange = function (e)
  {
		//alert("1. calling AddFile");	
    this.file_upload.AddFile();
  };
  this.file.file_upload = this;
  if(file_tmp)
    {
      this.form.insertBefore(this.file, file_tmp);
    }
  else
    {
      this.form.appendChild(this.file);
    }
}

/* METHOD for FileUpload
 *
 * Create the selected file display elements
 */
var bg = 0;
function CreateFileDisplay(file_obj)
{

  var div = this.doc.createElement("div");
  div.id = "file_" + file_obj.file_id + "_" + this.id;
  div.className = "upload_file_even";
  
  if(bg == 0) {
	div.style.background = "#cdcdcd";
  } else {
	div.style.background = "#dcdcdc";
  }
  bg = 1 - bg;
  div.innerHTML = file_obj.file_input.value;
  div.file_upload = this; 
	//alert("alert4. "+div.innerHTML+" , "+file_obj.file_input.value);
	
  var button = this.doc.createElement("input");
  button.id = "file_" + file_obj.file_id + "_remove" + this.id;
  button.setAttribute("type", "button");
  button.className = "upload_remove";
  button.setAttribute("value", "Remove");
  button.file_id = file_obj.file_id;
  button.onclick = function (e)
  {
    this.file_upload.RemoveFile(this.file_id);    
  };
  button.file_upload = this;
  div.appendChild(button);
  
  this.form.insertBefore(div, this.upload);
  
  return div;
}

/* METHOD for FileUpload
 *
 * Remove a file from the selected list
 */
function RemoveFile(file_id)
{
  var i;
  var j;
  for(i = 0; i < this.files.length; i++)
    {
      if(this.files[i].file_id == file_id)
	{
	  this.files[i].file_input.parentNode.removeChild(this.files[i].file_input);
	  this.files[i].file_input = null;
	  
	  this.files[i].file_display.parentNode.removeChild(this.files[i].file_display);
	  this.files[i].file_display = null;

	  this.files[i] = null;
	  break;
	}
    }
  for(j = i; j < (this.files.length-1); j++)
    {
      this.files[j] = this.files[j+1];
      this.files[j].ResetFileId(j+1);
    }
  this.files.pop();

  this.file_count = this.files.length + 1;
  this.SetInputStatus();
}

/* METHOD for FileUpload
 *
 * Add a newly selected file to the list
 */
function AddFile()
{
  /*
   * save current selection
   * with appropriate changes
   */
	
  var file_obj = new FileObject(this.id, this.file_count);
  
  file_obj.file_input = this.file;
  file_obj.DisableInput();
	
  file_obj.file_display = this.CreateFileDisplay(file_obj);

  file_obj.ResetFileId(this.file_count);
  this.files.push(file_obj);

  this.CreateFileInput();
  this.file_count ++;
  this.SetInputStatus();
}

/* METHOD for FileUpload
 *
 * Set various elements to disabled or enabled upon
 * change of number of selected files.
 */
function SetInputStatus()
{
		//alert("File Count : " + this.file_count);
  if(this.file_count > 1)
    {
			this.upload.removeAttribute("disabled"); //my_code : Camino problem			
			//alert("my_alert : Enable");
    }
  else
    {
      //this.upload.setAttribute("disabled", ""); //my_code : Camino problem
			//alert("my_alert : Disable");
			this.upload.setAttribute("disabled", "true");
    }
  
  if(this.max_file > 0)
    {
      if(this.file_count > this.max_file)
	{
	  this.file.setAttribute("disabled", "");
	}
      else
	{
	  this.file.removeAttribute("disabled");
	}
    }
  this.file_count_elem.setAttribute("value", this.file_count - 1);
}

/* METHOD for FileUpload
 *
 * submit the form
 */
function Upload()
{
  this.uploading_msg.style.display = '';
  this.form.submit();
}


/* CONSTRUCTOR for FileUpload
 *
 * The code that creates the FileUpload object is responsible
 * for adding it to the document by parent.appendChild(obj.main);
 *
 * For example:
 *   function addFileUpload(doc, id, url, max_file, input_vars)
 *   {
 *     var body = doc.getElementsByTagName("body").item(0);
 *     var file_upload = new FileUpload(doc, id, url, max_file, input_vars);
 *     body.appendChild(file_upload.main);
 *   }
 *
 * doc        = document where the elements should be created
 * id         = used to calculate element ids
 * url        = form.action attribute
 * max_file   = maximum number of files to select. < 0 for unlimited
 * input_vars = associative array of input.type == hidden variables
 *              to add to the form.               
 */
function FileUpload(doc, id, url, max_file, input_vars)
{
  /*
   * 
   */
  this.doc        = doc;
  this.id         = id;
  this.files      = Array();
  this.file_count = 1;
  this.max_file   = max_file;
  this.input_vars = input_vars;
  
  /*
   * methods
   */
  this.CreateFileInput   = CreateFileInput;
  this.CreateFileDisplay = CreateFileDisplay;
  this.RemoveFile        = RemoveFile;
  this.AddFile           = AddFile;
  this.Upload            = Upload;
  this.SetInputStatus    = SetInputStatus;
  
  /* Create the main div */
  this.main = doc.createElement("div");
  this.main.id = id;
  this.main.className = "upload_main";
  this.main.innerHTML = "";
  this.main.file_upload = this;

  /* create the main form in the div */
  var C = new utipsClient();
  if(C.ie)
    {
      this.form = doc.createElement('<form action="'+url+'" method="post" enctype="multipart/form-data">');
    }
  else
    {
      this.form = doc.createElement("form");
      this.form.setAttribute("action", url);;
      this.form.setAttribute("method", "post");
      this.form.setAttribute("enctype", "multipart/form-data");
    }
  this.form.id = "form_" + id;
  this.form.file_upload = this;
  this.main.appendChild(this.form);
  
  /* create the main file input in the form */
  this.CreateFileInput();

  /* create the file count input element */
  this.file_count_elem = doc.createElement("input");
  this.file_count_elem.setAttribute("type", "hidden");
  this.file_count_elem.setAttribute("name", "file_count");
  this.file_count_elem.setAttribute("value", 0);
  this.file_count_elem.style.display = "none";
  this.form.appendChild(this.file_count_elem);  
  
  // my_code : ashish : vinove : o create checkboxes
  
  if(id == "new_file") {
	  this.check1 = doc.createElement("input");  
	  this.check1.id = "check1_" + id;  
	  this.check1.setAttribute("type", "checkbox");
	  this.check1.setAttribute("name", "overwrite");
	  this.form.appendChild(this.check1);
	  
	  this.check1_cap = doc.createElement("span");
	  this.check1_cap.id = "check1_cap" + id;
	  this.check1_cap.innerHTML = "<b>Overwrite file if it exists</b><br>";
	  this.check1_cap.className = "upload_file_options";
		this.form.appendChild(this.check1_cap);
	  
	  this.check2 = doc.createElement("input");  
	  this.check2.id = "check2_" + id;  
	  this.check2.setAttribute("type", "checkbox");
	  this.check2.setAttribute("name", "new_window");
	  this.form.appendChild(this.check2);
	
	  this.check2_cap = doc.createElement("span");
	  this.check2_cap.id = "check2_cap" + id;
	  this.check2_cap.innerHTML = "<b>Open in new window</b><br>";
	  this.check2_cap.className = "upload_file_options";
		this.form.appendChild(this.check2_cap);
	  
	  this.check3 = doc.createElement("input");  
	  this.check3.id = "check3_" + id;  
	  this.check3.setAttribute("type", "checkbox");
	  this.check3.setAttribute("name", "resize_image");		
		this.check3.defaultChecked = true;		
	  this.form.appendChild(this.check3);
	  
	  this.check3_cap = doc.createElement("span");
	  this.check3_cap.id = "check3_cap" + id;
	  this.check3_cap.innerHTML = "<b>Auto-resize my uploaded image(s)</b><br><br>";  
	  this.check3_cap.className = "upload_file_options";
		this.form.appendChild(this.check3_cap);
  }
  // end of my_code : ashish : vinove : To create checkboxes ////
  
  /* create the upload button in the form */
  this.upload = doc.createElement("input");
  this.upload.id = "upload_" + id;
  this.upload.setAttribute("type", "button");
  this.upload.className = "upload_button";
  this.upload.setAttribute("value", "Upload");
  //this.upload.setAttribute("disabled", "");
	this.upload.setAttribute("disabled", "true");
  this.upload.onclick = function (e)
  {
    this.file_upload.Upload();
  };
  this.upload.file_upload = this;
  this.form.appendChild(this.upload); 
  
  
  /* create the hidden input variables */
  {
    var v;

    for(v in this.input_vars)
      {
	var elem = doc.createElement("input");
	elem.setAttribute("type", "hidden");
	elem.setAttribute("name", v);
	elem.setAttribute("value", this.input_vars[v]);
	elem.style.display = "none";
	this.form.appendChild(elem);
      }
  }
  
  /* create the uploading message */
  
  
  
  this.uploading_msg = doc.createElement("div");
  this.uploading_msg.id = "uploading_" + id;
  this.uploading_msg.className = "uploading_msg";
  this.uploading_msg.style.display = 'none';
  this.uploading_msg.style.border = "none"; // sample to change border width
  //this.uploading_msg.style.width = '50%';
  //this.uploading_msg.style.height = "190px";
  this.uploading_msg.style.padding = "5px";
  this.uploading_msg.style.fontSize = "small";
  this.uploading_msg.style.zIndex = "5";
  this.uploading_msg.style.position = "absolute";
  this.uploading_msg.style.top = "33%";
  this.uploading_msg.style.color = "#000033";
  this.uploading_msg.style.backgroundColor = "#FFFFFF";
  this.uploading_msg.style.left = "25%";
  this.uploading_msg.style.textAlign = "center";
  this.uploading_msg.innerHTML = "<div style='border: 1px solid black; background-color: white;'><strong><font size='+3'>Transferring...Please Wait</font></strong><br><img src=\"/images/loaders/uploading_file.gif\" border=\"0\"></div>";
  this.uploading_msg.file_upload = this;
  this.main.appendChild(this.uploading_msg);
  
}

/*
 * upload_file_odd
 * upload_file_even
 * upload_file
 * upload_remove
 * upload_main
 * upload_button
 */

