﻿// Flag Variables
var bForceUnLoadPostback = false;
var isCCS, isIE6, isNN7;

var GMap = null;
var GDir = null;

//****************************************************************************
// Method to determine the type of browser
function initDHTMLAPI()
{
  if (document.images)
  {
    isCCS = (document.body && document.body.style) ? true : false;
    isIE6 = (isCCS && document.all) ? true : false;
    isNN7 = (isIE6) ? false : true;
  }
}

//*****************************************************************************
// Called when the page is loaded
function OnLoad()
{
  initDHTMLAPI();
  
  var oSubmit = document.getElementById('Submit');
  if (oSubmit != null)
  {
    if (oSubmit.getAttribute('EmailSent').value == 'Yes')
      alert('We have received your Contact Us Form. Thank You.');
  }
}

//*****************************************************************************
// Called when the form is unloading
function OnBeforeUnLoad()
{
  try
  {
  }
  
  catch (e)
  {
  }
}

//=============================================================================
// SECTION: Contact Us
function OnSubmitContactUs()
{
    try
    {
        if (ValidateContactUsForm())
          __doPostBack('Submit', '');
    }
    
    catch (e)
    {
    }
}

//*****************************************************************************
// Called to submit the contact us form
function ValidateContactUsForm()
{
  var bRetVal = true;
  var sFix    = '';
  
  try
  {
    // Name
    var oObject = document.getElementById('Name');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      sFix += '- Full Name ';
      bRetVal = false;
      oObject.attributes['class'].value = 'inputboxerror';
    }
    else
    {
      oObject.attributes['class'].value = 'inputbox';
      if (sTest.length > 128)
        oObject.value = oObject.value.substr(0, 128);
    }
    
    // Email
    var oObject = document.getElementById('Email');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      sFix += '- Email Address ';
      bRetVal = false;
      oObject.attributes['class'].value = 'inputboxerror';
    }
    else
    {
      if (ValidateEmail(oObject) == false)
      {
        sFix += '- Email Address ';
        bRetVal = false;
        oObject.attributes['class'].value = 'inputboxerror';
      }
      else
      {
        oObject.attributes['class'].value = 'inputbox';
        if (sTest.length > 256)
          oObject.value = oObject.value.substr(0, 256);
      }
    }
    
    // Phone Number
    var oObject = document.getElementById('Phone');
    sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      sFix += '- Phone Number ';
      bRetVal = false;
      oObject.attributes['class'].value = 'inputboxerror';
    }
    else
    {
      if (!ValidateTelephone(oObject))
      {
        sFix += '- Phone Number ';
        bRetVal = false;
        oObject.attributes['class'].value = 'inputboxerror';
      }
      else
      {
        oObject.attributes['class'].value = 'inputbox';
        if (sTest.length > 20)
          oObject.value = oObject.value.substr(0, 20);
      }
    }
    
    // Address
    var oObject = document.getElementById('Address');
    sTest = TrimString(oObject.value);
    if (sTest.length > 128)
      oObject.value = oObject.value.substr(0, 128);
    
    // City
    var oObject = document.getElementById('City');
    sTest = TrimString(oObject.value);
    if (sTest.length > 128)
      oObject.value = oObject.value.substr(0, 128);
    
    // State
    var oObject = document.getElementById('State');
    sTest = TrimString(oObject.value);
    if (sTest.length > 128)
      oObject.value = oObject.value.substr(0, 128);
    
    // Zipcode
    var oObject = document.getElementById('Zipcode');
    sTest = TrimString(oObject.value);
    if (sTest.length > 10)
      oObject.value = oObject.value.substr(0, 10);
    
    // FindSite
    var oObject = document.getElementById('FindSite');
    sTest = TrimString(oObject.value);
    if (sTest.length > 128)
      oObject.value = oObject.value.substr(0, 128);
    
    // Comments
    var oObject = document.getElementById('Comments');
    sTest = TrimString(oObject.value);
    if (sTest.length > 512)
      oObject.value = oObject.value.substr(0, 512);
  }
  
  catch (e)
  {
    bRetVal = false;
  }
  
  if (bRetVal == false)
    alert('Please provide the missing required fields. ' + sFix);
  
  return bRetVal;
}

//=============================================================================
// SECTION: Helper Functions

