﻿function initOverLabels () 
{
  if (!document.getElementById) return;      

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  labels = document.getElementsByTagName('label');
 
  for (var j = 0; j < labels.length; j++) {
    if (labels[j].className == 'hidden') {
          // Skip labels that do not have a named association with another field.
          id = labels[j].htmlFor || labels[j].getAttribute('for');
          if (!id || !(field = document.getElementById(id))) {
            continue;
          } 
          
          // Change the applied class to hover the label over the form field.
          labels[j].className = 'overlay';

          // 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[j].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 j = 0; j < labels.length; j++) {
    field_for = labels[j].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[j].className = (hide) ? 'hidden' : 'overlay';
      return true;
    }
  }
}
window.onload = function () 
{
  setTimeout(initOverLabels, 50);
};


var _httpRequest = null;
var _POST_CONTACT_INFO_URL = '/contactinfo.aspx';
var _NAME_FIELD_ID = 'txtName';
var _COMPANY_FIELD_ID = 'txtCompany';
var _CONTACT_FIELD_ID = 'txtNumber';
var _MESSAGE_FIELD_ID = 'txtMessage';

function SubmitEmail()
{
    var fieldEmail = document.getElementById('email');
    var email = fieldEmail.value;
    
    reg = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
    if(!reg.test(email))
    {
        alert('Sorry but your email doesn\'t seem valid, please try again.');
        
        return;
    }

    var postParameters = _NAME_FIELD_ID + '=' + 'newsletter' + 
                    '&' + _COMPANY_FIELD_ID + '=' + 'novologies.com' +  
                    '&' + _CONTACT_FIELD_ID + '=' + email +  
                    '&' + _MESSAGE_FIELD_ID + '=' + '';
                       
    PostContactInfo(postParameters);
}
function PostContactInfo(postParameters)
{
    _httpRequest = GetXMLHttpRequestObject();
    if(_httpRequest==null)
    {
        alert('There seems to be a problem, please try again shortly.');
        return;
    }
    
    //sets up the object to send the data
     _httpRequest.onreadystatechange = OnHttpRequestStatusChange;

     _httpRequest.open('POST', _POST_CONTACT_INFO_URL, true);
     _httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     _httpRequest.setRequestHeader("Content-length", postParameters.length);
     _httpRequest.setRequestHeader("Connection", "close");
     _httpRequest.send(postParameters);
}
function OnHttpRequestStatusChange() 
{
    if (_httpRequest.readyState != 4) 
       return;

     if (_httpRequest.status == 200) //all is good
     {
           alert('Great! We hope our articles will offer an interesting glimpse into a variety of topics and how we do things.');
           var fieldEmail = document.getElementById('email');
           fieldEmail.value = '';
     } 
     else //there was an error submitting info
     {
        alert('There seems to be a problem, please try again shortly.');
     }
}
function GetXMLHttpRequestObject()
{
    try
    {
        var xhr = null;
        if (window.XMLHttpRequest)     // Object of the current windows
        { 
            xhr = new XMLHttpRequest();     // Firefox, Safari, ...
        } 
        else if (window.ActiveXObject)   // ActiveX version
        {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
        } 
 
        return xhr;
    }
    catch(e)
    {
        return null;
    }
}

function checkCR(evt) {
    var evt  = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = checkCR; 