
// general functions

function $(id) { 
return document.getElementById(id) 
}

function switchVisibility(target, mode) {
	if (mode == 'show') {
	target.style.display = 'block';
	}
	else {
	target.style.display = 'none';
	}
}

function checkDropDown(target)
{
	selecteditem = target.options[target.selectedIndex].value;
	if (selecteditem == 'nodata') {
		return false;
	}
	else {
		return true;
	}
}

var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1)
var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);

// labels
var amount_l,purpose_l,homeowner_l;

// reminder div
var reminder;

// checking variables
var sAmount,sPurpose,sHomeowner;

function initialize () {
	if (self.init) self.init();
}

var W3CDOM = (document.createElement && document.getElementsByTagName);
function init()
{
	if (!W3CDOM) return;
	
	// add a behaviour to all links with a rel=nofollow attribute
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("rel") == "nofollow") {
			// focus behaviour
			links[i].onclick = function() { 
				window.open(this.getAttribute("href"), 'LoanCube', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=350,height=300,left=100,top=100');
				return false;
			};
		}
	}
	
	/* preload tick image so it doesnt jump when selected */
	tick = new Image();
	tick.src = '/i/1x1.gif';

	/* form controls */
	amount = $('amount');	
	purpose = $('purpose');		
	homeowner = $('homeowner');
	reminder = $('reminder');

	/* form labels */
	amount_l = $('amount_l');
	purpose_l = $('purpose_l');	
	homeowner_l = $('homeowner_l');

	/* set initial state */ 
	checkAmount();
	checkPurpose();	
	checkHomeowner();

	/* define behaviours */
	amount.onchange = checkAmount;
	purpose.onchange = checkPurpose;	
	homeowner.onchange = checkHomeowner;
	document.forms[0].onsubmit = checkAll;
	
}

function switchBackground(target, mode) {
	if (mode == 'tick') {
	target.style.background = 'url(/i/ok.gif) no-repeat 0 18px';
	}
	else {
	target.style.background = 'url(/i/arrow2.gif) no-repeat 0 18px';
	}
}

// specific functions

function checkAll() {
	
	formvalid = true;
	
	// mandatory fields
	if (sAmount==false || sPurpose==false || sHomeowner==false) {
	formvalid = false;
	}
	
	if (formvalid == true) {
		switchVisibility(reminder, 'hide');
		return true;
	}
	else {
		switchVisibility(reminder, 'show');
		return false;
	}
	
}

function checkAmount() {
	if (checkDropDown(amount)) {
		switchBackground(amount_l, 'tick');
		sAmount = true;
	}
	else {
		switchBackground(amount_l, 'cross');
		sAmount = false;
	}	
}

function checkPurpose() {
	if (checkDropDown(purpose)) {
		switchBackground(purpose_l, 'tick');
		sPurpose = true;
	}
	else {
		switchBackground(purpose_l, 'cross');
		sPurpose = false;
	}	
}

function checkHomeowner() {
	if (checkDropDown(homeowner)) {
		switchBackground(homeowner_l, 'tick');
		sHomeowner = true;
	}
	else {
		switchBackground(homeowner_l, 'cross');
		sHomeowner = false;
	}	
}


window.onload = initialize;

