
var CityCombo ;
var CountyCombo ;
var txtZipCode ;
var marker;
Ext.onReady(function()
{
var OrgTypeDS=Genesis.Twogether.Store('../Handlers/LibraryFunction.ashx?cmd=OrganizationType','','','');
var CurriculumDS=Genesis.Twogether.Store('../Handlers/LibraryFunction.ashx?cmd=Curriculum','','','');
var MarriageEducationTypeDS=Genesis.Twogether.Store('../Handlers/LibraryFunction.ashx?cmd=MarriageEducationType','','','');


   Ext.QuickTips.init();
	 CityCombo = new Genesis.ComboBox({
		store: DistinctCityDS,
		displayField:'NAME',
		valueField: 'ID',
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		hideTrigger	:false,
		listWidth:150,
		selectOnFocus:true,
		autocomplete:'off',
		width:100,
		applyTo: 'together_LeftSearchMenu1_txtCity',
		  listeners:{select:{fn:function(combo, value) {
		  CountyCombo.setValue('');
		  txtZipCode.setValue('');
	}}}
	});

	 CountyCombo = new Genesis.ComboBox({
		store: CountyDS,
		displayField:'NAME',
		valueField: 'ID',
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		hideTrigger	:false,
		listWidth:150,
		width:100,
		selectOnFocus:true,
		autocomplete:'off',
		applyTo: 'together_LeftSearchMenu1_txtCounty',  		
		listeners:{select:{fn:function(combo, value) {
			CityCombo.setValue('');
			txtZipCode.setValue('');
	}}}
	});

	 txtZipCode = new Genesis.NumTextBox({		
		applyTo: 'together_LeftSearchMenu1_txtZipcode',
		width:100,
		listeners:{change:{fn:function(txt, value,newvalue) {
			CountyCombo.setValue('');
			CityCombo.setValue('');
	}}}
	});

	var txtProviderType=new Genesis.ComboBox({
            store: OrgTypeDS,
            typeAhead: false,
            displayField:'NAME',
            valueField:'ID',
       		hideTrigger	:false,
            mode: 'local',
            triggerAction: 'all',
            listWidth:250,
            width:100,
            selectOnFocus:true,
            autocomplete:'off',
            applyTo:'together_LeftSearchMenu1_txtProviderType',
            listeners:{select:{fn:function(combo, value) {
				document.getElementById('together_LeftSearchMenu1_hdnProviderType').value=combo.getValue(); 
				document.getElementById('together_LeftSearchMenu1_hdnEdutype').value='';
				document.getElementById('together_LeftSearchMenu1_hdnCuriculla').value='';
				txtCuriculla.setValue('');
				txtEdutype.setValue('');
		}}}
	})
	var txtCuriculla=new Genesis.ComboBox({
            store: CurriculumDS,
            typeAhead: false,
            displayField:'NAME',
            valueField:'ID',
       		hideTrigger	:false,
            mode: 'local',
            triggerAction: 'all',
			listWidth:250,
			width:100,
            selectOnFocus:true,
            autocomplete:'off',
            applyTo:'together_LeftSearchMenu1_txtCuriculla',
			listeners:{select:{fn:function(combo, value) {
				document.getElementById('together_LeftSearchMenu1_hdnCuriculla').value=combo.getValue(); 
				document.getElementById('together_LeftSearchMenu1_hdnEdutype').value='';
				document.getElementById('together_LeftSearchMenu1_hdnProviderType').value='';
				txtProviderType.setValue('');
				txtEdutype.setValue('');
		}}}
	})
	var txtEdutype=new Genesis.ComboBox({
            store: MarriageEducationTypeDS,
            typeAhead: false,
            displayField:'NAME',
            valueField:'ID',
       		hideTrigger	:false,
            mode: 'local',
			listWidth:250,
			width:100,
            triggerAction: 'all',
            selectOnFocus:true,
            autocomplete:'off',
            applyTo:'together_LeftSearchMenu1_txtEdutype',
			listeners:{select:{fn:function(combo, value) {
				document.getElementById('together_LeftSearchMenu1_hdnEdutype').value=combo.getValue(); 
				document.getElementById('together_LeftSearchMenu1_hdnCuriculla').value='';
				document.getElementById('together_LeftSearchMenu1_hdnProviderType').value='';
				txtProviderType.setValue('');
				txtCuriculla.setValue('');
		}}}
	})


	var txtRange=new Genesis.ComboBox({
            store: RangeDS,
            typeAhead: false,
            displayField:'NAME',
            valueField:'ID',
       		hideTrigger	:false,
            mode: 'local',
            triggerAction: 'all',
            selectOnFocus:true,
            autocomplete:'off',
            width:100,
            applyTo:'together_LeftSearchMenu1_txtRange' ,
            listeners:{select:{fn:function(combo, value) {
				document.getElementById('together_LeftSearchMenu1_hdnRange').value=combo.getValue(); 
		}}}
	})
	
	if(document.getElementById('together_LeftSearchMenu1_hdnSearchStatus').value  =="1")
	{
	    var ProviderType=	document.getElementById('together_LeftSearchMenu1_hdnProviderTypeName').value;
		var Curiculla=	document.getElementById('together_LeftSearchMenu1_hdnCuricullaName').value;
		var Edutype=	document.getElementById('together_LeftSearchMenu1_hdnEdutypeName').value;
		var Range=	document.getElementById('together_LeftSearchMenu1_hdnRange').value;
		
		txtProviderType.setValue(ProviderType);
		txtCuriculla.setValue(Curiculla);
		txtEdutype.setValue(Edutype);
		if ( Range!="")
			txtRange.setValue(Range);
		else
			txtRange.setValue('25');
	}
	
	else
	{
	   txtProviderType.setValue('');
	   txtCuriculla.setValue('');
	   txtEdutype.setValue('');
	   
		txtRange.setValue('25');
	}

  // marker = new GMarker(new GLatLng(37.4228, -122.085)); 

});


