//This function takes two paramaters: form (the ID of the form), and sid, which 
//differentiates the different uploads from each other.
function UploadAvatar(source,form,sid,error,target)
{
	if(CheckFileToUpload(source,'gif,jpg,jpeg,png',form, sid,error,target))
	{	
		switch(source)
		{
			case "upload_avatar_path":
				document.getElementById('close_avatar').style.display = 'none';
				document.getElementById("avatar_box_button").disabled = true;
				document.getElementById("darkBackgroundLayer").style.display="none";
				document.getElementById("darkBackgroundLayer").counter=0;
			break;
			case "new_listing_picture_path":
				document.getElementById("submit_new_listing_button").disabled = true;
				document.getElementById("upload_cancel").style.display = "inline";
			break;
			case "edit_forsale_picture_picture_path":
				document.getElementById("submit_forsale_edit_button").disabled = true;
			break;
		}
		//get the form, then submit it.
		var theForm = $(form);
		theForm.submit();
		var ele;
		// The variable request is an object that will contain details of the request.
		var request = {};
		var fileName = '';
 
		// This little bit loops through all the elements in the form, locates the
		// file input field, and extracts the file name of the file being uploaded.
		for (var i=0; i < theForm.elements.length; i++)
		{ 
			ele = theForm.elements[i];
			if (ele.type == 'file')
			{
				var fileName = ele.value;
    			if (fileName.indexOf('/') > -1)
				{ 
    				fileName = fileName.substring(fileName.lastIndexOf('/')+1, fileName.length);
				}
				else
				{
    				fileName = fileName.substring(fileName.lastIndexOf('\\')+1, fileName.length);
				}
				//Since there can only be one file per form for this script, we'll exit the loop here. 
				break;
			}
		}
    
		//These three parts of 'request' are required in order for the filestatus server-side script to work.
		request.sid = sid;
		request.fileName = fileName;
		request.iframe = theForm.target;
  
		// I hope this is self explanatory, but if not, this will send a JSON request to filestatus.php
		// every 1.5 seconds.  repeatGetAction is in SendRecieve.js or sr_c.js.  If a single call is
		// successful, the successFunc will be called. Otherwise, failFunc will be called.
		
		document.getElementById(source).disabled = true;
		repeater = new RepeatGetAction('ll/filestatus.php', request, 1500);
		if(document.getElementById("upload_cancel"))
		{
		  document.getElementById("upload_cancel").onclick=function(){
		    request.progress = 100;
        repeater.stop();
        $(request.iframe).src = 'blank.html';
        document.getElementById("submit_new_listing_button").disabled = false;
        document.getElementById("upload_cancel").style.display = "none";
        document.getElementById('listing_upload_progress').style.visibility='hidden';
			  document.getElementById('new_listing_picture_path').disabled = false;
			  document.getElementById('new_listing_picture_path').value='';
      };
    }
		repeater.successFunc = function (getBack) {
			if (getBack.progress >= 100 || getBack.progress=='done')
			{
				getBack.progress = 100;
				this.stop();
				switch(source)
				{
					case "upload_avatar_path":
						document.getElementById('close_avatar').style.display = 'block';
						document.getElementById("avatar_box_button").disabled = false;
						ResizeAvatar(fileName);
					break;
					case "new_listing_picture_path":
					  document.getElementById("upload_cancel").style.display = "none";
					  document.getElementById("submit_new_listing_button").disabled = false;
						ResizePicture(fileName,document.getElementById("new_listing_picture_path").name);
					break;
					case "edit_forsale_picture_picture_path":
					  document.getElementById("submit_forsale_edit_button").disabled = false;
					  ResizeEditPicture(fileName);
					break;
				}
			}
			$(getBack.sid + '_progress').style.width = getBack.progress + '%';
			$(getBack.sid + '_fileName').innerHTML = fileName;
		}; 
		repeater.failFunc = function (getBack) {
			this.stop();
			$(getBack.iframe).src = 'blank.html';
			alert(getBack.error_msg);
			switch(source)
			{
				case "upload_avatar_path":
				  document.getElementById("avatar_box_button").disabled = false;
					show_hide_div('upload_avatar_box');
				break;
				case "new_listing_picture_path":
				  document.getElementById("upload_cancel").style.display = "none";
					document.getElementById('new_listing_picture_path').disabled = false;
					document.getElementById("submit_new_listing_button").disabled = false;
					document.getElementById('new_listing_picture_path').value='';
				break;
				case "edit_forsale_picture_picture_path":
					document.getElementById('edit_forsale_picture_picture_path').disabled = false;
					document.getElementById("submit_forsale_edit_button").disabled = false;
					document.getElementById('edit_forsale_picture_picture_path').value='';
				break;
			}
		};
  
		// This MUST be called before the action will start.  When the repeater has served its 
		// purpose (or you get sick of it), you can call repeater.stop() to stop it.
		repeater.start();
	}
}

function ConfirmAvatar(uid,previewpath,avatarpath)
{
	show_hide_div('upload_avatar_box');
	document.getElementById('notImage').style.display = 'none';
	document.getElementById('avatar_preview').style.display = 'none';
	var fullpath = document.getElementById('preview_picture').src;
	fileName = fullpath.slice(previewpath.length);
	document.getElementById('preview_picture').src = previewpath;
	document.getElementById('upload_avatar_path').value='';
	document.getElementById('upload_avatar_path').disabled = false;
	document.getElementById('user_avatar').src = avatarpath;
	var url = "av_install.php?f=" + fileName + "&uid=" + uid ;
	request.open("GET", url, true);
	request.onreadystatechange = DisplayNewAvatar;
	request.send(null);
}

