

/**
 * Copyright (c) 2007 Ingo Schommer (www.chillu.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Splits a <ul>/<ol>-list into equal-sized columns.
 *
 * Requirements:
 * <ul>
 * <li>All list-elements need to have the same height.</li>
 * <li>List has to be blocklevel</li>
 * </ul>
 *
 * Caution: margin-top/margin-left on <li> are overridden.
 * Doesn't react to changes to the DOM, you need to call the function
 * manually afterwards.
 *
 * @see http://www.alistapart.com/articles/multicolumnlists
 */
jQuery.fn.columnizeList = function(settings){
   settings = jQuery.extend({
      cols: 3,
      width: '13',
      unit: 'em'
   }, settings);

   var prevColNum = 0;
   var size = $('li',this).size();
   var computedColHeight = 0;
   var baseFontSize = parseFloat($(this).css('font-size'));
   $('li',this).each(function(i) {
      var currentColNum = Math.floor(((i)/size) * settings.cols);
      $(this).css('margin-left',(currentColNum*settings.width)+''+settings.unit);
      if(prevColNum != currentColNum) {
         $(this).css('margin-top','-'+(computedColHeight/baseFontSize)+'em');
         computedColHeight = $(this).height();
      } else {
         $(this).css('margin-top','0');
         computedColHeight += $(this).height();
      }
      prevColNum = currentColNum;
   });

   this.css('height',(size/settings.cols)*(parseFloat($('li:first',this).height())/baseFontSize)+'em');
   this.after('<br style="clear: left;">');

   var onchange = function(e) {
      if(!e.originalTarget || e.originalTarget.tagName != 'LI') return true;
      var scope = this; // caution: closure
      setTimeout(function() {$(scope).columnizeList(settings);}, 50);
   };

   this.one('DOMNodeInserted',onchange);
   this.one('DOMNodeRemoved',onchange);

   return this;
};

jQuery.fn.uncolumnizeList = function(){
   $('li',this).each(function(i) {
      if(!$(this).attr('style')) return;
      $(this).attr('style',
         $(this).attr('style')
         .replace(/margin\-left[^,]*/g,'')
         .replace(/margin\-top[^,]*/g,'')
      );
   });
   $('ul',this).each(function(i) {
      if(!$(this).attr('style')) return;
      $(this).attr('style',
         $(this).attr('style')
         .replace(/[^-]height[^,]*/g,'')
      );
   });
   $(this).height('auto');
   this.unbind('DOMNodeInserted');
   this.unbind('DOMNodeRemoved');

   return this;
}
// </li></ol></ul>


function duplicateField(ob){
	var tmp = ob.id.split("-");
	var obj = $(ob).parent().parent();
	var maxFields = tmp[1];
	var numClones = obj.siblings("div").length + 1;
	if ((numClones) < maxFields) {
		var newObj = obj.clone();
		newObj.removeClass("first");
		newObj.children("select").attr("name", ob.rel+(numClones+1));
		newObj.insertAfter(obj);
	}
}

function getDoctor(url) {
   $("#doctor-record").show();
   $("#doctor-record").load(url);

}

// PC: // submit
function gi(_a6){

   if(document.getElementById&&document.getElementById(_a6)){
      return document.getElementById(_a6);
   }else{
      if(document.all&&document.all(_a6)){
         return document.all(_a6);
      }else{
         if(document.layers&&document.layers[_a6]){
            return document.layers[_a6];
         }else{
            return false;
         }
      }
   }
}


function addField(ob) {
	$(ob).parent().hide();
	var obj = $("#duplicate");
	var newObj = obj.clone();
	newObj.show();
	newObj.removeAttr("id");
	newObj.children("input").attr("name", ob.rel);
	var parentObj = $(this).parent();
	newObj.insertAfter(parentObj);
}


// PC: Search animation functions
// function doSearch(url) {

//    var queryString = $('#doctor-search').formSerialize();
//    $.post(  url,
//             queryString,
//             function(data){

//                // $("#search-disclaimer").fadeOut("slow");
//                $("#search-disclaimer").fadeOut("fast");
//                $("#search-results").fadeIn("slow");
//                $("#member-signup").fadeIn("slow")
//
// //                document.getElementById('search-content').innerHTML = data;
//                $('#search-content').html(data);
//                $("#search-results").show();
//             }
//    );
//    return false;
//
// }

function loadlinks(linkid,url) {

   // Remove 'current' class from all href tags
   $("ul#level-one a").removeClass("current");


   // Add 'current' to selected linkid
   $('#link_' + linkid).addClass("current");

   $.get(  url,
            function(data){

               $("#links-right").fadeIn("slow");
               // document.getElementById('links-right').innerHTML = data;
               $('#links-right').html(data);

            }
   );
   return false;
}

