function iMAWebCookie() {
	var webservicedomain = 'http://gw-services.vtrenz.net/';
	var cookieName = 'com.vtrenz.iMAWebCookie';
	var contactID = 0;
	var accesskey = '';
	var urlParams = {};
	var IFrameObj;

	this.main = function() {
		var webSyncID = readCookie(cookieName);
		
		//Check to see if cookie does not exists
		if(webSyncID == '')
			webSyncID = generateWebSyncID();
		
		//write cookie to keep it from expiring and ultimately losing the GUID
		if(webSyncID.length > 0)
			createCookie(cookieName,webSyncID,1000);
		else
			return false;
				
		//Get AccessKey
		getAccessKey();

		//Get URL Params
		getUrlParameters();

		//Register page visit
		registerPageVisit();
	}
	
	var generateWebSyncID = function() {
		var g = '';
		
		for(var i = 0; i < 32; i++)
			g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 7 || i == 11 || i == 15 || i == 19 ? "-" : "")

		return g;
	}
	
	var registerPageVisit = function() {
		var webSyncID = readCookie(cookieName);
		
		if(urlParams.contactid) contactID = urlParams.contactid;
		
		var params = 'webSyncID='+webSyncID+'&contactID='+contactID+'&accesskey='+accesskey+'&hostname='+location.hostname+'&pathname='+location.pathname;
		var url = webservicedomain + 'WebCookies/RegisterWebPageVisit.cfm/?' + params;
		postURL(url);
	}
	
	function postURL(URL) {
		if (!document.createElement) {return true};
		
		var IFrameDoc;
		
		if (!IFrameObj && document.createElement) {
			// create the IFrame and assign a reference to the
			// object to our global variable IFrameObj.
			// this will only happen the first time 
			// callToServer() is called
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
//		
//			if (document.frames) {
//				// this is for IE5 Mac, because it will only
//				// allow access to the document object
//				// of the IFrame if we access it through
//				// the document.frames array
//				IFrameObj = document.frames['RSIFrame'];
//			}
		}
//		
//		if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
//			// we have to give NS6 a fraction of a second
//			// to recognize the new IFrame
//			setTimeout('postURL(\''+URL+'\')',10);
//			return false;
//		}
//		
//		if (IFrameObj.contentDocument) {
//			// For NS6
//			IFrameDoc = IFrameObj.contentDocument; 
//		} else if (IFrameObj.contentWindow) {
//			// For IE5.5 and IE6
//			IFrameDoc = IFrameObj.contentWindow.document;
//		} else if (IFrameObj.document) {
//			// For IE5
//			IFrameDoc = IFrameObj.document;
//		} else {
//			return true;
//		}
//		
//		IFrameDoc.location.replace(URL);
//		return false;
	}
	
	var 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=/";
	}
	
	var eraseCookie = function(name) {
		createCookie(name, "", -1);
	}
	
	var readCookie = function(name) {
		var ca = document.cookie.split(';');
		var nameEQ = name + "=";
		
		for(var i=0; i < ca.length; i++) {
			var c = ca[i];

			while (c.charAt(0)==' ')
				c = c.substring(1, c.length); //delete spaces

			if (c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length, c.length);
		}
		
		return "";
	}
	
	var getAccessKey = function() {
		var scripttags = document.getElementsByTagName("script");
		for(var i=0; i<scripttags.length; i++) {
			if(scripttags[i].src && scripttags[i].src.match(/iMAWebCookie\.js(\?.*)$/i)) {
				accesskey = scripttags[i].src.replace(/^.*iMAWebCookie\.js\?(.*)$/i,'$1');
				return;
			}
		}
	}

	var getUrlParameters = function() {
		var params = window.location.search;
		params = params.substr(params.indexOf("?")+1).toLowerCase();
		
		var nameValuePairs = params.split("&");
		
		for(var i=0; i<nameValuePairs.length; i++) {
			var pair = nameValuePairs[i].split("=");
			urlParams[pair[0].toLowerCase()] = pair[1];
		}
	}
}
