﻿var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var panel_name = '';
var panel_id = 0;
var i=0;
var user_id = 0;
function thisNull(str) {
    if (str == null) {
        return '';
    }
    else {
        return str;
    }
}
$.fn.removeCol = function(col){
    if(!col){ col = 1; }
    $('tr td:nth-child('+col+'), tr th:nth-child('+col+')', this).remove();
    return this;
};
function loadPanel(ID, search_type) {
    $('#searching').dialog('open');
    $.ajax({type: 'POST',
           url: 'results.aspx',
           data: {
                searchType: 'panel',
                panel_id: ID
           }
           ,
           dataType: 'html',
           success: function(html){
               $("div#search_results").html('').html(html);
               if ($('#DataGrid1 tr').size() < 2) {                                    
                    $("#DataGrid1").hide();
                    $('#err').fadeIn();
                }
                $('#addPanel').hide();                           
                $("#DataGrid1").tablesorter({headers: { 0: { sorter: false } }});
                $("#DataGrid1 td").css('background-color', '#fff');
                $('#search_results').fadeIn('slow');
                $('#searching').dialog('close');
                $('#search_options').slideUp();
                $('#button1').val('Search Again...');
                $('#results_header').show();                                                        
                $('#export').show();
                if (typeof search_type != 'undefined') {
                    if (search_type == 'export') {
                         window.open('export.aspx?excel=1&panel=1','export');
                    }
                    else {
                         window.open('export.aspx?excel=0&panel=1','export');
                    }
                }
           },
           error: function(e) { alert("Failure: " + e.responseText + "-" + e.status + "-" + e.statusText); }
    });
}
function addPanel() {    
    var myString = '';
    $('.myCheck').each(function() {
        if ($(this).attr('checked') == true) {
            myString += $(this).val() + ',';
        }
    });
    if (myString == '') {
        alert('You must choose at least one provider to create a panel.');
        return;
    }
    var this_dialog = 'AddPanel.aspx';    
    $('#dialog').dialog('destroy');
    $('#dialog').remove(); 
    $(document.body).append('<div id="dialog" title="Create Panel"></div>');    
    $('#dialog').load(this_dialog).dialog({ 
        autoOpen: true, 
        modal: true , 
        resizable: false,
        buttons: {
				Ok: function() {
				     if ($('#new_panel').val() == '' && $('#existing_panel').val() == 0) {
				        alert('You must enter a new panel or choose an existing one.');
				     }
				     else {
				         if ($('#new_panel').val() != '') {
				            panel_name = $('#new_panel').val();
				            panel_id = 0;
				         }
				         else {
				            panel_id = $('#existing_panel').val()
				         }
				         $.ajax({type: 'POST',
                           url: '../ajaxproxy/handler.asmx/GetPanel',
                           data: {
                                panel_name: panel_name,
                                panel_id: panel_id,
                                id: myString.substring(0, myString.length -1)
                           },
                           dataType: 'html',
                           success: function(html){
                                $('#panels_existing').html('').html(html);
                                $('#existing_panel').val('');
                                if (panel_id == 0) {
                                    alert('Panel created.');
                                }
                                else {
                                    alert('Panel modified.');
                                }                                
                           },
                           error: function(e) { alert("Failure: " + e.responseText + "-" + e.status + "-" + e.statusText); }
                        });
					    $(this).dialog('close');
					}
				}
			}
    });
}
function deleteFromPanel(panel_id, unique_id) { 
     $('#searching').dialog('open');
     $.ajax({type: 'POST',
       url: '../ajaxproxy/handler.asmx/RemPanel',
       data: {
            unique_id: unique_id,
            panel_id: panel_id
       },
       dataType: 'html',
       success: function(html){
            alert('Doctor removed from panel');       
            loadPanel(panel_id);
       },
       error: function(e) { alert("Failure: " + e.responseText + "-" + e.status + "-" + e.statusText); }
    });
	$('#searching').dialog('close');			    
}
function faqDialog() { 
    $(document.body).append('<div id="faq_dialog" title="Frequently asked questions"></div>');  
    $('#faq_dialog').html('').load('faq.aspx').dialog({ 
        autoOpen: true, 
        modal: true, 
        resizable: false,
        width:550,
        height:520,
        close: function() { $('#faq_dialog').remove(); },
        buttons: {            	
            OK: function() { $(this).dialog('close');$('#faq_dialog').remove(); }
       }
    });     		    
}
function deletePanel(panel_id) { 
    $('#dialog').dialog('destroy');
    $(document.body).append('<div id="dialog" title="Delete Panel"></div>');  
    $('#dialog').html('Are you sure you want to delete this panel?').dialog({ 
        autoOpen: true, 
        modal: true, 
        resizable: false,
        buttons: {            	
			Delete: function() {
                 $.ajax({type: 'POST',
                   url: '../ajaxproxy/handler.asmx/RemPanelAll',
                   data: {panel_id: panel_id},
                   dataType: 'html',
                   success: function(html){
                        refreshPanels();
                   },
                   error: function(e) { alert("Failure: " + e.responseText + "-" + e.status + "-" + e.statusText); }
                });
                $(this).dialog('close');
            },
            Cancel: function() { $(this).dialog('close'); }
       }
    });     		    
}
function toggleRow() { 
   if ($(this).attr('checked') == true) {        
        $('table.tablesorter tbody tr#row_' + $(this).attr('id') + ' td').css('background-color', '#f3e39a');
   }
   else {
        $('table.tablesorter tbody tr#row_' + $(this).attr('id') + ' td').css('background-color', '#fff');
   }
}
function checkThis(obj) {
	var checkedValue = obj.attr('checked') ? 'checked' : '';
	$('.myCheck').attr('checked', checkedValue); 	
}
function openMap(address,geoaddress,latitude,longitude) {
    var map = new GMap2(document.getElementById("map"),{size:new GSize(610,300)});
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(
        geoaddress,
        function(point) {
          if (!point) {
            alert(geoaddress + " not found");
          } else {
            map.setCenter(point, 14);
            var marker = new GMarker(point);
            map.addOverlay(marker);
            var html = '<b>Directions</b> <em>(enter start address)</em>' +
            '<br><br><form action="http://maps.google.com/maps" method="get" target="_blank">' +
            '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" style="height:20px;" />' +
            '<INPUT value="Go" TYPE="SUBMIT" class="submit">' +
            '<input type="hidden" name="daddr" value="' + geoaddress + '" /></form>';
            marker.openInfoWindowHtml(html); 
          }
        }
      ); 
    $('#map').slideDown('slow');
}
function refreshPanels() {
 $.ajax({type: 'POST',
   url: '../ajaxproxy/handler.asmx/GetPanel',
   dataType: 'html',
   success: function(html){
        $('#panels_existing').html('').html(html);
   },
   error: function(e) { alert("Failure: " + e.responseText + "-" + e.status + "-" + e.statusText); }
});
}
$(document).ready(function(){
	$('.help_link').easyTooltip();
    $('#addPanel').click(addPanel);
    if (top.location != location) {
        top.location.href = document.location.href;
    }
    if (user_id > 0) {
        refreshPanels();
    }
    else {
        $('#addPanel').hide(); 
    }
    $("#searching").dialog({ autoOpen: false, modal: true, resizable: false});
    $('#contractType').bind('change',function(){
       if ($(this).val() != 'A') {
            $('#spec_2').attr('disabled','disabled');
       }
       else {
            $('#spec_2').attr('disabled','');
       } 
    });
    $('input[name="searchType"]').bind('click',function(){
        var this_val = $("input[name='searchType']:checked").val();    
        if (this_val == 'zip') {
            $('#nameSearch').hide();
            $('#stateSearch').hide();
            $('#zipSearch').fadeIn();
            $('#countySearch').hide();
            $('#tinSearch').hide();
            $('#prov_spec').show();  
        }
        else if (this_val == 'citystate') {
            $('#zipSearch').hide();
            $('#nameSearch').hide();
            $('#stateSearch').fadeIn();
            $('#countySearch').hide();
            $('#tinSearch').hide();
            $('#prov_spec').show();  
        }            
        else if (this_val == 'name') {
            $('#stateSearch').hide();
            $('#zipSearch').hide();
            $('#nameSearch').fadeIn(); 
            $('#countySearch').hide();
            $('#tinSearch').hide(); 
            $('#prov_spec').hide();     
        }
        else if (this_val == 'county') {
            $('#stateSearch').hide();
            $('#zipSearch').hide();
            $('#nameSearch').hide(); 
            $('#countySearch').fadeIn();
            $('#tinSearch').hide(); 
            $('#prov_spec').show();       
        }  
        else if (this_val == 'tin') {
            $('#stateSearch').hide();
            $('#zipSearch').hide();
            $('#nameSearch').hide();
            $('#countySearch').hide();
            $('#tinSearch').fadeIn(); 
            $('#prov_spec').hide();     
        }                 
    });
    $('input[name="spec"]').bind('click',function(){
        var this_val = $("input[name='spec']:checked").val();    
        if (this_val != 'spec') {
            $('#specialty').attr('disabled','disabled');
            $('#listNJVol').attr('disabled','');
        }                   
        else {
            $('#specialty').attr('disabled','');
            $('#listNJVol').attr('disabled','disabled');
        }                
    }); 
    $("#button2").click(function(){
        document.location.href='https://eqris.procuranet.com';
    });
    $("#button1").click(function(){
        var err_msg = '';
        if (thisNull($('#contractType').val()) == '') {
            err_msg += 'You must choose either workers compensation or auto contract.\n';
        }
        if ($("input[name='searchType']:checked").val() == '') {
            err_msg += 'You must choose a search type.\n';
        }
        if ($("input[name='searchType']:checked").val() == 'zip' && thisNull($('#tbZip').val()) == '') {
            err_msg += 'If you select zip, zip must be filled in.\n';
        }
        else if ($("input[name='searchType']:checked").val() == 'citystate' && (thisNull($('#tbCity').val()) == '' || thisNull($('#lbState').val()) == '')) {
            err_msg += 'If you select city/state, both must be filled in.\n';
        }
        else if ($("input[name='searchType']:checked").val() == 'name' && thisNull($('#lNameTB').val()) == '') {
            err_msg += 'If you select the name search, you must enter at least a last name.\n';
        }
        else if ($("input[name='searchType']:checked").val() == 'tin' && thisNull($('#tbTin').val()) == '') {
            err_msg += 'If you select the TIN search, you must enter at a TIN.\n';
        }
        else if ($("input[name='searchType']:checked").val() == 'county' && thisNull($('#tbCounty').val()) == '') {
            err_msg += 'If you select the county search, you must enter at a county.\n';
        }
        if ($("input[name='searchType']:checked").val() != 'name' && $("input[name='searchType']:checked").val() != 'tin' && $("input[name='spec']:checked").val() == 'spec' && (thisNull($('#specialty').val() == '') || $('#specialty').val() == '%')) {
            err_msg += 'You must choose a specialty.';
        }   
        else if ($("input[name='spec']:checked").val() == 'spec_nj' && (thisNull($('#listNJVol').val()) == '' || $('#listNJVol').val() == '%')) {
            err_msg += 'You must choose a NJ Voluntary specialty.';
        } 
        if (err_msg.length == 0) {
            if ($(this).val() == 'Search for Providers') {                
                $('#searching').dialog('open');
                $.ajax({type: 'POST',
                       url: 'results.aspx',
                       data: {contractType: thisNull($('#contractType').val()),
                              searchType: thisNull($("input[name='searchType']:checked").val()),
                              tbZip: thisNull($('#tbZip').val()),
                              tbTin: thisNull($('#tbTin').val()),
                              tbCounty: thisNull($('#tbCounty').val()),
                              tbAddress: thisNull($('#tbAddress').val()),
                              distance: thisNull($('#distance').val()),
                              lNameTB: thisNull($('#lNameTB').val()),
                              fNameTB: thisNull($('#fNameTB').val()),
                              lNameStateLB: thisNull($('#lNameStateLB').val()),
                              tbCity: thisNull($('#tbCity').val()),
                              lbState: thisNull($('#lbState').val()),
                              listNJVol: thisNull($('#listNJVol').val()),
                              specialty: thisNull($('#specialty').val()),
                              verified: $('#verified').is(':checked')
                              }
                       ,
                       dataType: 'html',
                       success: function(html){
                           $("div#search_results").append(html);
                           if ($('#DataGrid1 tr').size() < 2) {                                    
                                $("#DataGrid1").hide();
                                $('#err').fadeIn();
                            }
                            $('#addPanel').show();                            
                            $("#DataGrid1").tablesorter({headers: { 0: { sorter: false } }});
                            $("#DataGrid1 td").css('background-color', '#fff');
                            $('#search_results').fadeIn('slow');
                            $('#searching').dialog('close');
                            $('#search_options').slideUp();
                            $('#button1').val('Search Again...');
                            $('#results_header').show();                                                        
                            $('#export').show();
                       },
                       error: function(e) { alert("Failure: " + e.responseText + "-" + e.status + "-" + e.statusText); }
                });
            }
            else {
                $('#map').hide();
                $('#err').hide();
                $('#search_options').slideDown();
                $('#button1').val('Search for Providers');
                $('#search_results').html('');
                $('#results_header').hide();
                $('#export').hide();
                $('#tbZip').val('');
                $('#tbTin').val(''),
                $('#tbCounty').val('');
                $('#distance').val('');
                $('#lNameTB').val('');
                $('#fNameTB').val('');
                $('#lNameStateLB').val('');
                $('#tbCity').val('');
                $('#lbState').val('');
                $('#listNJVol').val('%');
                $('#specialty').val('%');
            }
        }
        else {
            alert(err_msg);
        }
    });  
});
 
