/***
 * Script de la page d'inscription
***/




// Fonctions globales --------------------------//

function shuffle(a, b)
{
	return ( Math.round( Math.random() ) - 0.5 );
}

// Fonctions specifiques ------------------------//
// Appele lorsque le user change de sexe :
function onSexChange( s, id_pays )
{
	var sx;
	if( s == null ) return;
	switch_img( 'thumbsRight', s );
	if( ct == 2 ) sx = 1;
	else if( s == 0 ) sx = 1;
	else if( s >= 1 ) sx = 0;
	miniListJs( 'default', 'type=default&ms='+s+'&s[sexe][]=' + sx + '&s[id_pays][]=' + id_pays , 470 );
}
function onSexChange2( s, id_pays, minilist )
{
	var sx;
	var sw;
	if( s == null ) return;
	switch_img( 'thumbsRight', s );
	switch (s)
	{
		case 0:
			sw = 1;
			break;
		case 1:
			sw = 0;
			break;
		case 2:
			sw = 2;
			break;
	}
	if ( ct == 2 )
	{
		sx = 1;
		sw = 1;
	}
	else if ( s == 0 )
	{
		sx = 1;
	}
	else if ( s >= 1 )
	{
		sx = 0;
	}
	if (minilist)
	{
		miniListJs( 'default', 'type=default&ms='+s+'&s[sexe][]=' + sx + '&s[id_pays][]=' + id_pays , 915 );
	}
	$('#photoHome').css('background-image','url(/img/inscription/photos/rencontre_'+photoHome[sw][(rand(0,(photoHome[sw].length-1)))]+'.jpg)');
}


// Appelé lors d'un changement de section
function changeCT( url )
{
	if( url.length )
	{
		var request = '?ctChange=1';
		$("#ezsignup").attr({ action:  url + request });
		$("#ezsignup").submit();
		return true;
	}
	else
	{
		return false;
	}
}

function switch_img( imgsName, sexe )
{
	if( ct == 2 ) sexe = 1;
	else if( sexe == 0 ) sexe = 1;
	else if( sexe == 1 ) sexe = 0;

	// creer le tableau aleatoire
	var temp = new Array();
	if( sexe == 2 )
	{
		var str = '';
		nm[ct][0].sort(shuffle);
		nm[ct][1].sort(shuffle);
		
		var tab0 = nm[ct][0].slice( 0 , nm[ct][0].length/2 );
		var tab1 = nm[ct][1].slice( 0 , nm[ct][1].length/2 );
		temp = tab0.concat( tab1 );
	}
	else
	{
		temp = nm[ct][sexe];
	}

	temp.sort(shuffle);
	
	$("[name=" + imgsName +"]").each ( 
		function (i){
			
			info = temp[i].split(':');
			$(this).attr({ href: '/?id=' + info[0] });
			$(this).css("background-image", 'url(' + info[2].replace( /thumb\/[0-9]+/, "thumb/100" ) + ')' );
			this.firstChild.innerHTML = info[1];
			this.firstChild.textContent = info[1];
		
		});
}
function init()
{
	switch_img( 'thumbsRight', inscriptionSexe );
}

//addEvent( window, 'load', init );

var firstpassload = true;
function insert_date_as_password()
{
	if (firstpassload)
	{
		firstpassload = false;
		auto_pass = $("[name='user[jj]']").val() + $("[name='user[mm]']").val();
		if (is_numeric(auto_pass) && auto_pass.length == 4 )
		{
			$("[name='user[pass]']").val(auto_pass);
		}
	}
}

function insert_prfxmail_as_login()
{
	f = document.getElementById('ezsignup');
	if ( !f.elements['user[login]'].value.length )
	{
		var reg=new RegExp("[ ,@]+", "g");
		var emailpseudo= $("[name='user[email]']").val().split(reg);
		emailpseudosub = emailpseudo[0].replace(/[^_a-zA-Z0-9]+/g,'');
		emailpseudosub = emailpseudosub.substring(0, 15);
		if (emailpseudosub.length > 2 )
		{
			$("[name='user[login]']").val(emailpseudosub);
			isLoginDispo(ct);
		}
	}
}
function onfocusclean(id, value, str)
{
	if (value == str)
	{
		$('#' + id ).val('');
	}
}
function onfocusoutclean(id, value, str)
{
	if (value == '')
	{
		$('#' + id ).val(str);
	}
}

