var mpmPixelLocation = 'pixel.optimaster.com/api/hitByQuery.aspx'

var mpmExpires=730 //expiration
var mpmIP
var mpmAID
var mpmCost
var mpmPageDesc
var mpmLink
var mpmFirstVisit
var mpmConvClick
var mpmCMPGN
var mpmImages=new Array;
var mpmIndex=0;	
//following only used when tracking clicks that do not open new window.
//window.onunload = mpmCheckConversion;	
//var mpmWait = 750 // unload delay in milliseconds.	

if (mpmGetCookie("MPMGUID")==null){
	mpmSetCookie('MPMGUID',mpmNewGuid(),mpmExpires);
	mpmFirstVisit=true;
}else{
	mpmFirstVisit=false;
}


//get campaignID
//Priority: set in cookie, set in QS, set on page
if (mpmGetCookie("MPMCMPGN")==null){
	if (mpmQueryString("cmpgn")!='false') {
		mpmCMPGN=mpmQueryString("cmpgn");
	}
	if (mpmCMPGN!=null){
		mpmSetCookie("MPMCMPGN",mpmCMPGN,mpmExpires)
	}
}else{
	if(mpmCMPGN==null){
		mpmCMPGN=mpmGetCookie("MPMCMPGN");
	}
}

if (mpmCMPGN!=null){
	mpmSendPixel(mpmAID,mpmCID,mpmCost,mpmPageDesc,mpmFirstVisit,mpmCMPGN);
}

//click scripts


function setConversion(actionID,linkID,pageDesc,opensNew){
	
	if (opensNew){
		mpmSendPixel(actionID,mpmCID,'',pageDesc,mpmFirstVisit,mpmCMPGN)
	}else{
		mpmAID = actionID;
		mpmPageDesc = pageDesc;
		mpmLink = linkID;
		mpmConvClick = 1;
	}
}
function mpmCheckConversion(){

	if (mpmConvClick > 0) {
		//there was a conversion click
	    mpmSendPixel(mpmAID, mpmCID,'',mpmPageDesc,mpmFirstVisit,mpmCMPGN)
		mpmPause(mpmWait) // wait for image to load
		document.location = exec(mpmLink + ".href.value");
		}
	}
function mpmPause(iMsecs){
	var d = new Date() //today's date
	while (1){
		var mill=new Date() // Date Now
		diff = mill-d //difference in milliseconds
		if( diff > iMsecs ) {break;}
	}
}
//pixel scripts
function mpmSendPixel(aID,cID,cost,pageDesc,firstVisit,cmpgn){
	var protocol="http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"
	var src = protocol + mpmPixelLocation;
	var dNow = new Date();
	src = src + '?MPMGUID=' + fix(mpmGetCookie('MPMGUID')) +
		 '&r=' + fix(document.referrer) + 
		 '&u=' + fix(document.URL) + 
		 '&c=' + fix(cID) +
		 '&ty=' + fix(aID) + 
		 '&coCost=' + fix(cost) +
		 '&desc=' + fix(pageDesc) +
		 '&t=' + dNow.getTime() +
		 '&f=' + firstVisit +
		 '&cmpgn=' + fix(cmpgn);	
	//document.MPMPIXEL.src=src
	mpmCreateImage(src);
}

function mpmCreateImage(mpmSrc){
	if (document.images){
		mpmImages[mpmIndex]=new Image;
		mpmImages[mpmIndex].src=mpmSrc;
		mpmIndex++;
	}
	else{
		document.write('<IMG BORDER="0" NAME="MPMIMG2" WIDTH="1" HEIGHT="1" SRC="'+mpmSrc+'">');
	}
}

function fix(val){
	if (val!=null&&val!=undefined){
		return escape(val);
	}else{
		return '';
	}
}

function mpmSetCookie(name, value) {
	var expdate =  new Date();
	expdate.setTime(expdate.getTime() + (mpmExpires*86400000));
	mpm_write_cookie(name,value,expdate);
}

function mpmGetCookie(name){
	var arg=name + "=";
	var alen = arg.length;
	var clen=document.cookie.length;
	var i=0;
	while(i<clen) {
		var j = i + alen;
		if(document.cookie.substring(i,j)==arg)	return mpm_read_cookie(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if  (i==0) break;
	}
	return null;
}
function mpm_write_cookie(name,value,expires){
	document.cookie = name+"="+escape(value)+
			((expires== null) ? "" : ("; expires=" + expires.toGMTString()))+
			"; path=/";
}
function mpm_read_cookie( begin ) {
	var end=document.cookie.indexOf(";", begin);
	return unescape(document.cookie.substring(begin,end<0?document.cookie.length:end));
}

function mpmNewGuid(){
    var g = "{";
    for(var i = 0; i < 32; i++)
    g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "")
    return g + "}";
}

function mpmPageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}

	this.getKeyValuePairs = function() { return this.keyValuePairs; }

	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}

	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }
}

function mpmQueryString(key){
	var page = new mpmPageQuery(window.location.search);
	return unescape(page.getValue(key));
}
