/**
 * Metoda do aktualizacji listy statusów na rynku pracy, powiatów oraz notki ODO
 */
$("document").ready(
		function() {
			
			// przeniesienie kontenera błędów do layoutu
			if( $("#errorContainer").length > 0 ) {
				$("#errorRow").html( $("#errorContainer") );
				$("#errorRow").show();
			}		
			
			if(!$("#isBeforePefs").length > 0)
			{
				$("#wh-bg01").hide();
				$("#wh-bg02").hide();
				$("#wh-bg03").hide();
			}
			

			if ($("input[@name='companySize']:checked").val() != null) {
				if ($("input[@name='companySize']:checked").val() < 4) {
					$("#conditions").show();
					$("#conditions2").hide();
					$("#conditions3").hide();
					showCompanyFields(true);
					if($("#isBeforePefs").length > 0) {
						$("#gbWhite01").css("top","893px");
						$("#gbWhite01").css("height","472px");
					} else {
						$("#gbWhite01").css("top","973px");
						$("#gbWhite01").css("height","432px");
					}
					$("#gbWhite02").css("top","338px");
					$("#gbWhite02").css("height","993px");
				} else if ( $("input[@name='companySize']:checked").val() == 5 ) {
					$("#conditions").hide();
					$("#conditions2").hide();
					$("#conditions3").show();
					showCompanyFields(true);
					if($("#isBeforePefs").length > 0) {
						$("#gbWhite01").css("top","893px");
						$("#gbWhite01").css("height","472px");
					} else {
						$("#gbWhite01").css("top","973px");
						$("#gbWhite01").css("height","432px");
					}
					$("#gbWhite02").css("top","338px");
					$("#gbWhite02").css("height","993px");
				} else { // DG
					$("#conditions").hide();
					$("#conditions2").show();
					$("#conditions3").hide();
					showCompanyFields(false);
					if($("#isBeforePefs").length > 0) {
						$("#gbWhite01").css("top","589px");
						$("#gbWhite01").css("height","412px");
					} else {
						$("#gbWhite01").css("top","650px");
						$("#gbWhite01").css("height","372px");
					}
					$("#gbWhite02").css("top","338px");
					$("#gbWhite02").css("height","763px");
				}
			}
			$("input[@name='companySize']").click(
					function() {
						if ($(this).val() < 4) { 
							$("#conditions").show();
							$("#conditions2").hide();
							$("#conditions3").hide();
							showCompanyFields(true);
							
							$("#gbWhite02").css("top","338px");
							$("#gbWhite02").css("height","993px");
							
						} else if ($(this).val() == 5){ // duża
							$("#conditions").hide();
							$("#conditions2").hide();
							$("#conditions3").show();
							showCompanyFields(true);
							$("#gbWhite02").css("top","338px");
							$("#gbWhite02").css("height","993px");
						} else { // DG
							$("#conditions").hide();
							$("#conditions2").show();
							$("#conditions3").hide();
							showCompanyFields(false);
							
							$("#gbWhite02").css("top","338px");
							$("#gbWhite02").css("height","763px");
						}

						$.getJSON("/platform/enrolment/update-occupations", {
							occupations :$(this).val()
						},
								function(j) {
									var options = '';
									for ( var i = 0; i < j.length; i++) {
										options += '<option value="'
												+ j[i].occupationId + '">'
												+ j[i].occupationStatus
												+ '</option>';
									}

									if ($("#occupationStatus").html()
											.toLowerCase() != options
											.toLowerCase()) {
										$("#occupationStatus").html(options);
										$("#acceptConditions").removeAttr(
												"checked");
									}

								});
						if($("#isBeforePefs").length > 0)
						{
							if($(this).val() != 4) {
								$("#fSSLabel02").html('<span class="title">Dane zapisowe</span><br/><br/>Te dane były podawane przy zapisie i możesz je zmienić.<br/>Musisz też uzupełnić pole nt. swojego statusu na rynku pracy oraz podać pełną nazwę i NIP Twojej firmy.');
								
								$("#gbWhite01").css("top","893px");
								$("#gbWhite01").css("height","452px");

							}
							if($(this).val() == 4) {
								$("#fSSLabel02").html('<span class="title">Dane zapisowe</span><br/><br/>Te dane były podawane przy zapisie i możesz je zmienić. Musisz też uzupełnić pole nt. swojego statusu na rynku pracy.');
								
								$("#gbWhite01").css("top","589px");
								$("#gbWhite01").css("height","412px");

							}
						} else {
							if($(this).val() != 4) {
								$("#fSSLabel02").html('<span class="title">Dane zapisowe</span><br/><br/>Te dane były podawane przy zapisie i możesz je zmienić.<br/>Musisz też uzupełnić pole nt. swojego statusu na rynku pracy oraz podać pełną nazwę i NIP Twojej firmy.');
								
								$("#gbWhite01").css("top","973px");
								$("#gbWhite01").css("height","432px");
								
								$("#fSSLabel03").css("top","500px");
								$("#fSSLabel04").css("top","570px");
							} 
							
							if($(this).val() == 4) {
								$("#fSSLabel02").html('<span class="title">Dane zapisowe</span><br/><br/>Te dane były podawane przy zapisie i możesz je zmienić. Musisz też uzupełnić pole nt. swojego statusu na rynku pracy.');
								
								$("#gbWhite01").css("top","650px");
								$("#gbWhite01").css("height","372px");
							}
						}
					});
		});

