// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// Alias Script File MODIFY THIS AT YOUR OWN RISK
//
//  Creation Date:  15 January 1997
//
//  Procedure Name:
//      associateWithKey
//
//  Description:
//		Procedure to associate a key with an array of strings. This
//		functionality is currently used to determine if menu items
//		need to be deleted and the menu rebuilt. The conditions are
//		that the key is unique, and the value items do not contain a
//		space character.
//
//      See buildPerspLookthruMenu as an example.
//
//  Input Arguments:
//      A unique key to associated with values.
//
//  Return Value:
//      True if the key and values are already stored, false is
//      returned if the key does not exist or the associated values
//      have changed.
//

global string $gAssociateKeys[];

global string $gAssociateValues[];

proc int associateFind( string $key )
{
    global string $gAssociateKeys[];

    int $found = false;

    int $count = size($gAssociateKeys);

    for ($i = 0; $i < $count; $i++) {
        if ($key == $gAssociateKeys[$i]) {
            $found = true;
            break;
        }
    }

    return $found ? $i : -1;
}

proc associateValueUpdate( int $index, string $value[] )
{
    global string $gAssociateValues[];

    int $count = size($value);

    for ($i = 0; $i < $count; $i++) {
        if ($i == 0) {
            $gAssociateValues[$index] = $value[$i];
        }
        else {
            $gAssociateValues[$index] =
                $gAssociateValues[$index]+" "+$value[$i];
        }
    }
}

proc int associateValueCompare( string $a1[], string $a2[] )
{
    int $result = true;

    int $count = size($a1);

    if ($count == size($a2)) {
        for ($i = 0; $i < $count; $i++) {
            if ($a1[$i] != $a2[$i]) {
                $result = false;
                break;
            }
        }
    }
    else {
        $result = false;
    }

    return $result;
}

global proc int associateWithKey( string $key, string $values[] )
{
    global string $gAssociateKeys[];
    global string $gAssociateValues[];

    int $match = true;

    int $index = associateFind( $key );

    if ($index == -1) {
        // Add value
        //

        int $count = size($gAssociateKeys);

        $gAssociateKeys[$count] = $key;

        associateValueUpdate( $count, $values );

        $match = false;
    }
    else {
        // Check if the values match
        //

        string $associateValues[];
        tokenize($gAssociateValues[$index]," ",$associateValues);

        if (!associateValueCompare($associateValues,$values)) {
            associateValueUpdate( $index, $values );

            $match = false;
        }
    }

    return $match;
}
