
/**
* Sets up the correct parameters on all links going to offerLink
* Sets a cookie with the offer, campaign and promo codes
* Displays the offer code on the page 
*/
function setupOffer(inParams) {
	
	var offerLink = "http://www.kajeet.com/kajeetStore/phones/index.html";
	
	var c = getC();	// campaign code
	var p = getP();	// promo code
	var o = getO(inParams);	// offer code
	var utm = inParams.utm;
  var cclabel = "Offer Code: "; 
  
  if (inParams.cclabel != null && typeof(inParams.cclabel) != 'undefined' ) {
	  cclabel = inParams.cclabel;
  }
  

	var parms = "?c=" + c + "&o=" + o + "&p=" + p;
	
	if (utm != null) {
		parms += "&utm_campaign=" + c + "&utm_source=" + utm + "&utm_medium=" + utm;
	}
	
	$("#campaign-code").text(cclabel + c + o);
	$("a[href='" + offerLink + "']").attr("href", offerLink + parms);
	setCookies(c, p, o);
	
}

function getC() {
	var c = jQuery.url.param("c");
	var result;
	var result = "";
	
	if (c != null && typeof(c) != 'undefined') {
  	if (c.length == 6) {
	  	result = c.substring(0,3).toUpperCase();	// if 6 digits, get first three
	  } else {
		  result = c.toUpperCase();		// should be >6 or 3
  	}
	}
	return result;
}

function getP() {
	var p = jQuery.url.param("p");
	var result = "";
	
	if (p != null) {
		result = p.toUpperCase();
	} else {
		result = "NONE-KKFJ-000001";
	}
	
	return result;
}

function getO(inParams) {
	var o = jQuery.url.param("o");
	var c = jQuery.url.param("c");
	if (c == null || typeof(c) == 'undefined') { c = ""; }
			
	var result;
	
	if (o != null) {
		result = o.toUpperCase();	// if there is a URL parameter, use that
	} else if (c.length == 6) {
		result = c.substring(3,6).toUpperCase();	// if c is 6 digits, use the last three
	}	else {
		result = inParams.o;	// if not, use passed in o variable
	}
	
	return result;
}

function setCookies(c, p, o) {

	$.cookie("orderOfferCode", o, { path: '/', expires: 30, domain: "www.kajeet.com" });
	$.cookie("campaignc", c, { path: '/', expires: 30, domain: "www.kajeet.com" });
	$.cookie("orderPromoCode", p, { path: '/', expires: 30, domain: "www.kajeet.com" });
	
}
