function mk_email(username, domain_prefix, domain_suffix) {
  return username + "@" + domain_prefix + "." + domain_suffix
}

function show_addr(username, domain_prefix, domain_suffix) {
  var email = mk_email(username, domain_prefix, domain_suffix);
  document.write("<a href=\"mailto:" + email + "\">" + email + "</a>");
}

function resize_callback(img_node, max_width, max_height) {
  var scale1 = max_width/img_node.width;
  var scale2 = max_height/img_node.height;
  var min = scale1 < scale2 ? scale1 : scale2;
  var new_width  = Math.round(img_node.width * min);
  var new_height = Math.round(img_node.height * min);
  img_node.height = new_height;
  img_node.width = new_width;
}

function show_image_in_bounding_box(max_width, max_height, url, extra) {
  var callback_args = max_width + ", " + max_height;
  var callback = "resize_callback(this, " + callback_args + ")";
  document.write("<div style=\"height: " + max_height + "; width: " + max_width + ";\">");
  document.write("<img style=\"display: none;\" onload=\"style.display = 'block'; " + callback + ";\" src=" + url + " " + extra + ">");
  document.write("</div>");
}


function stop_event(e) {
  if(!e) var e = window.event;
  
  //e.cancelBubble is supported by IE - this will kill the bubbling process.
  e.cancelBubble = true;
  e.returnValue = false;

  //e.stopPropagation works only in Firefox.
  if (e.stopPropagation) {
    e.stopPropagation();
    e.preventDefault();
  }
  return false;
}
function search_box_on_click(el){
  var ue=el.href;
  s = el.parentNode.parentNode.getElementsByTagName("input")[0];
  el.href=ue+"?search%20%22"+s.value+"%22";
}

function search_box_on_click_ui(el){
  var ue=el.href;
  s = el.parentNode.parentNode.getElementsByTagName("input")[0];
  var encoded = encode_query(s.value);
  el.href="/usa-flowers/search/"+encoded+".html";
  return true;
}


function search_box_clear(el){
  if (el.value == "keyword or #") {
    el.value = "";
  }
}

function search_box_on_enter(el, e){
  if(window.event) // IE
  {
    keynum = e.keyCode
  }
  else if(e.which) // Netscape/Firefox/Opera
  {
    keynum = e.which
  }
  if (keynum == 13) {
    stop_event(e);
    window.location.pathname = "/ordercatalogue/flowers.html"+"?search%20%22"+el.value+"%22";
  }
  return true;
}

function encode_query(str) {
  var acc = ""
  var regex = /[a-z0-9]/;
  var spaces = false;
  var s = str.toLowerCase();
  for (var i=0; i < s.length; i++) {
     var chr = s.charAt(i);
     console.debug("looking at char: "+chr);
     if (regex.test(chr)) {
       acc += chr;
       spaces = false;
     } else if (chr=="-") {
       acc += "--"
     } else if (!spaces) {
       acc += "-";
       spaces = true;
     }
  }
  return acc;
}

function search_box_on_enter_ui(el, e){
  if(window.event) // IE
  {
    keynum = e.keyCode
  }
  else if(e.which) // Netscape/Firefox/Opera
  {
    keynum = e.which
  }
  if (keynum == 13) {
    stop_event(e);
    var encoded = encode_query(el.value);
    window.location.pathname = "/usa-flowers/search/"+encoded+".html";
  }
  return true;
}
