UserValidator = {};

UserValidator.initialize = function(formId, modeVal, fieldNum, submitId)
{
	UserValidator.currentSelector = '';
	UserValidator.currentForm = formId;
	UserValidator.mode = modeVal;
	UserValidator.fieldNumToValidate = fieldNum;
	UserValidator.submitId = submitId;
	Utilities.getElement( UserValidator.submitId ).disabled = true;
	UserValidator.field = new Array();
}

UserValidator.setMode = function(modeVal)
{
	UserValidator.mode = modeVal;
	Utilities.getElement( UserValidator.submitId ).value = modeVal;
	if (modeVal=="Login" ) Utilities.getElement( UserValidator.submitId ).disabled = false;
		else Utilities.getElement( UserValidator.submitId ).disabled = true;
	Utilities.getElement( UserValidator.currentForm ).action += "?mode="+ modeVal;
}

UserValidator.validate = function(selector, value)
{
	if(selector != '' && value != '')
	{
		UserValidator.currentSelector = selector;
		
		var url = "/serviceConnector.php?object=UserManager&method=verifyUserInformation&params="+ selector +","+ value;
		AjaxUpdater.Update("GET", url, UserValidator.onValidation);
	}
	else if(selector != '' && value == '')
	{
		Utilities.getElement( selector ).style.backgroundColor = '#ffffff';
		Utilities.getElement( selector +"Message" ).innerHTML = '';
		if (UserValidator.field[selector] == true ) { UserValidator.fieldNumToValidate++; UserValidator.field[selector] = false; }
	}

		if(UserValidator.fieldNumToValidate == 0)
		{
			Utilities.getElement( UserValidator.submitId ).disabled = false;
		} else {
			Utilities.getElement( UserValidator.submitId ).disabled = true;
		}
}

UserValidator.onValidation = function()
{
	if (!UserValidator.field[UserValidator.currentSelector]) UserValidator.field[UserValidator.currentSelector]=false;
	if(Ajax.checkReadyState('loading') == "OK")
	{
		var color = '#ffffff';
		if(Ajax.getResponse().firstChild.data == 'success')
		{
			color = '#ff9999';
			Utilities.getElement( UserValidator.currentSelector +"Message" ).innerHTML = 'Ilyen adat már van regisztrálva!';
				if (UserValidator.field[UserValidator.currentSelector] == true ) { UserValidator.fieldNumToValidate++; UserValidator.field[UserValidator.currentSelector] = false; }
		}
		else
		{
			color = '#ccff99';
			Utilities.getElement( UserValidator.currentSelector  +"Message" ).innerHTML = 'OK';
				if (UserValidator.field[UserValidator.currentSelector] == false ) { UserValidator.fieldNumToValidate--; UserValidator.field[UserValidator.currentSelector] = true; }
		}
		Utilities.getElement( UserValidator.currentSelector ).style.backgroundColor = color;
		
	}
}

UserValidator.passvalidate = function(passid, value1, value2)
{
	if (!UserValidator.field[passid]) UserValidator.field[passid]=false;

	if(value1 != '' || value2!='')
	{
		if  (value1!=value2) {
				msg =  'A ket jelszo nem egyezik meg!';
				color = '#ff9999';
				if (UserValidator.field[passid] == true ) { UserValidator.fieldNumToValidate++; UserValidator.field[passid] = false; }
			} else if (value1.length<6) {
				msg = 'A jelszo hossza minimum 6 karakter!';
				color = '#ff9999';
				if (UserValidator.field[passid] == true ) { UserValidator.fieldNumToValidate++; UserValidator.field[passid] = false; }
			} else {
				msg = 'OK';
				color = '#ccff99';
				if (UserValidator.field[passid] == false ) { UserValidator.fieldNumToValidate--; UserValidator.field[passid] = true; }
			}
	}
	else if(value1 == '' && value2 == '')
	{
				msg = '';
				color = '#ffffff';
	}
	Utilities.getElement( passid ).style.backgroundColor = color;
	Utilities.getElement( passid +"Message" ).innerHTML = msg;

		if(UserValidator.fieldNumToValidate == 0)
		{
			Utilities.getElement( UserValidator.submitId ).disabled = false;
		} else {
			Utilities.getElement( UserValidator.submitId ).disabled = true;
		}
}


