
function validateForm() {
    var f = document.creditwiz;
    var elem;
    var sTemp = new String();
    var tempChoices;
    var cnt;
    var valid = true;
    var allAnswered = true;
    var nbrs = "0123456789";
    var score = 0;
    
    /* Zip Code validation 
    ---------------------------------------------------------------------------------------*/
    elem = document.getElementById("zipCode");
    sTemp = elem.value;
    var invalidZip = document.getElementById("marketZipError");
    if (invalidZip.value == "true") {
        document.getElementById('invalidzipmessage').style.display  = 'block';
        document.getElementById('ziplabel').style.color  = '#f00';
        document.getElementById('errorimage').style.display = 'block';
        elem.focus();
        valid = false;
    } else {
        document.getElementById('invalidzipmessage').style.display  = 'none';
        document.getElementById('ziplabel').style.color  = '#000';
        document.getElementById('errorimage').style.display = 'none';
    }

    /* Question Validations 
    --------------------------------------------------------------------------------------*/
    
    /* Question1 */    
    elem = document.getElementById("answer1answers");
    tempChoices = elem.getElementsByTagName('input');
    cnt = -1;
    for (var i=tempChoices.length-1; i > -1; i--) {
        if(tempChoices[i].checked) {
            cnt = i;
            score = score + Number(tempChoices[i].value);
            i = -1;
            }
    }
    if (cnt > -1) {
        document.getElementById('question1').style.color  = '#000';
        document.getElementById('answerallmessage').style.display  = 'none';    
    } else {
        window.scrollTo(0,0);    
        document.getElementById('question1').style.color  = '#f00';
        allAnswered = false;
        valid = false;        
    }

    /* Question 2 */  
    elem = document.getElementById("answer2answers");
    tempChoices = elem.getElementsByTagName('input');
    cnt = -1;
    for (var i=tempChoices.length-1; i > -1; i--) {
        if(tempChoices[i].checked) {
            cnt = i;
            score = score + Number(tempChoices[i].value);
            i = -1;
            }
    }
    if (cnt > -1) {
        document.getElementById('answerallmessage').style.display  = 'none';
        document.getElementById('question2').style.color  = '#000';
    } else {
        window.scrollTo(0,0);    
        document.getElementById('question2').style.color  = '#f00';
        allAnswered = false;
        valid = false;        
    }
    /* Question 3 */  
    elem = document.getElementById("answer3answers");
    tempChoices = elem.getElementsByTagName('input');
    cnt = -1;
    for (var i=tempChoices.length-1; i > -1; i--) {
        if(tempChoices[i].checked) {
            cnt = i;
            score = score + Number(tempChoices[i].value);
            i = -1;
            }
    }
    if (cnt > -1) {
        document.getElementById('answerallmessage').style.display  = 'none';
        document.getElementById('question3').style.color  = '#000';
    } else {
        window.scrollTo(0,0);    
        document.getElementById('question3').style.color  = '#f00';
        allAnswered = false;
        valid = false;        
    }
    /* Question 4 */  
    elem = document.getElementById("answer4answers");
    tempChoices = elem.getElementsByTagName('input');
    cnt = -1;
    for (var i=tempChoices.length-1; i > -1; i--) {
        if(tempChoices[i].checked) {
            cnt = i;
            score = score + Number(tempChoices[i].value);
            i = -1;
            }
    }
    if (cnt > -1) {
        document.getElementById('answerallmessage').style.display  = 'none';
        document.getElementById('question4').style.color  = '#000';
    } else {
        window.scrollTo(0,0);    
        document.getElementById('question4').style.color  = '#f00';
        allAnswered = false;
        valid = false;        
    }
    /* Question 5 */  
    elem = document.getElementById("answer5answers");
    tempChoices = elem.getElementsByTagName('input');
    cnt = -1;
    for (var i=tempChoices.length-1; i > -1; i--) {
        if(tempChoices[i].checked) {
            cnt = i;
            score = score + Number(tempChoices[i].value);
            i = -1;
            }
    }
    if (cnt > -1) {
        document.getElementById('answerallmessage').style.display  = 'none';
        document.getElementById('question5').style.color  = '#000';
    } else {
        window.scrollTo(0,0);    
        document.getElementById('question5').style.color  = '#f00';
        allAnswered = false;
        valid = false;        
    }
    /* Question 6 */  
    elem = document.getElementById("answer6answers");
    tempChoices = elem.getElementsByTagName('input');
    cnt = -1;
    for (var i=tempChoices.length-1; i > -1; i--) {
        if(tempChoices[i].checked) {
            cnt = i;
            score = score + Number(tempChoices[i].value);
            i = -1;
            }
    }
    if (cnt > -1) {
        document.getElementById('answerallmessage').style.display  = 'none';
        document.getElementById('question6').style.color  = '#000';
    } else {
        window.scrollTo(0,0);    
        document.getElementById('question6').style.color  = '#f00';
        allAnswered = false;
        valid = false;        
    }
    /* Question 7 */  
    elem = document.getElementById("answer7answers");
    tempChoices = elem.getElementsByTagName('input');
    cnt = -1;
    for (var i=tempChoices.length-1; i > -1; i--) {
        if(tempChoices[i].checked) {
            cnt = i;
            score = score + Number(tempChoices[i].value);
            i = -1;
            }
    }
    if (cnt > -1) {
        document.getElementById('answerallmessage').style.display  = 'none';
        document.getElementById('question7').style.color  = '#000';
    } else {
        window.scrollTo(0,0);    
        document.getElementById('question7').style.color  = '#f00';
        allAnswered = false;
        valid = false;        
    }
    
    if (!allAnswered) {
        document.getElementById('answerallmessage').style.display  = 'block';
    } else {
        if (score <= 17){
            document.getElementById('rdpage').value = 'ETG';
        } else if (score > 17 && score < 65){
            document.getElementById('rdpage').value = 'GTF';
        } else {
            document.getElementById('rdpage').value = 'FTP';
        }
    }
    if (valid) f.submit()
}

