function addSymbolCost(s) {
  var the_casting_cost = document.the_form_name__generate_magic_card.CardCastingCost;
  the_casting_cost.value = the_casting_cost.value + "[" + s + "]";
}
function addSymbolText(s) {
  var the_card_text = document.the_form_name__generate_magic_card.CardText;
  the_card_text.value = the_card_text.value + "[" + s + "]";
}

function cardTypeSelected(card_type) {
  document.getElementById('h__power_toughness').style.display = 'none';
  switch(card_type) {
    case 'Artifact Creature': { }
    case 'Creature': { }
    case 'Legendary Artifact Creature': { }
    case 'Legendary Creature': { }
    case 'Snow Artifact Creature': { }
    case 'Snow Creature': {
      document.getElementById('h__power_toughness').style.display = '';
      break;
    }
  }
}

function checkImageSize(the_image) {
  if(document.the_form_name__generate_magic_card.CardStyle.value == 'new') {
    new Cropper.Img('image_to_crop', { ratioDim: { x: 276, y: 202 }, displayOnInit: true, onEndCrop: setCropAnchors });
  }
  else {
    new Cropper.Img('image_to_crop', { ratioDim: { x: 255, y: 207 }, displayOnInit: true, onEndCrop: setCropAnchors });
  }
}

function loadCardImageBefore() {
  var the_contents = '<IMG SRC="" id="image_to_crop" class="image_to_crop" width="380" onLoad="checkImageSize(this);">';
  document.getElementById('image_crop_container').innerHTML = the_contents;
  return true;
}

function loadCardImage(the_form) {
  AIM.submit(the_form, {'onStart':loadCardImageBefore, 'onComplete':loadCardImageAfter});
  the_form.submit();
  return false;
}

function loadCardImageAfter(response) {
  document.getElementById('image_crop_container').style.display = '';
  var the_image = document.getElementById('image_to_crop');
  the_image.src = response;
  entry = response.split('/');
  document.the_form_name__generate_magic_card.ImageName.value = entry[entry.length-1];
}

function modifyCard() {
  ajaxSubmit('POST', 'generators/magic/reset_card_image_session.php', true, null);
  document.getElementById('h__status_message').style.display='none';
  document.getElementById('form_container').style.display='';
  document.getElementById('generated_card_container').style.display='none';

}

function onSubmitCreateCard() {
  document.body.style.cursor = 'wait';
  var message = 'You must enter the name of your card before you may continue.';
  if(isBlank(document.the_form_name__generate_magic_card.CardName.value)) { resetCursor(); alert(message); return false; }
  var message = 'You must select either a classic or modern style for your card before you may continue.';
  if(isBlank(document.the_form_name__generate_magic_card.CardStyle.value)) { resetCursor(); alert(message); return false; }
  var message = 'You must specify a color for this card before you may continue.';
  if(isBlank(document.the_form_name__generate_magic_card.CardColor.value)) { resetCursor(); alert(message); return false; }
  var message = 'Please select a rarity for this card before continuing.';
  if(isBlank(document.the_form_name__generate_magic_card.CardRarity.value)) { resetCursor(); alert(message); return false; }
  var card_type = document.the_form_name__generate_magic_card.CardType.value
  var message = 'You must select a type for your card before you may continue.';
  if(isBlank(card_type)) { resetCursor(); alert(message); return false; }
  switch(card_type) {
    case 'Artifact Creature': { }
    case 'Creature': { }
    case 'Legendary Artifact Creature': { }
    case 'Legendary Creature': { }
    case 'Snow Artifact Creature': { }
    case 'Snow Creature': {
      var message = 'You must specify this creature\'s power and toughness before you may continue.';
      if(isBlank(document.the_form_name__generate_magic_card.CardPower.value) || isBlank(document.the_form_name__generate_magic_card.CardToughness.value)) { resetCursor(); alert(message); return false; }
      break;
    }
  }
  document.getElementById('h__status_message').style.display = 'none';
  ajaxSubmit('POST', 'generators/magic/generate_card.php', true, ajaxFormatURL('the_form_name__generate_magic_card'));
  return false;
}

function registerForAccount() {
  document.location.href= webpage_url + 'New-Account-Signup.html';
}

function resetCursor() {
  document.body.style.cursor = 'default';
}

function saveCard() {
//  ajaxSubmit('POST', 'save_card.php', true, 'type=magic');
  ajaxSubmit('POST', 'save_card.php', true, ajaxFormatURL('the_form_name__generate_magic_card') + '&type=magic');  
}

function setCropAnchors(coords, dimentions) {
  document.the_form_name__generate_magic_card.image_x1.value = coords.x1;
  document.the_form_name__generate_magic_card.image_x2.value = coords.x2;
  document.the_form_name__generate_magic_card.image_y1.value = coords.y1;
  document.the_form_name__generate_magic_card.image_y2.value = coords.y2;
}

function styleChanged(the_style) {
  if(document.getElementById('image_to_crop') != null) {
    var the_contents = '<IMG id="image_to_crop" class="image_to_crop" width="380" onLoad="checkImageSize(this);" SRC="' + document.getElementById('image_to_crop').src + '">';
    document.getElementById('image_crop_container').innerHTML = the_contents;
  }
}

