﻿var telNumberErrorNo = 0;
telNumberErrors = new Array (0);
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers with 10 or 11 numbers, please - no other characters";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";
telNumberErrors[6] = "The telephone number has an invalid mix of numbers";

telNumberErrors[10] = "Valid Swedish telephone number";
telNumberErrors[12] = "Swedish telephone number without the country code, please";
telNumberErrors[13] = "Swedish telephone numbers with 8, 9, 10 or 11 numbers, please - no other characters";
telNumberErrors[14] = "The Swedish telephone number should start with a 0";
telNumberErrors[15] = "The Swedish telephone number is either invalid or inappropriate";

telNumberErrors[20] = "Valid Canadian telephone number";
telNumberErrors[22] = "Canadian telephone number without the country code, please";
telNumberErrors[25] = "Please introduce the telphone number using the format xxx-xxx-xxxx or xxx xxx xxxx";

function csIsEMail(oSrc, args) 
{
	args.IsValid = isEmail(args.Value);
}

function isEmail(pStr)
{
	var objText=pStr;
	var reg01 = /(@.*@)|(\.\.)|(@\.)|\,|\;|\\|\!|\%|\$|\&|\*|\(|\)|\=|\s|\'|\"|(\.@)|(^\.)/;   // not valid
	var reg02 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; // is valid
		
	return( (!reg01.test(objText) && reg02.test(objText)) );	
}

function isPostcode(pStr)
{
	var postcodeexp = /^[a-z]{1,2}[0-9]{1,2}\s*\D{0,2}[0-9]{1,1}[a-z]{1,2}$/i;
	var objText=pStr;
	
	return( objText.match(postcodeexp) );
}

function isYear(pStr)
{
	var yearexp = /^19[0-9][0-9]/;
	var objText=pStr;
	
	return( objText.match(yearexp) );
}

function isTelephoneNo(telephoneNumber)
{
  var telnum;
  telNumberErrorNo = 0;
  
  // Convert into a string and check that we were provided with a number
  telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;

  telnum = telephoneNumber;
  
  // Don't allow country codes to be included (assumes a leading "+")
  exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
  
  // Now check that the telephone number is appropriate.
  exp = /^(01|02|05|070|077|078|079)[0-9]+$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  var i, strChars;
  strChars = '';
  
  for(i = 0; i < telnum.length; i++) {
	if(strChars.indexOf(telnum.substr(i,1)) < 0) {
			strChars += telnum.substr(i,1);
		}
	}
  
  if(strChars.length <= 3) {
     telNumberErrorNo = 6;
     return false;
  }

  // Seems to be valid
 
  return true;
	
}

function isMobileTelephoneNo(pStr)
{
    var i;
	var strValidChars = "0123456789";	
	
    for (i = 0; i < pStr.length; i++)
    {
        var c = pStr.charAt(i);
		
		if(strValidChars.indexOf(c) < 0)  
		{	return false;
		}
    }

 	var idx = pStr.indexOf("07");
 	var len = pStr.length;

	return ( (len == 11) && (idx==0) );
}

function isName(pStr)
{
	var nameexp = /^([a-z,']+\s*\-*)+$/i;
	
	return( nameexp.test(pStr) );
}

function isSWE_TelephoneNo(telephoneNumber)
{
  var telnum;
  telNumberErrorNo = 10;
  
  // Convert into a string and check that we were provided with a number
  telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;

  telnum = telephoneNumber;
  
  // Don't allow country codes to be included (assumes a leading "+")
  exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 12;
     return false;
  }
  
  // Now check that all the characters are digits
  exp = /^[0-9]{8,11}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 13;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{7,10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 14;
     return false;
  }
  
  // Now check that the telephone number is appropriate.
  exp = /^(01|02|03|04|05|06|07|08|09)[0-9]+$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 15;
     return false;
  }
  
 
  // Seems to be valid
 
  return true;
	
}


function isCAN_TelephoneNo(telephoneNumber)
{
  var telnum;
  telNumberErrorNo = 20;

// Convert into a string and check that we were provided with a number
  telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;

  telnum = telephoneNumber;
  
  // Don't allow country codes to be included (assumes a leading "+")
  exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 22;
     return false;
  }
  
  // Now check that the telephone number is appropriate.
  exp = /^1{0,1}[0-9]{3}(\-| |)[0-9]{3}(\-| |)[0-9]{4}$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 25;
     return false;
  }
  
  var i, strChars;
  strChars = '';
  
  
  return true;
	
	
}


