// ++=========================================================================++
// || DTO ShowCase v2.0.0 - 213
// || Copyright © 2008-2010 Drive Thru Online, Inc. All Rights Reserved.
// || This file may not be redistributed in whole or significant part, or
// || used on web site without licensing of the enclosed code,
// || and software features.
// || http://www.drivethruonline.com | info@drivethruonline.com
// || Downloaded 19:12, Fri Jul 16th 2010
// || 623790213_604952770317
// ++ ========================================================================++

function DTO_Image()                           {}

function DTO_Image_new(id, ownerID, showcaseID, displayName, url)
{
    var obj = new DTO_Image();
    DTO_Image_init(obj, id, ownerID, showcaseID, displayName, url);
    return obj;
}

function DTO_Image_init(obj, id, ownerID, showcaseID, displayName, url)
{
    obj.id = id;
    obj.ownerID = ownerID;
    obj.showcaseID = showcaseID;
    obj.displayName = displayName;
    obj.url = url;
}

/*--------------------------------------------------------------*/

function DTO_ClassInheritance()                           {}

function DTO_ClassInheritance_new(classID, superClassID, className)
{
    var obj = new DTO_ClassInheritance();
    DTO_ClassInheritance_init(obj, classID, superClassID, className);
    return obj;
}

function DTO_ClassInheritance_init(obj, classID, superClassID, className)
{
    obj.classID = classID;
    obj.superClassID = superClassID;
    obj.className = className;
    obj.attributes = [];
    
    obj.addAttribute = function(attr)           {this.attributes.push(attr);}
}

DTO_ClassInheritance_cache = {};
 
function DTO_ClassInheritance_buildLookup(array)  
{
    var count = array.length;
    for (var i=0; i<count; i++) 
    {
        var obj = array[i];   
        DTO_ClassInheritance_cache[obj.classID] = obj;
    }
}

function DTO_ClassInheritance_cachedWithID(id)                {return DTO_ClassInheritance_cache[id];}

/*--------------------------------------------------------------*/

function DTO_ClassAttribute()                           {}

function DTO_ClassAttribute_new(classID, attributeID, attributeType, displayName, 
                            displayOrder, description, isSearchable, decimalPlaces, displayOrder)
{
    var obj = new DTO_ClassAttribute();
    DTO_ClassAttribute_init(obj, classID, attributeID, attributeType, displayName, 
                            displayOrder, description, isSearchable, decimalPlaces, displayOrder);
    return obj;
}

function DTO_ClassAttribute_init(obj, classID, attributeID, attributeType, displayName, 
                            groupingID, description, isSearchable, decimalPlaces, displayOrder)
{    
    obj.classID = classID;
    obj.attributeID = attributeID;
    obj.attributeType = attributeType;
    obj.displayName = displayName;
    obj.groupingID = groupingID;
    obj.description = description;
    obj.isSearchable = isSearchable;
    obj.decimalPlaces = decimalPlaces;
    obj.displayOrder = displayOrder;
    obj.popupOption = [];
    
    obj.addPopupOption = function(popup)            {this.popupOption.push(popup);}
}

DTO_ClassAttribute_cache = {};
 
function DTO_ClassAttribute_buildLookup(array)  
{
    var count = array.length;
    for (var i=0; i<count; i++) 
    {
        var obj = array[i];   
        DTO_ClassAttribute_cache[obj.attributeID] = obj;
        var theClass = DTO_ClassInheritance_cachedWithID(obj.classID);
        theClass.addAttribute(obj);
    }
}

function DTO_ClassAttribute_cachedWithID(id)                {return DTO_ClassAttribute_cache[id];}

/*--------------------------------------------------------------*/

function DTO_AttributeEnum()                           {}

function DTO_AttributeEnum_new(classID, attributeID, enumID, display, description)
{
    var obj = new DTO_AttributeEnum();
    DTO_AttributeEnum_init(obj, classID, attributeID, enumID, display, description);
    return obj;
}

function DTO_AttributeEnum_init(obj, classID, attributeID, enumID, display, description)
{
    obj.classID = classID;
    obj.attributeID = attributeID;
    obj.enumID = enumID;
    obj.display = display;
    obj.description = description;
}

