var response_name, reponse_contacts;
function toggle_form(id) {
  //$("#"+id).toggle("slide", {}, 1000);
  if($("#"+id).css("display") == 'none') {
    $("#"+id).fadeIn("slow")[0].scrollIntoView(true);
  } else {
    $("#"+id).fadeOut("slow");
  }
  /*
  if($("#"+id).css("visibility") == 'hidden') {
    $("#"+id).css({'visibility':'visible'});
  } else {
    $("#"+id).css({'visibility':'hidden'});
  }
  */		
}

function send_help(request_id) {
  init_form();
  var name = $("#response_name");
  var desc = $("#response_description");
  var contacts = $("#response_contacts");
  $.post("/modules/ajax/send_help.php", {request_id:request_id, name:encodeURIComponent($.trim(name.attr("value"))), desc:encodeURIComponent($.trim(desc.val())), contacts:encodeURIComponent($.trim(contacts.val()))}, function(responseXML){
	process(responseXML);
  })
}

function process(xmlDoc) {
  statusOp = xmlDoc.getElementsByTagName('status')[0];
  statusMsg = statusOp.firstChild.nodeValue;
  if(statusMsg == 'ok') {
    msg = $('<div class="success">Спасибо, ваше предложение будет направлено нуждающемуся</div>');
    $("#response_block").html(msg);
  } else {
    errMsg = xmlDoc.getElementsByTagName('desc');
    errMsgLength = errMsg.length;
    for(var i =0;i<errMsgLength;i++) {

      desc = errMsg[i].firstChild.nodeValue;

      if(desc == 'no_name') {
	  $("#response_name").css({border: "1px solid #FFCC00", backgroundColor: "#FFFCCF"});
	  $("#response_name_status").html("заполните поле");
      } else if(desc == 'no_contacts') {
	  $("#response_contacts").css({border: "1px solid #FFCC00", backgroundColor: "#FFFCCF"});
	  $("#response_contacts_status").html("заполните поле");
      }
    }
  }
}

function init_form() {
  response_name = $("#response_name").css({border:"", backgroundColor:"", color:""});
  reponse_contacts = $("#response_contacts").css({border:"", backgroundColor:"", color:""});
  $("#response_name_status").html("");
  $("#response_contacts_status").html("");
}

function get_answer(xmlDoc) {
  statusOp = xmlDoc.getElementsByTagName('status')[0];
  statusMsg = statusOp.firstChild.nodeValue;
  if(statusMsg == 'ok') {
    $("#hsend").attr("disabled", "disabled");
    $("#hmessage").html('<font color="#4c4c4c">Ваше сообщение отправлено</font>');
  } else {
    errMsg = xmlDoc.getElementsByTagName('desc');
    errMsgLength = errMsg.length;
    if(errMsgLength > 0) {
        $("#hmessage").append('<font color="#FF0000">Необходимо заполнить все поля!</font>');
    }
//    for(var i =0;i<errMsgLength;i++) {
//
//      desc = errMsg[i].firstChild.nodeValue;
//      if(desc == 'no_name') {
//	desc = 'Не указано имя нуждающегося';
//      } else if(desc == 'no_desc') {
//	desc = 'Не указано описание проблемы';
//      } else if(desc == 'no_mail') {
//	desc = 'Не указаны контакты';
//      }
//      desc = '<font color="#FF0000">' + desc + '</font>&nbsp;';
//
//      $("#hmessage").append(desc);
//    }
  }
}

function send_request() {
  var name = $("#hname");
  var desc = $("#hdesc");
  var mail = $("#hmail");
  $("#hmessage").html("");
  $.post("/modules/ajax/send_help.php?type=mail", {name:encodeURIComponent($.trim(name.attr("value"))), desc:encodeURIComponent($.trim(desc.val())), mail:encodeURIComponent($.trim(mail.attr("value")))}, function(responseXML){
	get_answer(responseXML);
  })
}
