var do_not_add_to_cart = 'No';
var payment_method = 'Credit Card';

var as_o = { script:webpage_url + "suggest_card_name.php?json=true&not_type=Hero&", varname:"input", json:true, minchars:3, noresults: "No cards found!" };
var as_o__all = { script:webpage_url + "suggest_card_name.php?json=true&", varname:"input", json:true, minchars:3, noresults: "No cards found!" };
var as_o__hero = { script:webpage_url + "suggest_card_name.php?json=true&type=Hero&", varname:"input", json:true, minchars:3, noresults: "No cards found!" };

function addToCart(item_number, qty) {
  ajaxSubmit('POST', webpage_url + 'oddsandends.php', true, 'shopping_cart=Yes&action=Add&item_number=' + item_number + '&qty=' + qty);
}

function addToCart__Trade(item_number, qty) {
  ajaxSubmit('POST', webpage_url + 'oddsandends.php', true, 'shopping_cart=Yes&is_trade=Yes&action=Add&item_number=' + item_number + '&qty=' + qty);
}

function changeBorderColor(set, subset) {
  var old_suffix = '';
  var new_suffix = '1';
  if(set == 0) {
    old_suffix = '1';
    new_suffix = '';
  }
  var victim = document.getElementById('cart_image__' + subset);
  victim.src = victim.src.replace('cart' + old_suffix + '.gif', 'cart' + new_suffix + '.gif');
  victim = document.getElementById('tl__' + subset);
  victim.src = victim.src.replace('tl' + old_suffix + '.gif', 'tl' + new_suffix + '.gif');
  victim = document.getElementById('t__' + subset);
  victim.style.background = victim.style.background.replace('t' + old_suffix + '.gif', 't' + new_suffix + '.gif');
  victim = document.getElementById('tr__' + subset);
  victim.src = victim.src.replace('tr' + old_suffix + '.gif', 'tr' + new_suffix + '.gif');
  victim = document.getElementById('l__' + subset);
  victim.style.background = victim.style.background.replace('l' + old_suffix + '.gif', 'l' + new_suffix + '.gif');
  victim = document.getElementById('r__' + subset);
  victim.style.background = victim.style.background.replace('r' + old_suffix + '.gif', 'r' + new_suffix + '.gif');
  victim = document.getElementById('bl__' + subset);
  victim.src = victim.src.replace('bl' + old_suffix + '.gif', 'bl' + new_suffix + '.gif');
  victim = document.getElementById('b__' + subset);
  victim.style.background = victim.style.background.replace('b' + old_suffix + '.gif', 'b' + new_suffix + '.gif');
  victim = document.getElementById('br__' + subset);
  victim.src = victim.src.replace('br' + old_suffix + '.gif', 'br' + new_suffix + '.gif');
}

function checkIfSubmitable__cc() {
  var message = 'You must select a card type before you may continue.';
  if(isBlank(document.the_form_name__cc.type.value)) { alert(message); return false; }
  var message = 'You must enter your card number before you may continue.';
  if(isBlank(document.the_form_name__cc.number.value)) { alert(message); return false; }
  var message = 'You must enter your card\'s cvv2 code before you may continue.';
  if(isBlank(document.the_form_name__cc.cvv2.value)) { alert(message); return false; }
  var message = 'You must enter your card\'s expiration date before you may continue.';
  if(isBlank(document.the_form_name__cc.expiration_month.value)) { alert(message); return false; }
  if(isBlank(document.the_form_name__cc.expiration_year.value)) { alert(message); return false; }
  var message = 'You must enter the name on your card before you may continue.';
  if(isBlank(document.the_form_name__cc.name.value)) { alert(message); return false; }
  if(document.the_form_name__cc.same_address.checked == false) {
    var message = 'You must enter your address before you may continue.';
    if(isBlank(document.the_form_name__cc.address1.value)) { alert(message); return false; }
    var message = 'You must enter in which city you are located before you may continue.';
    if(isBlank(document.the_form_name__cc.city.value)) { alert(message); return false; }
    var message = 'You must enter in which state you are located before you may continue.';
    if(isBlank(document.the_form_name__cc.state_number.value)) { alert(message); return false; }
    var message = 'You must enter your zip code before you may continue.';
    if(isBlank(document.the_form_name__cc.zip_code.value)) { alert(message); return false; }
  }
  return true;
}