/**
 * Metoda do aktualizacji listy powiatów
 */
$("document").ready(
		function() {

			$("#userDistrict").change(
					function() {
						$.getJSON("/platform/enrolment/update-sub-districts", {
							district :$(this).val()
						}, function(j) {
							var options = '';
							for ( var i = 0; i < j.length; i++) {
								if(i > 0 && j[i].sub_district_name.indexOf('m. ') == -1 && j[i-1].sub_district_name.indexOf('m. ') >= 0 ) 
									options += '<option value="0" disabled="disabled">&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;</option>';						
								
								options += '<option value="' + j[i].id + '">'
										+ j[i].sub_district_name + '</option>';
							}
							$("#userSubDistrict").html(options);
						})
					})
		});

// dane firmy włącz wyłącz
var showCompanyFields = function(show) {
	if (show) {
		$("#companyNip").removeAttr("disabled");
		$("#companyFullName").removeAttr("disabled");
		$("#companyStreetNumber").removeAttr("disabled");
		$("#companyCity").removeAttr("disabled");
		$("input[@name='companyCityId']").removeAttr("disabled");
		$("#companyPostCode").removeAttr("disabled");
		$("#companyDistrict").removeAttr("disabled");

//		if ($("#companyStreet").val() != '')
			switchField(true, 'companyStreet');
//		if ($("#companyAdditionalStreetNumber").val() != '')
			switchField(true, 'companyAdditionalStreetNumber');
		if ($("#companyPhone").val() != '')
			switchField(true, 'companyPhone');
		if ($("#companyEmail").val() != '')
			switchField(true, 'companyEmail');

		$("#fieldset-company").show();
		$("#gbDivider02").show();
		//rightcolumn
		$("#fSSLabel03").show();
		$("#fSSLabel04").show();
		
		//biale pola
		if($("#isBeforePefs").length > 0)
		{
			$("#wh-bg03").show();
			$("#wh-bg01").css("top", "471px");
			$("#wh-bg02").css("top", "539px");
			$("#wh-bg03").css("top", "596px");
		}
		
		//wyskosc formularza
	//	$("#register_").height(1370);
		$("#dg_q1").attr("disabled",true);
		$("#dg_q1n").attr("disabled",true);
		$("#dg_q2").attr("disabled",true);
		$("#dg_q3").attr("disabled",true);
		$("#dg_q4").attr("disabled",true);
		$("input[name='dg_q5']").attr("disabled",true);
		$("input[name='dg_q6']").attr("disabled",true);
		$("input[name='dg_q7']").attr("disabled",true);
		$("input[name='dg_q7_2']").attr("disabled",true);
		$("input[name='dg_q8']").attr("disabled",true);
		$("#dg_q9").attr("disabled",true);
		$("#dg_q10").attr("disabled",true);
		$("input[name='dg_q11']").attr("disabled",true);
		$("input[name='dg_q12']").attr("disabled",true);
		

	} else {

		$("#companyNip").attr("disabled", true);
		$("#companyFullName").attr("disabled", true);
		switchField(false, "companyStreet");
		$("#companyStreetNumber").attr("disabled", true);
		switchField(false, "companyAdditionalStreetNumber");
		$("#companyCity").attr("disabled", true);
		$("input[@name='companyCityId']").attr("disabled", true);
		$("#companyPostCode").attr("disabled", true);
		$("#companyDistrict").attr("disabled", true);
		switchField(false, "companyPhone");
		switchField(false, "companyEmail");

		$("#fieldset-company").hide();
		$("#gbDivider02").hide();
		//rightcolumn
		$("#fSSLabel03").hide();
		$("#fSSLabel04").hide();
		
		//biale pola
		if($("#isBeforePefs").length > 0)
		{
			$("#wh-bg03").hide();
			$("#wh-bg01").css("top", "488px");
			$("#wh-bg02").css("top", "556px");
		}
		
		$("#dg_q1").removeAttr("disabled");
		$("#dg_q1").removeAttr("disabled");
		$("#dg_q1n").removeAttr("disabled");
		$("#dg_q2").removeAttr("disabled");
		$("#dg_q3").removeAttr("disabled");
		$("#dg_q4").removeAttr("disabled");
		$("input[name='dg_q5']").removeAttr("disabled");
		$("input[name='dg_q6']").removeAttr("disabled");
		$("input[name='dg_q7']").removeAttr("disabled");
		if($("input[name='dg_q7_2']:checked").length > 0)
			$("input[name='dg_q7_2']").removeAttr("disabled");
		$("input[name='dg_q8']").removeAttr("disabled");
		$("#dg_q9").removeAttr("disabled");
		$("#dg_q10").removeAttr("disabled");
		$("input[name='dg_q11']").removeAttr("disabled");
		$("input[name='dg_q12']").removeAttr("disabled");
		//wyskosc formularza
		//$("#register_").height(950);

	}

};

