var matches;
window.msiecheck = /MSIE\s*(\d+)/;
matches = msiecheck.exec(window.navigator.userAgent);
window.msie6 = matches && matches[1]<7;
window.myQuery = new ParseQuery();
window.bkgRoll = false;
window.debug = false;
window.loadList = new Array();
window.unloadList = new Array();
window.checkDelay = 1000;

function enumerate(obj)
{
  var i;
  alert('enumerating: ('+ obj +'):'+ typeof(obj));
  for(i in obj) { alert(i +':'+ obj[i]); }
}

function tbi() { alert("To Be Implemented"); }

function WFXcopy(obj)
{
  var i, ret = {};
  for(i in obj)
  {
    if(typeof(obj[i])=='object') { ret[i] = WFXcopy(obj[i]); }
    else { ret[i] = obj[i]; }
  }

  return ret;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }

window.flexes = new Array();

flexes.mainHook = { 'outer': 'mainOuter', wires: 'wires', 'images': [], 'ninerect':[24,20,20,24],
    'minHeight':53, dsrect: [0,7,8,0] };
for(i=1; i<=9; i++) { flexes.mainHook.images[i] = 'res/images/whitebox_0'+ i +'.png'; }

flexes.deathBannerHook = WFXcopy(flexes.mainHook);
//for(i=1; i<=9; i++) { flexes.deathBannerHook.images[i] = 'res/images/bluebox_0'+ i +'.png'; }
flexes.deathBannerHook.outer = 'deathBannerOuter';

flexes.sideHookBlue = WFXcopy(flexes.mainHook);
for(i=1; i<=3; i++) { flexes.sideHookBlue.images[i] = 'res/images/bluebox_0'+ i +'.png'; }
flexes.sideHookBlue.outer = 'sideOuterBlue';

flexes.sideHookOrange = WFXcopy(flexes.mainHook);
for(i=1; i<=3; i++) { flexes.sideHookOrange.images[i] = 'res/images/orangebox_0'+ i +'.png'; }
flexes.sideHookOrange.outer = 'sideOuterOrange';

flexes.topbarHook = WFXcopy(flexes.mainHook);
for(i=1; i<=3; i++) { flexes.topbarHook.images[i] = 'res/images/bluebox_0'+ (i+3) +'.png'; }
for(i=4; i<=6; i++) { flexes.topbarHook.images[i] = 'res/images/orangebox_0'+ i +'.png'; }
for(i=7; i<=9; i++) { flexes.topbarHook.images[i] = 'res/images/orangebox_0'+ i +'.png'; }
flexes.topbarHook.outer = 'topbarOuter';
flexes.topbarHook.ninerect[0] = 6;
flexes.topbarHook.minHeight = 36;

flexes.btnHook = WFXcopy(flexes.mainHook);
for(i=1; i<=9; i++) { flexes.btnHook.images[i] = 'res/images/bluebox_0'+ i +'.png'; }
flexes.btnHook.ninerect[2] = 0;
flexes.btnHook.dsrect[2] = 0;
flexes.btnHook.outer = 'btnOuter';
flexes.btnHook.minHeight = 1;

for(i=7; i<=9; i++) { flexes.mainHook.images[i] = 'res/images/bluebox_0'+ i +'.png'; }

flexes.shadowHook = { 'outer': 'shadowOuter', wires: 'wires', 'images': [], 'ninerect':[8,1,0,7],
    'minHeight':17, dsrect: [0,7,8,0] };
for(i=1; i<=9; i++) { flexes.shadowHook.images[i] = 'res/images/squareshadow_0'+ i +'.png'; }