function loadadminlinks(url) {

      $.get(  url,
              function(data){
                  $("#links-right").fadeIn("slow");
                  $('#links-right').html(data);
               }
      );
      return false;
}

function doSavedSearch(url) {
   $.get(  url,
   function(data){
//          $("#search-disclaimer").fadeOut("slow");
//          $("#doctor-search").fadeOut("slow")
//          $("#search-results").fadeIn("slow");
//          $("#member-signup").fadeIn("slow")

         document.getElementById('search-content').innerHTML = data;
         $("#search-results").show();
   }

   );
   return false;
}

function popWindow(theURL,width,height) {
newWindow = window.open(theURL,'newWindow','toolbar=no,menubar=no,resizable=yes,scrollbars=yes,status=no,location =yes,width='+width+',height='+height);
}

$(document).ready(function() {


// override these in your code to change the default messages and styles
$.blockUI.defaults = {
    // the message displayed when blocking the entire page
//     pageMessage:    '<h1>Please wait...</h1>',
    // the message displayed when blocking an element
//     elementMessage: '', // none
    // styles for the overlay iframe
    overlayCSS:  { backgroundColor: '#fff', opacity: '0.75' },
    // styles for the message when blocking the entire page
//     pageMessageCSS:    { width:'250px', margin:'-50px 0 0 -125px', top:'50%', left:'50%', textAlign:'center', color:'#000', backgroundColor:'#fff', border:'3px solid #aaa' },
    // styles for the message when blocking an element
//     elementMessageCSS: { width:'250px', padding:'10px', textAlign:'center', backgroundColor:'#fff'},
    // styles for the displayBox
//     displayBoxCSS: { width: '400px', height: '400px', top:'50%', left:'50%' },
    // allow body element to be stetched in ie6
//     ie6Stretch: 1,
    // supress tab nav from leaving blocking content?
//     allowTabToLeave: 0,
    // Title attribute for overlay when using displayBox
//     closeMessage: 'Click to close',
    // use fadeIn effect
//     fadeIn:  1,
    // fadeIn transition time in millis
//     fadeInTime: 300,
    // use fadeOut effect when unblocking (can be overridden on unblock call)
//     fadeOut:  1,
    // fadeOut transition time in millis
//     fadeOutTime: 300
// PLAESE LEAVE THIS LINE INTACT
       i_am_a_dummy: 'false'
};


   $("a.add-patient-link").click(function(){
      $("#patient-form-container").toggle();

   });

	// hide the search agreement onload
// 	$("#search-agreement").hide();
/*	if (document.getElementById("doctor-search")) $("#search-results").hide();*/
// 	$("#member-signup").hide();
	$("#member-login").hide();

   // PCL For search results options
   $("#email-search-form").hide();
   $("#sms-search-form").hide();

   // PC: For doctor edit form
   $("#specialty_mgr").hide();
   $("#add-specialty-link").click(function(){
      $("#specialty_mgr").toggle();
   });




//    $("#del-specialty-link").click(function(){
//
//       $('option:selected','#specialty').each( function() {

//          val = this.value;
//          txt = this.text;
//          // vals.push( this.value )
//          var answer = confirm ("You are about to permanently delete specialty" + txt + ".\n This will affect all physicians that have this specialty linked to their profile.\n  Are you sure you want to continue?");
//          if (answer) {
//
//             var url = "/admin/specialty/delete/";
//
//             $.post(url,
//                { specialty_id : val },
//                function(data){
//                   var results = eval("(" + data + ")");
//
//                   if (results.success == false) {
//                      msg = results.message;
//                   }
//                   else {
//                      msg = 'Success. The chosen specialty was removed from the system.';
//                   }
//
//                   $('#warning').addClass('warning');
//                   $('#warning').html('Too late');
//                   $('#warning').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);
//                }
//             );
//
//          }
//          else {
//             ;
//          }
//
//       } );
//    });

   $("#subspecialty_mgr").hide();
   $("#add-subspecialty-link").click(function(){
      $("#subspecialty_mgr").toggle();
   });
   //
   $("#hospital_mgr").hide();
   $("#add-hospital-link").click(function(){
      $("#hospital_mgr").toggle();
   });
   //
   $("#healthplan_mgr").hide();
   $("#add-healthplan-link").click(function(){
      $("#healthplan_mgr").toggle();
   });

    // Added by kyle
    $("#url2_mgr").hide();
    $("#url2").click(function(){
       $("#url2_mgr").toggle();
    });

    $("#url3_mgr").hide();
    $("#url3").click(function(){
       $("#url3_mgr").toggle();
    });

    $("#address2_mgr").hide();
    $("#address2").click(function(){
       $("#address2_mgr").toggle();
    });

    $("#address3_mgr").hide();
    $("#address3").click(function(){
       $("#address3_mgr").toggle();
    });

    $("#phone2_mgr").hide();
    $("#phone2").click(function(){
       $("#phone2_mgr").toggle();
    });

    $("#phone3_mgr").hide();
    $("#phone3").click(function(){
       $("#phone3_mgr").toggle();
    });

   // End of Added by kyle

   // New way of doing it.
   // //////////////////////////////////////////////////////////////////////////
   // Hospitals
   // //////////////////////////////////////////////////////////////////////////
   $("#hospital_listing").hide();
   $("#hospital_add_options").hide();

   $("#show-hospital-list").click(function(){
      $("#hospital_listing").toggle();
//       $("#hospital_add_options").toggle();
   });

   $('"#hospitals"').change(function() {
      $("#hospital_add_options").show();
   });

//    $(function() {
//       $("#hospital_listing").change(editHospitalChanged);
//    });

//    function editHospitalChanged() {
//       $("#hospital_add_options").show();
//    }

   // //////////////////////////////////////////////////////////////////////////
   // Health Plans
   // //////////////////////////////////////////////////////////////////////////
   $("#healthplan_listing").hide();
   $("#healthplan_add_options").hide();

   $("#show-healthplan-list").click(function(){
      $("#healthplan_listing").toggle();
//       $("#hospital_add_options").toggle();
   });

   $('"#healthplans"').change(function() {
      $("#healthplan_add_options").show();
   });

/*
   $(function() {
      $("#healthplan_listing").change(editHealthplanChanged);
   });

   function editHealthplanChanged() {
      $("#healthplan_add_options").show();
   }*/

//    $("#doctor-more-details").hide();
//    $("#doctor-show-more").click(function(){
//       $("#doctor-more-details").toggle();
//       $("#doctor-show-more").hide();
//    });



	// toggles
	$("a.toggle").click(function(){
		$("#" + this.rel).slideToggle("slow");
	});

   // PC: Added fade effects for info and error messages
   $(".info").fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);
   $(".warning").fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);


   // PC: Added for faded search panels effect
   var page_type  = $('#page_type').attr('rel');
   if (page_type == 'patient-welcome')
   {
      $('#member-signup').addClass("fade");
      $('#saved-search').addClass("fade");
      $('#doctor-search').block();
      $('#saved-search').block();
      $('#member-signup').block();
   }
   else if (page_type == 'search-form')
   {
//       $("#doctor-search").removeClass("fade");
      $('#member-signup').addClass("fade");
      $('#saved-search').addClass("fade");
//       $('#doctor-search').block();
      $('#saved-search').block();
      $('#search-disclaimer').addClass("fade");
      $('#search-disclaimer').block();
      $('#member-signup').block();
   }
   else if (page_type == 'search-results')
   {
         // PC: added code to hide intro text instead of fade.
//          $("#search-disclaimer").addClass("fade");
//          $("#search-disclaimer").fadeOut("slow");
         $("#doctor-search").addClass("fade");
         $("#doctor-search").block();
         $("#saved-search").removeClass("fade");

         $('#member-signup').show();

         // PC: Unblock search form
//          $("#doctor-search").fadeIn("slow");
//          $('#doctor-search').unblock();
         $('#saved-search').unblock();
         $('#member-signup').unblock();
   }

   $("#agreement-buttons > a").click(function(){
      var wheretogo = this.getAttribute('finalhref');
      $("#search-agreement").slideToggle("slow", function() {
            window.location = wheretogo;
      } );

   });


