var firstEl;
function validateForm(frm) {
  errs = "";
  firstEl = null;
  els = frm.elements;
  for(i=0; i<els.length; i++) {
    if(els[i].id == "req")
      errs += checkVal(els[i]);
  }
  if(errs != "") {
    alert("Please make the following changes:\n\n" + errs);
    if(firstEl)
      firstEl.focus();
    return false;
  }
  return true;
}

function checkVal(el) {
  if(el) {
    if(((el.tagName == "INPUT" || el.tagName == "TEXTAREA") && el.value == "")
       || (el.tagName == "SELECT" && el.options[el.selectedIndex].value == "")) {
      if(firstEl == null)
        firstEl = el;
      return "- " + el.title + "\n";
    }
  }
  return "";
}
function goTo(u) {
  window.location = u;
}

function popup(url, width, height, scrolling, name) {
  openPopupWindow(url, width, height, scrolling, name);
}

function openPopupWindow(url, width, height, scrolling, name) {
  if (!width) width = 350;
  if (!height) height = 350;
  if (!scrolling) scrolling = "no"; 
  if (!name) name = "win"+Math.round(Math.random()*1000); 
  
  features = "width="+width+","
           + "height="+height+","
           + "toolbar=no,"
           + "location=no,"
           + "status=no,"
           + "menubar=no,"
           + "scrollbars="+scrolling+","
           + "top=50,"//+(window.screen.height-height)/2+","
           + "left=50";//+(window.screen.width-width)/2;
  window.open(url,name,features);
}

function openLegal() {
  openPopupWindow('/legal.jsp', 600, 400, 'yes', 'legalWin');
}

function watchVid() {
  popup('/vid.html',452,293,'no','vidWin');
}

function signup() {
  popup("/construction/signup",600,400,'no','signupWin');
}

function _clear(el) {
  t = el.title;
  if(el.value == t)
    el.value = '';
}

function _blur(el) {
  t = el.title;
  if(el.value == '')
    el.value = t;
}

function _validate(frm) {
    n = frm.name;
    pn = frm.phone;
    eml = frm.email;
    at = eml.value.indexOf('@');
    if(n.value == n.title || n.value == '') {
      alert("Please enter your name");
      n.focus();
      return false
    } else if(pn.value == pn.title || pn.value == '') {
        alert('Please enter your phone number');
        pn.focus();
        return false;
    } else if(pn.value.length < 10) {
        alert('Please enter a valid phone number including area code');
        pn.focus();
        return false;
    } else if(eml.value == eml.title || eml.value == '') {
      alert('Please enter email address');
      eml.focus();
      return false;
    } else if(at == -1 || eml.value.indexOf('.', at) <= at) {
      alert('Please enter a valid email address');
      eml.focus();
      return false;
    }
    return true;
  }

var skipCap = false;
function dropCap() {
  if(!skipCap && document.getElementById("page-text")) {
    t = document.getElementById("page-text").innerHTML;
    ok = "<>abcdefghijklmnopqrstuvwxyz";
    for(i=0; i<t.length; i++) {
      c = t.charAt(i).toLowerCase();
      if(ok.indexOf(c) == -1) {
        //skip it
      } else if(c == '<') {
        i = t.indexOf('>', i);
      } else {
        document.getElementById("page-text").innerHTML = t.substring(0,i)
            + '<img src="/images/letters/'+c+'.png" class="cap" />'
            + t.substring(i+1);
        break;
      }
    }
  }
}

$(document).ready(function() {
  h = 0;
  $("#ftr .brd").each(function() {
      if($(this).height() > h)
        h = $(this).height();
  });
  $("#ftr .brd").css("height", h+"px");
  $(".middle").each(function() {
      t = $(this);
      p = t.parent();
      m = h; //p.height();
      m -= t.height();
      m -= p.children("h3:first").height();
      t.css("margin-top", m/2+"px");
  });
  dropCap();
});

