﻿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';
var _EL_SEND_LINK = 'contactSend';
var _EL_SENDING_DIV = 'contactSending';
var _CONTACT_INFO_ERROR_MESSAGE = 'There was a problem sending your message. Please retry or send us an email instead.';
var _CONTACT_INFO_SUCESS_MESSAGE = 'Thanks for the message. We will be in touch soon.';
//-----------------------------------------
//sends the contact information to us
//-----------------------------------------
function SendContactInfo()
{
    var fieldName = document.getElementById(_NAME_FIELD_ID);
    var fieldContactInfo = document.getElementById(_CONTACT_FIELD_ID);
    
    var company = document.getElementById(_COMPANY_FIELD_ID).value;
    var message = document.getElementById(_MESSAGE_FIELD_ID).value;
    
    var name = fieldName.value;
    if(name.length < 2)
    {
        alert('Please enter your name, thanks.');
        fieldName.focus();
        return;
    }
       
    var contactInfo = fieldContactInfo.value;
    if(contactInfo.length < 5)
    {
        alert('Please enter your email or phone, thanks.');
        fieldContactInfo.focus();
        return;
    }

    var postParameters = _NAME_FIELD_ID + '=' + encodeURI(name) + 
                    '&' + _COMPANY_FIELD_ID + '=' + encodeURI(company) +  
                    '&' + _CONTACT_FIELD_ID + '=' + encodeURI(contactInfo) +  
                    '&' + _MESSAGE_FIELD_ID + '=' + encodeURI(message);
                       
    PostContactInfo(postParameters);
}
//-----------------------------------------
//posts the contact info to the server
//-----------------------------------------
function PostContactInfo(postParameters)
{
    _httpRequest = GetXMLHttpRequestObject();
    if(_httpRequest==null)
    {
        alert(_CONTACT_INFO_ERROR_MESSAGE);
        return;
    }
    
    ToggleSendingStatus();
    
    //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);
}
//-----------------------------------------
//fired when the status of the http request object changes, so we can determine when the data transfer has been completed
//-----------------------------------------
function OnHttpRequestStatusChange() 
{
    if (_httpRequest.readyState != 4) 
       return;
    
    ToggleSendingStatus();

     if (_httpRequest.status == 200) //all is good
     {
           alert(_CONTACT_INFO_SUCESS_MESSAGE);
           ClearContactInfo();
     } 
     else //there was an error submitting info
     {
        alert(_CONTACT_INFO_ERROR_MESSAGE);
     }
}
//-----------------------------------------
//clears the data on the contact form after submitting it to the server
//-----------------------------------------
function ClearContactInfo()
{
    ClearTextField(_NAME_FIELD_ID);
    ClearTextField(_COMPANY_FIELD_ID);
    
    ClearTextField(_CONTACT_FIELD_ID);
    ClearTextField(_MESSAGE_FIELD_ID);
}
//-----------------------------------------
//hides or shows sending status
//-----------------------------------------
function ToggleSendingStatus()
{
    
    if (YAHOO.util.Dom.getStyle(_EL_SENDING_DIV, 'display') == 'none') {
        YAHOO.util.Dom.setStyle(_EL_SENDING_DIV, 'display', 'block');
        YAHOO.util.Dom.setStyle(_EL_SEND_LINK, 'display', 'none');
    }
    else {
        YAHOO.util.Dom.setStyle(_EL_SENDING_DIV, 'display', 'none');
        YAHOO.util.Dom.setStyle(_EL_SEND_LINK, 'display', 'block');
    }
}
//-----------------------------------------
//creates the HttpRequest, and return it back
//-----------------------------------------
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;
    }
}
//-----------------------------------------
//clears the text on the specified field
//-----------------------------------------
function ClearTextField(id)
{
    var f = document.getElementById(id);
    if(f==null || !f.value)
        return;
        
    f.value='';
}