function checkIfSubmitable__combo() {
  var number_of_cards = 0;
  for(var a=1; a<=4; a++) {
    if(!isBlank(document.getElementById('card_' + a).value)) {
      number_of_cards += 1;
    }
  }
  var message = 'Combos must have a minimum of 2 cards, in order for them to be accepted.';
  if(number_of_cards < 2) { alert(message); return false; }
  return true;
}

function checkIfSubmitable__deck() {
  var message = 'You must enter a name from this deck before you may continue';
  if(isBlank(document.the_form_name__add_deck.title.value)) { alert(message); return false; }
  var message = 'You must specify a hero for this deck before you may continue';
  if(isBlank(document.the_form_name__add_deck.hero.value)) { alert(message); return false; }
  var number_of_cards = 0;
  for(var a=1; a<=40; a++) {
    if(!isBlank(document.getElementById('card_' + a).value) && isDigit(document.getElementById('qty_' + a).value)) {
      number_of_cards += Number(document.getElementById('qty_' + a).value);
    }
  }
  var message = 'Decks must have a minimum of 40 cards in the main deck, and the deck that you have entered currently only has ' + number_of_cards + ' cards. You must enter more cards before you may continue.';
  if(number_of_cards < 40) { alert(message); return false; }
  return true;
}

function checkIfSubmitable__email() {
  var message = 'You must enter your name before you may continue.';
  if(isBlank(document.the_form_name__contact_us.name.value)) { alert(message); return false; }
  var message = 'You must enter a valid email address so we can respond to your message before you may continue.';
  if(!isEmailAddress(document.the_form_name__contact_us.email_address.value)) { alert(message); return false; }
  var message = 'you must enter a subject for this message before you may continue.';
  if(isBlank(document.the_form_name__contact_us.subject.value)) { alert(message); return false; }
  var message = 'You must enter a message before you may continue.';
  if(isBlank(document.the_form_name__contact_us.message.value)) { alert(message); return false; }
  return true;
}

function checkIfSubmitable__tournament() {
  var message = 'You must specify for which game this tournament is before you may continue.';
  if(isBlank(document.the_form_name__add_tournament.game_type.value)) { alert(message); return false; }
  var message = 'You must enter an address for this tournament before you may continue.';
  if(isBlank(document.the_form_name__add_tournament.address1.value)) { alert(message); return false; }
  if(document.the_form_name__add_tournament.is_us.value == 'Yes') {
    var message = 'You must enter in which city this tournament is being held before you may continue.';
    if(isBlank(document.the_form_name__add_tournament.city.value)) { alert(message); return false; }
    var message = 'You must enter in which state this tournament is being held before you may continue.';
    if(isBlank(document.the_form_name__add_tournament.state_number.value)) { alert(message); return false; }
    var message = 'You must enter the zip code of the tournament location before you may continue.';
    if(isBlank(document.the_form_name__add_tournament.zip_code.value)) { alert(message); return false; }
  }
  else {
    var message = 'You must enter the country in which this tournament is being held before you may continue.';
    if(isBlank(document.the_form_name__add_tournament.country.value)) { alert(message); return false; }
  }
  var message = 'You must specify on which day this tournament is being held before you may continue.';
  if(isBlank(document.the_form_name__add_tournament.date__start.value)) { alert(message); return false; }
  if(document.the_form_name__add_tournament.is_multiple_days.value == 'Yes') {
    var message = 'You must specify on which day this tournament will end before you may continue.';
    if(isBlank(document.the_form_name__add_tournament.date__end.value)) { alert(message); return false; }
  }
  return true;
}

