/*
 * abtours.ru
 */

function popimg( s, w, h, t )
{
	var WADD = 28;
	var HADD = 24;

	if( w == null ) w = 800;
	if( h == null ) h = 600;
	 
	if( t ) HADD += 13;

	var wnd = window.open( "", "_blank", "width="+(w+WADD)+",height="+(h+HADD)
		+",location=no,menubar=no,status=no,toolbar=no,resize=no");
	var d = wnd.document; d.open();
	d.write("<html><head><title>"+window.popupTitle+"</title></head>" );
	d.write("<body style='padding: 0px; margin: 0px; background-color: #fff;'>");
	d.write("<div style='text-align: center; padding: 10px; margin: 0px;'>" );
	d.write("<img src=\""+s+"\" width="+w+" height="+h+" border=1 alt=\"\" onclick='window.close()' />" );
	if( t ) 
		d.write("<div style=\"font: bold 12px Verdana, sans-serif;"+
			" padding-top: 3px; color: #336666; letter-spacing: 1px;\">"
			+t+"</div>"); 
	d.write("</div></body></html>"); 
	d.close(); wnd.focus(); return false;
}

$(function(){
	var reqdiv = $("div.request");
	reqdiv.append(
		"<div class=\"btn_request\">Сделать заказ</div><div class=\"clrr\"></div>"
	);
	$("div.btn_request").click(function(){
		if( reqdiv.data("form") ) { 
			reqdiv.children("form").remove();
			reqdiv.data("form", false);
			return; 
		}
		reqdiv.data("form", true);
		var frm = $('<form id="frm_request" method="POST"/>')
			.append(
				$('<input type="hidden" id="fld_tour" name="tour"/>').val(reqdiv.data("tour"))
			)
			.append(
				'<label for="fld_cont">Контактное лицо, телефон или e-mail</label>'+
				'<input type="text" name="cont" id="fld_cont" value="" />'+
				'<label for="fld_note">Сроки заезда, количество человек, вопросы и пожелания</label>'+
				'<textarea rows=5 cols=40 name="note" id="fld_note"></textarea>'
			)
			.append(
				"<button>Отправить</button>"
			);
		frm.submit(function(){
			if( $(this).data("sent") ) { return false; }
			var data = { 'tour': $("#fld_tour").val(),
  						 'cont': $("#fld_cont").val(),
  						 'note': $("#fld_note").val() }; 
  			if( !data.cont || data.cont.length < 6 ) {
  				$("#fld_cont").focus();
  				return false;
  			}
  			$("#frm_request").data("sent", true );
  			$("#frm_request button").attr("disabled", "1");  			
			$.ajax({ 
				type: 'POST', url: '/send', data: data, dataType: 'json',
				success: function(rc){
					if( rc.ok ) {
						alert("Ваша заявка отправлена.");
					}
					else {
						alert("Ошибка при отправке!"); 
					}
				},
				error: function() { alert("Ошибка сервера!"); }
			});			
			return false;
		});
		reqdiv.append(frm);
	});
});

/* eof */

