var frmJoin;
var selTitle;
var txtFirstName;
var txtLastName;
var txtAddress1;
var txtAddress2;
var txtTown;
var spnTownDescription;
var txtState;
var selCounty;
var spnCountyDescription;
var txtPostCode;
var spnPostCodeDescription;
var selCountry;
var txtTelephone;
var txtMobile;
var txtEmailAddress;
var txtPassword;
var txtConfirmPassword;
var divAutoVoucherCode;
var divManualVoucherCode;
var btnClearAutoVoucherCode;
var txtVoucherCode;
var btnApplyVoucherCode;
var hidAction;
var spnVoucherCodeValidation;
var hidProductId;

var currentVoucherCode=null;

window.onload=function() {
	frmJoin=document.getElementById('frmJoin');
	selTitle=document.getElementById('selTitle');
	txtFirstName=document.getElementById('txtFirstName');
	txtLastName=document.getElementById('txtLastName');
	txtAddress1=document.getElementById('txtAddress1');
	txtAddress2=document.getElementById('txtAddress2');
	txtTown=document.getElementById('txtTown');
	spnTownDescription=document.getElementById('spnTownDescription');
	txtState=document.getElementById('txtState');
	selCounty=document.getElementById('selCounty');
	spnCountyDescription=document.getElementById('spnCountyDescription');
	txtPostCode=document.getElementById('txtPostCode');
	spnPostCodeDescription=document.getElementById('spnPostCodeDescription');
	selCountry=document.getElementById('selCountry');
	txtTelephone=document.getElementById('txtTelephone');
	txtMobile=document.getElementById('txtMobile');
	txtEmailAddress=document.getElementById('txtEmailAddress');
	txtPassword=document.getElementById('txtPassword');
	txtConfirmPassword=document.getElementById('txtConfirmPassword');
	divAutoVoucherCode=document.getElementById('divAutoVoucherCode');
	divManualVoucherCode=document.getElementById('divManualVoucherCode');
	btnClearAutoVoucherCode=document.getElementById('btnClearAutoVoucherCode');
	txtVoucherCode=document.getElementById('txtVoucherCode');
	btnApplyVoucherCode=document.getElementById('btnApplyVoucherCode');
	hidAction=document.getElementById('hidAction');
	spnVoucherCodeValidation=document.getElementById('spnVoucherCodeValidation');
	hidProductId=document.getElementById('hidProductId');
	
	if(frmJoin) frmJoin.onsubmit=frmJoin_onsubmit;
	if(selCountry) selCountry.onchange=selCountry_onchange;
	if(txtVoucherCode) txtVoucherCode.onblur=txtVoucherCode_onblur;
	
	if(txtVoucherCode.value.length>0) txtVoucherCode_onblur();
}

function txtVoucherCode_onblur(e) {
	if(!e) e=window.event;
	
	if(txtVoucherCode.value.length==0) {
		spnVoucherCodeValidation.className='';
		spnVoucherCodeValidation.innerHTML='';
	}
	else if(txtVoucherCode.value!=currentVoucherCode) {
		spnVoucherCodeValidation.className='';
		spnVoucherCodeValidation.innerHTML='';
	
		var options={
			method: 'get',
			parameters: 'action=validateVoucherCode&voucherCode='+encodeURIComponent(txtVoucherCode.value)+'&productId='+hidProductId.value,
			onSuccess: function(xhr, json) {
	
				var response=getObjectFromPostData(xhr.responseText);
				response=normaliseResponse(response);
	
				if(response.result=='ok') {
					if(response.validated==1) {
						spnVoucherCodeValidation.className='voucherCodeValid';
						spnVoucherCodeValidation.innerHTML=response.offerName;
						if(response.exception.length>0) spnVoucherCodeValidation.innerHTML+='. '+response.exception;
					}
					else if(response.validated==0) {
						spnVoucherCodeValidation.className='voucherCodeInvalid';
						spnVoucherCodeValidation.innerHTML='Not valid!';
					}
				}
			}
		};
	
		// Send request
		var request=new Ajax.Request('gift.php', options);
	}
	
	currentVoucherCode=txtVoucherCode.value;
}

