String.prototype.isEmail = function() {
	return this.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/);
}

String.prototype.isInteger = function() {
	return this.match(/^[0-9]+$/);
}

function recupererSondage(id) {
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: "action=recupererSondage&id="+id,
		success: function(html){				
			$("#ttSondages").html(html);
		}
	});		
}

function voter(k) {
	id_reponse = '';
	if (k) {
		r = $(":radio");
		for( i=0; i<r.length; i++) {
			if (r[i].name == 'reponse' && r[i].checked) {
				id_reponse = r[i].value;
				break;
			}
		}
	}
	if (!id_reponse && k) {
		alert('Choisissez une réponse');
	} else {
		$.ajax({
			type: "POST",
			url: "ajax.php",
			data: "action=voter&id_reponse="+id_reponse,
			success: function(html){				
				if (id_reponse) {
					date = new Date();
					set_cookie ( "vote", "ok", date.getFullYear(), date.getMonth(), date.getDate()+1 );
				}
				$("#mySondage").html(html);

			}
		});	
	}
}

function formVote() {
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: "action=formVote",
		success: function(html){				
			$("#mySondage").html(html);
		}
	});	
}

function newsletter(myForm) {
	email = jQuery.trim(myForm.email.value);
	if (!email) alert('Email est obligatoire');
	else if(!email.isEmail()) alert('Email invalide');
	else {
		$.ajax({
			type: "POST",
			url: "ajax.php",
			data: "action=newsletter&email="+email,
			success: function(html){
				$("#myNewsletter").html(html);
			}
		});	
	}
}


function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ) {
	var cookie_string = name + "=" + escape ( value );
	
	if ( exp_y ) {
		var expires = new Date ( exp_y, exp_m, exp_d );
		cookie_string += "; expires=" + expires.toGMTString();
	}
	
	if ( path )
		cookie_string += "; path=" + escape ( path );
	
	if ( domain )
		cookie_string += "; domain=" + escape ( domain );
	
	if ( secure )
		cookie_string += "; secure";
	
	document.cookie = cookie_string;
}

function get_cookie ( cookie_name ) {
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	
	if ( results )
		return ( unescape ( results[2] ) );
	else
		return null;
}

function delete_cookie ( cookie_name ) {
	var cookie_date = new Date ( ); 
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function verifRecherche(myForm) {
	q = jQuery.trim(myForm.q.value);
	if (q.length<2) {
		alert('Mot clé doit être plus que 2 charactères');
		return(false);
	}
	return(true);
}

function verifFormAbonne(myForm) {
	k = 0;
	for (i=0; i<myForm.length; i++) {
		v = jQuery.trim(myForm.elements[i].value);
		if (v=='') k++;
	}
	if (myForm.elements["address2"].value=='') k--;
	if (myForm.elements["amount"].value=='111') k=1;
	if (k>0) {
		alert('Tous les champs sont obligatoire');
		return false;
	}

	//if myForm.getElementById('amount')

	return true;
}