﻿function RegisterNamespace(nsToRegister)
{
    var root = window;
    nsToRegister.split(".").forEach(function(ns){
        if (root[ns] == null)
        {
            root[ns] = {};
        }
        root = root[ns];
    });
}
function setCookie(name, value, expires) 
{ 
    document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString()); 
} 

function getCookie (c_name) { 
   if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";

}
function delCookie(name) 
{ 
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/"; 
} 
function SetSelectedItem(object, value)
{
    //alert(object);
    var children = object.getChildren();
    children.each(function(item)
    {
        //alert(item.get('html') + ' , Value: ' + item.get('value'));
        //alert('Value Sent: ' + value);
        //alert('Current Value: ' + item.get('value'));
        if(item.get('value') == value)
        {
            //alert('DropDown Value: ' + item.get('value'));
            item.set('selected', 'true');
        }
    });
}
function ExpandContainer(footerTopMargin, mainContainerHeight)
{
    var _mainContainer = $('main_container');
    var _mainContent = $('main_content');
    var _searchResults = $('detail_search');
    var _searchResultsInner = $('searchResults');
    var _footer = $('footer_container');
    //var _footerBottomBorder = $('pcv_bottom_border');
    
    //var fxExpandMainContent = new Fx.Tween(_mainContent, {duration: 1000});
    //var fxExpandMainContainer = new Fx.Tween(_mainContainer, {duration: 1000});
    var fxExpandResults = new Fx.Tween(_searchResults, {duration: 1000});
    
    //var fxMoveBottomBorder = new Fx.Tween(_footerBottomBorder, {duration: 1000});
    var fxMoveFooter = new Fx.Tween(_footer, {duration: 1000});
    //var fxSwitchBackground = new Fx.Morph(_mainContent,{duration:0, transition: Fx.Transitions.Sine.easeOut});
        
    fxMoveFooter.start('margin-top',footerTopMargin);
    //fxMoveBottomBorder.start('margin-top','0');
    if(_searchResultsInner != null)
    {
        var fxExpandInnerResults = new Fx.Tween(_searchResultsInner, {duration: 1000});
        fxExpandInnerResults.start('height', mainContainerHeight);
    }
    fxExpandResults.start('height', mainContainerHeight);
    //fxExpandMainContent.start('height','1250');
    //fxSwitchBackground.start({
    //'background-image':'url(app_themes/main/images/main_content_tall.png)',
    //'background-repeat':'no-repeat'
    //});
}

