﻿function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}

function overlay(action)
{
	if(action==true)
	{
		if($('.overlay').length==0)
		{
			$('body').after('<div class="overlay"></div>');
			$('.overlay').hide().css('opacity', '0.7').css('height', $(document).height());
		}
		$('.overlay').fadeIn();
		$('object').hide();
	}
	else
	{
		$('.overlay').fadeOut('normal', function(){$('object').show();});
	}

}

$(document).ready(function() {
	$('.change_password input[name=password]').keyup(function() {
		value = $(this).val();

		if(value.length<6)
		$('#content li').eq(0).addClass('red');
		else
		$('#content li').eq(0).removeClass('red');
		
		var num_test = new RegExp(/.*[0-9]{1,}.*$/); 
		var low_test = new RegExp(/.*[a-z]{1,}.*$/); 
		var high_test = new RegExp(/.*[A-Z]{1,}.*$/); 

		if(!num_test.test(value))
		$('#content li').eq(1).addClass('red');
		else
		$('#content li').eq(1).removeClass('red');

		if(!low_test.test(value))
		$('#content li').eq(2).addClass('red');
		else
		$('#content li').eq(2).removeClass('red');

		if(!high_test.test(value))
		$('#content li').eq(3).addClass('red');
		else
		$('#content li').eq(3).removeClass('red');


	});

	$('.btn_clear').click(function() {
		$('input').not('.refresh').val('');
		$('.stars .hover').width('0');
		return false;
	});
	
	$('.select_all').click(function() {
		if($(this).text()=='zaznacz wszystko')
		{
			$(this).text('odznacz wszystko');
			$('.checkbox').attr('checked', true);
		}
		else
		{
			$(this).text('zaznacz wszystko');
			$('.checkbox').attr('checked', false);
		}
		return false;
	});

	$('#map').jmap('init', {'mapType':'map','mapCenter':[52.182116, 21.001107], 'mapZoom':14});
	$('#map').jmap('AddMarker', {'pointLatLng': [52.182116, 21.001107]});


	$('#faq').accordion({ 
    	autoheight: false 
	});

	var params = {
	  menu: "false",
	wmode: 'transparent'
	}; 

	swfobject.embedSWF("media/top.swf", "top", "1000", "325", "9.0.0", "expressInstall.swf", {}, params);

	if($('.editstars').length > 0)
	{
		$('.stars').not('.average').mousemove(function(e) {
			var offset = $(this).offset();
			var x = e.pageX - (offset.left);

			this.mouseval = Math.ceil(x/21);
			$(this).find('.hover').css('width', this.mouseval*21);
		});

		$('.stars').not('.average').mouseleave(function() {
			value = parseInt($(this).children('input').val());
			if(isNaN(value)) value=0
			
			$(this).find('.hover').css('width', value*21);
		});

		$('.stars').not('.average').click(function() {
			$(this).children('input').val(this.mouseval);
		});

		$('.stars').not('.average').each(function() {
			value = parseInt($(this).children('input').val());
			if(isNaN(value)) value=0;
			$(this).find('.hover').css('width', value*21);
		});
	}
	else
	{
		
		$('.stars').not('.average').each(function() {
			value = parseInt($(this).children('input').val());
			if(isNaN(value)) value=0;
			html='';
			for(xx=0; xx<5; xx++)
			{
				if(xx<value)
				html +='<img src="media/ventures/star.png">';
				else
				html +='<img src="media/ventures/star2.png">';
			}
			$(this).find('.hover').html(html);

		});

		$('.stars').unbind('click').unbind('mouseleave').unbind('mousemove').css('cursor', 'default');
	}

	if($('.average').length>0)
	{
		avg = $('.average .hover').html();
		$('.average .hover').html('<p>'+avg+'</p>');
		$('.average .hover').width(avg*21);
	}

	$('form').submit(function(){
		error = false;
	
		$(this).find('input[type=text], input[type=password], select, textarea').not('.btn').not('.expert_comment').each(function() {
			if($(this).val()=='')
			{
				$(this).parent().addClass('error');
				error = true;
			}
			else
			$(this).parent().removeClass('error');
		});

		$mail = $(this).find('input[name=email]');

		if($mail.length>0 && !checkemail($mail.val()))
		{
			$mail.parent().addClass('error');
			error = true;
		}	
		else
		$mail.parent().removeClass('error');



		if($(this).find('.checkbox').not('#iwant').length>0)
		{
			selected = false;
			$(this).find('input[type=checkbox]').each(function() {
				if($(this).attr('checked')==true)
				selected = true;
			});
			if(selected==false)
			{
				$('.grey').prepend('<p class="error">Wybierz przynajmniej jedną kategorię!</p>').children('.error').animate({opacity: 1.0}, 2000).fadeOut('normal', function(){$(this).remove()});
				error = true;
			}
		}

		


		if(error==false)
		{
			if($('input[name=module]').val()=='refresh')
			{
				return true;
			}

			resultList = '';
			$(this).find('input, select, textarea').not('.btn').each(function() {
				if(($(this).attr('type')=='checkbox' && $(this).attr('checked')==true) || $(this).attr('type')!=='checkbox')
				resultList += this.name+'='+this.value+'&';
			});

			$('form').hide();
			$('.loading').fadeIn('normal', function() {sendAjax()});
		}
		else if($('.grey .error').length==0)
		$('.grey').prepend('<p class="error">Wypełnij wszystkie pola oznaczone na czerwono</p>').children('.error').animate({opacity: 1.0}, 2000).fadeOut('normal', function(){$(this).remove()});
			
		
		return false;
	
	});


	$('form.expert_survey').submit(function() {
		error = false;
		counter = 0

		$(this).find('input').each(function() {
			if($(this).val()>0) counter++;
		});

		$(this).find('input').not('.btn').each(function() {
			if($(this).val()=='' && counter>0)
			error = true;
		});

		if(error)
		{
			$('.grey').prepend('<p class="error">Oceń wszystkie pytania!</p>').children('.error').animate({opacity: 1.0}, 2000).fadeOut('normal', function(){$(this).remove()});
			return false;
		}
	});

	function sendAjax()
	{
		$.ajax({   
			type: "POST",   
			url: "/extras/form.php",  
			data: resultList,   
			success: function(html)
			{  
				$('.loading').fadeOut('normal', function(){$('.loading').after('<div class="result_container">'+html+'</div>');});
				
			}	 
		});
	}

	$('.map img').click(function()
	{
		$('.map_container').css('top', $(window).scrollTop()+($(window).height()/2)).animate({'left': '50%'});

		overlay(true);
		return false;
	});

	$('.map_container a').click(function()
	{
		if($.browser.msie)
		$('.map_container').css({'left': '-50%'});
		else
		$('.map_container').animate({'left': '-50%'});

		overlay(false);
		return false;
	});



	$('#fileQueue').uploadify({
		'uploader': '/media/js/uploadify.swf',
		'script': '/extras/uploadify.php',
		'folder': '/upload/forms',
		'cancelImg': '/media/ventures/cancel.png',
		'rollover': true,
		'buttonImg': '/media/ventures/btn_upload.png',
		'width': 90,
		'height': 24,
		'sizeLimit': 1073741824,
		'auto': true,
		'fileExt':'*.pdf;*.xls;*.xlsx;*.doc;*.docx',
		'fileDesc':'*.pdf;*.xls;*.xlsx;*.doc;*.docx',
		'onError': onErrorUpload,
		'onComplete': onCompleteUpload
	});


});

function printErrorUpload(text) {
	if(text=='File Size')
	error = 'Błędny rozmiar pliku';

	complete = text.split('.');
	
	if(isNaN(complete[0]))
	error = text;
	else
	{
		$('input[name=file]').val(complete[0]);
		error = 'Plik został zapisany';
		$('#fileQueueUploader').hide();
	}

	$('span.percentage').parent().append('<span>'+error+'</span>').animate({opacity: 1.0}, 3000).animate({opacity: 0}, 500, function() {$(this).remove()});
	
}

function onCompleteUpload(event, queueID, fileObj, response){
	printErrorUpload(response);
	return false;
}

function onErrorUpload(event, queueID, fileObj, errorObj){
	printErrorUpload(errorObj.type);
	return false;
}


