function encrypt_password_register()
{
	var firstname = document.forms.register.firstname.value;
	var lastname = document.forms.register.lastname.value;
	var dob_day = document.forms.register.dob_day.value;
	var dob_month = document.forms.register.dob_month.value;
	var dob_year = document.forms.register.dob_year.value;
	var postcode = document.forms.register.postcode.value;
	var country = document.forms.register.country.value;
	var mobile = document.forms.register.mobile.value;
	var email = document.forms.register.email.value;
	var username = document.forms.register.username.value;
	var password1 = document.forms.register.password1.value;
	var password2 = document.forms.register.password2.value;
	var newsletter = document.forms.register.newsletter.checked;

	var error = "";
	if(password1.length < 8) error = "Password must be at least 8 characters long";
	if(password1.length > 32) error = "Maximum password length is 32 characters";
    if(password1 != password2) error = "Passwords don't match";
	if(username.length > 16) error = "Maximum username length is 16 characters";
	if(postcode.length > 16) error = "Maximum postcode length is 16 characters";
	if(mobile.length > 32) error = "Maximum mobile number length is 32 digits";
	if(lastname.length > 32) error = "Maximum first name length is 32 characters";
	if(firstname.length > 32) error = "Maximum last name length is 32 characters";

	if(username == "") error = "Please enter a user name";
	if(EmailCheck(email) == false) error = "Please enter a valid Email address";
	if(country == "") error = "Please select your country";
	if(postcode == "") error = "Please enter your Postcode";
	if(checkdate(dob_month+'/'+dob_day+'/'+dob_year) == false) error = "Please enter a valid date of birth";
	if(lastname == "") error = "Please enter your last name";
	if(firstname == "") error = "Please enter your first name";

    if(error != "")
    {
        alert(error);
        return;
    }

	var key = '9*n"¬pn@45gf(m^j';
	password1 = encrypt(password1,key);
	password2 = encrypt(password2,key);
	document.forms.register.register.value=true;
	document.forms.register.password1.value = password1;
	document.forms.register.password2.value = password2;
	document.forms.register.newsletter.value = newsletter;
    document.forms.register.submit();
}

var dtCh= "/";

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function checkdate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++)
    {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
		return false
	if (strMonth.length<1 || month<1 || month>12)
		return false
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		return false
	if (year==0)
		return false
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
		return false
    return true
}

function EmailCheck(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	   return false

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	    return false

	 if (str.indexOf(at,(lat+1))!=-1)
	    return false

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	    return false

	 if (str.indexOf(dot,(lat+2))==-1)
	    return false

	 if (str.indexOf(" ")!=-1)
	    return false

	 return true
}

function encrypt_password_login()
{
	var password = document.forms.login.password.value;
	var key = '9*n"¬pn@45gf(m^j';
	password = encrypt(password,key);
	document.forms.login.validate.value = true;
	document.forms.login.password.value = password;
    document.forms.login.submit();
}

// encrypt: Use 128 bits (16 chars) of string 'key' to encrypt string 'val'
//          (note key & val must be strings not string objects)
//           - return encrypted text as string

function encrypt(val, key)
{
    // 'escape' val so chars outside ISO-8859-1 work in single-byte packing, but
    // keep spaces as spaces (not '%20') so encrypted text doesn't grow too long!
    var v = strToLongs(escape(val).replace(/%20/g,' '));
    var k = strToLongs(key.slice(0,16)); var n = v.length;

    if (n == 0) return("");  // nothing to encrypt
    if (n == 1) v[n++] = 0;  // algorithm doesn't work for n<2 so fudge by adding nulls

    // TEA routine as per Wheeler & Needham, Oct 1998

    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = 0;

    while (q-- > 0) {  // 6 + 52/n operations gives between 6 & 32 mixes on each word
        sum += delta;
        e = sum>>>2 & 3;
        for (var p = 0; p < n-1; p++) {
            y = v[p+1];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
            z = v[p] += mx;
        }
        y = v[0];
        mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
        z = v[n-1] += mx;
    }
    // note: unsigned right-shift '>>>' is used in place of original '>>',
    // due to lack of 'unsigned' type declaration in JavaScript

    return(longsToStr(v));
}

// supporting functions

function strToLongs(s)
{  // convert string to array of longs, each containing 4 chars
    // note chars must be within ISO-8859-1 (with Unicode code-point < 256) to fit 4/long
    var l = new Array(Math.ceil(s.length/4))
    for (var i=0; i<l.length; i++) {
        l[i] = s.charCodeAt(i*4) + (s.charCodeAt(i*4+1)<<8) +
               (s.charCodeAt(i*4+2)<<16) + (s.charCodeAt(i*4+3)<<24);
    }
    return(l);  // note running off the end of the string generates nulls since
}               // bitwise operators treat NaN as 0

function longsToStr(l) {  // convert array of longs back to string
    var s = "";
    for (var i=0; i<l.length; i++) {
        s += String.fromCharCode(l[i] & 0xFF, l[i]>>>8 & 0xFF,
                                 l[i]>>>16 & 0xFF, l[i]>>>24 & 0xFF);
    }
    return(s);
}

function validate_contact()
{
	var name = document.forms.contact.username.value;
	var email = document.forms.contact.email.value;
	var comments = document.forms.contact.comments.value;
	
	var error = "";
	if(comments == "") error = "Please enter a comment";
	if(EmailCheck(email) == false) error = "Please enter a valid Email address";
	if(name == "") error = "Please enter your name";

    if(error != "")
    {
        alert(error);
        return;
    }

	document.forms.contact.contact_us.value=true;
    document.forms.contact.submit();
}

function validate_reminder()
{
	var email = document.forms.reminder.email.value;

	var error = "";
	if(email == "") error = "Please enter either a valid Email address or your username";
	if (email.indexOf("@") != -1)
	{
		if(EmailCheck(email) == false) error = "Please enter a valid Email address";
	}
	else
	{
		document.forms.reminder.username.value=email;
	}
	

    if(error != "")
    {
        alert(error);
        return;
    }

	document.forms.reminder.reminder.value=true;
    document.forms.reminder.submit();
}

function encrypt_password_reset()
{
	var password1 = document.forms.is_reset.password1.value;
	var password2 = document.forms.is_reset.password2.value;

	var error = "";
	if(password1.length < 8) error = "Password must be at least 8 characters long";
    if(password1 != password2) error = "Passwords don't match";

    if(error != "")
    {
        alert(error);
        return;
    }

	var key = '9*n"¬pn@45gf(m^j';
	password1 = encrypt(password1,key);
	password2 = encrypt(password2,key);
	document.forms.is_reset.is_reset.value=true;
	document.forms.is_reset.password1.value = password1;
	document.forms.is_reset.password2.value = password2;
    document.forms.is_reset.submit();
}