function DTO_AttributeEnum_buildLookup(array)  
{
    var count = array.length;
    for (var i=0; i<count; i++) 
    {
        var obj = array[i];   
        var attr = DTO_ClassAttribute_cachedWithID(obj.attributeID);
        attr.addPopupOption(obj);
    }
}

/*--------------------------------------------------------------*/

function DTO_SearchAttribute()                           {}

function DTO_SearchAttribute_new(attributeID, attributeType, displayName, groupingID, description, onUIChange)
{
    var obj = new DTO_SearchAttribute();
    DTO_SearchAttribute_init(obj, attributeID, attributeType, displayName, groupingID, description, onUIChange);
    return obj;
}

function DTO_SearchAttribute_init(obj, attributeID, attributeType, displayName, groupingID, description, onUIChange)
{    
    obj.attributeID = attributeID;
    obj.attributeType = attributeType;
    obj.displayName = displayName;
    obj.groupingID = groupingID;
    obj.description = description;
    obj.classAttributes = [];
    obj.popupOptions = [];
    obj.value = null;
    obj.valueHigh = null;
    obj.onUIChange = onUIChange;
    
    if(obj.attributeType == 12)         //popup
    {
        var nullOpt = DTO_SearchPopup_new(DTO_Phrase_cachedWithID('any').phrase);
        nullOpt.addAttrIDEnumID(-1, -1);
        obj.popupOptions.push(nullOpt);
    }    
    
    obj.addClassAttribute = function(attr)            
    {
        this.classAttributes.push(attr);
        if(this.attributeType == 12)      //popup
        {
            var i, count = attr.popupOption.length;
            for (var i=0; i<count; i++) 
            {
                var option = attr.popupOption[i];   
                var exists = DTO_SearchPopup_named(this.popupOptions, option.display);
                if(exists)
                    exists.addAttrIDEnumID(option.attributeID, option.enumID);
                else
                {
                    exists = DTO_SearchPopup_new(option.display);
                    exists.addAttrIDEnumID(option.attributeID, option.enumID);
                    this.popupOptions.push(exists);
                }
            }
        }
    }
    
    obj.sortByTitle = function()
    {
        if(this.popupOptions.length)
            this.popupOptions.sort(DTO_SearchPopup_sortByTitle);
    }
    
    obj.doPopup = function(value)                   {this.value = value; this.onUIChange(this);} 
    obj.doCheckbox = function(value)                {this.value = value; this.onUIChange(this);} 
    obj.doString = function(value)                  {this.value = value; this.onUIChange(this);} 

    obj.doIntRange = function(value)                
    {
        this.value = value.getLowValue(); 
        this.valueHigh = value.getHighValue(); 
        this.onUIChange(this);
    } 
    
    obj.doDecimalRange = function(value)                
    {
        this.value = value.getLowValue(); 
        this.valueHigh = value.getHighValue(); 
        this.onUIChange(this);
    } 
    
    obj.getValue = function()                       {return this.value;}
    obj.getValueHigh = function()                   {return this.valueHigh;}
    
    obj.setValue = function(newVal)                 
    {
        switch(this.attributeType)
        {
            case 10:        //int  
                this.value = (newVal ? newVal : 0);
                break;
            case 11:        //boolean
                if(newVal == 'yes' || newVal == 'no')
                    this.value = newVal;
                else
                    this.value = 'either';
                break;
            case 12:        //popup
                var exists = false;
                var i, count = (this.popupOption ? this.popupOption.length : 0);
                for (var i=0; i<count && !exists; i++) 
                {
                    var option = this.popupOption[i]; 
                    if(option.hasEnumID(newVal))
                        exists = true;
                }
                if(exists)
                    this.value = newVal;
                else
                    this.value = -1;
                break;
            case 14:        //string
                this.value = (newVal ? newVal : '');
                break;
            case 16:        //currency  
            case 17:        //decimal  
                this.value = (newVal ? newVal : 0);
                break;
        }
    }
    
    obj.setValueHigh = function(newVal)                 
    {
        switch(this.attributeType)
        {
            case 10:        //int  
            case 16:        //currency  
            case 17:        //decimal  
                this.valueHigh = (newVal ? newVal : 0);
                break;
        }
    }
    
    obj.emitSearchString = function()      
    {
        var stub = '';
        var i, count = this.classAttributes.length;
        var didOpt = false;
        for(i=0; i<count; i++)
        {
            var attr = this.classAttributes[i];
            if(this.value != null)
            { 
                if((this.attributeType == 11 && this.value != 'either')             //boolean
                || (this.attributeType == 12 && this.value > 0)                     //popup
                || (this.attributeType == 14 && this.value.length > 0)              //string
                || (this.attributeType == 15 && this.value.length > 0))             //text
                {
                    if(didOpt)
                        stub = stub + '&';
                    else
                        didOpt = true;
                    stub = stub + 'obj_0_attr_' + attr.attributeID + '=' + this.value;
                    //debug('emit ' + this.displayName + ' ' +this.attributeType + ' '+ attr.attributeID);
                }
                else if(this.attributeType == 10                                    //int 
                     || this.attributeType == 16                                    //currency
                     || this.attributeType == 17)                                   //decimal
                {
                    if(this.value && this.value.length > 0)
                    {
                        if(didOpt)
                            stub = stub + '&';
                        else
                            didOpt = true;
                        stub = stub + 'obj_0_attr_' + attr.attributeID + '_low=' + this.value;
                        //debug('emit ' + this.displayName + ' ' +this.attributeType + ' '+ attr.attributeID);
                    }    
                    if(this.valueHigh && this.valueHigh.length > 0)
                    {
                        if(didOpt)
                            stub = stub + '&';
                        else
                            didOpt = true;
                        stub = stub + 'obj_0_attr_' + attr.attributeID + '_high=' + this.valueHigh;
                        //debug('emit ' + this.displayName + ' ' +this.attributeType + ' '+ attr.attributeID);
                    }                
                }
            }        
        }
        return stub;
    }
}

