// Flag Variables
var bForceUnLoadPostback = false;
var isCCS, isIE6, isNN7;

var oSubMenu = null;
var sMenu    = '';

var GMap = null;
var GMap = null;

var oNewsText            = null;
var iNewsTextWidth       = 0;
var iCurrentNewsTextLeft = 845;
var iNewsBannerWidth     = 845;

//****************************************************************************
// 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;
  }
}

//****************************************************************************
// Method for Netscape browsers to find html controls
function SeekLayer(doc, name)
{
  var theObj;
  
  for (var i = 0; i < doc.forms[0].elements.length; i++)
  {
    if (doc.forms[0].elements[i].name == name)
    {
      theObj = doc.forms[0].elements[i];
      break;
    }
  }
  
  return theObj;
}

//****************************************************************************
// Method to abstract the access to html controls
function GetElementById(obj)
{
  var theObj;
  
  if (isIE6)
    theObj = document.getElementById(obj);
  else
  {
    theObj = document.getElementById(obj);
    if (theObj == null)
      theObj = SeekLayer(document, obj);
  }
   
  return theObj;
}

//****************************************************************************
// Called to show the sub menu
function ShowSubMenu(sId)
{
  if (oSubMenu != null)
  {
    clearTimeout(oSubMenu);
    oSubMenu = null;
  }
  
  // If a different panel is open close it immediately
  if ((sMenu.length > 0) && (sMenu != sId))
    WaitCloseSubMenu(sMenu);
  
  sMenu = sId;
  GetElementById('submenu' + sId).style.display = '';
}

//****************************************************************************
// Called to close the sub menu
function CloseSubMenu(sId)
{
  if (oSubMenu == null)
    oSubMenu = setTimeout("WaitCloseSubMenu('" + sId + "')", 500);
}

//****************************************************************************
// Called to close the sub menu
function WaitCloseSubMenu(sId)
{
  GetElementById('submenu' + sId).style.display = 'none';
  oSubMenu = null;
  sMenu    = '';
}

//****************************************************************************
// Called to show the correct panel
function OnServicesPanel(iPanel)
{
  GetElementById('Panel1').style.display = 'none';
  GetElementById('Panel2').style.display = 'none';
  GetElementById('Panel3').style.display = 'none';
  GetElementById('Panel4').style.display = 'none';
  GetElementById('Panel5').style.display = 'none';
  GetElementById('Panel6').style.display = 'none';
  GetElementById('Panel7').style.display = 'none';
  GetElementById('Panel8').style.display = 'none';
  
  GetElementById('Panel' + iPanel.toString()).style.display = '';
}

//****************************************************************************
// Called to open the specified document in its own window
function OpenDocument(sDocument)
{
  var sAttributes = "status=yes, resizable=yes, scrollbars=yes, toolbar=yes, width=900, height=700, top=0, left=0";
  var oWindow = window.open(sDocument, "Document", sAttributes);
}

//*****************************************************************************
// Called when a key stroke is press in the specified text box
function ValidateKeyPress(evt, oTextBox, sType, iLength)
{
  var sCode = (isIE6 == true) ? evt.keyCode : evt.charCode;
  
  // Get the key that was pressed
  var sChar = String.fromCharCode(sCode);
  
  if ((typeof(sType) == 'undefined') || (sType == 'Numeric'))
  {
    // If this is not a number do not allow it
    if (isNaN(parseInt(sChar)) == true)
      if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
  }
  else if (sType == 'Currency')
  {
    // If this is not a number and not a '.'
    if ((isNaN(parseInt(sChar)) == true) && (sChar != '.'))
      if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
  }
  else if (sType == 'Date')
  {
    // If this is not a number and not a '/'
    if ((isNaN(parseInt(sChar)) == true) && (sChar != '/'))
      if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
  }
  else if (sType == 'Phone')
  {
    // If this is not a number and not a '( ) or space'
    if ((isNaN(parseInt(sChar)) == true) && (sChar != '(') && (sChar != ')') && (sChar != ' ') && (sChar != '-'))
      if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
  }
  else if (sType == 'CurrencyNeg')
  {
    // If this is not a number and not a '.'
    if ((isNaN(parseInt(sChar)) == true) && (sChar != '.') && (sChar != '-'))
      if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
  }
  else if (sType == 'SingleDecimal')
  {
    var RxPattern = /^\d+\.{0,1}(\d{1})?$/;
    if (oTextBox.value.length > 0)
    {
      // Create Regular expression object
      var oRegEx = new RegExp(RxPattern);
      oRegEx.Global = true

      // Test input field with regular expression
      var result = oRegEx.exec(oTextBox.value + sChar);
      if (result == null)
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
    }
  }
 
  if (typeof(iLength) != 'undefined')
  {
    if (document.selection.type == 'None')
      if (oTextBox.value.length >= iLength)
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
  }
}

