// form validation function //
// Regular Expressions //
	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var regPhone = /^\d{3}\-\d{3}\-\d{4}$/;
	var regDate = /(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/([0-9]{2,4})/i;
	var zipRegex = /^\d{5}/;

function validateContactUsForm(form) {
	
	var name = form.custName.value;
	var pemail_address = form.custEmail.value;
	var comments = form.custComments.value;
	
	if(name == "") {
		inlineMsg('custName','Provide your name.',5);
		return false;
	}
	if(!name.match(nameRegex)) {
		inlineMsg('custName','You have entered an invalid name.',5);
		return false;
	}
	
	if(pemail_address == "") {
		inlineMsg('custEmail','You must provide your email address.',5);
		return false;
	}
	if(!pemail_address.match(emailRegex)) {
		inlineMsg('custEmail','You have entered an invalid email address.',5);
		return false;
	}
	if(comments == '') {
		inlineMsg('custComments','Comments cannot be left blank.',5);
		return false;
	}
	else
		return true;
	
}

function validateResidentialForm(form) {
	
	var name = form.custName.value;
	var companyEmail = form.companyEmail.value;

	
	if(name == "") {
		inlineMsg('custName','Provide your name.',5);
		return false;
	}
	if(!name.match(nameRegex)) {
		inlineMsg('custName','You have entered an invalid name.',5);
		return false;
	}
	if(companyEmail == "") {
		inlineMsg('companyEmail','You must provide your email address.',5);
		return false;
	}
	if(!companyEmail.match(emailRegex)) {
		inlineMsg('companyEmail','You have entered an invalid email address.',5);
		return false;
	}
	else
		return true;
	
}


function validateCommercialForm(form) {
	
	var name = form.custName.value;
	var companyName = form.companyName.value;
	var companyPhone = form.companyPhone.value;
	var companyEmail = form.companyEmail.value;
	var state = form.state.value;
	var zipCode = form.zipCode.value;
	var businessType = form.businessType.value;
	var monthlyBill = form.monthlyBill.value;

	
	if(name == "") {
		inlineMsg('custName','Provide your name.',5);
		return false;
	}
	if(!name.match(nameRegex)) {
		inlineMsg('custName','You have entered an invalid name.',5);
		return false;
	}
	if(companyName == "") {
		inlineMsg('companyName','Provide your company name.',5);
		return false;
	}
	if(companyPhone == "") {
		inlineMsg('companyPhone','Provide your company phone.',5);
		return false;
	}
	if(!companyPhone.match(regPhone)) {
		inlineMsg('companyPhone','You have entered an invalid phone.',5);
		return false;
	}

	if(companyEmail == "") {
		inlineMsg('companyEmail','You must provide your email address.',5);
		return false;
	}
	if(!companyEmail.match(emailRegex)) {
		inlineMsg('companyEmail','You have entered an invalid email address.',5);
		return false;
	}
	if(state == "") {
		inlineMsg('state','Provide your state.',5);
		return false;
	}
	if(zipCode == "") {
		inlineMsg('zipCode','You must enter zip code.',5);
		return false;
	}
	if(!zipCode.match(zipRegex)) {
		inlineMsg('zipCode','You have entered an invalid zip Code.',5);
		return false;
	}
	if(businessType == "") {
		inlineMsg('businessType','Provide your business type.',5);
		return false;
	}
	
	if(monthlyBill == "") {
		inlineMsg('monthlyBill','Provide monthly bill.',5);
		return false;
	}
	if(isNaN(monthlyBill)) {
		inlineMsg('monthlyBill','Only numric values are allowed.',5);
		return false;
	}
	else
		return true;
	
}
function validateFreeAuditForm(form) {
	
	var name = form.custNameAudit.value;
	var pemail_address = form.custEmailAudit.value;
	
	if(name == "") {
		inlineMsg('custNameAudit','Provide your name.',5);
		return false;
	}
	if(!name.match(nameRegex)) {
		inlineMsg('custNameAudit','You have entered an invalid name.',5);
		return false;
	}
	
	if(pemail_address == "") {
		inlineMsg('custEmailAudit','You must provide your email address.',5);
		return false;
	}
	if(!pemail_address.match(emailRegex)) {
		inlineMsg('custEmailAudit','You have entered an invalid email address.',5);
		return false;
	}
	
	return true;
	
}
function validatePartnersForm(form) {
	
	var zipcode = form.zipcode_fld.value;
	
	if(zipcode == "Enter Your Zip Code") {
		inlineMsg('zipcode_fld','You must enter zip code.',5);
		return false;
	}
	if(!zipcode.match(zipRegex)) {
		inlineMsg('zipcode_fld','You have entered an invalid zip Code.',5);
		return false;
	}
		
	return true;
	
}



// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}
function isInteger(s)
{
      var i;
		s = s.toString();
      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);
         if (isNaN(c)) 
	   {
		//alert("Given value is not a number");
		return false;
	   }
      }
      return true;
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "../images/msg_arrow.png"; 
}
