﻿//JScript File



//To change the src of a control ****CAUSING ISSUE WITH COREMETRICS
var imageNo = 1;

//Array.prototype.findIndex = function(value){
//var ctr = "";
//for (var i=0; i < this.length; i++) {
//// use === to check for Matches. ie., identical (===), ;
//if ((value.toUpperCase().indexOf(this[i].toUpperCase())) != -1) {
//return i;
//}
//}
//return ctr;
//};

function SwapImage(Ctrl, Url) {
    document.getElementById(Ctrl).src = Url;
}

function swapImages(inform, id_tourImage, id_thb2, fileCount) {
    var fullPath = document.getElementById(inform).src
    var arr = fullPath.split("/");
    var imgNo = parseInt(arr[arr.length - 1].split("_thb")[1].split(".")[0])
    if (imgNo + 2 > fileCount && imgNo % 2 == 1) {
        imgNo = 1
    }
    else if (imgNo + 2 > fileCount && imgNo % 2 == 0) {
        imgNo = 2
    }
    else {
        imgNo = imgNo + 2
    }
    var thbImage1 = fullPath.toString().replace(arr[arr.length - 1], arr[arr.length - 1].split("_thb")[0] + "_thb" + (imgNo) + "." + arr[arr.length - 1].split("_thb")[1].split(".")[1]);
    var thbImage2 = fullPath.toString().replace(arr[arr.length - 1], arr[arr.length - 1].split("_thb")[0] + "_thb" + (imgNo) + "." + arr[arr.length - 1].split("_thb")[1].split(".")[1]);
    var fileName = fullPath.toString().replace(arr[arr.length - 1], arr[arr.length - 1].toString().replace("_thb", "_med"));
    //alert(fileName + "\n" + thbImage1 + "\n" + thbImage2)
    document.getElementById(id_tourImage).setAttribute("src", fileName);
    document.getElementById(id_thb2).setAttribute("src", thbImage1);
    document.getElementById(inform).setAttribute("src", thbImage2);
    return false;
}


function NextImage(imgName, fileCount, lnkNext, Photocount) {
    var fullPath = document.getElementById(imgName).src
    var arr = fullPath.split("/");
    var imgNo = parseInt(arr[arr.length - 1].split("_lg")[1].split(".")[0])
    if (imgNo >= fileCount) {
        return false;
    }
    else {
        imgNo++;
    }
    var txt = imgNo + " of " + fileCount
    document.getElementById(Photocount).innerHTML = txt
    var thbImage1 = fullPath.toString().replace(arr[arr.length - 1], arr[arr.length - 1].split("_lg")[0] + "_lg" + (imgNo) + "." + arr[arr.length - 1].split("_lg")[1].split(".")[1]);
    document.getElementById(imgName).src = thbImage1

    return false;

}

function MoveToNextImage(imgName, fileCount, lnkNext, Photocount, imgLstCtl) {
    var fullPath = document.getElementById(imgName).src
    var ImageLst = document.getElementById(imgLstCtl).value.split("^");

    if (imageNo >= ImageLst.length && imageNo >= fileCount) {
        return false;
    }
    else {
        imageNo++;
    }
    var txt = "image " + imageNo + " of " + fileCount
    document.getElementById(Photocount).innerHTML = txt
    document.getElementById(imgName).src = ImageLst[imageNo - 1];

    return false;

}

function MoveToPrevImage(imgName, fileCount, lnkNext, Photocount, imgLstCtl) {
    var fullPath = document.getElementById(imgName).src
    var ImageLst = document.getElementById(imgLstCtl).value.split("^");

    if (imageNo <= 1) {
        return false;
    }
    else {
        --imageNo;
    }
    var txt = "image " + imageNo + " of " + fileCount
    document.getElementById(Photocount).innerHTML = txt;
    document.getElementById(imgName).src = ImageLst[imageNo - 1];

    return false;

}



function PrevImage(imgName, fileCount, lnkPrev, Photocount) {
    var fullPath = document.getElementById(imgName).src
    var arr = fullPath.split("/");
    var imgNo = parseInt(arr[arr.length - 1].split("_lg")[1].split(".")[0])

    if (imgNo < 1) {

        return false;
    }
    else {
        imgNo--;
    }
    var txt = "image " + imgNo + " of " + fileCount
    document.getElementById(Photocount).innerHTML = txt
    var thbImage1 = fullPath.toString().replace(arr[arr.length - 1], arr[arr.length - 1].split("_lg")[0] + "_lg" + (imgNo) + "." + arr[arr.length - 1].split("_lg")[1].split(".")[1]);
    document.getElementById(imgName).src = thbImage1
    return false;
}