//*****************************************************************************
// Called to validate email address  
function ValidateEmail(oTextBox)
{
  var bRetVal = false;
  
  // 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;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Romoves leading and trailing spaces on a string.
// 's' represents any input string.
function TrimString(s) 
{
  // Remove leading spaces and carriage returns
  while ((s.substring(0, 1) == ' ')  || 
        (s.substring(0, 1) == '\n') || 
        (s.substring(0, 1) == '\r'))
  {
    s = s.substring(1, s.length);
  }

  // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1, s.length) == ' ')  || 
        (s.substring(s.length-1, s.length) == '\n') || 
        (s.substring(s.length-1, s.length) == '\r'))
  {
    s = s.substring(0, s.length-1);
  }
  
  return s;
}

//*****************************************************************************
// Called to validate and submit the contact us form
function OnContactUsSubmit()
{
  if (ValidateContactUsInput())
  {
    GetElementById('SendingEmailPanel').style.display = '';
    
    GetElementById('SubmitContactUs').style.cursor = 'wait';
    
    __doPostBack('Submit','');
  }
}

//*****************************************************************************
// Called to validate the contact us input
function ValidateContactUsInput()
{
  var bValid = true;
  
  // First Name
  sTest = TrimString(GetElementById('FirstName').value);
  if (sTest.length == 0)
  {
    GetElementById('FirstNameError').innerHTML = 'Please provide your first name';
    bValid = false;
  }
  else
    GetElementById('FirstNameError').innerHTML = '';
  
  // Last Name
  sTest = TrimString(GetElementById('LastName').value);
  if (sTest.length == 0)
  {
    GetElementById('LastNameError').innerHTML = 'Please provide your last name';
    bValid = false;
  }
  else
    GetElementById('LastNameError').innerHTML = '';
  
  // Phone
  sTest = TrimString(GetElementById('Phone').value);
  if (sTest.length == 0)
  {
    GetElementById('PhoneError').innerHTML = 'Please provide your phone number';
    bValid = false;
  }
  else
    GetElementById('PhoneError').innerHTML = '';
    
  // Email Address
  sTest = TrimString(GetElementById('Email').value);
  if (sTest.length == 0)
  {
    GetElementById('EmailError').innerHTML = 'Please provide your Email Address';
    bValid = false;
  }
  else
  {
    if (ValidateEmail(GetElementById('Email')))
      GetElementById('EmailError').innerHTML = '';
    else
    {
      GetElementById('EmailError').innerHTML = 'Please provide your proper Email Address';
      bValid = false;
    }
  }
  
  // Comment
  sTest = TrimString(GetElementById('Comment').value);
  if (sTest.length == 0)
  {
    GetElementById('CommentError').innerHTML = 'Please provide your reason for contacting us';
    bValid = false;
  }
  else
    GetElementById('CommentError').innerHTML = '';
  
  return bValid;
}

