
$(document).ready(function() {

	$('input[name*="enrollTmp"]').click(function() {
		$.post('checkSession.php', {
			    'check':this.checked,
			    'sessVar':'enrollTmp',
			    'courseId':this.value
		     },
		    function() {
		    },
		    'json'
		    ); /// end $.post
	    });
	
	// toggle postalAddress by default if hasPostalAddress checked
	if ($('input[name="hasPostalAddress"]').attr('checked')) {
		$('#postalAddress').toggle(this.checked);
	}

	// toggle discountPiece by default hasDiscount checked
	if ($('input[name="hasDiscount"]').attr('checked')) {
		$('#discountPiece').toggle(this.checked);
	}


	// toggle hasDiscount
	$('input[name="hasDiscount"]').click(function() {
		$('#discountPiece').toggle(this.checked);
	    });


	// toggle hasDiscount
	/*
	if ($('select[name="discountType"]').attr('value')) {
	    //	    console.log(this);
	    //	    makeDiscountPaper(this.value, this.selected);
	}

	if ($('select[name="discountType"]').attr('value'))  {
	    makeDiscountPaper($('select[name="discountType"]').attr('value'));
	}

	// toggle discountPaper
	$('select[name="discountType"]').click(function()    {
	makeDiscountPaper(this.value)
       });

	*/

	function makeDiscountPaper(val) {
	    	paperSel = 'select[name="discountPaper"]';
		discountPaper = '#discountPaper';
		carryDiscountPaper = 'input[name="carryDiscountPaper"]';

		if (val) tg = true;
		else tg = false;
		$(discountPaper).toggle(tg);

		if (val == 'elev') {
		    var myOptions = Array('carnet elev',	'legitimatie transport');
		} else if (val == 'student') {
		    var myOptions = Array('carnet student',	'legitimatie transport');
		} else if (val == 'somer') {
		    var myOptions = Array('carnet de somer', 'act doveditor eliberat de  AJOFM');
		}

		$(paperSel).html('');
		for (i = 0; i < myOptions.length; i++) {
		    c = myOptions[i];
		    console.log(c);

		    $(paperSel).append('<option value="' + c + '">' + c + '</option>');
		}

		console.log($(carryDiscountPaper).attr('value'));
		if ($(carryDiscountPaper).attr('value')) {
		    toSetValue = $(carryDiscountPaper).attr('value');
		    $(paperSel).val(toSetValue);
		}




	    }


	// fills city, and automatically change county
	function changeCounty(el, countyName)
	{

	    if (el.attr('value') == 'Bucuresti') {
		elCounty = $('input[name="' + countyName + '"]');
		elCounty.attr('value', el.attr('value'));
	    }
	}

	// copy field
	function copyField(fromField, toField)
	{
	    if (!toField.attr('value'))
		toField.attr('value', fromField.attr('value'));
	}

	// when fills city, copies to pCity (and possible updates county and pCounty)
	$('input[name="city"]').change(function() {
		pCity = $('input[name="pCity"]');
		changeCounty($(this), 'county');
		copyField($(this), pCity);
		changeCounty(pCity, 'pCounty');
	    });

	// when fills county, automatically copies to pCounty
	$('input[name="county"]').change(function() {
		copyField($(this), $('input[name="pCounty"]'));
	    });





	$('input[name="hasPostalAddress"]').click(function() {
		$('#postalAddress').toggle(this.checked);
	    });




	//	$('.fieldHelp').html('<a href="">help</a>');
	/*
	$('.fieldHelp a').click(function(event) {
		this.dialog('open');
		event.preventDefault();
	    });
	*/




}); /// end doc ready