function isUnitedStatesChanged(is_us) {
  var theRules = getCssRules(3);
  if(is_us == 'Yes') {
    theRules[0].style.display='';
    theRules[1].style.display='none';
  }
  else {
    theRules[0].style.display='none';
    theRules[1].style.display='';
  }
}

function linkClicked(link_number) {
  document.location.href = webpage_url + link_number + '/Links.html';
}

function loggingInAndOut(in_or_out) {
  var theRules = getCssRules(4);
  switch(in_or_out) {
    case 'In': { theRules[0].style.display=''; theRules[1].style.display='none'; break; }
    case 'Out': { theRules[0].style.display='none'; theRules[1].style.display=''; break; }
  }
}

function paymentMethodSelected(type, order_number, order_number2) {
  switch(type) {
    case 'Credit Card': {
      if(checkIfSubmitable__cc()) {
        if(the_form_name__cc.existing_order.value == 'Yes') {
          tb_show(null, 'please_wait_page.php?height=204&width=400&message=Processing Credit Card', false);
        }
        document.the_form_name__cc.payment_method.value = type;
        ajaxSubmit('POST', webpage_url + 'oddsandends.php', true, ajaxFormatURL('the_form_name__cc'));
      }
      break;
    }
    case 'Mail Payment': {
      ajaxSubmit('POST', webpage_url + 'oddsandends.php', true, 'payment_method=' + type + '&order_number=' + order_number + '&order_number2=' + order_number2);
      break;
    }
    case 'PayPal': {
      ajaxSubmit('POST', webpage_url + 'oddsandends.php', true, 'payment_method=' + type + '&order_number=' + order_number + '&order_number2=' + order_number2);
      break;
    }
  }
}

function roundNumber(the_number, precision) {
  return the_number.toFixed(precision);
}

function topMenuClicked(page) {
  switch(page) {
    case 'Decks': { window.location='index.php?page=Decks'; return null; break; }
    case 'Homepage': { window.location='index.php?page=Homepage'; return null; break; }
    case 'Logout': { ajaxSubmit('POST', webpage_url + 'oddsandends.php', true, 'logout=Yes'); break; }
  }
  var theRules = getCssRules(1);
  switch(page) {
    case 'Cards': { theRules[0].style.display=''; theRules[1].style.display='none'; theRules[2].style.display='none'; theRules[3].style.display='none'; theRules[4].style.display='none'; theRules[5].style.display='none'; theRules[6].style.display='none'; break; }
    case 'Combos': { theRules[0].style.display='none'; theRules[1].style.display=''; theRules[2].style.display='none'; theRules[3].style.display='none'; theRules[4].style.display='none'; theRules[5].style.display='none'; theRules[6].style.display='none'; break; }
    case 'Decks': { theRules[0].style.display='none'; theRules[1].style.display='none'; theRules[2].style.display=''; theRules[3].style.display='none'; theRules[4].style.display='none'; theRules[5].style.display='none'; theRules[6].style.display='none'; break; }
    case 'Login': { theRules[0].style.display='none'; theRules[1].style.display='none'; theRules[2].style.display='none'; theRules[3].style.display=''; theRules[4].style.display='none'; theRules[5].style.display='none'; theRules[6].style.display='none'; break; }
    case 'My Account': { theRules[0].style.display='none'; theRules[1].style.display='none'; theRules[2].style.display='none'; theRules[3].style.display='none'; theRules[4].style.display=''; theRules[5].style.display='none'; theRules[6].style.display=''; break; }
    case 'Packs/Boxes': { theRules[0].style.display='none'; theRules[1].style.display='none'; theRules[2].style.display='none'; theRules[3].style.display='none'; theRules[4].style.display='none'; theRules[5].style.display=''; theRules[6].style.display='none'; break; }
    case 'Track Order': { theRules[0].style.display='none'; theRules[1].style.display='none'; theRules[2].style.display='none'; theRules[3].style.display='none'; theRules[4].style.display='none'; theRules[5].style.display='none'; theRules[6].style.display=''; break; }
  }
}

function windowResized() {
//  top_line.width = document.body.clientWidth;
}