function chAction(formId, obj)
{
	var jObj = $(obj);
	
	if(jObj.is('input'))
	{
		if(!obj.checked) return;
	}
	else if(!jObj.val().length) return;
	
	$('#' + formId).attr('action' ,'http://' + fl[obj.value] + '/login.php' )
}
function isMailOkReturn( email, result )
{
	arrayMail[email] = result;
	correctMail = false;
	
	$('#mailField').removeClass();
	$('#mailField').addClass('intxt');
	$('#infoMail').hide();
	
	switch( result )
	{
		case false:
		case 'false':
			$('.hiddenFields').hide();
			$('.hideongoodmail').show();
			$('#mailField').css('color', 'red');
			$('#infoMail').css('color', 'red');
			$('#infoMail').html(emailIncorrect);
			$('#infoMail').show();
		break;
		case true:
		case 'true':
			correctMail = true;
			$('.hiddenFields').show();
			$('.hideongoodmail').hide();
			$('#mailField').addClass('yes');
			$('#mailField').css('color', '');
		break;
	}
	
}
function isLoginDispoReturn( login, result )
{
	arrayLogin[ login ] = result;
	var text = 'Error';
	var color = 'red';
	correctLogin = false;
	
	$('#loginField').removeClass();
	$('#loginField').addClass('intxt');
	$('#infoLogin').hide();
	
	switch( result )
	{
		case 'no':
			text = noPseudo;
		break;
		case 'short':
			text = tooShort;
		break;
		case 'long':
			text = tooLong;
		break;
		case 'false':
			text = notAvail;
		break;
		case true:
		case 'true':
			correctLogin = true;
			$('#loginField').addClass('yes');
			$('#loginField').css('color', '');
		break;
		case 'bad':
			text = badSyntax;
		break;
	}
	if(correctLogin == false)
	{
		$('#infoLogin').show();
		$('#loginField').css('color', 'red');
		$('#infoLogin').css('color', 'red');
		$('#infoLogin').html(text);
	}
}
function rdrdrg()
{
	header('http://www.drague.org/rencontres.php');
	return false;
}
function blackscreen()
{
	if ($('#blackscreen').css("visibility") != 'visible')
	{
		$('#blackscreen').css("visibility" , "visible");
	}
}
function getSelectValue( name )
{
	return $('[name="' + name + '"]').val();
}
function verif_submit()
{
	formulaire	= document.getElementById('ezsignup');
	var errors = new Array();
	if( ct != 2 ) sexe = getanswer(formulaire.elements['user[sexe]']);
	else sexe = 1;
	jj			= $('[name="user[jj]"]').val();
	mm			= $('[name="user[mm]"]').val();
	aa			= $('[name="user[aa]"]').val();
	email		= $('[name="user[email]"]').val();
	zlogin		= $('[name="user[login]"]').val();
	pass		= $('[name="user[pass]"]').val();
	cgu			= document.getElementById('cgu').checked;
	//Sexe
	if( sexe != '0' && sexe != '1' && sexe != '2' ) errors.push( chooseSex );
	//Date de naissance
	if( !is_numeric(jj)  || !is_numeric(mm) || !is_numeric(aa) ) errors.push( chooseBirth );
	else if( parseInt( aa.concat( mm, jj ) ) > birthMin ) errors.push( noTeen );
	//Mail
	if(email == "") errors.push( chooseMail );
	else if(!is_mail(email) || correctMail == false )
	{
		errors.push( invalidMail );
		formulaire.elements['user[email]'].value = "";
	}
	//Login
	if(zlogin == "") errors.push( chooseLogin );
	else if(!is_login(zlogin) ) errors.push( invalidLogin );
	else if( correctLogin == false ) errors.push( getId('infoLogin').innerHTML );
	//Pass
	if(pass == "") errors.push( choosePassw );
	else if(!is_numeric(pass) || pass.length != 4 )
	{
		errors.push( invalidPass );
		formulaire.elements['user[pass]'].value = "";
	}
	else if(pass == aa || pass == '1234' || pass == '0000') errors.push( soEasyPassw );
	//CGU
	if(cgu != true) errors.push( acceptCGU );
		
	if( errors.length > 0 )
	{
		var htmlErrros = '';
		for( var i = 0; i < errors.length; i++ ) htmlErrros += '- '+errors[i]+"\n";
		alert(popTitle+"\n\n"+htmlErrros);
		return false;
	}
	else
	{
		$('#ezsignup').submit();
		return true;
	}
}
function showBase()
{
	if( arguments.length == 1 )
	{
		$('#ctChoose').css('display' , 'none');
	}
	else
	{
		$('#ctDef0').css('display' , 'none');
		$('#ctDef1').css('display' , 'none');
		$('#ctDef2').css('display' , 'none');
		for( var i = 0; i < arguments.length; i++ )
		{
			$('#ctDef' + arguments[i] ).css('display' , '');
		}
		$('#ctChoose' ).css('display' , '');
	}
}
$(document).ready( function (){ init(); });