	var errorField;
	var successField;

	function validatePasswordChange()
	{
		errorField = document.getElementById('errorField');
		successField = document.getElementById('success');

		var oldpassword = document.getElementById('oldpassword');
		var password = document.getElementById('password');
		var password2 = document.getElementById('password2');

		if (trim(oldpassword.value) == "")
		{
			errorField.innerHTML = "Old password input is obligatory for password change.";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (password.value != password2.value)
		{
			errorField.innerHTML = "Password confirmation failed.";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (password.value.length < 6 || password.value.length > 20)
		{
			errorField.innerHTML = "Password length should be 6-20 characters.";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (!checkForbidden(password.value))
		{
			errorField.innerHTML = "Forbidden characters in the password. Only Aa-Zz, 0-9 and '_' are allowed";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		return true;

	}

	function validateUpdateProfile()
	{
		errorField = document.getElementById('errorField');
		successField = document.getElementById('success');

		var minprice = document.getElementById('minpriceReg');
		var maxprice = document.getElementById('maxpriceReg');

		if (trim(minprice.value) != "")
		{
			if (!isNumeric(minprice.value))
			{
				errorField.innerHTML = "Price value should be numeric";
				if (successField != null)
					successField.style.display = "none";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
			if (parseInt(minprice.value) < 0)
			{
				errorField.innerHTML = "Price can't be a negative number";
				if (successField != null)
					successField.style.display = "none";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
		}

		if (trim(maxprice.value) != "")
		{
			if (!isNumeric(maxprice.value))
			{
				errorField.innerHTML = "Price should be a number";
				if (successField != null)
					successField.style.display = "none";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
			if (parseInt(maxprice.value) < 0)
			{
				errorField.innerHTML = "Price can't be a negative number";
				if (successField != null)
					successField.style.display = "none";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
		}

		if (trim(minprice.value) != "" && trim(maxprice.value) != "")
		{
			// Remove commas from price
			var minP = minprice.value.split(",");
			var maxP = maxprice.value.split(",");
			minP = minP.join("");
			maxP = maxP.join("");

			if (parseInt(minP) > parseInt(maxP))
			{
				errorField.innerHTML = "Minimum price should not be higher than maximum one";
				if (successField != null)
					successField.style.display = "none";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
		}

		return true;

	}

	function validateUpdateDetails()
	{
		errorField = document.getElementById('errorField');
		successField = document.getElementById('success');

		var fname = document.getElementById('firstname');
		var lname = document.getElementById('lastname');
		var email = document.getElementById('email');

		var address = document.getElementById('address');
		var postcode = document.getElementById('postcode');
		var country = document.getElementById('country');

		var fax = document.getElementById('fax');
		var tel = document.getElementById('tel');
		var mobile = document.getElementById('mobile');

		if (isArrayEmpty(new Array(fname.value, lname.value, address.value, postcode.value, email.value)))
		{
			errorField.innerHTML = "Some obligatory fields are empty. Fields with <span style='font-size:15px'>*</span> must be entered.";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (!validateEmail(email.value))
		{
			errorField.innerHTML = "Invalid e-mail.";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (trim(tel.value) == "" && trim(mobile.value) == "" && trim(fax.value) == "")
		{
			errorField.innerHTML = "You should provide at least one of the following: telephone, mobile phone or fax";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (country.value == getUkId() && !isValidPostcode(postcode.value))
		{
			errorField.innerHTML = "Invalid UK postcode.";
			if (successField != null)
				successField.style.display = "none";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		return true;

	}

	function validate()
	{
		errorField = document.getElementById('errorField');
		errorField.innerHTML = "";

		var fname = document.getElementById('firstname');
		var lname = document.getElementById('lastname');

		var email = document.getElementById('email');
		var email2 = document.getElementById('email2');

		var password = document.getElementById('password');
		var password2 = document.getElementById('password2');

		var address = document.getElementById('address');
		var postcode = document.getElementById('postcode');
		var country = document.getElementById('country');

		var fax = document.getElementById('fax');
		var tel = document.getElementById('tel');
		var mobile = document.getElementById('mobile');

		var minprice = document.getElementById('minpriceReg');
		var maxprice = document.getElementById('maxpriceReg');

		if (isArrayEmpty(new Array(fname.value, lname.value, address.value, postcode.value, email.value, password.value)))
		{
			errorField.innerHTML = "Some obligatory fields are empty. Fields with <span style='font-size:15px'>*</span> must be entered.";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (!validateEmail(email.value))
		{
			errorField.innerHTML = "Invalid e-mail.";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (email.value != email2.value)
		{
			errorField.innerHTML = "E-mail confirmation failed.";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (!checkUsername(email.value))
		{
			errorField.innerHTML = "User with this e-mail already exists in our system.";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (password.value != password2.value)
		{
			errorField.innerHTML = "Password confirmation failed.";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (password.value.length < 6 || password.value.length > 20)
		{
			errorField.innerHTML = "Password length should be 6-20 characters.";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (!checkForbidden(password.value))
		{
			errorField.innerHTML = "Forbidden characters in the password. Only Aa-Zz, 0-9 and '_' are allowed";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (trim(tel.value) == "" && trim(mobile.value) == "" && trim(fax.value) == "")
		{
			errorField.innerHTML = "You should provide at least one of the following: telephone, mobile phone or fax";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (country.value == getUkId() && !isValidPostcode(postcode.value))
		{
			errorField.innerHTML = "Invalid UK postcode";
			showHide(true);
			window.scroll(0,0);
			return false;
		}

		if (trim(minprice.value) != "")
		{
			if (!isNumeric(minprice.value))
			{
				errorField.innerHTML = "Price value should be numeric";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
			if (parseInt(minprice.value) < 0)
			{
				errorField.innerHTML = "Price can't be a negative number";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
		}

		if (trim(maxprice.value) != "")
		{
			if (!isNumeric(maxprice.value))
			{
				errorField.innerHTML = "Price should be a number";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
			if (parseInt(maxprice.value) < 0)
			{
				errorField.innerHTML = "Price can't be a negative number";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
		}
		else
		{
			errorField.innerHTML = "Desired price range is obligatory";
			showHide(true);
			window.scroll(0,0);
			return false;
		}


		if (trim(minprice.value) != "" && trim(maxprice.value) != "")
		{
			// Remove commas from price
			var minP = minprice.value.split(",");
			var maxP = maxprice.value.split(",");
			minP = minP.join("");
			maxP = maxP.join("");

			if (parseInt(minP) > parseInt(maxP))
			{
				errorField.innerHTML = "Minimum price should not be higher than maximum one";
				showHide(true);
				window.scroll(0,0);
				return false;
			}
		}

		return true;
	}

	function checkForbidden(str)
	{
		var exp = /^[_a-zA-Z0-9]+$/;
		if (exp.test(str) === true)
			return true;
		return false;
	}

	function isArrayEmpty(array)
	{
		var i;
		for (i=0;i<array.length;i++)
		{
			if (trim(array[i]) == "")
				return true;
		}
		return false;
	}

	function showHide(display)
	{
		 errorField = document.getElementById('errorField');
		(display) ? errorField.style.display = "block" : "none";
	}

	function validateEmail(strEmail)
	{
		var regExpEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if( regExpEmail.test(strEmail) === true ) return true;
		return false;
	}

	function trim (str)
	{
		str = str.replace(/^\s+/, '');
		for (var i = str.length - 1; i > 0; i--)
		{
			if (/\S/.test(str.charAt(i)))
			{
				str = str.substring(0, i + 1);
				break;
			}
		}
		return str;
	}

	function getUkId ()
	{
		ajax = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		ajax.open("GET", "ajax/getUkId.php", false);
		ajax.send(null);
		var result = ajax.responseText;
		return result;
	}

	function checkUsername (email)
	{
		ajax = (window.XMLHttpRequest)? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		ajax.open("GET", "ajax/checkUsername.php?username=" + email, false);
		ajax.send(null);
		var result = ajax.responseText;
		return (parseInt(result) == 0);
	}

	function checkUser (username, pass)
	{
		ajax = (window.XMLHttpRequest)? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		ajax.open("GET", "ajax/checkUser.php?username=" + username + "&password=" + pass, false);
		ajax.send(null);
		var result = ajax.responseText;
		return (parseInt(result) == 1);
	}

	function isValidPostcode (p)
	{
		p = p.toUpperCase();
		var pattern = /GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2}/;
		return p.match(pattern);
	}

	function isNumeric (vTestValue)
	{
		// put the TEST value into a string object variable
		var sField = new String(this.trim(vTestValue));

		// check for a length of 0 - if so, return false
		if(sField.length==0) { return false; }
		else if(sField.length==1 && (sField.charAt(0) == '.' || sField.charAt(0) == ',' || (sField.charAt(0) == '-'))) { return false; }

		// loop through each character of the string
		for(var x=0; x < sField.length; x++) {
			// if the character is < 0 or > 9, return false (not a number)
			if((sField.charAt(x) >= '0' && sField.charAt(x) <= '9') || sField.charAt(x) == '.' || sField.charAt(x) == ',' || (sField.charAt(x) == '-' && x==0)) { /* do nothing */ }
			else { return false; }
		}

		// made it through the loop - we have a number
		return true;
	}
