/*
	validateform.js
	for contact.html page in the dvr365 website
*/

/*
*	constructor for ValidateForm Class
*	this class will validate a form th make sure given field ids contain 
*	some data and an email field has a valid email entered
*/
function ValidateForm(fields, submitId, email)
{
	this.fieldIds = new Array();
	
	if ( !(fields instanceof Array ) )
		throw new Error("ValidateForm needs an array");
	
	for ( var i = 0; i != fields.length; i++ )
	{
		if ( typeof(fields[i]) == "string" )
		{
			var temp = document.getElementById(fields[i]);
			if ( temp != null )
				this.fieldIds[i] = temp;
		}
		else
			this.fieldIds[i] = fields;
	}
	
	if ( typeof(email) == 'string' )
		this.emailId = document.getElementById(email);
	else
		this.emailId = email;
		
	if ( typeof(submitId) == "string" )
		this.submitBtn = document.getElementById(submitId);
	else
		this.submitBtn = submitId;
	
	if ( this.submitBtn != null )
		this.regSubmit();
	else
		alert("not registed click event");
}

/*
*	registers the submit buttons click event with the validate method
*/
ValidateForm.prototype.regSubmit = function(submitBtn)
{	
	var thisObj = this;
	addEvent(this.submitBtn, 'click', function(e) { thisObj.validate.call(thisObj, e); });
}

/*
*	this is the method that controls the checking of the fields
*/
ValidateForm.prototype.validate = function(e)
{	
	var empty_tags = this.checkValues();
	
	if ( empty_tags.length != 0 )
	{
		this.alertUser(empty_tags);
		e.cancelable ? e.preventDefault() : e.returnValue = false;
	}
		
	if ( !this.emailCheck() )
	{
		alert("The email Adddress is not valid!");
		e.cancelable ? e.preventDefault() : e.returnValue = false;
	}
}

// checks all registered fields to see if the value has any data in it.
ValidateForm.prototype.checkValues = function()
{
	var empty = new Array();
	
	for (var i = 0, ii = 0; i != this.fieldIds.length; i++ )
	{
		if ( this.fieldIds[i].value.length == 0 )
			empty[ii++] = this.fieldIds[i].id;

	}
	
	return empty;
}

/*
*	display an alert box telling user which fields he has not filled in
*/
ValidateForm.prototype.alertUser = function(tag_names)
{
	var alert_string = "Error - empty fields in: " + tag_names.join("; ");
	
	alert(alert_string);
}

/*
*	this function check the email address for correcness 
*	 return flase if fails else true
*/
ValidateForm.prototype.emailCheck = function()
{
	var regex = /^.+@.+[\.].+$/;

	return regex.test(this.emailId.value);
}

/* check that a given field is a numeric string which can contain spaces as well as digits */
ValidateForm.validatePhone = function(phoneId, e)
{
	var phone;
	
	if ( typeof(phoneId) == 'string' )
	{
		phone = document.getElementById(phoneId);
		if ( phone == null )
			throw new Error('phoneId tag not found!');
	}
	else
		phone = phoneId;
		
	if ( !/^ *\d+[\d ]*$/.test(phone.value) )
	{
		alert('Your phone number must contain numbers or spaces only!');
		e.cancelable ? e.preventDefault() : e.returnValue = false;
	}
}

ValidateForm.regValidatePhone = function(submitId, phone)
{
	var submitBtn;
	
	if ( typeof(submitId) == "string" )
	{
		submitBtn = document.getElementById(submitId);
		if ( submitBtn == null )
			throw new Error('submit button not found!');
	}
	else
		submitBtn = submitId;
		
	addEvent(submitBtn, 'click', function(e) { ValidateForm.validatePhone(phone, e); });
}
	

// and array of forms to be validated - used by ValidatForm.init
ValidateForm.forms = new Array();

/*
*	can use this as an onload handler to creat store and use ValidatForm oblects. it stores each one as 
*	in an array so more tham one form on a page can be used.
* 	ids - an array of each of the forms fields to be checked: It may be an array of strings or elements.
*	subnitBtn - the id of the submit button: may be string or element.
*	email - the id of the email field to be validated: may be a string or an element. 
*/
ValidateForm.init = function(ids, submitBtn, email)
{
	ValidateForm.forms.push(new ValidateForm(ids, submitBtn, email));
}


/*
*	CeckSelectedFieldis used with a redio button set to validate a input tag if a certain
*	radio button is set
* 	radioId = id for radio input tags
*	fieldId = the taget input tag to validate
*	submitid = id of the submit button
*	regexp = a regular expresion used to validate the field
*/
function CheckSelectedField(radioId, fieldId, submitId, regexp)
{		
	if ( typeof(radioId) == 'string' )
	{
		this.radioBtn = document.getElementById(radioId);
		if ( this.radioBtn == null )
			throw new Error('no radio button of id ' + radioId + ' found!');
	}
	else
		this.radioBtn = radioId;
		
	if ( typeof(fieldId) == 'string')
	{
		this.field = document.getElementById(fieldId);
		if ( this.field == null )
			throw new Error('No input field called ' + fieldId + ' found!');
	}
	else
		this.field = fieldId;
	
	if ( typeof(submitId) == "string" )
	{
		this.submitBtn = document.getElementById(submitId);
		if ( this.submitBtn == null )
			throw new Error('submit button not found!');
	}
	else
		this.submitBtn = submitId;
	
	if ( typeof(regexp) == 'string' )
		this.fieldRegExp = new RegExp(regexp);
	else
		this.fieldRegExp = regexp;
	
	this.regSubmit();
}

/*
*	registers the submit buttons click event with the validate method
*/
CheckSelectedField.prototype.regSubmit = function()
{	
	var thisObj = this;
	addEvent(this.submitBtn, 'click', function(e) { thisObj.validate.call(thisObj, e); });
}


CheckSelectedField.prototype.validate = function(e)
{
	if ( this.radioBtn.checked && (this.field.value == "" || !this.isValidContent(this.field.value)) )
	{
		alert('Please provide us with the serial number of your machine (3 Characters followed by a number).');
		e.cancelable ? e.preventDefault() : e.returnValue = false;
	}
		
}

CheckSelectedField.prototype.isValidContent = function(val)
{
	return this.fieldRegExp.test(val);
}

/*
*	fields is an array of CheckSelecedField objects for us by init to create, register and store them.
*/
CheckSelectedField.fields = new Array();

CheckSelectedField.init = function(radioId, fieldId, submitId, regexp)
{
	CheckSelectedField.fields.push(new  CheckSelectedField(radioId, fieldId, submitId, regexp));
}
