

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

jQuery.Clinics = {
	options : {
		robot : 'no',
		wwwroot : ''
	},
	
	init : function(options)
	{
		if (options)
			jQuery.extend(jQuery.Clinics.options, options);

		if (jQuery.browser.msie) {

		}
	},
	
	checkZip : function(zip){
		if(zip.length == 5 && IsNumeric(zip))return zip;
	},
	
	//get the list of clinics from the server based on the zip that was entered
	getClinics : function(input){
		var zip = jQuery(input).val();
		if(jQuery.Clinics.checkZip(zip)){
			jQuery('#findClinicError').html('');
			var request = {'clinics':'byZip','zip':zip};
			jQuery.Clinics.sendSubmit(jQuery.Clinics.options.wwwroot+'lib/getGeoIP.php',request,jQuery.Clinics.loadClinics);
		}else{
			var request = {'clinics':'alpha'};
			jQuery.Clinics.sendSubmit(jQuery.Clinics.options.wwwroot+'lib/getGeoIP.php',request,jQuery.Clinics.loadClinics);
			jQuery('#findClinicError').html('Please enter a valid zipcode to calculate distance.');
		}
	},
	
	//load the returned list of clinics
	loadClinics : function(getBack){
		//write the list of clinics
		jQuery('#clinicList').empty();
		var UL = document.createElement('ul');
		jQuery(UL).attr('id','clinicsByDistance');
		for(var i=0;i<getBack.clinicArray.length;i++){
			var clinic = getBack.clinicArray[i];
			jQuery(UL).append(jQuery.Clinics.createClinicLI(clinic));
		}
		jQuery('#clinicList').append(UL);
		jQuery(UL)
		.css('height','0px')
		.animate({
				'height':'130px'
				},1500);
	},
	
	createClinicLI : function(clinic){
		//<li><a href=\"$CFG->wwwroot/painfree/$url\"><span class=\"clinicName\">$name</span> - <span class=\"clinicDistance\">$distance Miles</span></li></a>
		var clinicLI = document.createElement('li');
		var a = document.createElement('a');
		jQuery(a).attr('href',jQuery.Clinics.options.wwwroot+'painfree/'+clinic.url);
		var clinicName = document.createElement('span');
		jQuery(clinicName).attr('class','clinicName').append(document.createTextNode(clinic.clinic + ' - '));
		var clinicDistance = document.createElement('span');
		jQuery(clinicDistance).attr('class','clinicDistance').append(document.createTextNode(clinic.distance + ' Miles'));
		jQuery(a).append([clinicName,clinicDistance]);
		jQuery(clinicLI).append(a);
		return clinicLI;
	},
	
	sendSubmit : function(url,request,callback){
		var ul_vars = {
		  interval : 1000, //The time in milleseconds between each status request.
		  speeds : []      //Keeps track of the speeds of each upload.
		};
		setTimeout(function(){
			submitter = new RepeatGetAction(url, request, ul_vars.interval);
			submitter.go();
			submitter.successFunc = function (getBack) {
				setTimeout(function (){callback(getBack);},100);
			}
			submitter.failFunc = function (getBack) {
		    	jQuery('#findClinicError').html('Please enter a valid zipcode');
		  	}
			//this is what we do at the end
		;},100)
	}
};

//run this script when the document is ready
$(document).ready(function(){
	$.Clinics.init(
		{
			robot: 'test',
			wwwroot : $('#www0').text()
		}
	);
	//attach the find clinic action to the button and maybe some color changes.
	$('#findClinicButton').bind('click',function(e){
		$.Clinics.getClinics($('#findClinicInput'));
	});
	$('#findClinicInput').keypress(function(e){
		if(e.which == 13 || e.which == 3)$.Clinics.getClinics($('#findClinicInput'));
	});
	$('#findClinicInput').bind('click',function(e){
		if($('#findClinicInput').val() == 'zip')$('#findClinicInput').val('');
	});
});