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 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) {
	var intSelected = theAddress.options[theAddress.selectedIndex].value;
	if (intSelected == -1) {
		$(type + '_address1').value = "";
		$(type + '_address2').value = "";
		$(type + '_town').value = "";
		$(type + '_county').value = "";
		$(type + '_postcode').value = "";
		$(type + '_container').show();
	} else {
		$(type + '_container').hide();
		for (var i = 0; i < arrClients.length; i++) {
			splitArray = arrClients[i].split("|");
			if (splitArray[0] != "" && splitArray[1] != "") {
				if (intSelected == 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 setClientAddress(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 checkLender(theLender, theCheck) {
	var i = 0;
	if (theCheck.checked) {
		$("sal_lender" + theLender + "_container").show();
	} else {
		for (i = theLender; i < 5; i++) {
			if ($("sal" + i + "_loans")) $("sal" + i + "_loans").checked = false;
			$("sal_lender" + i + "_container").hide();
		}
	}
}

function checkAgent(theAgent, theRadio) {
	if (theRadio.value == 0) {
		$(theAgent + "_container").hide();
		$(theAgent + "_name").value = arrAgents[1];
		if (arrAgents[2] != "") $(theAgent + "_address1").value = arrAgents[2] + ", "
		if (arrAgents[3] != "") $(theAgent + "_address1").value += arrAgents[3] + " "
		$(theAgent + "_address1").value += arrAgents[4];
		$(theAgent + "_address2").value = arrAgents[5];
		$(theAgent + "_town").value = arrAgents[6];
		$(theAgent + "_county").value = arrAgents[7];
		$(theAgent + "_postcode").value = arrAgents[8];
		$(theAgent + "_contact_name").value = arrAgents[0];
		$(theAgent + "_telephone").value = arrAgents[10];
		$(theAgent + "_mobile_number").value = arrAgents[11];
		$(theAgent + "_email_address").value = arrAgents[9];
	} else {
		$(theAgent + "_container").show();
		$(theAgent + "_name").value = "";
		$(theAgent + "_address1").value = "";
		$(theAgent + "_address2").value = "";
		$(theAgent + "_town").value = "";
		$(theAgent + "_county").value = "";
		$(theAgent + "_postcode").value = "";
		$(theAgent + "_contact_name").value = "";
		$(theAgent + "_telephone").value = "";
		$(theAgent + "_mobile_number").value = "";
		$(theAgent + "_email_address").value = "";
	}
}

function checkReport(theReport, theRadio) {
	if (theRadio.value == 0) {
		$(theReport + "_container").hide();
	} else {
		$(theReport + "_container").show();
	}
}

function checkLogin(theForm) {
	if (theForm.login_password.value.length == 6) {
    //if (/^[A-Za-z]/.test(theForm.login_email_address.value)) {
		$("solcase-b").value = theForm.login_email_address.value;
		$("solcase-c").value = theForm.login_password.value;
		$("ycoform").submit();
        return false; // its numeric meaning that it must be TD(?)
    }
}

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 updateQuote(theTransaction) {
	switch (theTransaction) {
	case "1":
		// Sale and Purchase
		if ($('sale_holder')) {
			$('sale_holder').show();
			$('purchase_holder').show();
			$('remortgage_holder').hide();
		} else {
			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
		if ($('sale_holder')) {
			$('sale_holder').hide();
			$('purchase_holder').show();
			$('remortgage_holder').hide();
		} else {
			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
		if ($('sale_holder')) {
			$('sale_holder').show();
			$('purchase_holder').hide();
			$('remortgage_holder').hide();
		} else {
			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
		if ($('sale_holder')) {
			$('sale_holder').hide();
			$('purchase_holder').hide();
			$('remortgage_holder').show();
		} else {
			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:
		if ($('sale_holder')) {
			$('sale_holder').hide();
			$('purchase_holder').hide();
			$('remortgage_holder').hide();
		} else {
			quote.reset();
			$('sale_price').disable();
			$('sale_price').className = " normal";
			$('purchase_price').disable();
			$('purchase_price').className = " normal";
			$('mortgage_price').disable();
			$('mortgage_price').className = " normal";
		}
	}
}

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 GOverviewMapControl());
		gMap.addControl(new GMapTypeControl());
		gMap.setCenter(gDunf, 15);
		var gMarker = new GMarker(gDunf)
		gMap.addOverlay(gMarker);
		//gMarker = new GMarker(gGlas)
		//gMap.addOverlay(gMarker);
	}
}

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));
		ev.stop();
	},
	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;
  }
}