// włącz / wyłącz
var switchField = function(show, element) {
	if (show) {
		$("#" + element).removeAttr("disabled");
		$("#" + element + " + a").html("wyłącz");
		$("#" + element).css("background","#fff");
		// $("#"+element+" + a").attr("onclick","switchField(false,'" + element
		// +"');");
		$("#" + element + " + a").attr('href',
				'javascript:switchField(false,"' + element + '");');

	} else {
		$("#" + element).removeClass("error");
		$("#" + element).attr("disabled", true);
		$("#" + element).css("background","#ddd");
		$("#" + element + " + a").html("włącz");
		$("#" + element + " + a").attr('href',
				'javascript:switchField(true,"' + element + '");');
	}
};

// ustawienia startowe formularzy
// updateją się włączniki/wyłączniki
var onLoadAction = function() {

	var switchables = new Array("userAdditionalStreetNumber",
			"userPhone", "userMobilePhone",
			"companyAdditionalStreetNumber", "companyPhone", "companyEmail");
	var name;
	var field;
	for ( var n in switchables) {
		name = switchables[n];
		field = $("#" + name);
		if (field.val() == '')
			switchField(false, name);
	}

}

// autouzupełnianie kodu pocztowego
var autocompletePostCode = function(numObj) {
	nfv = numObj.value;
	sign = nfv.substring(nfv.length - 1, nfv.length);
	if (isNaN(sign)) {
		numObj.value = nfv.substring(0, nfv.length - 1);
		numObj.focus();
	} else {
		if (nfv.length == 2) {
			numObj.value = nfv + '-';
			numObj.focus();
		}
		if (nfv.length >= 3 && nfv.length <= 5) {
			if (nfv.indexOf('-') == -1) {
				numObj.value = nfv.substring(0, 2) + '-'
						+ nfv.substring(2, nfv.length);
				numObj.focus();
			}
		}
		if (nfv.length >= 6) {
			if (nfv.indexOf('-') == -1) {
				numObj.value = nfv.substring(0, 2) + '-' + nfv.substring(2, 5);
				numObj.focus();
			} else {
				numObj.value = nfv.substring(0, 6);
				numObj.focus();
			}

		}
	}

}

var updateSwitches = function() {
	if ($("#userCityId-t").is(":checked")) { // miasto
		switchField(true,"userStreet");
		$("#userStreet").next().hide();
	} else { // wieś
		$("#userStreet").next().show();
	}
	if ( $("#fieldset-company").length > 0 && $("#fieldset-company").is(":visible")) {
		if ($("#companyCityId-t").is(":checked")) { // miasto
			switchField(true,"companyStreet");
			$("#companyStreet").next().hide();
		} else { // wieś
			$("#companyStreet").next().show();
		}
	}
}

var toNumber = function(obj) {
	var i = parseInt( $(obj).val(), 10 );

	if ( isNaN(i) || i > 110 || i < 0) 
		i = "";
	$(obj).val( i );
}
var startWithUpper = function(obj) {
	var v = $(obj).val();
	v = v.substr(0,1).toUpperCase() + v.substr(1);
	$(obj).val(v);
}

$("document").ready( function() {
	$("#userPostCode").keyup(function(){
		autocompletePostCode(this);
	});
	$("#companyPostCode").keyup(function(){
		autocompletePostCode(this);
	});
	$("input[name='userCityId']").click(function(){
		updateSwitches(); 
	});
	$("input[name='companyCityId']").click(function(){
		updateSwitches(); 
	});
	$("#userAge").change(function(){
		toNumber(this); 
	});
	
	$("#firstname").change(function(){
		startWithUpper(this);
	});
	
	$("#lastname").change(function(){
		startWithUpper(this);
	});
	
	$("#userCity").change(function(){
		startWithUpper(this);
	});
	
	$("#companyCity").change(function(){
		startWithUpper(this);
	});

	updateSwitches();
});
