// JavaScript Document

var sCCDealDiv = "deal_creditcard";
var dbfield_loantype = "dbfield_loantype";
var dbfield_homeowner = "dbfield_homeowner";
var dbfield_ccfcreditcard = "dbfield_ccfcreditcard";
var dbfield_amount = "dbfield_amount";
var sEffectsQueueName = "notification";
var bInformCustomer = true;

function OpenAccount_dbfield_loantype_OnBlur(obj) {
	CheckforValidLoanType(obj);
}
function OpenAccount_dbfield_homeowner_OnBlur(obj) {
	CheckforValidLoanType(obj);
}

function CheckforValidLoanType(obj) {
	bInformCustomer = true;
	if (arguments.length == 2) {
		bInformCustomer = arguments[1];
	}
	var sPreviousLoanType = $F(dbfield_loantype);
	
	if (obj) {
		if ($F(dbfield_loantype) == "Commercial") {
			if ($F(dbfield_amount) == "" || $F(dbfield_amount) * 1 < 25000) {
				$(dbfield_amount).value = 25000;
				InformCustomer("I have changed <b>how much to borrow</b> to <i>&pound;25,000</i> as this is the minimum amount for a Commercial loan.");
			}
		}
		
		
		if (obj.id == dbfield_homeowner && $F(dbfield_homeowner) == "") {
			$(dbfield_homeowner).selectedIndex = 1;
			InformCustomer("I have just changed <b>Do you own your own home</b> to <i>Yes</i> because that question was left unanswered.  Please check we have assumed correctly.");
		}
	} else {
		if ($F(dbfield_homeowner) == "") {
			$(dbfield_homeowner).selectedIndex = 1;
		}
	}
	
	// Homeowner check
	if ($F(dbfield_homeowner) == "No" && (sPreviousLoanType == "Secured" || sPreviousLoanType == "Remortgage") ) {
		var oIndex = $A($(dbfield_loantype).options).find(function(o) {return o.value=="Unsecured"});
		if (oIndex) {
			$(dbfield_loantype).selectedIndex = oIndex.index;
			InformCustomer("I have just changed <b>type of loan</b> to <i>Unsecured</i> because you say that you do not own your own home.  Only homeowners may apply for a " + sPreviousLoanType + " loan.");
		}
	}
	
	
	
	// Amount check
	if ($F(dbfield_amount) != "" ) {		/* || !obj ..... if user has just pressed "submit" allow the app to go to Loanmakers anyway */
	
		/*
		if ($F(dbfield_amount) < 10000 && ($F(dbfield_loantype) == "Secured") ) {
			var oIndex = $A($(dbfield_loantype).options).find(function(o) {return o.value=="Unsecured"});
			if (oIndex) {
				$(dbfield_loantype).selectedIndex = oIndex.index;
				InformCustomer("I have just changed <b>type of loan</b> to <i>Unsecured</i> because Secured loans are a minimum of &pound;10,000.");
			}
		}
		*/
	
		if ( $F(dbfield_amount) < 10000 && ($F(dbfield_loantype) == "Secured" || $F(dbfield_loantype) == "Unsecured") && $F(dbfield_homeowner) == "Yes" ) {
			$(dbfield_amount).value = 10000;
			InformCustomer("I have just changed <b>how much to borrow</b> to <i>&pound;10,000</i> because Homeowners' minimum loans are &pound;10,000.  Change Homeowner to No if you wish to have a smaller loan.");
		} 
	
	} else if(!obj || (obj && obj.id==dbfield_amount)) {
		$(dbfield_amount).value = ( ($F(dbfield_loantype) == "Secured" || $F(dbfield_loantype) == "Unsecured") && $F(dbfield_homeowner) == "Yes" ) ? 10000 : 5000;
		InformCustomer("I have just changed <b>how much to borrow</b> to <i>&pound;" + $F(dbfield_amount) + "</i> because you left this field blank.");
	}
	
	
	
	CheckCreditCardDeal();
	try {
		LTVSection();
	} catch(e) {}
}


function OpenAccount_dbfield_amount_OnBlur(obj) {
	CheckforValidLoanType(obj);
	//try {
	//	LTVSection();
	//} catch (e) {}
	return isNumber(obj);
}



var bAlreadySetCCFCreditCardToYes = false;
function CheckCreditCardDeal() {
	try {
		__utmSetVar($F(dbfield_loantype) + "/" + ($F(dbfield_homeowner)=="Yes"?"Homeowner":"Non-Homeowner"));
	} catch (e) {}
	
	// if ($F("dbfield_loantype") == "Unsecured" && $F("dbfield_homeowner") == "No") {
	if ($F(dbfield_loantype) == "Unsecured" || $F(dbfield_loantype) == "Secured") {
		if (!Element.visible(sCCDealDiv))
			new Effect.BlindDown(sCCDealDiv, {duration:0.5, queue:{scope:sEffectsQueueName,position:'end'}});
			if (!bAlreadySetCCFCreditCardToYes) {
				$(dbfield_ccfcreditcard).selectedIndex = 1;
				bAlreadySetCCFCreditCardToYes = true;
			}
	} else {
		if (Element.visible(sCCDealDiv))
			new Effect.BlindUp(sCCDealDiv, {duration:0.5, queue:{scope:sEffectsQueueName,position:'end'}});
	}
	
}

var iNotificationTimer=0;
var sNotificationArea = "Notification";
var sNotificationContinue = "<div style='margin-top:6px; font-style:italic; font-size:11px'>This message for just your information and it will disappear in a moment.<br>Please carry on filling in the form.</div>";
function InformCustomer(sMessage) {
	if (!bInformCustomer)
		return;
		
	sMessage = "<b>&bull; Information:</b> " + sMessage;
	if (iNotificationTimer) {
		clearTimeout(iNotificationTimer);
		$(sNotificationArea).update(sMessage+ "<div style='margin-top:6px;'></div>" + $(sNotificationArea).innerHTML);
	} else {
		$(sNotificationArea).update(sMessage + sNotificationContinue);
		try {
			$(sNotificationArea).visualEffect("BlindDown", {duration:2, queue:{scope:sEffectsQueueName,position:'end'}});
		} catch(e) {}
	}
	try {
		$(sNotificationArea).visualEffect("Highlight", {startcolor:'#FD8577', endcolor:'#FFFF00', queue:{scope:sEffectsQueueName,position:'end'}});
	} catch(e) {}
	iNotificationTimer = setTimeout("CloseNotification()", 15000);
}

function CloseNotification() {
	iNotificationTimer = 0;
	try {
		$(sNotificationArea).visualEffect("BlindUp", {duration:2, queue:{scope:sEffectsQueueName,position:'end'}});
	} catch(e) {}
}