var mobileguard = mobileguard || {};
mobileguard.responseCodes = mobileguard.responseCodes || {};
mobileguard.responseCodes.NOT_BILLABLE = 'NOT_BILLABLE';
mobileguard.responseCodes.TRUE = 'TRUE';
mobileguard.responseCodes.FALSE = 'FALSE';
mobileguard.responseCodes.ERROR = 'ERROR';
mobileguard.ownsFeature = function(userid, featurekey, callback) {
	var timer = 0;
	if(!!mobileguard.callback) {
		return;
	}else{
	    if(!!mobileguard.viewer && mobileguard.viewer.userid == userid){
            mobileguard.optinURL = mobileguard.viewer.optinURL;
            setTimeout(function(){callback(mobileguard.viewer.status)}, 0);
            return;
        }
	    if(!!callback){
		    mobileguard.callback = function(code){setTimeout(function(){callback(code)} , 0);
		     mobileguard.callback = null;};
		 }else{
		   mobileguard.callback = function(code){mobileguard.callback = null;};
		 }
	}
	var mgScript = document.createElement('script');
	mgScript.src = 'http://api.mobileguard.org/client.ashx?rd='+ Math.random()+'&featurekey=' + encodeURIComponent(featurekey) + '&userid=' + encodeURIComponent(userid) + '&domain=' + encodeURIComponent(mobileguard.appDomain); 
    mgScript.type= 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(mgScript);
	timer = window.setTimeout(function() {mobileguard.callback(mobileguard.responseCodes.ERROR);}, 10000);
}
mobileguard.gotoOptin = function(returnUrl) {
	if(!returnUrl){
		returnUrl = document.location.href;
	}
	if(!!mobileguard.optinURL){
		document.location.href = mobileguard.optinURL + '&url=' + encodeURIComponent(returnUrl); 
	}
}
mobileguard.appDomain = document.domain;
