var popupzone=1, popupid=64, popupintervaldays=4, popupemail='';

function createflowad()
{
  flowad = document.createElement('div');
  flowad.id = 'flowad';
  if (flowadpopulate) flowadpopulate(flowad);
  return flowad;
}

function deployflowad(flowad)
{
  cookiearray = document.cookie.split(/;\s*/);
  var slideme = true;

  for (entry in cookiearray)
  {
    if (cookiearray[entry] == 'HDsub=1') return;
    if (cookiearray[entry] == 'HDflowad=1') return;
    if (cookiearray[entry] == 'HDflowad=2') slideme = false;
  }

  if (!document.defaultView)
  {
    scrollheight = document.body.parentNode.scrollTop;
    var subelements = [];
    for (var i=0; i<document.body.childNodes.length; i++)
    {
      subelements[i] = document.body.childNodes[i];
    }

    var zip = document.createElement('div');
      zip.className = 'zip';

    for (var i = 0; i < subelements.length; i++) {
      zip.appendChild(subelements[i]);
    }

    document.body.appendChild(zip); // add the major div
    flowad.style.position = 'absolute';
    document.body.className = 'zipped';
    document.body.parentNode.className = 'zipped';
    zip.scrollTop = scrollheight;
    document.body.parentNode.scrollTop = 0;
  }

  flowad.style.marginBottom = '-500px';
  document.body.appendChild(flowad);

  flowadheight = flowad.offsetHeight ? flowad.offsetHeight : 20;
  flowad.style.marginBottom = (0 - flowadheight) + 'px';
  flowadposition = 0;

  if (slideme) flowadtimeout = setTimeout(startflowad, 1600);
    else finishflowad()
}

function startflowad()
{
  flowadtimeout = setInterval(positionflowad, 25);
}

function positionflowad()
{
  flowadposition += 10;
  flowad.style.marginBottom = '-' + (((100 - flowadposition) / 100) * flowadheight) + 'px';
  if (flowadposition >= 100)
  {
    clearTimeout(flowadtimeout);
    flowadtimeout = setTimeout(finishflowad, 1);
  }
}

function finishflowad()
{
  flowad.style.marginBottom = '0';
  document.body.parentNode.style.paddingBottom = (flowadheight - getflowadoverlap()) +'px';
  //document.cookie = 'HDflowad=2; path=/';
  //var expire = new Date();
  //expire.setTime(expire.getTime() + (604800000)); // 7 days
  //document.cookie = 'HDflowad=1; expires=' + expire.toGMTString() + '; path=/';

}

function destroyflowad()
{
  if (!flowad) return false;
  document.cookie = 'HDflowad=2; path=/';
  var expire = new Date();
  expire.setTime(expire.getTime() + (604800000)); // 7 days
  document.cookie = 'HDflowad=1; expires=' + expire.toGMTString() + '; path=/';
  document.body.removeChild(flowad);
  document.body.parentNode.style.paddingBottom = '0';
  return false;
}

function destroyflowadnoreturn()
{
  destroyflowad();
  var expire = new Date();
  expire.setTime(expire.getTime() + (86400000 * 365));
  document.cookie = 'HDsub=1; expires=' + expire.toGMTString() + '; path=/';
  return false;
}

function flowadlaunchpopup()
{
  pp = window.open(this.href, 'flowadpopup', 'scrollbars,width=560,height=420,resizable');
  return pp ? false : true;
}

popupcookiesenabled = false;
var expire = new Date();
expire.setTime(expire.getTime() + (10000));
document.cookie = 'HDtestcookie=1; expires=' + expire.toGMTString() + '; path=/';
cookiearray = document.cookie.split(/;\s*/);
for (entry in cookiearray)
{
  if (cookiearray[entry] == 'HDtestcookie=1') popupcookiesenabled = true;
}
if (popupcookiesenabled)
{
  flowad = createflowad();
  flowadoldonload = window.onload;
  window.onload = function ()
  {
    deployflowad(flowad);
    if (flowadoldonload) flowadoldonload();
  }
}

function callme()
{
  document.getElementById('FRM').submit();
}