// 	// patient path progression
// 	$("#agreement-buttons > a").click(function(){
// 		$("#search-agreement").slideToggle("slow");
// 		if (this.className == "agree") {
//          // PC: added code to hide intro text instead of fade.
// 			$("#search-disclaimer").addClass("fade");
// //          $("#search-disclaimer").fadeOut("slow");
// 			$("#doctor-search").removeClass("fade");
//          $("#saved-search").removeClass("fade");
//
//          $('#member-signup').show();
//
//          // PC: Unblock search form
// //          $("#doctor-search").fadeIn("slow");
//          $('#doctor-search').unblock();
//          $('#saved-search').unblock();
//          $('#member-signup').unblock();
//
// 		} else {
// 			$("#search-disclaimer").removeClass("fade");
//          $("#saved-search").addClass("fade");
// 			$("#doctor-search").addClass("fade");
// 		}
// 	});

// //    $("#patient_next_step > a").click(function(){
// // //       $("#search-agreement").slideToggle("slow");
// // //       if (this.className == "agree") {
// //          // PC: added code to hide intro text instead of fade.
// // //          $("#search-disclaimer").addClass("fade");
// // //          $("#search-disclaimer").fadeOut("slow");
// //          $("#doctor-search").removeClass("fade");
// //          $("#saved-search").removeClass("fade");
// //
// //          // PC: Unblock search form
// // //          $("#doctor-search").fadeIn("slow");
// //          $('#doctor-search').unblock();
// //          $('#saved-search').unblock();
// //          $('#member-signup').unblock();
// //
// //
// // //       } else {
// // /*         $("#search-disclaimer").removeClass("fade");
// //          $("#saved-search").addClass("fade");
// //          $("#doctor-search").addClass("fade");*/
// // //       }
// //    });

	$("#doctor-search img").click(function(){
      // PC: Disabled this to allow form submission to invoke processing
      ;
/*		$("#search-disclaimer").fadeOut("slow");
		$("#search-results").fadeIn("slow");
		$("#member-signup").fadeIn("slow");*/
	});

	// doctor path progression
	$("#add-doctor").hide();
	$("div.doctor-disclaimer .form-button").click(function(){
		$("fieldset #doctor-login").hide();
		$("#add-doctor").slideToggle("slow");
	});
	$("div.doctor-disclaimer a.register").click(function(){
		$("#add-doctor").slideToggle("slow");
		$("#doctor-login").hide();
		$("#register").show();

	});
   // PC : Added effect for signup in login form
   $("div.member-signup a.register").click(function(){
      $("#add-doctor").slideToggle("slow");
      $("#doctor-login").hide();
      $("#register").show();

   });

