// - - - - - - - - - - - - - - - - - - - - -
//
// Title : Form Validator
// Author : Philipp Sackl
// URL : http://www.envis-precisely.com
// - - - - - - - - - - - - - - - - - - - - -

function validateForm(formId)
{
	var form = $(formId);
	var ok = true;
	for (var i = 0; i < form.elements.length; i++)
	{
		var el = form.elements[i];
		if (!validateField(el)) ok = false;
	}
	
	return ok;
}


function validateField(el)
{
	Element.extend(el);
	var ok = true;
	if (el.id == "author")
	{
		if (trim(el.value) == "") 
		{
			el.value = "";
			el.style.background = "#EEDBDB";
			el.previousSiblings()[0].style.backgroundPosition = "0px -20px";
		}
		else
		{
			el.style.background = "#F1F1EA";
			el.previousSiblings()[0].style.backgroundPosition = "0px 0px";
		}
	}
	
	
	if (el.id == "email")
	{
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(el.value))
		{
			ok = false;
			el.style.background = "#EEDBDB";
			el.previousSiblings()[0].style.backgroundPosition = "0px -20px";
		}
		else
		{
			el.style.background = "#F1F1EA";
			el.previousSiblings()[0].style.backgroundPosition = "0px 0px";
		}
	}
	
	if (el.id == "comment")
	{
		if (trim(el.value) == "") 
		{
			ok = false;
			el.value = "";
			el.style.background = "#EEDBDB";
		}
		else
		{
			el.style.background = "#F1F1EA";
		}
	}
	
	
	return ok;
}


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


var hasSubmittedForm = false;
window.onload = function()
{
	if ($("commentform"))
	{
		$("submit").remove();
		var newSubmit = new Element("input", {"type": "button", "name": "submit", "id": "submitButton", "value": "Post Comment"});
		$$(".form-submit")[0].appendChild(newSubmit);
		newSubmit.onclick = function() 
		{
			if (hasSubmittedForm == false)
			{
				if (validateForm("commentform"))
				{
					hasSubmittedForm = true;
					new Ajax.Request("http://www.envis-precisely.com/blog/wp-comments-post.php", {
						parameters: $("commentform").serialize(true),
						onSuccess: function(transport)
						{
							location.reload();
						}
					});
				}
			}
		}
	}
}



// - - - - - - - - - - - - - - - - - - - - -
//
// Title : Dynamic Resolution Dependent Layout Demo
// Author : Kevin Hale
// URL : http://particletree.com
//
// Description : This is a demonstration of a dynamic 
// resolution dependent layout in action. Change your browser 
// window size to see the layout respond to your changes. To 
// preserve the separation of the presentation and behavior 
// layers, this implementation delegates all the presentation 
// details to external CSS stylesheets instead of changing 
// each style property through JavaScript.
//
// Created : July 30, 2005
// Modified : November 15, 2005
//
// - - - - - - - - - - - - - - - - - - - - -

// getBrowserWidth is taken from The Man in Blue Resolution Dependent Layout Script
// http://www.themaninblue.com/experiment/ResolutionLayout/
function getBrowserWidth(){
	if (window.innerWidth){
		return window.innerWidth;}	
	else if (document.documentElement && document.documentElement.clientWidth != 0){
		return document.documentElement.clientWidth;	}
	else if (document.body){return document.body.clientWidth;}		
		return 0;
}

// dynamicLayout by Kevin Hale
function dynamicLayout(){
		
	var browserWidth = getBrowserWidth();

	//Load Thin CSS Rules
	if (browserWidth < 1065){
		changeLayout("thin");
	}
	//Load Wide CSS Rules
	if ((browserWidth >= 1065) /*&& (browserWidth <= 1465)*/){
		changeLayout("wide");
	}
	/*
	//Load Wider CSS Rules
	if (browserWidth > 1465){
		changeLayout("wider");
	}
	*/
}

// changeLayout is based on setActiveStyleSheet function by Paul Sowdon 
// http://www.alistapart.com/articles/alternate/
function changeLayout(description)
{
	var i, a, def;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1)
		{
			if(a.getAttribute("title") == description)
			{
				a.disabled = false;
			}
			else if(a.getAttribute("title") != "default")
			{
				a.disabled = true;
			}
			else if(a.getAttribute("title") == "default")
			{
				def = a;
			}
		}
	}
}

//addEvent() by John Resig
function addEvent( obj, type, fn ){ 
   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] ); 
   } 
} 

//Run dynamicLayout function when page loads and when it resizes.
addEvent(window, 'load', dynamicLayout);
addEvent(window, 'resize', dynamicLayout);
/*
addEvent(window, 'load', initOverLabels);
*/

// - - - - - - - - - - - - - - - - - - - - -
//
// Title : Making Compact Forms More Accessible
// Author : MIKE BRITTAIN
// URL : http://www.alistapart.com/articles/makingcompactformsmoreaccessible
//
// Description : Move form Label into its input field
//
// Created : December 19, 2006
// Modified : October 6, 2010 by envis precisely
//
// - - - - - - - - - - - - - - - - - - - - -

function initOverLabels () {
  if (!document.getElementById) return;      

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // LABELs with '' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {

    if (labels[i].className == '') {
      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute ('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      } 

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to LABEL elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i]. getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-1500px' : '0px';
      return true;
    }
  }
}

// Created by: Simon Willison
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  setTimeout(initOverLabels, 50);
});