//*****************************************************************************
// Called to validate email address  
function ValidateEmail(oTextBox)
{
  var bRetVal = false;
  
  try
  {
    // Email Regex object - email@some.com
    var RxPattern = /([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})/;
    
    if (oTextBox.value.length > 0)
    {
      // Create Regular expression object
      var oRegEx = new RegExp(RxPattern);
      
      // Test input field with regular expression
      var oResult = oRegEx.exec(oTextBox.value);
      if (oResult != null)
        bRetVal = true;
    }
  }
  
  catch (e)
  {
  }
  
  return bRetVal;
}

//*****************************************************************************
//** Called to validate Telephone Numbers
function ValidateTelephone(oTextBox)
{
  var bRetVal = false;
  
  try
  {
    // Validate zip code (123) 123-1234, 123.123.1234, (123) 123.1234
    var RxPattern = /((\(\d{3}\) ?)|(\d{3}[- \. \s]))?\d{3}[- \.]\d{4}(\s(x\d+)?){0,1}$/;
    
    if (oTextBox.value.length > 0)
    {
      // Create Regular expression object
      var oRegEx = new RegExp(RxPattern);
      
      // Test input field with regular expression
      var oResult = oRegEx.exec(oTextBox.value);
      if (oResult != null)
        bRetVal = true;
    }
  }
  
  catch (e)
  {
  }
  
  return bRetVal;
}

//*****************************************************************************
// Romoves leading and trailing spaces on a string.
// 's' represents any input string.
function TrimString(s) 
{
  try
  {
    // Trim the string
    s = s.replace(/^\s|\s$/g, ""); s.substring(0, s.length-1);
  }
  
  catch (e)
  {
  }
  
  return s;
}

//*****************************************************************************
// Called to load the google map for each address
function OnLoadMap(sType)
{
    if (GBrowserIsCompatible())
    {
        GMap = new GMap2(document.getElementById('Map'));
        GMap.addControl(new GSmallMapControl());

        GDir = new GDirections(GMap, document.getElementById("Directions"));
        GEvent.addListener(GDir, "error", OnHandleErrors);

        var sMarkerDisplay = '<table cellpadding="0" cellspacing="0" border="0" style="color: #000066">';
        sMarkerDisplay += '<tr><td height="8"></td></tr>';
        sMarkerDisplay += '<tr><td><b>Ellsworth Law Firm, PA</b></td></tr>';
        sMarkerDisplay += '<tr><td height="5"></td></tr>';
        sMarkerDisplay += '<tr><td>1501 Collins Avenue, Suite 208</td></tr>';
        sMarkerDisplay += '<tr><td>Miami Beach, FL 33139</td></tr>';
        sMarkerDisplay += '<tr><td>Telephone: (305) 535-2529</td></tr>';
        sMarkerDisplay += '</table>';

        ShowAddress(GMap, '1501 Collins Avenue, Miami Beach, FL 33139', sMarkerDisplay);
    }
}

//*****************************************************************************
// Called to perform the geo-coding with the google API
function ShowAddress(oMap, sAddress, sMarkerDisplay)
{
    if (GBrowserIsCompatible()) {
        var pGeocoder = new GClientGeocoder();
        if (pGeocoder) {
            pGeocoder.getLatLng(
        sAddress,
        function(pPoint) {
            if (!pPoint) {
                alert(sAddress + " not found");
            }
            else {
                oMap.setCenter(pPoint, 13);
                var pMarker = new GMarker(pPoint);
                oMap.addOverlay(pMarker);
                pMarker.openInfoWindowHtml(sMarkerDisplay);
            }
        }
      );
        }
    }
}

//*****************************************************************************
// Called to get directions
function OnGetDirections()
{
    var sFromAddress = document.getElementById('FromAddress').value;
    var sToAddress = '1501 Collins Avenue, Miami Beach, FL 33139';
    var sLocale = 'en_US';

    GDir.load("from: " + sFromAddress + " to: " + sToAddress, { "locale": sLocale });
}

//*****************************************************************************
// Called to handle google api errors
function OnHandleErrors()
{
    if (GDir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + GDir.getStatus().code);
    else if (GDir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + GDir.getStatus().code);

    else if (GDir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + GDir.getStatus().code);

    else if (GDir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + GDir.getStatus().code);

    else if (GDir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + GDir.getStatus().code);

    else alert("An unknown error occurred.");
}