function hideAlert() {
   $('#dialog').dialog('close');
   document.getElementById('ALERT').innerHTML='';
}

function vote(way,id){

        $.ajax({
        type:'GET',
        url:'/vote.php',
        data:{'way':way,'id':id},
        async:false,
        success:function(data) {
          if ( data.indexOf('ERROR') != -1 ) {
            alertJQ('Error','Sorry, but we had some difficulty, you probably already voted on this.');
          } else {
            alertJQ('Success','Thanks for your vote!');
            setTimeout(hideAlert,1000);
            _('post_'+way+'_'+id).innerHTML = data;
          }
        },
        error:function(requestObj,statusStr,errorObj) {
          alertJQ('Communication Error',statusStr);
        },
        timeout:30000
        });


}


function checkComment ( text_id ) {

  if ( document.getElementById('nickname_'+text_id).value.length < 2 ) {
    alertJQ('Error','Enter your nickname');
    return false;
  }

  if ( document.getElementById('comment_'+text_id).value.length < 2 ) {
    alertJQ('Error','Enter your comment');
    return false;
  }

  if ( document.getElementById('code_'+text_id).value.length < 4 ) {
    //alertJQ('Error','Enter the security code');
    //return false;
  }

  return true;

}

function _ ( id ) {

  return document.getElementById(id);

}

function alertJQ ( title, body, w, h ) {

  var append = '<a href="javascript:void(null);" onclick="location.reload()"> Refresh Page? </a>';

  if ( body == null ) { body = title; title = 'Alert'; append = '';}

  body = replaceChars(body,'\n','<br />');

  //$.ui.dialog.defaults.bgiframe = true;

  var style = "";

  var options = {modal:true};

  if ( w != null && h != null ) {

    options = {
      height: h, width: w, modal:true,
      close: function ( e, ui ) {
        $(this).dialog('destroy').remove();
      }
    };

    append = '';

  }

  append = '';

  var alertTxt = '<div id="dialog" title="'+title+'"><p style="'+style+'" id="dialog_content">'+body+'</p>'+append+'</div>';

  $('#ALERT').html(alertTxt);

  if ( $('#dialog') == null ) {

    alert(title + ' \n\n' + body);

    return true;

  }

  $('#dialog').dialog(options);

}

function replaceChars(entry,x,y) {
out = x; // replace this
add = y; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
return temp;
}



