// Validate PA
function validate_preapproval(form) {
	var fname = $(form['fname']);
	var lname = $(form['lname']);
	var email =$(form['email']);

	if(form['landline']) var landline = $(form['landline']);
	if(form['phone']) var phone = $(form['phone']);
	if(form['mobile']) var mobile = $(form['mobile']);

	var state = $(form['state']);

	if(form['company_name']) var company_name = $(form['company_name']);
	if(form['ownership']) var ownership = $(form['ownership']);

	var amount = $(form['amount']);

	if(form['balloon']) var balloon = $(form['balloon']);

	var terms = $(form['terms']);

	if(form['employment']) var employment = $(form['employment']);
	if(form['employment_time_1']) var employment_time_1 = $(form['employment_time_1']);
	if(form['employment_time_2']) var employment_time_2 = $(form['employment_time_2']);
	if(form['employment_time_3']) var employment_time_3 = $(form['employment_time_3']);
	
	if(form['residential_status']) var residential_status = $(form['residential_status']);
	if(form['residency_time_1']) var residency_time_1 = $(form['residency_time_1']);
	if(form['residency_time_2']) var residency_time_2 = $(form['residency_time_2']);
	if(form['residency_time_3']) var residency_time_3 = $(form['residency_time_3']);

	if(form['credit_history']) var credit_history = $(form['credit_history']);

	if(form['loan_type']) var loan_type = $(form['loan_type']);

	var sError = "";

	// Validate Name
	if ( fname.value.length == 0 ) {
		sError += "Please enter your First Name.\n";
	}

	if ( lname.value.length == 0 ) {
		sError += "Please enter your Last Name.\n";
	}

	// Validate Email
	if ( email.value.length == 0 ) {
		sError += "Please enter your Email Address.\n";
	} else {

		var str = email.value;
		var patt1 = new RegExp("^[a-z][a-zA-Z0-9_.-]*@[a-zA-Z0-9_.-]+\.[a-z]{2,4}$");
		var result = patt1.test(str);
		if ( !result ) {
			sError += "The Email is not valid.\n";
		}
	}

	// Validate landline
	if(landline) {
		if ( landline.value.length == 0 ) {
			sError += "Please provide your Landline Number.\n";
		} else {

			var str = landline.value;
			var newphone = str.replace(/(\s|\(|\)|-)/gi, "");
			var patt1 = new RegExp("[0-9]{10}");
			var result = patt1.test(newphone);
			if ( !result ) {
				sError += "This Phone Number is not valid.\n";
				landline.select();
			}
		}
	}

	// Validate phone
	if(phone) {
		if ( phone.value.length == 0 ) {
			sError += "Please enter your Phone Number.\n";
		} else {

			var str = phone.value;
			var newphone = str.replace(/(\s|\(|\)|-)/gi, "");
			var patt1 = new RegExp("[0-9]{10}");
			var result = patt1.test(newphone);
			if ( !result ) {
				sError += "This Phone Number is not valid.\n";
				phone.select();
			}
		}
	}

	// Validate mobile
	if(mobile) {
		if ( mobile.value.length == 0 ) {
			sError += "Please provide your Mobile Number.\n";
		} else {

			var str = mobile.value;
			var newphone = str.replace(/(\s|\(|\)|-)/gi, "");
			var patt1 = new RegExp("[0-9]{10}");
			var result = patt1.test(newphone);
			if ( !result ) {
				sError += "This Mobile Number is not valid.\n";
				mobile.select();
			}
		}
	}

	// Validate State
	if ( state.value.length == 0 ) {
		sError += "Please select your State.\n";
	}

	// Company Name
	if(company_name) {
		// Validate Company Name
		if ( company_name.value.length == 0 ) {
			sError += "Please enter your Company Name.\n";
		}
	}

	// Company Ownership
	if(ownership) {
		// Validate Company Ownership
		if ( $('ownership_y').checked == false && $('ownership_n').checked == false  ) {
			sError += "Please specify Company Ownership.\n";
		}
	}



	// Validate Employment -----------------------------------------------------
	if(employment) {
		if ( employment.value.length == 0 ) {
			sError += "Please select your Employment.\n";
		}

		// Check if Unemployed
		if ( employment.value == 'Unemployed' ) {
			sError = "\nDue to new legislation we cannot provide our services to unemployed people at this stage.\nWe apologise for any inconvenience.\n\n";
		}

		// Check Time in Employment
		if ( employment_time_1.value.length == 0 ) {
			sError += "Please select Time in Current Employment.\n";
		} else if(employment_time_1.value=='0-6 months') {

			// Check Time in Previous Employment
			if ( employment_time_2.value.length == 0 ) {
				sError += "Please select Time in Previous Employment.\n";
			} else if(employment_time_2.value=='0-6 months') {

				// Check Time in Previous Past Employment
				if ( employment_time_3.value.length == 0 ) {
					sError += "Please select Time in Previous Employment.\n";
				} 
			}
		}

		// Evidence of income
		var x=$A(form['income_evidence']).find(function(s) {return s.checked}); 
		if(x) {
			// all good
		} else {
			sError += "Can you provide evidence of regular income?.\n";
		}

	}
	// -------------------------------------------------------------------------

	// Validate Residency -----------------------------------------------------
		if(residential_status) {
			if ( residential_status.value.length == 0 ) {
				sError += "Please select your Residential Status.\n";
			}
		}

		// if Time in Current Residency is set
		if(residency_time_1) {
				// Check Time in Current Residency
				if ( residency_time_1.value.length == 0 ) {
					sError += "Please select Time in Current Residency.\n";
				} else if(residency_time_1.value=='0-6 months') {

					// Check Time in Previous Residency
					if ( residency_time_2.value.length == 0 ) {
						sError += "Please select Time in Previous Residency.\n";
					} else if(residency_time_2.value=='0-6 months') {

						// Check Time in Past Previous Residency
						if ( residency_time_3.value.length == 0 ) {
							sError += "Please select Time in Previous Residency.\n";
						} 
					}
				}
		}

	// -------------------------------------------------------------------------


	// Validate Amount
	if ( amount.value.length == 0 ) {
		sError += "Please enter the Loan Amount.\n";
	} else {

		var str = amount.value;
		str = str.replace(/(\.0+$)/gi, "");
		str = str.replace(/([^0-9])/gi, "");
		if ( str<10000 ) {
			sError += "Sorry, we do not provide loans under $10,000.\n";
			amount.select();
		}
	}

	if(balloon) {
		// Validate Balloon
		if ( balloon.value.length == 0 ) {
			sError += "Please enter the Balloon Amount.\n";
		} else if(balloon.value.length > 2) {
			sError += "Please enter the Balloon Amount as a percentage.\n";
		}
	}

		// Primary car use
		var x=$A(form['car_use']).find(function(s) {return s.checked}); 
		if(x) {
			// all good
		} else {
			sError += "What is the primary intended use of the car?.\n";
		}

	// -------------------------------------------------------------------------

	// Validate Credit History
	if ( credit_history.value.length == 0 ) {
		sError += "Please select your Credit History.\n";
	} else {
		if ( credit_history.value == 'Some problems') {
			
			var x=$A(form['credit_problem']);
			var none_checked=true;
			x.each(function(n) { 
				if(n.checked) {
					if(
						n.value=="Part 9 - Discharged for less than 12 months" || 
						n.value=="Bankrupt - Discharged for less than 12 months" || 
						n.value=="Bankrupt - current"
					){
						sError = "\nYour Application Has Been Declined.\n\nSorry, we can not assist you at this time.\n";
					}
					none_checked=false;
				}
			});
			if(none_checked) {
				sError += "Please select details of your Credit History.\n";
			}

			//sError = "\nYour Application Has Been Declined.\n\nSorry, we can not assist you at this time.\n";
		}
	}

	// Loan Type
	if(loan_type) {
		// Validate Company Ownership
		if ( ($('loan_type_1').checked == false) && ($('loan_type_2').checked == false) && ($('loan_type_3').checked == false) && ($('loan_type_4').checked == false) ) {
			sError += "Please select Loan Type.\n";
		}
	}


	if ( sError.length == 0 ) {
		return true;
	}
	else {
		alert( sError );
		return false;
	}
}

// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------
// Validate Contact Form
function validate_contact(form) {
	var name = $(form['name']);
	var email =$(form['email']);
	var phone = $(form['phone']);

	var sError = "";

	// Validate Name
	if ( name.value.length == 0 ) {
		sError += "Please enter your Name.\n";
	}

	// Validate Email
	if ( email.value.length == 0 ) {
		sError += "Please enter your email address.\n";
	} else {

		var str = email.value;
		var patt1 = new RegExp("^[a-z][a-zA-Z0-9_.-]*@[a-zA-Z0-9_.-]+\.[a-z]{2,4}$");
		var result = patt1.test(str);
		if ( !result ) {
			sError += "The email is not valid.\n";
		}
	}

	// Validate Phone
	if ( phone.value.length == 0 ) {
		sError += "Please enter your contact phone number.\n";
	}

	if ( sError.length == 0 ) {
		return true;
	}
	else {
		alert( sError );
		return false;
	}

}

// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------


// on page load
Event.observe(window, 'load', function() {

	// Balloon Info
	if($('balloon_info')) {

		$('balloon_info').setStyle({
			'float': 'right',
			width: '19px',
			height: '18px',
			background: "transparent url('images/qm.gif') no-repeat 100% 50%",
			padding: 0,
			cursor: 'pointer'
		});

		$('balloon_info').observe('click', function(event) {
			Event.stop(event);
			alert("\n\
Balloon (Residual) represents the approximate value of the car at the end of the loan term.\n\
\n\
Maximum Residual Car Value for Cars Guidelines:\n\
 - 12 - 36 months up to 40%\n\
 - 37 - 48 months up to 35%\n\
 - 49 - 60 months up to 25%\n\
			"); 
		});
	}


	// Expanding Sub Sections
	$$('div.expand').each(function(x) {
		x.setStyle({height:'1px', overflow:'hidden'});

		n=x.previous('h2');
		n.setStyle({
			background:'transparent url(images/plus.gif) no-repeat 0 50%',
			padding:'0 0 0 26px',
			cursor:'pointer'
		});

		n.observe('click', function() {

			if(x.getHeight()==1){
				$$('div.expand').each(function(zz) {
					zz.setStyle({height:'1px'});
					zz.previous('h2').style.color='#D92E00';
					zz.previous('h2').style.background='transparent url(images/plus.gif) no-repeat 0 50%';
				});

				x.setStyle({height:'auto'});
				this.style.color='#AA0000';
				this.style.background='transparent url(images/minus.gif) no-repeat 0 50%';
			} else {
				x.setStyle({height:'1px'});
				this.style.color='#D92E00';
				this.style.background='transparent url(images/plus.gif) no-repeat 0 50%';
			}

		});

	});
/*
	if($('qq')) {
		$('qq').onclick = function(){
			if((!$('ajax_form').hasClassName('qq')) || ($('ajax_form').style.display='none')) {
				// update form
				new Ajax.Updater('ajax_form', 'ajax_get.php', {  
					parameters: { m: 'qq' }, 
					onComplete: function() { 
						$('ajax_form').className = 'qq';
						$('qq_form').onsubmit = function() {
							if(validate_qq()) { 
								//new Ajax.Request('form_req.php?m=qq', { parameters: $('qq_form').serialize(true) }); 
								return true; 
							} else { 
								return false; 
							}
						}
						$('ajax_form').show();
					} 
				});
			} else { 
				$('ajax_form').hide();
			} 
			return false;
		}
	}

*/


});


