// Validator Object code
//___________________________________________
 
function Validator()
{
	this.isValid = true;
	this.validNumbers = '0123456789';
	this.validPhoneCharacters = '0123456789-.()+';
	this.validZipCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ,;_()';
	this.validAddressCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#0123456789- .,;_/()\'';
	this.validTextCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ';
}

new Validator();

function raiseError(message)
{
	if (this.isValid) alert(message);
	this.isValid = false;
}



// Name and Text Only
function V_validateTextChars(text, label)
{
	if (!this.isValid) return;
	if (text.value == '' || text.value.replace(/ /gi,'')=='')
	{
		this.raiseError('Please enter your ' + label);
		text.value='';
		text.focus();
		return false;
	}
	
	else if (!V_isTextValid(text.value))
	{
		this.raiseError(label + ' contains invalid characters.');
		text.select();
		return false;
	}
return true;
}

// for last name
function V_validateTextCharsLength(text, label)
{
	if (!this.isValid) return;
	if (this.V_isTwoCharsLong(text.value))
	{
		this.raiseError(label + ' must contain two or more characters.');
		text.select();
		return false;
	}
return true;
}

// Telephone Number
function V_validateTelFax(phone, label)
{	
	if (!this.isValid) return;
	if (phone.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		phone.focus();
		return false;
	} 
	else if (phone.value.length < 7)
	{
		this.raiseError(label + ' should have at least seven numbers.');
		phone.focus();
		return false;
	}
	// look for too many of the same chars
	else if (phone.value.search(/0{7,20}/) != (-1) || phone.value.search(/1{7,20}/) != (-1) || phone.value.search(/2{7,20}/) != (-1) || phone.value.search(/3{7,20}/) != (-1) || phone.value.search(/4{7,20}/) != (-1) || phone.value.search(/5{7,20}/) != (-1) || phone.value.search(/6{7,20}/) != (-1) || phone.value.search(/7{7,20}/) != (-1) || phone.value.search(/8{7,20}/) != (-1) || phone.value.search(/9{7,20}/) != (-1))
	{
		this.raiseError(label + ' appears to be invalid.');
		phone.focus();
		return false;
	}
	else if (!this.V_isPhoneNumeric(phone.value))
	{
		this.raiseError(label + ' should have only numbers and seperators.');
		phone.focus();
		return false;
	}
return true;
}

// for EMail
function V_validateEmail(email, label)
{
	if (!this.isValid) return;
	if (email.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		email.focus();
		return false;
	}
	if (email.value == 'noemail@aol.com' || email.value == 'noemail@yahoo.com' || email.value == 'noemail@hotmail.com' || email.value == 'noemail@netzero.com' || email.value == 'noemail@juno.com' || email.value == 'noemail@netscape.com' || email.value == 'noemail@prodegy.net' || email.value == 'noemail@msn.net')
	{
		this.raiseError('Please enter a valid ' + label);
		email.focus();
		return false;
	}
	else if (email.value.length < 6)
	{
		this.raiseError(label + ' must be at least six characters.');
		email.focus();
		return false;
	}
	if (email.value != '')
	{
		var str = email.value;
		var instancecounter = 0;

		str += '';
		intAt = str.indexOf( '@', 1 );							// the "@"
		intDot = str.lastIndexOf( '.' );						// the last "."
		namestr = str.substring( 0, intAt );  					// everything before the "@"
		domainstr = str.substring( intAt +1, str.length ); 		// everything after the "@"

		if ((str.indexOf(" ")!=-1) || (intAt == -1) || (intDot == -1 ) || (namestr.length == 0) || (domainstr.length == 0) || (intAt > intDot) || (domainstr.indexOf(".") <= 0))
		{
			this.raiseError(label + ' appears to be invalid.');
			email.select();
			return false;
		} else {
			// iterate through email address checking for
			// more than 1 @ sysmbol, or none at all
			for ( i = 0; i < str.length; i++ ) {
				if ((str.substring(i,i+1)) == "@" ) {
					instancecounter = instancecounter + 1;
				}
			}

			// Check to see if we have none, or more than one @ symbol
			if ((instancecounter > 1) || (instancecounter == 0 )) {
				this.raiseError(label + ' appears to be invalid.');
				email.select();
				return false;
			}
		}
	}
return true;
}

