//////////////////////////////////////////////////////////////////////
//This set of functions identify whether a string is a				//
//valid car registration mark.										//
//To use these functions just pass a string into the function		//
//CheckPlate().														//
//i.e.	CheckPlate(RegString)										//
//The function will return either True or False (True meaning the	//
//registration is valid)											//
//																	//
//Author - Ian Clayton (2001)										//
//																	//
//Permission from the author is required before use.				//
//////////////////////////////////////////////////////////////////////

function isNumeric(string, ignoreWhiteSpace) 
{
	if (string.search) 
	{
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

function AddSpace(RN, SpaceIndex)
{
	//document.write (RN.length);
	//document.write ("'" + RN + "/" + (SpaceIndex) + "'<br>1-" + RN.substr(0,SpaceIndex) + "<BR>2-" + RN.substr(SpaceIndex,RN.length));
	RN = RN.substr(0,SpaceIndex) + " " + RN.substr(SpaceIndex,RN.length)
	return RN
}

function RemoveSpace(RN)
{
	return (RN.replace(/\s/gi,""))
}

function ConvertToMask(RN)
{
	var PlateMask="";
	for (i = 0; i < (RN.length); i++)
	{
		if(isNumeric(RN.charAt(i)) == true)
		{
			PlateMask = PlateMask + "N";
		}
		else
		{
			PlateMask = PlateMask + "L";
		}
	}
	return (PlateMask)
}

function CheckPlate(RN)
{
	var Plate = new Array(2);
	var MaskedPlate;
	RN = RemoveSpace(RN);
	RN = RN.toUpperCase()	
	MaskedPlate = ConvertToMask(RN);
	switch (MaskedPlate.length)
	{
		case 2:
			var ValidMasks = new Array(1);	//	(Mask,Char where the space should be inserted later)
			ValidMasks[0] = new Array(2)
			ValidMasks[1] = new Array(2)
			
			ValidMasks[0][0] = "LN"
			ValidMasks[0][1] = 1
			ValidMasks[1][0] = "NL"
			ValidMasks[1][1] = 1
			for (i = 0; i <= 1; i++)
			{
				//document.write (ValidMasks[i][0] + " - " + MaskedPlate)
				if(ValidMasks[i][0] == MaskedPlate)
				{
					Plate[0] = "valid";
					Plate[1] = ValidMasks[i][1];
				}
			}
		break
		case 3:
			var ValidMasks = new Array(3);	//	(Mask,Char where the space should be inserted later)
			ValidMasks[0] = new Array(2)
			ValidMasks[1] = new Array(2)
			ValidMasks[2] = new Array(2)
			ValidMasks[3] = new Array(2)
			
			ValidMasks[0][0] = "LLN"
			ValidMasks[0][1] = 2
			ValidMasks[1][0] = "NNL"
			ValidMasks[1][1] = 2
			ValidMasks[2][0] = "LNN"
			ValidMasks[2][1] = 1
			ValidMasks[3][0] = "NLL"
			ValidMasks[3][1] = 1
			for (i = 0; i <= 3; i++)
			{
				if(ValidMasks[i][0] == MaskedPlate)
				{
					Plate[0] = "valid"; 
					Plate[1] = ValidMasks[i][1];
				}
			}
		break
		case 4:
			var ValidMasks = new Array(5);	//	(Mask,Char where the space should be inserted later)
			ValidMasks[0] = new Array(2)
			ValidMasks[1] = new Array(2)
			ValidMasks[2] = new Array(2)
			ValidMasks[3] = new Array(2)
			ValidMasks[4] = new Array(2)
			ValidMasks[5] = new Array(2)
			
			ValidMasks[0][0] = "LNNN"
			ValidMasks[0][1] = 1
			ValidMasks[1][0] = "LLNN"
			ValidMasks[1][1] = 2
			ValidMasks[2][0] = "LLLN"
			ValidMasks[2][1] = 3
			ValidMasks[3][0] = "NLLL"
			ValidMasks[3][1] = 1
			ValidMasks[4][0] = "NNLL"
			ValidMasks[4][1] = 2
			ValidMasks[5][0] = "NNNL"
			ValidMasks[5][1] = 3
			
			for (i = 0; i <= 5; i++)
			{
				if(ValidMasks[i][0] == MaskedPlate)
				{
					Plate[0] = "valid"; 
					Plate[1] = ValidMasks[i][1];
				}
			}
		break
		case 5:
			var ValidMasks = new Array(7);	//	(Mask,Char where the space should be inserted later)
			ValidMasks[0] = new Array(2)
			ValidMasks[1] = new Array(2)
			ValidMasks[2] = new Array(2)
			ValidMasks[3] = new Array(2)
			ValidMasks[4] = new Array(2)
			ValidMasks[5] = new Array(2)
			ValidMasks[6] = new Array(2)
			ValidMasks[7] = new Array(2)
			
			ValidMasks[0][0] = "LNNNN"
			ValidMasks[0][1] = 1
			ValidMasks[1][0] = "LLNNN"
			ValidMasks[1][1] = 2
			ValidMasks[2][0] = "LLLNN"
			ValidMasks[2][1] = 3
			ValidMasks[3][0] = "LNLLL"	//PREFIX PLATES
			ValidMasks[3][1] = 2
			ValidMasks[4][0] = "NNNNL"
			ValidMasks[4][1] = 4
			ValidMasks[5][0] = "NNNLL"
			ValidMasks[5][1] = 3
			ValidMasks[6][0] = "NNLLL"
			ValidMasks[6][1] = 2
			ValidMasks[7][0] = "LLLNL"	//SUFFIX PLATES
			ValidMasks[7][1] = 3
			for (i = 0; i <= 7; i++)
			{
				if(ValidMasks[i][0] == MaskedPlate)
				{
					Plate[0] = "valid"; 
					Plate[1] = ValidMasks[i][1];
				}
			}
		break
		case 6:
			var ValidMasks = new Array(5);	//	(Mask,Char where the space should be inserted later)
			ValidMasks[0] = new Array(2)
			ValidMasks[1] = new Array(2)
			ValidMasks[2] = new Array(2)
			ValidMasks[3] = new Array(2)
			ValidMasks[4] = new Array(2)
			ValidMasks[5] = new Array(2)
			
			ValidMasks[0][0] = "LLLNNN"
			ValidMasks[0][1] = 3
			ValidMasks[1][0] = "NNNLLL"
			ValidMasks[1][1] = 3
			ValidMasks[2][0] = "LLNNNN"
			ValidMasks[2][1] = 2
			ValidMasks[3][0] = "NNNNLL"
			ValidMasks[3][1] = 4						
			ValidMasks[4][0] = "LNNLLL"	//PREFIX PLATES
			ValidMasks[4][1] = 3
			ValidMasks[5][0] = "LLLNNL" //SUFFIX PLATES
			ValidMasks[5][1] = 3
			for (i = 0; i <= 5; i++)
			{
				if(ValidMasks[i][0] == MaskedPlate)
				{
					Plate[0] = "valid"; 
					Plate[1] = ValidMasks[i][1];
				}
			}
		break		
		case 7:
			var ValidMasks = new Array(4);	//	(Mask,Char where the space should be inserted later)
			ValidMasks[0] = new Array(2)
			ValidMasks[1] = new Array(2)
			ValidMasks[2] = new Array(2)
			ValidMasks[3] = new Array(2)
			ValidMasks[4] = new Array(2)
			
			ValidMasks[0][0] = "LLLNNNN"
			ValidMasks[0][1] = 3
			ValidMasks[1][0] = "NNNNLLL"
			ValidMasks[1][1] = 4
			ValidMasks[2][0] = "LLNNLLL" //NEW STYLE
			ValidMasks[2][1] = 4
			ValidMasks[3][0] = "LNNNLLL"	//PREFIX PLATES
			ValidMasks[3][1] = 4
			ValidMasks[4][0] = "LLLNNNL"	//SUFFIX PLATES
			ValidMasks[4][1] = 3
			for (i = 0; i <= 4; i++)
			{
				if(ValidMasks[i][0] == MaskedPlate)
				{
					Plate[0] = "valid"; 
					Plate[1] = ValidMasks[i][1];
				}
			}
		break		
		default:
	}
	if(Plate[0] == "valid")
	{
		RN = AddSpace(RN,Plate[1])
		return (RN)
	}
	else
	{
		return ("invalid")
	}
}

///////////////////////////////////////////////////////////////////
//	End of Plate Validation Functions							 //
///////////////////////////////////////////////////////////////////

/*************************************
* Validates an email address.
* If it is invalid, it puts the focus 
* on the email field and selects all 
* the text in it.
* Parameter: email = <input> object
*         with the email address in it
* Returns: true / false
**************************************/
function validateEmail(email) {
	
// 1) look for one or more "word" characters to start, optionally
// 2) followed by a period or hyphen,
// 3) followed by one or more word characters, repeated zero
// or more times,
// 4) followed by a '@',
// 5) followed by at least one word character, followed by at
// 6) least one period, 
// 7) followed by two or three word characters,
// repeated one or more times,
// anchored to end of string

     
	var      re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w+)+$/ 
//                   1    2    3   4 5   6             7
	if ( !re.test(email.value) ) {
		email.focus();
		email.select();
		return false;
	} else 
		return true;
	
}

// However, getCookie cannot distinguish between these and will return
// the first cookie that matches a given name. It is therefore
// recommended that you *not* use the same name for cookies with
// different paths. (Bear in mind that there is *always* a path
// associated with a cookie; if you don't explicitly specify one,
// the path of the setting document is used.)
//
// Function to return the value of the cookie specified by "name".
// name - String object, the cookie name.
// returns - String object, the cookie value, or null if the cookie does not exist.

function getCookie(name) {
     var prefix = name + "=";
     var begin = document.cookie.indexOf(prefix);

     if (begin == -1) return null;

     var end = document.cookie.indexOf(";", begin);

     if (end == -1) end = document.cookie.length;

     return unescape(document.cookie.substring(begin + prefix.length, end));
}

// Function to create or update a cookie.
// name - String object containing the cookie name.
// value - String object containing the cookie value. May contain
// any valid string characters.
// [expires] - Date object containing the expiration data of the cookie. If
// omitted or null, expires the cookie at the end of the current session.
// [path] - String object indicating the path for which the cookie is valid.
// If omitted or null, uses the path of the calling document.
// [domain] - String object indicating the domain for which the cookie is
// valid. If omitted or null, uses the domain of the calling document.
// [secure] - Boolean (true/false) value indicating whether cookie transmission
// requires a secure channel (HTTPS).
//
// The first two parameters are required. The others, if supplied, must
// be passed in the order listed above. To omit an unused optional field,
// use null as a place holder. For example, to call setCookie using name,
// value and path, you would code:
//
// setCookie("myCookieName", "myCookieValue", null, "/");
//
// Note that trailing omitted parameters do not require a placeholder.
//
// To set a secure cookie for path "/myPath", that expires after the
// current session, you might code:
//
// setCookie(myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
// NOTE that it is possible to set multiple cookies with the same
// name but different (nested) paths. For example:
//
// setCookie("color", "red", null, "/outer");
// setCookie("color", "blue", null, "/outer/inner");

function setCookie(name, value, expires, path, domain, secure) {
     document.cookie = name + "=" + escape(value) +
                                    ((expires) ? "; expires=" + expires.toGMTString() : "") +
                                    ((path) ? "; path=" + path : "") +
                                    ((domain) ? "; domain=" + domain : "") +
                                    ((secure) ? "; secure" : "");
}

// deleteCookie now sets the expiration date to the earliest
// usable date (one second into 1970), and sets the cookie's value
// to null for good measure.
//
// Also, this version adds optional path and domain parameters to
// the deleteCookie function. If you specify a path and/or domain
// when creating (setting) a cookie, you must specify the same
// path/domain when deleting it, or deletion will not occur.
// Function to delete a cookie. (Sets expiration date to start of epoch)
// name - String object containing the cookie name
// path - String object containing the path of the cookie to delete. This MUST
// be the same as the path used to create the cookie, or null/omitted if
// no path was specified when creating the cookie.
// domain - String object containing the domain of the cookie to delete. This MUST
// be the same as the domain used to create the cookie, or null/omitted if
// no domain was specified when creating the cookie.//

function deleteCookie(name, path, domain) {
     if (getCookie(name)) {
          document.cookie = name + "=" +
                                         ((path) ? "; path=" + path : "") +
                                         ((domain) ? "; domain=" + domain : "") +
                                         "; expires=Thu, 01-Jan-70 00:00:01 GMT";
     }
}

///////////////////////////////////
//	Navigation Function			 //
///////////////////////////////////
function Navigate(sURL)										
{
	top.location.href = sURL;
}

//////////////////////////////////
// End of Navigation Function	//
//////////////////////////////////

///////////////////////////////////
//	Decrypt function used for	 //
//  Offer code					 //
///////////////////////////////////
function Decrypt(text) {
	var c = "";
	arr = text.split('-');
	for(var n=0; n<arr.length; n++) {
		var i = "%" + (parseInt("0" + parseInt(arr[n]))-1).toString(16);
		i = unescape(i);
		c += i;
	}
	return c;
}

function FormatNumber(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

