// ===================================================================
// Contats functions
// ===================================================================

labels = new Array();
labels[1] = new Array();
labels[1]['it'] = 'Grazie per la vostra richiesta.';
labels[1]['en'] = 'Thanks';
labels[2] = new Array();
labels[2]['it'] = 'Torna indietro';
labels[2]['en'] = 'Back';
labels[3] = new Array();
labels[3]['it'] = 'Nuova richiesta';
labels[3]['en'] = 'New request';


function resetContacts(){
	$("#contactsForm").bind('reset', function(evt){
        var count_errors = 0;
		
        $("#contactsForm span").each(function(index, el) {
            if (el.id.length > 0)
            {
            	$('#span_'+el.id).removeClass('error');
            }
        });
    });
}

function submitContacts(labels, temp_language){
    
	$("#contactsForm").bind('submit', function(evt){
        
        $("#contactsForm *").each(function(index, el) {
            $('#span_'+el.id).removeClass('error');
        });
		//$('#span_dateOfBirth').removeClass('error');
		//$('#dateOfBirthMessage').text('');
		
		
        var count_errors = 0;
		var newsletterSubscription = false;
		
		$("#contactsForm *").each(function(index, el) 
		{
            if (el.id.length > 0)
            {
			
				if ( el.type == 'text' || el.type == 'checkbox' || el.type == 'textarea' || el.type == 'select-one' ) {
					if ($(el).attr('rel') == 'required' && $(el).val() == '') {
						//$('#errors').css('display', 'block');
	                    $('#span_'+el.id).addClass('error');
	                    count_errors++;
					}
					
					if ($(el).attr('rel') == 'required_email') {
						if ( !$(el).val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/) || $(el).val() == '' ) {
							//$('#errors').css('display', 'block');
	                        $('#span_'+el.id).addClass('error');
	                        count_errors++;
						}
					}
					
					if ($(el).attr('rel') == 'required_privacy') {
						if ( !el.checked ) {
							//$('#errors').css('display', 'block');
	                        $('#span_'+el.id).addClass('error');
	                        count_errors++;
						}
					}
					
					if ($(el).attr('rel') == 'required_newsletter') {
						if (el.checked ) {
							$newsletterSubscription = true;
						}
					}
					
				}
            }
		});
		
        if (count_errors > 0)
        {
            return false;
        }
		else
        {
			dataToSend = '';
            numPar = 0;
			$("#contactsForm *").each(function(index, el){
	            if (el.id.length > 0)
	            {
					if (el.type == 'select-one' || el.type == 'text' || el.type == 'hidden' || el.type == 'textarea' || (el.type == 'checkbox' && el.checked)){
	    				if ($(el).attr('rel') != 'not_post')
	                    {
	                        if (numPar > 0) dataToSend += '&'
	    					dataToSend += $(el).attr('name') + '=' + $(el).val();
	                        numPar++;
	                    }
					}
	            }
			});
			
			$("#contactsForm *").each(function(index, el){
	            if (el.id.length > 0)
	            {
					if ((el.type == 'checkbox' && el.checked)){
	    				if ($(el).attr('rel') == 'newsletter')
						{
							$.ajax({
								type: "POST",
								url: '/ws/newsletter.php',
								data: dataToSend,
								dataType: "html",
								complete: function (data){}
							});
						}
					}
	            }
			});
			
			$.ajax({
	  			type: "POST",
				beforeSend: startLoading("contactsForm"),
	  			url: '/ws/contacts.php',
				data: dataToSend,
				dataType: "html",
				complete: function (data){
					stopLoading("contactsForm");
					if (data.responseText == "usercode_error")
					{
			            $('#span_input_usercode').addClass('error');
						count_errors++;
					}
					else
					{
						//$('#errors').css('display', 'none');
						$.scrollTo(0, 800);
						$("#contact_requestForm").html('<p>'+data.responseText + '</p></p><p><a href=\"?\">'+labels[3][temp_language]+'</a></p>');
					}
				}
			});
		}
		return false;
	});
}

function reloadForm ()
{
    $('#response_form').css('display', 'none');
	$('#request_form').css('display', 'block');
}


