function defaultResultOnMouseOver(el) {
  var this_tds = el.getElementsByTagName('td');
  var x;
  for(var x = 0; x < this_tds.length; x++) {
    this_tds[x].style.backgroundColor = "#f3ffed";
  }
}

function defaultResultOnMouseOut(el) {
  var this_tds = el.getElementsByTagName('td');
  for(var x = 0; x < this_tds.length; x++) {
    this_tds[x].style.backgroundColor = "#fff";   
  }
}

function featuredResultOnMouseOver(el) {
  el.style.backgroundColor = '#f3ffed';
}

function featuredResultOnMouseOut(el) {
  el.style.backgroundColor = '#fff';
}

function inputOnFocus(el) {
  el.style.backgroundColor = '#f3ffed';
}

function inputOnBlur(el) {
  el.style.backgroundColor = '#fff';
}
 
function closePopup() {
  var overlay = document.getElementById('overlay');
  var popup = document.getElementById('popup');
  popup.style.display = 'none';
  overlay.style.display = 'none';
}
  
function openPopup() {
  var overlay = document.getElementById('overlay');
  var popup = document.getElementById('popup');
  popup.style.display = 'block';
  overlay.style.display = 'block';   
}

function showEmailChange() {
  document.getElementById('email_change_link').className = 'off';
  document.getElementById('email_change_form').className = '';
}

function submitEmailChange() {
  var email = document.getElementById('email_address_input').value;
  var ajax = new Ajax(submitEmailChangeOnSuccess, submitEmailChangeOnFailure);
  ajax.post('/ajax/change_email/', 'email=' + (encodeURIComponent ? encodeURIComponent(email) : email));
}

function submitEmailChangeOnSuccess(ajaxObj, responseText) {
  var email = '';
  var email_formatted = '';
  var errno = 0;

  eval(responseText);

  // reset the error states
  document.getElementById('email_already_exists_msg').style.display = 'none';
  document.getElementById('email_general_error_msg').style.display = 'none';

  switch(errno) {
    case 4:
      document.getElementById('email_already_exists_msg').style.display = 'block';
      break;
    case 0:
      if(document.getElementById('email_address_text')) {
        document.getElementById('email_address_text').innerHTML = email;
      }
      if(document.getElementById('email_address_text_formatted')) {
        document.getElementById('email_address_text_formatted').innerHTML = email_formatted;
      }
      document.getElementById('email_change_form').className = 'off';
      document.getElementById('email_change_link').className = '';
      break;
    default:
      document.getElementById('email_general_error_msg').style.display = 'block';
      break;
  }
}

function submitEmailChangeOnFailure() {

}

function showCountryChange() {
  document.getElementById('country_change_link').style.display = 'none';
  document.getElementById('country_change_form').style.display = 'block';
}

function submitCountryChange(baseURL) {

  var selectEl = document.getElementById('country_selector');
  var selectedIndex = selectEl.selectedIndex;
  if(selectedIndex == -1) { return false; }

  window.location = baseURL + '&cn=' + selectEl.options[selectedIndex].value;
}

function animateLoader(newURL) {
 document.getElementById('connector_container').style.backgroundImage = newURL;
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if(node == null) {
    node = document;
  }
  if(tag == null) {
    tag = '*';
  }
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for(i = 0, j = 0; i < elsLen; i++) {
    if(pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}	
 
function sortNumber(a,b) {
  return a - b;
}

function adjust_height () {
  var main_offers = document.getElementById('main_offers');
  if(!main_offers) {return false; }
  var height_var_p = getElementsByClass('height_var',main_offers,'p') ;
  var height_var_h4 =  getElementsByClass('height_var',main_offers,'h4') ;
  var p_height = new Array();
  var h4_height = new Array();
  for(i=0; i<height_var_p.length; i++) {
    p_height.push(height_var_p[i].offsetHeight);
    h4_height.push(height_var_h4[i].offsetHeight);
  }
  if(!p_height.sort || !h4_height.sort) { return false; }
  p_height.sort(sortNumber);
  h4_height.sort(sortNumber);
  for(y=0;y<p_height.length; y++) {
    height_var_p[y].style.height = p_height[p_height.length-1]-15 + 'px'; 
    height_var_h4[y].style.height = h4_height[h4_height.length-1] + 'px';
  }
}

var popup_failed = false;

/*******************************************
 * Opens a popup window.
 *
 * @param url         The url to open.
 * @param width       The width of your window in pixels.
 * @param height      The height of your window in pixels.
 * @param lean        True to render a simple popup; false to render a popup with scrollbars, toolbars, etc.
 * @param resizable   True to allow the popup to be resized; false otherwise.
 * @param centered    True to center the popup window on the user's screen.
 * @param name        The name of your window (so you can access the window via JS).
 * @param redirect_url  The url to open if window.open() returns false
 */
function popup_window(url, width, height, lean, resizable, centered, name, redirect_url) {

  /* Calculate the window settings */
  var status, toolbar, location, menubar, directories, scrollbars;
  status = toolbar = location = menubar = directories = scrollbars = 1;
  if(lean == true) {
    status = toolbar = location = menubar = directories = scrollbars = 0;
  }
  var settings =
    'width='+width+',height='+height+',status='+status+',toolbar='+toolbar+
    ',location='+location+',menubar='+menubar+',directories='+directories+
    ',scrollbars='+scrollbars+',resizable='+(resizable ? '1' : '0');
  if(centered == true) {
    x_offset = (screen.width) ? (screen.width-width)/2 : 0;
    y_offset = (screen.height) ? (screen.height-height)/2 : 0;
    settings+= ',left='+x_offset+',top='+y_offset;
  }

  /* Open window and return a handle to that window */
  var ret = window.open(url, name, settings);

  if(!ret) {
    js_error_log(200, url);
    popup_failed = true;
    if(redirect_url) {
      window.location = redirect_url;
    }
  }

  return ret;
}

function check_popup_failed() {
  if(popup_failed) {
    return false;
  } else {
    return true;
  }
}

/*********************************************
 *
 * @param error_code      error code to record
 * @param error_msg       error message to log if check_return is false
 */
function js_error_log(error_code, error_msg) {
  var image = new Image();
  image.src = tp_static_host + '/error/?ec=' + error_code + ((error_msg && encodeURIComponent) ? '&d=' + encodeURIComponent(error_msg) : '');
}


function hide(el_name) {
  if(!el_name) { return false; }

  var el = document.getElementById(el_name);
  if(!el || !el.style) { return false; }

  el.style.display = 'none';
  return false;
}

function show(el_name) {
  if(!el_name) { return false; }

  var el = document.getElementById(el_name);
  if(!el || !el.style) { return false; }

  el.style.display = '';
  return false;
}
