
function DROdioTracking() {
	
	var This = this;
	var landing;
	var steps;
	var path;
	var referrer;

	this.init = function() {
		var url_referral = This.readCookie('external_referrer');
		
		if (url_referral == null) {
			This.initCookie(0);
		} else {
			var ref_url = document.referrer;
			var curr_url = document.domain;
			
			if (1 < ref_url.indexOf(curr_url)) {
				This.steps = This.readCookie('total_steps');
				This.steps++;
				This.createCookie('total_steps', This.steps, 1);
				
				This.path = This.readCookie('path');
				This.path = This.path + ' => ' + window.location.href;
				This.createCookie('path', This.path);
				
				This.referrer = This.readCookie('external_referrer');
				This.landing = This.readCookie('landing_page');
			} else {
				this.initCookies(1);
			}
		}
	} //init
	
	this.initCookie = function(delCookie) {
		if (delCookie == 1) {
			This.eraseCookie('external_referrer');
			This.eraseCookie('landing_page');
			This.eraseCookie('path');
			This.eraseCookie('total_steps');
		}

		This.referrer = document.referrer;
		This.landing = window.location.href;
		This.steps = 1;
		
		if (This.referrer.length > 0) {
			This.path = This.referrer + ' => ' + This.landing;
		} else {
			This.path = 'Direct Address Input => ' + This.landing;
		}

		This.createCookie('external_referrer', This.referrer, 1);
		This.createCookie('landing_page', This.landing, 1);
		This.createCookie('total_steps', This.steps, 1);
		This.createCookie('path', This.path);
	} //initCookie
	
	this.createCookie = function(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}
	} //createCookie
	
	this.readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		
		for (var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		
		return null;
	} //readCookie
	
	this.eraseCookie = function(name) {
		This.createCookie(name, "", -1);
	} //eraseCookie
	
	this.initForm = function(form_id) {
		formObj = document.getElementById(form_id);
		formObj.onsubmit = function() {
			var input_external_referrer = document.createElement('input');
			input_external_referrer.setAttribute('type', 'hidden');
			input_external_referrer.setAttribute('name', 'external_referrer');
			input_external_referrer.setAttribute('value', This.referrer);
			formObj.appendChild(input_external_referrer);
			
			var input_landing_page = document.createElement('input');
			input_landing_page.setAttribute('type', 'hidden');
			input_landing_page.setAttribute('name', 'landing_page');
			input_landing_page.setAttribute('value', This.landing);
			formObj.appendChild(input_landing_page);
			
			var input_total_steps = document.createElement('input');
			input_total_steps.setAttribute('type', 'hidden');
			input_total_steps.setAttribute('name', 'total_steps');
			input_total_steps.setAttribute('value', This.steps);
			formObj.appendChild(input_total_steps);
			
			var input_path = document.createElement('input');
			input_path.setAttribute('type', 'hidden');
			input_path.setAttribute('name', 'path');
			input_path.setAttribute('value', This.path);
			formObj.appendChild(input_path);
				
			alert(formObj.innerHTML);
			
			return false;
		}
	}

}

var drodio_tracking = new DROdioTracking();
drodio_tracking.init();

