var saveList = new Array( ); //Global variable to store check state
var saveTable = new Array( ); //Global variable to store check state


function doSubmitExport(subform) {
var result ="";
for (var i = 0; i < saveList.length; i++) 
   {
     if (result!="")
     {
       result = result+",";
     }
    result=result+saveList[i];
   }  
      subform["export"].value=result;
   subform.submit();   
    clearAll();
}



function clearAll() {
try {
for (var i=0;i < document.exportform.docIDs.length;i++)

        {
                var e = document.exportform.docIDs[i];
                e.checked=false;
                
        }
  } catch(e) {
  
    }
for (var i = 0; i < saveList.length; i++) {
   try {
    document.exportform[saveList[i]].checked=false;
    } 
    catch(e) {
    //Do nothing because its not on this page
    }
   }  
   
 saveList = new Array();
 saveTable=new Array();

 deleteCookie("exportData");
}

function doCheckBox(ctlName,ctlValue) {


  //saveList[ctlName]=ctlValue;
  //Find location of the item in the array
  var location = saveTable[ctlName];
  if (ctlValue)
  {
   //true

    if (location==null)
    {
    //Append value to array
    saveList[saveList.length] = ctlName;

     computePositions();

    }   
  } 
  else
  {
   //false
//   alert("rmove");
    if (location!=null)
    {
  //       alert("removing");
     var removed = saveList.splice(location,1);
    // alert("removed");
     computePositions();
    }
  }
  //If unset then remove from array
  //If set then append to the end of the array
  //Recomput the positions of the items the array
 }

function computePositions()
{
saveTable = new Array( );
for (var i = 0; i < saveList.length; i++) {
     saveTable[saveList[i]] = i;
}
}

 function loadExportData()
 {
    //Executed on Page Load
   
   var data = getCookie("exportData");
   if (data != '') {
     try {
           saveList = string2Array(data);
         } catch(e) {//Do nothing because its not on this page 
         }
  try{
  if(saveList.length>0)
  {
   for (var i = 0; i < saveList.length; i++) {
           for (var j = 0; j < document.exportform.docIDs.length; j++) {
             try{
              var e = document.exportform.docIDs[j];
               if(e.value==saveList[i]){    
                   e.checked=true;}
                  
                }catch(e){}
              }  
          }    
        // document.exportform[saveList[i]].checked=true;
    }else{
     clearAll();
    }    
     } catch(e) {//  alert(e);//Do nothing because its not on this page 
     }
     
     
      
   }
    computePositions();
 }
 
 function saveExportData()
 {
   //Executed on Page Save
   var data = array2String(saveList);
   var expDate = getExpDate(180, 0, 0);
//   alert(data);
   setCookie("exportData", data, expDate);
 }
 
 
 function array2String(array) {
    var output = "";
    if (array) {
        output += "[";
        for (var i in array) {
            val = array[i];
            switch (typeof val) {
                case ("object"):
                    if (val[0]) {
                        output += array2String(val) + ",";
                    } else {
                        output += object2String(val) + ",";
                    }
                    break;
                case ("string"):
                    output += "'" + escape(val) + "',";
                    break;
                default:
                    output += val + ",";
            }
        }
        output = output.substring(0, output.length-1) + "]";
    }
    return output;
}

function string2Array(string) {
    eval("var result = " + string);
    return result;
}

function object2String(obj) {
    var val, output = "";
    if (obj) {    
        output += "{";
        for (var i in obj) {
            val = obj[i];
            switch (typeof val) {
                case ("object"):
                    if (val[0]) {
                        output += i + ":" + array2String(val) + ",";
                    } else {
                        output += i + ":" + object2String(val) + ",";
                    }
                    break;
                case ("string"):
                    output += i + ":'" + escape(val) + "',";
                    break;
                default:
                    output += i + ":" + val + ",";
            }
        }
        output = output.substring(0, output.length-1) + "}";
    }
    return output;
}

function string2Object(string) {
    eval("var result = " + string);
    return result;
}

// utility function to retrieve an expiration date in proper
// format; pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire (or negative
// values for a past date); all three parameters are required,
// so use zeros where appropriate
function getExpDate(days, hours, minutes) {
    var expDate = new Date( );
    if (typeof days == "number" && typeof hours == "number" && 
        typeof minutes == "number") {
        expDate.setDate(expDate.getDate( ) + parseInt(days));
        expDate.setHours(expDate.getHours( ) + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));
        return expDate.toGMTString( );
    }
}
   
// utility function called by getCookie( )
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}
   
// primary function to retrieve cookie by name
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return "";
}
   
// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
   
// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}