//*****************************************************************************
// 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);
      
    switch (sType)
    {
      case 'IronBridge':
        var sMarkerDisplay = '<table cellpadding="0" cellspacing="0" border="0" style="color: #000066">';
            sMarkerDisplay += '<tr><td height="8"></td></tr>';
            sMarkerDisplay += '<tr><td><b>Advanced Orthopedics and Sports Medicine Institute</b></td></tr>';
            sMarkerDisplay += '<tr><td height="5"></td></tr>';
            sMarkerDisplay += '<tr><td style="color: #0455A4"><b>Iron Bridge Office</b></td></tr>';
            sMarkerDisplay += '<tr><td>501 Iron Bridge Road Suite 1</td></tr>';
            sMarkerDisplay += '<tr><td>Freehold, NJ 07728</td></tr>';
            sMarkerDisplay += '<tr><td>Ph: 732.720.2555  Fax: 732.720.2556</td></tr>';
            sMarkerDisplay += '</table>';
            
            ShowAddress(GMap, '501 Iron Bridge Road, Freehold, NJ 07728', sMarkerDisplay);
         break;
      
      case 'Route9':
        var sMarkerDisplay = '<table cellpadding="0" cellspacing="0" border="0" style="color: #000066">';
            sMarkerDisplay += '<tr><td height="8"></td></tr>';
            sMarkerDisplay += '<tr><td><b>Advanced Orthopedics and Sports Medicine Institute</b></td></tr>';
            sMarkerDisplay += '<tr><td height="5"></td></tr>';
            sMarkerDisplay += '<tr><td style="color: #0455A4"><b>Route 9 Office</b></td></tr>';
            sMarkerDisplay += '<tr><td>4247 Route 9 North Building 1</td></tr>';
            sMarkerDisplay += '<tr><td>Freehold, NJ 07728</td></tr>';
            sMarkerDisplay += '<tr><td>Ph: 732.720.2555  Fax: 732.720.2556</td></tr>';
            sMarkerDisplay += '</table>';
            
            ShowAddress(GMap, '4247 Route 9 North, Freehold, NJ 07728', sMarkerDisplay);
         break;
       
       case 'PondView':
        var sMarkerDisplay = '<table cellpadding="0" cellspacing="0" border="0" style="color: #000066">';
            sMarkerDisplay += '<tr><td height="8"></td></tr>';
            sMarkerDisplay += '<tr><td><b>Advanced Orthopedics and Sports Medicine Institute</b></td></tr>';
            sMarkerDisplay += '<tr><td height="5"></td></tr>';
            sMarkerDisplay += '<tr><td style="color: #0455A4"><b>Pond View Professional Park</b></td></tr>';
            sMarkerDisplay += '<tr><td>301 Professional View Drive</td></tr>';
            sMarkerDisplay += '<tr><td>Freehold, NJ 07728</td></tr>';
            sMarkerDisplay += '<tr><td>Ph: 732.720.2555  Fax: 732.720.2556</td></tr>';
            sMarkerDisplay += '</table>';
            
            ShowAddress(GMap, '301 Professional View Drive, Freehold, NJ 07728', sMarkerDisplay);
         break;
       
       case 'Renaissance':
        var sMarkerDisplay = '<table cellpadding="0" cellspacing="0" border="0" style="color: #000066">';
            sMarkerDisplay += '<tr><td height="8"></td></tr>';
            sMarkerDisplay += '<tr><td><b>Advanced Orthopedics and Sports Medicine Institute</b></td></tr>';
            sMarkerDisplay += '<tr><td height="5"></td></tr>';
            sMarkerDisplay += '<tr><td style="color: #0455A4"><b>Renaissance Crossing Medical Arts Building</b></td></tr>';
            sMarkerDisplay += '<tr><td>312 Applegarth Road&nbsp;&nbsp;Suite 101</td></tr>';
            sMarkerDisplay += '<tr><td>Monroe Township, NJ 08831</td></tr>';
            sMarkerDisplay += '<tr><td>Ph: 609.235.4100</td></tr>';
            sMarkerDisplay += '</table>';
            
            ShowAddress(GMap, '312 Applegarth Road, Monroe Township, NJ 08831', sMarkerDisplay);
            break;
        case 'Neptune':
            var sMarkerDisplay = '<table cellpadding="0" cellspacing="0" border="0" style="color: #000066">';
            sMarkerDisplay += '<tr><td height="8"></td></tr>';
            sMarkerDisplay += '<tr><td><b>Advanced Orthopedics and Sports Medicine Institute</b></td></tr>';
            sMarkerDisplay += '<tr><td height="5"></td></tr>';
            sMarkerDisplay += '<tr><td style="color: #0455A4"><b>Neptune Office</b></td></tr>';
            sMarkerDisplay += '<tr><td>10 Neptune Blvd, Suite 106</td></tr>';
            sMarkerDisplay += '<tr><td>Neptune, NJ 07753</td></tr>';
            sMarkerDisplay += '<tr><td>Ph: 732-643-5555</td></tr>';
            sMarkerDisplay += '<tr><td>Fax: 732-720-2556</td></tr>';
            sMarkerDisplay += '</table>';

            ShowAddress(GMap, '10 Neptune Blvd, Suite 106, Neptune, NJ 07753', sMarkerDisplay);
            break;
    }
  }
}

//*****************************************************************************
// 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 OnGetIronBridgeDirections()
{
  var sFromAddress = GetElementById('FromAddressIronBridge').value;
  var sToAddress   = '501 Iron Bridge Road, Freehold, NJ 007728';
  var sLocale      = 'en_US';
  
  GDir.load("from: " + sFromAddress + " to: " + sToAddress, { "locale": sLocale });
}

//*****************************************************************************
// Called to get directions
function OnGetRoute9Directions()
{
  var sFromAddress = GetElementById('FromAddressRoute9').value;
  var sToAddress   = '4247 Route 9 North, Freehold, NJ 007728';
  var sLocale      = 'en_US';
  
  GDir.load("from: " + sFromAddress + " to: " + sToAddress, { "locale": sLocale });
}

