/*************************************************************************************
validation.js - validation library
**************************************************************************************
Author:        Andy Hall, Andy Henshall, Wade Montague
Date:          04/09/2008
History:       n/a
*************************************************************************************/

/**
* function to validate a name
* @access public
*/
function isName(sName)
{
    regex = new RegExp("^([a-zA-Z,']+\\s?\-?)+$", "i");
	regex_vowels = new RegExp("[aeiouyAEIOUY]{1,}", "i");
	regex_triple = /([a-zA-Z])\1{2,}/; 

	val1 = regex.test(sName);
	val2 = regex_vowels.test(sName);
	val3 = !regex_triple.test(sName);
	//alert (val1 + ' ' + val2 + ' ' val3);
	if (val1 && val2 && val3) { retval = true; } else { retval = false; }
	return retval;
}

function processForm(){

      var fname = document.getElementById('firstname');

      var lname = document.getElementById('lastname');

      checkDoubleNames(fname, lname);

      return false;

}

 

function checkDoubleNames(fname, sname) 
{
      var fnameval = fname.value.toUpperCase();
      var snameval = sname.value.toUpperCase();
      var firstnameArray=fnameval.split(" ");
      var surnameArray=snameval.split(" ");
      if(fnameval==snameval){
            return;
      }
      if (firstnameArray.length > 1) 
      {
            for (var i=0, len=firstnameArray.length; i<len; i++)
            {
                  snpos = surnameArray.inArray(firstnameArray[i]); 
                  if (snpos!==false) {
                        if(i > snpos){
                              firstnameArray[i] = "";
                        }
                  }
            }
      }
      if (surnameArray.length > 1) 
      {
            for (var i=0, len=surnameArray.length; i<len; i++)
            {
                  fnpos = firstnameArray.inArray(surnameArray[i]); 
                  if (fnpos!==false) {
                        if(i <= fnpos){
                              surnameArray[i] = "";
                        }
                  }
            }
      }
      fname.value = firstnameArray.join(" ").trim();
      sname.value = surnameArray.join(" ").trim();
}

Array.prototype.inArray = function ( search_phrase )
{
  for( var i = 0; i < this.length; i++ )
  {
    if( search_phrase == this[i] )
    {
      return i;
    }
  }
  return false;
}

String.prototype.trim = function () 
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


/**
* function to validate a landline
* @access public
*/
function isLandline(number)
{
    // remove spaces
    regex = new RegExp(" +", "g");
    number = number.replace(regex, "");
    // validate number
    regex = new RegExp("^(01\\d{8,9}|020(7|8)\\d{7}|02(3|4|8|9)\\d{8}|08(4)\\d{8})$");
 //   regex = new RegExp("^(01\\d{9}|020(7|8)\\d{7})$");
	return regex.test(number);
}

/**
* function to validate a mobile
* @access public
*/
function isMobile(number)
{
    // remove spaces
    regex = new RegExp(" +", "g");
    number = number.replace(regex, "");
    // validate number
    regex = new RegExp("^07[0-9]{9}$");
	return regex.test(number);
}

/**
* function to validate a landline
* or a mobile number
* @access public
*/
function isPhone(number)
{
    return (isLandline(number) || isMobile(number));
}

/**
* function to validate a postcode
* @access public
* allows for the following outcodes:
* AN, ANN, AAN, AANN, ANA, AANA
* all incodes conform to NAA
*/
function isPostcode(postcode)
{
    // remove spaces
    regex = new RegExp(" +", "g");
    postcode = postcode.replace(regex, "");
    // validate number
    regex = new RegExp("^[a-zA-Z]{1,2}\\d{1,2}[a-zA-Z]?\\d{1}[a-zA-Z]{2}$");
	return regex.test(postcode);
}


/**
* function to correct incorrectly 
* typed common domain names for 
* email addresses
*/
function checkEmail(sEmail)
{
	try {
	var emailObj = document.getElementById('email');
	myUserDomAndExt = sEmail.split('@');
	myDomAndExt = myUserDomAndExt[1].split('.');
	myDom = myDomAndExt[0];
	
	var newDomain = myDom;
	
	switch (myDom)
		{
		case 'yaoho' :
		case 'yhaoo' :
		case 'yaoo' : 
		case 'yahho' :
		case 'yahhoo' :
		case 'yaho' : // Add new case statement for each additional spelling of yahoo required
			newDomain = 'yahoo'
			break;
		case 'htomail' :
		case 'ohtmail' : 
		case 'hotmil' : 
		case 'hotmmail' : 
		case 'hotmaial' : 
		case 'hotmil' : 
		case 'hotmaul' : 
		case 'hotrmail' : // Add new case statement for each additional spelling of hotmail required
			newDomain = 'hotmail'
			break;		
		case 'gmali' :
		case 'gamil' :
		case 'gmial' : // Add new case statement for each additional spelling of gmail required
			newDomain = 'gmail'
			break;		
		}
		
		sEmail = sEmail.replace(myDom, newDomain)
		emailObj.value = sEmail;
	return isEmail(sEmail);
	}
	catch (err)
	{
		return isEmail(sEmail);	
	}

}

function isEmail(sEmail)
{
	var regex = new RegExp(/^\w+([\.-]*\w+)*@\w+([\.-]?\w+)*\.\w{2,}$/);
	return regex.test(sEmail);
}

function isFirstname(sValue) 
{ 
	if(!sValue) { return false;	}
	if(sValue.toUpperCase() == 'FIRSTNAME') { return false; }
	if(!isName(sValue)) { return false; }
	return true; 
}
function isSurname(sValue) 
{ 
	if(!sValue) { return false;	}
	if(sValue.toUpperCase() == 'SURNAME') { return false; }
	if(sValue.length < 2) { return false; }
	if(!isName(sValue)) { return false; }
	return true; 
}
function checkNotDefaultHouseNumber(sValue) 
{ 
	return !(sValue == 'House Number'); 
}

