var valid_fn  = false;
var valid_ln  = false;
var valid_pw  = false;
var valid_tc  = false;
var valid_em  = false;
var valid_mt  = false;
var valid_db = false;
var valid_sq  = false;
var valid_sa  = false;
var valid_hnm = false;
var valid_rn = false;
var valid_ct = false;
var valid_county = false;
var valid_pcode = false;

function validateDoB( element ) 
{
		var y = $('dobYear').value;
		var m = $('dobMonth').value;
		var d = $('dobDay').value;

		var date_to_check = d+'/'+m+'/'+y;

		if(isDate(date_to_check)) 
		{ 
			Element.update($('dobStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
			Element.update($('Status'),''); 
			if(Element.hasClassName('ldob','form_error')) { Element.removeClassName('ldob','form_error'); }
			Element.addClassName('ldob','blue');
			valid_db = true;
		} 
		else 
		{
			Element.update($('dobStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
			//Element.update($('Status'),'please enter a valid data of birth'); 
			if(Element.hasClassName('ldob','blue')) { Element.removeClassName('ldob','blue'); }
			Element.addClassName('ldob','form_error');
			valid_db = false;
		}
		
		checkAll();
		return false;
}

function validateFirstName( element ) {
		var fn = $('firstname').value;
		if (fn.length > 1) { 
				Element.update($('firstnameStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lfirstname','form_error')) { Element.removeClassName('lfirstname','form_error'); }
				Element.addClassName('lfirstname','blue');
				valid_fn = true;
			} else {
				Element.update($('firstnameStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please enter first name'); 
				if(Element.hasClassName('lfirstname','blue')) { Element.removeClassName('lfirstname','blue'); }
				Element.addClassName('lfirstname','form_error');
				valid_fn = false;
			}
			checkAll();
			return false;
		}

function validateSecQ( element ) {
		var sq = $('secq').value;
		if (sq.length > 1) { 
				Element.update($('secQuestionStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lsecq','form_error')) { Element.removeClassName('lsecq','form_error'); }
				Element.addClassName('lsecq','blue');
				valid_sq = true;
			} else {
				Element.update($('secQuestionStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please choose a security question'); 
				if(Element.hasClassName('lsecq','blue')) { Element.removeClassName('lsecq','blue'); }
				Element.addClassName('lsecq','form_error');
				valid_sq = false;
			}
			checkAll();
			return false;
		}

function validateSecA( element ) {
		var sa = $('seca').value;
		if (sa.length > 1) { 
				Element.update($('secAnswerStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lseca','form_error')) { Element.removeClassName('lseca','form_error'); }
				Element.addClassName('lseca','blue');
				valid_sa = true;
			} else {
				Element.update($('secAnswerStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please choose a security answer'); 
				if(Element.hasClassName('lseca','blue')) { Element.removeClassName('lseca','blue'); }
				Element.addClassName('lseca','form_error');
				valid_sa = false;
			}
			checkAll();
			return false;
		}

function validateLastName( element ) {
		var ln = $('lastname').value;
		if (ln.length > 1) { 
				Element.update($('lastnameStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('llastname','form_error')) { Element.removeClassName('llastname','form_error'); }
				Element.addClassName('llastname','blue');
				valid_ln = true;
			} else {
				Element.update($('lastnameStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please enter last  name'); 
				if(Element.hasClassName('llastname','blue')) { Element.removeClassName('llastname','blue'); }
				Element.addClassName('llastname','form_error');
				valid_ln = false;
			}
			checkAll();
			return false;
		}

function validatePassword( element ) {
		var p1 = $('password1').value;
		var p2 = $('password2').value;
		
		if (p1.length > 5) { 
				Element.update($('password1Status'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lpassword1','form_error')) { Element.removeClassName('lpassword1','form_error'); }
				Element.addClassName('lpassword1','blue');
			} else if (p1.length > 0) {
				Element.update($('password1Status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
				Element.update($('Status'),'password too short'); 
				if(Element.hasClassName('lpassword1','blue')) { Element.removeClassName('lpassword1','blue'); }
				Element.addClassName('lpassword1','form_error');
			} else {
				Element.update($('password1Status'),''); 
				if(Element.hasClassName('lpassword1','blue')) { Element.removeClassName('lpassword2','blue'); }
				if(Element.hasClassName('lpassword1','form_error')) { Element.removeClassName('lpassword2','form_error'); }
			}
		
		if (p2.length > 5) { 
				Element.update($('password2Status'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lpassword2','form_error')) { Element.removeClassName('lpassword2','form_error'); }
				Element.addClassName('lpassword2','blue');
			} else if (p2.length > 0){
				Element.update($('password2Status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
				Element.update($('Status'),'password too short'); 
				if(Element.hasClassName('lpassword2','blue')) { Element.removeClassName('lpassword2','blue'); }
				Element.addClassName('lpassword2','form_error');
			} else {
				Element.update($('password2Status'),''); 
				if(Element.hasClassName('lpassword2','blue')) { Element.removeClassName('lpassword2','blue'); }
				if(Element.hasClassName('lpassword2','form_error')) { Element.removeClassName('lpassword2','form_error'); }
			}
		if (p1.length > 5 && p2.length > 5 && p1 != p2) {
				Element.update($('password2Status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
				Element.update($('Status'),'passwords do not match'); 
				if(Element.hasClassName('lpassword2','blue')) { Element.removeClassName('lpassword2','blue'); }
				Element.addClassName('lpassword2','form_error');
		}
		if (p1.length > 5 && p2.length > 5 && p1 == p2) {
			valid_pw = true;
		}
		
			checkAll();
			return false;
	}

function validateEmail( element ) {
		var em = $('email').value;	
		var response;
		   if ( (em.indexOf(".") > 0) && (em.indexOf("@") > 0)) {
			var a = new Ajax.Request('/join/check_data.php',
  				{
    			method:'get',
    			onSuccess: function(transport) {
					response = transport.responseText || "empty";
					  Element.update($('emailStatus'),'check email completed ');
					   if (response == 'ok') { 
							Element.update($('emailStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
							Element.update($('Status'),''); 
							if(Element.hasClassName('lemail','form_error')) { Element.removeClassName('lemail','form_error'); }
							Element.addClassName('lemail','blue');
							valid_em = true;
							checkAll();
						}  else if (response == 'registered') {
							Element.update($('Status'),'already registered'); 
							Element.update($('emailStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
							if(Element.hasClassName('lemail','blue')) { Element.removeClassName('lemail','blue'); }
							Element.addClassName('lemail','form_error');
							valid_em = false;
						}  else if (response == 'upgrade') {
							Element.update($('Status'),'already registered');
							Element.update($('Upgrade'),'This address is already registered, you can upgrade it with the discount <a href="/promo/'+$('promo_code').value+'U">here</a>.'); 
							Element.update($('emailStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
							if(Element.hasClassName('lemail','blue')) { Element.removeClassName('lemail','blue'); }
							Element.addClassName('lemail','form_error');
							valid_em = false;
						} else {
							Element.update($('Status'),'invalid email address'); 
							Element.update($('emailStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
							if(Element.hasClassName('lemail','blue')) { Element.removeClassName('lemail','blue'); }
							Element.addClassName('lemail','form_error');
							valid_em = false;
						}
    			},
    			onFailure: function(){ Element.update($('Status'),'could not check email address...')},
				onLoading: function(){ Element.update($('Status'),'checking email address...')},
				parameters: Form.Element.serialize('email')+'&'+Form.Element.serialize('promo_code')
 				});
			checkAll();
			return false;
		} else {
				Element.update($('emailStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
				Element.update($('Status'),'invalid email address'); 
				if(Element.hasClassName('lemail','blue')) { Element.removeClassName('lemail','blue'); }
				Element.addClassName('lemail','form_error');
				valid_em = false;
		}	
			checkAll();
			return false;
	}
function validateTerms( element ) {
		var t = $('terms').checked;
		if (t) { 
				Element.update($('termsStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lterms','form_error')) { Element.removeClassName('lterms','form_error'); }
				Element.addClassName('lterms','blue');
				valid_tc = true;
			} else {
				Element.update($('termsStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please agree to the t & c\'s'); 
				if(Element.hasClassName('lterms','blue')) { Element.removeClassName('lterms','blue'); }
				Element.addClassName('lterms','form_error');
				valid_tc = false;
			}
			checkAll();
			return false;
		}

function validateMemType( element ) {
		var mt = $('memtype').value;
		if (mt != '') { 
				Element.update($('memtypeStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lmemtype','form_error')) { Element.removeClassName('lmemtype','form_error'); }
				Element.addClassName('lmemtype','blue');
				valid_mt = true;
			} else {
				Element.update($('memtypeStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please choose your main role'); 
				if(Element.hasClassName('lmemtype','blue')) { Element.removeClassName('lmemtype','blue'); }
				Element.addClassName('lmemtype','form_error');
				valid_mt = false;
			}
			checkAll();
			return false;
		}

function validateHouse_Name_Number( element ) {
		var hnm = $('house_name_number').value;
			if (hnm != '') { 
				Element.update($('house_name_number_status'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lhouse_name_number','form_error')) { Element.removeClassName('lhouse_name_number','form_error'); }
				Element.addClassName('lhouse_name_number','blue');
				valid_hnm = true;
			} else {
				Element.update($('house_name_number_status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please enter house name/number'); 
				if(Element.hasClassName('lhouse_name_number','blue')) { Element.removeClassName('lhouse_name_number','blue'); }
				Element.addClassName('lhouse_name_number','form_error');
				valid_hnm = false;
			}
			checkAll();
			return false;
		}

function validateRoad_Name( element ) {
		var rn = $('road_name').value;
			if (rn != '') { 
				Element.update($('road_name_status'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lroad_name','form_error')) { Element.removeClassName('lroad_name','form_error'); }
				Element.addClassName('lroad_name','blue');
				valid_rn = true;
			} else {
				Element.update($('road_name_status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please enter road name'); 
				if(Element.hasClassName('lroad_name','blue')) { Element.removeClassName('lroad_name','blue'); }
				Element.addClassName('lroad_name','form_error');
				valid_rn = false;
			}
			checkAll();
			return false;
		}

function validateCity_Town( element ) {
		var ct = $('city_town').value;
			if (ct != '') { 
				Element.update($('city_town_status'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lcity_town','form_error')) { Element.removeClassName('lcity_town','form_error'); }
				Element.addClassName('lcity_town','blue');
				valid_ct = true;
			} else {
				Element.update($('city_town_status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please enter city/town'); 
				if(Element.hasClassName('lcity_town','blue')) { Element.removeClassName('lcity_town','blue'); }
				Element.addClassName('lcity_town','form_error');
				valid_ct = false;
			}
			checkAll();
			return false;
		}

function validateCounty( element ) {
		var county = $('county').value;
			if (county != '') { 
				Element.update($('county_status'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lcounty','form_error')) { Element.removeClassName('lcounty','form_error'); }
				Element.addClassName('lcounty','blue');
				valid_county = true;
			} else {
				Element.update($('county_status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please enter a county'); 
				if(Element.hasClassName('lcounty','blue')) { Element.removeClassName('lcounty','blue'); }
				Element.addClassName('lcounty','form_error');
				valid_county = false;
			}
			checkAll();
			return false;
		}

function validatePost_Code( element ) {
		var pcode = $('post_code').value;
			if (pcode.length > 5) { 
				Element.update($('post_code_status'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 
				Element.update($('Status'),''); 
				if(Element.hasClassName('lpost_code','form_error')) { Element.removeClassName('lpost_code','form_error'); }
				Element.addClassName('lpost_code','blue');
				valid_pcode = true;
			} else {
				Element.update($('post_code_status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">');
				Element.update($('Status'),'please enter a valid post code'); 
				if(Element.hasClassName('lpost_code','blue')) { Element.removeClassName('lpost_code','blue'); }
				Element.addClassName('lpost_code','form_error');
				valid_pcode = false;
			}
			checkAll();
			return false;
		}

function checkAll() {

	var get_address = $('get_address').value;

	if (valid_fn && valid_ln && valid_em && valid_tc && valid_pw && valid_mt && valid_db && valid_sq && valid_sa) 
	{
		if (get_address == 'true')
		{
			if (valid_hnm && valid_rn && valid_ct && valid_county && valid_pcode)
			{
				Element.update($('submitStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 

			}
			else
			{
				Element.update($('submitStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
			}
		}
		else
		{
			Element.update($('submitStatus'),'<img src="/images/join/tick.png" width="16" height="16" alt="yes">'); 

		}
	}
	else 
	{
		Element.update($('submitStatus'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
	}
	return false;
}

function checkSubmit() 
{
	var get_address = $('get_address').value;

	if (valid_fn && valid_ln && valid_em && valid_tc && valid_pw && valid_mt && valid_db && valid_sq && valid_sa) 
	{
		if (get_address == 'true')
		{
			if (valid_hnm && valid_rn && valid_ct && valid_county && valid_pcode)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true
		}
	} 
	else 
	{ 
		validateTerms();
		validateEmail();
		validateMemType();
		validateFirstName();
		validateLastName();
		validatePassword();
		validateDoB();
		validateSecQ();
		validateSecA();

		if (get_address == 'true')
		{
			validateHouse_Name_Number();
			validateRoad_Name();
			validateCity_Town();
			validateCounty();
			validatePost_Code();
		}

		if (! valid_pw) 
		{ 
			Element.update($('password1Status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
			if(Element.hasClassName('lpassword1','blue')) { Element.removeClassName('lpassword1','blue'); }
			Element.addClassName('lpassword1','form_error');
			Element.update($('password2Status'),'<img src="/images/join/cross.png" width="16" height="16" alt="no">'); 
			if(Element.hasClassName('lpassword2','blue')) { Element.removeClassName('lpassword2','blue'); }
			Element.addClassName('lpassword2','form_error');
		}

		Element.update($('Status'),'please complete the whole form to sign up');

		return false;
	}
}


