var edGeoIPdetect = function (referrerList, defaultMode, currentLang, ajaxURL) {
	
	var 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;
	}

	var edGeoIPLang = readCookie('edGeoIPLang');

		// 1. Check if saved language (from cookie) is the current language of the page
	if (edGeoIPLang!=currentLang) {

			// 2. Determine the detection mode for the referrer (can be 'ignore' or 'detect')
		var referrerDomain = '';
		if (document.referrer != '') {
			var rArr = document.referrer.split('/');
			var domainPortArr = (rArr.length>1?rArr[2]:'').split(':');
			referrerDomain = domainPortArr[0];
		} 

		var detectionMode = '';
		for (var i=0; i < referrerList.length; i++) {
			for (var j=0; j < referrerList[i].list.length; j++) {
				if (referrerList[i].list[j]==referrerDomain) {
					detectionMode = referrerList[i].mode;
					break;
				}
			}
			if (detectionMode!='') {
				break;
			}
		}
		if (detectionMode=='') {
			detectionMode=defaultMode;
		}
		
			// 3. If detection  mode is 'detect', temporary blank the page and call ajax script
		if (detectionMode=='detect') {
			var body = document.getElementsByTagName("body")[0];
			body.style.display = 'none';
				// old code, include JS
			// document.write(unescape("%3Cscript src='" + ajaxURL + "' type='text/javascript'%3E%3C/script%3E"));
			
			try {
					// new code, use synchronous "AJAX" call (or better name is SJAX :D)
				var xhr;
				if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
					xhr = new XMLHttpRequest();
				} else { // code for IE6, IE5
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
				xhr.open("POST", ajaxURL, false);

				var tstamp = new Date().getTime();
				var params = "tstamp=" + tstamp;
					//Send the proper header information along with the request
				xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xhr.setRequestHeader("Content-length", params.length);
				xhr.setRequestHeader("Connection", "close");

				xhr.send(params);			
					// IE error sometimes returns 1223 when it should be 204 so treat it as success
				if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 || xhr.status === 1223) {
					document.write(unescape("%3Cscript type=\"text/javascript\"%3E"+xhr.responseText+"%3C/script%3E"));
				} else {
						// failed loading redirect data, just show the current page
					var body = document.getElementsByTagName("body")[0];
					body.style.display = 'block';
				}
			} catch(xhrError) {
					// failed loading redirect data, just show the current page
				var body = document.getElementsByTagName("body")[0];
				body.style.display = 'block';
			}

		}
	}
};

var edGeoIPprocess = function (redirectURL, setCookieLang) {
	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=/";
	}
	
	if (setCookieLang>=0) {
		createCookie('edGeoIPLang', setCookieLang, 3650);
	}
	
	if (redirectURL!='') {
		window.location.href = redirectURL;
	} else {
		var body = document.getElementsByTagName("body")[0];
		body.style.display = 'block';
	}
};
