//----------------------------------------------------------------------------
// StringBuffer Object -- used for doing string manipulation
//----------------------------------------------------------------------------
function StringBuffer() {
    this.__strings__ = new Array ;
} ;

StringBuffer.prototype.append = function (str) {
    this.__strings__.push(str);
} ;

StringBuffer.prototype.toString = function () {
    return this.__strings__.join("");
} ;

StringBuffer.prototype.length = function () {
    return this.__strings__.join("").length;
} ;

StringBuffer.prototype.assign = function (str) {
	for (var i=0; i < this.__strings__.length; i++) {
		this.__strings__.pop() ;
	}
    this.__strings__.push(str);
} ;

//---------------------------------------------------------------------------
// Output values
//---------------------------------------------------------------------------
function boolToString( value ) {
   var tBuffer = new StringBuffer ;
   switch( value ) {
      case true :
         tBuffer = "true" ;
         break ;
      case 1 :
         tBuffer = "true" ;
         break ;
      case "Y" :
         tBuffer = "true" ;
         break ;
      case "y" :
         tBuffer = "true" ;
         break ;
      case false :
         tBuffer = "false" ;
         break ;
      case 0 :
         tBuffer = "false" ;
         break ;
      case "N" :
         tBuffer = "false" ;
         break ;
      case "n" :
         tBuffer = "false" ;
         break ;
      default:
         tBuffer = "?" ;
   }
   return tBuffer.toString() ;
} ;

//----------------------------------------------------------------------------
// GeographyMapItem Object -- using dynamic prototype method for class construction
//
// Copywrite 2007 -- LJ Wine Products, Inc.
//----------------------------------------------------------------------------
function GeographyMapItem() {
    // Define member variables
    this.entryGUID = new StringBuffer ;
    this.country = new StringBuffer ;
    this.state = new StringBuffer ;
    this.region = new StringBuffer ;
    this.region_flag = new StringBuffer ;
    this.subregion = new StringBuffer ;
    this.subregion_flag = new StringBuffer ;
    this.appellation = new StringBuffer ;
    this.appellation_flag = new StringBuffer ;
    this.vineyard = new StringBuffer ;
    this.vineyard_flag = new StringBuffer ;
    this.county = new StringBuffer ;
    this.longitude = 0.0 ;
    this.latitude = 0.0 ;
    this.geocoding_method = 0 ;
    this.geo_note_map_guid = new StringBuffer ;
    this.map_attribute_guid = new StringBuffer ;
    this.map_source_guid = new StringBuffer ;
    this.map_type = new StringBuffer ;
    this.map_note_map_guid = new StringBuffer ;
    this.geospecificItem_guid = new StringBuffer ;
    this.weight_factor = 0.0 ;
    this.goodness = 0.0 ;
    if( typeof GeographyMapItem._initalized == "undefined") {
        // Define member functions for GeographyTypeLookup object
               
        GeographyMapItem._initialized = true ;
    } ;
} ;

//----------------------------------------------------------------------------
// DescriptorMapItem Object -- using dynamic prototype method for class construction
//
// Copywrite 2007 -- LJ Wine Products, Inc.
//----------------------------------------------------------------------------
function DescriptorMapItem() {
    // Define member variables
    this.Entry_GUID = new StringBuffer ;
    this.Descriptor = new StringBuffer ;
    this.Descriptor_type = new StringBuffer ;
    this.ImagePath = new StringBuffer ;
    this.Weighting = 0.0 ;
    this.Goodness = 0.0 ;
    this.Child_Weight = 0.0 ;
    this.child_Goodness = 0.0 ;
    this.child_Count = 1 ;
    this.Attribute_GUID = new StringBuffer ;
    this.Source_GUID = new StringBuffer ;
    this.MapType = new StringBuffer ;
    this.Note_Map_GUID = new StringBuffer ;
    this.Descriptor_GUID = new StringBuffer ;
    this.General_Weight_Factor = 0.0 ;
    this.General_Goodness = 0.0 ;
    
    if( typeof DescriptorMapItem._initalized == "undefined") {
        // Define member functions for GeographyTypeLookup object
               
        DescriptorMapItem._initialized = true ;
    } ;
} ;

//----------------------------------------------------------------------------
// GeographyTypeLookup Object -- using dynamic prototype method for class construction
//
// Copywrite 2007 -- LJ Wine Products, Inc.
//----------------------------------------------------------------------------
function GeographyTypeLookup() {
    // Define member variables
    this.Entry_GUID = new StringBuffer ;
    this.Country = new StringBuffer ;
    this.Region_flag = new StringBuffer ;
    this.Subregion_flag = new StringBuffer ;
    this.Appelation_flag = new StringBuffer ;
    this.Vineyard_flag = new StringBuffer ;
    this.Descriptor = new StringBuffer ;
    this.DescriptorAbbr = new StringBuffer ;
    
    if( typeof GeographyTypeLookup._initalized == "undefined") {
        // Define member functions for GeographyTypeLookup object
               
        GeographyTypeLookup._initialized = true ;
    } ;
} ;

