//////////////////////////////////////////////////////////////////////
//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 aPos1 = new Array();
        var aPos2 = new Array();
        var aPos3 = new Array();
        var aPos4 = new Array();
        RN = RN.replace(/ /g, '').toUpperCase();
        
        aPos1.push(/^[A-Z][1-9]$/i);        
        aPos1.push(/^[1-9][A-Z]$/i);
        aPos1.push(/^[1-9][A-Z][A-Z]$/i);
        aPos1.push(/^[A-Z][1-9][0-9]$/i);
        aPos1.push(/^[A-Z][1-9][0-9][0-9]$/i); 
        aPos1.push(/^[1-9][A-Z][A-Z][A-Z]$/i);
        aPos1.push(/^[A-Z][1-9][0-9][0-9][0-9]$/i);
	
        aPos2.push(/^[A-Z][A-Z][1-9]$/i); 
        aPos2.push(/^[1-9][0-9][A-Z]$/i); 
        aPos2.push(/^[A-Z][A-Z][1-9][0-9]$/i); 
        aPos2.push(/^[1-9][0-9][A-Z][A-Z]$/i); 
        aPos2.push(/^[A-Z][A-Z][1-9][0-9][0-9]$/i); 
        aPos2.push(/^[ABCDEFGHJKLMNOPRSTUVWXY][1-9][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY]$/i);  //PREFIX
        aPos2.push(/^[A-Z][A-Z][1-9][0-9][0-9][0-9]$/i); 
        aPos2.push(/^[1-9][0-9][A-Z][A-Z][A-Z]$/i);
	
        aPos3.push(/^[A-Z][A-Z][A-Z][1-9]$/i); 
        aPos3.push(/^[1-9][0-9][0-9][A-Z]$/i); 
        aPos3.push(/^[A-Z][A-Z][A-Z][1-9][0-9]$/i); 
        aPos3.push(/^[1-9][0-9][0-9][A-Z][A-Z]$/i); 
        aPos3.push(/^[A-Z][A-Z][A-Z][1-9][A-Z]$/i); 
        aPos3.push(/^[A-Z][A-Z][A-Z][1-9][0-9][0-9]$/i); 
        aPos3.push(/^[1-9][0-9][0-9][A-Z][A-Z][A-Z]$/i);
        aPos3.push(/^[ABCDEFGHJKLMNOPRSTUVWXY][1-9][0-9][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY]$/i);  //PREFIX
        aPos3.push(/^[ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][1-9][0-9][ABCDEFGHJKLMNOPRSTUVWXY]$/i); 
        aPos3.push(/^[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9]$/i); 
        aPos3.push(/^[ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][1-9][0-9][0-9][ABCDEFGHJKLMNOPRSTUVWXY]$/i);
    
        aPos4.push(/^[1-9][0-9][0-9][0-9][A-Z]$/i); 
        aPos4.push(/^[1-9][0-9][0-9][0-9][A-Z][A-Z]$/i); 
        aPos4.push(/^[1-9][0-9][0-9][0-9][A-Z][A-Z][A-Z]$/i); 
        aPos4.push(/^[ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][0-9][0-9][A-Z][A-Z][A-Z]$/i);  //NEWSTYLE plate
        aPos4.push(/^[ABCDEFGHJKLMNOPRSTUVWXY][1-9][0-9][0-9][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY][ABCDEFGHJKLMNOPRSTUVWXY]$/i); //PREFIX
	
        for(var n=0; n< aPos1.length; n++) {
            if(RN.match(aPos1[n]) != null)
                return AddSpace(RN,1);
        }
	
        for(var n=0; n< aPos2.length; n++) {
            if(RN.match(aPos2[n]) != null)
                return AddSpace(RN,2);
        }
        for(var n=0; n< aPos3.length; n++) {
            if(RN.match(aPos3[n]) != null)
                return AddSpace(RN,3);
        }
        for(var n=0; n< aPos4.length; n++) {
            if(RN.match(aPos4[n]) != null)
                return AddSpace(RN,4);
        }
        return 'invalid';
}

///////////////////////////////////////////////////////////////////
//	End of Plate Validation Functions							 //
///////////////////////////////////////////////////////////////////

function validateEmail(email) {
	if(email.value.substr(0,4) == 'www.')
		return false;
	var ex = email.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/);
											
	if(ex == null) {
		return false;
	} else
		return true;
}
/*************************************
* 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 validateEmailOld(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	//
//////////////////////////////////