function DTO_SearchAttribute_compress(attrs, onUIChange)
{
    var searches = [];
    var i, count = (attrs ? attrs.length : 0);
    var attrID = 1;
    
    for(i=0; i<count; i++)
    {
        var attr = attrs[i];
        if(attr.isSearchable)
        {
            var search = DTO_SearchAttribute_forClassAttr(searches, attr);
            if(search)
                search.addClassAttribute(attr);
            else
            {
                search = DTO_SearchAttribute_new(attrID, attr.attributeType, attr.displayName, 
                                                     attr.groupingID, attr.description, onUIChange);
                attrID = attrID + 1;
                search.addClassAttribute(attr);
                searches.push(search);
            }
        }    
    }
    
    /* disabled to test showcase's attr sort from fetch
       need to fix for vendor inventory
    searches.sort(DTO_SearchAttribute_sortByDisplay);
    count = searches.length;
    for(i=0; i<count; i++)
    {
        var search = searches[i];
        search.sortByTitle();
    }
    */
    return searches;
}

function DTO_SearchAttribute_uncompress(searches)
{   
    var stubAll = '';
    var i, count = searches.length;
    
    for(i=0; i<count; i++)
    {
        var search = searches[i];
        var stub = search.emitSearchString();
        if(stub.length)
        {
            if(stubAll.length)
                stubAll = stubAll + '&';
            stubAll = stubAll + stub;
        }
    }
    return stubAll;
}

function DTO_SearchAttribute_forClassAttr(searches, attr)
{
    var search = null;
    var i, count = searches.length;
    for(i=0; i<count && !search; i++)
    {
        var aSearch = searches[i];
        if(aSearch.groupingID == attr.groupingID
        && aSearch.attributeType == attr.attributeType
        && aSearch.displayName == attr.displayName)
            search = aSearch;
    }
    return search;
}

function DTO_SearchAttribute_sortByDisplay(a, b)
{
    var nameA = a.displayName;
    var nameB = b.displayName;
    if(nameA == nameB)
        return 0;
    else
        return (nameA > nameB ? 1 : -1);
}

