//********************************************************************
//*-------------------------------------------------------------------
//* Affiliate handling functions
//* Created: 20040902
//*-------------------------------------------------------------------
//*

	var queryObj = parseQueryString(location.search);

	var debug = false;
	var siteID;
	
	if (queryObj.siteID != null) {
		siteID = queryObj.siteID;
	} else {
		siteID = null;
	}
	
	if(debug)alert("Site ID: " + siteID); // Site ID
	
	expireTime = new Date();
	var dateTime = "0000";
	var cpnDate = "";
	d = new Date();
	var year = d.getFullYear();
	var month = d.getMonth() + 1;
	var day = d.getDate();
	
	if (month.toString().length == 1) {
		month = "0" + month;
	}
	if (day.toString().length == 1) {
		day = "0" + day;
	}
	cpnDate = year + "" + month + "" + day;
	
	/** Debug statements */
	if(debug)alert("Coupon Date: " + cpnDate);
	
	/** If a siteID was passed in the request object */
	if (siteID != null) {
		//alert("Site ID exists");
		expireTime.setYear(expireTime.getFullYear() + 10);
		
		/** Get the hour, minute and second from the current date */
		var hour = d.getHours();
		var minute = d.getMinutes();
		var second = d.getSeconds();
		
		var tag = "";
		/** Determine whether the time is AM or PM */
		if (hour > 12) {
			hour = hour - 12;
			tag = "PM";
		} else {
			tag = "AM";
		}
		/** If the hour is a single digit pad it on the left with "0" */
		if (hour.toString().length == 1) {
			hour = "0" + hour;
		}
		/** Make sure that the hour isnt "00" */
		if (hour == "00") {
			hour = "12";
		}
		/** If the minute is a single digit pad it on the left with "0" */
		if (minute.toString().length == 1) {
			minute = "0" + minute;
		}
		/** If the second is a single digit pad it on the left with "0" */
		if (second.toString().length == 1) {
			second = "0" + second;
		}
		
		dateTime = month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + second + " " + tag;
	
		/** Set the cookies */
		setCookie("lsnsite", siteID, 3650);
		setCookie("lsndtm", dateTime, 3650);
		
		/** Debug statements */
		if(debug)alert("LSNSITE (with siteID): " + getCookie("lsnsite"));
		if(debug)alert("LSNDTM: (with siteID)" + getCookie("lsndtm"));
		
	/** A siteID was not passed in the request object */
	} else {
		//alert("Site ID does not exist");
		siteID = "EXPLODER";
		var sName = "lsnsite";
    	var dName = "lsndtm";
    	var tempDate = " ";
    	
    	/** Look for the "lsnsite" cookie */
		siteID = getCookie("lsnsite");
		if (siteID != null) {
			if ((siteID.toString().length == 0) || (siteID == " ")) {
	            siteID = "EXPLODER";
	         	
	        }  
	        //Set expiration date to 10 years in seconds (includes leap year)
	        expireTime.setYear(expireTime.getFullYear() + 10);       
	  	}
	  	
		/** Look for the "lsndtm" cookie */
		tempDate = getCookie("lsndtm");
		if (tempDate != null) {
			if ((tempDate.toString().length == 0) || (tempDate == " ")) {
	            tempDate = "0000";
	        }  
	        dateTime = tempDate;
	  	} 
	  	
	  	/** Set the cookies */
	  	setCookie("lsnsite", siteID, 3650);
		setCookie("lsndtm", dateTime, 3650);
		
		/** Debug Statements */
		if(debug)alert("LSNSITE (no siteID): " + getCookie("lsnsite"));
		if(debug)alert("LSNDTM (no siteID): " + getCookie("lsndtm"));
	} 
	
	var coupon = "";
	
	/** Reset the expire time to the current date */
	expireTime = new Date();
	if (queryObj.couponID != null) {
		if(debug)alert("Found couponID: " + queryObj.couponID);
		coupon = queryObj.couponID;
	} else if (queryObj.couponid != null) {
		if(debug)alert("Found couponid: " + queryObj.couponid);
		coupon = queryObj.couponid;
	} else {
		if(debug)alert("invalid coupon: invalid");
		coupon = "invalid";
	}
	/** If the couponid is found in the request object set the cookie */
	if (coupon != "invalid") {
		expireTime.setYear(expireTime.getFullYear() + 10);
		setCookie("couponid", coupon, expireTime, "/");
	}
	
function parseQueryString(queryString) {
	var queryObject = new Object();
	queryString = queryString.replace(/^.*\?(.+)$/,'$1');

	while ((pair = queryString.match(/([^=]+)=\'?([^\&\']*)\'?\&?/)) && pair[0].length) {
    	queryString = queryString.substring( pair[0].length );

	    if (/^\-?\d+$/.test(pair[2])) pair[2] = parseInt(pair[2]);
	    	queryObject[pair[1]] = pair[2];
  		}
  	return queryObject;
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	if(expiredays != 0){
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+"; path=/"
	}
	else{
		document.cookie=c_name+ "=" +escape(value)+"; path=/";
	}
}
		