function QueryString() {
	var data = [];
	this.Read = function() 
	{
		var aPairs, aTmp;
		var queryString = new String(window.location.search);
		queryString = queryString.substr(1, queryString.length); //remove "?"
		aPairs = queryString.split("&");	
		
		for (var i=0 ; i<aPairs.length; i++)
		{
			aTmp = aPairs[i].split("=");
			data[aTmp[0]] = aTmp[1];
		}
	}
	
	this.GetValue = function( key )
	{
		return data[key];
	}
	this.SetValue = function( key, value )
	{
		if (value == null)
			delete data[key];
		else 
			data[key] = value;
	}
	this.ToString = function()
	{
		var queryString = new String(""); 
		
		for (var key in data)
		{
			if (queryString != "")
				queryString += "&"
			if (data[key])
				queryString += key + "=" + data[key];		
		}
		if (queryString.length > 0)
			return "?" + queryString;
		else
			return queryString;
	}
	this.Clear = function()
	{
		delete data;
		data = [];
	}
}

function Cookies() {
	this.cookieData = [];
	this.Read = function()
	{
		var pairs = new String(window.document.cookie).split(";");	
		var tmp, cookieName, keyName;
		for (var i=0 ; i<pairs.length; i++)
		{
			tmp = pairs[i].split("=");
			
			if (tmp.length == 3)
			{
				cookieName = new String(tmp[0]);
				cookieName = cookieName.replace(" ", "");
				
				if (this.cookieData[cookieName] == null || this.cookieData[cookieName] == undefined)
					this.cookieData[cookieName] = [];
				this.cookieData[cookieName][tmp[1]] = unescape(tmp[1]);
			}
			else //length = 2
			{
				keyName = tmp[0];
				keyName = keyName.replace(" ", "");
				if (keyName.substring(0,12)!="ASPSESSIONID") 
				{
					if (this.cookieData[""] == null)
						this.cookieData[""] = [];
					this.cookieData[""][keyName] = unescape(tmp[1]);
				}
			}	
		}	
		
	}
	
	this.GetValue = function( cookie, key )
	{
		if (this.cookieData[cookie] != null)
			return this.cookieData[cookie][key];
		else
			return null;
	}
	this.SetValue = function( cookie, key, value )
	{
		if (this.cookieData[cookie] == null || this.cookieData[cookie] == undefined)
			this.cookieData[cookie] = [];
		this.cookieData[cookie][key] = value;
	}
	this.Write = function()
	{
	
		var toWrite;
		var thisCookie;
		var expireDateKill = new Date();
		var expireDate = new Date();
		expireDate.setYear(expireDate.getFullYear() + 10);
		expireDateKill.setYear(expireDateKill.getFullYear() - 10);


		var pairs = new String(window.document.cookie).split(";");	
		var tmp, cookieName, keyName;
		for (var i=0 ; i<pairs.length; i++)
		{
			tmp = pairs[i].split("=");
			if (tmp.length == 3)	
			{		
				window.document.cookie = tmp[0] + "=" + tmp[1] + "='';expires=" + expireDateKill.toGMTString();
			}
			else
			{
				keyName = tmp[0];
				keyName = keyName.replace(" ", "");
				if (keyName.substring(0,12)!="ASPSESSIONID") 
					window.document.cookie = keyName + "='';expires=" + expireDateKill.toGMTString();
			}
		}

		for (var cookie in this.cookieData)
		{
			toWrite = "";
			thisCookie = this.cookieData[cookie];
			for (var key in thisCookie)
			{
				if (thisCookie[key] != null)
				{
					toWrite += key + "=" + escape(thisCookie[key]) + "&";
				}
			}
			if(cookie == "")
				toWrite = (toWrite.length > 2 ? toWrite.substring(0, toWrite.length - 2) + "; " : toWrite) + "expires=" + expireDate.toGMTString();
			else{
				toWrite = (toWrite.length > 2 ? toWrite.substring(0, toWrite.length - 2) + "; " : toWrite) + "expires=" + expireDate.toGMTString() + (cookie.toLowerCase() == "unionhome" ? "; path=/" : "");
				toWrite = cookie + "=" + toWrite;
			}
			window.document.cookie = toWrite;	
		}
	}
}

function addRemoteJavascript(remoteJs){
    var head = document.getElementsByTagName('head')[0];
    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', remoteJs);
    head.appendChild(js);
}

var qstring = new QueryString();
var uhCookie = new Cookies();
qstring.Read();
uhCookie.Read();

// uhAId, uhPId, uhCId, uhVId, uhKey
var uhAid = qstring.GetValue("a") || "";
var uhPid = qstring.GetValue("p") || "";
var uhCid = qstring.GetValue("c") || "";
var uhVid = qstring.GetValue("v") || "";
var uhKey = qstring.GetValue("uh") || "";

if(uhKey.toLowerCase() == "yes"){
    // user come from UnionHome
    //1. change current user cookies.
    // Attention: this cookie only reserve one ad info
    uhCookie.SetValue("UnionHome", "a", uhAid);
    uhCookie.SetValue("UnionHome", "p", uhPid);
    uhCookie.SetValue("UnionHome", "c", uhCid);
    uhCookie.SetValue("UnionHome", "v", uhVid);
    uhCookie.Write();
    addRemoteJavascript("http://www.ezunion.net/c.ashx?a=" + uhAid + "&p=" + uhPid + "&c=" + uhCid + "&v=" + uhVid);
}
