// JavaScript Document
// July 15, 2011 - 16:21 (Application form validation JS)

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function submitApplicationForm() {
	var theform = document.getElementById("appform"),
        SELECT = '',
        OTHER = 'other';

	if (!theform.title.value) {
		alert("Please enter the Loan Amount Required!");
		theform.title.select();
		return false;
	}

    // An item is selected, if other is selected the other box has a value
	var oneChecked = false;
    oneChecked = (theform["shortdescription[]"][0].value.toLowerCase() != SELECT) &&  
                 (theform["shortdescription[]"][0].value.toLowerCase() != OTHER || theform["shortdescription[]"][1].value != '') ?
                 true : false;

	if (!oneChecked) {
		alert("Please select the Purpose of your Loan!");
		theform.title.select();
		return false;
	}

    if ((theform["content[]"].value.toLowerCase() == SELECT) || (theform["author"].value == '')) {
		alert("Please select the loan security and detail.");
		return false;
    } 

	if (!theform.organisationalname.value) {
		alert("Please enter the Main Borrower's Title!");
		return false;
	}
	if (!theform.authorbio.value) {
		alert("Please enter the Main Borrower's First Names!");
		theform.authorbio.select();
		return false;
	}
	if (!theform.address.value) {
		alert("Please enter the Main Borrower's Surname!");
		theform.address.select();
		return false;
	}
	if (!theform.phone.value) {
		alert("Please enter the Main Borrower's Date of Birth!");
		theform.phone.select();
		return false;
	}
	if (!theform.fax.value) {
		alert("Please enter the Main Borrower's Home Phone Number!");
		theform.fax.select();
		return false;
	}
	if (!theform.value.value) {
		alert("Please enter the Main Borrower's Email Address!");
		theform.value.select();
		return false;
	}
	if (theform.inhouse.checked) {
		if (!theform.restricted.value) {
			alert("Please enter the Co Borrower's Title!");
			return false;
		}
		if (!theform.headline.value) {
			alert("Please enter the Co Borrower's First Names!");
			theform.headline.select();
			return false;
		}
		if (!theform.extra1.value) {
			alert("Please enter the Co Borrower's Surname!");
			theform.extra1.select();
			return false;
		}
		if (!theform.extra2.value) {
			alert("Please enter the Co Borrower's Date of Birth!");
			theform.extra2.select();
			return false;
		}
		if (!theform.extra3.value) {
			alert("Please enter the Co Borrower's Home Phone Number!");
			theform.extra3.select();
			return false;
		}
		if (!theform.extra6.value) {
			alert("Please enter the Co Borrower's Email Address!");
			theform.extra6.select();
			return false;
		}
	}
	if (!theform.extra7.value) {
		alert("Please enter the Best time and phone number to contact you on!");
		theform.extra7.select();
		return false;
	}
	var oneChecked = false;
	for (var i = 0; i < theform.extra8.length; i++) {
		if (theform.extra8[i].checked) {
			oneChecked = true;
		}
	}
	if (!oneChecked) {
		alert("Please select whether you are a NZ Resident!");
		return false;
	}
	if (!theform.extra8[0].checked) { //if not nz resident
		if (!theform.inhouse.checked) {
			alert("Because you are not an NZ Resident, you must enter a Co Borrower who is!");
			return false;
		}
	}
	if (!theform.extra9.value) {
		alert("Please enter your Current Address!");
		theform.extra9.select();
		return false;
	}


	var oneChecked = false;
    oneChecked = (theform["extra10"].value.toLowerCase() != SELECT)
	if (!oneChecked) {
		alert("Please select your Current Address Status!");
		return false;
	}
	if (!theform.extra11.value) {
		alert("Please enter your Current Occupation!");
		theform.extra11.select();
		return false;
	}
	if (!theform.extra13.value) {
		alert("Please enter your Average Net Income!");
		theform.extra13.select();
		return false;
	}
	var oneChecked = false;
	for (var i = 0; i < theform.extra14.length; i++) {
		if (theform.extra14[i].checked) {
			oneChecked = true;
		}
	}
	if (!oneChecked) {
		alert("Please enter your Income Frequency!");
		theform.extra13.select();
		return false;
	}
	if (!theform.extra16.value) {
		alert("Please select where you heard about us from!");
		return false;
	}
	
/*
	 if (!theform.extra17.checked) {
		 alert("Please tick to acknowledge Question 1!");
		 return false;
	 }
	 if (!theform.extra19.checked) {
		alert("Please tick to acknowledge Question 2!");
		return false;
	 }
	if (!theform.extra21.checked) {
		alert("Please tick to acknowledge Question 3!");
		return false;
	}
	if (!theform.extra23.checked) {
		alert("Please tick to acknowledge Question 4!");
		return false;
	}
	if (!theform.extra25.checked) {
		alert("Please tick to acknowledge Question 5!");
		return false;
	}
	if (!theform.extra27.checked) {
		alert("Please tick to acknowledge Question 6!");
		return false;
	}
	if (!theform.extra29[0].checked) {
		alert("Please tick to acknowledge Question 7!");
		return false;
	}
	if (theform.inhouse.checked) {
		if (!theform.extra18.checked) {
			alert("Co Borrower - Please tick to acknowledge Question 1!");
			return false;
		}
		if (!theform.extra20.checked) {
			alert("Co Borrower - Please tick to acknowledge Question 2!");
			return false;
		}
		if (!theform.extra22.checked) {
			alert("Co Borrower - Please tick to acknowledge Question 3!");
			return false;
		}
		if (!theform.extra24.checked) {
			alert("Co Borrower - Please tick to acknowledge Question 4!");
			return false;
		}
		if (!theform.extra26.checked) {
			alert("Co Borrower - Please tick to acknowledge Question 5!");
			return false;
		}
		if (!theform.extra28.checked) {
			alert("Co Borrower - Please tick to acknowledge Question 6!");
			return false;
		}
		if (!theform.extra30[0].checked) {
			alert("Co Borrower - Please tick to acknowledge Question 7!");
			return false;
		}
	}
*/	
	if (!theform.extra31.checked) {
		alert("Please read and accept our Privacy Statement!");
		return false;
	}
	
	theform.submit();
}