function frmJoin_onsubmit() {
	if(selTitle.selectedIndex==0) {
		alert('Please provide your title.');
		selTitle.focus();
		return(false);
	}
	else if(txtFirstName.value.length==0) {
		alert('Please provide your first name.');
		txtFirstName.focus();
		return(false);
	}
	else if(txtLastName.value.length==0) {
		alert('Please provide your last name.');
		txtLastName.focus();
		return(false);
	}
	else if(txtAddress1.value.length==0) {
		alert('Please provide your address.');
		txtAddress1.focus();
		return(false);
	}
	else if(txtTown.value.length==0) {
		if(selCountry.value.toUpperCase()=='UK' || selCountry.selectedIndex<1) {
			alert('Please provide your town.');
		}
		else {
			alert('Please provide your city.');
		}
		
		txtTown.focus();
		return(false);
	}
	else if(selCounty.selectedIndex<1 && (selCountry.value.toUpperCase()=='UK' || selCountry.selectedIndex<1)) {
		alert('Please provide your county.');
		selCounty.focus();
		return(false);
	}
	else if(txtState.value.length==0 && selCountry.value.toUpperCase()!='UK' && selCountry.selectedIndex>0) {
		alert('Please provide your state.');
		txtState.focus();
		return(false);
	}
	else if(txtPostCode.value.length==0) {
		if(selCountry.value.toUpperCase()=='UK' || selCountry.selectedIndex==0) {
			alert('Please provide your post code.');
		}
		else {
			alert('Please provide your zip code.');
		}
		
		txtPostCode.focus();
		return(false);
	}
	else if(selCountry.selectedIndex<1) {
		alert('Please provide your country.');
		selCountry.focus();
		return(false);
	}
	else if(txtTelephone.value.length==0 && txtMobile.value.length==0) {
		alert('Please provide your telephone number or mobile telephone number.');
		txtTelephone.focus();
		return(false);
	}
	else if(!validateEmail(txtEmailAddress.value)) {
		alert('The e-mail address you provided is not valid.');
		txtEmailAddress.focus();
		return(false);
	}
	else if(txtPassword.value.length==0) {
		alert('Please choose a password.');
		txtPassword.focus();
		return(false);
	}
	else if(txtPassword.value.length<6) {
		alert('Your password needs to be at least 6 characters long.');
		txtPassword.focus();
		return(false);
	}
	else if(txtConfirmPassword.value.length==0) {
		alert('Please type your password in again to confirm it is correct.');
		txtConfirmPassword.focus();
		return(false);
	}
	else if(txtPassword.value!=txtConfirmPassword.value) {
		alert('The password you provided does not match the confirmation version.');
		txtPassword.focus();
		return(false);
	}
	else
	{
		return(true);
	}
}

function selCountry_onchange() {
	if(selCountry.value.toUpperCase()=='UK') {
		spnTownDescription.innerHTML='Town';
		spnCountyDescription.innerHTML='County';
		spnPostCodeDescription.innerHTML='Post Code';
		selCounty.style.display='inline';
		txtState.style.display='none';
	}
	else {
		spnTownDescription.innerHTML='City';
		spnCountyDescription.innerHTML='State';
		spnPostCodeDescription.innerHTML='Zip';
		selCounty.style.display='none';
		txtState.style.display='inline';
	}
}

function btnClearAutoVoucherCode_onclick() {
	if(confirm('Are you sure you want to remove this voucher code and supply your own?')) {
		divAutoVoucherCode.style.display='none';
		divManualVoucherCode.style.display='block';
		txtVoucherCode.focus();
	}
}

function btnApplyVoucherCode_onclick() {
	hidAction.value='applyCode';
	frmJoin.submit();
}

function validateEmail(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					
}