function stringReplace (orig_s, find_s, repl_s) {
  var pos = 0;
  var len = find_s.length;
  pos = orig_s.indexOf(find_s);
  while (pos != -1) {
    pre_s = orig_s.substr(0,pos);
    post_s = orig_s.substr(pos+len,orig_s.length);
    orig_s = pre_s + repl_s + post_s;
    pos = orig_s.indexOf(find_s);
    }
  return orig_s;
  }

function add_item_plain(item) {
  var str = "http://www.hobbylinc.com/~hobbylinc/cgi-bin/a3.cgi?add=" + item + "&q=1";
  parent.location = str;
}

function add_item(item) {
  var myCookie = CookieGet("cart_ck");
  //alert("cart_ck=" + myCookie);
  if (myCookie == "0") {
    add_item_stay(item);
  } else {
    add_item_go(item);
  }
}

function add_item_go(item) {
  var tmp = stringReplace(location.href,"\&","_AMPERSAND_");
  var str = "http://www.hobbylinc.com/~hobbylinc/cgi-bin/a3.cgi?add=" + item + "&q=1&back=" + tmp;
  parent.location = str;
}

function add_item_stay(item) {
  var quan = prompt("How many " + item + " would you like to add to your cart?", "1");
  var href = "http://www.hobbylinc.com/~hobbylinc/cgi-bin/a3.cgi?add=" + item + "&q=" + quan;
  if (quan != null) {
    myimage = new Image();
    myimage.src = href;
    document.hl.hl.value = href;
    }
}

function CookieGet (name) {
  var result = null;
  var myCookie = " " + document.cookie + ";";
  var searchName = " " + name + "=";
  var startOfCookie = myCookie.indexOf(searchName);
  var endOfCookie;
  if (startOfCookie != -1) {
    startOfCookie += searchName.length;
    endOfCookie = myCookie.indexOf(";",startOfCookie);
    result = unescape(myCookie.substr(startOfCookie,1));
  }
  return result;
}

function CookieSetBack () {
  var newCookie = "b=" + location.href + "; path=/";
  //alert(newCookie);
  document.cookie = newCookie;
}

function CookieSetEz (name,value) {
  //alert("CookieSetEz(" + name + "," + value);
  var newCookie = name + "=" + value + "; path=/";
  document.cookie = newCookie;
  }

function CookieSetForever (name,value) {
  //alert("CookieSetForever(" + name + "," + value);
  var exp = new Date()
  var tenYearsFromNow = exp.getTime() + (10 * 365 * 24 * 60 * 60 * 1000)
  //var tenYearsFromNow = exp.getTime() + (2 * 60 * 1000)
  exp.setTime(tenYearsFromNow )
  var newCookie = name + "=" + value + "; expires=" + exp.toGMTString() + "; path=/";
  //var newCookie = name + "=" + value + "; path=/";
  //alert("Setting " + newCookie);
  document.cookie = newCookie;
  //alert(document.cookie);
}

function CookieDelete( name ) {
//alert("CookieDelete(" + name + ")");
document.cookie = name + "=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function submit_search (name) {
  //alert("submit_search(" + name + ")");
  if (name == "cat") {
    document.SEARCH_FORM.mfg_s.value = "";
    document.SEARCH_FORM.age.value = "";
    }
  else {
    if (name == "age") {
      document.SEARCH_FORM.mfg_s.value = "";
      }
    else {
      if ( (name == "sort") | (name == "show") ) {
        document.SEARCH_FORM.sort_i.value = document.SORT_FORM.sort_i.options[document.SORT_FORM.sort_i.selectedIndex].value;
        document.SEARCH_FORM.show_i.value = document.SORT_FORM.show_i.options[document.SORT_FORM.show_i.selectedIndex].value;
        //alert(document.SORT_FORM.flag.value);
        document.SEARCH_FORM.flag.value = document.SORT_FORM.flag.value;
        document.SEARCH_FORM.also_s.value = document.SORT_FORM.also_s.value;
        }
      }
    }
  document.SEARCH_FORM.submit();
  }

