function isNumeric(s)
{
	return s.match(/^[0-9\s]+$/);
}

function isAlphaNumeric(s)
{
	if (s == '')
		return false;
	else
		return true;
	return s.match(/^[0-9A-Za-zåäöÅÄÖØøæÆ\-\s\.]+$/);
}

function isAlphabet(s)
{
	if (s == '')
		return false;
	else
		return true;
	return s.match(/^[A-Za-zåäöÅÄÖØøæÆ\-\s\.]+$/)
}

function isMobileNumber(s)
{
	return s.match(/^\+?\(?[0-9]*\)?[\-\s]?[0-9\s\-]+$/);
}

function isEmail(s){  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return emailPattern.test(s);  
}
	


function validateRegForm(form)
{
	var nonZero = [
	{
		node : "store",
		error : validationError.store
	},
	{
		node : "confirm",
		error : validationError.confirm
	}
	]
	
	var alphabet = [
	{
		node : "user_name_firstname",
		error : validationError.user_name_firstname
	},
	{
		node : "surname",
		error : validationError.surname
	},
	{
		node : "city",
		error : validationError.city
	}
	]
	
	var selected = [
	{
		node : "day",
		error : validationError.day
	},
	{
		node : "month",
		error : validationError.month
	},
	{
		node : "year",
		error : validationError.year
	},
	{
		node : "gender",
		error : validationError.gender
	}
	]
	var numeric = [
	{
		node : "postcode_zippeli",
		error : validationError.postcode_zippeli
	}]

	var nonNull = [
	{
		node : "password1", error : validationError.password
	},
	{
		node : "streetname", error : validationError.streetname
	},
	{
		node : "validation", error : validationError.captcha
	}]
	
	var errors = []
	
	errors = errors.concat(checkFields(selected, function(s) {return s != '----';}))
	errors = errors.concat(checkFields(alphabet, isAlphabet))
	
	errors = errors.concat(checkFields([{node: "con_email", error : validationError.emailNotMatched}], 
		function(s) {
			if (s == document.getElementById("user_email").value)
				return true;
			else
				return false;
		}));
	errors = errors.concat(checkFields([{node: "user_email", error : validationError.email}], isEmail));

	errors = errors.concat(checkFields(numeric, isNumeric));

	errors = errors.concat(checkFields(nonZero, function(v) {
		if (v == "0" || !v)
			return false
		else
			return true
	}))
	errors = errors.concat(checkFields(
		[{"node" : "mobilephone", error : validationError.mobilephone}],
	 isMobileNumber));
	
	errors = errors.concat(checkFields(
		nonNull, function(s) {if (s != '') return true; else return false;}));
	
	errors = errors.concat(checkFields(
		[{node:"password1", error: validationError.passwordMismatch}], 
			function(s) 
				{if (s == document.getElementById("password2").value) return true; else return false;}
	));
	
	return errors;
}

function checkFields(values, f)
{
	var errors = []

	for (var i in values)
	{
		var field = values[i];
		
		element = document.getElementById(field.node);
		
		if (!element)
			continue
		
		var value;
		if (element.tagName == "INPUT")
		{
			if (element.type == "checkbox")
			{
				value = element.checked;
			}
			else
			 	value = element.value;
		}
		else
			value = element.value;
		
		
		if (!f(value))
		{
			errors.push(field.error)
			element.oldColor = element.style.backgroundColor;
			element.style.backgroundColor = "#fddfee";
		}
		else
			element.style.backgroundColor = "#ffffff";
			
	}		
	
	return errors;
}

function doFormValidation(form)
{ 

	
	var errors = validateRegForm(form);

	if (errors.length)
	{
		var s = "";
		for (var i in errors)
		{
			var error = errors[i];
			s += error + "\n";
		}
	
		alert(s);
		
		return false;
	}
	
	return true;
}

