var color_norm = "#333333";
var color_fail = "#FA220B";

var last_fail = '';
var error = 'error';

function validLoginForm()
{
	var state = false;              
	state = fieldsCheck('pass', 'user');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validRegForm()
{
	var state = false;                
	state = fieldsCheck('gender', 'pass2', 'pass', 'email', 'birthday', 'lastName', 'firstName');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validRemindForm()
{
	var state = false;              
	state = fieldsCheck('email');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validReminderForm()
{
	var state = false;
	state = fieldsCheck('date2', 'text');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validProfileForm()
{
	var state = false;   
	state = fieldsCheck('gender', 'birthday', 'lastName', 'firstName', 'email');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validPresentForm()
{
	var state = false;   
	state = fieldsCheck('title', 'description');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validPassForm()
{
	var state = false;   
	error = 'error2';
	state = fieldsCheck('pass2', 'pass', 'old_pass');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validInviteForm()
{
	var state = false;  
	state = fieldsCheck('text', 'email');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validRegInviteForm()
{
	var state = false;  
	state = fieldsCheck('gender', 'email', 'birthday', 'lastName', 'firstName', 'pass2', 'pass');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function fieldsCheck()
{
	var state = true;
	document.getElementById(error).style.display = 'block';
    for (var n = 0; n < arguments.length; n++)
	{
		document.getElementById(arguments[n] + '_l').style.color = color_norm;
	}
    for (var n = 0; n < arguments.length; n++)
	{
		if (arguments[n] == 'email')
		{
			if (emailCheck(document.getElementById('email').value) == false)
			{
				last_fail = arguments[n];
				document.getElementById(arguments[n] + '_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_email;
				fadeIn(error, 0);
			}
		}
		else if (arguments[n] == 'birthday' || arguments[n] == 'date')
		{
			var failed = 0;
			if (document.getElementById('day').value == "")
			{
				last_fail = 'day';
				failed = 1;
			}
			if (document.getElementById('month').value == "")
			{
				last_fail = 'month';
				failed = 1;
			}
			if (document.getElementById('year').value == "")
			{
				last_fail = 'year';
				failed = 1;
			}
			if (failed == 1)
			{
				document.getElementById(arguments[n] + '_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_fields;
				fadeIn(error, 0);
			}
		}
		else if (arguments[n] == 'date2')
		{
			var failed = 0;
			if (document.getElementById('day').value == "")
			{
				last_fail = 'day';
				failed = 1;
			}
			if (document.getElementById('month').value == "")
			{
				last_fail = 'month';
				failed = 1;
			}
			if (failed == 1)
			{
				document.getElementById(arguments[n] + '_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_fields;
				fadeIn(error, 0);
			}
		}
		else if (arguments[n] == 'gender')
		{
			if ((document.getElementById('male').checked == false) && (document.getElementById('female').checked == false))
			{
				last_fail = 'male';
				state = false;
				document.getElementById(arguments[n] + '_l').style.color = color_fail;
				document.getElementById(error).innerHTML = err_fields;
				fadeIn(error, 0);
			}
		}
		else if (arguments[n] == 'pass2')
		{
			document.getElementById('pass_l').style.color = color_norm;
			document.getElementById('pass2_l').style.color = color_norm;
            if (document.getElementById('pass').value != "" && document.getElementById('pass2').value != "")
            {
				if (document.getElementById('pass').value != document.getElementById('pass2').value)
				{
					last_fail = 'pass';
					document.getElementById('pass_l').style.color = color_fail;
					document.getElementById('pass2_l').style.color = color_fail;
					state = false;
					document.getElementById(error).innerHTML = err_pass2;
					fadeIn(error, 0);
				}
			}
			else
			{
				last_fail = 'pass';
				document.getElementById('pass_l').style.color = color_fail;
				document.getElementById('pass2_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_fields;
				fadeIn(error, 0);
            }
        }
		else if (document.getElementById(arguments[n]).value == 0)
		{
			last_fail = arguments[n];
			document.getElementById(arguments[n] + '_l').style.color = color_fail;
			state = false;
			document.getElementById(error).innerHTML = err_fields;
			fadeIn(error, 0);
		}
	}
	return state; 
}

function emailCheck (emailStr)
{
	if(emailStr == "")
	{
		return false;
	}
    var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|lt|ru|ro|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);

	if (matchArray == null)
	{
		return false;
	}

	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++)
	{
		if (user.charCodeAt(i) > 127)
		{
			return false;
		}
	}

	for (i = 0; i < domain.length; i++)
	{
		if (domain.charCodeAt(i) > 127)
		{
			return false;
		}
	}

	if (user.match(userPat) == null)
	{
		return false;
	}

	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null)
    {
		for (i = 1; i <= 4; i++)
		{
			if (IPArray[i] > 255)
			{
				return false;
			}
		}
		return true;
    }

	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i = 0; i < len; i++)
	{
		if (domArr[i].search(atomPat) == -1)
		{
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1)
	{
		return false;
	}

	if (len < 2)
	{
		return false;
	}
}

function fadeIn(objId, opacity) 
{
    if (document.getElementById)
	{
		if (opacity <= 100)
        {
			document.getElementById(objId).style.MozOpacity = opacity / 100;
			document.getElementById(objId).style.filter = "alpha(opacity:" + opacity + ")";
			opacity += 10;
			window.setTimeout("fadeIn('" + objId + "'," + opacity + ")", 100);
        }
	}
}

function fadeOut(objId, opacity)
{
	if (document.getElementById)
	{
		if (opacity >= 0)
        {
			document.getElementById(objId).style.MozOpacity = opacity / 100;
			document.getElementById(objId).style.filter = "alpha(opacity:" + opacity + ")";
			opacity -= 10;
			window.setTimeout("fadeOut('" + objId + "'," + opacity + ")", 100);
		}
	}
}