// JavaScript Document
var actualwin, actualcolor, errorFlag, noWindow;
var resp = "";

function refrsh()
{
	$(".new-window").click(	function(){
									 actualwin = this;
									 $("#dialog-confirm").dialog('open');
									 } );
	
	$(".new-color").click(	function(){
									 actualcolor = this;
									 $("#dialog-color").dialog('open');
									 } );
	
	$(".remove").click( function(){
								 $(this).parents("tr").remove();
								 });
}

function addwindow()
{
	var temp = $("#calc-tab").html();
	temp +=('<tr class="item"><td class="winelement"><div class="new-window"><span class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"><span class="ui-button-text">Zvolte typ okna</span></span></div></td>                <td><input type="text" class="inbox width"/>Šířka                  <br />                  <br />                             <input type="text" class="inbox height" />Výška                  <br />                  <br />                                <input type="text" class="inbox number"/>Počet<br  />                  <div class="wincolor"><span class="new-color"><img src="css/template/images/none.jpg" /></span>Vnější barva             </div></td>                <td><INPUT TYPE="checkBox" class="zaluzie"/>                  Žaluzie<br />                  <INPUT TYPE="checkBox" class="inparapet"/>                  Parapety vnitřní<br />                  <INPUT TYPE="checkBox" class="outparapet"/>                  Parapety vnější<br />                  <INPUT TYPE="checkBox" class="sit"/>                  Sítě<br />                  <INPUT TYPE="checkBox" class="rolety"/>                  Rolety<br />                  </td>                <td><textarea rows="7" cols="17" class="description" ></textarea>                  <br />                  <span class="remove"><a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"><span class="ui-button-text">Zrušit položku</span></a></span></td>              </tr>');

	$("#calc-tab").html(temp);
		var temp1 = $("#calc-tab").html();
	refrsh();
}

function error(msg)
{
	$("#errmsg").text(msg);
	$("#error").show();
	errorFlag = true;
}

function descItem(type, width, height, number, desc, type, color, zaluzie, inParapet, outParapet, sit, rolety)
{
	if(type == null)
		return "";
		
//	number = parseInt(number);
	if(isNaN(number) || number < 1)
	{
		error("Zadejte počet oken.");
		return "";
	}
	
	if(isNaN(width) || width < 300)
	{
		error("Šířka okna musí být větší jak 300mm.");
		return "";
	}
	
	if(isNaN(height) || height < 300)
	{
		error("Výška okna musí být větší jak 300mm.");
		return "";
	}
	
	if(color == "" || color == null)
	{
		error("Vyberte vnější barvu okna.");
		return "";
	}

	noWindow = false;

	var msg = "-----------------------------------------------<br /><br />";
	msg += "Typ okna: " + type + "<br />";
	msg += "Šířka : " + width +" mm<br />";
	msg += "Výška : " + height +" mm<br />";
	msg += "Počet : " + number +" ks<br />";
	msg += "Barva : " + color +"<br />";
	msg += "Žaluzie : " + zaluzie +"<br />";
	msg += "Parapet vnitřní : " + inParapet +"<br />";
	msg += "Parapet vnější : " + outParapet +"<br />";
	msg += "Síť : " + sit +"<br />";
	msg += "Rolety : " + rolety +"<br />";
	msg += "Poznámka : " + desc +"<br />";
	return msg;
}

function parseItem(iitem)
{
	var zaluzie, inParapet, outParapet, sit, rolety;
	
	var width = $(iitem).find(".width").val();
	var height = $(iitem).find(".height").val();
	var number = $(iitem).find(".number").val();
	var desc = $(iitem).find(".description").val();
	var type = $(iitem).find(".winelement img").attr("alt");
	var color = $(iitem).find(".wincolor img").attr("alt");
	
	if($(iitem).find(".zaluzie").is(':checked'))
		zaluzie = "ano";
	else
		zaluzie = "ne";
	
	if($(iitem).find(".inparapet").is(':checked'))
		inParapet = "ano";
	else
		inParapet = "ne";
		
	if($(iitem).find(".outparapet").is(':checked'))
		outParapet = "ano";
	else
		outParapet = "ne";
		
	if($(iitem).find(".sit").is(':checked'))
		sit = "ano";
	else
		sit = "ne";

	if($(iitem).find(".rolety").is(':checked'))
		rolety = "ano";
	else
		rolety = "ne";
		
	
	return descItem(type, width, height, number, desc, type, color, zaluzie, inParapet, outParapet, sit, rolety);
}