// for s7c.cgi
function submit_search2 (name) {
  //alert("submit_search2(" + name + ")");
  if (name == "cat") {
    document.SEARCH_FORM.mfg_i.value = 0;
    document.SEARCH_FORM.age_i.value = 0;
    }
  else {
    if (name == "age") {
      document.SEARCH_FORM.mfg_i.value = 0;
      }
    else {
      if ( (name == "sort") | (name == "show") ) {
        document.SEARCH_FORM.sort_i.value = document.SORT_FORM.sort_i.options[document.SORT_FORM.sort_i.selectedIndex].value;
        document.SEARCH_FORM.show_i.value = document.SORT_FORM.show_i.options[document.SORT_FORM.show_i.selectedIndex].value;
        //alert(document.SORT_FORM.flag.value);
        document.SEARCH_FORM.flag.value = document.SORT_FORM.flag.value;
        }
      }
    }
  document.SEARCH_FORM.submit();
  }

function submit_form (name) {
  if (name == "sort") {
    document.SORT_FORM.submit();
    }
  }

function help_box (file) {
  window.open (file, 'newwindow', config='height=300, width=650, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
  }

function ShowHide (name) {
  if (document.getElementById(name)) {
    if (document.getElementById(name).style.display == "") {
      document.getElementById(name).style.display = "none";
      }
    else {
      document.getElementById(name).style.display = "";
      }
    }
  }

function addEvent( obj, type, fn )
{
        if (obj.addEventListener)
                obj.addEventListener( type, fn, false );
        else if (obj.attachEvent)
        {
                obj["e"+type+fn] = fn;
                obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
                obj.attachEvent( "on"+type, obj[type+fn] );
        }
}

function createElement(element) {
        if (typeof document.createElementNS != 'undefined') {
                return document.createElementNS('http://www.w3.org/1999/xhtml',element);
        }
        if (typeof document.createElement != 'undefined') {
                return document.createElement(element);
        }
        return false;
}

function insertTop(obj) {
        d=createElement("div");
        d.className="bt"; 
    d2=createElement("div");
    d.appendChild(d2);
        obj.insertBefore(d,obj.firstChild);
}

function insertBottom(obj) {
        d=createElement("div");
        d.className="bb";
    d2=createElement("div");
    d.appendChild(d2);
        obj.appendChild(d);
}
function initCB()
{
        var divs = document.getElementsByTagName('div');
        var cbDivs = [];
        for (var i = 0; i < divs.length; i++) {
                if (/\bcbb\b/.test(divs[i].className))
                        cbDivs[cbDivs.length] = divs[i];
        }
        var thediv, outer, i1, i2;
        for (var i = 0; i < cbDivs.length; i++) {
                thediv = cbDivs[i];
                outer = createElement('div');
                outer.className = thediv.className;
                outer.className = thediv.className.replace('cbb', 'cb');
                thediv.className = 'i3';
                thediv.parentNode.replaceChild(outer, thediv);
                i1 = createElement('div');
                i1.className = 'i1';
                outer.appendChild(i1);
                i2 = createElement('div');
                i2.className = 'i2';
                i1.appendChild(i2);
                i2.appendChild(thediv);
                insertTop(outer);
                insertBottom(outer);
        }
}

if(document.getElementById && document.createTextNode)
{
        addEvent(window, 'load', initCB);
}

////////////////////////////////////////////////////////////////////////////////
function postUserText(form_s,id_s) {
  //alert("postUserText " + form_s + " " + id_s);
  dojo.xhrGet({
    form: form_s,
    url: "/dev/cgi-bin/user.cgi",
    load: function(response, ioArgs){
      //alert("SETTING HTML TO " + response);
      dojo.byId(id_s).innerHTML = response;
      return response;
    },
    error: function(response, ioArgs){
      alert(response);
      dojo.byId(id_s).innerHTML = "An error occurred, with response: " + response;
      return response;
    },
    handleAs: "text"
  });
}

////////////////////////////////////////////////////////////////////////////////
function postText(form_s,id_s) {
  //alert("postText " + form_s + " " + id_s);
  dojo.xhrPost({
    form: form_s,
    url: "/dev/cgi-bin/mgr.cgi",
    load: function(response, ioArgs){
      dojo.byId(id_s).innerHTML = response;
      return response;
    },
    error: function(response, ioArgs){
      dojo.byId(id_s).innerHTML = "An error occurred, with response: " + response;
      return response;
    },
    handleAs: "text"
  });
}

function getText(file_s,id_s) {
  //alert("getText("+file_s+","+id_s+")");
  dojo.xhrGet({
    url: file_s,
    load: function(response, ioArgs){
      dojo.byId(id_s).innerHTML = response;
      return response;
    },
    error: function(response, ioArgs){
      dojo.byId(id_s).innerHTML = "An error occurred, with response: " + response;
      return response;
    },
    handleAs: "text"
  });
}

function getPage(page_s) {
  document.location = page_s;
}

