var img 				= new Image();
img.src 				= 'retail/blue/img/kontakt/tloSelect.gif';
var img2				= new Image();
img2.src				= 'retail/blue/img/kontakt/trescOverlay.gif';
var kliknieto 			= false;
var walidujReklamacja 	= false;
var selectAktywny		= false;
var rozwiniete			= false;

var index = 400;

function klikForm() {

	
	//	jesli dane sie zgadzaja, pokaz potwierdzenie.... 
	//	dodanie danych z forma....
	var p = $('<p></p>');
	$('#danezforma').empty().append(p);
	s ='';
	p.append('Temat wiadomości: <b>'+$('#kategoria').val()+'</b><br />');
	if ( $('#kategoria').val() == 'Reklamacja' ) {
		p.append('Reklamacja dotyczy: <b>'+$('#dotyczy').val()+'</b><br />');
	}
	p.append('Imię i nazwisko: <b>'+$('#imie').val()+'</b><br />');
	if ( $('#kategoria').val() == 'Reklamacja' ) {
		p.append('Numer PESEL: <b>'+$('#pesel').val()+'</b><br />');
		p.append('Adres e-mail: <b>'+$('#adresEmail').val()+'</b><br />');
	}
	if ( $('#formaTelefon').is(':checked') ) {
		p.append('Numer telefonu: <b>'+$('#home_phone').val()+'</b><br />');
	} else if ( $('#formaEmail').is(':checked') ) {
		p.append('Adres e-mail: <b>'+$('#formaEmailInput').val()+'</b><br />');
	}
	p.append('Treść wiadomości: <pre>'+$('#tresc').val()+'</pre></p>');
	
	$('#potwierdzenie').fadeIn();
	return false;
};