// Quick Path Validation
function validateQuickPathForm() {
    var f = document.creditwiz;
    var errorBkImg = '/img/fyc/gfx_error_17x17.gif';
    var errorWtImg = '/img/white_dot.gif';
    var elem;
    var sTemp = new String();
    var tempChoices;
    var cnt;
    var valid;
    var allAnswered = true;
    var nbrs = "0123456789";
    var sendToArticle = false;
    
    /* Zip Code validation 
    ---------------------------------------------------------------------------------------*/
    elem = document.getElementById("zipCode");
    sTemp = elem.value;
    if (sTemp.length != 5 || sTemp == '000000') {
        window.scrollTo(0,0);
        document.getElementById('invalidzipmessage').style.display  = 'block';
        document.getElementById('ziplabel').style.color  = '#f00';
        document.getElementById('errorimage').style.display = 'block';    
        elem.focus();
        valid = false;
    } else {
        for (var i=0; i < sTemp.length; i++) {
            temp = "" + sTemp.substring(i, i+1);
            if (nbrs.indexOf(temp) == "-1") {
                document.getElementById('invalidzipmessage').style.display  = 'block';
                document.getElementById('ziplabel').style.color  = '#f00';
                document.getElementById('errorimage').style.display = 'block';
                elem.focus();
                valid = false;
                break;
            } else {
                document.getElementById('invalidzipmessage').style.display  = 'none';
                document.getElementById('ziplabel').style.color  = '#000';
                document.getElementById('errorimage').style.display = 'none';
            }
        }
    }
    
    var validZip = document.getElementById("marketZipError");
    if (validZip.value == "true") {
        document.getElementById('invalidzipmessage').style.display  = 'block';
        document.getElementById('ziplabel').style.color  = '#f00';
        document.getElementById('errorimage').style.display = 'block';
        elem.focus();
        valid = false;
    } else {
        valid = true;
    }
        
    /* Question Validations 
    --------------------------------------------------------------------------------------*/
    
    /* Question1 */
    var etg = ['0','0','0','0','0','0','0'];
    var gtf = ['7.5','6','3.5','12.5','12.5','10','0'];
    var ftp = ['15','12','7','25','25','20','90'];
    
    cnt = -1;
    for (var i=0; i < f.creditTier.length; i++)  { 
        if (f.creditTier[i].checked){
            cnt = i;
            tempChoices = f.creditTier[i].value;
            i = f.creditTier[i].length +1;
        }
    }
    if (cnt > -1) {
        document.getElementById('answerallmessage').style.display  = 'none';
        document.getElementById('qpAnswerError').style.display  = 'none';
        document.getElementById('qpQuestionEtG').style.color  = '#000';
        document.getElementById('qpErrorEtG').src = errorWtImg;
        document.getElementById('qpQuestionGtF').style.color  = '#000';
        document.getElementById('qpErrorGtF').src = errorWtImg;
        document.getElementById('qpQuestionFtP').style.color  = '#000';
        document.getElementById('qpErrorFtP').src = errorWtImg;
        document.getElementById('qpQuestionNC').style.color  = '#000';
        document.getElementById('qpErrorNC').src = errorWtImg;
    } else {
        window.scrollTo(0,0);    
        document.getElementById('qpAnswerError').style.color  = '#f00';
        document.getElementById('qpAnswerError').style.display  = 'block';
        document.getElementById('qpQuestionEtG').style.color  = '#f00';
        document.getElementById('qpErrorEtG').src = errorBkImg;
        document.getElementById('qpQuestionGtF').style.color  = '#f00';
        document.getElementById('qpErrorGtF').src = errorBkImg;
        document.getElementById('qpQuestionFtP').style.color  = '#f00';
        document.getElementById('qpErrorFtP').src = errorBkImg;
        document.getElementById('qpQuestionNC').style.color  = '#f00';
        document.getElementById('qpErrorNC').src = errorBkImg;
        allAnswered = false;
        valid = false;        
    }
    if (!allAnswered) {
        document.getElementById('answerallmessage').style.display  = 'block';
    } else {
        if (tempChoices == 'etg'){
            document.getElementById('rdpage').value = 'ETG';
            for (var i=1; i <= etg.length; i++)  {
                document.getElementById(eval('"q' + i + '"')).value = etg[i-1];           
            }
        } else if (tempChoices == 'gtf'){
            document.getElementById('rdpage').value = 'GTF';
            for (var i=1; i <= gtf.length; i++)  {
                document.getElementById(eval('"q' + i + '"')).value = gtf[i-1];
            }
        } else if (tempChoices == 'ftp'){
            document.getElementById('rdpage').value = 'FTP';
            for (var i=1; i <= ftp.length; i++)  {
                document.getElementById(eval('"q' + i + '"')).value = ftp[i-1];
            }
        } else {
            sendToArticle = true;            
        }
    }
    if (valid && !sendToArticle){
        f.submit();
    } else if (valid && sendToArticle){
        window.location.href = '/creditcenter/learn.jsp?contentid=25359';
    }
}

