
// PRIVATE FUNCTIONS & submitMouseOver

// show error - NOTE: radio buttons cannot select
function showError(field, msg)
{
	alert(msg);
	field.focus();
	//field.select();
}

// returns the value of the radio field
function getRadioValue(field)
{
	for(var i=0; i<field.length; i++)
	{
		if(field[i].checked == true)
		{
			var result = field[i].value;
		}
	}
	return result;
}

// e.g. remove £ or $ from money
function removeNonNumbers(val)
{
	var a,result = "";
	a = val.split("");
	for (var j in a)
	{
		if (a[j].match(/\d/))
		{
			result += a[j];
		}
	}
	return result;
}

// number to 2 decimal places
function decimalPlaces(val, places)
{
	var result = Math.floor((val*Math.pow(10,places)))/Math.pow(10,places);
	return result;
}

// remove ALL whitespace
function removeWhitespace(val)
{
	re = / /gi;
	result = val.replace(re,"");
	return result;
}

// check if string is empty 
function isEmpty(field, type)
{
	if(type == "field")
	{
		field = field.value;
	}
	if(field == "" || removeWhitespace(field) == "" || field == null || (field.length == 0))
	{
		return true;
	}
	return false;
}

// this is part of both sections - should be called in submitMouseOver but does return errors
function checkMoney(field, msg, optional)
{
	if(optional == true && isEmpty(field, "field") == true)
	{
		return true;
	}
	
	var result;
	var parts = field.value.split(".");
	
	// if there is more than one decimal place
	if(parts.length > 2)
	{
		showError(field, msg);
		return false;
	}
	
	// remove non - number characters
	for(var i=0; i<parts.length; i++)
	{
		parts[i] = removeNonNumbers(parts[i]);
	}

	// if there is no decimal place
	// alert(parts.length);
	if(parts.length == 1 && parts[0])
	{
		result = parts[0];
	}
	else if(parts.length == 2)
	{
		// if there are more than 2 decimal points
		if(parts[1].length > 2)
		{
			showError(field, msg);
			return false;
		}
		result = parts[0]+"."+parts[1];
	}
	else
	{
		showError(field, msg);
		return false;
	}
	field.value=result;
	return true;
}

// PUBLIC FUNCTIONS

// match 2 values
function matchValues(fielda, fieldb, msg)
{
	if(fielda.value != fieldb.value){
		showError(fielda, msg);
		return false;
	}
	return true;
}

// if 2 values depend on each other, check they're both there
function checkDependentFields(fielda, fieldb, msg)
{
	if(isEmpty(fielda, "field") != isEmpty(fieldb, "field"))
	{
		showError(fielda, msg);
		return false;
	}
	return true;
}

function validateNb(field,dec,msg) {
    if (field.value == "") {
	//alert('true');
      return true;
    }
 
    if (isNaN(field.value)) {
		//alert('false');
		showError(field, msg);
       return false;
    }
    else {
      if (field.value.indexOf('.') == -1) {
	  	//alert('true');
          return true;
      }
      else {
        dectext = field.value.substring(field.value.indexOf('.')+1, field.value.length);
        if (dectext.length > dec)  {
			//alert('false');
			showError(field, msg);
           return false;
        }
        else {
			//alert('true');
           return true;
        }
      }
    }
 }

// call with validateNumber(this.phone, 'Please enter a phone number, numbers only', 5, 10) 
function validateNumber(field, msg, min, max) 
{
if(field.value != "")
{
	if (!min) { 
		min = 0;
	}
	if (!max) { 
		max = 255;
	}
	if ( isNaN(field.value) || (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) {
		showError(field, msg);
		return false;
	}
	return true;
}
}

// call with validatePhoneNumber(this.phoneNo)
function validatePhoneNumber(field, msg) 
{  
	var phoneRE = /^\d\d\d\d\d\d\d\d\d\d\d$/;  
	if (field.value.match(phoneRE)) {    
		return true;  
	} else {    
		showError(field, msg);    
		return false;  
	}
}

// call with validateString(this.firstName, 'Please enter your first name', 3, 15) 
function validateString(field, msg, min, max, optional) 
{
	if (!min) { 
		min = 1; 
	}
	if (!max){ 
		max = 65535;
	}
	if ((optional==false && !field.value) || (optional==false && field.value.length < min) || field.value.max > max) {
		showError(field, msg);
		return false;
	}
	return true;
}

// call with validateEmail(this.email, 'Please enter your email address')
// or (for optional field) validateEmail(this.email, 'Please enter a correct email address or leave the field blank', true)
function validateEmail(field, msg, optional) 
{
	if (isEmpty(field, "field") && optional==true) 
	{
		return true;
	}
	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	if (!re_mail.test(field.value))
	{
		showError(field, msg);
		return false;
	}
	return true;
}
