//
// Javascript functions to handle everything with dynamic stuff within a page.
// @author bjoern
//

/**
 * Checks whether an input form field has a content. 
 * @param field {String} Id of the form field to check
 * @returns {Boolean} <code>TRUE</code> if a value is available, <code>FALSE</code> otherwise
 */
function checkFormVar(field) {
  if (document.getElementById(field).value == null) {
    return false;
  }
  return true;
}

/**
 * Opens a Check-Dialog to the user, so he can check whether to follow the link or not.
 * The action follows the link in case of Ok or closes the dialog in case of Cancel.
 * @param aId HTML {String} Id of the a-tag
 * @param wintitle {String} Title of the window to show
 * @param content {String} Content to show
 * @param okText {String} Text of the OK-button
 * @param cancelText {String} Text of the Cancel-button
 */
function checkAction(aId, wintitle, content, okText, cancelText) {
  var url = document.getElementById(aId).getAttributeNode("href").value;
  var $dialog = $('<div></div>')
    .html(content)
    .dialog({
      autoOpen: false,
      modal: true,
      title: wintitle,
      buttons: [{
        text: okText,
        click: function(){
          $dialog.dialog('close');
          window.location.href = url;
          return true;
        }
      }, {
        text: cancelText,
        click: function(){
          $dialog.dialog('close');
          return false;
        }
      }]
    });
  $dialog.dialog('open');
}
/**
 * Opens a Check-Dialog to the user, so he can check whether to follow the link or not.
 * The action follows the link in case of Ok or closes the dialog in case of Cancel.
 * @param wintitle {String} Title of the window to show
 * @param content {String} Content to show
 */
function viewAction(wintitle, content) {
  var $dialog = $('<div></div>')
    .html(content)
    .dialog({
      autoOpen: false,
      modal: true,
      closeText:"",
      title: wintitle,
      resizable: true,
      draggable: true,
      width: 'auto',
      close: function() {
        $(this).dialog('destroy').empty();
      }
    });
  $dialog.dialog('open');
}

/**
 * Opens a Check-Dialog to the user to show static info
 * @param wintitle {String} Title of the window to show
 * @param content {String} Content to show
 */
function showInfoAction(wintitle, content) {
  var $dialog = $('<div></div>')
    .html(content)
    .dialog({
      autoOpen: false,
      modal: true,
      closeText:"",
      title: wintitle,
      resizable: true,
      draggable: true,
      width: 'auto',
      close: function() {
        $(this).dialog('destroy').empty();
      }
    });
  $dialog.dialog('open');
}

/**
 * Opens a Form-Dialog to get decline reason
 * @param wintitle {String} Title of the window to show
 */
function showFormDecline(aId, wintitle, text, okText, cancelText, betId ) {
  var url = document.getElementById(aId).getAttributeNode("href").value;
  var content = '<div id="dialog-form" title="Decline">'+
  '<p >'+text+'</p>'+
  '<form method="post" id="declineForm" enctype="application/x-www-form-urlencoded" >'+
    '<textarea name="declineReason" id="declineReason" size="50" rows="3" cols="40"></textarea>'+
    '<input type="hidden" name="bet_id" id="bet_id" value="'+betId+'" /><br/>'+
    '<input type="hidden" name="action" value="'+okText+'" /><br/>'+
  '</form>'+
'</div>';
  var $dialog = $('<div></div>')
    .html(content)
    .dialog({
      autoOpen: false,
      modal: true,
      closeText:"",
      title: wintitle,
      resizable: true,
      draggable: true,
      width: 'auto',
      buttons: [{
        text: okText,
        click: function(){
         document.forms["declineForm"].submit();
          $dialog.dialog('close');
       //   window.location.href = url;
          return false;
        }
      },{text: "Cancel",
        click: function(){
          $dialog.dialog('close');
          return false;
        }
      }],
      close: function() {
      $(this).dialog('destroy').empty();
  }
    });
  $dialog.dialog('open');
}

/**
 * Switch an image tag's image to a different image.
 * @param imgId {String} Id of the image tag
 * @param imgSrc {String} Image location to set
 */
function switchImage(imgId, imgSrc) {
  document.getElementById(imgId).getAttributeNode("src").value = imgSrc;
}

