/*-----------------------------------------------------------------------------
Form for "jeu-concours" game
-------------------------------------------------------------------------------
20091023	First version											ADH
-----------------------------------------------------------------------------*/
var strAlertPhone = "Le numéro de téléphone doit avoir au moins 10 chiffres\n";
var strAlertZip = "Le code postal doit avoir au moins 5 chiffres\n";
jQuery(function($) {
	$('#Phone').keypress(function (e) {
		if (IsNum(e.which, true)) return true;
		return false;
	});
	$('#Zip').keypress(function (e) {
		if (IsNum(e.which, false)) return true;
		return false;
	});
	$('#Phone').blur(function () {
/**** Finishing later :-(
		$('.Balloon').css('top', '465px');
		$('.Balloon').css('left', '700px');
		$('.Balloon').css('visibility', 'visible');
		var str = 'Top ' + $('#Phone').css('offsetTop');
		str += ' Left ' + $('#Phone').css('offsetLeft');
		$('.ErrText').html(str);
***/
		var iNbChar = NbNumChar($('#Phone').val());
		if (iNbChar < 10) alert(strAlertPhone);
	});
	$('.Balloon').find('a').click(function () {
		$('.Balloon').css('visibility', 'hidden');
		return false;
	});
	$('#Zip').blur(function () {
		var iNbChar = NbNumChar($('#Zip').val());
		if (iNbChar < 5) alert(strAlertZip);
	});
	$('.Balloon').find('a').click(function () {
		$('.Balloon').css('visibility', 'hidden');
		return false;
	});
	$('#MainForm').submit( function() {
		var strAlert = "";
		var iNbChar = NbNumChar($('#Phone').val());
		if ((iNbChar != 0) && (iNbChar < 10)) strAlert += strAlertPhone; // field not mandatory : empty is allowed
		iNbChar = NbNumChar($('#Zip').val());
		if (iNbChar < 5) strAlert += strAlertZip;
		if (strAlert != "") {
			alert(strAlert);
			return false;
		}
		return true;  // submit OK
	});
});
function IsNum(c, bExt) {
	if(typeof(bExt) == 'undefined') var bExt = false;
	if (c <= 8) return true; // ctrl char
	if ((c >= 48) && (c <= 57)) return true; // num
	if (bExt == false) return false; // no ext chars
	if ((c >= 45) && (c <= 47)) return true; // '/' '.' '-'
	if (c == 32) return true; // space
	return false;
}
function NbNumChar(str) {
	var iNumChar = 0;
	for (var i = 0; i < str.length; i++) {
		c = str.charCodeAt(i);
		if ((c >= 48) && (c <= 57)) iNumChar++;
	}
	return iNumChar;
}
