if (typeof(AdFuUtilities) == 'undefined') {
    AdFuUtilities = {};
}

// RPC responses - duplicated in constants.inc.php
var RPC_OK = 0;
var RPC_NOTLOGGEDIN = 1;
var RPC_PERMISSIONDENIED = 2;
var RPC_INTERNALERROR = 3;
var RPC_NOTHINGTODO = 4;
var RPC_OTHER = 5;
var RPC_FAILEDTRANSACTION = 6;

function getStyle(name) {
  return getBlock(name).style;
}

function getStyleInParent(name) {
  return getBlockInParent(name).style;
}

function existsBlock(name) {
  if (document.getElementById) {
    return document.getElementById(name)!=null;
  } else if (document.all) {
    return document.all[name]!=null;
  } else if (document.layers) {
    return document.layers[name]!=null;
  }
}

function getBlock(name) {
  if (document.getElementById) {
    return document.getElementById(name);
  } else if (document.all) {
    return document.all[name];
  } else if (document.layers) {
    return document.layers[name];
  }
}

function getBlockInParent(name) {
  if (parent.document.getElementById) {
    return parent.document.getElementById(name);
  } else if (parent.document.all) {
    return parent.document.all[name];
  } else if (parent.document.layers) {
    return parent.document.layers[name];
  }
}

// Thou shalt not use this function except in VERY careful cases.
// If someone reloads the frame, state can get out of sync
function getBlockInChild(child, name) {
  if (frames[child].document.getElementById) {
    return frames[child].document.getElementById(name);
  } else if (frames[child].document.all) {
    return frames[child].document.all[name];
  } else if (frames[child].document.layers) {
    return frames[child].document.layers[name];
  }
}

function countChecks(checkboxarray) {
  var count=0;
  for(i=0; i<checkboxarray.length; i++) {
    if(checkboxarray[i].checked) count++; 
  }
  return count;
}

// Counts number of 'element' in an array 'arr'
function countElements(arr, element) {
  var count=0;
  for(i=0; i<arr.length; i++) {
    if(arr[i] == element) count++; 
  }
  return count;
}

function hideBlock(name) {
  getStyle(name).display="none";
}

function showBlock(name) {
  getStyle(name).display="";
}

function popup(url, width, height) {
  var newwin = window.open(url,'name','height='+width+',width='+height);
  newwin.focus();
  return false;
}

function popupHTML(html, width, height) {
  var newwin = window.open("text/html",'name','height='+width+'px,width='+height+'px');
  newwin.document.writeln(html);
  newwin.focus();
  newwin.document.close();
  return false;
}


var blockNames = new Array();
function registerBlock(blockName) {
  var i = blockNames.length;
  blockNames[i] = blockName;
}

function showOnlyBlock(blockName) {
  for(i=0; i<blockNames.length; i++) {
    if (blockNames[i] != blockName) {
      hideBlock(blockNames[i]);
    }
  }
  showBlock(blockName);
}

function getPosition(myObj) {
  var x=0;
  var y=0;
  if (myObj.offsetParent) {
    x = myObj.offsetLeft;
    y = myObj.offsetTop;
    while (myObj = myObj.offsetParent) {
      x += myObj.offsetLeft;
      y += myObj.offsetTop;
    }
  }
  return [x,y];
}

function log10(x) {
  return Math.LOG10E * Math.log(x);
}

function asciiToBin(str) {
  var newstr = "";
  for(var i=0; i<str.length; i++) {
    newstr += str.charCodeAt(i);
    newstr += ","; 
  }
  return newstr;
}


// Trims whitespace off front and end
function strtrim(str) {
  return str.replace(/^\s+|\s+$/g,'');
}