//----------------------------------------------------------------------------
// SiteStats Object -- used for tracking site statistics
//----------------------------------------------------------------------------
function SiteStats() {
    // Define member variables
    this.totalProducers = 0 ;
    this.totalWines = 0 ;
    this.totalGrapes = 0 ;
    this.totalReviews = 0 ;
    this.totalUsers = 0 ;
    this.totalInfoSources = 0 ;
    
    if( typeof SiteStats._initalized == "undefined") {
        // Define member functions for SiteStats object
        
        SiteStats.prototype.loadStatsFromDB = function () {
        } ;
        
        SiteStats._initialized = true ;
    } ;
} ;

//---------------------------------------------------------------------------
// Descriptor Object -- to hold information about a descriptor
//---------------------------------------------------------------------------
function Descriptor() {
    // Define member variables
    this.Name = new StringBuffer ;
    this.Value = new StringBuffer ;
    this.Weighting = 0.0 ;
    
    if( typeof Descriptor._initalized == "undefined") {
        // Define member functions for Descriptor object
        
        Descriptor.prototype.loadDescriptorFromDB = function () {
        } ;
        
        Descriptor.prototype.saveDescriptorToDB = function () {
        }
        
        Descriptor._initialized = true ;
    } ;
} ;

//----------------------------------------------------------------------------
// UserProfile Object -- using dynamic prototype method for class construction
//
// Copywrite 2006, 2007 -- LJ Wine Products, Inc.
//----------------------------------------------------------------------------
function UserProfile() {
    // Define member variables
    this.AppearanceDescriptors = new Array ;
    this.AromaDescriptors = new Array ;
    this.TasteDescriptors = new Array ;
    this.QualtyDescriptors = new Array ;
    this.OtherDescriptors = new Array ;
    this.Geography = new Array ;
    
    if( typeof UserProfile._initalized == "undefined") {
        // Define member functions for UserProfile object
        
        UserProfile.prototype.loadUserProfile = function () {
        } ;
        
        UserProfile.prototype.saveUserProfile = function () {
        } ;
        
        UserProfile._initialized = true ;
    } ;
} ;