function validC2TPhoneUS(fm){
  alert('::'+fm);
  var width = "470";
  var height = "440";
  if(fm.txtusphnum1.value.length!=3 || isNaN(fm.txtusphnum1.value) || fm.txtusphnum2.value.length!=3 || isNaN(fm.txtusphnum2.value) || fm.txtusphnum3.value.length!=4 || isNaN(fm.txtusphnum3.value)){
    alert("A valid US phone number is required");
    return false;
  }
  else{
    fm.target = "" + width + "x" + height
    C2TopenWind("", width, height);
    return true;
  }
}

function flowadpopulate(flowad)
{
  flowad.style.backgroundImage = 'url(/images/flowad.png)';
  flowad.style.height = '72px';
  flowad.style.padding = '0';


  // create goaway
  ul = document.createElement('ul');
  l1 = document.createElement('li');
  l2 = document.createElement('li');
  closelink = document.createElement('a');
  closelink.appendChild(document.createTextNode('Close this'));
  closelink.href = '#';
  closelink.onclick = destroyflowad;
  closelink.style.color = '#FFFFFF';
  nomorelink = document.createElement('a');
  nomorelink.appendChild(document.createTextNode('Don\'t show this again'));
  nomorelink.href = '#';
  nomorelink.onclick = destroyflowadnoreturn;
  nomorelink.style.color = '#FFFFFF';
  l1.appendChild(closelink);
  l2.appendChild(nomorelink);
  l1.style.display = "inline";
  l2.style.display = "inline";
  l1.style.padding = "0px 6px 0px 0px";
  ul.appendChild(l1);
  ul.appendChild(l2);
  ul.style.styleFloat = ul.style.cssFloat = 'right';
  ul.style.margin = '5px 0px 0px 0px';
  ul.style.fontSize = 'x-small';
  ul.style.listStyleType = 'none';
  ul.style.backgroundColor = '#004782';
  ul.style.padding = '2px 10px 2px 10px';
  ul.style.borderLeft = '1px solid #5A5651';
  ul.style.borderTop = '1px solid #5A5651';
  ul.style.borderBottom = '1px solid #004782';
  flowad.appendChild(ul);

  // form
  frm = document.createElement('div');
  frm.id = 'flowform';

  sform = document.createElement('form');
  sform.method = 'post';
  sform.name = 'FRM';
  sform.id = 'FRM';
  sform.target = '_blank';
  sform.action = 'http://account.click2talk.com/account/click2talk/c2tpopup/popup.aspx';
  sform.onSubmit = 'validC2TPhoneUS('+document.FRM+')';
  sform.style.margin = '0px';
  sform.style.padding = '0px';

  guid = document.createElement('input');
  guid.name = 'guid';
  guid.value = '2329NCCYDSgoEHHOCSKelSAJ';
  guid.type = 'hidden';

  cbdly = document.createElement('input');
  cbdly.name = 'cbdly';
  cbdly.value = '0';
  cbdly.type = 'hidden';

  fbox = document.createElement('input');
  fbox.type = 'text';
  fbox.id = 'fbox';
  fbox.maxLength = 3;
  fbox.name = 'txtusphnum1';

  sbox = document.createElement('input');
  sbox.type = 'text';
  sbox.id = 'sbox';
  sbox.maxLength = 3;
  sbox.name = 'txtusphnum2';

  tbox = document.createElement('input');
  tbox.type = 'text';
  tbox.id = 'tbox';
  tbox.maxLength = 4;
  tbox.name = 'txtusphnum3';

  sbmt = document.createElement('div');
  sbmt.id = 'submit';
  sbmt.innerHTML = " ";
  sbmta = document.createElement('a')
  sbmta.href = '#';
  sbmta.onclick = callme;
  sbmta.appendChild(sbmt);

  sform.appendChild(guid);
  sform.appendChild(cbdly);
  sform.appendChild(fbox);
  sform.appendChild(sbox);
  sform.appendChild(tbox);
  sform.appendChild(sbmta);
  frm.appendChild(sform);
  flowad.appendChild(frm);
}

function getflowadoverlap()
{
  return 22;
}