function ValidateHomePage()
{
    var city,county,zip;
    city=trim(document.getElementById('together_LeftSearchMenu1_txtCity').value);
    county=trim(document.getElementById('together_LeftSearchMenu1_txtCounty').value);
    zip=trim(document.getElementById('together_LeftSearchMenu1_txtZipcode').value);
   var dvError=document.getElementById('dvError');
    dvError.innerHTML='';
    if(city=='' && county=='' && zip=='')
    {
        //ShowError('Please provide value of City or County or Zip Code');
        dvError.innerHTML='Please provide value of City or County or Zip Code';
        document.getElementById('together_LeftSearchMenu1_txtZipcode').focus();
        return false;
    }
    else if(zip.length<5 && zip.length>0 )
    {
		//ShowError('Invalid Zip Code format. Please provide the Zip Code in 5 digit format.');
		dvError.innerHTML='Invalid Zip Code format. Please provide the Zip Code in 5 digit format.';
		document.getElementById('together_LeftSearchMenu1_txtZipcode').focus();
		return false;
    }
    
    return true;
}

var map ;
var gdir;
var geocoder = null;
var addressMarker;
var providername,providerAddress,zipcode;
function OpenGoogleMap(provider,address,zip)
{	        
		  providername=provider;
		  providerAddress=address;
		  zipcode=zip
			setVisible('layer1',true);			
			document.getElementById("directions").innerHTML='';
		   //	marker = new GMarker(new GLatLng(latitute, longitute)); 
			if (GBrowserIsCompatible()) 
			{
				map = new GMap2(document.getElementById("map_canvas"));
				var ZoomLevel = 13;	  
				
				map.setUIToDefault();
                geocoder = new GClientGeocoder();
                if (geocoder) 
                {
					 geocoder.getLatLng(
                        providerAddress,
						function(point)
                        {
                            if (!point) 
                            {
                                ShowError(providerAddress + " not found");
                            }
                            else
                            {	                             
									gdir = new GDirections(map, document.getElementById("directions"));
									GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
									GEvent.addListener(gdir, "error", handleErrors);
									          
									var html = '<div style="width:230px; padding-right:10px;">'+
										'Address:'+ providername+ '<br/>' + providerAddress +'<br \><br \>'+  
										'<a href="javascript:ToHere('+ zipcode + ',\''+ providerAddress +'\');">Get Directions</a> - '+
										'<a href="javascript:ZoomIn('+ZoomLevel+')">Zoom In</a> - '+
										'<a href="javascript:ZoomOut('+ZoomLevel + ')">Zoom Out</a>'+
										'</div>';
									
								    map.setCenter(point, ZoomLevel);
									marker = new GMarker(point, {draggable: true});
                                
									//map.setCenter(new GLatLng(latitute, longitute), ZoomLevel);           
									map.addOverlay(marker);
									marker.openInfoWindowHtml(html);
									GEvent.addListener(marker, "click", function() {
									marker.openInfoWindowHtml(html);});
									map.addControl(new GSmallMapControl());
								    map.addControl(new GMapTypeControl());
							 }
						}
					 )
				}
           } 

		 var dirRH1=document.getElementById("dirRH1");
		 var dirRH2=document.getElementById("dirRH2");
         var mapRH2=document.getElementById("mapRH2");
		 
		 dirRH1.style.display="none";
         dirRH2.style.display="none";	 

    return false;   
}

        function ToHere(zip,address) {
                  
           var testhtml = '<div style="width:230px; padding-right:10px;">'+
					'Address:'+ providername+ '<br/>' + providerAddress +'<br \><br \>'+
                    'Get Directions: To here - <a href="javascript:FromHere(\''+ zip + '\',\''+ address +'\');">From here</a><br \>'+
                    'Start address<br \><input  type="text" id="txtFrom"><input type="button" onclick="setDirections(\'\'' + ',\'' + address +'\'); return false" name="btnGo" value="Go"></div>';        
                    
            marker.openInfoWindowHtml(testhtml);
            document.getElementById("txtFrom").focus();
        }
        
        function FromHere(zip,address) {
            var testhtml = '<div style="width:230px; padding-right:10px;">'+
					'Address:'+ providername+ '<br/>' + providerAddress +'<br \><br \>'+
                    'Get Directions: <a href="javascript:ToHere(\''+ zip + '\',\''+ address +'\');">To here</a> - From here<br \>'+
                    'End address<br \><input  type="text" id="txtTo"><input type="button" onclick="setDirections(\''+ address +'\','+'\'\'); return false" name="btnGo" value="Go"></div>';        
            marker.openInfoWindowHtml(testhtml);
            document.getElementById("txtTo").focus();
        }
        
        function ZoomIn(ZoomLevel)
        {
            ZoomLevel = parseInt(ZoomLevel, 0) + 1;
            if (GBrowserIsCompatible()) {
				map.setUIToDefault();
                geocoder = new GClientGeocoder();
                if (geocoder) 
                {
					 geocoder.getLatLng(
                        providerAddress,
						function(point)
                        {
                            if (!point) 
                            {
                                ShowError(providerAddress + " not found");
                            }
                            else
                            {	                             
									gdir = new GDirections(map, document.getElementById("directions"));
									GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
									GEvent.addListener(gdir, "error", handleErrors);
									var html = '<div style="width:230px; padding-right:10px;">'+
										'Address:'+ providername+ '<br/>' + providerAddress +'<br \><br \>'+  
										'<a href="javascript:ToHere('+ zipcode + ',\''+ providerAddress +'\');">Get Directions</a> - '+
										'<a href="javascript:ZoomIn('+ZoomLevel+')">Zoom In</a> - '+
										'<a href="javascript:ZoomOut('+ZoomLevel + ')">Zoom Out</a>'+
										'</div>';
									
								    map.setCenter(point, ZoomLevel);
									marker = new GMarker(point, {draggable: true});
                                
									//map.setCenter(new GLatLng(latitute, longitute), ZoomLevel);           
									map.addOverlay(marker);
									marker.openInfoWindowHtml(html);
									GEvent.addListener(marker, "click", function() {
									marker.openInfoWindowHtml(html);});
									map.addControl(new GSmallMapControl());
								    map.addControl(new GMapTypeControl());
							 }
						}
					 )
				}
          }
        }
        function ZoomOut(ZoomLevel)
        {
            ZoomLevel = parseInt(ZoomLevel, 0) - 1;
            if (GBrowserIsCompatible()) {
            map.setUIToDefault();
                geocoder = new GClientGeocoder();
                if (geocoder) 
                {
					 geocoder.getLatLng(
                        providerAddress,
						function(point)
                        {
                            if (!point) 
                            {
                                ShowError(address + " not found");
                            }
                            else
                            {	                             
									gdir = new GDirections(map, document.getElementById("directions"));
									GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
									GEvent.addListener(gdir, "error", handleErrors);
									          
									var html = '<div style="width:230px; padding-right:10px;">'+
										'Address:'+ providername+ '<br/>' + providerAddress +'<br \><br \>'+  
										'<a href="javascript:ToHere('+ zipcode + ',\''+ providerAddress +'\');">Get Directions</a> - '+
										'<a href="javascript:ZoomIn('+ZoomLevel+')">Zoom In</a> - '+
										'<a href="javascript:ZoomOut('+ZoomLevel + ')">Zoom Out</a>'+
										'</div>';
										
								    map.setCenter(point, ZoomLevel);
									marker = new GMarker(point, {draggable: true});
                                
									//map.setCenter(new GLatLng(latitute, longitute), ZoomLevel);           
									map.addOverlay(marker);
									marker.openInfoWindowHtml(html);
									GEvent.addListener(marker, "click", function() {
									marker.openInfoWindowHtml(html);});
									map.addControl(new GSmallMapControl());
								    map.addControl(new GMapTypeControl());
							 }
						}
					 )
				}
          }
        }
       
    function setDirections(fromAddress, toAddress) {
       
         var dirRH1=document.getElementById("dirRH1");
		 var dirRH2=document.getElementById("dirRH2");
         var mapRH2=document.getElementById("mapRH2");
		 
		 dirRH1.style.display="block";
         dirRH2.style.display="block";
         
        var locale=document.getElementById("together_Header1_hdnLanguage").value; 
       
		if(	fromAddress=='')					 
			fromAddress=document.getElementById("txtFrom").value;
		else
			toAddress=document.getElementById("txtTo").value;
		
		map.removeOverlay(marker);
	  // toAddress='818 E. 53rd Street, Austin,78751'     ;
	  // fromAddress	 ='3601 N Progress Ave, Harrisburg, 17110';
		gdir.load("from: " + fromAddress + " to: " + toAddress,	 
			{ "locale": locale });
		
    }

    function handleErrors(){
		   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			 ShowError("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)
			 ShowError("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)
			 ShowError("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)
			 ShowError("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)
			 ShowError("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		   else ShowError("An unknown error occurred.");
    }

  function onGDirectionsLoad(){ 
	   var poly = gdir.getPolyline();
	   if (poly.getVertexCount() > 100) {
		// ShowError("This route has too many vertices");
		 return;
	   }
	   var baseUrl = "http://maps.google.com/staticmap?";

	   var params = [];
	   var markersArray = [];
	   markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
	   markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
	   params.push("markers=" + markersArray.join("|"));

	   var polyParams = "rgba:0x0000FF80,weight:5|";
	   var polyLatLngs = [];
	   for (var j = 0; j < poly.getVertexCount(); j++) {
		 polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
	   }
	   params.push("path=" + polyParams + polyLatLngs.join("|"));
	   params.push("size=300x300");
	   //THIS IS GENESIS KEY
	   // params.push("key=ABQIAAAAjU0EJWnWPMv7oQ-jjS7dYxSPW5CJgpdgO_s4yyMovOaVh_KvvhSfpvagV18eOyDWu7VytS6Bi1CWxw");
	   //THIS IS TEXAS DEVTEST ENV KEY
	   //params.push("key=AABQIAAAATwhaMSTL_taFTW3eUtHDABSLv5k-kTTOWqvhZXeBXI9Iw-wH8hTdguk4fpUX6uXL3iuCazc_cfgmKw");
	   //THIS IS TEXAS PROD ENV KEY KEY
        params.push("key=ABQIAAAATwhaMSTL_taFTW3eUtHDABQzqXZZ4oKT3sJCis18WZwuaGjhvhQGq7882YdrNt5cdu9GEF_2eG6A6g");
}