// Landing Page Validation
function validateLandingForm() {
    var f = document.creditwiz;
    var ftp = ['15','12','7','25','25','20','90'];
    var elem;
    var sTemp = new String();
    var valid;
    var nbrs = "0123456789";
    
    /* Zip Code validation
    ---------------------------------------------------------------------------------------*/
    elem = document.getElementById("zipCode");
    sTemp = elem.value;
    if (sTemp.length != 5 || sTemp == '000000') {
        window.scrollTo(0,0);
        document.getElementById('invalidzipmessage').style.display  = 'block';
        document.getElementById('ziplabel').style.color  = '#f00';
        //document.getElementById('errorimage').style.display = 'block';    
        elem.focus();
        valid = false;
    } else {
        for (var i=0; i < sTemp.length; i++) {
            temp = "" + sTemp.substring(i, i+1);
            if (nbrs.indexOf(temp) == "-1") {
                document.getElementById('invalidzipmessage').style.display  = 'block';
                document.getElementById('ziplabel').style.color  = '#f00';
                //document.getElementById('errorimage').style.display = 'block';
                elem.focus();
                valid = false;
                break;
            } else {
                document.getElementById('invalidzipmessage').style.display  = 'none';
                document.getElementById('ziplabel').style.color  = '#000';
                //document.getElementById('errorimage').style.display = 'none';
            }
        }
    }
    var validZip = document.getElementById("marketZipError");
    if (validZip.value == "true") {
        document.getElementById('invalidzipmessage').style.display  = 'block';
        document.getElementById('ziplabel').style.color  = '#f00';
        //document.getElementById('errorimage').style.display = 'block';
        elem.focus();
        valid = false;
    } else {
        valid = true;
    }
    
    document.getElementById('rdpage').value = 'FTP';
    for (var i=1; i <= ftp.length; i++)  {
        document.getElementById(eval('"q' + i + '"')).value = ftp[i-1];
    }
    if (valid) f.submit()
}

