// JavaScript Document

//Class: 						XSynApplication.js 
//Author: 						Jodi Curtis
//Created:						09/05/2008
//Modified:						12/05/2008
//Status:						R1.0.0
//Copyright: 					Xatrox Ltd All Rights Reserved
//Information and Licensing:	Contact Jodi Curtis at Xatrox Ltd or email jcurtis@xatroxsoftware.com

var xvalidator = new XValidator();

function cloneObject(what) {
    for (i in what) {
        if (typeof what[i] == 'object') {
            this[i] = new cloneObject(what[i]);
        }
        else
            this[i] = what[i];
    }
}

function Address(address1, address2, city, County, postcode, time) {
	this.address1 = address1;
	this.address2 = address2;
	this.city = city;
	this.County = County;
	this.postcode = postcode;
	this.time = time;
	this.address1Valid = false;
	this.address2Valid = false;
	this.cityValid = false;
	this.CountyValid = false;
	this.postcodeValid = false;
	this.timeValid = false;
	
	this.ValidateAddress = function () {
	
		this.address1Valid = xvalidator.validateString(2, 64, this.address1, ''); 
		this.address2Valid = xvalidator.validateString(0, 64, this.address2, ''); 
		this.cityValid = xvalidator.validateString(2, 64,  this.city, ''); 
		this.CountyValid = xvalidator.validateString(2, 64, this.County, ''); 
		this.postcodeValid = xvalidator.validatePostcode(this.postcode, ''); 
		this.timeValid = xvalidator.validateNumber(0, 65536, this.time, ''); 
		
		if ( (this.address1Valid == true) && (this.address2Valid == true) && (this.postcodeValid == true) && (this.cityValid == true) && (this.CountyValid == true) && (this.timeValid == true) ) {
			return true;
		}
		return false;
	}
};

function Partner(name, dob, certs) {
	this.name = name;
	this.dateOfBirth = dob;
	this.certsHeld = certs;
	this.numAddresses = 0;
	this.lastNumAddresses = 0;
	this.address = new Array();
	this.nameValid = false;
	this.dateOfBirthValid = false;
	this.certsHeldValid = false;
	this.numAddressesValid = false;
	this.addressValid = Array();
	this.requiredAddresses = 0;
	
	this.ValidatePartner = function () {
	
		this.nameValid = xvalidator.validateString(2, 64, this.name, '');
		this.dateOfBirthValid = xvalidator.validateDate(this.dateOfBirth, '');
		this.certsHeldValid = xvalidator.validateString(0, 255, this.certsHeld, '');
		this.numAddressesValid = xvalidator.validateNumber(1, 3, this.numAddresses, '');	
		
		var validpass = true;
		
		for (var i = 0; i < this.numAddresses; i++) {
			this.addressValid[i] = this.address[i].ValidateAddress();
			if ( this.addressValid == false) {
				validpass = false;
			}
		}
		
		if (validpass == true) {
			if ( (this.nameValid == true) && (this.dateOfBirthValid == true) && ( this.certsHeldValid == true) && ( this.numAddressesValid == true) ) {
				return true;
			}
		}
		return false;
	}
	
	this.AddAddress = function(id, address1, address2, city, County, postcode, time) {
		this.address[id] = new Address(address1, address2, city, County, postcode, time);
	}

	this.UpdateAddress = function(id, address1, address2, city, County, postcode, time) {
		this.address[id].address1 = address1;
		this.address[id].address2 = address2;
		this.address[id].city = city;
		this.address[id].County = County;
		this.address[id].postcode = postcode;
		this.address[id].time = time;
	}
	
	this.RemoveAddress = function(id) {
		this.address[id] = null;
	}
};

