window.onerror=null;
 
function createCookie(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 = "";
        
        var domains = window.location.host.split('.');
        var domain = (domains.length > 2)
                   ? "." + domains.slice(1).join('.')
                   : null;
        
        document.cookie = name+"="+value+expires+"; path=/;" + (domain ? ("domain=" + domain +";") : null);
}

function readCookie(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;
}

function deleteCookie (cookie_name)
{
  var cookie_date = new Date();  // current date & time
  cookie_date.setTime (cookie_date.getTime() - 1);
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function getMRC() {
	// look on URL, then read cookie
	// if found on URL, write to cookie before returning value
	var returnValue = null;
	returnValue = getQueryValue("marketingRefCode");
	if (returnValue == null) returnValue = getQueryValue("mrc");
	if (returnValue == null) returnValue = getQueryValue("s_kwcid");
	
   	// write to cookie, then return if we found an mrc on the URL
	if (returnValue != null) {
		createCookie('marketingRefCode', returnValue, 30);
	}
	else {
		// see if there is an MRC in a cookie
		returnValue = readCookie('marketingRefCode');
	}

	if (returnValue != null) return unescape(returnValue);
	else return null;
}

function getQueryValue(key) {
	// look on URL, then read cookie
	// if found on URL, write to cookie before returning value
	var returnValue = null;
	var q = window.location.search;
	if(q.length > 1) {
		q = q.substring(1, q.length);
		var keyValuePairs = new Array();
		for(var i=0; i<q.split("&").length; i++) {
			keyValuePairs[i] = q.split("&")[i];
		}
		for(var j=0; j < keyValuePairs.length; j++) {
			var nextKey = keyValuePairs[j].split("=")[0];
			if(nextKey == key) returnValue = keyValuePairs[j].split("=")[1];
		}

	}
	if (returnValue != null) return unescape(returnValue);
	else return null;
}

function getBPID() {
	// look on URL, then read cookie
	// if found on URL, write to cookie before returning value
	var returnValue = getQueryValue('bpID');
	
	// write to cookie, then return if we found it on the URL
	if (returnValue != null) {
		if (!isNaN (parseInt(returnValue)))	createCookie('bpID', returnValue, 30);
		else returnValue = null;
	}
	else {
		// see if there is an MRC in a cookie
		returnValue = readCookie('bpID');
	}
	if (returnValue != null) {
		return unescape(returnValue);
	}
	else return null;
}

function getEID() {
	// look on URL, then read cookie
	// if found on URL, write to cookie before returning value
	var returnValue = getQueryValue('eID');
	
	// write to cookie, then return if we found it on the URL
	if (returnValue != null) {
		if (!isNaN (parseInt(returnValue)))	createCookie('eID', returnValue, 30);
		else returnValue = null;
	}
	else {
		// see if there is an MRC in a cookie
		returnValue = readCookie('eID');
	}
	if (returnValue != null) return unescape(returnValue);
	else return null;
}

function writeGlobalBeacons(cat,revenue,ordernum,spnum) {
	if (ordernum.indexOf('SP') == 0) {
		document.write('<img src=\"https://whitefence.rsys1.net/servlet/cct?_a_=PM&_c_=ALL&ORDERID_=' + ordernum + '&NRF_=' + revenue + '&CAT_=' + cat + '&SCP_=' + spnum + '\" width=1 height=1>');
	}
}

function URLEncode(plaintext) {
	var SAFECHARS = "0123456789" +					
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";

	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) &0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}

	return encoded;
};