/* Rewards Calculator Page*/
function setValues(TourLength, PayParticipant, SubTotal, SRBonus, TourYear, Incentive, Total, TxtTotal, currYear, globalEarn, quadEarn, tripleEarn, doubleEarn) {
    setTourYear(TourYear, Incentive, currYear, globalEarn)
    setIncentive(TourLength, PayParticipant, SRBonus, TourYear, Incentive, Total, TxtTotal, currYear, globalEarn, quadEarn, tripleEarn, doubleEarn)
    setSubTotal(TourLength, PayParticipant, SubTotal)
}
function setCalValues(TourLength, PayParticipant, SubTotal, SRBonus, TourYear, Total, currYear, seasonBonus) {

    setSeasonalBonus(TourLength, PayParticipant, SRBonus, TourYear, Total, currYear, seasonBonus)
    setSubTotal(TourLength, PayParticipant, SubTotal)
}
function setSeasonalBonus(TourLength, PayParticipant, SRBonus, TourYear, Total, currYear, seasonBonus) {
    var bonus;
    bonus = document.getElementById(SRBonus).value;
    if (bonus <= 2 && bonus > 0) {
        bonus = 3;
    }
    else if (bonus == 3) {
        bonus = 2;
    }
    else if (bonus == 4) {
        bonus = 2;
    }
    else if (bonus == 13) {
        bonus = 4;
    }
    else {
        bonus = 1;
    }

    var tot
    switch (parseInt(bonus)) {
        case 4:
            {
                if ((document.getElementById(TourYear).value == parseInt(currYear) + 1) || (document.getElementById(TourYear).value == parseInt(currYear) + 2)) {

                    bonus = 4;
                }
                else {
                    bonus = 1;
                }

                break
            }
        case 3:
            {
                if ((document.getElementById(TourYear).value == parseInt(currYear) + 2)) {
                    bonus = 4;
                }
                else if ((document.getElementById(TourYear).value == currYear) || (document.getElementById(TourYear).value == parseInt(currYear) + 1)) {

                    bonus = 1
                }

                break
            }
        case 2:
            {
                if ((document.getElementById(TourYear).value == parseInt(currYear) + 2)) {
                    bonus = 4;
                }
                else if ((document.getElementById(TourYear).value == currYear) || (document.getElementById(TourYear).value == parseInt(currYear) + 1)) {

                    bonus = 1
                }

                break
            }
        default:
            {

                break
            }
    }
    if (document.getElementById(TourYear).value != 0) {
        tot = bonus * document.getElementById(PayParticipant).value * document.getElementById(TourLength).value
        if (bonus == 1) {
            document.getElementById(seasonBonus).innerHTML = "none";
        }
        else {
            document.getElementById(seasonBonus).innerHTML = "x" + bonus;
        }

    }
    else {
        document.getElementById(seasonBonus).innerHTML = "none";
    }
    if (!parseInt(tot) > 0) {
        tot = 0;
    }

    document.getElementById(Total).innerHTML = tot;



}

function setSubTotal(TourLength, PayParticipant, SubTotal) {

    var tot = document.getElementById(TourLength).value * document.getElementById(PayParticipant).value;
    if (!parseInt(tot) > 0) {
        tot = 0;
    }
    document.getElementById(SubTotal).innerHTML = tot;
}

function setIncentive(TourLength, PayParticipant, SRBonus, TourYear, Incentive, Total, TxtTotal, currYear, globalEarn, quadEarn, tripleEarn, doubleEarn) {
    var bonus;
    bonus = document.getElementById(SRBonus).value;
    if (bonus <= 2 && bonus > 0) {
        bonus = 3;
    }
    else if (bonus == 3) {
        bonus = 2;
    }
    else if (bonus == 4) {
        bonus = 2;
    }
    else {
        bonus = 1;
    }

    var tot
    switch (parseInt(bonus)) {
        case 4:
            {
                if (document.getElementById(TourYear).value == currYear) {
                    document.getElementById(Incentive).innerHTML = globalEarn;
                    bonus = 1
                }
                else {
                    document.getElementById(Incentive).innerHTML = quadEarn;
                }

                break
            }
        case 3:
            {
                if (document.getElementById(TourYear).value == currYear) {
                    document.getElementById(Incentive).innerHTML = globalEarn;
                    bonus = 1
                }
                else {
                    document.getElementById(Incentive).innerHTML = tripleEarn;
                }
                break
            }
        case 2:
            {
                if (document.getElementById(TourYear).value == currYear) {
                    document.getElementById(Incentive).innerHTML = globalEarn;
                    bonus = 1
                }
                else {
                    document.getElementById(Incentive).innerHTML = doubleEarn;
                }
                break
            }
        default:
            {
                document.getElementById(Incentive).innerHTML = globalEarn;
                break
            }
    }
    if (document.getElementById(TourYear).value != 0) {
        tot = bonus * document.getElementById(PayParticipant).value * document.getElementById(TourLength).value
    }
    if (!parseInt(tot) > 0) {
        tot = 0;
    }
    document.getElementById(Total).innerHTML = tot;
    document.getElementById(TxtTotal).value = tot;


}

function setTourYear(TourYear, Incentive, currYear, globalEarn) {

    if (document.getElementById(TourYear).value == currYear) {
        document.getElementById(Incentive).innerHTML = globalEarn;
    }
}

