// (c) SW-Tools 2000 ico/common.js

function wif(a)            // Testprint
{   if (typeof(wifw)=="undefined" || !wifw) wifw=window.open('','wifwin','scrollbars=yes,resizable=yes');
    w=wifw.document;
    w.writeln(a+"<br>");
}

function kok(navn,val)   // Cookie Read: x=kok("a"), Write: kok("a",x), Delete: kok("a",""), ReadFirst: x=kok()
{
    var r,i,koks,expir,na;
    r="";                                   // Returnvalue
    if (document.cookie!="")                // Read it
    {   koks=document.cookie.split(";");
        for (i=0; i<koks.length; i++)
        {   na=koks[i].split("=");
            while (na[0].substring(0,1)==" ") na[0]=na[0].substring(1); // _lastrap remove
            if (navn==null || navn==na[0])
            {   r=na[1].replace(/\,/g,";");                 // , --> ;
                r=r.replace(/\%3d/g,"=");                   // %3d -> =
                r=r.replace(/\%2c/g,",");                   // %2c -> ,
                r=r.replace(/\%0d/g,"\015");                  // lf -> %0d
                r=r.replace(/\%0a/g,"\012");                  // cr -> %0a
                break;
            }
        }
    }
    if (typeof(val)=="undefined" || val==null) return(r);  // Just getit

    expir=new Date; i=val=="" ? -1 : 1;          // Delete by pulling expiration down
    expir.setFullYear(expir.getFullYear()+i);    // New expires in one year
    val=val.replace(/\012/g,"%0a");              // cr -> %0a
    val=val.replace(/\015/g,"%0d");              // lf -> %0d
    val=val.replace(/\,/g,"%2c");                // , --> %2c
    val=val.replace(/\=/g,"%3d");                // = --> %3d
    val=val.replace(/\;/g,",");                  // ; --> ,

    if (document.domain) val+="; domain="+document.domain;            // All paths in this domain
    val+="; path="+"/";
    //val+="; secure="+"0";

    document.cookie=navn+"="+val+";expires="+expir.toGMTString();
    return(val);
} 

function put(nr,str,val,sep)    // csv_put(nr,str,val)
{
   var j,m,r,x;
   if (nr<=0) return("");
   if (typeof(str)=="undefined" || str==null) str="";
   if (typeof(val)=="undefined" || val==null) val=""; else val=""+val;
   if (typeof(sep)=="undefined" || sep==null) sep=";", val=val.replace(/\;/g,","); // ; --> ,
   val=val.replace(/\;/g,"%3b");                   // ; -> %3b
   for (x=str.split(sep),x[nr-1]=val,j=0,m=0,r=""; j<x.length; j++,r+=sep) 
       if (!(typeof(x[j])=="undefined" || x[j]==null || x[j]=="")) r+=""+x[j],m=r.length;
   return(r.substring(0,m));
}

function get(nr,str,val,sep)    // csv_get(nr,str,val)
{
   var x;
   if (nr<=0) return("");
   if (typeof(str)=="undefined" || str==null) str="";
   if (typeof(sep)=="undefined" || sep==null) sep=";";
   x=str.split(sep)
   if (--nr>=x.length || typeof(x[nr])=="undefined" || x[nr]==null) return("");
   x=x[nr].replace(/\%3b/g,";");                   // %3b -> ;
   return(x);
}

function kokload(felt)  // put/get all fields in a form
{
    var form,txt,i;

    if (typeof(felt.form)!="undefined")  // <body onLoad="kokload(document.myform.myfield);">
    {   form=felt.form, txt=kok(form.name);
        if (txt.length)
            for (i=0; i<form.length; i++)
                setvalue(form[i],get(i+1,txt));
        felt.focus();
        felt.select();
    } else                              // form... onsubmit="kokload(document.myform);" Put all for onLoad
    {   for (i=0,txt="",form=felt; i<form.length; i++)
            txt=put(i+1,txt,getvalue(form[i]));
        kok(form.name,txt);
    }
}

function setvalue(felt,val) // Set value of field
{
    var j,opt;

    switch (typnr(felt))
    { case 1: 
      case 2: felt.value  =val; break;
      case 3: felt.checked=val ? 1 : 0; break;
      case 4: felt.checked=val==felt.value; break;
      case 5: 
      case 6: for (j=0,opt=felt.options; j<opt.length; j++)
                  opt[j].selected =(oneof(opt[j].value,val) ? 1 : 0);
              break;
    }
}

function getvalue(felt) // Get value of field
{
    var val,j,opt;

    val="";
    switch (typnr(felt))
    { case 1: 
      case 2: val=felt.value;   break;
      case 3: val=felt.checked ? 1 : ""; break;
      case 4: val=felt.checked ? felt.value : ""; break;
      case 5: 
      case 6: for (j=0,opt=felt.options; j<opt.length; j++)
                  if (opt[j].selected && opt[j].value && opt[j].value!="undefined")
                       val+=(val ? "," : "")+opt[j].value;
              break;
    }
    return(val);
}

function typnr(felt)    // Get numeric fieldtype
{
  return(
    felt.type=="text"            ? 1  : 
    felt.type=="password"        ? 1  : 
    felt.type=="textarea"        ? 2  : 
    felt.type=="checkbox"        ? 3  : 
    felt.type=="radio"           ? 4  : 
    felt.type=="select-one"      ? 5  : 
    felt.type=="select-multiple" ? 6  : 
    felt.type=="button"          ? 7  :
    felt.type=="submit"          ? 8  : 
    felt.type=="reset"           ? 9  : 
    felt.type=="hidden"          ? 10 : 
    felt.type=="fileupload"      ? 11 : 0);
}

function oneof(val,values) // 7 in 1,2,3,7,9 ?
{
    var i,s;

    if (!val) val="0";
    if (val==values || (val=="0" && !values)) return(1);
    if (values)
        for (i=0,s=values.split(","); i<s.length; i++)
            if (val==s[i]) return(1);
    return(0);        
}

