function GetFormEl(){
	var Felem='save=true';
	$("input").each(function(){
		Felem=Felem+'&'+$(this).attr('name')+'='+$(this).val();
	});
	
	$("textarea").each(function(){
		Felem=Felem+'&'+$(this).attr('name')+'='+$(this).val();
	});
	return Felem;
}

$(function() {	
	var error;
	
	$('#thumbs img').click(function() {
		var picBig=$(this).attr('src');
		picBig=picBig.replace("storage/products/thumbs/", "storage/products/");
		$('#preview').html('<img src="'+picBig+'" />');
	});
	
	
	$('form :reset').click(function() {
		error=false;
		$('#error').hide();
		$('#response').remove();
		$("input:[alt='must']").each(function(){	
			$(this).css('background-color','#FFFFFF');
		});
		
		$("textarea:[alt='must']").each(function(){	
			$(this).css('background-color','#FFFFFF');
		});
	});
	
	$('form :submit').click(function() {
		error=false;
		$('#error').hide();
		$("input:[alt='must']").each(function(){	
			if(!$(this).val()){ 
				error=true;
				$(this).css('background-color','#FF0000');
			}
			else $(this).css('background-color','#FFFFFF');
		});
		
		$("textarea:[alt='must']").each(function(){	
			if(!$(this).val()){ 
				error=true;
				$(this).css('background-color','#FF0000');
			}
			else $(this).css('background-color','#FFFFFF');
		});

		if(!error){
			$.ajax({
				url: 'submit.php',
				type: 'POST',
				data: GetFormEl(),
				success: function(result) {
					$('#response').remove();
					$('#contact form').prepend(result);
				}
			});
		}
		else $('#error').show();
		return false;
	});
	
	if($('#map').is(":visible")){
			var t=$("#map").html();
			if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById("map"));
				map.setCenter(new GLatLng(37.176116,21.937637), 13);
				var infoTabs = [new GInfoWindowTab("Tab #1", t)];
				var marker = new GMarker(map.getCenter());
				GEvent.addListener(marker, "click", function() {marker.openInfoWindowTabsHtml(infoTabs);});
				map.addOverlay(marker);
				marker.openInfoWindowTabsHtml(infoTabs);
				map.addControl(new GLargeMapControl ( ));
				map.addControl(new GMapTypeControl ( ));
				map.addControl(new GScaleControl ( ));
			}
			$(window).unload( function () { GUnload(); } );
	};	
	
	$('#subscr').click(function() {
		$('#response').remove();
		$('#error').remove();			
		$.ajax({
			url: 'http://localhost/www.medinex.gr/submit.php',
			type: 'POST',
			data: 'subscr=true&mail='+$('#mail').val(),
			success: function(result) {
				$('#newsletter').prepend(result);
			}
		});
		return false;
	});
	
});