//
//  stdlib.js
// 
//  Copyright (c) 1999-2000 Seann Herdejurgen (seann@mail.com)
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//
//  1. Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright
//     notice, this list of conditions and the following disclaimer in the
//     documentation and/or other materials provided with the distribution.
//  3. The name of the author may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
//  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
//  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
//  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
//  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
//  SUCH DAMAGE.
//
//  Standard Javascript library
//
function GetCookie(name) {
   var cookies=document.cookie.split('; ')
   if (document.cookie=='') cookies.length=0
   for (var i=0;i<cookies.length;i++) {
      if (cookies[i].indexOf(name+'=')==0) {
         var cookie=cookies[i].split('=')
         return unescape(cookie[1])
      }
   }
   return ''
}

function SetCookie(name, value, expires, path, domain, secure) {
   var argc = SetCookie.arguments.length
   document.cookie = name + '=' + escape(value)
                   + ((argc > 2) ? '; expires='+expires.toGMTString() : '')
                   + ((argc > 3) ? '; path='+path : '')
                   + ((argc > 4) ? '; domain='+domain : '')
                   + ((argc > 5) ? '; secure='+secure : '')
}

function nextyear() {
   var now = new Date()
   now.setTime(now.getTime() + (365 * 24 * 3600 * 1000))
   return now
}

function value(field) {
   switch (field.type) {
      case 'select-one':
           return field.options[field.selectedIndex].value
         break;
      case 'select-multiple':
           var a=new Array()
           for (var i=0; i<field.options.length; i++) {
              if (field.options[i].selected) a[a.length]=field.options[i].value
           }
           return a.join(',')
         break;
      case 'text':
      case 'textarea':
      case 'hidden':
      case 'button':
      case 'reset':
           return field.value
         break;
      default:
           alert('Unknown field type '+field.type)
         break;
   }
   return 'unknown'
}

function openwin(url,title,x,y) {
   return window.open(url,title,'toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width='+x+',height='+y+',screenX='+Math.round((screen.availWidth-x)/2)+',screenY='+Math.round((screen.availHeight-y)/2))
}

function replacepage(form,url) {
   var parms=new Array()
   for (var i=0; i<form.elements.length; i++) {
      if (form.elements[i].name.substring(0,1)!='_') {
         parms[parms.length]=form.elements[i].name+'='+escape(value(form.elements[i]))
      }
   }
   if (url) {
      location.replace(url+'&'+parms.join('&'))
   } else {
      location.replace(form.action+'?'+parms.join('&'))
   }
}

function savechanges(f,v) {
   var changes=document.forms[0].changes.value.split(',')
   if (document.forms[0].changes.value=='') changes.length=0
   for (var i=0;i<changes.length;i++) {
      if (changes[i].indexOf(f+'=')==0) {
         changes[i]=f+'='+v
         f=''
      }
   }
   if (f!='') changes[changes.length]=f+'='+v
   document.forms[0].changes.value=changes.join(',')
}

function trackchange(field) {
   savechanges(field.name,value(field))
}

function validate(form) {
   if (value(form.changes)=='') {
      alert('No changes have been made')
      return
   }
   replacepage(form)
}

function increment(field) {
   with (document.forms[0]) field=eval(field)
   field.value++
   trackchange(field)
   return false
}

function decrement(field) {
   with (document.forms[0]) field=eval(field)
   if (field.value>0) field.value--
   trackchange(field)
   return false
}

function move(source,dest) {
   for (var i=0;i<source.options.length;i++) {
      if (source.options[i].selected) {
         with (source.options[i])
            dest.options[dest.options.length]=new Option(text, value, false, false)
         savechanges(source.options[i].value,dest.name)
      }
   }
   for (i=source.options.length-1;i>=0;i--) {
      if (source.options[i].selected) {
         source.options[i]=null
      }
   }
}

function moveall(source,dest) {
   for (var i=0;i<source.options.length;i++) {
      with (source.options[i])
         dest.options[dest.options.length]=new Option(text, value, false, false)
      savechanges(source.options[i].value,dest.name)
   }
   source.options.length=0
}

function checklist(list,value) {
   for (var i=0;i<list.length;i++) {
      if (list[i]==value) return i
   }
   return -1
}

function addlist(list,value) {
   if (checklist(list,value)==-1) list[list.length]=value
   return list
}

function removelist(list,value) {
   var i=checklist(list,value)
   if (i!=-1) list=list.slice(0,i).concat(list.slice(i+1,list.length))
   return list
}
