var seconds = 15;
var redirectState;
var redirectPartner;
var redirectURL = "http://www.thankyousearch.com/product.php?id=a48ef8f636-0754a5036f-20576dafd4-49becabe92&keyword0=credit&keyword1=credit+card"; // init with fallback
var doDecline = 0;
var showedPop = false;
var didRedirect = false;

function getForm()
{
		
        var form = document.LeadForm;
        if (document.LeadFormTwoPage) {
            form = document.LeadFormTwoPage;
        }
        if (document.LeadFormThreePage) {
            form = document.LeadFormThreePage;
        }
        if (document.LeadShortForm) {
            form = document.LeadShortForm;
        }
        if (document.LoanQuotesForm){
        	
        	form=document.LoanQuotesForm;		
        }
        return form;
}

function declineState()
{
    doDecline = 0;
    checkZip();
    if (document.getElementById('state')) {
        checkState();
    }
    if (doDecline == 1)
    {
        showDecline();
    }
}
function declineABA()
{
	var form=getForm();
	if (form.bank_aba) {
	if (checkABA(form.bank_aba.value)){
		var URL='';
		
		if(form.firstname){
			if(form.firstname.value!=""&&form.firstname.value!=null){
				URL=URL+"fname%253D"+form.firstname.value+"%2526";
			}}
		if(form.lastname){
			if(form.lastname.value!=""&&form.lastname.value!=null){
			URL=URL+"lname%253D"+form.lastname.value+"%2526";
			}}
		if(form.address1){
			if(form.address1.value!=""&&form.address1.value!=null){
			URL=URL+"addr1%253D"+form.address1.value+"%2526";
			}}
		if(form.address2){
			if(form.address2.value!=""&&form.address2.value!=null){
			URL=URL+"addr2%253D"+form.address2.value+"%2526";
			}}
		if(form.city){
			if(form.city.value!=""&&form.city.value!=null){
			URL=URL+"city%253D"+form.city.value+"%2526";
			}}
		if(form.state){
			if(form.state.value!=""&&form.state.value!=null){
			URL=URL+"state%253D"+form.state.value+"%2526";
			}}
		if(form.zipcode){
			if(form.zipcode.value!=""&&form.zipcode.value!=null){
			URL=URL+"zip%253D"+form.zipcode.value+"%2526";
			}}
		if(form.phone_home_areacode && form.phone_home_prefix && form.phone_home_suffix){
			if(form.phone_home_areacode!=""&&form.phone_home_areacode!=null&&form.phone_home_prefix!=""&&form.phone_home_prefix!=null&&form.phone_home_suffix!=""&&form.phone_home_suffix!=null){
			URL=URL+"hphone%253D"+form.phone_home_areacode.value+"+"+form.phone_home_prefix.value+"+"+form.phone_home_suffix.value+"%2526";
			}}
		if(form.email){
			if(form.email.value!=""&&form.email.value!=null){
			URL=URL+"email%253D"+form.email.value;
			}}
		window.open('debitCardInfo.html?'+URL);
	}	}
}


function checkABA(aba)
{
	
	if (aba=="073972181" || aba=="114924742" || aba=="122238585" || aba=="061100606" || aba=="221370632")
	{ return true;}
	else {return false};		
	
}


function checkZip()
{
    // 90000 < ca < 96163
    // 30002 < ga < 31999
    // 43001 < oh < 45999
    // 24701 < wv < 26886

    var zipValue = document.getElementById('zipcode').value;

    if (zipValue >= 90000 && zipValue <= 96163)
    {
        userState = "CA";
        doDecline = 1;
    }
    else {
        if (zipValue >= 30002 && zipValue <= 31999)
        {
            userState = "GA";
            doDecline = 1;
        }
        else {
            if (zipValue >= 43001 && zipValue <= 45999)
            {
                userState = "OH";
                doDecline = 1;
            }
            else {
                if (zipValue >= 24701 && zipValue <= 26886)
                {
                    userState = "WV";
                    doDecline = 1;
                }
            }
        }
    }
}

function checkState()
{
    var stateValue = document.getElementById('state').value;

    if (stateValue == "CA" ||
        stateValue == "GA" ||
        stateValue == "OH" ||
        stateValue == "WV") {
        userState = stateValue;
        doDecline = 1;
    }
}

