<!--//
/*
    WSGC Library Application
    Copyright (C) 2008 Jez 'N' Di jez@jezndi.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
    USA
*/


function sprintf(num)
{
// 9 -> 09 etc, prefix with leading zero.
    if (num <= 9)
        return '0'+num;
    return num;
}


function add_to_select(tabname,surname,forename,newid,id)
{
    var extra='';
    if (id > 0 )
         { extra='_id'+sprintf(id);}
    else
         { extra='_id';}
    var fred=window.opener.document.getElementsByName(tabname+extra)[0];
// need to save the length before increasing the size - because size is one greater than the last element
// because the elements are numbered from zero, ie. elements, 0,1 = size 2. So using the length is a handy
// way to access the next unused element.
    var stringy=surname;
    switch (tabname)
    {
       case 'composer':
       case 'artist':
       case 'arranger':
       case 'author':
           stringy=surname.toUpperCase();
// no break - we always append the forename.
// I did try to pad the gap between surname and forename with spaces, as I do in PHP,
// but alas literal spaces get ignored, and &nbsp; and &#032; are exported literally. :-(
       default:
           stringy+=' '+forename;
           break;
    }
    fred.length=1;
    fred[0]=new Option(stringy,newid);
    fred.selectedIndex=0;
}


function pretty_string(stringy)
{
     var upper=stringy.value.toUpperCase();
     var len=upper.length;
     var res=' ';
     var outstring='';
     var ch='';
     for (ix=0; ix<len; ix++)
     {
         ch=upper.charAt(ix);
         if ((ch >= 'A')
         && (ch <='Z')
         && (res != ' ')
         && (res != '(')
         && (res != '-')
         && (res != '.')
         && (! ((res == "'") && (ch != 'S')) )
         && (! ((res == "U") && (ch == 'K')) )
         && (! ((res == "G") && (ch == 'B')) )
         && (! ((res == "P") && (upper.substr(ix).length > 1) && (upper.substr(ix,2) == 'O '))))
             res = ch.toLowerCase();
         else
              res = ch;
         outstring = outstring + res;
     }
     return outstring;
}

function validate(myForm) {
    if (myForm.title.value == "") {
        alert("Please enter the item's title");
        myForm.title.focus();
        return false;
    }
    if (myForm.type.value == " ") {
        alert("Please specify the item's type");
        myForm.type.focus();
        return false;
    }
    return true;
}


function querycheck(myForm) {
    if (myForm.what.value == "2" || myForm.what.value == "3")
    {
        if ((myForm.composer_id.value == "0")
           && (myForm.artist_id.value == "0")
           && (myForm.arranger_id.value == "0")
           && (myForm.description.value == "")
           && (myForm.genre_id.value == "0")
           && (myForm.publisher_id.value == "0")
           && (myForm.instrument_id.value == "0")
           && (myForm.location.value == ""))
        {
           alert("Please enter one or more of:\nDescription\nLocation\nArtist\nComposer\nArranger\nInstrument\nGenre\nPublisher");
           return false;
        }
    }
    if (myForm.what.value == "1")
    {
       if (myForm.type.value == " ")
       {
           alert("Please select a type");
           myForm.type.focus();
           return false;
        }
    }
    return true;
}



function deprecated_sortlist(listbox)
{
    var i = 0;
    var j = 0;
    var ix = 0;
    var k = 0;
// save the selected value index
    var selval=listbox.options[listbox.selectedIndex].value;
    arrayText = new Array();
    arrayVals = new Array();
    arrayOldText = new Array();
    var desc ='';
    var noop ='';

    for(i=0; i<listbox.length; i++)
    {
// don't want to sort special values 999999999 and zero - these are to remain at the
// top of the listbox after the sort, if they're present that is.
        switch (listbox.options[i].value)
        {
           case '999999999':
              desc = listbox.options[i].text;
              break;
           case '0':
              noop = listbox.options[i].text;
              break;
           default:
// otherwise push the text out to *2* arrays and the value to another
// one array gets sorted, the other is used to retrieve the value since it
// remains in the same order as the value array
              ix++;
              arrayText[ix] = listbox.options[i].text;
              arrayVals[ix] = listbox.options[i].value;
              arrayOldText[ix] = listbox.options[i].text;
              break;
        }
    }

    arrayText.sort();

// feedin the special 999999999 and zero values if we found them in the original listbox
    if (noop != '')
    {
        listbox.options[k].value = '0';
        listbox.options[k].text = noop;
        if (selval == '0') { listbox.selectedIndex=k;}
        k++;
    }
    if (desc != '')
    {
        listbox.options[k].value = '999999999';
        listbox.options[k].text = desc;
        if (selval == '999999999') { listbox.selectedIndex=k; }
        k++;
    }

// adjust number of itterations by no of special values inserted
    ix=listbox.length - k;
    for(i=0; i<ix; i++)
    {
// push text back in to listbox
        listbox.options[k].text = arrayText[i];
        for(j=0; j<listbox.length; j++)
        {
// itterate over old text array, still in original order - use the index
// to pop the value from the value array
            if (arrayText[i] == arrayOldText[j])
            {
// pop value
                listbox.options[k].value = arrayVals[j];
// does the value popped match the selected value prior to sort?
                if (selval == arrayVals[j])
                {
// Yes! so set the new selected index to match
                    listbox.selectedIndex=k;
                }
// bail out of itteration - we found it - assumes only one matching value which is fine in this case
                j = listbox.length;
            }
        }
        k++
    }
}


function deprecated_add_to_select_all(tabname,surname,forename,newid,callid)
{
// only called if tabname is composer, artist or arranger.
// Will be called when either the main item or one of the 20 subsidary ones are added
    var sel=0;
    if (callid == 0 ) // the main one, so we want to select it
    {
        sel = 1;
    }
// but we don't pass in any callid, because this is a request for the main item
    add_to_select(tabname,surname,forename,newid,0,sel);

    var i=0;
    for (i=1; i<=20; i++)
    {
// callid matches the loop variable, so this is the 1/20 to mark as selected.
// we don't want all 20 to become selected - only to push the new value into the possible
// values in sorted order.
        if (i == callid)
            {sel=1;}
        else
            {sel=0;}
        add_to_select(tabname,surname,forename,newid,i,sel);
    }
}


function depracated_add_to_select(tabname,surname,forename,newid,id,sel)
{
    var extra='';
    if (id > 0 )
         { extra='_id'+sprintf(id);}
    else
         { extra='_id';}
    var fred=window.opener.document.getElementsByName(tabname+extra)[0];
// need to save the length before increasing the size - because size is one greater than the last element
// because the elements are numbered from zero, ie. elements, 0,1 = size 2. So using the length is a handy
// way to access the next unused element.
    var len=fred.length;
    var stringy=surname;
    switch (tabname)
    {
       case 'composer':
       case 'artist':
       case 'arranger':
       case 'author':
           stringy=surname.toUpperCase();
// no break - we always append the forename.
// I did try to pad the gap between surname and forename with spaces, as I do in PHP,
// but alas literal spaces get ignored, and &nbsp; and &#032; are exported literally. :-(
       default:
           stringy+=' '+forename;
           break;
    }
// add the new option the listbox
    fred[fred.length]=new Option(stringy,newid);
    if ( sel == 1 )
    {
// set the value added to be the selected one prior to calling sortlist -
// because sortlist finds the current selected and preserves it through the sort.
        fred.selectedIndex=len;
    }
    sortlist(fred);
}


//-->
