// JavaScript code for DB/Text InfoCart
//  Copyright © 2001, Inmagic, Inc., Woburn, MA, USA. All rights reserved.
// This source file was created for the American Geological Society, Sep 2001

// This variable sets the location of the page you are returned
// to when the cart is emptied by the user.
var queryPageURL = "http://odp.georef.org/dbtw-wpd/qbeodp.htm";

   // These variables need to be updated for every tb
   // each string variable contains the name\value pair required by webPub
   // They must be URL encoded.
   
   var textbase = escape("odp");
   var idField = escape("id");
   var bu = escape("http://" + document.domain + "/QBEODP.htm");
   var recordsPerPage = "50";
   var reportForm = escape("____________________");
   var displayForm = escape("Single Record");
   var rl = "1";
   var dl = "0";
   var np = "1";
   
   
      //sets the domain the cookie can be used in
      //null means just the default domain
      var domain = null;
      
      
// Heinle's function for retrieving a cookie. INFOCART

function getCookie(name){
   var cname = name + "=";
   var dc = document.cookie;
   if (dc.length > 0) {
     begin = dc.indexOf(cname);
     if (begin != -1) {
       begin += cname.length;
       end = dc.indexOf(";", begin);
          if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
     }
   }
  return null;
}

// An adaptation of Dorcht's function for setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
  //send them back to Query Page
  window.location.href = queryPageURL ;
}


//set expiration date 7 days ahead
var expiration = new Date();
expiration.setTime(expiration.getTime() + 604800000);

//set this cookie
function setPassword(form){
	var password = form.ID.value;
	setCookie('inmusers', password, expiration, null, domain, null);
	}
	
//get this cookie
function getPassword(){	
	if (getCookie('inmusers') != null){ 	
		document.qbe_form.ID.value = getCookie('inmusers');
	}
}
//delete this cookie
function clearPassword(form){
	document.qbe_form.ID.value = "";
	delCookie('inmusers', null, domain);
}

// Clear all the boxes on the form except the password
function clearForm (form) {
   var formLength = form.elements.length;
   
   //walk through all the elements of the form
   for(i=0;i<formLength;i++){

      //if it is a text element then clear it. wont work for textarea
      if (form.elements[i].type == 'text') {
         form.elements[i].value = '';
      }
   }
}

// check for id already in cookie

function checkDup(form,idValueArray,value){
   
   var valueLength = idValueArray.length
   var x = 0;
   
   while (x < valueLength){
      
      if (idValueArray[x] == value){
         alert("You have already selected this item.");
         return false;
         }
      x++;
    }
      return true;
   
}

//====================================================
// Function:   addIDbtn
// Purpose:    This will write the info to a cookie
// Parameters: form - pointer to the host form
// Returns:    -
// Note:       update item count disabled
//====================================================


function addIDbtn(form){

   var arrayLength;
   var idValueArray = new Array;
   var cookieValue = '';
   
      // check for existing cookie and get existing values
   
   if(getCookie('cart')){
         cookieValue = getCookie('cart');
         idValueArray = cookieValue.split('/');
         
      }
       
         if (checkDup(form,idValueArray,form.id.value)){
            idValueArray[idValueArray.length] = form.id.value;
            }

   
   
   // join the ids into a string then add to any existing values
     
   cookieValue = idValueArray.join('/');
   setCookie("cart",cookieValue,expiration,"/",null,null);

//Disabled for AmGeo
   //update the count in the cart at the bottom of the page if it exists
//   if(idValueArray.length>0)
//      document.cartStatus.cartCount.value = idValueArray.length;

}

// run a canned query to get the record selected
function getRecords(cookieName){
   var searchString = getCookie(cookieName);
   

   var CannedQuery = 'http://' + document.domain + '/dbtw-wpd/exec/dbtwpub.dll?';
   CannedQuery += 'TN=' + textbase;
   CannedQuery += '&QF0='+ idField;
   CannedQuery += '&QI0=' + searchString;
   CannedQuery += '&BU=' + bu;
   CannedQuery += '&MR=' + recordsPerPage;
   CannedQuery += '&RF=' + reportForm;
   CannedQuery += '&RL=' + rl;
   CannedQuery += '&DF=' + displayForm;
   CannedQuery += '&DL=' + dl;
   CannedQuery += '&NP=' + np;
   CannedQuery += '&AC=QBE_QUERY';
   //alert(CannedQuery);
   
   if(getCookie('cart')){
      window.location.href = CannedQuery;
      }
   else {
      alert("Your cart is empty. You will be returned to the search page.");
      window.location.href = queryPageURL;
      }
}


//copyright 2000 Inmagic, Inc.
function deleteArrayValue(removeVal,array){


   var arrayLength = array.length;
   var i = 0;
   var j = 0;
   var tempArray = new Array;
   
   while (i < arrayLength) {
      if (array[i] == removeVal){
         i++
         }
      else{
         tempArray[j] = array[i];
         i++;
         j++;
         }     
      }
      return tempArray;
}

function removeIDbtn (id) {

  if(getCookie('cart')) {
   var idValue = id;
   var cartString = getCookie('cart');
   var cartArray =  cartString.split('/');
   
   cartArray = deleteArrayValue(idValue,cartArray);
   cookieValue = cartArray.join('/');
   setCookie("cart",cookieValue,expiration,"/",null,null);
   }
   getRecords('cart');
}