var j = jQuery.noConflict();
j(document).ready(function() {

	j('textarea').focus(function() {$(this).addClass("active");});
    j('textarea').blur(function() {$(this).removeClass("active");});
    j('input[type="text"]').focus(function() {$(this).addClass("active");});
    j('input[type="text"]').blur(function() {$(this).removeClass("active");});

	// selecty
	j(".selectbox").selectbox();

	// tooltipy
	j('.jtooltip').tooltip({ 
        track: true,
        delay: 0, 
        showURL: false,
        showBody: " - ", 
        id: "jtooltip" 
    });    
			
	// metoda ukrywa tooltipy z bledami	
	function hideTooltipsError()
	{
		j(".tooltipError").hide();
		j(".tooltipError").remove();
	}
	
	// metoda wyswietla tooltip z bledem
	function showTitle(obj)
	{
		hideTooltipsError();
		var title = "";
		if(obj.hasClass("jquery-custom-selectboxes-replaced"))
			title = obj.children("select").attr("title");
		else
			title = obj.attr("title");
		if (title.length > 0)
		{
			var parent = obj.parent();
			parent.css("position","relative");
			parent.append('<div class="tooltipError"><div class="content">' + title + '</div></div>');
			j(".tooltipError").fadeIn();
			var objectHeight = obj.height();
			var tooltipHeight = j(".tooltipError .content").height();
			var posTop = 0;
			//if (obj.hasClass("requiredDDL"))
				posTop = objectHeight + tooltipHeight + 20;
			//else 
			//	posTop = objectHeight + tooltipHeight + 5;
			var objectWidth = obj.parent().width();
			j(".tooltipError").css("top","-" + posTop );
			var posLeft = objectWidth - 120;
			if (posLeft < 0)
				posLeft = 10;
			j(".tooltipError").css("left", posLeft);
			
		}
	}		
	
	// metoda sprawdza czy pole jest zwalidowane
	function val(obj)
	{
		if (obj.hasClass("phone"))
		{
			var value = obj.attr("value");
			if (value.match('^[0-9]'))
			{
				if (value.length > 10)
				{
					longPhone = 1;
					return false;
				}
				else
				{
					longPhone = null;
					return true;	
				}
			}
			else
				return false;
		}
		if (obj.hasClass("email"))
		{
			var value = obj.attr("value");
			if (value.match('[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}'))
				return true;	
			else
				return false;
		}
		if (obj.hasClass("calendar"))
		{
			var value = obj.attr("value");
			if (value.match('[0-9]{4}-[0-9]{2}-[0-9]{2}'))
				return true;	
			else
				return false;
		}
		if (obj.hasClass("year"))
		{
			var value = obj.attr("value");
			if (value.match('^[0-9]{4}'))
				return true;	
			else
				return false;
		}
		if (obj.hasClass("selectbox"))
		{
			var value = obj.attr("value");
			if (value != null)
			{
				if (value == "-1" || value.trim() == 'wybierz'|| value.trim() == '')
					return false;
				else
					return true;	
			}
		}
		if (obj.hasClass("jquery-custom-selectboxes-replaced"))
		{
			var value = obj.children("select").attr("value");
			if (value != null)
			{
				if (value == "-1" || value.trim() == 'wybierz'|| value.trim() == '')
					return false;
				else
					return true;	
			}
		}
		else
		{
			var content = obj.attr("value").trim().length;
			if (content == 0)
				return false;	
			else
				return true;	
		}
	return false;
	}	
	
	// zdarzenia dla required
	j('.required').each(function() {
		j(this).keyup(function(){
			var valid = val(j(this));
			if (!valid)
			{
				if (!j(this).parent().children("tooltipError"))
					showTitle(j(this));
				j(this).addClass("error");
			}
			else
			{
				j(".tooltipError").hide();
				j(".tooltipError").remove();
				j(this).removeClass("error");
			}
		});	
		j(this).blur(function(){
			var valid = val(j(this));
			if (!valid)
			{
				showTitle(j(this));
				j(this).addClass("error");
			}
			else
			{
				j(".tooltipError").hide();
				j(".tooltipError").remove();
				j(this).removeClass("error");
			}
		});	
	});			
	
	// year
	j(".requiredYear").each(function() {
		j(this).keyup(function(){
			var valid = val(j(this));
			if (!valid)
			{
				if (!j(this).parent().children("tooltipError"))
					showTitle(j(this));
				j(this).addClass("error");
			}
			else
			{
				j(".tooltipError").hide();
				j(".tooltipError").remove();
				j(this).removeClass("error");
			}
		});	
		j(this).blur(function(){
			var valid = val(j(this));
			if (!valid)
			{
				showTitle(j(this));
				j(this).addClass("error");
			}
			else
			{
				j(".tooltipError").hide();
				j(".tooltipError").remove();
				j(this).removeClass("error");
			}
		});	
	});

	// zdarzenia dla emailValidator
	j(".emailValidator").each(function() {
		j(this).blur(function(){
			var valid = val(j(this));
			if (!valid)
			{
				if (j(this).val() != '')
				{
					j("#emailError").show();
					j("#emailError1").hide();
					j("#contactError").hide();
				}
				else
				{
					j("#emailError1").show();
					j("#emailError").hide();
					j("#contactError").hide();
				}
			}
			else
			{
				j("#emailError").hide();
				j("#emailError1").hide();
			}
		});	
	});
	
	// zdarzenia dla phoneValidator
	j(".phoneValidator").each(function() {
		j(this).blur(function(){
			var valid = val(j(this));
			if (!valid)
			{
				
				if (j(this).val().length > 10)
				{
					j("#contactErrorPhone").hide();
					j("#contactErrorPhone1").hide();
					j("#contactError").hide();
					j("#contactErrorPhoneLong").show();
					return;
				}
				else if (j(this).val() != '')
				{
					j("#contactErrorPhone").show();
					j("#contactErrorPhone1").hide();
					j("#contactError").hide();
					j("#contactErrorPhoneLong").hide();
					return;
				}
				else
				{
					j("#contactErrorPhone1").show();
					j("#contactErrorPhone").hide();
					j("#contactError").hide();
					j("#contactErrorPhoneLong").hide();
					return;
				}
			}
			else
			{
				j("#contactErrorPhone").hide();
				j("#contactErrorPhone1").hide();
				j("#contactErrorPhoneLong").hide();
				return;
			}
		});	
	});

	// zdarzenia dla drop downow
	j('.requiredDDL').each(function() {
		j(this).change(function(){
			var valid = val(j(this));
			if (!valid)
			{
				showTitle(j(this));
				j(this).parent().addClass("error");
			}
			else
			{
				j(".tooltipError").hide();
				j(".tooltipError").remove();
				j(this).parent().removeClass("error");
			}
		});
	});
		
	// METODA TYLKO DLA UBRANYCH DROPOW
	// jazda probna - extra valid: godzina, kontakty
	// indywidualna oferta - extra valid: rok produkcji, email i telefon
	// metoda wykonywana na submit, sprawdza czy wszystko sie waliduje, wyswietla komunikaty tylko pod polem + pokolorowanie na czerwono
	j('.btnButtonSend').click(function() {
		hideTooltipsError();
		var valid = false;
		var validCar = false;
		var validDealer = false;
		var validExtraParam = false;
		if (j(this).hasClass("btnButtonSendJP")) // jazda probna - walidujemy godzine oraz grupe kontaktowa
		{
			// godzina
			j('.requiredHour').each(function() {
				validExtraParam = val(j(this));
				if (!validExtraParam)
				{
					j(this).parent().addClass("error");
					j('#hourError').show();
				}
				else
				{
					j(this).parent().removeClass("error");
					j('#hourError').hide();
				}
			});
			// kontakt
			j('#contactValidator').each(function() {
				validExtraParam = false;
				j('#contactValidator input').each(function() {
					var validation = val(j(this));
					if (validation)
						validExtraParam = true;
				});	
				if (!validExtraParam)
				{
					j('#contactError').show();
					j('#contactValidator input').each(function() {
						j(this).addClass("error");
					});
				}
				else
				{
					j('#contactError').hide();
					j('#contactValidator input').each(function() {
						j(this).removeClass("error");
					});	
				}
			});
		}
		if (j(this).hasClass("btnButtonSendIO")) // indywidualna oferta - walidujemy rok produkcji
		{
			if (j("#pnlOldCar").css("display") == 'none')
			{
				requireYear = false;
				validExtraParam = true;
			}
			else
			{
				requireYear = true;
				validExtraParam = false;
			}
			if (requireYear)
			{
				validExtraParam = val(j(".requiredYear"));
			}
			if (!validExtraParam)
				j(".requiredYear").parent().next(".commentError").show();
			else
				j(".requiredYear").parent().next(".commentError").hide();
		}
		// reszta inputow
		j('.required').each(function() {
			valid = val(j(this));
			if (!valid)
			{	
				j(this).addClass("error");
				j(this).parent().next(".commentError").show();
				if (j(this).hasClass("emailValidator"))
				{
					if (j(this).val() != '')
					{
						j("#emailError").show();
						j("#emailError1").hide();
					}
					else
					{
						j("#emailError1").show();
						j("#emailError").hide();
					}
				}
				if (j(this).hasClass("phoneValidator"))
				{
					if (j(this).val() != '')
					{
						j("#contactErrorPhone").show();
						j("#contactErrorPhone1").hide();
					}
					else
					{
						j("#contactErrorPhone1").show();
						j("#contactErrorPhone").hide();
					}
				}
				if (j("#contactErrorPhone1").css("display") == "block" || j("#contactErrorPhone").css("display") == "block" ||
						j("#emailError").css("display") == "block" || j("#emailError1").css("display") == "block")
					j("#contactError").hide();
			}
			else
			{
				j(this).removeClass("error");
				j(this).parent().next(".commentError").hide();
			}
		});
		// grupa dropow samochod
		j('#samochodValidator select').each(function() {
			validCar = val(j(this));
			if (!validCar)
			{
				j(this).parent().addClass("error");
				j('#carError').show();
			}
			else
			{
				j(this).parent().removeClass("error");
				j('#carError').hide();
			}
		});
		// grupa dropow dealer
		j('#dealerValidator select').each(function() {
			validDealer = val(j(this));
			if (!validDealer)
			{
				j(this).parent().addClass("error");
				j('#dealerError').show();
			}			
			else
			{
				j(this).parent().removeClass("error");
				j('#dealerError').hide();
			}
		});
		if (valid && validDealer && validCar && validExtraParam)
			j('.btnSend').submit();
		else
			return false;
	});

	
	// agree tooltip
	j(".agreeContent .close").css("cursor","pointer");
	j(".agreeContent .close").click(function(){
			j(".agreeContent").css("display","none");
	});
	
	j(".tooltipWithAgree").css("cursor","pointer");
	j(".tooltipWithAgree").click(function(){
		if (j(".agreeContent").css("display") == 'block')
			j(".agreeContent").css("display","none");
		else
			j(".agreeContent").css("display","block");

	});
	
	
	// metoda uzupelnia dane samochodu
	j(".model select").each(function() {
		j(this).change(function() {
		
		// wyczyscic wsio
		var select = j(".nadwozie select");
		j(".nadwozie .jquery-custom-selectboxes-replaced").remove();
		j(".nadwozie").append(select);
		select.show();
		j(".nadwozie select option").remove();
		var select = j(".version select");
		j(".version .jquery-custom-selectboxes-replaced").remove();
		j(".version").append(select);
		select.show();
		j(".version select option").remove();

		id = document.ankieta.model[document.ankieta.model.selectedIndex].value;
		if (id == "-1") {
			j(".nadwozie select").attr("disabled","disabled");
			j(".version select").attr("disabled","disabled");
			j(".nadwozie select").selectbox();
			j(".version select").selectbox();
			return;
		}
		else {
			j(".nadwozie select").attr("disabled","");
			var option = document.createElement('option');
			option.value = "-1";
			option.appendChild(document.createTextNode("wybierz..."));
			j(".nadwozie select").append(option);
			var versions = getVersions(id);
			for (i in versions) {
				if (tNadwozie[versions[i]] != undefined)
				{
					option = document.createElement('option');
					option.value = versions[i];
					option.appendChild(document.createTextNode(tNadwozie[versions[i]]));
					j(".nadwozie select").append(option);
				}
			}
			j(".nadwozie select").selectbox();
			j(".version select").selectbox();
			j(".nadwozie select").each(function() {
				j(this).change(function() {
					// wyczyscic wsio
					var select = j(".version select");
					j(".version .jquery-custom-selectboxes-replaced").remove();
					j(".version").append(select);
					select.show();
					j(".version select option").remove();
					
					id = document.ankieta.model[document.ankieta.model.selectedIndex].value;
					id2 = document.ankieta.nadwozie[document.ankieta.nadwozie.selectedIndex].value
					if (id2 == "-1") {
						j(".version select").attr("disabled","disabled");
						j(".version select").selectbox();
						return;
					}
					else {
						j(".version select").attr("disabled","");
						var option = document.createElement('option');
						option.value = "-1";
						option.appendChild(document.createTextNode("wybierz..."));
						j(".version select").append(option);
						
						var engines = getVersionEngines(id,id2);
						for (i in engines) {
							if (tSilnik[engines[i]] != undefined)
							{
								option = document.createElement('option');
								option.value = engines[i];
								option.appendChild(document.createTextNode(tSilnik[engines[i]]));
								j(".version select").append(option);
							}
						}
						j(".version select").selectbox();
						
					}
					});
				});	

		}
		
		});
	});	
	
	
	
	
	
});