var zip;

function validMarketZipLandingPage() {
    zip = document.getElementById("zipCode").value;
    MarketManager.getMarketByZipCode(zip, marketLandingPageCallBack);
}

var marketLandingPageCallBack = function (data) {
    if (data.zipCode != null) {
        document.creditwiz.marketZipError.value = "false";
    } else {
        document.creditwiz.marketZipError.value = "true";
    }
    validateLandingForm();
}

function validMarketZip() {
    zip = document.getElementById("zipCode").value;
    MarketManager.getMarketByZipCode(zip, marketCallBack);
}

var marketCallBack = function (data) {
    if (data.zipCode != null) {
        document.creditwiz.marketZipError.value = "false";
    } else {
        document.creditwiz.marketZipError.value = "true";
    }
}

function disclaimer_popup() {
    window.open('inc/popup_myatc_disclaimer.jsp','disclaimer','height=210,width=410,status=no,toolbar=no,titlebar=no,menubar=no,location=no,screenX=512,screenY=384,left=512,top=384');
}

// Start PNG Alpha Fix

// Correctly handle PNG transparency in Win IE 5.5 or higher.
// http://homepage.ntlworld.com/bobosola. Updated 02-March-2004
function correctPNG(){
    for(var i=0; i<document.images.length; i++){
        var img = document.images[i];
        var imgName = img.src.toUpperCase();
        var imgVersionBust = /\?/;
        var imgVersionLocal = imgName.search(imgVersionBust);
        if (imgName.substring(imgVersionLocal-3, imgVersionLocal) == "PNG"){
            var imgID = (img.id) ? "id='" + img.id + "' " : "";
            var imgClass = (img.className) ? "class='" + img.className + "' " : "";
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
            var imgStyle = "display:inline-block;" + img.style.cssText;
            if (img.align == "left") imgStyle = "float:left;" + imgStyle;
            if (img.align == "right") imgStyle = "float:right;" + imgStyle;
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
            var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='" + getImageScalingMethod(img.className) + "');\"></span>"
            img.outerHTML = strNewHTML
            i = i-1
        }
    }
}

var scaleAsImageClasses = new Array(
    "offer-box-top",
    "offer-box-bottom",
    "offer-or-image"
);
    
function getImageScalingMethod(imgClassName) {
    return scaleAsImageClasses.inArray(imgClassName) ? "image" : "scale";
}

String.prototype.contains = function (string) {
    return this.indexOf(string) != -1;
}

Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] == value) {
            return true;
        }
    }
    return false;
};

if (window.attachEvent && !window.opera) {
    window.attachEvent("onload", correctPNG);
}

// End PNG Alpha Fix

// Set Element Width
function setPageElement2MaxWidth(elementID){
    var element = document.getElementById(elementID);
    var clientWidth = maxWidth();
    // changed to set value to stay within 990px
    // old code: var styleWidth = (clientWidth - 20) + 'px';
    var styleWidth = '980px';
    element.style.width = styleWidth;
}

function maxWidth() {
    var w=0;
    if (window.document.innerWidth > w){
        w=window.document.innerWidth;
    } else if (window.document.documentElement.clientWidth > w){
        w=window.document.documentElement.clientWidth;
    } else if (window.document.body.clientWidth > w){
        w=window.document.body.clientWidth;
    }
    return w;
}

function resetPageElement2MaxWidth(elementID){
    if(document.getElementById(elementID)){
        setPageElement2MaxWidth(elementID);
    }
}
// End Set Element Width