
/**
 * Glow Library Tests
 */

// Is the specififed value an email address
glow.forms.tests.isEmail = function(values, opts, callback)
{
    var message = opts.message  || "Must be a valid email address.";

    for (var i = 0, len = values.length; i < len; i++)
    {
        if (!/^[A-Za-z0-9](([_\.\-]*[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/.test(values[i]))
		{
			if ((/^\s*$/.test(values[i]) === false) || (opts.required === true))
			{
                callback(glow.forms.FAIL, message);
                return;
			}
        }
    }
	
    callback(glow.forms.PASS, message);
}

// Is the specified value a UK telephone number
glow.forms.tests.isUkPhoneNumber = function(values, opts, callback)
{
    var message = opts.message  || "Must be a valid uk number.";
	
    for (var i = 0, len = values.length; i < len; i++)
    {	
        if ((valid = checkUKTelephone(values[i])) !== true)
		{
			if ((/^\s*$/.test(values[i]) === false) || (opts.required === true))
			{
                callback(glow.forms.FAIL, valid);
                return;
			}
        }
    }
	
    callback(glow.forms.PASS, message);
}

// Did the user enter the correct captcha code?
glow.forms.tests.captchaCheck = function(values, opts, callback, formValues)
{
    var queryValues = {};
	var message     = (opts.message || "Please enter/check the security code");

    for (var p in formValues)
	{
        if (typeof formValues[p] == "string")
	    {
            queryValues[p] = escape(formValues[p]);
        }
        else if (typeof formValues[p].push != "undefined")
	    {
            queryValues[p] = glow.lang.map(formValues[p], function(i) { return escape(i); }).join(",");
        }
    }
	
	var url = glow.lang.interpolate(opts.url, queryValues);
	
    var request = glow.net.get(url,
	{
      onLoad: function(response)
	  {
          if (response.text() == "ok") {
              callback(glow.forms.PASS, message);
          } else {
              callback(glow.forms.FAIL, message);
          }
      },
      onError: function(response)
	  {
          callback(glow.forms.PASS, message);
      }
    });
}

/**
 * Utility functions to aid in validation
 */

function checkUKTelephone(telephoneNumber)
{
	// Has the user entered a value?
	
	var telnum = telephoneNumber + " ";
	
	if (telnum.length == 1)
		return 'Number not provided';

	telnum.length = telnum.length - 1;

	// Don't allow country codes to be included (assumes a leading "+")
	
	var exp = /^(\+)[\s]*(.*)$/;
	
	if (exp.test(telnum) == true)
		return 'UK number without the country code please';

	// Remove spaces from the telephone number to help validation
	
	while (telnum.indexOf(" ")!= -1)
		telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)

	// Remove hyphens from the telephone number to help validation
	
	while (telnum.indexOf("-")!= -1)
		telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)

	// Check that all the characters are digits
	
	exp = /^[0-9]{10,11}$/;
	
	if (exp.test(telnum) != true)
		return 'Number should contain 10 or 11 digits';
		
	// Now check that the first digit is 0
	
	exp = /^0[0-9]{9,10}$/;
	
	if (exp.test(telnum) != true)
		return 'Number should start with a 0';

	// Disallow numbers allocated for dramas.

	var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);

	for (var i=0; i<tnexp.length; i++)
		if (tnexp[i].test(telnum))
		    return 'Number is either invalid or inappropriate';

	// Check that the telephone number is appropriate.
	
	exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	
	if (exp.test(telnum) != true)
		return 'Number is either invalid or inappropriate';

	return true;
}