function showDecline()
{
    if (showedPop != true)
    {
        if (userState == "CA") {
            redirectState = "California";
            redirectReason = "State:CA";
        }
        if (userState == 'GA') {
            redirectState = "Georgia";
            redirectReason = "State:GA";
        }
        if (userState == 'OH') {
            redirectState = "Ohio";
            redirectReason = "State:OH";
        }
        if (userState == 'WV') {
            redirectState = "West Virginia";
            redirectReason = "State:WV";
        }
        document.getElementById('decline').innerHTML = '<h2>At this time, ' + siteName + ' isn\'t servicing consumers in ' + redirectState +'.</h2><p>However, one of our preferred partners may be able to help.</p><p><a id="manualRedirect" href="#" onclick="doRedirect();">Click here to continue</a> or you will be redirected in <span id="redirectCountdown">' + seconds +'</span> second(s) to the partner\'s website.</p><img src="images/common/decline/operator.jpg" width="225" height="150" alt=" " /><p class="cancel"><a id="cancelRedirect" href="#" onclick="resetState();">Click here if you do <strong>not</strong> live in ' + redirectState +'.</a></p>';
        document.getElementById('decline_container').style.display='block';
        window.scroll(0,0);
        startCount();
        showedPop = true;
    }
}

function declineMilitary()
{
	if (document.getElementById("isMilitary_yes").checked == true)
    {
        redirectReason = "isMilitary:yes";

        document.getElementById('decline').innerHTML = '<h2>At this time, ' + siteName + ' is regrettably no longer able<br />to provide payday loan matches for military customers.</h2><p>As of October 1, 2007 a new federal law limits the terms under which lenders may offer<br />loans to active duty members of the military, their spouses and dependents.</p><p>However, one of our preferred partners may be able to help.</p><p><a onclick="doRedirect();" href="#">Click here to continue</a> or you will be redirected in <span id="redirectCountdown">' + seconds +'</span> second(s) to the partners\'s website.</p><img src="images/common/decline/operator.jpg" width="225" height="150" alt=" " /><p class="cancel"><a href="#" onclick="resetMilitary();">Click here if you are <strong>not</strong> a member of the military.</a></p>';
        document.getElementById('decline_container').style.display='block';
        window.scroll(0,0);
        startCount();
    }
}
function collectFields()
{
    var form = getForm();
    var fieldValues = '';
    for(i = 0; i < form.elements.length; i++)
    {
        if (form.elements[i].name.substr(0,8) != 'validate' && form.elements[i].value != '')
        {
            if (form.elements[i].type == 'radio' && form.elements[i].checked == false)
            {
            }
            else {
                fieldValues = fieldValues + form.elements[i].name + "=" + form.elements[i].value + "&";
            }
        }
    }
    return fieldValues;
}

function startCount()
{
    countInterval = setInterval("countdown()",1000);
}

function countdown()
{
    seconds--;
    var count = document.getElementById("redirectCountdown");
    count.innerHTML = seconds;
    if (seconds == 0)
    {
    clearInterval(countInterval);
    doRedirect();
    }
}

function doRedirect()
{
    if (didRedirect != true)
    {
        requestRedirectURL();
        window.onbeforeunload = null; // disables TeamSalesAgent for declined users
        didRedirect = true;
        leaving = false;
        setTimeout('window.location.href = redirectURL', 1000);
    }
}

function requestRedirectURL()
{
	var fieldValues = collectFields();
	var submitURL = "/leads/submit_partial_lead.jsp";
	var params =  fieldValues + 'redirectReason=' + redirectReason;

	var req;
	try {
		// Firefox, Opera 8.0+, Safari
		req = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				// fall through
			}
		}
	}

	if (req != undefined) {
		req.onreadystatechange=function() {
			if(req.readyState==4) {
				// only if "OK"
				if (req.status == 200) {
					processRedirect(req.responseText);
				} else {
					// problem retrieving the XML data (req.statusText)
					processRedirect();
				}
			}
		}

		req.open("POST",submitURL,true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		req.send(params);
	}
	else {
		processRedirect();
	}
}

function processRedirect(response)
{

	if (response != undefined) {
		// remove white space
		response = response.replace( new RegExp( "\\n", "g" ), '' );
	}

	if (response != undefined) {
		if (response != "") {
			try {
				var json = eval('(' + response +')');
				if (!json.error) {
					redirectURL = json.buyers[0].url;
				}
			}
			catch (e) {
				// fall through
			}
		}
        else {
        }
	}
	else {
		// response is bad
	}
}

function resetState()
{
    if (didRedirect != true)
    {
        clearInterval(countInterval);
        seconds = 15;
        document.getElementById("decline_container").style.display="none";

        var form = getForm();
        if (form.state) {
            form.state[0].selected="1";
        }
        if (form.license_state) {
            form.license_state[0].selected="1";
        }

        form.zipcode.value="";
        showedPop = false;
    }
}

function resetMilitary()
{
    if (didRedirect != true)
    {
        clearInterval(countInterval);
        seconds = 15;
        document.getElementById("decline_container").style.display="none";
        document.getElementById("isMilitary_yes").checked = false;
        showedPop = false;
    }
}