var ct = 0;
var cd = 0;
var copaystate = 'AL';
var copaycount = 1;
var radius = 5;
var dosage = 5;
var pharmzip = 0;
var emailchecked = false;
var phonechecked = false;
var smschecked = false;
var map = null;
var markersArray = [];
var geocoder = null;
var pharmurl = '';

$(document).ready(function() {
  $("#patient-assistance").dialog({
  autoOpen: false,
  width: 650,
  resizeable: false,
  draggable: false,
  modal: true
});
  
  $("#intro_patient,#intro_physician").click(function() {
    $("#intro-overlay").fadeOut("slow");
  });

$(".patient-assistance-open")
  .button()
  .click(function() {
    $(window).scrollTop(0);
    $("#patient-assistance").dialog("open");
    $("#patient-assistance-close").blur();
  });

$("#co-pay").dialog({
  autoOpen: false,
  width: 650,
  resizeable: false,
  draggable: false,
  modal: true
});

$("#locator").dialog({
  autoOpen: false,
  width: 650,
  resizeable: false,
  draggable: false,
  modal: true
});
  
$("#calculator").dialog({
    autoOpen: false,
    width: 676,
    resizeable: false,
    draggable: false,
    modal: true
  });

  $(".calculator-open")
    .button()
    .click(function() {
    $("#calculator").dialog("open");
    $("#calculator-help").dialog("open");
    $('div:last').hide();
    if($.browser.msie && $.browser.version == "7.0")
    {
      window.scrollTo({ top: '0', left: '0'}, 0);
    }
    });

$(".co-pay-open")
  .button()
  .click(function() {
    $("#co-pay").dialog("open");
    $(".patient-button").blur();
  });
  
$(".locator-open")
  .button()
  .click(function() {
    $("#locator").dialog("open");
  });

$(".toolsclose")
  .button()
  .click(function() {
    $("#patient-assistance").dialog("close");
    $("#co-pay").dialog("close");
    $("#calculator").dialog("close");
  $("#locator").dialog("close");
  });

$(".closemodal, #calc-help-close")
  .button()
  .click(function() {
    $("#co-pay-hcp-download").dialog("close");
    $("#calculator-help").dialog("close");
    $("#co-pay-help").dialog("close");
    $("#locator-help").dialog("close");
    $("#co-pay-patient-download").dialog("close");
  });

$("#co-pay-hcp-download").dialog({
  autoOpen: false,
  width: 616,
  resizeable: false,
  draggable: false,
  modal: true
});
  
$("#co-pay-patient-download").dialog({
  autoOpen: false,
  width: 616,
  resizeable: false,
  draggable: false,
  modal: true
});
  
$("#calculator-help").dialog({
  autoOpen: false,
  width: 616,
  resizeable: false,
  draggable: false,
  modal: true
});
  
$("#co-pay-help").dialog({
  autoOpen: false,
  width: 616,
  resizeable: false,
  draggable: false,
  modal: true
});
  
$("#locator-help").dialog({
  autoOpen: false,
  width: 616,
  resizeable: false,
  draggable: false,
  modal: true
});

$(".hcp-button")
  .button()
  .click(function() {
    $("#co-pay-hcp-download").dialog("open");
    $("#copaystate-button").blur();
    $('div:last').hide();
  });
  
$("#calculator-help-open")
  .button()
  .click(function() {
    $("#calculator-help").dialog("open");
  $('div:last').hide();
  if($.browser.msie && $.browser.version == "7.0")
  {
    window.scrollTo({ top: '0', left: '0'}, 0);
  }
  });
  
$("#co-pay-help-opener")
  .button()
  .click(function() {
    $("#co-pay-help").dialog("open");
  $('div:last').hide();
  if($.browser.msie && $.browser.version == "7.0")
  {
    window.scrollTo({ top: '0', left: '0'}, 0);
  }
  });
  
$("#locator-help-opener")
  .button()
  .click(function() {
    $("#locator-help").dialog("open");
  $('div:last').hide();
  if($.browser.msie && $.browser.version == "7.0")
  {
    window.scrollTo({ top: '0', left: '0'}, 0);
  }
  });

$("select#copaystate").selectmenu({
  change: copaystatechange,
  open: selectmenuOpen
});

  $(".patient-button")
  .button()
  .click(function() {
    $("#co-pay-patient-download").dialog("open");
  $('div:last').hide();
  });
  
  $('select#currenttreatment').selectmenu({
    change: selectmenuChange
  });
  
  $('#currentdosage')
    .click(function() {
      $('#currentdosage').val('');
    });
  $('#calcreset')
        .click(function() {
          $('#currentdosage').val('0');
      $('#currenttreatment').selectmenu('value', '0');
      $('#conversionratio').text('0');
      $('#dailydose .bigval').text('0');
      $('#opanadailydose .bigval').text('0');
        });
    
  $('#calcdosage')
    .click(function(e) {
      e.preventDefault();
      cd = $('#currentdosage').val();
      
      if (cd.match(/^\d+$/) == null) {
        alert("Please enter only a numeric value for your dosage in mg.");
        return;
      }
      
      if(ct == '0' || cd == '0') {
        alert("Please select your current medication and enter your current dosage.");  
      }
      else if (cd > 999) {
          alert("Please enter dosage between 1 and 999.");
      }
      else
      {
        $('#conversionratio').text(ct);
        $('#dailydose .bigval').text(Math.round(ct * cd));
        $('#opanadailydose .bigval').text(Math.round(ct * cd)/2);
      }
  });
  
    $('select#pldistance').selectmenu({
      change: selectRadiusChange                    
    });
    
    $('select#plstep2distance').selectmenu({
      change: selectRadiusChange                    
    });
    
    $('select#pldosage').selectmenu({
      change: selectDosageChange                    
    });
    
    $('#pharmsubmit1')
      .click(function(){
        pharmzip = $("#pharmzipcode").val();
        
        if (pharmzip.match(/^\d+$/) == null) {
          alert("Please enter a valid US Zip Code.");
          return;
        }
        
        if(pharmzip < 1 || pharmzip > 99999) {
          alert("Please enter a valid US Zip Code.");
          return;
        }
        
        $("#plstep2zip").val(pharmzip);
        
        pharmurl = "/services/Pharmacies.ashx?zipcode="+ pharmzip +"&distance="+ radius;
        
        $("#mapresults").load(pharmurl);
        initialize();

        $.getScript(pharmurl + "&mode=script");

        $('#plstep1').hide('fast');
        $('#plstep2').delay(400).show('fast');
    
    $("#mapresult1 .mapaddress").toggle("fast");
      });
  
    $("#plstep2submit")
      .click(function(){
        pharmzip = $("#plstep2zip").val();
        
        if (pharmzip.match(/^\d+$/) == null) {
          alert("Please enter a valid US Zip Code.");
          return;
        }
        
        if(pharmzip < 1 || pharmzip > 99999) {
          alert("Please enter a valid US Zip Code.");
          return;
        }
        
        pharmurl = "/services/Pharmacies.ashx?zipcode="+ pharmzip +"&distance="+ radius;
        
        $("#mapresults").load(pharmurl);

        $.getScript(pharmurl + "&mode=script");
      });
      
    $('#plstep2gethelp')
      .click(function(){
        $('#plstep2').hide('fast');
        $('#plstep3').show('fast');
      });
      
    $('#plstep3submit')
      .click(function(){
    var method;
    var name = jQuery.trim($("#fullname").val());
    var zip = jQuery.trim($("#plzipcode").val());
    var phone = jQuery.trim($("#pldayphone").val());
    var cell = jQuery.trim($("#plcellphone").val());
    var email = jQuery.trim($("#emailaddress").val());
    var supply = jQuery.trim($("#pharmdosage").val());
  
  if(!emailchecked && !phonechecked && !smschecked)
  {
    alert("Please select your prefered contact method.");
    return;
  }
  
  if(!checkLength( name, "name", 6, 80 ))
  {
    alert("Please provide us with your name.");
    return;
  }
  
  if(emailchecked)
  {
    if(!checkLength( email, "email", 6, 80 ))
    {
      alert("Please enter a valid internet e-mail address.");
      return;
    }
    
    if(!checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" ))
    {
      alert("Please enter a valid internet e-mail address.");
      return;
    }
  }
  
  if (zip.match(/^\d+$/) == null) {
          alert("Please enter a valid five-digit US Zip Code.");
          return;
        }
  
  if(!checkLength( zip, "zip", 5, 5 ))
  {
    alert("Please enter a valid five-digit US Zip Code.");
    return;
  }
  
  if(phonechecked && !checkLength( phone, "phone", 10, 16 ) && !checkRegexp(phone, /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/))
  {
    alert("Please provide your home phone number.");
    return;
  }
  
  if(smschecked && !checkLength( cell, "cell", 10, 16 ) && !checkRegexp(cell, /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/))
  {
    alert("Please provide your SMS/mobile phone number.");
    return;
  }
    
    if(emailchecked)
      method = "E";
      
    if(phonechecked)
      method = "T";
      
    if(smschecked)
      method = "S";
    
    $.ajax({ url: "/services/ConciergeService.ashx?m="+ method +"&n="+ name +"&z="+ zip +"&h="+ phone +"&c="+ cell +"&e="+ email +"&d="+ dosage +"mg&s="+ supply +"&v=N&p=" });
    
        $('#plstep3').hide('fast');
        $('#plstep4').delay(400).show('fast');
      });
      
    $('#plstep4back')
      .click(function(){
        $('#plstep4').hide('fast');
        $('#plstep2').show('fast');
      });
    
    $("#plemailwrap").click(function() {
      emailchecked = true;
      phonechecked = false;
      smschecked = false;
      clearChecks();
    $("#plemailwrap").removeClass("plunchecked");
      $("#plemailwrap").addClass("plchecked");
    });
    
    $("#plphonewrap").click(function() {
      emailchecked = false;
      phonechecked = true;
      smschecked = false;
      clearChecks();
    $("#plphonewrap").removeClass("plunchecked");
      $("#plphonewrap").addClass("plchecked");
    });
    
    $("#plsmswrap").click(function() {
      emailchecked = false;
      phonechecked = false;
      smschecked = true;
      clearChecks();
    $("#plsmswrap").removeClass("plunchecked");
      $("#plsmswrap").addClass("plchecked");
    });
  
    $(".maptoggle").live('click', function(){
      toggleMapLocation($(this).siblings(".mapaddress"));
    });
  
  $(".mapmarker").live('click', function(){
      toggleMapLocation($(this).siblings(".mapaddress"));
    });
  
  $(".maplocation").live('click', function(){
      toggleMapLocation($(this).siblings(".mapaddress"));
    });
  
  $("#patient-download-pdf").click(function(){
    var name = jQuery.trim($("#CoPayPatientFullName").val());
    var email = jQuery.trim($("#CoPayPatientEmail").val());
    var address = jQuery.trim($("#CoPayPatientAddress").val());
    var city = jQuery.trim($("#CoPayPatientCity").val());
    var state = jQuery.trim($("#CoPayPatientState").val());
    var zip = jQuery.trim($("#CoPayPatientZipCode").val());
    var phone = jQuery.trim($("#CoPayPatientDayPhone").val());
    var cell = jQuery.trim($("#CoPayPatientDayPhone").val());
    
    var requestUrl = '/services/PatientCard.ashx?'
    
    if(name.length > 0)
    {
      requestUrl += "fullname=" + name +"&";
    }
    
    if(email.length > 0)
    {
      if(checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" ))
      {
        requestUrl += "email=" + email +"&";
      } else {
        alert("Please enter a valid email address.");
        return;
      }
    }
      
    if(address.length > 0)
    {
       requestUrl += "address=" + address +"&";
    }
      
    if(city.length > 0)
    {
       requestUrl += "city=" + city +"&";
    }
      
    if(state.length > 0)
    {
      if(state.length == 1 || state.length > 2)
      {
         alert("Please enter the two-letter abbreviation for your state");
      } else {
        requestUrl += "state=" + state +"&";
      }
    }
    
    if(zip.length > 0) {
      if (zip.length > 0 && zip.match(/^\d+$/) == null) {
        alert("Please enter a valid five-digit US Zip Code.");
        return;
      } else {
        requestUrl += "zip=" + zip +"&";
      }
    }
      
    if(phone.length > 0) {
      if(!checkRegexp(phone, /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/))
      {
        alert("Please enter a valid US Home Phone Number");
        return;
      } else {
        requestUrl += "phone=" + phone+"&";
      }
    }
    
    if(cell.length > 0) {
      if(!checkRegexp(cell, /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/))
      {
        alert("Please enter a valid US Cell Phone Number");
        return;
      } else {
        requestUrl += "cell=" + cell +"&";
      }
    }
        
    requestUrl = requestUrl.substring(0, requestUrl.length - 1);
      
    window.open(requestUrl);
  });
  
  $("#hcp-download-pdf")
  .click(function(e) {
      e.preventDefault();
      copaycount = $('#hcp-download-count').val();
      hcpname = $("#txtVermontName").val();
      email = $("#txtVermontEmail").val();
      license = $("#txtVermontLicense").val();
    
      var requestUrl = '/services/HCPCard.ashx?state='+ copaystate +'&numcards='+ copaycount
      
      if (copaycount.match(/^\d+$/) == null) {
        alert("Please enter only a numeric value for your co-pay card count.");
        return;
      }
      
      if(copaycount < 1 || copaycount > 10) {
        alert("Please enter a value between 1 and 10.");
    return;
      }
    
    if(copaystate == 'VT')
    {
      if(checkLength(hcpname, '', 1, 50))
      {
         requestUrl += "&fullname=" + hcpname;
      } else {
        alert("Please enter your name");
        return;
      }
      
      if(checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" ))
      {
        requestUrl += "&email=" + email;
      } else {
        alert("Please enter a valid email address.");
        return;
      }
      
      // if(checkRegexp(license, /([0-9]{3})-([0-9]{7})/, ""))
      if(checkRegexp(license, /([0-9]{10})/, ""))
      {
         requestUrl += "&license=" + license;
      } else {
        alert("Please enter a valid state license number using no dashes or special characters.");
        return;
      }
    }
    
    window.open(requestUrl);
    });
  
  function toggleMapLocation(current)
  {
    var show = current.is(":hidden");
    $("#mapresults").find(".mapaddress").each(function(){
      if($(this).is(":visible"))
        $(this).slideToggle("fast");
      });
    if(show)
      current.slideToggle("fast");
  }
  
  function clearChecks() {
    $("#plstep3").find("div").each(function(){
      $(this).removeClass("plchecked");
    $(this).addClass("plunchecked");
    });
  }
  
  $("#inner-nav").show();
});

function selectmenuChange(event, value)
{
  ct = value.value;
}

function selectmenuOpen(event, value)
{
 if($.browser.msie && ($.browser.version == "7.0" || $.browser.version == "8.0"))
  {
    window.scrollTo({ top: '0', left: '0'}, 0);
  }
}

function copaystatechange(event, value)
{
  copaystate = value.value;
  if(copaystate == "VT")
  {
    $("#vermonthcp").show();
  } else {
    $("#vermonthcp").hide();
  }
}

function selectRadiusChange(event, value)
{
  radius = value.value;
}

function selectDosageChange(event, value)
{
  dosage = value.value;
}

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new google.maps.Map(document.getElementById("plmap"));
    map.enableScrollWheelZoom();
    geocoder = new GClientGeocoder();
  }
}

function showAddress(address,fulladdress) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 15);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(fulladdress);
      }
    }
  );
}

function addMarker(address,fulladdress) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        var marker = new GMarker(point);
        map.addOverlay(marker);
      }
    }
  );
}

function setCenter(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (point) {
        map.setCenter(point, 15);
      }
    }
  );
  map.checkResize();
}

function checkLength( o, n, min, max ) {
  if ( o.length > max || o.length < min ) {
    return false;
  } else {
    return true;
  }
}

function checkRegexp( o, regexp, n ) {
  if ( !( regexp.test( o ) ) ) {
    return false;
  } else {
    return true;
  }
}