/* All popup pages in GR Section */
function OpenPopupGR(url, width, height, left, top, resizeable, scrollbar) {
    var myHeight;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    }
    var mywindow = window.open(url, "EFTours", "width=" + width + ",height=" + myHeight + ",left=" + left + ",top=" + top + ",resizable=" + resizeable + ",scrollbars=" + scrollbar + ",status=1");
    mywindow.focus();
}


function SubmitRewards(id, url) {
    var rewards = document.getElementById(id);
    var index = rewards.selectedIndex;
    var selValue = rewards.options[index].value;
    if (selValue != 0) {
        OpenPopupGR(url, '610', '700', '100', '100', '1', '1');
    }
    return false;
}


function FillorClearGLDetails(ctl, fName, lName, add1, add2, cty, state, zip1, zip2, homePh1, homePh2, homePh3, wrkPh1, wrkPh2, wrkPh3, email) {
    if (ctl.checked) {
        document.getElementById(fName + 'Ctl').value = document.getElementById(fName).value;
        document.getElementById(lName + 'Ctl').value = document.getElementById(lName).value;
        document.getElementById(add1 + 'Ctl').value = document.getElementById(add1).value;
        document.getElementById(add2 + 'Ctl').value = document.getElementById(add2).value;
        document.getElementById(cty + 'Ctl').value = document.getElementById(cty).value;
        document.getElementById(state + 'Ctl').value = document.getElementById(state).value;
        document.getElementById(zip1 + 'Ctl').value = document.getElementById(zip1).value;
        document.getElementById(zip2 + 'Ctl').value = document.getElementById(zip2).value;
        document.getElementById(homePh1 + 'Ctl').value = document.getElementById(homePh1).value;
        document.getElementById(homePh2 + 'Ctl').value = document.getElementById(homePh2).value;
        document.getElementById(homePh3 + 'Ctl').value = document.getElementById(homePh3).value;
        document.getElementById(wrkPh1 + 'Ctl').value = document.getElementById(wrkPh1).value;
        document.getElementById(wrkPh2 + 'Ctl').value = document.getElementById(wrkPh2).value;
        document.getElementById(wrkPh3 + 'Ctl').value = document.getElementById(wrkPh3).value;
        document.getElementById(email + 'Ctl').value = document.getElementById(email).value;
    }
    else {
        document.getElementById(fName + 'Ctl').value = '';
        document.getElementById(lName + 'Ctl').value = '';
        document.getElementById(add1 + 'Ctl').value = '';
        document.getElementById(add2 + 'Ctl').value = '';
        document.getElementById(cty + 'Ctl').value = '';
        document.getElementById(state + 'Ctl').value = '';
        document.getElementById(zip1 + 'Ctl').value = '';
        document.getElementById(zip2 + 'Ctl').value = '';
        document.getElementById(homePh1 + 'Ctl').value = '';
        document.getElementById(homePh2 + 'Ctl').value = '';
        document.getElementById(homePh3 + 'Ctl').value = '';
        document.getElementById(wrkPh1 + 'Ctl').value = '';
        document.getElementById(wrkPh2 + 'Ctl').value = '';
        document.getElementById(wrkPh3 + 'Ctl').value = '';
        document.getElementById(email + 'Ctl').value = '';
    }
}


function ShowPreviousImage(thm1Ctl, tourImgId, thmbImgId, mImgLstId, tImgLstId, fileCount) {
    var tourImgCtl = document.getElementById(tourImgId);
    var thmbImgCtl = document.getElementById(thmbImgId);
    var mImgLst = document.getElementById(mImgLstId).value.split("^");
    var tImgLst = document.getElementById(tImgLstId).value.split("^");

    var idx = findIndexOfGivenValueInaArray(tImgLst, thm1Ctl.src); //tImgLst.findIndex(thm1Ctl.src);

    if (idx <= 0) { return false; }
    else {
        tourImgCtl.src = mImgLst[idx - 1];
        thm1Ctl.src = tImgLst[idx - 1];
        thmbImgCtl.src = tImgLst[idx];

    }
}

function ShowNextImage(thm2Ctl, tourImgId, thmbImgId, mImgLstId, tImgLstId, fileCount) {
    var tourImgCtl = document.getElementById(tourImgId);
    var thmbImgCtl = document.getElementById(thmbImgId);
    var mImgLst = document.getElementById(mImgLstId).value.split("^");
    var tImgLst = document.getElementById(tImgLstId).value.split("^");

    var idx = findIndexOfGivenValueInaArray(tImgLst, thm2Ctl.src); //tImgLst.findIndex(thm2Ctl.src);

    if (idx == tImgLst.length) { return false; }
    else {
        tourImgCtl.src = mImgLst[idx];
        thmbImgCtl.src = tImgLst[idx];
        if (++idx < tImgLst.length)
            thm2Ctl.src = tImgLst[idx];
        else
            thmbImgCtl.src = tImgLst[idx - 2];

    }
}


function findIndexOfGivenValueInaArray(arrLst, value) {
    var ctr = "";
    for (var i = 0; i < arrLst.length; i++) {
        if (value.toUpperCase().indexOf(arrLst[i].toUpperCase()) != -1) {
            return i;
        }

    }
    return ctr;
}