$( function () { 
	
	$('select').each( function() { 
		$(this).css({'visibility':'hidden'});
		var offset = $(this).offset();
		var parent = $(this).parent();
		while( !parent.hasClass('inbox') ) {
			parent = parent.parent();
		}
		var div = $('<div class="fnForm_replacedSelect"></div>');
		div.css({ 'position':'absolute', 'left':0, 'top':0 });
		div.attr('id','fnSelect_'+this.id);
		parent.css({'z-index':index}).append(div);
		index--;
		
		var span = $('<span></span>').css('width', '200px');
		span.text($(this).find('option').get(0).innerHTML);

		
		var i = 0;
		var ul = $('<ul></ul>').css({'background':'url(/retail/blue/img/kontakt/tloSelect.gif) bottom no-repeat', 
				'zIndex':199, 'margin':'5px 0 0 -4px', 'list-style-type':'none', 'display':'none', 'width':'210px' });
		var li = '';
		$(this).find('option').each( function() {
			li = $('<li></li>');
			li.text( $(this).text() );
			li.attr('id', 'fnSelectIndex_'+i);
			ul.append(li);
			i++;

			li.hover(function() {
					$(this).css( {'font-weight':'bold', 'cursor':'pointer' });
				}, function() {
					$(this).css( {'font-weight':'', 'cursor':'default' });
			});
			li.click(function() {
				$(this).css( {'font-weight':'', 'cursor':'default' });
				$(this).parent().parent().find('ul').hide();
				$(this).parent().hide();
				$(this).parent().css('display','none');
				span.text( $(this).text() );
				var selectID = $(this).parent().parent().attr('id');
				selectID = selectID.slice( selectID.indexOf('_') +1 );
				$('#'+ selectID ).attr('selectedIndex',  $(this).attr('id').slice( $(this).attr('id').indexOf('_')+1 ));
				if ( selectID == 'kategoria' ) {
					if ( $(this).text() == 'Reklamacja' ) {
						$('.forReklamacja').fadeIn();
						$('.forReklamacjaNo').fadeOut();
						walidujReklamacja = true;
					} else { 
						$('.forReklamacja').fadeOut();
						$('.forReklamacjaNo').fadeIn();
						walidujReklamacja = false;
					}
					
					//	dislaimers
					if ( typeof disclaimers != 'undefined' ) {
						var discTxt = disclaimers.base;
						if ( $(this).text() == 'Reklamacja' || $(this).text() == 'inny' || $(this).text() == 'Citibank Online' ) {
							discTxt = disclaimers.kontakt;
						} else { 
							discTxt = disclaimers.base;
						}
						$('label[for="zgoda"]').html(discTxt);
					}
				}
				return false;
			});
		});

		div.append( span );
		div.append(ul);
		span.click(function() {
			if ( rozwiniete ) {
				$(this).find('.fnForm_replacedSelect ul').hide();
				ul.hide();
				rozwiniete = false;
			} else { 
				ul.show();
				setTimeout(function() { rozwiniete = true; }, 50);
			}
			return false;
		}).hover( function() {
			$(this).css('cursor','pointer'); }, function() {
			$(this).css('cursor', ''); 
		});
		
	});
	
	$('#kform').submit(function() {
		var error = false;
		var errortxt = new Array();
		
		if ( !$('#kategoria').val() ) {
			errortxt.push('Wybierz temat wiadomości');
			error = true;
		}
		if ( $('#kategoria').val() == 'Reklamacja' ) {
			if ( !$('#dotyczy').val() ) {
				errortxt.push('Wybierz czego dotyczy reklamacja');
				error = true;
			}
		}
		if ( $('#imie').val() == 'Imię i Nazwisko' || !$.trim($('#imie').val()) ) {
			errortxt.push('Podaj imię i Nazwisko');
			error = true;
		}
		if ( $('#kategoria').val() == 'Reklamacja' ) {
			if ( $('#pesel').val() == 'Numer PESEL' || !$.trim($('#pesel').val()) ){ 
				errortxt.push('Podaj numer PESEL');
				error = true;
			} else { 
				if ( !forms.v.pesel( $('#pesel').val() ) ) {
					errortxt.push('Podaj prawidłowy numer PESEL');
					error = true;
				}
			}
			if ( $('#adresEmail').val() == 'Adres e-mail' 
					||  !$.trim( $('#adresEmail').val() ) 
					||  !forms.v.valid( $('#adresEmail').val(), 'email' ) ) {
				errortxt.push('Podaj prawidłowy adres e-mail');
				error = true;
			}
		}
		
		if ( $('#formaTelefon').is(':checked') ) {
			if ( !$('#home_phone').val() || !forms.v.valid( $('#home_phone').val(), 'telefon' ) ) {
				errortxt.push('Podaj prawidłowy telefon');
				error = true;
			}
		} else if ( $('#formaEmail').is(':checked') ) {
			if ( !$('#formaEmailInput').val() ||  !forms.v.valid( $('#formaEmailInput').val(), 'email' )  ) {
				errortxt.push('Podaj prawidłowy e-mail');
				error = true;
			}
		} else if ( $('#formaAdres').is(':checked') || $('#formaTelefonReklamacja').is(':checked') ) {
			;
		} else { 
			errortxt.push('Wybierz formę kontaktu');
			error = true;
		}
		
		if ( !kliknieto || $('#tresc').val() == '') {
			errortxt.push('Wpisz treść wiadomości');
			error = true;
		}
		if ( !$('#zgoda').is(':checked') ) {
			errortxt.push('Musisz wyrazić zgodę na przetwarzanie danych');
			error = true;
		}
		
		if ( error ) {
			alert( errortxt.join('\n') ); 
			return false;
		}
			
		if ( $('#kategoria').attr('selectedIndex') == 1 ) {
			if ( document.location.href.indexOf('uat') > 0 ) {
				$('#kform').attr('action','https://contentdropbox.citigroup.net/securemail/last.sm?path=/poland/portal/kontakt/cbol/');
			} else {
				$('#kform').attr('action','https://www.citibank.com/securemail/last.sm?path=/poland/portal/kontakt/cbol/');
			}
		} else if ( $('#kategoria').attr('selectedIndex') == 6 ) { 
			if ( $('#dotyczy').attr('selectedIndex') == 1 ) {
				if ( document.location.href.indexOf('uat') > 0 ) {
					$('#kform').attr('action','https://contentdropbox.citigroup.net/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_karta/');
				} else {
					$('#kform').attr('action','https://www.citibank.com/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_karta/');
				}
			} else if ( $('#dotyczy').attr('selectedIndex') == 2 ) {
				if ( document.location.href.indexOf('uat') > 0 ) {
					$('#kform').attr('action','https://contentdropbox.citigroup.net/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_konto/');
				} else {
					$('#kform').attr('action','https://www.citibank.com/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_konto/');
				}

			} else if ( $('#dotyczy').attr('selectedIndex') == 3 ) {
				if ( document.location.href.indexOf('uat') > 0 ) {
					$('#kform').attr('action','https://contentdropbox.citigroup.net/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_kredyt/');
				} else {
					$('#kform').attr('action','https://www.citibank.com/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_kredyt/');
				}

			} else if ( $('#dotyczy').attr('selectedIndex') == 4 ) {
				if ( document.location.href.indexOf('uat') > 0 ) {
					$('#kform').attr('action','https://contentdropbox.citigroup.net/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_inwestycje/');
				} else {
					$('#kform').attr('action','https://www.citibank.com/securemail/last.sm?path=/poland/portal/kontakt/reklamacje_inwestycje/');
				}
			} else {
				error = true;
			}
			
		} else {
			if ( document.location.href.indexOf('uat') > 0 ) {
				$('#kform').attr('action','https://contentdropbox.citigroup.net/securemail/last.sm?path=/poland/portal/kontakt/gen/');
			} else {
				$('#kform').attr('action','https://www.citibank.com/securemail/last.sm?path=/poland/portal/kontakt/gen/');
			}
		}
		
		if ( error ) {
			alert('Wystąpił błąd. Proszę spróbować ponownie za kilka minut');
			return false;
		}
		
		//this.submit();
		
		$('#divFormularz').hide();
		$('#divTH').show();	
		$('#potwierdzenie').fadeOut();
			
		var pageTracker = _gat._getTracker("UA-1972630-1");
		pageTracker._initData();
		pageTracker._trackPageview('wyslany kontakt _ '+$('#kategoria').val());
	});

	$('#aPowrot').click( function() {
		$('#divFormularz').show();
		$('#divTH').hide();
		document.getElementById('kform').reset();
		$('#fnSelect_kategoria span').text('Wybierz temat wiadomości');
		$('#fnSelect_dotyczy span').text('Dotyczy');
		kliknieto = false;
	});
	
	$('#divTH').hide();
	
	$('.tipImg').hover(function() {
			$(this).parent().find('.tip').fadeIn();
		}, function() {
			$(this).parent().find('.tip').fadeOut();
	});

	$('#submitek').hover(function() {
		$(this).fadeTo(100,0.7);
	}, function() {
		$(this).fadeTo(100,1);
	});
	
	$('#tresc').focus(function() {
		if ( !kliknieto ) {
			kliknieto = true;
			$('#trescOverlay').fadeIn();
			$(this).attr("disabled", 'disabled');
		}
	});
	$('#trescOverlay').click(function() {
		$(this).fadeOut();
		$('#tresc').attr("disabled", '');
		$('#tresc').focus();
	});
	$('body').click(function() {
		if ( rozwiniete ) { 
			rozwiniete = false;
			$('.fnForm_replacedSelect ul').hide();
			return false;
		}
	});

});