function bLoad()
{
  var i, j, k, src, tags, claz, wire, obj, obj2, flex;

  if(obj = document.forms.upperRightSearch)
  {
    obj.q.value = myQuery.q?myQuery.q:'';
    for(i=0; i<obj.sitesearch.length; i++)
    {
      if(obj.sitesearch[i].value == myQuery.sitesearch)
      {
        obj.sitesearch[i].checked = true;
        break;
      }
    }
  }

/*
  if(myQuery.magic==null)
  {
    var matches = /magic=(true|false)/.exec(document.cookie);
    window.magic = (matches==null) && (matches[1]=='true');
  } else {
    window.magic = (myQuery.magic=='true');
    document.cookie = 'magic='+ (myQuery.magic=='true') +'; path=/';
  }
*/

  window.magic = true;

  if(!window.magic) { return; }

  window.flexNodes = new Array();
  if(msie6)
  {
    tags = document.getElementsByTagName('img');

    for(i=0; i<tags.length; i++) { IEpngfix(tags.item(i)); }

    window.bodyOldWidth = 0;
  }

  tags = document.getElementsByTagName('div');
  for(i=0; i<tags.length; i++)
  {
    groomNode(tags.item(i));
  }
  tags = document.getElementsByTagName('a');
  for(i=0; i<tags.length; i++)
  {
    groomNode(tags.item(i));
  }

  if(window.bkgRoll) { window.bkgOffset = 0; }

  obj = document.createElement('div');
  obj2 = obj.appendChild(document.createElement('img'));
  obj2.setAttribute('src', 'res/images/radio-waves.png');
  obj2.setAttribute('id', 'radioWaves');
  obj2.setAttribute('alt', '');
  document.body.insertBefore(obj, document.body.firstChild);
  IEpngfix(obj2);

  execList(window.loadList);

  check();
}

function bUnload()
{
  execList(window.unloadList);
}

function execList(list)
{
  var i;
  for(i=0; i<list.length; i++) { eval(list[i]); }
}

function groomNode(obj)
{
  var i, j, k, src, tags, claz, wire, obj, obj2, flex;

  claz = obj.attributes['class'];
  if(claz==null) { return; }
  if(!(flex = flexes[claz.value])) { return; }
  if(isNaN(parseInt(obj.style.minHeight))) { obj.style.minHeight = 0; }
  setPX(obj, 'minHeight', Math.max(parseInt(obj.style.minHeight), flex.minHeight));
  if(msie6) { obj.style.height = obj.style.minHeight; }
  obj.className = flex.outer;
  wire = document.createElement('div');
  wi = obj.offsetWidth;
  hi = obj.offsetHeight;
  setPX(wire, 'width', wi);
  setPX(wire, 'height', hi);
  subwi = wi - flex.ninerect[1] - flex.ninerect[3];
  subhi = hi - flex.ninerect[0] - flex.ninerect[2];

  window.flexNodes.push(new Array(obj, wi, hi, flex));
  wire.className = flex.wires;
  obj.insertBefore(wire, obj.firstChild);
  for(j=1; j<=9; j++)
  {
    obj = document.createElement('img');
    obj.src = flex.images[j];
    obj.style.position = 'absolute';
    wire.appendChild(obj);
    if(j==1 || j==4 || j==7)
      { setPX(obj, 'width', flex.ninerect[3]+flex.dsrect[3]); setPX(obj, 'left', -flex.dsrect[3]); }
    if(j==2 || j==5 || j==8)
      { setPX(obj, 'width', subwi); setPX(obj, 'left', flex.ninerect[3]); }
    if(j==3 || j==6 || j==9)
      { setPX(obj, 'width', flex.ninerect[1]+flex.dsrect[1]); setPX(obj, 'right', -flex.dsrect[1]); }
    if(j==1 || j==2 || j==3)
      { setPX(obj, 'height', flex.ninerect[0]+flex.dsrect[0]); setPX(obj, 'top', -flex.dsrect[0]); }
    if(j==4 || j==5 || j==6)
      { setPX(obj, 'height', subhi); setPX(obj, 'top', flex.ninerect[0]); }
    if(j==7 || j==8 || j==9)
      { setPX(obj, 'height', flex.ninerect[2]+flex.dsrect[2]); setPX(obj, 'bottom', -flex.dsrect[2]); }

    IEpngfix(obj);
  }
}

