/* 
==============================
seedfundo
==============================
This Javascript cucumber was pickled by:
>> Robin Wallar at Fathom Solutions
>> www.fathomsolutions.ca | robin [at] fathomsolutions.ca
------------------------------
Written with massive mits on while scraping ice of the screen
of my icicle that serves as a monitor. 
------------------------------

------------------------------
Feel free to use this to educate
yourself on the wonders of javascript.
If you plan on copying something directly please contact us. 
------------------------------
*/
/***************************************************************************************************************************************************************************
AJAX FUNCTIONS START
***************************************************************************************************************************************************************************/
var http_request = false;
function makePOSTRequest(url, parameters, span)
{
	http_request = false;
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	 	if (http_request.overrideMimeType)
	 	{
			// set type accordingly to anticipated content type
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
  
	if (!http_request)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}
  
	http_request.onreadystatechange = function()
	{	
	
		/*readyState values *
		0	The request is not initialized
		1	The request has been set up
		2	The request has been sent
		3	The request is in process
		4	The request is complete */
		
		if(http_request.readyState == 4)
		{
			if (http_request.status == 200)
			{
				var response = http_request.responseText;
				document.getElementById(span).innerHTML = response;
			}
		}				
	}		
	
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

/* Begin second ajax function. This create the ajax http object */
function createRequestObject()
{
    var tmpXmlHttpObject;
    
	/*Set up browser support*/
	if (window.XMLHttpRequest)
	{ 
        /* Mozilla, Safari */
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } else if (window.ActiveXObject) { 
        /* IE */
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return tmpXmlHttpObject;
}

/*create the xmlHttp object */
var http = createRequestObject();
	
function ajax(url, span)
{
	document.getElementById(span).innerHTML = '<div style="align:center; width:100%;"><img src="images/ajax-loader.gif"></div>';
	setTimeout('', 1000);

	http.open('get', url);
	http.onreadystatechange = function()
	{	
	
		/*readyState values *
		0	The request is not initialized
		1	The request has been set up
		2	The request has been sent
		3	The request is in process
		4	The request is complete */
		
		if(http.readyState == 4)
		{
			var response = http.responseText;
			document.getElementById(span).innerHTML = response;
		}				
	}		
	http.send(null);
}
/*end second ajax function */
/***************************************************************************************************************************************************************************
AJAX FUNCTIONS END
***************************************************************************************************************************************************************************/

/***************************************************************************************************************************************************************************
Other Javascript FUNCTIONS START
***************************************************************************************************************************************************************************/
function sfd_confirm_do()
{
	var confirmed = confirm("Are you sure you want to do that?");
	if(confirmed)
	{
		return true;
	}else{
		return false;
	}
}

/*File listing stuff*/
var sfd_file_field="sfd_file_list";
var sfd_file_id;

function sfd_file_list_close()
{
	document.getElementById(this.sfd_file_id).innerHTML = "";
}

function sfd_file_list(url, field, span)
{
	this.sfd_file_field=field;
	this.sfd_file_id = span;
	ajax(url, span);
}
/*ende file listing stuff*/
/*delete*/
function sfd_delete(file)
{
	var confirmed = confirm("Are you sure you want delete that picture?");
	if(confirmed)
	{
		ajax('seefun_delete.php?file=' + file, 'sfd_status');
		document.getElementById('sfd_file_list_loader').innerHTML ="";
	}
}

/*use this to assign file to the img field*/
function sfd_assign_file_field(input)
{
	this.sfd_file_field.value = this.sfd_dir + input;	/*append the path*/
	document.getElementById(this.sfd_file_id).innerHTML = "";
	ImageDialog.showPreviewImage(this.sfd_dir + input);
}

/*HOVERING*/
function sfd_hover(id, file)
{
	document.getElementById(id).style.background='#CCCCCC';
	document.getElementById('sfd_preview').innerHTML='<img src=" ' + this.sfd_dir_rel + file + '" width="50px">';
	
}
function sfd_hover_off(id, color)
{
	document.getElementById(id).style.background=color;
	document.getElementById('sfd_preview').innerHTML='';
}
/*END HOVERING*/


/*this is used to reload in places where i don't want to pass all the parameters but i can't embed it in js.*/
function sfd_file_list_reload()
{
	sfd_file_list('seefun_file_list.php?page=' + this.sfd_dir, document.imgform.src, 'sfd_file_list_loader');
}

/*make sure a file is being uploaded*/
function sfd_validate_upload()
{
	if(document.sfd_uploader.file.value=="")
	{
		alert("Please select a file");
		return false;
	}else{
		return true;
	}
}