function testEmail(value)
{
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

function testURL(value)
{
	return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
}

function testDigits(value)
{
	return /^\d+$/.test(value);
}

function testSecurity(value)
{
	return ((value==3)||(value=='trei')||(value=='TREI'));
}

function Slide2Next(slideID)
{	
	if(slideID>0) count = slideID;
	else if(count<items) count++;
	else count = 1; // reached the last slide, get back to first
	
	$('#splash-content').animate({opacity:0},1000, function(){
		// faded out, changing content
		$('#splash-content').html(content[count]);
		
		// fancy box 
		$("a.fancy").fancybox({
			'overlayOpacity' : 0.8,
			'overlayColor' : '#333333'
		});
		
		//if(sheight[count]==0) 
		//{
		//	sheight[count] = $('#splash-content').height();
		//}
		// animating
		$('#splash-content').toggle(1000, function(){
			// change style of controls
			$('#splash-controls a.selected').removeAttr("class");
			$('#splash-controls a[rel='+count+']').attr("class","selected");
			// fading back in
			$('#splash-content').toggle( 1000, function(){
				$('#splash-content').animate({opacity:1},1000);
			});
		});
	});
}

jQuery(document).ready(function($)
{	
	// clear the clear
	$('.clear').height(0);
	
	// slider
	items = 0;
	content = new Array(10);
	control = '';
	count = 1;
	//sheight = new Array(10);
	//for(s=0;s<10;s++)
	//sheight[s]=0;

	$.get('xml/slider.xml', function(data)
	{ // get contents from xml

		$(data).find('slide').each(function()
		{
			// populate array
			items++;
			var $slide = $(this); 
			content[items] = $slide.find('content').text();
			// create controls
			if(items==1) control = control + '<a href="#" class="selected" rel="'+items+'">'+items+'</a>';
			else control = control + '<a href="#" rel="'+items+'">'+items+'</a>';
		});
		
		$('#splash-content').html(content[count]);
		//sheight[count] = $('#splash-content').height();
		
		// fancy box 
		$("a.fancy").fancybox({
			'overlayOpacity' : 0.8,
			'overlayColor' : '#333333'
		});
		
		// add controls
		
		$('#splash-controls').html(control);
	});
	
	// control is clicked
	$('#splash-controls a').live('click',function (){
		
		var slideID = $(this).attr('rel');
		Slide2Next(slideID);
		clearInterval(slideInterval);
		slideInterval = setInterval( "Slide2Next()", 20000 );
		return false;
	});
	
 	slideInterval = setInterval( "Slide2Next()", 20000 );

	// end of code for custom slider
	
	//pikachoose slider
	$("#pikame").PikaChoose({
							auto_play: true,
							thumb_width: 50,
							thumb_height: 50,
							buttons_text: {play: ["porneste"], stop: ["opreste"], previous: ["Poza anterioara"], next: ["Poza urmatoare"]},
							show_captions: false,
							playstop_enabled: false
						});

	// fancy box 
	$("a.fancy").fancybox({
		'overlayOpacity' : 0.8,
		'overlayColor' : '#333333'
	});
	
	// menu styling
	id = $('body').attr('id');
	$('#header ul li.'+id+' a').attr('class','all-rounded selected');
	$('#header ul li.'+id+' a').removeAttr('href');
	
	// contact page bahaviours
	$('textarea').elastic();
	$('textarea').height(50);
	$('<small>Campul va creste pe masura ce scrieti</small>').insertAfter('textarea');
	
	// check for errors on submit
	$('#contactform').live('submit',function(){
		
		$('#contactform input.button2').attr('disabled','disabled');
		$('#contactform input.button2').val('Procesare date');
		errors = 0;
		$('#contactform span.error').remove();
		$.each($('input[rel=error],textarea[rel=error],select[rel=error]'), function(){
				errors++; // count if there are errors
				error = 0; 
				inputText = '';
				inputText = $(this).val(); // this works for inputs
				if(inputText.length==0) inputText = $(this).text(); // this works for textareas

				classes = $(this).attr('class');
				classesArray = classes.split(' ');
				$.each(classesArray, function(index, value){
					if((value=='v-required')&&(inputText.length==0)&&(error==0))
					{// required field empty
						error = 1;
					}
					if((value=='v-digits')&&(testDigits(inputText)==false)&&(error==0))
					{ // field needs to be digits only
						error = 2;
					}
					if((value=='v-email')&&(testEmail(inputText)==false)&&(error==0))
					{ // field needs to be email format
						error = 3;
					}
					if((value=='v-url')&&(testURL(inputText)==false)&&(error==0))
					{ // field needs to be URL format
						error = 4;
					}
					if((value=='v-security')&&(testSecurity(inputText)==false)&&(error==0))
					{ // field needs to be URL format
						error = 5;
					}
				});

				if(error>0)
				{// there's some errors
					if(error==1) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Camp obligatoriu</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==2) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Doar cifre va rugam</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==3) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Email invalid</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==4) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Adresa web invalida</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else if(error==5) 
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Raspuns gresit</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
					else
					{ 
						$(this).next('span.error').remove(); $(this).next('span.ok').remove();
						$('<span class="error">Eroare necunoscuta</span>').insertAfter($(this));
						$(this).attr('rel','error');
					}
				}
				else
				{
					$(this).next('span.error').remove();
					$(this).next('span.ok').remove();
					$('<span class="ok">Ok</span>').insertAfter($(this));
					$(this).removeAttr('rel');
				}
		});
		
		if(errors==0) return true;
		else 
		{
			
			$('#contactform input.button2').removeAttr('disabled');
			$('#contactform input.button2').val('Corectati greseli si retrimiteti');
			return false; 
		}
	});
	
	// add error to required fields
	$('#contactform input.v-required,#contactform textarea.v-required').attr('rel','error');
	
	$('#contactform input:not(.button2), #contactform textarea, #contactform select').focusout(function(){
		error = 0; // 1 for required,
		inputText = '';
		inputText = $(this).val(); // this works for inputs
		if(inputText.length==0) inputText = $(this).text(); // this works for textareas

		classes = $(this).attr('class');
		classesArray = classes.split(' ');
		$.each(classesArray, function(index, value){
			if((value=='v-required')&&(inputText.length==0)&&(error==0))
			{// required field empty
				error = 1;
			}
			if((value=='v-digits')&&(testDigits(inputText)==false)&&(error==0))
			{ // field needs to be digits only
				error = 2;
			}
			if((value=='v-email')&&(testEmail(inputText)==false)&&(error==0))
			{ // field needs to be email format
				error = 3;
			}
			if((value=='v-url')&&(testURL(inputText)==false)&&(error==0))
			{ // field needs to be URL format
				error = 4;
			}
			if((value=='v-security')&&(testSecurity(inputText)==false)&&(error==0))
			{ // field needs to be URL format
				error = 5;
			}
		});

		if(error>0)
		{// there's some errors
			if(error==1) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Camp obligatoriu</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==2) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Doar cifre va rugam</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==3) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Email invalid</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==4) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Adresa web invalida</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else if(error==5) 
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Raspuns gresit</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
			else
			{ 
				$(this).next('span.error').remove(); $(this).next('span.ok').remove();
				$('<span class="error">Eroare necunoscuta</span>').insertAfter($(this));
				$(this).attr('rel','error');
			}
		}
		else
		{
			$(this).next('span.error').remove();
			$(this).next('span.ok').remove();
			$('<span class="ok">Ok</span>').insertAfter($(this));
			$(this).removeAttr('rel');
		}
	});
});
