// JavaScript Document

(function() {
	get();		
})();


function get(onload) {
	url = getUrl();
	var xhr = new XMLHttpRequest();
	if ("withCredentials" in xhr){
    	xhr.open("get", url, true);
  	} else if (typeof XDomainRequest != "undefined"){
    	xhr = new XDomainRequest();
    	xhr.open("get", url);
  	} else {
 		xhr = null;
  	}
  	if (xhr) {
    	xhr.onload = function() { onload(xhr); }
    	xhr.send();
  	}
  	return xhr;
}

function getUrl() {

	var code = "";
	var url;
	var voucherCode = "";
	var  sessionId = 0;

	if(querySt("r") == null) 
	{
		if(getCookie("round") != "undefined") {
			code = getCookie("round");
		}
		if(getCookie("voucher") != "undefined") {
			voucherCode = getCookie("voucher");
		}
		if(getCookie("sessionId") != "undefined") {
			sessionId = getCookie("sessionId");
		}
	} else 
	{
		var round = querySt("r");
		setCookie("round", round, 1);
		code = round;
		var newDate = new Date;

		sessionId = newDate.getTime();
		setCookie("sessionId ", sessionId, 1);
		if(querySt("v") == null) {
			voucherCode = "";
		} else {
			var voucher = querySt("v");
			setCookie("voucher", voucher, 1);
			voucherCode = voucher;
		}

	}

	if(code != null && code != "") {
		if(voucherCode == "") {
			url = 'http://delivery.email-analysis.com/LogVisit.aspx?r=' + code + '&s=' + sessionId + '&p=' + window.location.href;
		} else {
			url = 'http://delivery.email-analysis.com/LogVisit.aspx?r=' + code + '&v=' + voucherCode + '&s=' + sessionId + '&p=' + window.location.href;
		}
		return encodeURI(url);
	} else {
		return null;
	}

}

function loadXMLDoc(url, params)
{
  if(window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  } else if(window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        req = false;
      }
    }
  }
  if(req) {
    req.open("GET", url + '?' + params, true);
    req.send(null);
    return true;
  }
  return false;
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

function setCode() {
	var code = querySt("r");
	setCookie("round", code, 1);
}

function showCode() {
	var code = getCookie("round");
	alert('Cookie is : ' + code);	
}

function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			return unescape(y);
		}
	}
}

	
