// Madasafish affiliate customisations
// (c) Madasafish 2007
// v0001d

function gMaffGetCookieString(name){
	var pos=document.cookie.indexOf(name+"=");
	if (pos!=-1){
		var start=pos+name.length+1;
		var end=document.cookie.indexOf(";",start);
		if (end==-1){
			end=document.cookie.length;
		}
		return unescape(document.cookie.substring(start,end));
	}
	return "";
}

function gMaffSetCookieFull(strName, strValue, strDomain, strPath, intLifetimeDays){
	var dCur = new Date();
	var dExp = new Date(dCur.getTime() + 86400000 * intLifetimeDays);								 
	document.cookie = strName + "=" + strValue + "; expires=" + dExp.toGMTString() + "; path=" + strPath + "; domain=" + strDomain;
}

function gMaffGetStringParam(s, p, d, pref) {
	if ((typeof(s) == "undefined") || (s == "")) return "";
	var offs = 0;
	if (pref != "") {
		if (s.substring(0, pref.length) == pref) offs = pref.length;
	}
	var tmp = p + "=";
	var i = offs;
	if (s.substring(offs, tmp.length + offs) != tmp) {
		i = s.indexOf(d + tmp, offs);
		if (i >= 0) i += d.length;
	}
	if (i >= 0) {
		i += tmp.length;
		var j = s.indexOf(d, i);
		if (j < 0) j = s.length;
		return s.substring(i, j);
	} else {
		return "";
	}
}	

function gMaffSetBackground(strID, strUrl) {
	if (strUrl == "") return;
	var e = document.getElementById(strID);
	if (typeof(e) != "undefined") {
		e.style.backgroundImage = "url(" + strUrl + ")";
	}
}

function gMaffSetElementValue(strID, strValue) {
	if (typeof(strValue) == "undefined") return;
	if ('' == strValue) return;
	var e = document.getElementById(strID);
	if (typeof(e) != "undefined") {
		e.value = strValue;
	}
}

function gMaffDisplayDiv(strID, blnVisible)
{
  var e;
  if (document.getElementById) {
	  e = document.getElementById(strID);
  } else {
	  if (document.all) {
		  e = document.all[strID];
	  } else {
		  if(document.layers) {
			  e = document.layers[strID];
		  }
	  }
  }
  if (typeof(e) != "undefined") {
	  if (blnVisible) {
	  	e.style.display = "block";
  	} else {
	  	e.style.display = "none";	  	
  	}
  }
}

function gMaffGetCampaignCode() {
	return gMaffGetStringParam(document.location.search, "r", "&", "?");
}


function gMaffGetReferralCode() {
	var s;

	s = gMaffGetStringParam(document.location.search, "ref", "&", "?");
	if (s != "") return s;

	s = gMaffGetCookieString("COOKIE_REFERRAL_UID");
	if (s != "") {
		if (s.substring(0, 5) == "MAAF_") {
			s = s.substring(5, s.length);
			return s;
		} else {
			return "[REF_SERVICE_ID]";
		}
	}
	return "";
}
