function formFocus(theForm, theCurrent, theDesired) {
	if (theForm.value == theCurrent) {
		theForm.value = theDesired;
	}
}

function formColour(theElement) {
	var theClass = theElement.className
	if (theClass.indexOf("normal") > -1) {
		theClass = theClass.replace("normal", "active");
	} else if (theClass.indexOf("active") > -1) {
		theClass = theClass.replace("active", "normal");
	}
	theElement.className = theClass;
}

function showPasswordField(theDummy, thePassword) {
	$(theDummy).className = "normal hide";
	$(thePassword).className = "required normal show";
	$(thePassword).focus();
}

function hidePasswordField(theDummy, thePassword) {
	if ($(thePassword).value == "") {
		$(theDummy).className = "normal show";
		$(thePassword).className = "required normal hide";
	}
}

function updateQuote(theTransaction) {
	switch (theTransaction) {
	case "1":
		// Sale and Purchase
		quote.reset();
		$('sale_price').enable();
		$('sale_price').className = "required validate-number normal";
		$('purchase_price').enable();
		$('purchase_price').className = "required validate-number normal";
		$('mortgage_price').disable();
		$('mortgage_price').className = " normal";
		break;
	case "2":
		// Purchase Only
		quote.reset();
		$('sale_price').disable();
		$('sale_price').className = " normal";
		$('purchase_price').enable();
		$('purchase_price').className = "required validate-number normal";
		$('mortgage_price').disable();
		$('mortgage_price').className = " normal";
		break;
	case "3":
		// Sale Only
		quote.reset();
		$('sale_price').enable();
		$('sale_price').className = "required validate-number normal";
		$('purchase_price').disable();
		$('purchase_price').className = " normal";
		$('mortgage_price').disable();
		$('mortgage_price').className = " normal";
		break;
	case "4":
		// Re-mortgage Only
		quote.reset();
		$('sale_price').disable();
		$('sale_price').className = " normal";
		$('purchase_price').disable();
		$('purchase_price').className = "normal";
		$('mortgage_price').enable();
		$('mortgage_price').className = " required validate-number normal";
		break;
	default:
		quote.reset();
		$('sale_price').disable();
		$('sale_price').className = " normal";
		$('purchase_price').disable();
		$('purchase_price').className = " normal";
		$('mortgage_price').disable();
		$('mortgage_price').className = " normal";
	}
}

function copyAddress(theBox) {
	if (theBox.checked) {
		$("client2_address1").value = $F("client1_address1");
		$("client2_address2").value = $F("client1_address2");
		$("client2_town").value = $F("client1_town");
		$("client2_county").value = $F("client1_county");
		$("client2_postcode").value = $F("client1_postcode");
		for (var i = 0; i < $('client1_country').options.length; i++) {
			if ($('client1_country').options[i].selected) $('client2_country').options[i].selected = true;
		}
	} else {
		$("client2_address1").value = "";
		$("client2_address2").value = "";
		$("client2_town").value = "";
		$("client2_county").value = "";
		$("client2_postcode").value = "";
	}
}

function setAddress(theAddress, type) {
	for (var i = 0; i < arrClients.length; i++) {
		splitArray = arrClients[i].split("|");
		if (splitArray[0] != "" && splitArray[1] != "") {
			if (theAddress.options[theAddress.selectedIndex].value == i) {
				$(type + '_address1').value = splitArray[2];
				$(type + '_address2').value = splitArray[3];
				$(type + '_town').value = splitArray[4];
				$(type + '_county').value = splitArray[5];
				$(type + '_postcode').value = splitArray[6];
			}
		}
	}
}

function showElement(theLink) {
	var theDD = $$("dd");
	for (var i = 0; i < theDD.length; i++) {
		if (theDD[i].innerHTML == theLink.parentElement.innerHTML) {
			theDD[i+1].toggle();
		}
	}
}

function centre(size,area) {
	return (area/2)-(size/2);
}

function addFavorite(title, url) {
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print) { // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

var gMap, gDunf, gGlas
function googleload() {
	if (GBrowserIsCompatible()) {
		gDunf = new GLatLng(56.071335, -3.46109);
		gGlas = new GLatLng(55.875407, -4.251559);
		gMap = new GMap2(document.getElementById("map"));
		gMap.enableContinuousZoom();
		gMap.addControl(new GLargeMapControl());
		gMap.addControl(new GMapTypeControl());
		gMap.setCenter(gDunf, 14);
		var gMarker = new GMarker(gDunf)
		gMap.addOverlay(gMarker);
		gMarker = new GMarker(gGlas)
		gMap.addOverlay(gMarker);
	}
}

function centerMap(theLocation) {
	if (theLocation == 1) {
		gMap.setCenter(gDunf, 14);
	}

	if (theLocation == 2) {
		gMap.setCenter(gGlas, 14);
	}
}

var showHide = Class.create();
showHide.prototype = {
	initialize : function() {
		var loc = document.location.href.match(/#(\w+)/) ? RegExp.$1 : "kyle";
		this.links = $$("div.links a");
		this.people = $$("div.people-holder");
		this.people.without(loc).each(this.hide.bind(this));
		this.show(loc);
		this.links.each(this.setup.bind(this));
	},
	setup : function(elm) {
		Event.observe(elm, 'click', this.activate.bindAsEventListener(this), false)
	},
	activate : function(ev) {
		var elm = Event.findElement(ev, "a");
		this.people.without(elm).each(this.hide.bind(this));
		this.show(this.hash(elm));
	},
	hash : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	hide : function(elm) {
		$(elm).hide();
	},
	show : function(elm) {
		$(elm).show();
	}
}

var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    this.options.top!="" ? openoptions+=",top="+this.options.top : openoptions+=",top="+centre(this.options.height, screen.availHeight);
    this.options.left!="" ? openoptions+=",left="+this.options.left : openoptions+=",left="+centre(this.options.width, screen.availWidth);
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}