function PictureTimeout(filename, target)
{
	document.getElementById(target).src = document.getElementById(target).src + filename;
	if(document.getElementById("upload_avatar_box"))
	{
	  if(document.getElementById("upload_avatar_box").style.display=="block")
	  {
      setTimeout("resizeIFrame(document.getElementById('upload_avatar_box'));",500);
    }
  }
}

function DisplayNewAvatar()
{
	if (request.readyState == 4)
	{
	    if (request.status == 200)
		{
			setTimeout('PictureTimeout(request.responseText,"user_avatar")',100);
			document.getElementById('user_avatar').style.display = 'inline';
		}
	}
}
function ResetAvatarBox(uid,previewpath)
{
	document.getElementById('notImage').style.display = "none";
	document.getElementById('avatar_preview').style.display = "none";
	var fullpath = document.getElementById('preview_picture').src;
	if(fullpath != previewpath)
	{
		fileName = fullpath.slice(previewpath.length);
		var url = "av_cancel.php?f=" + fileName + "&uid=" + uid ;
		request.open("GET", url, true);
		request.onreadystatechange = nothing;
		request.send(null);
		document.getElementById('preview_picture').src = previewpath;
	}
	document.getElementById('upload_avatar_path').value='';
	document.getElementById('upload_avatar_path').disabled = false;
}

function ResetPicturePreview(previewpath)
{
	document.getElementById('notImage_listing').style.display = "none";
	document.getElementById('listing_picture').style.display = "none";
	document.getElementById('listing_preview_picture').src = previewpath;
}

function ResetEditPicturePreview(previewpath)
{
	document.getElementById('notImage_edit_forsale_picture').style.display = "none";
	document.getElementById('edit_forsale_picture').style.display = "none";
	document.getElementById('edit_forsale_preview_picture').src = previewpath;
}

function ResizeAvatar(filename)
{
	var url = "av_preview.php?f=" + filename;
	request.open("GET", url, true);
	request.onreadystatechange = ShowTempAvatar;
	request.send(null);
}

function ShowTempAvatar() 
{
	if (request.readyState == 4)
	{
	    if (request.status == 200)
		{	
			setTimeout('PictureTimeout(request.responseText,"preview_picture")',100);
			document.getElementById('avatar_upload_progress').style.display='none';
			document.getElementById('upload_avatar_path').value='';
			document.getElementById('avatar_preview').style.display='block';
		}
	}
}

function ResizeEditPicture(filename)
{
	var url = "photo-preview.php?f=" + filename;
	request.open("GET", url, true);
	request.onreadystatechange = ShowTempEditPicture;
	request.send(null);
}

function ResizePicture(filename, name)
{
	var url = "photo-preview.php?f=" + filename;
	request.open("GET", url, true);
	if (name == "new_object_picture_path")
	{
    request.onreadystatechange = ShowTempHalfPicture;
  }
  else
  {
    request.onreadystatechange = ShowTempPicture;
  }
	request.send(null);
}

function ShowTempPicture() 
{
	if (request.readyState == 4)
	{
	    if (request.status == 200)
		{
			var picture = request.responseText;
			document.getElementById('listing_upload_progress').style.visibility='hidden';
			document.getElementById('new_listing_picture_path').disabled = false;
			document.getElementById('new_listing_picture_path').value='';
			document.getElementById('listing_preview_picture').src = document.getElementById('listing_preview_picture').src + picture;
			document.getElementById('listing_preview_picture').title = picture;
			document.getElementById('listing_picture').style.display ='block';
		}
	}
}

function ShowTempHalfPicture() 
{
	if (request.readyState == 4)
	{
	    if (request.status == 200)
		{
			var picture = request.responseText;
      document.getElementById('listing_preview_picture').style.visibility ='hidden';
			document.getElementById('listing_picture').style.display ='block';	
			document.getElementById('listing_upload_progress').style.visibility='hidden';
			document.getElementById('new_listing_picture_path').disabled = false;
			document.getElementById('new_listing_picture_path').value='';
			document.getElementById('listing_preview_picture').title = picture;
			//document.getElementById('listing_preview_picture').src = document.getElementById('listing_preview_picture').src + picture;
			setTimeout('PictureTimeout(request.responseText,"listing_preview_picture")',200);
			setTimeout('reducePictureHalfSize();',300);
			
		}
	}
}

function reducePictureHalfSize()
{
    document.getElementById("listing_preview_picture").style.width="auto";
    document.getElementById("listing_preview_picture").style.height="auto";
    document.getElementById("listing_preview_picture").style.width=parseFloat(document.getElementById("listing_preview_picture").offsetWidth - 2)/2 + "px";
    document.getElementById("listing_preview_picture").style.height=parseFloat(document.getElementById("listing_preview_picture").offsetHeight - 2 ) + "px";
    document.getElementById('listing_preview_picture').style.visibility ='visible';
}

function ShowTempEditPicture() 
{
	if (request.readyState == 4)
	{
	    if (request.status == 200)
		{
			var picture = request.responseText;
			document.getElementById('edit_forsale_picture_upload_progress').style.visibility='hidden';
			document.getElementById('edit_forsale_picture_picture_path').disabled = false;
			document.getElementById('edit_forsale_picture_picture_path').value='';
			document.getElementById('edit_forsale_preview_picture').src = document.getElementById('edit_forsale_preview_picture').src + picture;
			document.getElementById('edit_forsale_preview_picture').title = picture;
			document.getElementById('edit_forsale_picture').style.display ='block';
			setTimeout("resizeIFrame(document.getElementById('edit_forsale_box'));",200);
		}
	}
}

function nothing(){}