/*--------------------------------------------------------------*/

function DTO_SearchPopup()                           {}

function DTO_SearchPopup_new(title)
{
    var obj = new DTO_SearchPopup();
    DTO_SearchPopup_init(obj,title);
    return obj;
}

function DTO_SearchPopup_init(obj, title)
{    
    obj.title = title;
    obj.attrEnums = [];

    obj.addAttrIDEnumID = function(attrID, enumID)
    {
        this.attrEnums.push(DTO_SearchPopupEnum_new(attrID, enumID));
    }    
    
    obj.hasEnumID = function(hasID)
    {
        var found = null;
        var i, count = this.attrEnums.length;
        for(i=0; i<count && !found; i++)
        {
            var enumID = this.attrEnums[i];
            if(enumID == hasID)
                found = option;
        }
        return found;
    }
}

function DTO_SearchPopup_named(popups, name)
{
    var found = null;
    var i, count = popups.length;
    for(i=0; i<count && !found; i++)
    {
        var option = popups[i];
        if(option.title == name)
            found = option;
    }
    return found;
}

function DTO_SearchPopup_sortByTitle(a, b)
{
    if(a.attrEnums[0].enumID == -1)
        return -1;
    else if(b.attrEnums[0].enumID == -1)
        return 1;
    else
    {
        var nameA = a.title;
        var nameB = b.title;
        if(nameA == nameB)
            return 0;
        else
            return (nameA > nameB ? 1 : -1);
    }
}

/*--------------------------------------------------------------*/

function DTO_SearchPopupFormatter()                            {}

function DTO_SearchPopupFormatter_new(array)
{
    var obj = new DTO_SearchPopupFormatter();
    DTO_SearchPopupFormatter_init(obj, array);
    return obj;
}

function DTO_SearchPopupFormatter_init(obj, array)
{
    obj.array = array;
    
    obj.count = function()              {return this.array.length;}
    obj.getID = function(i)             {return this.array[i].attrEnums[0].enumID;}
    obj.getTitle = function(i)          {return this.array[i].title;}
}

/*--------------------------------------------------------------*/

function DTO_SearchPopupEnum()                           {}

function DTO_SearchPopupEnum_new(attributeID, enumID)
{
    var obj = new DTO_SearchPopupEnum();
    DTO_SearchPopupEnum_init(obj, attributeID, enumID);
    return obj;
}

function DTO_SearchPopupEnum_init(obj, attributeID, enumID)
{    
    obj.attributeID = attributeID;
    obj.enumID = enumID;
}

/*--------------------------------------------------------------*/

function DTO_DisplayGrouping()                           {}

function DTO_DisplayGrouping_new(groupingID, displayOrder, groupingName)
{
    var obj = new DTO_DisplayGrouping();
    DTO_DisplayGrouping_init(obj, groupingID, displayOrder, groupingName);
    return obj;
}

function DTO_DisplayGrouping_init(obj, groupingID, displayOrder, groupingName)
{    
    obj.groupingID = groupingID;
    obj.displayOrder = displayOrder;
    obj.groupingName = groupingName;
}

DTO_DisplayGrouping_cache = {};
 
function DTO_DisplayGrouping_buildLookup(array)  
{
    var count = array.length;
    for (var i=0; i<count; i++) 
    {
        var obj = array[i];   
        DTO_DisplayGrouping_cache[obj.groupingID] = obj;
    }
}

function DTO_DisplayGrouping_cachedWithID(id)                {return DTO_DisplayGrouping_cache[id];}

/*--------------------------------------------------------------*/

function DTO_ObjectAttribute()                           {}

function DTO_ObjectAttribute_new(objectID, attributeID, value)
{
    var obj = new DTO_ObjectAttribute();
    DTO_ObjectAttribute_init(obj, objectID, attributeID, value);
    return obj;
}

function DTO_ObjectAttribute_init(obj, objectID, attributeID, value)
{    
    obj.objectID = objectID;
    obj.attributeID = attributeID;
    obj.value = value;
}


