var map = null;
var geocoder = null;
var mlong = null;
var mlat = null;
var mcity = null;
var mstate = null;
var place = null;
var maddress = null;
var mzip = null;
	
function load() 
{
    if (GBrowserIsCompatible())
        geocoder = new GClientGeocoder();
}	
	
    
function showAddress(address2, istrue)
{
    if(istrue)
        ss=istrue;
    if (geocoder)
    {
        geocoder.getLatLng(
            address2,
            function(point)
            {
                if (!point)
                {
                    alert("Address: " + address2 + " not found, please re-enter address!");
                    if(ss==true){
                        ss=false;
                        document.getElementById("btn1").style.display="block";                        
                        document.getElementById("address1").focus();
                        if (GBrowserIsCompatible()){
                            geocoder = new GClientGeocoder();
                            map = new GMap2(document.getElementById("map"));
                            map.addControl(new GSmallMapControl());
                            map.setCenter(new GLatLng(0,0), 13);
                        }
                    }
                    document.getElementById("Specs4").style.display = "";
                }
                else
                {
                    map = new GMap2(document.getElementById("map"));
                    map.addControl(new GSmallMapControl());
                    mlong = point.x;
                    mlat = point.y;
                    map.setCenter(point, 13);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    geocoder.getLocations(address2,GetLocationsFunction);
                }
            }
            );
    }
}
	
    
function GetLocationsFunction(response) 
{
    if (!response || response.Status.code != 200)
    {
        alert("Sorry, we were unable to geocode that address");
        if(ss==true){
            ss=false;
            document.getElementById("btn1").style.display="block";
            document.getElementById("address1").focus();
            if (GBrowserIsCompatible()){
                geocoder = new GClientGeocoder();
                map = new GMap2(document.getElementById("map"));
                map.addControl(new GSmallMapControl());
                map.setCenter(new GLatLng(0,0), 13);
            }
        }
    }
    else
    {
        place = response.Placemark[0];
            
        /*
			alert('address is '+place.address);
			alert('accuracy is '+place.AddressDetails.Accuracy);
			*/
        var parts = place.address.split(',');
        /*
			for (var i= 0; i<parts.length; i++){
				alert(parts[i]);
			}
			*/
        point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
        //showPlace(place);
        if(place.address.toLowerCase().indexOf("usa")>=0){
            mSpecs2 = "<p><strong>Is this address correct? <br>'<font color=#330000>" + place.address + "</font>'</strong>"
            +"<br><BR>"
            + "<input type=\"button\" class=\"formButton2\" value=\"Yes, continue\" onclick=\"javascript:YesProcess()\" /> &nbsp;"
            + "<input type=\"button\" class=\"formButton2\" value=\"No\" onclick=\"javascript:NoProcess()\" /><br><br>";
        }
        else
        {
            ss=false;
            document.getElementById("btn1").style.display="block";
            alert("Address is out of USA");
            
            document.getElementById("Specs4").style.display = "none";
            document.getElementById("Specs3").style.display = "none";
            document.getElementById("address1").focus();
        }
        try{
            if (document.all)
            {
                Specs2.innerHTML=mSpecs2;
            }

            else if (document.getElementById)
            {
                document.getElementById("Specs2").innerHTML=mSpecs2;
            }
            mstate = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
        }catch(e){}
        try{
            mcity = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
            mzip = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
            maddress = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
        }
        catch(e){
            try{
                mcity = place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                mzip = place.AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                maddress = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
            }catch(e){}
        }
			
			
    }
}

    
function NoProcess() 
{
    if (document.all)
    {
        map.innerHTML="";
        Specs2.innerHTML="";
        Specs3.innerHTML= "";
    }
    else if (document.getElementById)
    {
        document.getElementById("map").innerHTML="";
        document.getElementById("Specs2").innerHTML="";
        document.getElementById("Specs3").innerHTML="";
    }
    if(ss==true)
    {
        ss=false;
        document.getElementById("btn1").style.display="block";
        document.getElementById("address1").focus();
        // document.getElementById("tzip").readOnly = false;
        if (GBrowserIsCompatible()){
            geocoder = new GClientGeocoder();
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GSmallMapControl());
            map.setCenter(new GLatLng(0,0), 13);
        }
    }
}	
	

function YesProcess() 
{
    var temp="";
    if(maddress!=null)
        document.getElementById("address").value = maddress;
    else{
        temp = document.getElementById("taddress").value;
        document.getElementById("address").value = temp;
    }//document.getElementById("address").value = maddress;
    document.getElementById("city").value=mcity;
    document.getElementById("state").value=mstate;
    document.getElementById("lat").value=mlat;
    document.getElementById("long").value=mlong;
    if(mzip!=null)
        document.getElementById("zip").value=mzip;
    else{
        temp = document.getElementById("tzip").value;
        document.getElementById("zip").value = temp;
    }//document.getElementById("zip").value=mzip;
    //alert(mlong);
    document.getElementById("signup").submit();
}		
	
/* New additions made on 2008-01-02, all the js functions related to customer and restaurant signup
    are in this file. No need to include other functions or scripts 
    
    Functions:
		isAddress
		isZip
		processFirstStep
*/
    
function isAddress(input) {
    return(input.value.length > 1);
} 

function isZip(input) {
    return(input.value.length == 5);
} 

function processFirstStep() {
    var form = document.getElementById('signup');
    var error = "Error: \n";
		
    if (!isAddress(form.address1)) {
        error += "Please enter an Address;\n";
    }

    if (!isZip(form.zip)) {
        error += "Please enter a valid Zip Code - 5 characters;\n";
    }
				
    if (error == "Error: \n") {
        document.getElementById("btn").style.display = "none";
        document.getElementById("btn1").style.display = "none";
        var FullAddress = form.address1.value + ", " + form.zip.value;
        load();
        showAddress(FullAddress,true);
        document.getElementById("Specs4").style.display = "none";
        document.getElementById("Specs3").style.display = "none";
    } else {
        alert(error);
        return false;
    }
}
function keycheck(ev){
    var key = (window.event) ? window.event.keyCode : ev.keyCode;
    var RETURN = 13;
    switch(key){
        case RETURN:
            processFirstStep();
            break;
    }
}

