var COMPARE_LIMIT = 4;
var CART_EMPTY_HANDLER = '/cart/empty';
var CART_RMV_HANDLER = '/cart/remove';
var CARD_VIEW_PRINT_HANDLER = '/card/view/card_id/';
var CARD_COMPARE_PRINT_HANDLER = '/cart/compare/p/k';
var CARD_APP_HANDLER = '/card/app/card_id/';
var FEATURE_COUNT_LIMIT = 4;
var mrkt_seg = '';

function card_to_cart (card_id) {
  var url = "/cart/add/card_id/" + card_id;
  new Ajax.Request(url, {
      method: 'get',
      onSuccess: function() {
        alert("The card is now in your cart for comparison!\nPlease go to \"Compare Cards\" to compare selected cards side by side.");
      }
  });
}

function compare_cards() {
  var f = document.getElementById('card_compare_form');
  var card_list = f.elements['card_list[]'];
  if (!card_list.length) {
    alert('Card comparison requires at least 2 cards!');
    retrun;
  }

  count = 0;
  for (i = 0; i < card_list.length; i++) {
    if (card_list[i].checked)
    count++;
  }
  
  if (count > COMPARE_LIMIT) {
    alert("Currently we only support up to " + COMPARE_LIMIT + "cards in one comparison.");
    return;
  } else if (count <= 1) {
    alert("Please select at least 2 cards to compare!");
    return;
  } else {
    f.submit();
  }
}

function remove_cards() {
  var f = document.getElementById('card_compare_form');
  f.operation.value = 'remove';
  var card_list = f.elements['card_list[]'];
  count = 0;

  if (card_list.length) {
    for (i = 0; i < card_list.length; i++) {
      if (card_list[i].checked)
        count++;
    }
  } else {
    if (card_list.checked)
      count++;
  }

  if (count == 0) {
    alert("No card selected!");
  } else {
    f.action = CART_RMV_HANDLER;
    f.submit();
  }
}

function empty_cart() {
  if (confirm("Are you sure you want to empty the cart?")) {
    location = CART_EMPTY_HANDLER;
  }
}

function pre_populate_cart() {
  var f = document.getElementById('card_compare_form');
  var card_list = f.elements['card_list[]'];
  if (!card_list.length) {
    retrun;
  }

  count = 0;
  for (i = 0; i < card_list.length; i++) {
    if (card_list[i].checked)
      count++;
  }

  if (count > 0) return;
  
  size = card_list.length > COMPARE_LIMIT ? COMPARE_LIMIT : card_list.length;
  for (i = 0; i < size; i++) {
    card_list[i].checked = true;
  }
}

var feature_count = 0;
  
/**   
 * mark a feature selected
 */
function mark(obj_name) {
  if (feature_count >= FEATURE_COUNT_LIMIT) {
    alert('Currently we only support up to ' + FEATURE_COUNT_LIMIT + ' features in one query.');
    return;
  }

  var cur_node = document.getElementById(obj_name);
  var new_node = cur_node;    

  var src_container = document.getElementById('src_list');
  src_container.removeChild(cur_node);    

  var tar_container = document.getElementById('tar_list');
  new_node.innerHTML = new_node.innerHTML.replace(/mark/, 'unmark');
  tar_container.appendChild(new_node);
  
  feature_count++;
  
  document.getElementById("button_div").style.display = 'block';
}
  
/**
 * unmark a selected feature
 */
function unmark(obj_name) {
  var cur_node = document.getElementById(obj_name);
  var new_node = cur_node;

  var src_container = document.getElementById('tar_list');
  src_container.removeChild(cur_node);    

  var tar_container = document.getElementById('src_list');
  new_node.innerHTML = new_node.innerHTML.replace(/unmark/, 'mark');
  tar_container.appendChild(new_node);
  
  if (feature_count > 0)
    feature_count--;
  
  if (feature_count == 0)
    document.getElementById("button_div").style.display = 'none';
}

/**
 * post the search query
 */
function post_query() {
  var f = document.getElementById('card_finder_form');
  e = document.createElement("input");   
  e.setAttribute("name", "mrkt_seg");
  e.setAttribute("type", "hidden");
  e.setAttribute("value", mrkt_seg);
  f.appendChild(e);
  
  var tar_container = document.getElementById('tar_list');
  for (i = 0; i < tar_container.childNodes.length; i++) {
    e = document.createElement("input");
    e.setAttribute("name", "fcode[]");
    e.setAttribute("type", "hidden");
    e.setAttribute("value", tar_container.childNodes[i].id);
    f.appendChild(e);
  }
  f.submit();
}

function show_desc(cat) {
  var desc = '';
  switch(cat) {
    case 'BUSINESS' : desc = "Business credit cards available to both small business owners as well as corporate executives. Earn rewards, cash back or airline miles for every dollar you and your employees spend on business expenses. Get your company's expenses on track with a corporate or small business credit card."; break;
    case 'CONSUMER' : desc = 'Personal credit card is very important in your daily life. That is why every time you spend your money, you want gain as much as rewards as you can.  Select your favorite features, find your best credit card with the most rewards you want.  You next rewards are only one "click" away!'; break;
    case 'STUDENT'  : desc = 'We understand the difficulties to get a credit card for students. However, through our "Credit Card Finder" tool, it will help you to find the best-fit card specifically design for your need no matter good, bad, or no credit card history. You can start building your credit card history or improve your credit scores into next level right here and right now.'; break;
    default: break;
  }
  document.getElementById('desc_div').innerHTML = desc;
}
  
function hide_desc() {
  document.getElementById('desc_div').innerHTML = '';
}

function add_favorite(url, title)
{
  if (window.sidebar) { // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
  } else if( window.external ) { // IE Favorite
    window.external.AddFavorite( url, title); 
  } else if(window.opera && window.print) { // Opera Hotlist
    alert("Please press Ctrl+D to add to favorite.");
  }
}

function get_position_left(obj){
  var el = obj; var pl = 0;
  while(el) { pl += el.offsetLeft; el = el.offsetParent; }
  return pl;
}

function get_position_top(obj){
  var el = obj; var pt = 0;
  while(el) { pt += el.offsetTop; el = el.offsetParent; }
  return pt;
}

function show_contact_email() {
  document.write('<a href="mailto:info@myawesomecard.com">info@myawesomecard.com</a>');
}

function show_careers_email() {
  document.write('<a href="mailto:info@myawesomecard.com">careers@myawesomecard.com</a>');
}

var pcard_view_win;
function print_card_view(card_id) {
  pcard_view_win = window.open(CARD_VIEW_PRINT_HANDLER + card_id + '/p/k','pcard_view_win',
    'left=20,top=20,width=770,height=600,toolbar=1,menubar=0,resizable=0,scrollbars=1');
  pcard_view_win.focus();
}

var pcard_comp_win;
function print_card_compare() {
  pcard_comp_win = window.open(CARD_COMPARE_PRINT_HANDLER,'pcard_comp_win',
    'left=20,top=20,width=780,height=600,toolbar=1,menubar=0,resizable=0,scrollbars=1');
  pcard_comp_win.focus();
}

String.prototype.trim = function () {
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var pcard_app_win;
function card_app(card_id) {
  pcard_app_win = window.open(CARD_APP_HANDLER + card_id,'pcard_app_win');
  pcard_app_win.focus();
}