window.onload=checkCookie;

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end==-1) {
				c_end=document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function setCookie(c_name, value, expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name+ "=" +value+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie() {
	var querystring = window.location.querystring;
	var aff = getCookie('affiliate');
	
	if (aff!=null && aff!="") {
		//cookie exists
	} else {
		//cookie does not exist
		if (querystring['idAff']!==undefined && querystring['idAff']!=null) {
			setCookie('affiliate', 'idAff='+querystring['idAff'], 14);
		}
	}
}

window.location.querystring = (function() {
    // by Chris O'Brien, prettycode.org
    var collection = {};
 
    // Gets the query string, starts with '?'
    var querystring = window.location.search;
 
    // Empty if no query string
    if (!querystring) {
        return { toString: function() { return ""; } };
    }
 
    // Decode query string and remove '?'
    querystring = decodeURI(querystring.substring(1));

	// Load the key/values of the return collection
    var pairs = querystring.split("&");
 
    for (var i = 0; i < pairs.length; i++) {
        // Empty pair (e.g. ?key=val&&key2=val2)
        if (!pairs[i]) {
            continue;
        }
 
        // Don't use split("=") in case value has "=" in it
        var seperatorPosition = pairs[i].indexOf("=");
 
        if (seperatorPosition == -1) {
            collection[pairs[i]] = "";
        } else {
            collection[pairs[i].substring(0, seperatorPosition)] = pairs[i].substr(seperatorPosition + 1);
        }
    }
 
    // toString() returns the key/value pairs concatenated
    collection.toString = function() {
        return "?" + querystring;
    };
 
    return collection;
})();
