// JavaScript Document

/**
 * Function that verifyes if user press Enter on step 1
 **/
function regKey1( e )
{
	var keynum;
	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }	
	if( keynum == 13 )
	{
		verifyStep1(root_path);
	}
}

/**
 * Function that verifyes if step1 fields are completed
 **/
function verifyStep1( path )
{
	// where we store the result
	var sw = true;
	var root_path = path;
	
	if( document.getElementById('user_name').value == '' )
	{
		sw = false;
		field = "Username";
	} else	
	if( document.getElementById('user_pass').value == '' )
	{
		sw = false;
		field = "Password";
	} else	
	if( document.getElementById('user_pass_confirm').value == '' )
	{
		sw = false;
		field = "Confirm password";
	} else	
	if( document.getElementById('user_mail').value == '' )
	{
		sw = false;
		field = "E-mail address";
	}	
	if( !sw )
		alert( "Please complete the '"+field+"' field !" );
	else
	{
		var showUserAvailable = function( ){
			if( ajax.response == 'available')
			{
				document.getElementById('step1').style.display = 'none';
				document.getElementById('step2').style.display = 'block';
			}
			else
			{
				if( ajax.response == 'exists')
				{
					alert( "Username already exists. Please try another one!" );				
				}

				if( ajax.response == 'short')
				{
					alert( "Username is too short. Please try another one with minimum 4 chars!" );				
				}

				if( ajax.response == 'long')
				{
					alert( "Username is too long. Please try another one with maximum 16 chars!" );				
				}

				if( ajax.response == 'reserved')
				{
					alert( "Username is a reserved word. Please try another one!" );
				}

				if( ajax.response == 'chars')
				{
					alert( "Username must not contain special chars. Please try another one!" );				
				}

				if( ajax.response == 'bad_mail')
				{
					alert( "The e-mail you entered is not valid. Please enter a valid one!" );				
				}

				if( ajax.response == 'mail_exists')
				{
					alert( "The e-mail is already in use by another user. Please try another one!" );				
				}	}
		}

		ajax.requestFile = root_path + 'application/ajax/register/verifyUserExists.php?user_name=' + document.getElementById('user_name').value + '&user_mail=' + document.getElementById('user_mail').value;
		ajax.onCompletion = showUserAvailable;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function		
		
	}	
	return sw;	
}
function verifyStep2( path )
{
	// where we store the result
	var sw = true;
	var root_path = path;
	
	if( document.getElementById('user_first_name').value == 0 )
	{
		sw = false;
		field = "Your first name";
	} else	
	if( document.getElementById('user_birth_date_day').value == 0 )
	{
		sw = false;
		field = "Date of birth - day";
	} else	
	if( document.getElementById('user_birth_date_month').value == 0 )
	{
		sw = false;
		field = "Date of birth - month";
	} else	
	if( document.getElementById('user_birth_date_year').value == 0 )
	{
		sw = false;
		field = "Date of birth - year";
	} else	
	if( document.getElementById('user_country').value == 0 )
	{
		sw = false;
		field = "Country";
	} 
	if( !sw )
		alert( "Please complete the '"+field+"' field !" );
	else
	{
		var showStep3 = function(  ){
				document.getElementById('step2').style.display = 'none';
				document.getElementById('step3').style.display = 'block';
				document.getElementById('step3').innerHTML = ajax.response;
		}
//		alert(root_path);
	ajax.requestFile = root_path + 'application/ajax/register/addUser.php?user_name='+document.getElementById('user_name').value+'&password='+document.getElementById('user_pass').value+'&mail='+ document.getElementById('user_mail').value+'&birth_date='+document.getElementById('user_birth_date_day').value+'.'+document.getElementById('user_birth_date_month').value+'.'+document.getElementById('user_birth_date_year').value+'&country='+document.getElementById('user_country').value+'&region='+document.getElementById('user_region').value+'&first_name='+document.getElementById('user_first_name').value;
	
	if( document.getElementById('user_ref').value != '' )
		ajax.requestFile += '&user_ref=' + document.getElementById('user_ref').value ;
	ajax.onCompletion = showStep3;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function		

	}	
}
/**
 * Function that displays regions of a selected country
 */
function displayRegions( selectedCountry, root_psath )
{
	var displayRegion = function( ){
		var reg_obj = document.getElementById('regionsLayer');
		reg_obj.innerHTML = ajax.response ;
	}

	ajax.requestFile = root_path + 'application/ajax/register/loadRegions.php?country=' + selectedCountry ;
	ajax.onCompletion = displayRegion;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function		

	
}

function showDatePicker()
{
	var dateMenu = new Ext.DatePicker({
		applyTo:'datePickerEl',
		autoShow:true,
	    handler : function(datepicker, date){
	    	document.getElementById('user_birth_date').value = date.format('j.m.Y') ;
	        dateMenu.hide();
	    }
	});
	
	dateMenu.show( );
}

function doLogin( root_path )
{
	var responseLogin = function( response ){
		if( response.responseText == 'ok' )
		{
			document.location = root_path + "ro/home/index.html";
		}
		else
		{
				Ext.MessageBox.alert( "FunFoll.com message","Bad username or password !<br/> Please try again !" );				
		}
	}
	Ext.Ajax.request({
	   url: root_path + 'application/ajax/www/loginUser.php',
	   success: responseLogin,
	   params: { 
	   	username: document.getElementById('username').value,
	   	password: document.getElementById('password').value
	    }
	});
	
}
