// 
// Ad Client Functions
// 
function AdMaxAdClient() {
  var rand = Math.floor(Math.random()*1000000);
  this.scriptId = "ScriptId_" + rand;
  this.divId = "ad" + rand;

  this.renderAd = function (url) {
    var head = null;
    var heads = document.getElementsByTagName("head");
    if (heads != null && heads.item(0) != null) {
    	head = heads.item(0); // Get the first <head> element
    }

    // Create the new script tag
    var oScript = document.createElement("script");

    // Setup the src attribute of the script tag
    oScript.setAttribute("src", url);

    // Set the id attribute of the script tag
    oScript.setAttribute("id",this.scriptId);

    // Create the ad container node
    document.write('<div id="' + this.divId + '" style="text-align:center;">');
    
     // Create the new script tag which causes the proxy to be called
    if (false) { // false : 10/29/2010 - looking for head & adding content there means only 1 ad per page! 
	    // HEAD is available
   	head.appendChild(oScript);
    } else {
	    // HEAD is NOT available, add script tag inline
    	document.write('<script type="text/javascript" id="' + this.scriptId + '" src="' + url + '" ></script>');
    }
    document.write('</div>');
    
  },

  this.buildRequestURL = function (url, params) {
    // admax ad request url
    var admax_request = url + "?cTag=" + this.divId;

    for (i in params) {
      admax_request += "&" + i + "=" + escape(params[i]);
    }
    return admax_request;
  }
}

//
// Calling Function 
//
function admaxAd(params){
  var url = "http://admax.nexage.com/admax/adServe.do";
  params.ua = navigator.userAgent; // User Agent
  params.of = "js"; // Identifies type
  var adClient = new AdMaxAdClient();
  var adURL = adClient.buildRequestURL(url, params);
	log("Connecting to " + adURL);
  adClient.renderAd(adURL);
}

//
// Utility functions
//
function encodeParams(params) {
  var prms = "";
  for(i in params) {
     prms += "&" + i + "=" + escape(params[i]);
  }
  return prms;
}

function log(msg) {
	//alert(msg);
}

// PR-571 
function are_cookies_enabled()
{
  var cookieEnabled = (navigator.cookieEnabled) ? true : false;

  if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) { 
      document.cookie="testnx";
      cookieEnabled = (document.cookie.indexOf("testnx") != -1) ? true : false;
  }
  return (cookieEnabled);
}

function readCookie(name) {
    if (document.cookie) {  
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) { 
            var c = ca[i];
            while (c.charAt(0)==' ') {
                c = c.substring(1,c.length);
            }
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

/* Generate a GUID according to RFC 4412, Sec 4.4
// Due to u(id) restrictions of 32 characters, eliminating the hyphens generally used in GUIDs
*/
function generateGuid() {
//xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
  return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); });
}

function createCookie(name,value,days) {
    var expires = "";
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      expires = ";expires="+date.toGMTString();
    } else {
      expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function getSuid() {
  if (are_cookies_enabled()) {
      var suid = readCookie("nexagesuid");
      if (suid) return suid;
      else { 
        var newSuid = generateGuid();
        createCookie("nexagesuid",newSuid,365);
      }
  }
  return null;
}

