/*
'NAME:					jscript/ValidateUserDetails.js
'VERSION:				1.0
'VERSIONED:				2005-06-23
'APPLICATION:			First Report admin
'LANGUAGE:				JavaScript
'FOR:					First Report Ltd
'CREATED:
'COPYRIGHT				yMonda Ltd 2005
'AUTHOR:				W Charlton yMonda Ltd
'RETURN CODE:			HTML
'DEPENDS ON:
'DEPENDENTS:			/FRadmin/UserInfo.asp Edit and ADD Modes, /OpenAccount.asp, /Account/EditDetails.asp
' ***************************************************************
' DESCRIPTION and NOTES
'Validation functios for all User details forms. Any functions that are not common will be held in page speific JS files
' ***************************************************************
'REVISION HISTORY

2005-06-23 VS removed Post Code and Email Validations and replaced Regex validations for the same Fields
2005-06-30 WHC allowed apostrophe in FamilyName and changed FamilyName and GivenName to not allow numbers
2005-07-06 VS UK post code fix
2005-07-07 WHC allowed apostrophe in GivenName, Address and Town
2005-07-15 JO fixed regex for email
2005-07-20 VS replased allow length for reGivenName from 3 to 2
2005-11-03 VS added regex for DOB
2005-11-17 VS added OrgName Regex
2006-02-08 JO added trim function added told it to trim all check values
2007-09-19 JO replased allow length for reFamilyName from 3 to 2
'*/


// regular expressions used by checking functions
var reNonBlank=/[\S]/;
var reHexColor=/^#[0-9a-fA-F]{6}$/;
var reInt=/^\d+$/;


var reDOB=/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/;



var reSignedInt=/^(\+|-)?\d+$/;
var reFloat=/^\d+(\.\d+)?$/;
var reSignedFloat=/^(\+|-)?\d+(\.\d+)?$/;
var reChar=/^[\w\-]+$/;
var reEMail=/^\w[\w\-\.]+\@\w[\w\-]+(\.\w[\w\-]+)+$/;
var reIP=/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var rePostalCA=/^(\w\d){3}$/;
var reURL=/^http(s)?\:\/\/\w[\w\-]+(\.\w[\w\-]+)+([\/\%\?\&\+\#\.\w\-]+)*$/;


//var reTel=/^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/;
var reTel=/^((\(?0\d{4}\)?\s?\d{2,3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/;
var reMobile=/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/;
var reAddress=/^[\w\-\d\s\,\']{5,}$/;
var reTown=/^[A-Za-z\!\s\']{3,}/;
var reOrgName=/^[A-Za-z0-9\!\s\']{3,}/;
var reFamilyName= /^[A-Za-z\-\s\,\']{2,}$/;
var reGivenName = /^[A-Za-z\-\s\,\']{2,}$/;
//var reUKPostCode = /^(\d{5}(-\d{4})?|[a-z][a-z][a-z]?\d\d\d? ?\d[a-z][a-z])$/i;
var reUKPostCode =/^[A-Za-z]{1,2}\d{1,2}[A-Za-z]? \d[A-Za-z]{2}$/;
//var reEmail = /^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
var reEmail = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[\w-\.]*[a-zA-Z0-9]\.[a-zA-Z]{2,7}$/;


function _alertIt(msg, mode) {
  if (mode) {
    totalAlert+=msg+"\n";
  }
  else {
    totalAlert="";
    alert(msg);
  }
}

// wrapper functions
function _checkIt(re, field, msg, mode) {
  field.value = trim(field.value)
  if (!re.test(field.value)) {
    _alertIt(msg, mode);

    if (field.select) {
      field.select();
    }
    if (field.focus) {
      field.focus();
    }

    return (mode && mode==1)?true:false;
  }

  return true;
}

function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}

function goodChar(field, msg, mode) {
  return _checkIt(reChar, field, msg, mode);
}

function goodDOB(field, msg, mode) {
  return _checkIt(reDOB, field, msg, mode);
}

function goodInt(field, msg, mode) {
  return _checkIt(reInt, field, msg, mode);
}


function reCharNM(n,m) {
  return new RegExp("\^[\\w\\s\\-]{"+n+","+m+"}\$");
}

function goodCharLen(n, m, field, msg, mode) {
  return _checkIt(reCharNM(n,m), field, msg, mode);
}


function goodAddress(field, msg, mode) {
  return _checkIt(reAddress, field, msg, mode);
}

function goodTownCounty(field, msg, mode) {
  return _checkIt(reTown, field, msg, mode);
}

function goodTel(field, msg, mode) {
  return _checkIt(reTel, field, msg, mode);
}

function goodMobile(field, msg, mode) {
  return _checkIt(reMobile, field, msg, mode);
}


function goodFamilyName(field, msg, mode) {
  return _checkIt(reFamilyName, field, msg, mode);
}

function goodOrgName(field, msg, mode) {
  return _checkIt(reOrgName, field, msg, mode);
}


function goodGivenName(field, msg, mode) {
    return _checkIt(reGivenName, field, msg, mode);
}

function goodUKPostCode(field, msg, mode) {
    return _checkIt(reUKPostCode, field, msg, mode);
}


function goodEmailAddress(field, msg, mode) {
    return _checkIt(reEmail, field, msg, mode);
}



/*
function isEmailAddress(email_address) {
	var period_pattern = "([A-Za-z0-9][A-Za-z0-9_\-]{0,255})";
	var before_at_sign_pattern = "(" + period_pattern + "([.]" + period_pattern + "){0,255})";
	var after_at_sign_pattern = "(" + period_pattern + "([.]" + period_pattern + "){1,255})";
	var valid_email_address = new RegExp("^" + before_at_sign_pattern + "@" + after_at_sign_pattern + "$");
	return valid_email_address.test(email_address);
}
*/




//////////////////////// CC Validations Functions //////////////////////////////

function DigitsMoveNext(currentBox, nextBox)
{
	var iBoxLength = 3
	if(currentBox.name == 'txtDigits4')
	{
		iBoxLength = 7
	}
	else
	{
		iBoxLength = 3
	}
	if(currentBox.value.length > iBoxLength)
	{
		nextBox.focus();
	}

}

function ClearField(currentBox)
{
	if(currentBox.value.length > 3)
	{
		currentBox.value = '';
		currentBox.focus();
	}
}


function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

if (st.length < 16)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()


function ValidateCC(objForm)
{
	with (objForm)
	{
		if(isCreditCard(objForm.txtDigits1.value + objForm.txtDigits2.value + objForm.txtDigits3.value + objForm.txtDigits4.value) == false)
		{
		alert('Please check the credit card number you have provided.');
		return false;
		}

		if(objForm.expMM.value == '--')
		{
		alert('Please enter a valid expiry date.');
		return false;
		}

		if(objForm.expYY.value == '--')
		{
		alert('Please enter a valid expiry date.');
		return false;
		}
	}

	counter++;
	if(counter > 1)
	{
		if(counter > 2) { return false; }
		alert('Page is processing, please wait...\n\n' +
		'One click is sufficient.\n\n' +
		'Thank you for your patience.');
		return false;
	}

	 return (true);
}

//////////////////////// End CC Validations Functions //////////////////////////////////////
