// (c) 2007-11 HMD Change Technology LLC
var domain = 'http://walzguitar.com/';
function getValue( stub, tag ) {
    if( stub.indexOf( tag ) > -1 ) {
        return stub.substring( 
            stub.indexOf( "<" + tag + ">" ) + tag.length + 2, 
            stub.indexOf( "</" + tag + ">" )
            );
    } else {return "";}
}
function code( key, offset, stub, way ) {
    var r = '';
    stub = stub.toString( );
    if( way == 'in' ) {
        offset = parseInt( offset );
        for( var i = 0; i < stub.length; i++ ) {
            r += key.indexOf( stub.charAt( i ) ) + offset;
        }
    } else {
        for( var i = 0; i < stub.length; i+=offset.length ) {
            r += key.charAt( stub.substring( i, i + offset.length ) - parseInt( offset ) );
        }
    }
    return r;
}
function getRankHTML( studentGoalCount, teacherGoalCount) {
    // 10 scale (15 30) (43 55) (65 75) (83 90)
    var r = parseInt( studentGoalCount ) / parseInt( teacherGoalCount ) * 100;
    var r2 = parseInt( studentGoalCount ) / parseInt( teacherGoalCount ) * 100;
    var close = '', s = ' style="text-align:center;font-size:smaller;"';
    if( studentGoalCount < 2 ) {r = 1;}
    var h = '<div><img src="' + domain + 'resources/images/';
    if( r < 16 ) {h += 'rank01.png" alt="Beginner" title="Beginner"';}
    else if( r < 31 ) {
        h += 'rank01a.png" alt="Beginner" title="Beginner"';
        if( r2 > 30 ) {close = '<div' + s + '>One skill away from next rank.</div>';}
    } else if( r < 44 ) {h += 'rank02.png"  alt="Advanced beginner" title="Advanced beginner"';}
    else if( r < 56 ) {
        h += 'rank02a.png"  alt="Advanced beginner" title="Advanced beginner"';
        if( r2 > 55 ) {close = '<div' + s + '>One skill away from next rank.</div>';}
    }
    else if( r < 66 ) {h += 'rank03.png"  alt="Intermediate" title="Intermediate"';}
    else if( r < 76 ) {
        h += 'rank03a.png"  alt="Intermediate" title="Intermediate"';
        if( r2 > 75 ) {close = '<div' + s + '>One skill away from next rank.</div>';}
    }
    else if( r < 84 ) {h += 'rank04.png"  alt="Advanced intermediate" title="Advanced intermediate"';}
    else if( r < 91 ) {
        h += 'rank04a.png"  alt="Advanced intermediate" title="Advanced intermediate"';
        if( r2 > 90 ) {close = '<div' + s + '>One skill away from last rank.</div>';}
    }
    else {h += 'rank05.png"  alt="expert" title="Expert"';}
    h += ' border="0" /></div><div class="c">(' + studentGoalCount + '/' + teacherGoalCount + ')</div>';
    return h + close;
}
function getObjCount( otext ) {
    var c = 0;
    otext = otext.split( /\n/ );
    for( var i = 0; i < otext.length; i++ ) {
        if( otext[ i ].length > 5 ) {c++;}
    }
    return c;
}
function dbCharReplace( s ) {
    s = s.replace( /'/g, '' );
    return s;
}
function getWindowWidth( ) {
    var w = 600;
    if( document.body && document.body.offsetWidth ) {
        w = document.body.offsetWidth;
    }
    if( document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) {
        w = document.documentElement.offsetWidth;
    }
    if( window.innerWidth && window.innerHeight ) {
        w = window.innerWidth;
    }
    return w;
}
function getVariableSelect( list, elementID, first ) {
    // list separated by ~, value text separated by :
    list = list.split( "~" );
    list = list.sort( );
    var h = '<select id="' + elementID + '"><option value="0"> - ' + first + ' - </option>';
    var s = '';
    for(var i = 0; i < list.length; i++ ) {
        if( list[ i ] ) {
            s = list[ i ].split( ':' );
            h += '<option value="' + s[ 0 ] + '">' + s[ 1 ] + '</option>';
        }
    }
    return h + '</select>';
}
function setSelectValue( id, selectedValOrIndex ) {
    var sel = document.getElementById( id );
    for( var i = 0; i < sel.options.length; i++ ) {
        if( sel.options[ i ].text == selectedValOrIndex || i == selectedValOrIndex ) {
            sel.selectedIndex = i;
        }
    }
}
function getFormattedRecents( r ) {
    r = r.split( '\n' );
    var h = '', g = '';
    for( var i = 0; i < r.length - 1; i++ ) {//length - 1 for ending line
        g = getValue( r[ i ], 'gift' );
        if( getValue( g, 'type' ) == 'teacher' ) {
            h += '<div style="background-color:#ccffff;">';
        } else {
            h += '<div>';
        }
        h += '<img src="' + getValue( g, 'imagesrc' ) + 
            '" style="border:0px;height:24px;width:24px;" /> ' +
            ' ' + getValue( r[ i ], 'to' ) + ' received <i>' + 
            getValue( g, 'name' ) + '</i> from ' + getValue( r[ i ], 'from' ) + 
            ' on ' + getValue( r[ i ], 'date' ) + '</div>';
    }
    return h;
}
function formatShelf( s ) {
    var h = '';
    s = s.replace( /<\/gift>/g, '~' );
    s = s.split( '~' );
    var f = '';
    var fbStyle = 'style="background-color:gold;border:0px;"';
    var fbStatus = 'no';
    if( document.getElementById( 'fbstatus' ) ) {
        fbStatus = document.getElementById( 'fbstatus' ).value;
    }
    for( var i = 0; i < s.length - 1; i++ ) {//len-1 for ending
        if( fbStatus == 'yes' && getValue( s[ i ], 'new' ).length > 0 ) {
            f = getValue( s[ i ], 'from' ).split( ':' )[ 0 ];
            h += '<span>' +
                '<a href="#" onclick="fbPost( \'' + getValue( s[ i ], 'new' ) + '\', \'' + getValue( s[ i ], 'ImageSrc' ) + '\' );"><img ' + fbStyle + ' src="' + domain + '/resources/trophies/post_facebook.png" /></a>' +
                '<img src="' + getValue( s[ i ], 'ImageSrc' ) + '" ' + fbStyle + ' title="From: ' + f + ' on ' + getValue( s[ i ], 'date' ) + '" />' +
                '<a href="#" onclick="fbPost( \'' + getValue( s[ i ], 'new' ) + '\' );"><img ' + fbStyle + ' src="' + domain + '/resources/trophies/post_ignore.png" /></a>' +
                '</span>&nbsp;';
        } else {
            f = getValue( s[ i ], 'from' ).split( ':' )[ 0 ];
            h += '<img src="' + getValue( s[ i ], 'ImageSrc' ) + '" style="border:0px;" title="From: ' + f + ' on ' + getValue( s[ i ], 'date' ) + '" />&nbsp;';
        }
    }
    return h;
}
function formatMiniShelf( s ) {
    var h = '';
    s = s.replace( /<\/gift>/g, '~' );
    s = s.split( '~' );
    var f = '';
    for( var i = 0; i < s.length - 1; i++ ) {//len-1 for ending
        f = getValue( s[ i ], 'from' ).split( ':' )[ 0 ];
        h += '<img src="' + getValue( s[ i ], 'ImageSrc' ) + '" style="border:0px;height:33px;width:33px;" title="From: ' + f + '" />&nbsp;';
    }
    return h;
}
function getTrophyGallery( t ) {//id:name:type:src
    var h = '';
    t = t.split( "~" );
    t = t.sort( );
    var s = '';
    for(var i = 0; i < t.length; i++ ) {
        if( t[ i ] ) {
            s = t[ i ].split( ':' );
            h += '<img src="http:' + s[ 4 ] + '" onclick="trophyClick(\'' + s[ 0 ] + '\', \'' + s[ 4 ] + '\');" /> ';
        }
    }
    return h;
}
function trophyClick( tid, src ) {
    document.getElementById( 'tlist' ).value = tid;
    document.getElementById( 'chosent' ).innerHTML = '<img src="http:' + src + '" />';
}
function toggleShowHide( id ) {
    if( id == 'show' ) {
        document.getElementById( 'show' ).style.display = 'none';
        document.getElementById( 'hide' ).style.display = 'inline';
    } else {
        document.getElementById( 'hide' ).style.display = 'none';
        document.getElementById( 'show' ).style.display = 'inline';
    }
}
