/**
* Custom Validation Script
*
* This simple validation script gives you simple but complete control in client-side validating your files.
*
* author: J.Saladf
*
*/
var errors = new Array(); // holds errors
var exclude_entries = new Array('First Name','Last Name','Postcode','Email','Company','Business Telephone'); // Exclude array: considered empty
var OK = true; // white list approach

// Validate form
function validateCRMForm()
{
	errors = new Array(); // Reset errors
	OK = true;

	OK = validateEmpty(); // Validat Empty Fields
	if (OK) {
		OK = validateEmail();  // Validate Email
	}
	
	if (OK) {
		clearErrorMessages();
		var data = jQuery('#web-form').serialize();
		// Ajax post to enable session data to be stored
		var confirm_url  =  jQuery('#web-form-url').val();

		// pass the data to the correct model to store the data
		jQuery.ajax({
				type:			"POST",
				url:			confirm_url,
				async: 			false,
				data:			'&'+data					
		});
		// Post form data
		return true;
	} else {
		displayErrorMessages();
		return false;
	}
	return false;
}

// Validate fields
var validated_fields = new Array();
function emptyField(field) {
	var value 	= field.value;
	var name 	= field.name;
	
	// If the text in the textfield is the standard text then clear it otherwise do nothing
	if (jQuery.inArray(value, exclude_entries) == -1) {
		// Do nothing - valid entry
	}else {
		// Store for repopulating
		validated_fields[name] = value;
		field.value = '';
	}
}

/**
* Repopulate required fields (explanation in fields)
*/ 
function popField(field) { 
	var value 	= field.value;
	var name 	= field.name;
	
	// If the field is not empty hold the current value
	if (value.length == 0) {
		field.value = validated_fields[name];
	}
}

// Validate empty fields
function validateEmpty()
{
	jQuery(".required").each(function(i)
	{
		var field_name 	= jQuery(this).attr('name');
		var field_value = jQuery(this).val();
		if (isEmpty(field_value)) {
			setErrorBackgroundColor(this);
			setErrorMessage(jQuery(this).attr('title'));
			OK = false;
		} else {
			clearErrorBackgroundColor(this);
		}
    });
    
    return OK;
}

// Validate Email address
function validateEmail()
{
	jQuery(".email").each(function(i)
	{
		var field_name 	= jQuery(this).attr('name');
		var field_value = jQuery(this).val();
		if (isValidEmail(field_value)) {
			clearErrorBackgroundColor(this);
			OK = true;
		}else {
			setErrorBackgroundColor(this);
			setErrorMessage(jQuery(this).attr('title'));
			OK = false;
		}
    });
    
    return OK;
}

// Add an error to the errors holder
function setErrorMessage(error)
{
	if (error.length != 0 && error != null) {
		errors.push(error);
	}
}

// Clear errors
function clearErrorMessages() {
	jQuery('#innovation-form-errors').html('');
}

// Display errors
function displayErrorMessages() {
	jQuery('#innovation-form-errors').html('');
	for(var i = 0; i < errors.length; i++)
	{
		var errormessage = '<li>* '+errors[i]+'</li>';
		jQuery('#innovation-form-errors').append(errormessage);
	}
	
	jQuery('#innovation-form-errors').show();
}

// Clear background color
function clearErrorBackgroundColor(field) {
	jQuery(field).removeClass('inputfield-error-bg');
}

// Set background color
function setErrorBackgroundColor(field) {
	jQuery(field).addClass('inputfield-error-bg');
}

// Check if field is empty or in exclude array
function isEmpty(field_value) {
		if ((field_value.length != 0) && (field_value != null)) {
			if (jQuery.inArray(field_value, exclude_entries) != -1) {
				return true;
			}
			
			return false;
		}	
	return true;
}

// Validate Email
function isValidEmail(email) {
    var emailEx = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
    if (!email.match(emailEx)) {
        return false;
    }
    else {
       return true;
    }
}

// Validate postcode
function validatePostcode() 
{
	var region_guid = jQuery('#region_guid').val();
		if (isEmpty(region_guid)) {		
			if (OK) {
				setErrorMessage('Please enter the first letter of your postcode');
				jQuery('#postcode_prefix').addClass('inputfield-error-bg');
			}
			return false;
		} else {
			jQuery('#postcode_prefix').removeClass('inputfield-error-bg');
		}
	return true;
}

/*************************************** OTHER ***************************************************/

// Clear postcode fields
jQuery("#postcode_prefix").click(function() {
	jQuery("#postcode_prefix").val('');
	jQuery('#region_guid').val('');
	jQuery('#director_guid').val('');
});




