// wForms - a javascript extension to web forms.
// Copyright (c) 2007  Veer West LLC http://www.veerwest.com

// document.addEventListener('DOMContentLoaded',enableResumeLater,false);
base2.DOM.Element.addEventListener(document, 'DOMContentLoaded',enableResumeLater,false);

function enableResumeLater() {
	var b = document.getElementById('tfa_resumeLater');
	if(b) {
		b.onclick = function(e) { 
			this.value=" ... "; 
			var f = this.form;
			var b = wFORMS.getBehaviorInstance(f,"validation");
			if(b) { b.run = function(){return true;}} // bypass validation
			f.submit();
		};

		var l = document.getElementById('tfa_saveForLaterLink');
		if(l) {	
			l.onclick = function() {
				
				// Get form element, necessary to retrieve instance.
				f = this;
				while(f && f.tagName!='FORM') {
					f = f.parentNode;
				}
				
				elem = document.getElementById('tfa_saveForLater');
				if(!elem.checked) {
					elem.checked = true;
				}
				if(elem.scrollIntoView) {	
					elem.scrollIntoView();
				} else {
					location.hash="#tfa_saveForLater";
				}
				
				var b = wFORMS.getBehaviorInstance(f,"switch");
				b.run(null, elem);
			}
		}
	}
}


wFORMS.behaviors.validation.onFail = function(bInstance) {
	var m = wFORMS.behaviors.validation.messages.notification;
	var firstErrorId = null;
	var c = 0;
	for (var id in bInstance.elementsInError) {
		c++;
		if(!firstErrorId) 
			firstErrorId = id;
	}
	m = m.replace('%%', c);
	
	var elem = document.getElementById(firstErrorId);
	if(elem.scrollIntoView) {	
		elem.scrollIntoView();
	} else {
		location.hash="#"+firstErrorId;
	}
	alert(m);
}


// 

wFORMS.behaviors.validation.rules.isUKPostCode = { selector: ".validate-ukpostcode", check: 'validateUKPostCode' };

wFORMS.behaviors.validation.messages.isUKPostCode = "Not a valid Australian Post Code. It must contain 4 numbers.";

wFORMS.behaviors.validation.instance.prototype.validateUKPostCode = function(element, value) {


// was 1 - 9
var regexp = /^[0-9]\d{3}$/



return this.isEmpty(value) || regexp.test(value);
}

// 

wFORMS.behaviors.validation.rules.isAusPhone = { selector: ".validate-AusPhone", check: 'validateAusPhone' };

wFORMS.behaviors.validation.messages.isAusPhone = "Not a valid Australian Phone number.";

wFORMS.behaviors.validation.instance.prototype.validateAusPhone = function(element, value) {


var regexp =  /^\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3}$/


// var regexp = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/






return this.isEmpty(value) || regexp.test(value);



}


// make the first field in the form have focus
function setFocus()
{
	document.leasing.tfa_Fullnameofowners.focus();
}


// allow numbers only 

  function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