/*	$("div.doctor-disclaimer a.login").click(function(){
		$("#add-doctor").slideToggle("slow");
		$("#register").hide();
		$("#doctor-login").show();
	});
 */
   // PC: Added effect for login from top header menu
//    $("a.login").click(function(){

//       $("#add-doctor").slideToggle("slow");
//       $("#register").hide();
//       $("#next-action").hide();
//       $("#doctor-login").show();
//    });
   // \PC: Added effect for login from top header menu

   $("a.patientlogin").click(function(){


//       $("#add-doctor").slideToggle("slow");
//       $("#register").hide();
//       $("#next-action").hide();
      $("#patient-login").show();
   });


//    // PC: Added effect for login from top header menu
//    $("a.logout").click(function(){

// /*      $("#add-doctor").slideToggle("slow");
//       $("#register").hide();
//       $("#next-action").hide();
//       $("#doctor-login").show();*/
//    });

   // PC: Added admin login action
   $("#admin-login .form-button").click(function(){
      $("form")[0].submit();
   });


	$("#doctor-begin .form-button").click(function(){
		$("form")[0].submit();
		var hidden_fields = new Array("first_name", "last_name", "npi_number", "license_number", "state", "dea_number");
		for (i=0; i<hidden_fields.length; i++) {
			$("#hidden_" + hidden_fields[i]).val($("#" + hidden_fields[i]).val());
		}
	});

	$("#duplicate").hide();
	$("fieldset a.add-text").click(function(){
		$(this).parent().hide();
		var obj = $("#duplicate");
		var newObj = obj.clone();
		newObj.show();
		newObj.removeAttr("id");
		newObj.children("input").attr("name", this.rel);
		var parentObj = $(this).parent();
		newObj.insertAfter(parentObj);
	});

	////////////////////////////////////////////////////////////////////////////
	// Admin
	////////////////////////////////////////////////////////////////////////////

	// adsense login
	$("#adsense-login").hide();

	// adsense toggle
	$("#navigation .adsense").click(function(){
		$("#adsense-login").slideToggle("slow");
	});

	// deleting items
	$("td.link-delete a").click(function(){
		var answer = confirm("Are you sure you want to delete this?");
		if (answer) {
         return true;
			// do something
		}
      else {
         return false;
      }
	});

	// doctor form show/hide
