// ExitRate object
function ExitRate() {}

// Consumer data variables.
ExitRate.prototype.loantype = '';
ExitRate.prototype.loanamount = '';
ExitRate.prototype.zipcode = '';
ExitRate.prototype.state = '';
ExitRate.prototype.ltv = '';
ExitRate.prototype.credit = '';

// Messages.
ExitRate.prototype.newwindowmessage = 'No time to fill out a form? Just click OK to see rates from top lenders.';
ExitRate.prototype.samewindowmessage = 'View rates from top lenders by clicking Cancel to say on this page. No need to fill the form.';

// Variables to control logic.
ExitRate.prototype.status = 'qsExitRate';
ExitRate.prototype.idletimelimit = 60000;
ExitRate.prototype.toggle = 1;
ExitRate.prototype.pageoverride = [];
ExitRate.prototype.newwindow = false;
ExitRate.prototype.userstayed = false;
ExitRate.prototype.locationchanged = false;
ExitRate.prototype.newwindowproperties = 'width=800,height=600,top=10,left=10,screenX=0,screenY=10,resizable=yes,scrollbars=yes';
ExitRate.prototype.targetpage = {
    'mortgagerates': {'url':'/'},
    'homefinance': {'url':'/'}
};
ExitRate.prototype.valuemap = {};

// Timers to trigger dialog.
ExitRate.prototype.loadinterval = null;
ExitRate.prototype.idletimer = null;

// Methods.
ExitRate.prototype.resetTimer = function() {
    if (this.idletimer == null) {
        if (xrate.newwindow == true) {
            this.idletimer = window.setTimeout('xrate.showNewWindow()', this.idletimelimit);
        } else {
            this.idletimer = window.setTimeout('xrate.showSameWindow()', this.idletimelimit);
        }
    } else {
        window.clearTimeout(this.idletimer);
        if (xrate.newwindow == true) {
            this.idletimer = window.setTimeout('xrate.showNewWindow()', this.idletimelimit);
        } else {
            this.idletimer = window.setTimeout('xrate.showSameWindow()', this.idletimelimit);
        }
    }
};
ExitRate.prototype.setCookie = function(name,value,days) {
    if (name == null) {name = this.status;}
    if (value == null) {value = 0;}
    if (days == null) {days = 1;}
    var expiry = new Date();
    expiry.setTime(expiry.getTime()+(days*24*60*60*1000));
    document.cookie = name + '=' + value + ';expires=' + expiry.toGMTString() + ';path=/';
};
ExitRate.prototype.getCookie = function(name) {
    if (!(typeof name == 'string')) {return null;}
    var allcookies = document.cookie.split(';');
    var cookiename = name + '=';
    for (var i = 0; i < allcookies.length; i++) {
        var c = allcookies[i];
        while (c.charAt(0) == ' ') {c = c.substring(1, c.length);}
        if (c.indexOf(cookiename) == 0) {return c.substring(cookiename.length, c.length);}
    }
    return null;
};
ExitRate.prototype.delCookie = function(name) {
    this.setCookie(name, null, -1);
};
ExitRate.prototype.enableRate = function() {
    this.toggle = 1;
};
ExitRate.prototype.disableRate = function() {
    this.toggle = 0;
};
ExitRate.prototype.showNewWindow = function() {
    window.clearTimeout(this.idletimer);
    var status = this.getCookie(this.status);
    if (status != '0' && this.toggle != 0) {
        this.setCookie(this.status, 0, 1);
        if (confirm(this.newwindowmessage)) {
            var pageurl = this.targetUrl();
            var xratewin = window.open(pageurl, 'xRatePage', this.newwindowproperties);
            xratewin.focus();
        }
    }
};
ExitRate.prototype.showSameWindow = function() {
    window.clearTimeout(this.idletimer);
    var status = this.getCookie(this.status);
    if (status != '0' && this.toggle != 0) {
        this.setCookie(this.status, 0, 1);
        if (confirm(this.newwindowmessage)) {
            var pageurl = this.targetUrl();
            window.top.location = pageurl;            
        }
    }
};
ExitRate.prototype.changeLocation = function() {
    if (this.userstayed == true) {
        this.locationchanged = true;
        if (this.loadinterval != null) {
            window.clearInterval(this.loadinterval);
        }
        var pageurl = this.targetUrl();        
        window.top.location = pageurl;
    }
};
ExitRate.prototype.targetUrl = function() {
    var vloan = this.loantype;
    var vcredit = this.credit;
    var vstate = this.state;
    var vamount = parseInt(this.loanamount);
    var vltv = parseFloat(this.ltv);
    var vzipcode = this.zipcode;

    var page = this.targetpage.mortgagerates.url;
    var isNotRateTable = false;
    var params = '?';

    if (this.valuemap.loantype) {
        if (vloan) {
            vloan = this.valuemap.loantype[vloan];
        } else {
            vloan = '2';
        }
    }
    if (typeof vloan == 'undefined') {
        vloan = '2';
    }
    if (this.valuemap.credit) {
        if (vcredit) {
            vcredit = this.valuemap.credit[vcredit];
        } else {
            vcredit = '';
        }
    }
    if (typeof vcredit == 'undefined') {
        vcredit = '';
    }
    if (isNaN(vamount)) {
        vamount = '';
    } else {
        if (vamount < 100000) {
            isNotRateTable = true;
        }
    }
    if (isNaN(vltv)) {
        vltv = '';
    } else {
        if ((vltv > 80) && (vloan == '2')) {
            isNotRateTable = true;
        }
        if ((vltv > 95) && (vloan == '1')) {
            isNotRateTable = true;
        }
    }
    if (vcredit != '' && (vcredit != 'Excellent') && (vcredit != 'Good')) {
        isNotRateTable = true;
    }
    if (isNotRateTable) {
        page = this.targetpage.homefinance.url;
    }
    params += 'loantype=' + vloan + '&';
    params += 'zipcode=' + vzipcode + '&';
    params += 'statecode=' + vstate + '&';
    params += "loanamount=" + vamount;

    return page + params;
};