(function($) {

	$.fn.easyTooltip = function(options){
	  
		// default configuration properties
		var defaults = {	
			xOffset: 10,		
			yOffset: 25,
			tooltipId: "easyTooltip",
			clickRemove: false,
			content: "",
			useElement: ""
		}; 
			
		var options = $.extend(defaults, options);  
		var content;
				
		this.each(function() {  				
			var title = $(this).attr("title");				
			$(this).hover(function(e){											 							   
				content = (options.content != "") ? options.content : title;
				content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
				$(this).attr("title","");									  				
				if (content != "" && content != undefined){			
					$("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");		
					$("#" + options.tooltipId)
					    .addClass("ui-corner-all")
						.css("position","absolute")
						.css("top",(e.pageY - options.yOffset) + "px")
						.css("left",(e.pageX + options.xOffset) + "px")						
						.css("display","none")
						.fadeIn("fast")
				}
			},
			function(){	
				$("#" + options.tooltipId).remove();
				$(this).attr("title",title);
			});	
			$(this).mousemove(function(e){
				$("#" + options.tooltipId)
					.css("top",(e.pageY - options.yOffset) + "px")
					.css("left",(e.pageX + options.xOffset) + "px")					
			});	
			if(options.clickRemove){
				$(this).mousedown(function(e){
					$("#" + options.tooltipId).remove();
					$(this).attr("title",title);
				});				
			}
		});
	  
	};

})(jQuery);