// 	if (document.getElementsByTagName("table").length) $("#doctor-admin fieldset").hide();
	$("#buttons-1").hide();
	$("#buttons-2").hide();

	$("#main-buttons a.doctor").click(function(){
		$("#more-info").slideToggle("slow");
		$("#buttons-1").slideToggle("slow");
	});

	$("#add-more-info").click(function(){
		$("#buttons-1").hide();
		$("#buttons-2").slideToggle("slow");
		$("#all-info").slideToggle("slow");
		$("table.link-admin").hide();
	});


	// patient form show/hide
	$("#main-buttons a.patient").click(function(){
		$("#doctor-admin fieldset").slideToggle("slow");
	});

   // PC: Show email form for search result
   $("#emailresults").click(function(){
      $("#email-search-form").fadeIn("slow");
   });

   // PC: Show sms form for search result
   $("#smsresults").click(function(){
      $("#sms-search-form").fadeIn("slow");
   });


   // //////////////////////////////////////////////////////////////////////////
   // For Links . Replace suckerfish menus
   // //////////////////////////////////////////////////////////////////////////
   $.fn.hoverClass = function(c) {
      return this.each(function(){
         $(this).hover(
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
         );
      });
   };

   $("#nav-one li").hover(
      function(){ $("ul", this).fadeIn("slow"); },
      function() { }
   );
   if (document.all) {
      $("#nav-one li").hoverClass ("sfHover");
   }

   // //////////////////////////////////////////////////////////////////////////
   // tabs emulator for links
   // //////////////////////////////////////////////////////////////////////////
   // setting the tabs in the sidebar hide and show, setting the current tab
   $('div.tabbed div').hide();
   $('div.t1').show();
   $('div.tabbed ul.tabs li.t1 a').addClass('tab-current');

   // SIDEBAR TABS

   $('div.tabbed ul li a').click(function(){
      var thisClass = this.className.slice(0,2);
      $('div.tabbed div').hide();
      $('div.div_' + thisClass).show();
      $('div.tabbed ul.tabs li a').removeClass('tab-current');
//       $('div.alpha-pager ul.tabs li a').addClass('current');
      $(this).addClass('tab-current');

//       $('div.tabbed ul.tabs li a').addClass('current');

   });

   // Select the 'a' element as the default one
   $("#tabbutton_a").click();

//    $('a.link-item').click(function(){
//       var link_id = this.id;
//       $('#link_' + link_id).addClass("current");
//    });

// $('#demolist').columnizeList({cols:4,width:203,unit:'px'});

});


   function toggle_subcategory_form(){
      $("#subcategory_div").toggle();
   }

   function toggle_link_form(){
      $("#link_div").toggle();
   }


   function saveSubCategory(url) {

      $.post(url,
         {
            id              : $("#id").val(),
            subcategory_name: $("#subcategory_name").val(),
            parent_category : $("#parent_category").val(),
            Submit          : 'Save'
         },
         function(data){

            if (data == 'false') {
               msg = 'Warning. Your data could not be saved.';
            }
            else {
               msg = 'Success. Your subcategory data was saved.';
               $("#category-container").html(data);
            }

//             loadadminlinks(parent_category);

            $('#link-message').addClass('warning');
            $('#link-message').html(msg);
            $('#link-message').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);



            $("#subcategory_div").hide();
//             $("#subcategory_div").after(msg);
         }
      );
      return false;
   }


   function saveLink(url) {

      $.post(url,
         {
            id              : $("#link_id").val(),
            link_name       : $("#link_name").val(),
            url             : $("#url").val(),
            link_category   : $("#linkCategoryId").val(),
            Submit          : 'Save'
         },
         function(data){

            if (data == 'false') {
               msg = 'Warning. Your data could not be saved.';
            }
            else {
               msg = 'Success. Your link data was saved.';
               $("#category-container").html(data);

            }


            $('#link-message').addClass('warning');
            $('#link-message').html(msg);
            $('#link-message').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);



            $("#link_div").hide();
         }
      );
      return false;
   }


