/***************************************************************************************************
* DFF_Tools                                                                                        *
***************************************************************************************************/
function DFF_Tools(params) {
	/***************************************************************************************************
	* Configurations and Parameters                                                                    *
	***************************************************************************************************/
	this.DebugStatus = (params.DebugStatus?params.DebugStatus:false);
	this.DebugBoxId = (params.DebugBoxId?params.DebugBoxId:'debug');
	this.ServerUri = (params.ServerUri?params.ServerUri:'');
	this.ServerBaseUri = (params.ServerBaseUri?params.ServerBaseUri:'./');
	
	/***************************************************************************************************
	* Commonly used methods                                                                            *
	***************************************************************************************************/
	this.LoadSwf = function (filename, element_id, width, height, version) {
		if (typeof SWFObject == 'undefined') {
			if (this.DebugStatus) {
				alert('Please check you have included the swfobject.js file');
			}
			return false;
		}
		
		// Default values
		if (!width) { width = 400; }
		if (!height) { height = 250; }
		if (!version) { version = '6'; }
		
		this.AttachOnload(function () {
			var so = new SWFObject(filename+"?rnd="+Math.random(), "f_"+element_id, width, height, version);
			so.write(document.getElementById(element_id));
		});
		
		return true;
	};
	
	this.NewWindow = function (url, width, height, top, left, location, menubar, resizable, scrollbars, target, focus_window, blur_window, on_event) {
		// Default values
		if (null == url) { return false; }
		if (null == width) { width = 800; }
		if (null == height) { height = 600; }
		if (null == top) { top = 0; }
		if (null == left) { left = 0; }
		if (null == location) { location = 'no'; }
		if (null == menubar) { menubar = 'no'; }
		if (null == resizable) { resizable = 'yes'; }
		if (null == scrollbars) { scrollbars = 'yes'; }
		if (null == target) { target = '_blank'; }
		if (null == focus_window) { focus_window = false; }
		if (null == blur_window) { blur_window = false; }
		if (null == on_event) { on_event = false; } // Can be onload|onunload
		
		var params = new Array();
		params.push('width='+width);
		params.push('height='+height);
		params.push('top='+top);
		params.push('left='+left);
		params.push('location='+location);
		params.push('menubar='+menubar);
		params.push('resizable='+resizable);
		params.push('scrollbars='+scrollbars);
		
		var execute = function () {
			var popup = window.open(url, target, params.join(','));
			
			if (popup) {
				if (focus_window) {
					popup.focus();
				} else if (blur_window) {
					popup.blur();
				}
				return popup;
			} else {
				return false;
			}
		};
		
		switch (on_event) {
			case 'onload':
				this.AttachOnload(execute);
				break;
			case 'onunload':
				this.AttachOnunload(execute);
				break;
			case false:
			default:
				execute();
				if (window.event) { window.event.cancelBubble = true; }
				return false;
		}
	};
	
	// If a field is set to a specific value, clear it (usually when it takes focus)
	this.FieldOnfocus = function (element, setval) {
		if (element.value == setval) {
			element.value = '';
		}
		return true;
	}

	// If a field is blank (usually when it loses focus) populate it with a specific value.
	this.FieldOnblur = function (element, setval) {
		if (element.value == '') {
			element.value = setval;	
		}
		return true;
	}
	
	/***************************************************************************************************
	* Mouse tracking functions                                                                         *
	***************************************************************************************************/
	this.mouseDownStatus = false;
	
	this.startMouseTracking = function () {
		document.onmousedown = this.mouseDown;
		document.onmouseup = this.mouseUp;
	}
	
	this.mouseDown = function () {
		this.mouseDownStatus = true;
	}
	
	this.mouseUp = function () {
		this.mouseDownStatus = false;
	}
	
	this.mouseIsDown = function () {
		return (this.mouseDownStatus == true);
	}
	
	this.mouseIsUp = function () {
		return (this.mouseDownStatus == false);
	}
	
	/***************************************************************************************************
	* Ajax triggered functions                                                                         *
	***************************************************************************************************/
	this.DebugDisplay = function (request, instance) {
		if (request.readyState == 4) {
			if (instance.DebugStatus) {
				var box = document.getElementById(this.DebugBoxId);
				if (box) {
					instance.DebugAddMessage(box, 'Modified ID: '+request.responseText);
				} else {
					alert(request.responseText);
				}
			}
		}
	};
	
	this.DebugAddMessage = function (box, message) {
		if (!box) { return false; }
		
		// Set some styles
		if (box.innerHTML == '') {
			box.style.display = 'block';
			box.style.border = '1px solid #CCCCCC';
			box.style.padding = '15px';
			box.style.fontFamily = 'Courier New';
			box.style.fontSize = '12px';
		}
		
		box.innerHTML+= message+'<br />';
	}
	
	/***************************************************************************************************
	* Miscellaneous functions                                                                          *
	***************************************************************************************************/
	this.GetCookie = function (id, default_value) {
		if (!default_value) { default_value = null }
		var re          = new RegExp(id+'=(.*)');
		var value       = re.exec(document.cookie);
		return (value) ? value[1].split(';')[0] : default_value;
	};
	
	this.SetCookie = function (id, value) {
		var now = new Date();
		var future = new Date();
		future.setTime(Date.parse(now) + 365*24*60*60*1000);
		expires = future.toUTCString();
		expires.replace(/(\d\d)\s(\w\w\w)\s\d\d(\d\d)/,"$1-$2-$3");
		
		document.cookie = id+'='+value+';path=/;expires='+expires;
	}
	
	this.GetReferrer = function () {
		if (this.referrer == null) {
			if (document.referrer && document.referrer != "") {
				this.referrer = document.referrer;
			} else {
				this.referrer = 'N/A';
			}
		}
		
		return this.referrer;
	};
	
	this.AttachOnload = function (new_function) {
		// Should only be using this during development
		if (typeof window.onload == 'function') {
			var old_onload = window.onload;
			window.onload = function () {
				old_onload();
				new_function();
			}
		} else {
			window.onload = function () {
				new_function();
			}
		}
	};
	
	this.AttachOnunload = function (new_function) {
		// Should only be using this during development
		if (typeof window.onunload == 'function') {
			var old_onunload = window.onunload;
			window.onunload = function () {
				old_onunload();
				new_function();
			}
		} else {
			window.onunload = function () {
				new_function();
			}
		}
	};
	
	/***************************************************************************************************
	* Prepare tracking request                                                                         *
	***************************************************************************************************/
	this.SendData = function (data, additional_process, serverUri) {
		data['rand'] = Math.random();
		
		var queryString = new Array()
		for (var key in data) {
			queryString.push(window.encodeURIComponent(key)+'='+window.encodeURIComponent(data[key]));
		}
		
		if (!serverUri) {
			serverUri = this.ServerUri;
		}
		
		return this.AjaxRequest(this.ServerBaseUri+serverUri+'?'+queryString.join('&'), additional_process);
	};
	
	/***************************************************************************************************
	* Ajax Request                                                                                     *
	***************************************************************************************************/
	this.AjaxRequest = function (url, additional_process) {
		//document.getElementById('DFF_tracker').src = url;
		//return;
		
		var request = false;
		if (window.XMLHttpRequest && !(window.ActiveXObject)) { // branch for native XMLHttpRequest object
			try {
				request = new XMLHttpRequest();
			} catch(e) {
				request = false;
			}
		} else if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					request = false;
				}
			}
		}
		
		if (request) {
			var that = this;
			request.onreadystatechange = function () {
				that.DebugDisplay(request, that);
				if (typeof additional_process == 'function') {
					additional_process(request, that);
				}
			};
			request.open("GET", url, true);
			request.send("");
		}
		
		return request;
	};
}
