if ((top.OneRateUIServiceInit == "undefined") || (top.OneRateUIServiceInit != true))
{
	/*************************************************************************
	 *
	 * Home Buyers UI Service CLASS definition.
	 *
	 */
	function OneRateUIService(stateCode){
		this.service = new OneRateService(stateCode);
		this.service.setUIService(this);
		
		DynamicFormActionManager.register(this);
		ButtonActionManager.register(this);
		
		this.county = "orange";
		
		this.PurchasePrice = "0";
		this.FirstLoanAmount = "0";
		this.SecondLoanAmount = "0";
		
		this.firstLoanChecked = false;
		this.secondLoanChecked = false;
	}

	/*************************************************************************
	 *
	 * Global initializer
	 *
	 */
	function initializeComponents(stateCode){	
	    OneRateUIService.initializeClient(document, stateCode);
	}

	OneRateUIService.prototype.doFormAction = function(attribute, action, value){
		if(action == "datachange"){
			if(attribute == "County"){
				this.county = value;
				this.service.setCounty(value);
				this.setQuoteCounty(value);
			}
			else if(attribute == "PurchasePrice"){
				this.PurchasePrice = value;
				this.service.setPurchasePrice(value);
				this.setQuotePurchasePrice(value);
			}
			else if(attribute == "IncludeEscrow"){
				this.service.setIncludeEscrow(value);
				this.setQuoteEscrowText(value);
			}
			else if(attribute == "FirstLoanAmount"){
				this.FirstLoanAmount = value;
				this.setQuoteFirstLoanAmount(value);
			}
			else if(attribute == "SecondLoanAmount"){
				this.SecondLoanAmount = value;
				this.setQuoteSecondLoanAmount(value);
			}
			
		}else if(action == "itemChecked"){
			if(attribute == "IncludeFirstLoan"){
				this.firstLoanChecked = !this.firstLoanChecked;
				this.service.setFirstLoanIncluded(this.firstLoanChecked);
				if(this.firstLoanChecked)
				{
					top.eventService.publish('FormElement','enablefield','FirstLoanAmount','');
					top.eventService.publish('FormElement','enablefield','IncludeSecondLoan','');
					if(this.secondLoanChecked){
						top.eventService.publish('FormElement','enablefield','SecondLoanAmount','');
					}
				} else{
					top.eventService.publish('FormElement','disablefield','FirstLoanAmount','');
					top.eventService.publish('FormElement','disablefield','IncludeSecondLoan','');
					top.eventService.publish('FormElement','disablefield','SecondLoanAmount','');
				}
			} else if(attribute == "IncludeSecondLoan"){
				this.secondLoanChecked = !this.secondLoanChecked;
				this.service.setSecondLoanIncluded(this.secondLoanChecked);
				if(this.secondLoanChecked){
					top.eventService.publish('FormElement','enablefield','SecondLoanAmount','');
				} else{
					top.eventService.publish('FormElement','disablefield','SecondLoanAmount','');
				}
			} 
		}
	}
	
	OneRateUIService.prototype.doButtonAction = function(id){
		if(id=="backButton"){
			this.clearForm();
			this.showMainForm();
		} else if(id=="getQuoteButton"){
			if (this.isFormValid()) {
				this.service.doGetQuote();
			}
		}
	}

	OneRateUIService.prototype.setQuotePurchasePrice = function(data){
		top.eventService.publish('Label', 'setText', 'providedPurchasePrice', top.Formatter.currencyFormat(data));		
	}	

	OneRateUIService.prototype.setQuoteFirstLoanAmount = function(data){
		top.eventService.publish('Label', 'setText', 'providedFirstLoanAmount', top.Formatter.currencyFormat(data));		
	}
	
	OneRateUIService.prototype.setQuoteSecondLoanAmount = function(data){
		top.eventService.publish('Label', 'setText', 'providedSecondLoanAmount', top.Formatter.currencyFormat(data));		
	}	

	OneRateUIService.prototype.setQuoteCounty = function(data){
		top.eventService.publish('Label', 'setText', 'loanCounty', data);		
	}	

	OneRateUIService.prototype.setLoanAmount = function(){
		var loan = 0;
		if(this.firstLoanChecked){
			loan = top.Formatter.toNumber(this.FirstLoanAmount);
			if(this.secondLoanChecked){
				loan = loan + top.Formatter.toNumber(this.SecondLoanAmount);
			}
		}
		this.service.setLoanAmount(loan);
	}

	OneRateUIService.prototype.setQuoteEscrowText = function(data){
		if(data=="true"){
			top.eventService.publish('Label', 'setText', 'transactionType', 'and escrow services');
			top.eventService.publish('Label', 'setText', 'escrowItems', 'and Loan Escrow Services (including all customary escrow services and the following specific items):');
					
			top.eventService.publish('View', 'setVisibility', 'withEscrowMisc', true);
			top.eventService.publish('View', 'setVisibility', 'withoutEscrowMisc', false);

			top.eventService.publish('View', 'setVisibility', 'allocationBuyerSellerEscrow', true);

			top.eventService.publish('Label', 'setText', 'miscResults', 'Escrow Related Services');
		} else {
			top.eventService.publish('Label', 'setText', 'transactionType', 'without First American escrow services');
			top.eventService.publish('Label', 'setText', 'escrowItems', ':');
					
			top.eventService.publish('View', 'setVisibility', 'withEscrowMisc', false);
			top.eventService.publish('View', 'setVisibility', 'withoutEscrowMisc', true);

			top.eventService.publish('View', 'setVisibility', 'allocationBuyerSellerEscrow', false);

			top.eventService.publish('Label', 'setText', 'miscResults', 'Sub-Escrow Fees');
		}
	}	

	OneRateUIService.prototype.displayQuote = function(data){
		if(data != "undefined" || data != null){
			var date = data.getDate();
			
			var buyerEagleRate = data.getBuyersRates()[0];
			var buyerSimultaneousRate = data.getBuyersRates()[1];
			var buyerEscrowRate = data.getBuyersRates()[2];
			var buyerMiscFee = data.getBuyersRates()[3];

			var sellerEagleRate = data.getSellersRates()[0];
			var sellerSimultaneousRate = data.getSellersRates()[1];
			var sellerEscrowRate = data.getSellersRates()[2];
			var sellerMiscFee = data.getSellersRates()[3];

			var buyerTotal = top.Formatter.currencyFormat(buyerEagleRate + buyerSimultaneousRate + buyerEscrowRate + buyerMiscFee);
			var sellerTotal = top.Formatter.currencyFormat(sellerEagleRate + sellerSimultaneousRate + sellerEscrowRate + sellerMiscFee);
			
			var rate = top.Formatter.currencyFormat(buyerEagleRate + buyerSimultaneousRate + buyerEscrowRate + buyerMiscFee + 
			                                        sellerEagleRate + sellerSimultaneousRate + sellerEscrowRate + sellerMiscFee);

			top.eventService.publish('Label','setText','loanDate',date);
			
			//top.eventService.publish('Label','setText','loanRate',rate);
			//Break Down rates
			top.eventService.publish('Label','setText','buyerEagleRate', top.Formatter.currencyFormat(buyerEagleRate));
			top.eventService.publish('Label','setText','buyerSimultaneousRate', top.Formatter.currencyFormat(buyerSimultaneousRate));
			top.eventService.publish('Label','setText','buyerEscrowRate', top.Formatter.currencyFormat(buyerEscrowRate));
			top.eventService.publish('Label','setText','buyerMiscFee', top.Formatter.currencyFormat(buyerMiscFee));
			top.eventService.publish('Label','setText','sellerEagleRate', top.Formatter.currencyFormat(sellerEagleRate));
			top.eventService.publish('Label','setText','sellerSimultaneousRate', top.Formatter.currencyFormat(sellerSimultaneousRate));
			top.eventService.publish('Label','setText','sellerEscrowRate', top.Formatter.currencyFormat(sellerEscrowRate));
			top.eventService.publish('Label','setText','sellerMiscFee', top.Formatter.currencyFormat(sellerMiscFee));

			top.eventService.publish('Label','setText','buyerTotal', buyerTotal);
			top.eventService.publish('Label','setText','sellerTotal', sellerTotal);
			
			this.showQuotePage();
		}
	}
	
	OneRateUIService.prototype.showQuotePage = function(){
		if(this.secondLoanChecked){
			top.eventService.publish('Label', 'setVisibility', 'oneLoanHeader', false);
			top.eventService.publish('Label', 'setVisibility', 'twoLoanHeader', true);
			top.eventService.publish('Label', 'setVisibility', 'noLoanHeader', false);

			top.eventService.publish('View', 'setVisibility', 'infoProvidedFirstAmount', true);
			top.eventService.publish('View', 'setVisibility', 'infoProvidedSecondAmount', true);

			top.eventService.publish('Label', 'setVisibility', 'withEscrowFirstLoan', true);
			top.eventService.publish('Label', 'setVisibility', 'withEscrowSecondLoan', true);

			top.eventService.publish('Label', 'setVisibility', 'withoutEscrowFirstLoan', true);
			top.eventService.publish('Label', 'setVisibility', 'withoutEscrowSecondLoan', true);

			top.eventService.publish('Label', 'setVisibility', 'electronicDocuments', true);
			top.eventService.publish('Label', 'setText', 'electronicDocuments', '2 Electronic Document Downloads');
		} else if(this.firstLoanChecked) {
			top.eventService.publish('Label', 'setVisibility', 'oneLoanHeader', true);
			top.eventService.publish('Label', 'setVisibility', 'twoLoanHeader', false);
			top.eventService.publish('Label', 'setVisibility', 'noLoanHeader', false);

			top.eventService.publish('View', 'setVisibility', 'infoProvidedFirstAmount', true);
			top.eventService.publish('View', 'setVisibility', 'infoProvidedSecondAmount', false);

			top.eventService.publish('Label', 'setVisibility', 'withEscrowFirstLoan', true);
			top.eventService.publish('Label', 'setVisibility', 'withEscrowSecondLoan', false);

			top.eventService.publish('Label', 'setVisibility', 'withoutEscrowFirstLoan', true);
			top.eventService.publish('Label', 'setVisibility', 'withoutEscrowSecondLoan', false);

			top.eventService.publish('Label', 'setVisibility', 'electronicDocuments', true);
			top.eventService.publish('Label', 'setText', 'electronicDocuments', '1 Electronic Document Download');
		} else {
			top.eventService.publish('Label', 'setVisibility', 'oneLoanHeader', false);
			top.eventService.publish('Label', 'setVisibility', 'twoLoanHeader', false);
			top.eventService.publish('Label', 'setVisibility', 'noLoanHeader', true);

			top.eventService.publish('View', 'setVisibility', 'infoProvidedFirstAmount', false);
			top.eventService.publish('View', 'setVisibility', 'infoProvidedSecondAmount', false);

			top.eventService.publish('Label', 'setVisibility', 'withEscrowFirstLoan', false);
			top.eventService.publish('Label', 'setVisibility', 'withEscrowSecondLoan', false);

			top.eventService.publish('Label', 'setVisibility', 'withoutEscrowFirstLoan', false);
			top.eventService.publish('Label', 'setVisibility', 'withoutEscrowSecondLoan', false);

			top.eventService.publish('Label', 'setVisibility', 'electronicDocuments', false);
		}

		top.eventService.publish('View', 'setVisibility', 'content', false);		
		top.eventService.publish('View', 'setVisibility', 'quote', true);
	}
	
	OneRateUIService.prototype.showMainForm = function(){
		top.eventService.publish('View', 'setVisibility', 'quote', false);		
		top.eventService.publish('View', 'setVisibility', 'content', true);		
	}
	
	OneRateUIService.prototype.isFormValid = function(){
		var validFlag = true;
		
		
		
		if (validFlag && Component.getDocument().getElementById("County") != null && this.county.length < 1) {
			alert("Please select a county.");
			validFlag = false;
		}
		
		if (validFlag && top.Formatter.toNumber(this.PurchasePrice) < top.OneRateConstants.MIN_AMOUNT) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions which are under " + top.Formatter.currencyFormat(top.OneRateConstants.MIN_AMOUNT).substring(0, 7) + " or over " + top.Formatter.currencyFormat(top.OneRateConstants.MAX_AMOUNT).substring(0, 10) + ".\nPlease contact your local First American Title office to obtain a quote.");
			validFlag = false;
		}
		if (validFlag && top.Formatter.toNumber(this.PurchasePrice) > top.OneRateConstants.MAX_AMOUNT) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions which are under " + top.Formatter.currencyFormat(top.OneRateConstants.MIN_AMOUNT).substring(0, 7) + " or over " + top.Formatter.currencyFormat(top.OneRateConstants.MAX_AMOUNT).substring(0, 10) + ".\nPlease contact your local First American Title office to obtain a quote.");
			validFlag = false;
		}

		if (this.firstLoanChecked) {
			if (validFlag && top.Formatter.toNumber(this.FirstLoanAmount) < top.OneRateConstants.MIN_AMOUNT) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions which are under " + top.Formatter.currencyFormat(top.OneRateConstants.MIN_AMOUNT).substring(0, 7) + " or over " + top.Formatter.currencyFormat(top.OneRateConstants.MAX_AMOUNT).substring(0, 10) + ".\nPlease contact your local First American Title office to obtain a quote.");
				validFlag = false;
			}
			if (validFlag && top.Formatter.toNumber(this.FirstLoanAmount) > top.OneRateConstants.MAX_AMOUNT) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions which are under " + top.Formatter.currencyFormat(top.OneRateConstants.MIN_AMOUNT).substring(0, 7) + " or over " + top.Formatter.currencyFormat(top.OneRateConstants.MAX_AMOUNT).substring(0, 10) + ".\nPlease contact your local First American Title office to obtain a quote.");
				validFlag = false;
			}
			if (validFlag && top.Formatter.toNumber(this.FirstLoanAmount) > top.Formatter.toNumber(this.PurchasePrice)) {
				alert("The Homebuyer’s OneRate Calculator does not quote transactions where loan amount(s) is greater than purchase price.\nPlease contact your local First American Title office to obtain a quote.");
				validFlag = false;
			}
		}
		
		if (this.secondLoanChecked) {
		
			if (validFlag && top.Formatter.toNumber(this.SecondLoanAmount) < top.OneRateConstants.MIN_AMOUNT) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions which are under " + top.Formatter.currencyFormat(top.OneRateConstants.MIN_AMOUNT).substring(0, 7) + " or over " + top.Formatter.currencyFormat(top.OneRateConstants.MAX_AMOUNT).substring(0, 10) + ".\nPlease contact your local First American Title office to obtain a quote.");
				validFlag = false;
			}
			if (validFlag && top.Formatter.toNumber(this.SecondLoanAmount) > top.OneRateConstants.MAX_AMOUNT) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions which are under " + top.Formatter.currencyFormat(top.OneRateConstants.MIN_AMOUNT).substring(0, 7) + " or over " + top.Formatter.currencyFormat(top.OneRateConstants.MAX_AMOUNT).substring(0, 10) + ".\nPlease contact your local First American Title office to obtain a quote.");
				validFlag = false;
			}
			if (validFlag && top.Formatter.toNumber(this.SecondLoanAmount) > top.Formatter.toNumber(this.PurchasePrice)) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions where loan amount(s) is greater than purchase price.\nPlease contact your local First American Title office to obtain a quote.");
				validFlag = false;
			}
		}

		if (this.firstLoanChecked && this.secondLoanChecked) {
			if (validFlag && top.Formatter.toNumber(this.FirstLoanAmount) + top.Formatter.toNumber(this.SecondLoanAmount) > top.Formatter.toNumber(this.PurchasePrice)) {
				alert("The Homebuyer’s OneRate® Calculator does not quote transactions where loan amount(s) is greater than purchase price.\nPlease contact your local First American Title office to obtain a quote.");
				validFlag = false;
			}
		}
		return validFlag;
	}

	OneRateUIService.prototype.clearForm = function(){
		top.eventService.publish('FormElement','datachange',"County", '');

		top.eventService.publish('FormElement','checkfield',"escrow",'true');
		top.eventService.publish('FormElement','datachange',"PurchasePrice",'');

		top.eventService.publish('FormElement','uncheckfield',"IncludeFirstLoan",'');
		top.eventService.publish('FormElement','enablefield',"IncludeFirstLoan",'');
		top.eventService.publish('FormElement','datachange',"FirstLoanAmount",'');
		top.eventService.publish('FormElement','disablefield','FirstLoanAmount','');

		top.eventService.publish('FormElement','uncheckfield','IncludeSecondLoan','');
		top.eventService.publish('FormElement','disablefield','IncludeSecondLoan','');
		top.eventService.publish('FormElement','datachange','SecondLoanAmount','');
		top.eventService.publish('FormElement','disablefield','SecondLoanAmount','');
		
		this.county = "";
		
		this.PurchasePrice = "0";
		this.FirstLoanAmount = "0";
		this.SecondLoanAmount = "0";
		
		this.firstLoanChecked = false;
		this.secondLoanChecked = false;

		this.setQuoteEscrowText("true");

		this.service.setIncludeEscrow("true");
		this.service.setLoanAmount(0);
		this.service.setFirstLoanIncluded(false);
		this.service.setSecondLoanIncluded(false);
		
		top.eventService.publish('View', 'setVisibility', 'quote', false);		
	}
	
	OneRateUIService.initializeClient = function(docObj, stateCode){
		top.channelService.setDOMDoc(docObj);
		top.OneRateUIService = new OneRateUIService(stateCode);
		top.OneRateUIService.clearForm();
	}
	
	//////////////////////////////////////
	
	top.OneRateUIServiceInit = true;
}
