function ge(e) {
  return document.getElementById(e);
}

function form_getParamDataByName(name) {
  var obj = ge(name);
  if (obj) 
    return "&"+name+"="+encodeURIComponent(obj.value);
  else
	return null;
}

function setElementContent(eleName,html) {
  if (ge(eleName))  
    ge(eleName).innerHTML = html;
}

function getElementText(el) {
  if (!el) { return false; }
  if (el.textContent) return el.textContent;
  if (el.innerText) return el.innerText;
  return el.innerHTML.replace(/<[^>]*>/g,'');
}

function htmlentities(strInput) {
  var chars = new Array ('&','>','<');
  var entities = new Array ('amp','gt','lt');

  var newString = strInput;
  for (var i = 0; i < chars.length; i++)
  {
    myRegExp = new RegExp();
    myRegExp.compile(chars[i],'g')
    newString = newString.replace (myRegExp, '&' + entities[i] + ';');
  }
  return newString;

}

function savePageHandler () {
  if (ajaxRequest.readyState == 4) {
    if (ajaxRequest.status == 200) {
      currentAjaxHandler(1,ajaxRequest.responseText);
      return true;
    }
    currentAjaxHandler(0,'Failed status '+ajaxRequest.status);
   }
}

function displayErrors(strErrorDivId, aryErrors, nStartIndex) {
  var strErrorHTML = "<p>There were errors in your submission:</p>\n<ul id='errors'>\n";
  for (i = nStartIndex; i < aryErrors.length; i++) {
    strErrorHTML += "<li style='margin-left:20px;'>"+aryErrors[i]+"</li>\n";
  }
  strErrorHTML += "</ul>\n";
  setElementContent(strErrorDivId, strErrorHTML);
}

function submitDemoRequest() {
  ge("contact-form-info").innerHTML = "";
  var aryDemoRequestFields = ['customername','companyname','email','phone','location'];
  var aryDemoRequestLabels = ['Name','Company','E-mail','Phone','Country'];
  var aryErrors = [];
  var strRequestParams = "submitted=1&ajax=1&type=demorequest";

  for (var i = 0; i < aryDemoRequestFields.length; i++) {
    if (ge(aryDemoRequestFields[i]).value.length  < 2)
      aryErrors[aryErrors.length] = aryDemoRequestLabels[i]+" is a required field.\n";
    else {
      strRequestParams += form_getParamDataByName(aryDemoRequestFields[i]);
    }
  }
  
  if (aryErrors.length === 0) {
    ge("contact-form-contents").style.display = "none";
    setElementContent("contact-form-info", "<div><p id=\"contact-form-loader\">Submitting...</p></div>");
    $.post('/contact-us', strRequestParams, function(data) {
      if (data.charAt(1) == ',') {
        var aryResult = data.split(',');
        if (aryResult[0] == '1') {
          $('#contact-form-info').html("<p>Thank you! Your request for a demo has been received. <br/><br/>We will be in touch with you shortly.</p>");
          
          // Analytics Goal Tracking
          pageTracker._trackPageview("/demo-request-submitted/");
          
          // AdWords conversion tracking
          var google_conversion_id = 1065270537;
          var google_conversion_language = "en_GB";
          var google_conversion_format = "3";
          var google_conversion_color = "ffffff";
          var google_conversion_label = "_r5ACN-XlwEQifr6-wM";
          if (10.0) {
            var google_conversion_value = 10.0;
          }
          eval('<scr' + 'ipt type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></scr' + 'ipt>');
          
          return true;
        } else {
          displayErrors("contact-form-info", aryResult, 1);
          $("#contact-form-contents").css('display', "block");
          return false;
        }
      }
    });
  } else {
    displayErrors("contact-form-info", aryErrors, 0);
  }
  return false;
}