function check()
{
  var obj, i, wi, hi, wire, flex;

  for(i=0; i<window.flexNodes.length; i++)
  {
    obj = window.flexNodes[i][0];
    flex = window.flexNodes[i][3];
    flex.topbottom = flex.ninerect[0] + flex.ninerect[2];
    flex.leftright = flex.ninerect[1] + flex.ninerect[3];
    wire = obj.firstChild;
    window.flexNodes[i][1] = wi = obj.offsetWidth;
    window.flexNodes[i][2] = hi = obj.offsetHeight;
    if(isNaN(wi) || wi<1) { return; }

    setPX(wire, 'width', wi);
    setPX(wire, 'height', hi);
    subwi = wi - flex.ninerect[1] - flex.ninerect[3];
    subhi = hi - flex.ninerect[0] - flex.ninerect[2];
    setPX(wire.childNodes.item(1), 'width', subwi);
    setPX(wire.childNodes.item(4), 'width', subwi);
    setPX(wire.childNodes.item(7), 'width', subwi);
    setPX(wire.childNodes.item(3), 'height', subhi);
    setPX(wire.childNodes.item(4), 'height', subhi);
    setPX(wire.childNodes.item(5), 'height', subhi);
    // IE: object changes height, calculated position of these two objects gets left behind. So we jiggle them.
    if(msie6 && flex.dsrect[2]>0)
    {
      setPX(wire.childNodes.item(6), 'bottom', flex.dsrect[2]);
      setPX(wire.childNodes.item(7), 'bottom', flex.dsrect[2]);
      setPX(wire.childNodes.item(8), 'bottom', flex.dsrect[2]);
      setPX(wire.childNodes.item(6), 'bottom', -flex.dsrect[2]);
      setPX(wire.childNodes.item(7), 'bottom', -flex.dsrect[2]);
      setPX(wire.childNodes.item(8), 'bottom', -flex.dsrect[2]);
    }
  }

  if(window.bkgRoll)
  {
    window.bkgOffset = (window.bkgOffset+5)%640;
    document.body.style.backgroundPosition='0 '+ window.bkgOffset +'px'
  }

  setTimeout('check()', window.checkDelay);
}

function IEpngfix(obj)
{
  if(!msie6) { return; }
  src = obj.src;
  if(src.search(/.png/)<0) { return; }
  obj.src = 'res/images/spacer.gif';
  obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ src +"', sizingMethod='scale')";
}

function setPX(obj, prop, val)
{
  var sp = 'set'+ prop;
  obj.style[sp] = val;
  if((prop=='width' || prop=='height') && val<1) { val = 1; }
  if(obj.style.setwidth<1 || obj.style.setheight<1) { obj.style.display = 'none'; }
  else { obj.style.display = 'block'; }
  obj.style[prop] = val +'px';
}

function ParseQuery()
{
  var query, qs = top.location.search.substring(1);
  var queries = qs.split(/\&/);
  for (var i=0; i<queries.length; i++)
  {
    query = queries[i].split(/=/, 2);
    this[urldecode(query[0])] = (typeof query[1] == 'undefined') ? null : urldecode(query[1]);
  }
}

function urldecode(str) { return(unescape(str.replace(/\+/g, ' '))); }

function goto(url) { window.location = url; }

function vrfyEmailAutoconfig()
{
  var frm = document.forms.emailConfigFrm;
  if(frm.fullname.value.search(/\S/)<0)
  {
    alert("Please enter the Full Name you wish people to see when you send them email.");
    frm.fullname.value = '';
    frm.fullname.focus();
    return;
  }
  if(!validate_email(frm.emailaddress.value))
  {
    alert("Please enter your full email address, such as john@webformixair.com.");
    frm.emailaddress.select();
    return;
  }

  frm.action = 'https://www.webformix.com/manage/email_configurator.php';
  frm.submit();
}

function validate_email(str)
{
  var local_nodot = '[a-z0-9!#$%&\'*+/=?^_`{|}~-]';
  var local_dot   = '[a-z0-9!#$%&\'*+/=?^_`{|}~.-]';
  var domain      = '[a-z0-9-]';
  var regstr      = '^'+ local_nodot +'('+ local_dot +'*'+ local_nodot +')?@'+ domain +'+(\\.'+
                    domain +'+)+$';
  var regex       = new RegExp(regstr, 'i');
  str = str.trim();
  return(str.search(regex)==0);
}