// PC: Added some form processing helper funtions
   function newSpecialty(url) {

      $.post(url,
         { specialty_name: $("#specialty_name").val() },
         function(data){
            if (data == 'false') {
               msg = 'Warning. Your data could not be saved.';
            }
            else {
               // To add...
/*               $('#itemsControl').get(0).add(
               new Option(itemList[n].name,itemList[n].id),
                        document.all ? 0 : null
               );*/
// or
               var Obj = document.getElementById('specialty');
               var value = $("#specialty_name").val();
               Obj.options.add(new Option(value, data));
               msg = 'Success. Your specialty data was saved.';
            }
//             $("#specialty_mgr").hide();
//             $("#specialty_mgr").after(msg);

            $('#specialty_mgr_msg').html(msg);
            $('#specialty_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);


         }
      );
      return false;
   }

   function remove_specialty(doctor_id, specialty_id, url, callback_url) {

      $.post(url,
         { doctor_id       : doctor_id,
           specialty_id    : specialty_id },
         function(data){
            var results = eval("(" + data + ")");


            if (results.success == false) {
               msg = results.message;
            }
            else {

               msg = 'Success. The chosen specialty was deleted from your profile.';
            }

            $('#specialty_msg').html(msg);
            $('#specialty_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new specialties listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
                  $('#specialty_list_container').html(data);
               }
            );

         }
      );
      return false;

   }

   function remove_subspecialty(doctor_id, subspecialty_id, url, callback_url) {

      $.post(url,
         { doctor_id          : doctor_id,
           subspecialty_id    : subspecialty_id },
         function(data){
            var results = eval("(" + data + ")");


            if (results.success == false) {
               msg = results.message;
            }
            else {

               msg = 'Success. The chosen subspecialty was deleted from your profile.';
            }

            $('#subspecialty_msg').html(msg);
            $('#subspecialty_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new subspecialties listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
                  $('#subspecialty_list_container').html(data);
               }
            );

         }
      );
      return false;

   }

   function remove_hospital(doctor_id, hospital_id, url, callback_url) {
      return delete_hospital(doctor_id, hospital_id, url, callback_url);

//       $.post(url,
//          { doctor_id          : doctor_id,
//            hospital_id        : hospital_id },
//          function(data){
//             var results = eval("(" + data + ")");
//
//             if (results.success == false) {
//                msg = results.message;
//             }
//             else {
//
//                msg = 'Success. The chosen hospital was deleted from your profile.';
//             }
//
//             $('#hospital_msg').html(msg);
//             $('#hospital_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);
//
//             // Get new hospital listings
//             $.post(callback_url,
//                { doctor_id : doctor_id },
//                function(data){
//                   $('#hospital_list_container').html(data);
//                }
//             );
//
//          }
//       );
//       return false;

   }

   function remove_healthplan(doctor_id, healthplan_id, url, callback_url) {
      return delete_healthplan(doctor_id, healthplan_id, url, callback_url);
   }



   function saveSelectedSpecialty(doctor_id, url, callback_url) {
      var selectedOption = $('#specialty option:selected');
   var specialty_id = selectedOption.val();

      $.post(url,
         { doctor_id       : doctor_id,
           specialty_id    : specialty_id},
         function(data){
            var results = eval("(" + data + ")");


            if (results.success == false) {
               msg = results.message;
            }
            else {
               msg = 'Success. The chosen specialty was added to your profile.';
            }

            // Render the new specialty listing

            $("#specialty_listing").hide();
            $("#specialty_add_options").hide();

            $('#specialty_msg').html(msg);
            $('#specialty_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new specialty listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
               $('#specialty_list_container').html(data);

               }
            );

         }
      );
      return false;

   }

   function saveSelectedSubSpecialty(doctor_id, url, callback_url) {
      var selectedOption   = $('#subSpecialty option:selected');
      var subspecialty_id  = selectedOption.val();

      $.post(url,
         { doctor_id          : doctor_id,
           subspecialty_id    : subspecialty_id},
         function(data){
            var results = eval("(" + data + ")");


            if (results.success == false) {
               msg = results.message;
            }
            else {
               msg = 'Success. The chosen subspecialty was added to your profile.';
            }

            // Render the new specialty listing

            $("#subspecialty_listing").hide();
            $("#subspecialty_add_options").hide();

            $('#subspecialty_msg').html(msg);
            $('#subspecialty_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new specialty listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
               $('#subspecialty_list_container').html(data);

               }
            );

         }
      );
      return false;

   }




   function newSubSpecialty(url) {

      $.post(url,
         { subspecialty_name: $("#subspecialty_name").val() },
         function(data){
            if (data == 'false') {
               msg = 'Warning. Your data could not be saved.';
            }
            else {
               var Obj = document.getElementById('subSpecialty');
               var value = $("#subspecialty_name").val();
               Obj.options.add(new Option(value, data));

               msg = 'Success. Your subspecialty data was saved.';
            }

            $('#subspecialty_mgr_msg').html(msg);
            $('#subspecialty_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

/*            $("#subspecialty_mgr").hide();
            $("#subspecialty_mgr").after(msg);*/
         }
      );
      return false;
   }

   function newHospital(url) {
      $.post(url,
         { hospital_name: $("#hospital_name").val() },
         function(data){
            if (data == 'false') {
               msg = 'Warning. Your data could not be saved.';
            }
            else {
               var Obj = document.getElementById('hospitals');
               var value = $("#hospital_name").val();
               Obj.options.add(new Option(value, data));
               msg = 'Success. Your hospital data was saved.';
            }

            $('#hospital_mgr_msg').html(msg);
            $('#hospital_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

/*            $("#hospital_mgr").hide();
//             $("#hospital_mgr").after(msg);
            $('#hospital_msg').html(msg);
            $('#hospital_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);*/
         }
      );
      return false;
   }

   function saveSelectedHospital(doctor_id, url, callback_url) {
      var selectedOption = $('#hospitals option:selected');

   var hospital_id = selectedOption.val();

      $.post(url,
         { doctor_id    : doctor_id,
           hospital_id  : hospital_id },
         function(data){

            var results = eval("(" + data + ")");


            if (results.success == false) {
               msg = results.message;
            }
            else {
/*               var Obj = document.getElementById('hospitals');
               var value = $("#hospital_name").val();
               Obj.options.add(new Option(value, data));*/
               msg = 'Success. The chosen hospital was added to your profile.';
            }

            // Render the new hospital listing

            $("#hospital_listing").hide();
            $("#hospital_add_options").hide();

//             $("#hospital_mgr").hide();
//             $("#show-hospital-list").after(msg);
            $('#hospital_msg').html(msg);
            $('#hospital_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new hospital listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
                  $('#hospital_list_container').html(data);
               }
            );

         }
      );
      return false;

   }

   function saveSelectedHealthplan(doctor_id, url, callback_url) {
      var selectedOption = $('#healthplans option:selected');

      var healthplan_id = selectedOption.val();

      $.post(url,
         { doctor_id       : doctor_id,
           healthplan_id   : healthplan_id },
         function(data){
            var results = eval("(" + data + ")");

            if (results.success == false) {
               msg = results.message;
            }
            else {
               msg = 'Success. The chosen healthplan was added to your profile.';
            }

            // Render the new hospital listing
            $("#healthplan_listing").hide();
            $("#healthplan_add_options").hide();

            $('#healthplan_msg').html(msg);
            $('#healthplan_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new healthplan listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
                  $('#healthplan_list_container').html(data);
               }
            );

         }
      );
      return false;

   }





   // Delete ('remove') from profile
   function delete_hospital(doctor_id, hospital_id, url, callback_url) {

//    var hospital_id = selectedOption.val();

      $.post(url,
         { doctor_id    : doctor_id,
           hospital_id  : hospital_id },
         function(data){
            var results = eval("(" + data + ")");


            if (results.success == false) {
               msg = results.message;
            }
            else {

/*               var Obj = document.getElementById('hospitals');
               var value = $("#hospital_name").val();
               Obj.options.add(new Option(value, data));*/
               msg = 'Success. The chosen hospital was deleted from your profile.';
            }

            // Render the new hospital listing

//             $("#hospital_listing").hide();
//             $("#hospital_add_options").hide();

//             $("#hospital_mgr").hide();
            // $("#show-hospital-list").after(msg);
            $('#hospital_msg').html(msg);
            $('#hospital_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new hospital listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
                  $('#hospital_list_container').html(data);
               }
            );

         }
      );
      return false;

   }

   function delete_healthplan(doctor_id, healthplan_id, url, callback_url) {

      $.post(url,
         { doctor_id       : doctor_id,
           healthplan_id   : healthplan_id },
         function(data){

            var results = eval("(" + data + ")");

            if (results.success == false) {
               msg = results.message;
            }
            else {
/*               var Obj = document.getElementById('hospitals');
               var value = $("#hospital_name").val();
               Obj.options.add(new Option(value, data));*/
               msg = 'Success. The chosen health plan was deleted from your profile.';
            }

            // Render the new hospital listing

//             $("#hospital_listing").hide();
//             $("#hospital_add_options").hide();

//             $("#hospital_mgr").hide();
            // $("#show-hospital-list").after(msg);
            $('#healthplan_msg').html(msg);
            $('#healthplan_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

            // Get new healthplan listings
            $.post(callback_url,
               { doctor_id : doctor_id },
               function(data){
                  $('#healthplan_list_container').html(data);
               }
            );

         }
      );
      return false;
   }


   function newHealthplan(url) {
      $.post(url,
         { healthplan_name: $("#healthplan_name").val() },
         function(data){
            if (data == 'false') {
               msg = 'Warning. Your data could not be saved.';
            }
            else {
               var Obj = document.getElementById('healthplans');
               var value = $("#healthplan_name").val();
               Obj.options.add(new Option(value, data));
               msg = 'Success. Your health plan data was saved.';
            }
            $('#healthplan_mgr_msg').html(msg);
            $('#healthplanl_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

/*            $("#healthplan_mgr").hide();
            $("#healthplan_mgr").after(msg);*/
         }
      );
      return false;
   }

   function DeleteSelectedSpecialty(url) {
      $('option:selected','#specialty').each( function() {

         val = this.value;
         txt = this.text;
         // vals.push( this.value )
         var answer = confirm ("You are about to permanently delete specialty " + txt + ".\n This will affect all physicians that have this specialty linked to their profile.\n  Are you sure you want to continue?");
         if (answer) {

            $.post(url,
               { specialty_id : val },
               function(data){
                  var results = eval("(" + data + ")");

                  if (results.success == false) {
                     msg = results.message;
                  }
                  else {
                     msg = 'Success. The chosen specialty was removed from the system.';
                  }

//                   $('#warning').addClass('warning');
                  $('#specialty_mgr_msg').html(msg);
                  $('#specialty_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);
               }
            );

         }

         // Row remove the item from the select box
         $(this).remove();

      } );

   }

   function DeleteSelectedSubSpecialty(url) {
      $('option:selected','#subSpecialty').each( function() {

         val = this.value;
         txt = this.text;
         // vals.push( this.value )
         var answer = confirm ("You are about to permanently delete subspecialty " + txt + ".\n This will affect all physicians that have this subspecialty linked to their profile.\n  Are you sure you want to continue?");
         if (answer) {

            $.post(url,
               { subspecialty_id : val },
               function(data){
                  var results = eval("(" + data + ")");

                  if (results.success == false) {
                     msg = results.message;
                  }
                  else {
                     msg = 'Success. The chosen subspecialty was removed from the system.';
                  }

//                   $('#warning').addClass('warning');
                  $('#subspecialty_mgr_msg').html(msg);
                  $('#subspecialty_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);
/*                  $('#warning').addClass('warning');
                  $('#warning').html(msg);
                  $('#warning').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);*/
               }
            );

         }

         // Row remove the item from the select box
         $(this).remove();

      } );

   }

   function DeleteSelectedHospital(url) {
      $('option:selected','#hospitals').each( function() {

         val = this.value;
         txt = this.text;
         // vals.push( this.value )
         var answer = confirm ("You are about to permanently delete hospital " + txt + ".\n This will affect all physicians that have this hospital linked to their profile.\n  Are you sure you want to continue?");
         if (answer) {

            $.post(url,
               { hospital_id : val },
               function(data){
                  var results = eval("(" + data + ")");

                  if (results.success == false) {
                     msg = results.message;
                  }
                  else {
                     msg = 'Success. The chosen hospital was removed from the system.';
                  }

//                   $('#warning').addClass('warning');
                  $('#hospital_mgr_msg').html(msg);
                  $('#hospital_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

/*                  $('#warning').addClass('warning');
                  $('#warning').html(msg);
                  $('#warning').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);*/
               }
            );

         }

         // Row remove the item from the select box
         $(this).remove();

      } );

   }

   function DeleteSelectedHealthplan(url) {
      $('option:selected','#healthplans').each( function() {

         val = this.value;
         txt = this.text;
         // vals.push( this.value )
         var answer = confirm ("You are about to permanently delete health plan " + txt + ".\n This will affect all physicians that have this health plan linked to their profile.\n  Are you sure you want to continue?");
         if (answer) {

            $.post(url,
               { healthplan_id : val },
               function(data){
                  var results = eval("(" + data + ")");

                  if (results.success == false) {
                     msg = results.message;
                  }
                  else {
                     msg = 'Success. The chosen health plan was removed from the system.';
                  }

//                   $('#warning').addClass('warning');
                  $('#healthplan_mgr_msg').html(msg);
                  $('#healthplan_mgr_msg').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

/*                  $('#warning').addClass('warning');
                  $('#warning').html(msg);
                  $('#warning').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);*/
               }
            );

         }
//          else {
//             ;
//          }

         // Row remove the item from the select box
         $(this).remove();

      } );

   }

   function GetPatientDetails(url) {
      $.get(url,
         function(data){
            $('#patient-form-container').html(data);
            $('#patient-form-container').show();
         }
      );
   }

   function GetLinkCategoryDetails(url) {
      $.get(url,
         function(data){
            $('#subcategory_div').html(data);
            $('#subcategory_div').show();
         }
      );
   }

   function GetLinkDetails(url) {
      $.get(url,
         function(data){
            $('#link_div').html(data);
            $('#link_div').show();
         }
      );
   }


   function DeleteLinkCategory(url) {
      $.get(url,
         function(data){

            try {
               var results = eval("(" + data + ")");
            }
            catch(err) {
               var results = "unknown";
            }

            if (results.success == false) {
               msg = results.message;
            }
            else {
               msg = 'The chosen link category was removed from the system.';
               $("#category-container").html(data);
            }

            $('#link-message').addClass('warning');
            $('#link-message').html(msg);
            $('#link-message').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

         }
      );
   }

   function DeleteLink(url) {

      $.get(url,
         function(data){

            try {
               var results = eval("(" + data + ")");
            }
            catch(err) {
               var results = "unknown";
            }

            if (results.success == false) {
               msg = results.message;
            }
            else {
               msg = 'The chosen link was removed from the system.';
               $("#category-container").html(data);
            }

            $('#link-message').addClass('warning');
            $('#link-message').html(msg);
            $('#link-message').fadeIn(2000).fadeTo(5000, 1).fadeOut(2000);

         }
      );
   }