function Apply() {

	this.companyName = '';
	this.registrationNo = '';
	this.email = '';
	this.telephone = '';
	this.timeExisted = 0;
	this.businessType = '';
	this.numPartners = 0;
	this.address = new Address('','','','','',0);
	this.partners = Array();
	this.loanPurpose = '';
	this.loanAmount = 0;
	this.loanTerm = 5;
	this.additionalInfo = '';
	this.lastPartners = 0;
	
	this.checked = false;
	this.companyNameValid = false;
	this.registrationNoValid = false;
	this.emailValid = false;
	this.telephoneValid = false;
	this.timeExistedValid = false;
	this.businessTypeValid = false;
	this.numPartnersValid = false;
	this.addressValid = false;
	this.partnersValid = Array();
	this.loanPurposeValid = false;
	this.loanAmountValid = false;
	this.loanTermValid = false;
	this.additionalInfoValid = false;
	
	this.AddPartner = function (id, name, dob, certs) {
		this.partners[id] = new Partner(name, dob, certs);
		this.partners[id].requiredAddresses++;
	}
	
	this.RemovePartner = function (id) {
		this.partners[id] = null;
	}
	
	this.UpdatePartner = function (id, name, dob, certs) {
		this.partners[id].name = name;
		this.partners[id].dateOfBirth = dob;
		this.partners[id].certsHeld = certs;
	}
	
	this.Validate = function () {
		
		var counter = 0;
		
		this.companyNameValid = xvalidator.validateString(2, 64, this.companyName, '');
		this.registrationNoValid = xvalidator.validateString(0, 64, this.registrationNo, '');
		this.emailValid = xvalidator.validateEmail(this.email, '');
		this.telephoneValid = xvalidator.validateTelno(this.telephone, '');
		this.timeExistedValid = xvalidator.validateNumber(0, 65536, this.timeExisted, '');
		this.businessTypeValid = xvalidator.validateString(2, 64, this.businessType, '');
		this.numPartnersValid = xvalidator.validateNumber(1, 5, parseInt(this.numPartners), '');
		
		this.loanPurposeValid = xvalidator.validateString(2, 64, this.loanPurpose, '');

		this.loanAmountValid = xvalidator.validateNumber(1000, 50000000, parseInt(this.loanAmount), '');
		this.loanTermValid = xvalidator.validateNumber(0.5, 10, parseFloat(this.loanTerm), '');
		this.additionalInfoValid = xvalidator.validateString(0, 16384, this.additionalInfo, '');
		
		document.getElementById('showerror').innerHTML = '';
		
		if (this.companyNameValid == false) {
			SetInvalid(document.getElementById('company_name'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please enter a valid business name.<br />';
			}
			counter++;
		}
		
		if (this.registrationNoValid == false) {
			SetInvalid(document.getElementById('RegistrationNo'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please enter a valid registration number.<br />';
			}
			counter++;
		}
		
		if (this.emailValid == false) {
			SetInvalid(document.getElementById('Email'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please provide a valid email address.<br />';
			}
			counter++;
		}
		
		if (this.telephoneValid == false) {
			SetInvalid(document.getElementById('Telephone'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please provide a valid telephone number.<br />';
			}
			counter++;
		}
		
		if (this.timeExistedValid == false) {
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please set the length of time the business has been operating.<br />';
			}
			counter++;
		}
		
		if (this.businessTypeValid == false) {
			SetInvalid(document.getElementById('NatureOfBusiness'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please provide the nature of the business.<br />';
			}
			counter++;
		}
		
		if (this.numPartnersValid == false) {
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please select the number of partners or directors the business has.<br />';
			}
			counter++;
		}
		
		var validpass = true;
		this.addressValid = this.address.ValidateAddress();
		
		if (this.addressValid == false) {
			validpass = false;
			
			if (this.address.address1Valid == false) {
				SetInvalid(document.getElementById('Addr1'));
				if (counter < 5) {
					document.getElementById('showerror').innerHTML += 'Please enter a valid address building name or number for the business.<br />';
				}
				counter++;
			}
			
			if (this.address.address2Valid == false) {
				SetInvalid(document.getElementById('Addr2'));
				if (counter < 5) {
					document.getElementById('showerror').innerHTML += 'Please enter less text for the address street of the business.<br />';
				}
				counter++;
			}
			
			if (this.address.cityValid == false) {
				SetInvalid(document.getElementById('City'));
				if (counter < 5) {
					document.getElementById('showerror').innerHTML += 'Please enter a valid city.<br />';
				}
				counter++;
			}
			
			if (this.address.CountyValid == false) {
				SetInvalid(document.getElementById('County'));
				if (counter < 5) {
					document.getElementById('showerror').innerHTML += 'Please enter a valid county.<br />';
				}
				counter++;
			}
			
			if (this.address.postcodeValid == false) {
				SetInvalid(document.getElementById('Postcode'));
				if (counter < 5) {
					document.getElementById('showerror').innerHTML += 'Please enter a valid zip/postal code.<br />';
				}
				counter++;
			}
		}
		
		for (var i = 0; i < this.numPartners; i++) {
		
			this.partnersValid[i] = this.partners[i].ValidatePartner();
			
			if (this.partnersValid[i] == false) {
				validpass = false;
				
				if (this.partners[i].nameValid == false) {
					SetInvalid(document.getElementById('Part'+i+'Name'));
					if (counter < 5) {
						document.getElementById('showerror').innerHTML += 'Please provide a full name for partner/director '+(i+1)+'.<br />';
					}
					counter++;
				}
				
				if (this.partners[i].dateOfBirthValid == false) {
					if (counter < 5) {
						document.getElementById('showerror').innerHTML += 'Please provide a valid date of birth for partner/director '+(i+1)+'.<br />';
					}
					counter++;
				}
				
				if (this.partners[i].certsHeldValid == false) {
					SetInvalid(document.getElementById('Part'+i+'Quals'));
					if (counter < 5) {
						document.getElementById('showerror').innerHTML += 'Please provide a more concise list of certificates held for partner/director '+(i+1)+'.<br />';
					}
					counter++;
				}
				document.getElementById('Part'+i+'NumAddresses').value = this.partners[i].numAddresses;
				for (var j = 0; j < this.partners[i].numAddresses; j++) {
		
					if (this.partners[i].address[j].address1Valid == false) {
						SetInvalid(document.getElementById('Part'+i+'Addr'+j+'Addr1'));
						if (counter < 5) {
							document.getElementById('showerror').innerHTML += 'Please provide the address building name or number for partner/director '+(i+1)+' address '+(j+1)+'.<br />';
						}
						counter++;
					}
					
					if (this.partners[i].address[j].address2Valid == false) {
						SetInvalid(document.getElementById('Part'+i+'Addr'+j+'Addr2'));
						if (counter < 5) {
							document.getElementById('showerror').innerHTML += 'Please enter less text for the address street of partner/director '+(i+1)+' address '+(j+1)+'.<br />';
						}
						counter++;
					}
					
					if (this.partners[i].address[j].cityValid == false) {
						SetInvalid(document.getElementById('Part'+i+'Addr'+j+'City'));
						if (counter < 5) {
							document.getElementById('showerror').innerHTML += 'Please enter the address city for partner/director '+(i+1)+' address '+(j+1)+'.<br />';
						}
						counter++;
					}
					
					if (this.partners[i].address[j].CountyValid == false) {
						SetInvalid(document.getElementById('Part'+i+'Addr'+j+'County'));
						if (counter < 5) {
							document.getElementById('showerror').innerHTML += 'Please enter the address county for partner/director '+(i+1)+' address '+(j+1)+'.<br />';	
						}
						counter++;
					}
					
					if (this.partners[i].address[j].postcodeValid == false) {
						SetInvalid(document.getElementById('Part'+i+'Addr'+j+'Post'));
						if (counter < 5) {
							document.getElementById('showerror').innerHTML += 'Please provide a valid postcode for partner/director '+(i+1)+' address '+(j+1)+'.<br />';
						}
						counter++;
					}
				}
				
			}
		}
		
		if (this.loanPurposeValid == false) {
			SetInvalid(document.getElementById('Purpose'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please tell us the purpose of the loan.<br />';
			}
			counter++;
		}
		
		if (this.loanAmountValid == false) {
			SetInvalid(document.getElementById('Amount'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please choose a loan amount between 1K and 50M pounds entering whole numbers only.<br />';
			}
			counter++;
		}
		
		if (this.loanTermValid == false) {
			SetInvalid(document.getElementById('Term'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please select the number of months/years the loan is to be repaid over.<br />';
			}
			counter++;
		}
		
		if (this.additionalInfoValid == false) {
			SetInvalid(document.getElementById('Additional'));
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please provide more concise additional information.<br />';
			}
			counter++;
		}
		
		if (this.checked == false) {
			if (counter < 5) {
				document.getElementById('showerror').innerHTML += 'Please confirm ALL directors / partners have read and agree to our data protection policy and terms and conditions.<br />';
			}
			counter++;
		}
		
		if ( (validpass == true) && (this.checked == true) ) {
			if ( (this.companyNameValid == true) && (this.registrationNoValid == true) && ( this.emailValid == true) && ( this.telephoneValid == true) && (this.timeExistedValid == true) && (this.businessTypeValid == true) && (this.numPartnersValid == true) && (this.loanPurposeValid == true) && (this.loanAmountValid == true) && (this.loanTermValid == true) && (this.additionalInfoValid == true) ) {
				return true;
			} 
		}
		
		if (counter > 5) {
			var errors = counter - 5;
			document.getElementById('showerror').innerHTML += 'This application contains '+errors+' more errors.<br />';
		}

		document.getElementById('applyerrortitle').innerHTML = 'This application form contains the following errors:';
		document.getElementById('Agree').checked = false;
		this.checked = (document.getElementById('Agree').checked == true) ? true : false;
		return false;
		
	}
	
	this.StoreInfo = function () {

		this.checked = (document.getElementById('Agree').checked == true) ? true : false;
		this.companyName = document.getElementById('company_name').value;
		this.registrationNo = document.getElementById('RegistrationNo').value;
		this.email = document.getElementById('Email').value;
		this.telephone = document.getElementById('Telephone').value;
		this.timeExisted = (parseInt(document.getElementById('TimeEstYear').value) * 12) + parseInt(document.getElementById('TimeEstMonth').value);
		this.businessType = document.getElementById('NatureOfBusiness').value;

		this.loanPurpose = document.getElementById('Purpose').value;
		this.loanAmount = document.getElementById('Amount').value;
		this.loanTerm = document.getElementById('Term').value;
		this.additionalInfo = document.getElementById('Additional').value;
		
		this.address = null;
		
		this.address = new Address( 
			(document.getElementById('Addr1').value), 
			(document.getElementById('Addr2').value), 
			(document.getElementById('City').value), 
			(document.getElementById('County').value), 
			(document.getElementById('Postcode').value), 
			1);	
		
		this.lastPartners = this.numPartners;
		this.numPartners = document.getElementById('NumPartners').value;
		
		for (var i = 0; i < this.lastPartners; i++) {
			this.partners[i].lastNumAddresses = this.partners[i].numAddresses;
		}
		
		var partCopy = Array();
		
		for (var i = (this.lastPartners - 1); i >= 0; i--) {
			partCopy[i] = new cloneObject(this.partners[i]);
			
			for (var j = (this.partners[i].numAddresses - 1); j >= 0 ; j--) {
				this.partners[i].RemoveAddress(j);
				this.partners[i].numAddresses--;
			}
			this.RemovePartner(i);
		} 
		
		this.partners = null;
		this.partners = Array();
		
		for (var i = 0; i < this.numPartners; i++) {	
			this.AddPartner(i,'','','');
			
			if (i < this.lastPartners) {
				this.partners[i].requiredAddresses = partCopy[i].requiredAddresses;
				this.partners[i].lastNumAddresses = partCopy[i].lastNumAddresses;
			}
			
			for (var j = 0; j < Math.max(this.partners[i].requiredAddresses,1); j++) {
				this.partners[i].AddAddress(j,'','','','','',0);
				this.partners[i].numAddresses++;
			}
		}
		
		for (var i = 0; i < (Math.min(this.lastPartners,this.numPartners)); i++) {
			var dobD = (parseInt(document.getElementById('Part'+i+'DobDay').value));
			var dobM = (parseInt(document.getElementById('Part'+i+'DobMonth').value));
			var dobY = (parseInt(document.getElementById('Part'+i+'DobYear').value));
			
			var	strdobD = (dobD < 10) ? '0'+dobD : dobD;
			var	strdobM = (dobM < 10) ? '0'+dobM : dobM;
			var strappended = (strdobD+'-'+strdobM+'-'+dobY);
			this.UpdatePartner(i, 
				(document.getElementById('Part'+i+'Name').value), 
				strappended, 
				(document.getElementById('Part'+i+'Quals').value) 
			);
			
			for (var j = 0; j < (Math.min(this.partners[i].lastNumAddresses,this.partners[i].numAddresses)); j++) {
				var y = (parseInt(document.getElementById('Part'+i+'Addr'+j+'TimeYears').value) * 12);
				var m = (parseInt(document.getElementById('Part'+i+'Addr'+j+'TimeMonths').value) );
				var len = ( y + m );
				
				this.partners[i].UpdateAddress(j, 
					(document.getElementById('Part'+i+'Addr'+j+'Addr1').value), 
					(document.getElementById('Part'+i+'Addr'+j+'Addr2').value), 
					(document.getElementById('Part'+i+'Addr'+j+'City').value), 
					(document.getElementById('Part'+i+'Addr'+j+'County').value), 
					(document.getElementById('Part'+i+'Addr'+j+'Post').value), 
					len );																																																																																																													
			}
		}
		partCopy = null;
	}

	this.RestoreInfo = function () { 
		
		document.getElementById('Agree').checked = this.checked;
		document.getElementById('company_name').value = this.companyName;
		document.getElementById('RegistrationNo').value = this.registrationNo;
		document.getElementById('Email').value = this.email;
		document.getElementById('Telephone').value = this.telephone;
		document.getElementById('TimeEstYear').value = parseInt(Math.floor(this.timeExisted / 12));
		document.getElementById('TimeEstMonth').value = (this.timeExisted - (parseInt(Math.floor(this.timeExisted / 12))) * 12);
		document.getElementById('NatureOfBusiness').value = this.businessType;

		document.getElementById('Purpose').value = this.loanPurpose;
		document.getElementById('Amount').value = this.loanAmount;
		document.getElementById('Term').value = this.loanTerm;
		document.getElementById('Additional').value = this.additionalInfo;
		
		document.getElementById('Addr1').value = this.address.address1;
		document.getElementById('Addr2').value = this.address.address2;
		document.getElementById('City').value = this.address.city;
		document.getElementById('County').value = this.address.County;
		document.getElementById('Postcode').value = this.address.postcode;
		
		document.getElementById('NumPartners').value = this.numPartners;
		
		for (var i = 0; i < this.numPartners; i++) {
			
			for (var j = 0; j < Math.min(this.partners[i].lastNumAddresses, this.partners[i].numAddresses); j++) {
				var y = parseInt(Math.floor(this.partners[i].address[j].time / 12));
				var m = parseInt(this.partners[i].address[j].time) - (y * 12);
				document.getElementById('Part'+i+'Addr'+j+'Addr1').value = this.partners[i].address[j].address1;
				document.getElementById('Part'+i+'Addr'+j+'Addr2').value = this.partners[i].address[j].address2;
				document.getElementById('Part'+i+'Addr'+j+'City').value = this.partners[i].address[j].city;
				document.getElementById('Part'+i+'Addr'+j+'County').value = this.partners[i].address[j].County;
				document.getElementById('Part'+i+'Addr'+j+'Post').value = this.partners[i].address[j].postcode;
				document.getElementById('Part'+i+'Addr'+j+'TimeYears').value = y;
				document.getElementById('Part'+i+'Addr'+j+'TimeMonths').value = m;
			}
			
			document.getElementById('Part'+i+'Name').value = this.partners[i].name;
			
			var dobD = this.partners[i].dateOfBirth.substr(0,2);
			var dobM = this.partners[i].dateOfBirth.substr(3,2);
			var dobY = this.partners[i].dateOfBirth.substr(6,4);
			document.getElementById('Part'+i+'DobDay').value = parseInt(dobD);
			document.getElementById('Part'+i+'DobMonth').value = parseInt(dobM);
			document.getElementById('Part'+i+'DobYear').value = parseInt(dobY);
			document.getElementById('Part'+i+'Quals').value = this.partners[i].certsHeld;
			document.getElementById('Part'+i+'NumAddresses').value = this.partners[i].numAddresses;
		}
		
		this.lastPartners = this.numPartners;
		this.numPartners = document.getElementById('NumPartners').value;
		
		for (var i = 0; i < this.numPartners; i++) {
			this.partners[i].lastNumAddresses = this.partners[i].numAddresses;
		}
	}	
	
	this.PartnerSelect = function () {
		this.StoreInfo();
		this.UpdateFormPartner();
		this.RestoreInfo();
	}

	this.AddressSelect = function (partner) {
		var len = 0;
		var pass = true;
		this.partners[partner].requiredAddresses = 1;
		var stopat = 0;
		var postinfo = '';
		var stopset = false;
		
		document.getElementById('applyerrortitle').innerHTML = '';
		document.getElementById('showerror').innerHTML = '';
		
		SetValid(document.getElementById('Part'+partner+'Name'));
		SetValid(document.getElementById('Part'+partner+'Quals'));
			
		for (var i = 0; i < this.partners[partner].numAddresses; i++) {
			SetValid(document.getElementById('Part'+partner+'Addr'+i+'Addr1'));
			SetValid(document.getElementById('Part'+partner+'Addr'+i+'Addr2'));
			SetValid(document.getElementById('Part'+partner+'Addr'+i+'City'));
			SetValid(document.getElementById('Part'+partner+'Addr'+i+'County'));
			SetValid(document.getElementById('Part'+partner+'Addr'+i+'Post'));
					
			var curlen = 0;
			 curlen = ( (parseInt(document.getElementById('Part'+partner+'Addr'+i+'TimeYears').value) * 12) + (parseInt(document.getElementById('Part'+partner+'Addr'+i+'TimeMonths').value) ) );
			len += curlen; 
			
			if ( (curlen <= 0) && (len < 36) && (stopset != true) ) {
				if (stopset != true) {
					stopset = true;
					stopat = this.partners[partner].requiredAddresses;
				}
	
			}
			
			if ( (len < 36) && (stopset != true) &&  (this.partners[partner].requiredAddresses < 3) ) {
				this.partners[partner].requiredAddresses++;
				
			}
		}
		if ( (stopat > 0) && (this.partners[partner].requiredAddresses <= stopat) ) {	
			document.getElementById('applyerrortitle').innerHTML = 'Before processing this application you must enter a length of time director / partner '+(partner+1)+' has been at each address.';
			document.getElementById('Agree').checked = false;
			this.checked = (document.getElementById('Agree').checked == true) ? true : false;
			pass = false;
		} else if (this.partners[partner].requiredAddresses > 3) {
			document.getElementById('applyerrortitle').innerHTML += 'A director cannot have more than three addresses in three years, please call an advisor on 0845 124 111 to discuss whether we can still accept your application.';
			document.getElementById('Agree').checked = false;
			this.checked = (document.getElementById('Agree').checked == true) ? true : false;
			pass = false;
		} else if (this.partners[partner].requiredAddresses > this.partners[partner].numAddresses) {
			document.getElementById('applyerrortitle').innerHTML += 'Please provide an additional address for director / partner '+(partner+1)+'.';
			document.getElementById('Agree').checked = false;
			this.checked = (document.getElementById('Agree').checked == true) ? true : false;
			pass = false;
		}
		
		this.StoreInfo();
		this.UpdateFormAddress(partner);
		this.RestoreInfo();
		
		return pass;
	}
	
	this.ProcessApp = function() {
		var continueapp = true;
		
		SetValid(document.getElementById('company_name'));
		SetValid(document.getElementById('RegistrationNo'));
		SetValid(document.getElementById('Email'));
		SetValid(document.getElementById('Telephone'));
		SetValid(document.getElementById('NatureOfBusiness'));

		SetValid(document.getElementById('Purpose'));
		SetValid(document.getElementById('Amount'));
		SetValid(document.getElementById('Term'));
		SetValid(document.getElementById('Additional'));
		
		SetValid(document.getElementById('Addr1'));
		SetValid(document.getElementById('Addr2'));
		SetValid(document.getElementById('City'));
		SetValid(document.getElementById('County'));
		SetValid(document.getElementById('Postcode'));
		
		
		for (var i  = 0; i < this.numPartners; i++) {
			if (continueapp == true) {
				if(this.AddressSelect(i) == false) {
					continueapp = false;
				}	
			}
		}
		
		if (continueapp == true) {
			continueapp = this.Validate();
		}
		
		if (continueapp == true) {
			/* copy across all data */
			var frm = document.getElementById('formapply');
			
			for (var i = 0; i < this.numPartners; i++) {
				//copy each partner dets to form element
				frm.appendChild(document.getElementById('Part'+i+'Name'));	
				frm.appendChild(document.getElementById('Part'+i+'Quals'));	
				frm.appendChild(document.getElementById('Part'+i+'DobDay'));	
				frm.appendChild(document.getElementById('Part'+i+'DobMonth'));	
				frm.appendChild(document.getElementById('Part'+i+'DobYear'));
				frm.appendChild(document.getElementById('Part'+i+'NumAddresses'));
				
				document.getElementById('Part'+i+'NumAddresses').value = this.partners[i].numAddresses;
				
				for (var j = 0; j < this.partners[i].numAddresses; j++) {
					//copy address details to cc form element
					frm.appendChild(document.getElementById('Part'+i+'Addr'+j+'Addr1'));	
					frm.appendChild(document.getElementById('Part'+i+'Addr'+j+'Addr2'));
					frm.appendChild(document.getElementById('Part'+i+'Addr'+j+'City'));	
					frm.appendChild(document.getElementById('Part'+i+'Addr'+j+'County'));	
					frm.appendChild(document.getElementById('Part'+i+'Addr'+j+'Post'));
					frm.appendChild(document.getElementById('Part'+i+'Addr'+j+'TimeMonths'));
					frm.appendChild(document.getElementById('Part'+i+'Addr'+j+'TimeYears'));
				}
			}
			document.formapply.submit();
		}
		
		return continueapp;
	}
	
	this.UpdateFormPartner = function () {
		
		document.getElementById('partnersLoc').innerHTML = '';
		
		for (var i = 0; i < this.numPartners; i++) {
			
			var output = '';
			
			output += "<div><table width=\"390px\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td width=\"390px\" colspan=\"3\"><br /></td></tr><tr><td width=\"390px\" colspan=\"3\"><span class=\"xBodyHeaderSmall\">Partner / Director "+(i+1)+" Details:<span></td></tr><tr><td width=\"390px\" colspan=\"3\"><br /></td></tr><tr></tr></table></div>";
		
			output += "<div class=\"xColLeft\"><table width=\"390px\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr height=\"24px\"><td width=\"10px\" class=\"xPageMainNormal\">*</td><td width=\"150px\"><span class=\"xPageMainNormal\">Name:</span></td><td width=\"230px\">";
			
			output += "<input class=\"xFormInput\" name=\"Part"+i+"Name\" id=\"Part"+i+"Name\" type=\"text\" size=\"36\" onMouseOver'SetActive(this);' onMouseOut='SetInactive(this);' onBlur='SetInactive(this);' onFocus='SetActive(this);' maxLength=\"64\" /></td></tr><tr height=\"24px\"><td width=\"10px\" class=\"xPageMainNormal\">*</td><td class=\"xPageMainNormal\" width=\"150px\"><span class=\"xPageMainNormal\">Date of birth:</span></td><td class=\"xPageMainNormal\" width=\"230px\">";
			
			output += "<select class=\"xFormInput\" name=\"Part"+i+"DobDay\" id=\"Part"+i+"DobDay\" size=\"1\" onMouseOver'SetActive(this);' onMouseOut='SetInactive(this);' onBlur='SetInactive(this);' onFocus='SetActive(this);' >";
			

			for (var j = 1; j < 32; j++) { 
				output += "<option value=\""+j+"\">"+j+"</option>"; 	
			}
			
			output += "";
			
			output += "</select>";
		
			output +="<select class=\"xFormInput\" name=\"Part"+i+"DobMonth\" id=\"Part"+i+"DobMonth\" size=\"1\" onMouseOver'SetActive(this);' onMouseOut='SetInactive(this);' onBlur='SetInactive(this);' onFocus='SetActive(this);' >";
				
			for (var k = 1; k < 13; k++) { 
				output += "<option value=\""+k+"\">"+k+"</option>"; 	
			}
			
			output += "</select>";
		
			output +="<select class=\"xFormInput\" name=\"Part"+i+"DobYear\" id=\"Part"+i+"DobYear\" size=\"1\" onMouseOver'SetActive(this);' onMouseOut='SetInactive(this);' onBlur='SetInactive(this);' onFocus='SetActive(this);' >";
			
			for (var l = 1900; l < (new Date().getFullYear()) - 15; l++) { 
				output += "<option value=\""+l+"\">"+l+"</option>"; 	
			}
			
			output += "</select></td></tr>";
					
			output +="<tr height=\"100px\"><td class=\"xPageMainNormal\" width=\"10px\"></td><td valign=\"top\" width=\"150px\"><span class=\"xPageMainNormal\">Practicing<br /> certificates<br /> held:</span></td><td valign=\"top\" class=\"xPageMainNormal\" width=\"230px\"><textarea class=\"xFormInput\" name=\"Part"+i+"Quals\" id=\"Part"+i+"Quals\" rows=\"3\" cols=\"36\" onMouseOver'SetActive(this);' onMouseOut='SetInactive(this);' onBlur='SetInactive(this);' onFocus='SetActive(this);' ></textarea></td></tr></table><input name=\"Part"+i+"NumAddresses\" id=\"Part"+i+"NumAddresses\" type=\"hidden\" />";

			output += "</div><div class=\"xColRight\" name=\"Part"+i+"AddressLoc\" id=\"Part"+i+"AddressLoc\"></div><br clear=\"all\" >";
			document.getElementById('partnersLoc').innerHTML += output;
			
			this.UpdateFormAddress(i); 
		}
	}

	this.UpdateFormAddress = function (partner) {
		
		document.getElementById('Part'+partner+'AddressLoc').innerHTML = '';
		
		for (var z = 0; z < this.partners[partner].requiredAddresses; z++) {
			var output = '';
			
			var addrstring = '';
			switch (z) {
				case 0:
				addrstring='Please enter the current home address';
				break;
				case 1:
				addrstring='Please enter the previous home address';
				break;
				case 2:
				addrstring='Please enter an earlier home address';
				break;
				default:
				break;
			}
			
			output += '<div><table width=\"390px\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr height="24px"><td colspan="3" class="xPageSubBig">'+addrstring+'</td></tr>';
	
	
			
			
			output += '<tr height="24px"><td class="xPageMainNormal" width="10px">*</td><td width="150px"><span class="xPageMainNormal">House name/no:</span></td><td width="230px"><input class="xFormInput" name="Part'+partner+'Addr'+z+'Addr1" id="Part'+partner+'Addr'+z+'Addr1" type="text" size="36" maxLength="64" onFocus="SetActive(this)" onBlur="SetInactive(this)" onMouseOver="SetActive(this)" onMouseOut="SetInactive(this)" /></tr>';
			
			
			
			output += '<tr height="24px"><td class="xPageMainNormal" width="10px">*</td><td width="150px"><span class="xPageMainNormal">Street:</span></td><td width="230px"><input class="xFormInput" name="Part'+partner+'Addr'+z+'Addr2" id="Part'+partner+'Addr'+z+'Addr2" type="text" size="36" maxLength="64" onFocus="SetActive(this)" onBlur="SetInactive(this)" onMouseOver="SetActive(this)" onMouseOut="SetInactive(this)" /></tr>';
			
			
			output += '<tr height="24px"><td class="xPageMainNormal" width="10px">*</td><td width="150px"><span class="xPageMainNormal">Town or city:</span></td><td width="230px"><input class="xFormInput" name="Part'+partner+'Addr'+z+'City" id="Part'+partner+'Addr'+z+'City" type="text" size="36" maxLength="64" onFocus="SetActive(this)" onBlur="SetInactive(this)" onMouseOver="SetActive(this)" onMouseOut="SetInactive(this)" /></tr>';
			output += '<tr height="24px"><td class="xPageMainNormal" width="10px">*</td><td width="150px"><span class="xPageMainNormal">County:</span></td><td width="230px"><input class="xFormInput" name="Part'+partner+'Addr'+z+'County" id="Part'+partner+'Addr'+z+'County" type="text" size="36" maxLength="64" onFocus="SetActive(this)" onBlur="SetInactive(this)" onMouseOver="SetActive(this)" onMouseOut="SetInactive(this)" /></tr>';
			
			output += '<tr height="24px"><td class="xPageMainNormal" width="10px">*</td><td width="150px"><span class="xPageMainNormal">Postcode:</span></td><td width="230px"><input class="xFormInput" name="Part'+partner+'Addr'+z+'Post" id="Part'+partner+'Addr'+z+'Post" type="text" size="16" maxLength="64" onFocus="SetActive(this)" onBlur="SetInactive(this)" onMouseOver="SetActive(this)" onMouseOut="SetInactive(this)" /></tr>';
			
			output += '<tr height="24px"><td class="xPageMainNormal" width="10px">*</td><td width="150px"><span class="xPageMainNormal">Time at address:</span></td><td width="230px">';
			
			
			output += '<select class="xFormInput" name="Part'+partner+'Addr'+z+'TimeYears" id="Part'+partner+'Addr'+z+'TimeYears"  size="1" maxLength="64" onFocus="SetActive(this)" onBlur="SetInactive(this)" onMouseOver="SetActive(this)" onMouseOut="SetInactive(this)" />';	
			
			for (var i = 0; i < 49; i++) { 
				output += '<option value="'+i+'">'+i+'</option>'; 	
			} 
			
			output += "<option value=\"50+\">50+</option>";
			output += '</select><span class="xPageMainNormal">&nbsp;Years&nbsp;</span>';
			
			output += '<select class="xFormInput" name="Part'+partner+'Addr'+z+'TimeMonths" id="Part'+partner+'Addr'+z+'TimeMonths"  size="1" maxLength="64" onFocus="SetActive(this)" onBlur="SetInactive(this)" onMouseOver="SetActive(this)" onMouseOut="SetInactive(this)" />';	
			
			for (var i = 0; i < 11; i++) { 
				output += "<option value=\""+i+"\">"+i+"</option>"; 	
			} 
		
			output += '</select><span class="xPageMainNormal">&nbsp;Months&nbsp;</span></td><tr></table></div><br />';
			
			document.getElementById('Part'+partner+'AddressLoc').innerHTML += output;
		}
	}
};