//*****************************************************************************
// Called to get directions
function OnGetRenaissanceDirections()
{
  var sFromAddress = GetElementById('FromAddressRenaissance').value;
  var sToAddress   = '312 Applegarth Road, Monroe Township, NJ 08831';
  var sLocale      = 'en_US';
  
  GDir.load("from: " + sFromAddress + " to: " + sToAddress, { "locale": sLocale });
}
//*****************************************************************************
// Called to get directions
function OnGetPondViewDirections() {
    var sFromAddress = GetElementById('FromAddressPondView').value;
    var sToAddress = '301 Professional View Dr, Freehold Twp, NJ 07728';
    var sLocale = 'en_US';

    GDir.load("from: " + sFromAddress + " to: " + sToAddress, { "locale": sLocale });
}

//*****************************************************************************
// Called to get directions
function OnGetNeptuneDirections() {
    var sFromAddress = GetElementById('FromAddressNeptune').value;
    var sToAddress = '10 Neptune Boulevard, Neptune, NJ 07753-4848, United States (Alder Bernard Md)';
    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.");
 }
 
//*****************************************************************************
// Called to show or hide FAQ content
function ShowFAQ(sPanel)
{
  var oPanel = GetElementById(sPanel);
  
  // If it is open then hide it and visa versa
  if (oPanel.style.display == 'none')
    oPanel.style.display = '';
  else
    oPanel.style.display = 'none';
}

//*****************************************************************************
// Called to start the news ticker
function StartNews()
{
  // Capture a reference
  oNewsText = GetElementById('NewsText');

  // Define the message  
  var sNewsLine  = '<nobr>...AOSMI Breaking News';
  
   sNewsLine += "&nbsp;&nbsp;&diams;&nbsp;&nbsp;<span class='utxtnavticker'>Congratulations to Dr. Gerald Mauriello, Jr., DPM, newly Board Qualified with the American College of Foot and Ankle Surgeons in both Foot Surgery and Reconstructive Rearfoot and Ankle Surgery</span>&nbsp;&nbsp;&nbsp;&nbsp;";
  
     sNewsLine += "&nbsp;&nbsp;&diams;&nbsp;&nbsp;<span class='utxtnavticker'>Dr. Greller, Dr. Goldberg, Dr. Banzon, Dr. Berkowitz and Dr. Goldberger have medical privileges at St. Peter's University Hospital in New Brunswick </span>&nbsp;&nbsp;&nbsp;&nbsp;";
	 
	   sNewsLine += "&nbsp;&nbsp;&diams;&nbsp;&nbsp;<span class='utxtnavticker'>Dr. Nasar and Dr. Cozzarelli have medical privileges at Jersey Shore Medical Center and have office hours in Neptune</span>&nbsp;&nbsp;&nbsp;&nbsp;";
   sNewsLine += '&nbsp;&nbsp;&diams;&nbsp;&nbsp;<span class="utxtnavticker"><a href="http://www.advancedorthosports.com/Doctors.aspx?title=Dr.%20James%20Cozzarelli,%20MD">Dr. James Cozzarelli has been named the new Orthopedic Medical Director at CareOne at Jackson</a></span>&nbsp;&nbsp;&nbsp;&nbsp;';
   
 
      sNewsLine += '&nbsp;&nbsp;&diams;&nbsp;&nbsp;AOSMI Breaking News...</nobr>';
  
  // Load the ticker
  oNewsText.innerHTML  = sNewsLine;
  
  // Set the starting position
  if (isIE6 == true)
    oNewsText.style.pixelLeft = iNewsBannerWidth;
  else
    oNewsText.style.left = iNewsBannerWidth + 'px';
  iCurrentNewsTextLeft = iNewsBannerWidth;
  
  // Preset the message width
  iNewsTextWidth = oNewsText.offsetWidth * -1;
  
  // StartTicker
  if (isIE6 == true)
    setTimeout("RunTicker()", 16);
  else
    setTimeout("RunTicker()", 20);
}

//****************************************************************************
// Run the ticker
function RunTicker(iOffetWidth)
{
  // Increment the text by one pixel
  iCurrentNewsTextLeft = iCurrentNewsTextLeft - 1;

  // Is the message done
  if (iCurrentNewsTextLeft < iNewsTextWidth)
    iCurrentNewsTextLeft = 845;

  // Move the message
  if (isIE6 == true)
    oNewsText.style.pixelLeft = iCurrentNewsTextLeft;
  else
    oNewsText.style.left = iCurrentNewsTextLeft.toString() + 'px';
  
  // StartTicker
  if (isIE6 == true)
    setTimeout("RunTicker()", 16);
  else
    setTimeout("RunTicker()", 20);
}