//---------------------------------------------------------------------
// User Object -- using dynamic prototype method for class construction
// 
//  Copywrite 2006,2007 -- LJ Wine Products, Inc.
//---------------------------------------------------------------------
function User() {
    // Define member variables
    this.userID = new StringBuffer ;
    this.login = new StringBuffer ;
    this.password = new StringBuffer ;
    this.firstName = new StringBuffer ;
    this.middleInitial = new StringBuffer ;
    this.lastName = new StringBuffer ;
    this.screenName = new StringBuffer ;
    this.primaryEmailAddress = new StringBuffer ;
    this.secondaryEmailAddress = new StringBuffer ;
    this.mailCompany = new StringBuffer ;
    this.mailAddr1 = new StringBuffer ;
    this.mailAddr2 = new StringBuffer ;
    this.mailCity = new StringBuffer ;
    this.mailZipcode = new StringBuffer ;
    this.mailState = new StringBuffer ;
    this.informationSrcGUID = new StringBuffer ;
    this.userGUID = new StringBuffer ;
    this.latitude = 0.0 ;
    this.longitude = 0.0 ;
    this.userProfile = new UserProfile ;
    this.loggedIn = false ;
    this.modeGuest = true ;
    this.modeEditor = false ;
    this.modeAdmin = false ;
    this.modeAffiliate = false ;
    this.premiumUser = false ;
    this.usageTermsFlag = false ;
    this.privacyPolicyFlag = false ;
    this.sendEmailFlag = false ;
    this.sendNewsletterFlag = false ;
    this.sendSuggestionFlag = false ;
    this.sendAffiliateFlag = false ;
    
    if (typeof User._initialized == "undefined") {
        // define member functions of User object
        User.prototype.LoginUser = function () {
            // validate user information with server database
        } ;
        
        User.prototype.SignInUser = function() {
            // User info has been valiadated
            this.loggedIn = true ;
            
            // Load the user profile from the server
            this.userProfile.loadUserProfile() ;
        } ;
        
        User.prototype.SignOutUser = function() {
            // User has signed off
            this.loggedIn = false ;
            this.modeAdmin = false ;
            this.premiumUser = false ;
            this.modeGuest = true ;
            this.modeEditor = false ;
            this.modeAffiliate = false ;
		    this.firstName.assign( "" ) ;
		    this.middleInitial.assign( "" ) ;
		    this.lastName.assign( "" );
		    this.screenName.assign ( "" );
		    this.primaryEmailAddress.assign( "" );
		    this.secondaryEmailAddress.assign( "" );
		    this.mailCompany.assign( "" ) ;
		    this.mailAddr1.assign( "" ) ;
		    this.mailAddr2.assign( "" ) ;
		    this.mailCity.assign( "" ) ;
		    this.mailZipcode.assign( "" ) ;
		    this.mailState.assign ("") ;
		    this.informationSrcGUID.assign ("") ;
		    this.userGUID.assign ("") ;
	    if( this.userProfile.AppearanceDescriptors.length > 0 ) {
		    this.userProfile.AppearanceDescriptors.slice(0,this.userProfile.AppearanceDescriptors.length) ;
	    }
	    if( this.userProfile.AromaDescriptors.length > 0 ) {
		    this.userProfile.AromaDescriptors.slice(0,this.userProfile.AromaDescriptors.length) ;
	    }
	    if( this.userProfile.TasteDescriptors > 0 ) {
		    this.userProfile.TasteDescriptors.slice(0,this.userProfile.TasteDescriptors.length) ;
	    }
	    if( this.userProfile.QualtyDescriptors > 0 ) {
		    this.userProfile.QualtyDescriptors.slice(0,this.userProfile.QualtyDescriptors.length) ;
	    }
	    if( this.userProfile.Geography.length > 0 ) {
		    this.userProfile.Geography.slice(0,this.userProfile.Geography.length) ;
	    }
            
    this.AppearanceDescriptors = new Array ;
    this.AromaDescriptors = new Array ;
    this.TasteDescriptors = new Array ;
    this.QualtyDescriptors = new Array ;
    this.OtherDescriptors = new Array ;
    this.Geography = new Array ;

            // reload the navigation frames to adjust for results of the login
            window.top.frames["user_nav"].location.reload( false );
            window.top.frames["main_nav"].location.reload( false );
        } ;

        User.prototype.SetEditor = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.modeEditor = value ;
                if( value == true ) {
                    this.modeGuest = false ;
                }
            }
        } ;
    
        User.prototype.SetAdmin = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.modeAdmin = value ;
                if( value == true ) {
                    this.modeGuest = false ;
                }
            }
        } ;

        User.prototype.SetAffiliate = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.modeAffiliate = value ;
                if( value == true ) {
                    this.modeGuest = false ;
                }
            }
        } ;

        User.prototype.SetPremiumUser = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.premiumUser = value ;
                if( value == true && this.loggedIn == false ) {
                    this.premiumUser = false ;
                }
            }
        } ;

        User.prototype.SetUsageTermsFlag = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.usageTermsFlag = value ;
                if( value == true && this.loggedIn == false ) {
                    this.usageTermsFlag = false ;
                }
            }
        } ;

        User.prototype.SetPrivacyPolicyFlag = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.privacyPolicyFlag = value ;
                if( value == true && this.loggedIn == false ) {
                    this.privacyPolicyFlag = false ;
                }
            }
        } ;

        User.prototype.SetSendEmailPreference = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.sendEmailFlag = value ;
                if( value == true && this.loggedIn == false ) {
                    this.sendEmailFlag = false ;
                }
            }
        } ;

        User.prototype.SetSendNewsletterPreference = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.sendNewsletterFlag = value ;
                if( value == true && this.loggedIn == false ) {
                    this.sendNewsletterFlag = false ;
                }
            }
        } ;

        User.prototype.SetSendSuggestionPreference = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.sendSuggestionFlag = value ;
                if( value == true && this.loggedIn == false ) {
                    this.sendSuggestionFlag = false ;
                }
            }
        } ;
        
        User.prototype.SetSendAffiliatePreference = function( value ) {
            // validate value -- should be boolean
            if( value == true || value == false ) {
                this.sendAffiliateFlag = value ;
                if( value == true && this.loggedIn == false ) {
                    this.sendAffiliateFlag = false ;
                }
            }
        } ;
        
        User._initialized = true ;
    } 
} ;

//------------------------------------------------------------------
//  Utility functions for window refreshing
//------------------------------------------------------------------
function doLoad()
{
    // the timeout value should be the same as in the "refresh" meta-tag
    setTimeout( "refresh()", 1000 );
}

function refresh()
{
    //  This version of the refresh function will be invoked
    //  for browsers that support JavaScript version 1.2
    //
    
    //  The argument to the location.reload function determines
    //  if the browser should retrieve the document from the
    //  web-server.  In our example all we need to do is cause
    //  the JavaScript block in the document body to be
    //  re-evaluated.  If we needed to pull the document from
    //  the web-server again (such as where the document contents
    //  change dynamically) we would pass the argument as 'true'.
    //  
    window.location.reload( false );
}