function escapeSlashes(text) {
  text = text.replace(/\'/g,"\\'");
  text = text.replace(/\"/g,"\\\"");
  return text;
}

function escapeQuotes(text) {
  text = text.replace(/'/g,"&#39;");
  text = text.replace(/"/g,"&quot;");
  return text;
}

function unescapeQuotes(text) {
  text = text.replace(/&quot;/g, "\"");
  text = text.replace(/&#39;/g, "\'");
  return text;
}

function escapeText(text, disallowLB) {
  if (typeof(disallowLB) == 'undefined') disallowLB = false;
  text = text.replace(/&/g,"&amp;");
  text = text.replace(/  /g," &nbsp;");
  text = text.replace(/>/g,"&#62;");
  text = text.replace(/</g,"&#60;");
  if (!disallowLB) {
    text = text.replace(/\n/g,"<br>");
  }
  return text;
}

function unescapeText(text) {
  // Get rid of \n and \t's since they have no meaning in html except
  // as whitespace
  text = text.replace(/\n/gi, " ");

  text = text.replace(/<br>/gi, "\n");
  text = text.replace(/&#60;/g, "<");
  text = text.replace(/&lt;/g, "<");
  text = text.replace(/&#62;/g, ">");
  text = text.replace(/&gt;/g, ">");
  text = text.replace(/&nbsp;/g, " ");
  text = text.replace(/&amp;/g, "&");
  return text;
}

function canonicalizeURL(url) {
  var turl = strtrim(url);
  var arr = turl.match(/^[a-zA-Z0-9]+:\/\/(.*)/);
  if (!arr) {
    turl = 'http://'+turl;
  }
  return turl;
}

function escapeSpecials(text) {
  text = text.replace(/\[(\/?)i\]/g,"<$1i>");
  text = text.replace(/\[(\/?)b\]/g,"<$1b>");
  text = text.replace(/\[(\/?)u\]/g,"<$1u>");
  text = text.replace(/\[(\/?)s\]/g,"<$1s>");
  text = text.replace(/\[bullet\]/g,"&#149;");
  text = text.replace(/\[plusminus\]/g,"&#177;");
  text = text.replace(/\[half\]/g,"&#189;");
  text = text.replace(/\[euro\]/g,"&euro;");
  text = text.replace(/\[inf\]/g, "&#8734;");

  // // Turn www.blah.com into http://www.blah.com
  // text = text.replace(/(www\.[^\s\.'"]*\.com)/g, "http://$1");
  // Turn http:// links into links
  text = text.replace(/([a-zA-Z0-9^']+:\/\/[^\s']*)/g, "[a '$1']$1[/a]");

  var tastr = /\[[aA]\s[^'"\]]*['"]([^'"]*)['"]\]([^\[]*)\[\/[aA]\]/;
  var arr = text.match(tastr);
  if (arr) {
    var turl = arr[1];
    turl = canonicalizeURL(turl);
    text = text.replace(tastr, "<a href='javascript:void(0)' onClick='window.open(\""+turl+"\",\"_blank\")'>"+arr[2]+"</a>");
  }

  return text;
}

function escapeTextAndSpecials(text, disallowLB) {
  // Special char replacement
  text = escapeText(text, disallowLB);
  text = escapeSpecials(text);
  return text;
}

function formatMoney(amount) {
  amount -= 0;
  amount = Math.ceil(amount*100)/100;
  if (amount == Math.floor(amount)) {
    return amount+'.00';
  }
  else if (amount*10 == Math.floor(amount*10)) {
    return amount+'0';
  }
  else {
    return amount;
  }
}

function getCSSClass(className) {
  var rulesName;
  if (document.getElementById) {
    rulesName = 'cssRules';
  } else if (document.all) {
    rulesName = 'rules';
  } else {
    throw "CSS rules array doesn't exist!";    
  }

  for (var i=0; i<document.styleSheets.length; i++){
    var rule = document.styleSheets[i][rulesName];
    for (var j=0; j<rule.length; j++) {
      var obj = rule[j];
      if (obj.selectorText == className) {
        return obj;
      }
    }
  }
  return null;
}

function chopLastChar(str) {
  return str.substr(0, str.length-1);
}

var MONTHS = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function confirmAndForward(message, forwardurl) {
  if (confirm(message)) {
    window.location = forwardurl;
  }
}

function getScreenDimensions() {
  var width, height;
  if (typeof(window.innerWidth) == "number") {
    width  = window.innerWidth;
    height = window.innerHeight;
  } else if (document.body && 
             (document.body.clientWidth !=0 || 
              document.body.clientHeight!=0)) {
    width  = document.body.clientWidth;
    height = document.body.clientHeight;
  } else if (document.documentElement && 
             (document.documentElement.clientWidth!=0 || 
              document.documentElement.clientHeight!=0)) {
    width  = document.documentElement.clientWidth;
    height = document.documentElement.clientHeight;
  } else {
    width = 0;
    height = 0;
  }
  return [width,height];
}

