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 addEvent( obj, type, fn ){  // the add event function
  if (obj.addEventListener) obj.addEventListener( type, fn, false );
  else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() {
      obj["e"+type+fn]( window.event );
    };
    obj.attachEvent( "on"+type, obj[type+fn] );
  }
}


/** MOUSE OVER EFFECTS **/
function preloadImgs() {
  imgs = document.images;
  for(i=0; i<imgs.length; i++) {
    src = imgs[i].src;
    if(imgs[i].className != 'no-hover' && src.indexOf('_off') != -1) {
      document.onImg = new Image();
      document.onImg.src = src.replace('_off', '_on');

      addEvent(imgs[i], "mouseover", toggleImg);
      addEvent(imgs[i], "mouseout", toggleImg);
    }
  }
  imgs = document.getElementsByTagName("input");
  for(i=0; i<imgs.length; i++) {
    if(imgs[i].type.toLowerCase() == 'image') {
      src = imgs[i].src;
      if(imgs[i].className != 'no-hover' && src.indexOf('_off') != -1) {
        document.onImg = new Image();
        document.onImg.src = src.replace('_off', '_on');

        addEvent(imgs[i], "mouseover", toggleImg);
        addEvent(imgs[i], "mouseout", toggleImg);
      }
    }
  }
}

addEvent(window, "load", preloadImgs);

function toggleImg() {
  src = this.src;
  if(src.indexOf('_off') != -1)
    this.src = src.replace('_off', '_on');
  else if(src.indexOf('_on') != -1)
    this.src = src.replace('_on', '_off');
}

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 viewLegal() {
  openPopupWindow('/disclaimer', 600, 400, 'yes', 'legalWin');
}

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

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;
}

/*
 * Image preview script
 * powered by jQuery (http://www.jquery.com)
 *
 * written by Alen Grakalic (http://cssglobe.com)
 *
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

this.imagePreview = function(){
	/* CONFIG */

    // assumes set height on the images
    imgHeight = 400;
		xOffset = imgHeight/2;
		yOffset = 30;

		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result

	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";
    var imgSrc = $(this).attr("popup");
    if(!imgSrc)
      imgSrc = this.href;
		$("body").append("<p id='preview'><img src='"+ imgSrc +"' alt='Image preview' />"+ c +"</p>");

		$("#preview")
			.css("top",getTop(e.pageY) + "px")
			.css("left",getLeft(e.pageX) + "px")
			.fadeIn("fast");
    },
    function(){
      this.title = this.t;
      $("#preview").remove();
      });
    $("a.preview").mousemove(function(e){
      $("#preview")
        .css("top",getTop(e.pageY) + "px")
        .css("left",getLeft(e.pageX) + "px");
    }
  );

  function getTop(eventY) {
    previewHeight = $("#preview").height();
    scrollTop = $(document).scrollTop();
    scrollBottom = scrollTop + $(window).height();
    marginTop = 10; marginBottom = 50;
    xOffset = previewHeight/2;
    _top = eventY - xOffset;
    //alert(eventY + " + " + xOffset + " > " + $(window).height());
    if(eventY-xOffset < (scrollTop+marginTop))
      _top = scrollTop+marginTop;
    else if(eventY+xOffset > (scrollBottom-marginBottom))
      _top = scrollBottom-imgHeight-marginBottom;
    return _top;
  }

  function getLeft(eventX) {
    var offset = 30;
    if(eventX > ($(window).width()/2))
      offset = -630;

    return eventX + offset;
  }
};

// starting the script on page load
$(document).ready(function(){
	imagePreview();
});