// URL Validation (new)
function V_validateURL(url, label)
{
	if (!this.isValid) return;
	if (url.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		url.focus();
		return false;
	}
	else if (url.value=='CD-ROM'  || url.value=='cd-rom' || url.value=='crrom' || url.value=='CDROM')
	{
		return true;
	}
	else if (url.value.length < 6)
	{
		this.raiseError(label + ' must be at least six characters.');
		url.focus();
		return false;
	}
	// perfect part by Behrooz
	if (url.value != '')
	{
		var str = url.value;
		var instancecounter = 0;
		str += '';
		intDot = str.lastIndexOf( '.' );						// the last "."
		namestr = str.substring( 0, intDot );  					// everything before the "."
		extensionstr = str.substring( intDot +1, str.length ); 		// everything after the "."

		if ((str.indexOf(" ")!=-1) || (intDot == -1 ) || (namestr.length == 0) || (namestr.length < 3) || (extensionstr.length == 0)  || (extensionstr.length < 2) )
		{
			this.raiseError(label + ' appears to be invalid.');
			url.select();
			return false;
		} else {
			
			// Check to see if there is an @ symbol
			var instancecounter = 0;
			for ( i = 0; i < str.length; i++ ) {
				if ((str.substring(i,i+1)) == "@" ) {
					instancecounter = instancecounter + 1;
				}
			}
			if (instancecounter > 0) {
				this.raiseError(label + ' appears to be invalid. [@ sign?]');
				url.select();
				return false;
			}
		}
	}
return true;
}
// for Approximate Area
function V_validateArea (area,label)
{
	if (!this.isValid) return;
	if (area.value == '')
	{
		this.raiseError('Approximate required Area must be specified.\nYou can change it at your contract signature time.');
		area.focus();
		return false;
	} 
	else if(!V_isNumeric(area.value))
		{
			this.raiseError('Area must be specified in NUMBERS of square meters.Use Numeric input.\nYou can change Area at your contract signature time.');
			area.select();
			return false;
		}
	else if((area.value%4)!=0)
		{	
			if ((area.value%3)!=0)
				{
				this.raiseError('Area must be either multiple of 4 (12,16,20,...) or 3 (12,15,18,...).  \nYou can change it at your contract signature time.');
				area.select();
				return false;
				}
		}
	return true;
}

function V_isNumeric(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validNumbers.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isPhoneNumeric(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validPhoneCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isZipValid(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validZipCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}
function V_isAddressValid(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validAddressCharacters.indexOf(field.charAt(i),0) == -1 || this.(field.charAt(i),0) != String.fromCharCode(0Dh)) return false;
	}
	return true;
}

function V_isTextValid(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validTextCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isTwoCharsLong(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (i>0) return false;
	}
	return true;
}
// for streetAddress
function V_validateStreetAddress (address,label)
{
	if (!this.isValid) return;
	if (address.value == '')
	{
		this.raiseError(label +' must be provided');
		address.focus();
		return false;
	} 
	else if (!V_isAddressValid (address.value))
		{
			this.raiseError (label+' contains invalid characters.');
			return false;
		}
	
	else if (address.value.length<10)
		{
			this.raiseError (label+' appears to be not entered completely.\nConsider revision with more than 10 characters.');
			address.select();
			return false;
		}
	return true;
}

// for password
function V_validatePassword (password,label)
{
	if (!this.isValid) return;
	if (password.value == '')
	{
		this.raiseError('You must pick a Password\nYou may change it later in your page.');
		password.focus();
		return false;
	} 
	else if (password.value.length>10)
		{
			this.raiseError ('Password must be 10 characters maximum.\nYou may change it later in your page.');
			password.select();
			return false;
		}
	else if (password.value.length<4)
		{
			this.raiseError ('It is not safe to take less than 3 characters password.\nSelect a password with at least 3 characters length.\nYou may change it later in your page.');
			password.select();
			return false;
		}
	return true;
}




Validator.prototype.raiseError = raiseError;
Validator.prototype.validateTextChars = V_validateTextChars;
Validator.prototype.validateTextCharsLength = V_validateTextCharsLength;
Validator.prototype.isNumeric = V_isNumeric;
Validator.prototype.isZipValid = V_isZipValid;
Validator.prototype.isTextValid = V_isTextValid;
Validator.prototype.isTwoCharsLong = V_isTwoCharsLong;
Validator.prototype.isPhoneNumeric = V_isPhoneNumeric;
Validator.prototype.validateEmail = V_validateEmail;