function Language()
{
	this.phone_ext_not_allowed = 'Extension is not allowed in %s.';
	this.invalid_chars = '%s contains one of the following invalid characters';
	this.invalid_chars_are = ' %s';
	this.too_short = '%s is too short.';
	this.too_long = '%s is too long.';
	this.ext_too_short = '%s extension is too short.';
	this.ext_too_long = '%s extension is too long.';
	this.field_not_valid = '%s is not in a valid format.';
	this.enter_value = 'Please enter a value in %s.';
	this.not_numeric = '%s must be a number.';
	this.passwords_dont_match = 'Your password and confirmation do not match.';
}

var lang = new Language();
function Validation()
{
	this.error = new Array();
	this.add_error = function(err)
	{
		this.error[this.error.length] = err;
	}

	this.error_as_string = function()
	{
		return this.error.join("\n");
	}

	this.check_phone_isd = function(pnumber, fieldname, custom_error)
	{
		if( pnumber.length < 1 )
		{
			var err = 'Please enter your %s.';
			var this_error = err.replace(/%s/, fieldname );
			this.add_error( this_error );
			return false;
		}
		return this.check_only_number(pnumber, fieldname, custom_error);
	}
	
	this.check_phone = function(pnumber, fieldname, noext)
	{
		// check for an extension
		var parts = pnumber.split(/ext|#/);
		
		if( noext && typeof parts[1] == 'string' && parts[1].length > 0 )
		{
			var this_error = lang.phone_ext_not_allowed.replace(/%s/,fieldname);
			this.add_error( this_error );
			return false;
		}

		// remove any characters that we don't care about
		var fpart = parts[0].replace(/[\-\.\)\(\+\s]/g,"");
		if( fpart.match(/[^\d]/) )
		{
			var this_error = lang.invalid_chars.replace(/%s/,fieldname);
			if( fpart.length > 0 )
			{
				this_error += " " + lang.invalid_chars_are.replace(/%s/, this.get_invalids(fpart,/([^\d])/g,1));
			}

			this.add_error( this_error );
			return false;
		}

		// make sure we have some numbers
		if( fpart.length < 7 )
		{
			var this_error = lang.too_short.replace(/%s/,fieldname );
			this.add_error( this_error );
			return false;
		}

		// make sure we don't have too many!
		// (i've seen several validators that say 13 should be the max, but I'm not convinced they allow for leading zeros or ones. 
		// a couple of extra shouldn't matter much)
		if( fpart.length > 15 )
		{
			var this_error = lang.too_long.replace(/%s/,fieldname);
			this.add_error(this_error);
			return false;
		}

		// now check our extension
		if( typeof parts[1] == 'string' && parts[1].length > 0 )
		{
			var ext = parts[1].replace(/[^\d]/,"");
			if( ext.length < 1 )
			{
				var this_error = lang.ext_too_long.replace(/%s/,fieldname);
				this.add_error( this_error );
				return false;
			}

			if( ext.length > 6 )
			{
				var this_error = lang.ext_too_short.replace(/%s/,fieldname);
				this.add_error( this_error );
				return false;
			}
		}

		return true;
	}

	this.check_characters = function(str,name,regex,antiregex,custom_error)
	{
		if( str.length < 1 )
		{
			var err = 'Please enter your %s';
			var this_error = err.replace(/%s/, name );
			this.add_error( this_error );
			return false;
		}
		
		// ignore non-printing characters, server-side should strip them completely.
		str.replace(/[^[:print:]]/g,'');
		
		if( str.match(regex) ){
			return true;
		}else if(custom_error){
			this.add_error( custom_error );
			return false;
		}else{
			var invalids = this.get_invalids( str, antiregex );
			var this_error = lang.invalid_chars.replace(/%s/,name);
			if( str.length > 0 )
			{
				this_error += ' ' + lang.invalid_chars_are.replace(/%s/,invalids);
			}

			this.add_error( this_error );
			return false;
		}
	}

	this.check_store_name = function(value, fieldname, custom_error )
	{
		return this.check_characters(value,fieldname,/^[A-Za-z0-9\s\']+$/, /([^A-Za-z0-9\s\'])/g, custom_error );
	}

	this.get_invalids = function(str,regex, opt)
	{
		if(opt==1){
			var invalids = str.match(regex);
			var inv_str = invalids.join(",",invalids);
			return inv_str;
		}	
		return regex;
	}

	this.check_company = function( value, fieldname, custom_error )
	{
		return this.check_characters(value, fieldname, /^[A-Za-z0-9\s\.\-\,\']+$/, /([^A-Za-z0-9\s\.\-\,\'])/g, custom_error);
	}

	this.check_numeric = function( num, fieldname, custom_error )
	{

		if( typeof num == 'number' || ( typeof num == 'string' && num.match(/^[\d\.]+$/) ) )
		{
			return true;
		}

		if( typeof custom_error != 'string' || custom_error.length < 1 )
		{
			var this_error = lang.not_numeric.replace(/%s/, fieldname );
		}
		else
		{
			this_error = custom_error;
		}

		this.add_error( this_error );
		return false;
	}
	
	this.check_only_number = function( num, fieldname, custom_error )
	{

		if( typeof num == 'number' || ( typeof num == 'string' && num.match(/^[\d]+$/) ) )
		{
			return true;
		}

		if( typeof custom_error != 'string' || custom_error.length < 1 )
		{
			var this_error = lang.not_numeric.replace(/%s/, fieldname );
		}
		else
		{
			this_error = custom_error;
		}

		this.add_error( this_error );
		return false;
	}	
	
	this.check_free_text = function(value, fieldname, custom_error)
	{
		//return this.check_characters( value, fieldname, /^[A-Za-z0-9\s\,\.\?\:\;\+\!\$_\"\'\)\(\\\/\-]+$/, /([^A-Za-z0-9\s\,\.\?\:\;\+\!\$_\"\'\(\)\\\/\-])/g );
		return this.check_characters( value, fieldname, /^[A-Za-z0-9\s\?\&\$\']+$/, '~`()-_+=\][{}|;:"/.,><!@#%^*', custom_error );
	}

	this.check_free_desc_text = function(value, fieldname, custom_error)
	{
		//return this.check_characters( value, fieldname, /^[A-Za-z0-9\s\!\@\#\$\%\^\&\*\-\?]+$/, /([^A-Za-z0-9\s\!\@\#\$\%\^\&\*\-\?])/g, custom_error );
		return this.check_characters( value, fieldname, /^[A-Za-z0-9\s\!\@\#\$\%\^\&\*\-\?\;\:\"\'\,\.\(\)\=\+]+$/, '~`_\][{}|/><\'', custom_error );
	}
	
	this.check_name = function( value, fieldname)
	{
		if( value.length < 2 )
		{
			var this_error = lang.too_short.replace( /%s/, fieldname );
			this.add_error( this_error );
			return false;
		}
		return this.check_characters( value, fieldname, /^[A-Za-z\s\'\.]+$/, /([^A-Za-z\s\'\.])/g, 'Please only use letters dashes spaces periods or \' in Name.' );
	}

	this.check_first_name = function( value, fieldname)
	{
		if( value.length < 2 )
		{
			var this_error = lang.too_short.replace( /%s/, fieldname );
			this.add_error( this_error );
			return false;
		}
		return this.check_characters( value, fieldname, /^[A-Za-z\s\'\.]+$/, /([^A-Za-z\s\'\.])/g, 'Please only use letters dashes spaces periods or \' in First Name.' );
	}

	this.check_last_name = function(value, fieldname)
	{
		if( value.length < 3 )
		{
			var this_error = lang.too_short.replace( /%s/, fieldname );
			this.add_error( this_error );
			return false;
		}
		return this.check_characters( value, fieldname, /^[A-Za-z\s\'\.]+$/,/([^A-Za-z\s\'\.])/g, 'Please only use letters dashes spaces periods or \' in Last Name.' );
	}

	this.check_email = function(value, fieldname)
	{
		if( value.match(/^[A-Za-z0-9\_\.\-]+\@[A-Za-z0-9\.\-]+$/) )
		{
			return true;
		}
		var this_error = lang.field_not_valid.replace(/%s/,fieldname);
		this.add_error( this_error );
	}

	this.check_address = function(value,fieldname)
	{
		if( value.length < 3 )
		{
			var this_error = lang.too_short.replace( /%s/, fieldname );
			this.add_error( this_error );
			return false;
		}
		
		return this.check_characters( value, fieldname,/^[A-Za-z0-9\s\.\,\-]+$/,/([^A-Za-z0-9\s\.\,\-])/g, 'Please only use letters, numbers, spaces, dashes, periods and commas in Street Address' );
	}

	this.check_apt = function(value, fieldname)
	{
		return this.check_characters( value, fieldname,/^[A-Za-z0-9\-\s]+$/,/([^A-Za-z0-9\-\s])/g, 'Please only use letters numbers dashes and spaces in Apt/Suite' );
	}

	this.check_city = function( value,fieldname)
	{
		if( value.length < 2 )
		{
			var this_error = lang.too_short.replace( /%s/, fieldname );
			this.add_error( this_error );
			return false;
		}

		return this.check_characters( value, fieldname,/^[A-Za-z\s\'\.]+$/,/([^A-Za-z\s\'\.])/g, 'Please only use letters periods and spaces in City');
	}

	this.check_state = function(value, fieldname)
	{
		if( value.length < 2 )
		{
			var this_error = lang.too_short.replace( /%s/, fieldname );
			this.add_error( this_error );
			return false;
		}

		return this.check_characters( value, fieldname,/^[A-Za-z\s]+$/,/([^A-Za-z\s])/g, 'Please only use letters periods and spaces in State' );
	}

	this.check_postcode = function(value, fieldname)
	{
		if( value.length < 4 )
		{
			var this_error = lang.too_short.replace( /%s/, fieldname );
			this.add_error( this_error );
			return false;
		}

		return this.check_characters( value, fieldname,/^[A-Za-z0-9\s\-]+$/,/([^A-Za-z0-9\s\-])/g, 'Zip/Postal Code contains one of the following invalid characters ~`()_+=\][{}|\';:"/.,><');
	}

	this.check_im = function(value,fieldname)
	{
		return this.check_characters( value, fieldname, /^[A-Za-z0-9\_\@\s\.\-]+$/,/([^A-Za-z0-9\_\@\s\.\-])/g);
	}

	this.check_google_merchant = function(value,fieldname)
	{
		if( value.length < 10 )
		{
			var this_error = lang.too_short.replace(/%s/, fieldname );
			this.add_error( this_error );
			return false;
		}

		return this.check_characters( value, fieldname,/^[0-9\-]+/,/([^0-9\-])/g);
	}
	
	this.check_marketplace_name = function(value,fieldname,custom_error)
	{
		if( value.length < 3 )
		{
			var this_error = lang.too_short.replace(/%s/, fieldname );
			this.add_error( this_error );
			return false;
		}

		return this.check_characters( value, fieldname, /^[A-Za-z0-9\s,\-]+$/, /([^A-Za-z0-9\s,\-])/g, custom_error);				
	}
	
	this.check_item_name = function(value,fieldname,custom_error)
	{
		if( value.length < 3 )
		{
			var this_error = lang.too_short.replace(/%s/, fieldname );
			this.add_error( this_error );
			return false;
		}

		return this.check_characters( value, fieldname, /^[A-Za-z0-9\s,\.\-]+$/, /([^A-Za-z0-9\s,\.-])/g, custom_error);				
	}

	this.check_item_keywords = function(value,fieldname,custom_error)
	{
		$ret = this.check_characters( value, fieldname, '/^[A-Za-z0-9\s,\-\.]+$/','/([^A-Za-z0-9\s,\-\.])/', custom_error );
		if(ret){
			arr=value.split(',');
			if(arr.length>5){
				this.add_error( "Item Keywords can not be more than 5." );
				return false;
			}
		}else{
			return ret;
		}	
	}

	this.check_captcha = function(value,fieldname,custom_error)
	{
		return this.check_characters( value, fieldname, /^[A-Za-z0-9\s\?\&\$\'\@\#\$\%\^\&\*]+$/, '~`()-_+=\][{}|\';:"/.,><', custom_error );
	}
	
	this.check_password = function(pw,confirm_pw,fieldname,confirm_field)
	{
		if( pw.length < 6 )
		{
			this.add_error( lang.too_short.replace(/%s/,fieldname) );
			return false;
		}
		else if( pw != confirm_pw )
		{
			this.add_error( lang.passwords_dont_match );
			return false;
		}

		return true;
	}
}
