﻿	$(document).ready(function() {
		SetRestaurantName();
        $("select[id$='ddlFromState']").change(function() {
            var country = $("select[id$='ddlFromState'] option:selected")[0].title;
            $("input[id$='txtFromCountry']")[0].value = country;
        });
        $("select[id$='ddlFromStreetConflict']").change(function() {
            var optionValue = $("select[id$='ddlFromStreetConflict'] option:selected")[0].value;
            var addressParts = optionValue.split('|');
            var city = addressParts[0];
            var state = addressParts[4];
            var zipCode = addressParts[5];
            $("input[id$='txtFromCity']")[0].value = city;
            $("input[id$='txtFromZipCode']")[0].value = zipCode;
            var dFromState = $("select[id$='ddlFromState']")[0];
            for (var i = 0; i < dFromState.length; i++) {
                if (dFromState[i].value == state) {
                    dFromState.selectedIndex = i;
                    break;
                }
            }
        });
        
        Sys.Application.add_load(RegisterPageEvents);
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(OnUpdating);
        //Added an ajax end request event handler to set the page title back to original
    	//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() { document.title = 'Location Landing'; OnUpdated(); });
    });

    
    function ShowDrivingDirctionsWindow() {
        var mapWindow = window.open("driving-directions-popup.aspx", null, "height=700,width=770,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,");
		
		if(!mapWindow) {
			$('.contentwithmap').prepend('<a href="/driving-directions-popup.aspx" target="_blank">Popup blocked.  Click here to open.</a>');
			alert("Popup blocker detected.  Click link at the top of Driving Directions section to open.");
			
		}
		else {
			mapWindow.moveTo(50, 50);
		}
		
        return false;
    }
    
    function DisableSelect()
    {
    	$("select").each(function() {
			$(this).attr("disabled", "true");	
		});
    }
    
    function EnableSelect()
    {
    	$("select").each(function() {
			$(this).attr("disabled", "");	
		});
    }


    function PopulateStartingLocation(street, city, state, zipCode) {
        $("input[id$='txtFromStreet']")[0].value = street;
        $("input[id$='txtFromCity']")[0].value = city;
        var dFromState = $("select[id$='ddlFromState']")[0];
        for (var i = 0; i < dFromState.length; i++) {
            if (dFromState[i].value == state) {
                dFromState.selectedIndex = i;
                break;
            }
        }
        $("input[id$='txtFromZipCode']")[0].value = zipCode;
    }
    
    function ShowDrivingDirections() 
    {
    	var btnDrivingDirections = jQuery("input[id$='btnDrivingDirections']").attr("name");
	    __doPostBack(btnDrivingDirections,"");
    }
    
    function SetRestaurantName()
	{
		//$("td[id$='tdRestaurantName']")[0].innerHTML = $("input[id$='hdnRestaurantName']").val();
	}
    
    function RegisterPageEvents()
    {
    	$("select[id$='ddlFromState']").change(function() {
            var country = $("select[id$='ddlFromState'] option:selected")[0].title;
            $("input[id$='txtFromCountry']")[0].value = country;
        });
        $("select[id$='ddlFromStreetConflict']").change(function() {
            var optionValue = $("select[id$='ddlFromStreetConflict'] option:selected")[0].value;
            var addressParts = optionValue.split('|');
            var city = addressParts[0];
            var state = addressParts[4];
            var zipCode = addressParts[5];
            $("input[id$='txtFromCity']")[0].value = city;
            $("input[id$='txtFromZipCode']")[0].value = zipCode;
            var dFromState = $("select[id$='ddlFromState']")[0];
            for (var i = 0; i < dFromState.length; i++) {
                if (dFromState[i].value == state) {
                    dFromState.selectedIndex = i;
                    break;
                }
            }
        });
    }
    
    function OnUpdating(sender,args){
	   	//get the update progress div
	    var updateProgressDiv = $get('updateProgressDiv'); 
		var updateProgressUploader = $get('updateProgressLoader');
		
	   	//get the update panel
		var index  = sender._postBackSettings.panelsToUpdate[0].lastIndexOf('$') + 1;
		var updatePanelName = sender._postBackSettings.panelsToUpdate[0].substring(index, sender._postBackSettings.panelsToUpdate[0].length);
		var objUpdatePanel = $get(jQuery("div[id$= " + updatePanelName + "]")[0].id);
	 	DisableSelect();
	    //make the update progress div visible
	    updateProgressDiv.style.display = '';	    
	    
	    //get the bounds of both the update panel and the progress div
	    var updatePanelBounds = Sys.UI.DomElement.getBounds(objUpdatePanel);
	    var updateProgressDivBounds = Sys.UI.DomElement.getBounds(updateProgressDiv);
	
	    updateProgressDiv.style.width = (updatePanelBounds.width - 4) + "px";
		updateProgressDiv.style.height = (updatePanelBounds.height - 5) + "px";
		updateProgressUploader.style.marginTop = Math.round(updatePanelBounds.height / 2) + "px";
		updateProgressUploader.style.marginLeft = Math.round(updatePanelBounds.width / 2) + "px";
			
	    //set the progress element to this position
	    Sys.UI.DomElement.setLocation (updateProgressDiv, updatePanelBounds.x + 4, updatePanelBounds.y );           
	}
	
	function OnUpdated() {
	    // get the update progress div
	    var updateProgressDiv = $get('updateProgressDiv'); 
	    // make it invisible
	    updateProgressDiv.style.display = 'none';
	    EnableSelect();
	}
	
	function SaveCookie(name, value)
	{
		//Set the Restaurant ID cookie from the parameters passed
	    jQuery.cookie(name, value, { expires: 365 });
	    jQuery.cookie("Online_Ordering", "true", { expires: 365 });
	    jQuery.cookie("International", "No", { expires: 365 });
	}

