/* 
	Clean Form Validation was written from scratch by Marc Grabanski
// http://marcgrabanski.com/code/clean-form-validation
/* Under the Creative Commons Licence http://creativecommons.org/licenses/by/3.0/
	Share or Remix it but please Attribute the authors. */
	

var contactValidator = 
{
	init: function (settings) 
	{
		this.settings = settings;
		this.form = document.getElementById(this.settings["formId"]);
		formInputs = this.form.getElementsByTagName("input");
	},
	control: function () 
	{
		error = contactValidator.validate();
			
		if(error.length < 1) 
		{
			//alert("no errors");
			document.form1.submit();
			return true;
		} 
		else 
		{
			contactValidator.printError(error);
			return false;
		}
	},
	validate: function () 
	{
		error = '';
		
		validationTypes = new Array("isRequired", "isEmail", "isNumeric", "IsChecked");
		
		for(n=0; n<validationTypes.length; n++) 
		{
			var x = this.settings[validationTypes[n]];
			
			if(x != null) 
			{
				for(i=0; i<x.length; i++) 
				{
					inputField = document.getElementById(x[i][0]);
					inputDefault = x[i][1];
					
					switch (validationTypes[n]) 
					{
						case "isRequired" 	: valid = isRequired(inputField.value, inputDefault); errorMsg = "is a required field."; break;
						case "isEmail" 		: valid = isEmail(inputField.value, inputDefault); errorMsg = "is an invalid email address."; break;
						case "isNumeric" 	: valid = isNumeric(inputField.value, inputDefault); errorMsg = "can only be a number."; break;
						case "IsChecked" 	: valid = IsChecked(inputField.value); errorMsg = "you have to accept the billing."; break;
					}
					//alert("valid: "+valid+" / "+inputField.value);
					
					if(!valid) 
					{
						error += x[i][1]+" "+errorMsg+"\n";
						inputField.style.background = this.settings["errorColors"][0];
						inputField.style.border = "1px solid "+this.settings["errorColors"][1];
					} 
					else 
					{
						inputField.style.background = this.settings["inputColors"][0];
						inputField.style.border = "1px solid "+this.settings["inputColors"][1];
					}
				}
			}
		}
		
		return error;
	},
	printError: function (error) 
	{
		alert(error);
	}
};

var tafValidator = 
{
	init: function (settings) 
	{
		this.settings = settings;
		this.form = document.getElementById(this.settings["formId"]);
		formInputs = this.form.getElementsByTagName("input");
	},
	control: function () 
	{
		error = tafValidator.validate();
			
		if(error.length < 1) 
		{
			//alert("no errors");
			document.tellafriend_form.submit();
			return true;
		} 
		else 
		{
			tafValidator.printError(error);
			return false;
		}
	},
	validate: function () 
	{
		error = '';
		
		validationTypes = new Array("isRequired", "isEmail", "isNumeric", "isMatching");
		
		for(n=0; n<validationTypes.length; n++) 
		{
			var x = this.settings[validationTypes[n]];
			
			if(x != null) 
			{
				for(i=0; i<x.length; i++) 
				{
					inputField = document.getElementById(x[i][0]);
					inputDefault = x[i][1];
					
					switch (validationTypes[n]) 
					{
						case "isRequired" 	: valid = isRequired(inputField.value, inputDefault); errorMsg = "is a required field."; break;
						case "isEmail" 		: valid = isEmail(inputField.value, inputDefault); errorMsg = "is an invalid email address."; break;
						case "isNumeric" 	: valid = isNumeric(inputField.value, inputDefault); errorMsg = "can only be a number."; break;
						case "isMatching" 	: valid = IsMatch(inputField.value, inputDefault); errorMsg = "is not matching."; break;
					}
					//alert("valid: "+valid+" / "+inputField.value);
					
					if(!valid) 
					{
						if(x[i][1] == "67huz89")
						{
							error += "Code "+errorMsg+"\n";
						}
						else
						{
							error += x[i][1]+" "+errorMsg+"\n";
						}
						inputField.style.background = this.settings["errorColors"][0];
						inputField.style.border = "1px solid "+this.settings["errorColors"][1];
					} 
					else 
					{
						inputField.style.background = this.settings["inputColors"][0];
						inputField.style.border = "1px solid "+this.settings["inputColors"][1];
					}
				}
			}
		}
		
		return error;
	},
	printError: function (error) 
	{
		alert(error);
	}
};



/**
*	returns true if the string is not empty
*/
function isRequired(str, def)
{
	if(str == null || str.length == 0 || str == def)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/**
*	returns true if the string is a valid email
*/
function isEmail(str, def)
{
	if(!isRequired(str, def)) return false;
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(str);
}

/*
*	------------------------------------------------------------------------
* 	CHECKED
*	------------------------------------------------------------------------
*/
function IsChecked(str) 
{
   if ((str.checked != true)) return false;
   else return true;
}

/**
*	returns true if the string only contains characters 0-9 and is not null
*/
function isNumeric(str, def)
{
	if(!isRequired(str, def)) return false;
	var reg = /[\D]/g
	
	if (reg.test(str)) return false;
	return true;
}

/**
*	is matching
*/
function IsMatch(str, def)
{
	//if(!isRequired(str, def)) return false;
	
	//alert("IsMatch: "+str+" , "+def);

	if (str != def) return false;
	else return true;
}