function readSrv()
{
	var msg = "<br />Požadované služby:<br />";
	
	if($("#service1").is(':checked'))
		msg += "Demontáž starých oken<br />";
		
	if($("#service2").is(':checked'))
		msg += "Likvidace starých oken<br />";
		
	if($("#service3").is(':checked'))
		msg += "Zednické začištění<br />";
		
	return msg;
}


function readContacs()
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var name = $("#name").val();
	var surname = $("#surname").val();
	var street = $("#street").val();
	var city = $("#city").val();
	var psc = $("#psc").val();
	var tel = $("#tel").val();
	var email = $("#email").val();
	var desc = $("#main-description").val();
	var msg = "";
/*	
	if(name == "")
	{
		error("Zadejte vaše jméno.");
		return "";
	}
	
	if(surname == "")
	{
		error("Zadejte vaše příjmení.");
		return "";
	}
	
	if(city == "")
	{
		error("Zadejte město realizace.");
		return "";
	}
	*/
	if(reg.test(email) == false)
	{
		error("Nekorektně zadaný email.");
		return "";
	}
	
	msg += "<br />Kontaktní údaje:<br />";
	msg += "Jméno a příjmení: "+ name + " " + surname +"<br />";
	msg += "Ulice: "+ street +"<br />";
	msg += "Město: "+ city +"<br />";
	msg += "PSČ: "+ psc +"<br />";
	msg += "Telefon: "+ tel +"<br />";
	msg += "Email: "+ email +"<br />";
	msg += "Doplňující informace: "+ desc +"<br />";
	
	return msg;
}

function parseItems()
{
	var msg = "";
	
	$("#error").hide();
	errorFlag = false;
	
	var items = $(".item").toArray();	
	
    for (var i = 0; i < items.length; i++)
	{
		msg += parseItem(items[i]);
    }
	msg += readSrv();
	msg += readContacs();
	
	if(noWindow && errorFlag == false)
	{
		$("#warnig-nowindow").dialog('open');
	}
	
	if(errorFlag == false && noWindow == false)
	{
		
		var html = $.ajax({
        type: "GET",
        url: "send.php",
        data: "msg="+msg,
        async: false,
		success: function(msgg,rsp){ resp = rsp;}
    	}).responseText;
		
		if(resp == "success" && html == "ok")
			window.location.replace("http://bclasic.cz/odeslano.html");
		else
			window.location.replace("http://bclasic.cz/neodeslano.html");
	}
}


$(document).ready(function(){
		   refrsh();
		   
		   $("#submit").click ( function() {
										 noWindow = true;
										 parseItems();
										 });
		   
   		   $("#script-need").css("display","block");
		   
		   $(".addthis").click( function(){
										 $(actualwin).html($(this).html());
										 $(actualwin).switchClass('new-window','change-window');
										 if($(actualwin).attr("class") != "change-window")
										 	addwindow();
										 $("#dialog-confirm").dialog('close');
										 } );
		
			$(".addcolor").click( function(){
										 $(actualcolor).html($(this).html());
										 $("#dialog-color").dialog('close');
										 } );
		
			$(".change-window").click(	function(){
										 actualwin = this;
										 $("#dialog-confirm").dialog('open');
										 } );		

			$("#dialog-confirm").dialog({
				autoOpen: false,
				resizable: false,
				width:950,
				modal: true
			});
			
			$("#dialog-color").dialog({
				autoOpen: false,
				resizable: false,
				width: 750,
				height: 600,
				modal: true
			});
			
			$("#warnig-nowindow").dialog({
				autoOpen: false,
				resizable: false,
				width: 350,
				modal: true,
				buttons: {
						'Ano': function() {
								$(this).dialog('close');
								noWindow = false;
								parseItems();
						},
						'Ne': function() {
								$(this).dialog('close');
						}
				}
			});
			
			
			$("#error").hide();
	});