function CreateStars(_ratingControl, _location)
{
     _ratingControl.set('value',"0");
    for(i=1;i<=5;i++)
    {
        var _emptyStar = new Element('img',{'id':'star_' + _location + i,'src':'app_themes/main/images/EmptyStar.png','class':'rating_control'});
        _emptyStar.inject(_ratingControl);
        _emptyStar.addEvent('click',function()
        {
            var _rating = this.id.substr(this.id.length - 1,1);
            var _diff = 5 - _rating;
            //alert(_rating);
            for(count=1;count<=5;count++)
            {
                var _filledStar = $('star_' + _location + count);
                if(count <= _rating)
                {
                _filledStar.set('src','app_themes/main/images/FilledStar.png');
                }
                else 
                {
                _filledStar.set('src','app_themes/main/images/EmptyStar.png');
                }
            }
            _ratingControl.set('value',_rating);
        });
    }
}
function ConvertSymbols(value)
{
    if(value != null)
    {
        //alert(value);
    
        var newValue = value.replace(/%3A/g,':');
        newValue = newValue.replace(/%2F/g,'/');
        newValue = newValue.replace(/%40/g,'@');
        newValue = newValue.replace(/%20/g,' ');
        newValue = newValue.replace(/%3C/g,'<');
        newValue = newValue.replace(/%3E/g,'>');
        newValue = newValue.replace(/%23/g,'#');
        newValue = newValue.replace(/%2C/g,',');
        newValue = newValue.replace(/%2B/g,'+');
        newValue = newValue.replace(/%26/g,'&');
        newValue = newValue.replace(/%24/g,'$');
        newValue = newValue.replace(/%3B/g,';');
        newValue = newValue.replace(/%3D/g,'=');
        newValue = newValue.replace(/%3F/g,'?');
        newValue = newValue.replace(/%7B/g,'{');
        newValue = newValue.replace(/%7D/g,'}');
        newValue = newValue.replace(/%7C/g,'|');
        newValue = newValue.replace(/%5E/g,'^');
        newValue = newValue.replace(/%7E/g,'~');
        newValue = newValue.replace(/%5B/g,'[');
        newValue = newValue.replace(/%5D/g,']');
        newValue = newValue.replace(/%60/g,'`');
        newValue = newValue.replace(/%22/g,'"');
        
        return newValue;
    }
}
function ResetRatingStars(_location)
{
    for(i=1;i<=5;i++)
    {
        var _emptyStar = $('star_' + _location + i)
        _emptyStar.set('src','app_themes/main/images/EmptyStar.png');
    }
}
function GoHome()
{
    window.location = "http://www.KupOfJoe.com";
}
function NavigateTo(page)
{
    window.location.href = page + '.aspx';
}
function PopAdminSearchStates(text)
{
    this.response.json = JSON.decode(text, this.options.secure);
    var obj = eval(text);
    var _dropDownListStates = $('adminSearchStates');
    _dropDownListStates.addEvent('change',function()
    {
        KupOfJoe.MetaData.GetStateCities(this.get('value'),PopAdminSearchCities);
    });
    obj.Result.each(function(state)
    {
        var _option = new Element('option');
        _option.set('value',state.StateID);
        _option.set('text',state.StateName);
        _option.inject(_dropDownListStates);
    });
}
function PopAdminSearchCities(text)
{
    this.response.json = JSON.decode(text, this.options.secure);
    var obj = eval(text);
    var _dropDownListCities = $('adminSearchCities');
    _dropDownListCities.addEvent('change',function()
    {
        //Going to get Shops within this state and generate an alphabet list based on
        //the cities in which those shops are located
        KupOfJoe.ListingsService.GetListingsByCity(this.get('value'),0,0,PopAdminSearchShops);
    });
    _dropDownListCities.empty();
    var _option = new Element('option');
    _option.set('value','0');
    _option.set('text','[Select a City]');
    _option.inject(_dropDownListCities);
    obj.Result.each(function(city)
    {
        var _option = new Element('option');
        _option.set('value',city.CityID);
        _option.set('text',decodeURI(city.Name));
        _option.inject(_dropDownListCities);
    });
}
function PopAdminSearchShops(text)
{
    this.response.json = JSON.decode(text, this.options.secure);
    var obj = eval(text);
    var _dropDownListShops = $('adminSearchShops');
    _dropDownListShops.empty();
    var _option = new Element('option');
    _option.set('value','0');
    _option.set('text','[Select a Shop]');
    _option.inject(_dropDownListShops);
    obj.Result.each(function(shop)
    {
        var _option = new Element('option');
        _option.set('value',shop.ID);
        _option.set('text',decodeURI(shop.Name));
        _option.inject(_dropDownListShops);
    });
}
function PopAdminPhotoStates(text)
{
    this.response.json = JSON.decode(text, this.options.secure);
    var obj = eval(text);
    var _dropDownListStates = $('photos_states');
    _dropDownListStates.addEvent('change',function()
    {
        KupOfJoe.MetaData.GetStateCities(this.get('value'),PopAdminPhotoCities);
    });
    obj.Result.each(function(state)
    {
        var _option = new Element('option');
        _option.set('value',state.StateID);
        _option.set('text',state.StateName);
        _option.inject(_dropDownListStates);
    });
}
function PopAdminPhotoCities(text)
{
    this.response.json = JSON.decode(text, this.options.secure);
    var obj = eval(text);
    var _dropDownListCities = $('photos_city');
    _dropDownListCities.addEvent('change',function()
    {
        //Going to get Shops within this state and generate an alphabet list based on
        //the cities in which those shops are located
        KupOfJoe.ListingsService.GetListingsByCity(this.get('value'),0,0,PopAdminPhotoShops);
    });
    _dropDownListCities.empty();
    var _option = new Element('option');
    _option.set('value','0');
    _option.set('text','[Select a City]');
    _option.inject(_dropDownListCities);
    obj.Result.each(function(city)
    {
        var _option = new Element('option');
        _option.set('value',city.CityID);
        _option.set('text',decodeURI(city.Name));
        _option.inject(_dropDownListCities);
    });
}
function PopAdminPhotoShops(text)
{
    this.response.json = JSON.decode(text, this.options.secure);
    var obj = eval(text);
    var _dropDownListShops = $('photos_shops');
    _dropDownListShops.addEvent('change',function()
    {
        GetShopPhotos();
        $('selected_shop').set('value',this.get('value'));
    });
    _dropDownListShops.empty();
    var _option = new Element('option');
    _option.set('value','0');
    _option.set('text','[Select a Shop]');
    _option.inject(_dropDownListShops);
    obj.Result.each(function(shop)
    {
        var _option = new Element('option');
        _option.set('value',shop.ID);
        _option.set('text',decodeURI(ConvertSymbols(shop.Name)));
        _option.inject(_dropDownListShops);
    });
}
function ShowListingsScreen()
{
    $('home_page').setStyle('display','none');
    $('account_details').setStyle('display','none');
    $('events_details').setStyle('display','none');
    $('register_details').setStyle('display','none');
    $('coffee_details').setStyle('display','block');
    
    $('add_shop_controls').setStyle('display','none');
    $('add_review_controls').setStyle('display','none');
    $('review_controls').setStyle('display','none');
    $('search_controls').setStyle('display','block');
}
function ShowAddCoffeeShopScreen()
{
    $('home_page').setStyle('display','none');
    $('account_details').setStyle('display','none');
    $('events_details').setStyle('display','none');
    $('register_details').setStyle('display','none');
    $('coffee_details').setStyle('display','block');
    
    $('add_shop_controls').setStyle('display','block');
    $('add_review_controls').setStyle('display','none');
    $('review_controls').setStyle('display','none');
    $('search_controls').setStyle('display','none');
}
function ShowReviewsScreen()
{
    ShowListingsScreen();
    $('search_controls').setStyle('display','none');
    $('add_shop_controls').setStyle('display','none');
    $('add_review_controls').setStyle('display','none');
    $('review_controls').setStyle('display','block');
}
function querySt(ji) 
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == ji) 
        {
            return ft[1];
        }
    }
}