
var places = null;
var map = null;
var geocoder = null;
var text = "";
var acc = 0;
var address = null;
var city = null;
var state = null;
var zip = null;
var business = null;

function initialize(){

    if (GBrowserIsCompatible()) {
        geoXml = new GGeoXml("http://centraltelcom.com/media/kml/k2.kml");

        //map = new GMap2(document.getElementById("map_canvas"));
        //map.setCenter(new GLatLng(39.6262665, -111.4392846), 10);
        geocoder = new GClientGeocoder();

        //map.setUIToDefault();
        //map.addOverlay(geoXml);
    }

    $("input[type='submit']").click(addressSubmit);
    $("#addressForm").validate();
    
    
    //auto select set up
    var thing = $(".auto_suggest_option input").toArray();
    var arr = new Array();
    for(var t in thing)arr[t] = $(thing[t]).val();
    $("#installation_city").autocomplete({source: arr, select: autosuggestselect});

}

function autosuggestselect(event, ui){
    var key = $(ui.item).val();
    var parent = $(".auto_suggest_option input[value='" + key + "']").parent();
    ui.item.value = $(".city", parent).html();
    $("#installation_state").val($(".state", parent).html());
    $("#installation_zip").val($(".zip", parent).html());
}

function addressSubmit(event) {

    if(!$("#addressForm").validate().form())return false;
    address = $("#installation_address").attr('value');
    city = $("#installation_city").attr('value');
    state = $("#installation_state").attr('value');
    zip = $("#installation_zip").attr('value');
    business = $(this).val();
    wholeaddress = address + " " + city + ", " + state + " " + zip;
    if (geocoder) {
        geocoder.getLocations(
            wholeaddress,
            locationsFunction);
    }
    return false;
}

function locationsFunction(response){
    if (!response || response.Status.code != 200) {
        alert("Address not found");
    } else {
        places = response.Placemark;
        $.ajax({
            type: 'POST',
            url: '/shop/processPlaces',
            //dataType: 'json',
            data: {
                places: places,
                installation_address:address,
                installation_city:city,
                installation_state:state,
                installation_zip:zip,
                business:business
            },
            success: newsuccess
        });
        //printPlaces();
    }
    return false;
}

function newsuccess(code){

    //codes
    unsure = 1;
    notinarea = 2;
    good = 3;
    twozips = 4;


    if(code == unsure)text = "We are unable to confirm your address in our service area.  " +
                            "You can continue to shop and a customer service " +
                            "representative will call you to verify your address " +
                            "after you have completed your order.";
    else if(code == notinarea){
        $companytelephone = $("input[name='company_telephone']").val();
        text = "The address supplied is not in our service area.  " +
                        "You may double check the information you supplied " +
                        "or call us at " + $companytelephone + ".";
    }
    else if(code == twozips){

        $("body").append("<div id='chooseSubarea'></div>");

        var arr = {
            modal : true,
            autoOpen : false,
            width: '800px'
        };
        $("#chooseSubarea").dialog(arr);
        

        $('#chooseSubarea').load('/shop/zipChoices/' + zip, null, dialogReady);

        //location.replace('/shop/zipChoices/' + zip);
    }
    else if(code == good)text = null;
    else text = code;
    //else alert("Error... please try again later.");
	
    if(text)alert(text);
	
    if(code == 1 || code == 3)location.replace('/shop/restart');
    //if(code != 2)location.replace('/shop');
}

function dialogReady(){
    $("#chooseSubarea").dialog("open");
}

function formReturn(responseText){//3
    alert('formReturn' + responseText);
}
