var aAnswers = new Array();
var spnCost = ''
var iGPrice = 0
var gclassName = 'wiztdwhite'

// init values
aAnswers[0] = ''
aAnswers[1] = 1 // farms
aAnswers[2] = '' // storage
aAnswers[3] = '' // products
aAnswers[4] = '' // calendar
aAnswers[5] = '' // risk assessment
aAnswers[6] = '' // placards
aAnswers[7] = '' //copy storage
aAnswers[8] = '' // copy treatments
aAnswers[9] = '' // copy between farms
aAnswers[10] = '' // description
aAnswers[11] = '' //price
aAnswers[12] = '' //accid
aAnswers[13] = '' //prodtype
aAnswers[14] = '' //description



function calculateOptions(btn, showPrice) {

    aAnswers[1] = document.getElementById("selFarm").value  // farms
    aAnswers[2] = document.getElementById("selStorage").value  // storage
    aAnswers[3] = document.getElementById("selProducts").value  // products

    if (showPrice) {

        if (document.getElementById("selFarm").value == 0) {
            alert('Please select number of premises')
            return
        }
        /*if (document.getElementById("selStorage").value == 0) {
            alert('Please select number of storage areas')
            return
        } */
        if (document.getElementById("selProducts").value == 0) {
            alert('Please select number of products stored in your storage')
            return
        }
        // checking of storage values should be greater than or equal to number of farm
       /* if (document.getElementById("selStorage").value > 0 && document.getElementById("selFarm").value > 0) {
            if (document.getElementById("selStorage").value < document.getElementById("selFarm").value) {
                alert('Number of storage areas should be at least ' + document.getElementById("selFarm").value)
                return
            }
        }       */
    }
    if (document.getElementById("chkCalendar").checked)
        aAnswers[4] = 1 // calendar
    else
        aAnswers[4] = 2 // calendar

    if (document.getElementById("chkRisksA").checked)
        aAnswers[5] = 1 // risk assessment
    else
        aAnswers[5] = 2 // risk assessment

    if (document.getElementById("chkPlacards").checked)
        aAnswers[6] = 1 // placards
    else
        aAnswers[6] = 2 // placards

    if (document.getElementById("chkMoveStorage").checked)
        aAnswers[7] = 1 //copy storage
    else
        aAnswers[7] = 2 //copy storage


    var substype = decideSubscription()
    if (showPrice) {
        computeWizPrice(aAnswers[12], aAnswers[13], "wizspnCost")
    }

    if (btn != null) {
        if (btn.value == 'FIND') {
            btn.style.display = 'none'
            document.getElementById("btnChange").style.display = ''
        }
        resetAll();

    }



}


function selfarm_Change(objList) {
    return
}


function decideSubscription() {
    // alert(aAnswers)
    aAnswers[12] = ''
    aAnswers[13] = ''
    aAnswers[14] = ''
    if (aAnswers[1] == 1 && aAnswers[2] == 0 && aAnswers[3] == 1 && aAnswers[4] == 2 && aAnswers[5] == 2 && aAnswers[6] == 2 && aAnswers[7] == 2) { // Single premises basic
        aAnswers[12] = '160'
        aAnswers[13] = 'BASIC'
        aAnswers[14] = 'Single Premises '
    } else if ((aAnswers[1] == 1 && aAnswers[3] == 2 && aAnswers[5] == 2 && aAnswers[6] == 2) ||
                (aAnswers[1] == 1 && aAnswers[2] >= 0 && aAnswers[3] != 3 && aAnswers[5] == 2 && aAnswers[6] == 2 && (aAnswers[4] == 1 || aAnswers[7] == 1)) ||
                (aAnswers[1] == 1 && aAnswers[2] >= 0 && aAnswers[3] != 3 && aAnswers[5] == 2 && aAnswers[6] == 2)
            ) {
        //(aAnswers[1] == 1 && aAnswers[2] >= 0 && aAnswers[3] == 2 && aAnswers[4] == 2 && aAnswers[5] == 2) ||
        //(aAnswers[1] == 1 && aAnswers[2] > 0 && aAnswers[3] == 1 && aAnswers[4] == 2 && aAnswers[5] == 2)
        // Single premises pro
        aAnswers[12] = '160'
        aAnswers[13] = 'PROFESSIONAL'
        aAnswers[14] = 'Single Premises '

    } else if ((aAnswers[1] == 1 && (aAnswers[3] == 3 || aAnswers[5] == 1 || aAnswers[6] == 1))) { // Single premises ultimate
        //computeWizPrice(60, 'ULTIMATE', "wizspnCost")
        aAnswers[12] = '160'
        aAnswers[13] = 'ULTIMATE'
        aAnswers[14] = 'Single Premises '

    } else if (aAnswers[1] > 1 && aAnswers[3] == 1 && aAnswers[4] == 2 && aAnswers[4] == 2 && aAnswers[5] == 2 && aAnswers[6] == 2 && aAnswers[7] == 2) { // Multi premises basic
        aAnswers[12] = '170'
        aAnswers[13] = 'BASIC'
        aAnswers[14] = 'Multiple Premises '
    } else if ((aAnswers[1] > 1 && aAnswers[3] == 2 && aAnswers[5] == 2 && aAnswers[6] == 2) ||
                (aAnswers[1] > 1 && aAnswers[2] >= 0 && aAnswers[3] != 3 && aAnswers[3] != 3 && aAnswers[5] == 2 && aAnswers[6] == 2 && (aAnswers[4] == 1 || aAnswers[7] == 1)) ||
                (aAnswers[1] > 1 && aAnswers[2] >= 0 && aAnswers[3] != 3 && aAnswers[5] == 2 && aAnswers[6] == 2)
            ) { // Multi premises pro
        aAnswers[12] = '170'
        aAnswers[13] = 'PROFESSIONAL'
        aAnswers[14] = 'Multiple Premises '
    } else if ((aAnswers[1] > 1 && (aAnswers[3] == 3 || aAnswers[5] == 1 || aAnswers[6] == 1))) { // Multi premises ultimate
        aAnswers[12] = '170'
        aAnswers[13] = 'ULTIMATE'
        aAnswers[14] = 'Multiple Premises '
    }
}


function gototab(accid) {
    if (aAnswers[12] == '170')
        showTab('ms_accntInfo_tabView', 3)
    else
        showTab('ms_accntInfo_tabView', 2)
}

function appendtoTable() {

    var mainTable = document.getElementById("mainSubsTable")
    var newtr = document.createElement('tr')
    var sLink = ''

    if (aAnswers[12] == '170')
        sLink = 'Index_FMRegister.asp'
    else
        sLink = 'Index_FSRegister.asp'
    sLink = "#"
            
    //col Sub type
    var newtd = document.createElement('td')
    newtd.className = gclassName
    var newtdData = document.createElement('span')
    newtdData.innerHTML = '<a  href="' + sLink + '" onclick="gototab(' +aAnswers[12] +')">' + aAnswers[14] + aAnswers[13] + '</a>'
    newtd.appendChild(newtdData)
    newtr.appendChild(newtd)


    //col farm
    var newtd = document.createElement('td')
    newtd.setAttribute('align', 'center')
    newtd.className = gclassName
    var newtdData = document.createElement('span')
    newtdData.innerHTML = aAnswers[1]
    newtd.appendChild(newtdData)
    newtr.appendChild(newtd)


    //col storage
    var newtd = document.createElement('td')
    newtd.setAttribute('align', 'center')
    newtd.className = gclassName
    var newtdData = document.createElement('span')
    newtdData.innerHTML = aAnswers[2]
    newtd.appendChild(newtdData)
    newtr.appendChild(newtd)


    //col products
    var newtd = document.createElement('td')
    newtd.setAttribute('align', 'center')
    newtd.className = gclassName
    var newtdData = document.createElement('span')
    if (aAnswers[3] == 1)
        newtdData.innerHTML = 'Up to 35'
    else if (aAnswers[3] == 2)
        newtdData.innerHTML = '36 to 100'
    else if (aAnswers[3] == 3)
        newtdData.innerHTML = 'More than 100'

    newtd.appendChild(newtdData)
    newtr.appendChild(newtd)

    //col features
    var sData = ''
    var newtd = document.createElement('td')
    var newtdData = document.createElement('span')
    newtd.setAttribute('nowrap', 'true')
    newtd.setAttribute('width', '110px')
    newtd.className = gclassName
    /*sData = '<span class="wizfeatdata">Calendar</span>'
    if (aAnswers[4] == 1)
        sData += '<span class="wizfeatimg"><img alt="" src="../images/Y.gif"  /></span><br />'
    else
        sData += '<span class="wizfeatimg"><img alt="" src="../images/N.gif"  /></span><br />'
    */    
    sData += '<span class="wizfeatdata">Risk Assessment</span>'
    if (aAnswers[5] == 1)
        sData += '<span class="wizfeatimg"><img alt="" src="../images/Y.gif"  /></span><br />'
    else
        sData += '<span class="wizfeatimg"><img alt="" src="../images/N.gif"  /></span><br />'
    
    sData += '<span class="wizfeatdata">Placards</span>'
    if (aAnswers[6] == 1)
        sData += '<span class="wizfeatimg"><img alt="" src="../images/Y.gif"  /></span><br />'
    else
        sData += '<span class="wizfeatimg"><img alt="" src="../images/N.gif"  /></span><br />'

    sData += '<span class="wizfeatdata">Copy & Move Data</span>'
    if (aAnswers[7] == 1)
        sData += '<span class="wizfeatimg"><img alt="" src="../images/Y.gif"  /></span><br />'
    else
        sData += '<span class="wizfeatimg"><img alt="" src="../images/N.gif"  /></span><br />'    

    newtdData.innerHTML = sData
    newtd.appendChild(newtdData)
    newtr.appendChild(newtd)

    //col price
    var newtd = document.createElement('td')
    newtd.className = gclassName
    var newtdData = document.createElement('span')
    newtdData.innerHTML = '$' + aAnswers[11] + '<br /><br />' + '<input name="Button2" type="button" value="Subscribe" class="imgbutton" '
                              + ' onclick="proceedSubscription(' + aAnswers[12] + ',\'' + aAnswers[14] + '\',\'' + aAnswers[13]
                              + '\',' + aAnswers[1] + ',' + aAnswers[2] + ')">'
    newtd.appendChild(newtdData)
    newtr.appendChild(newtd)

    mainTable.tBodies[0].appendChild(newtr)
    if (gclassName == 'wiztdwhite')
        gclassName = "wiztdgray"
    else
        gclassName = "wiztdwhite"
}


function computeWizPrice(sAccID, sSubType, pspnCost) {
    var iFarms = 0, iStorages = 0, sCouponNo = '';
    iFarms = document.getElementById("selFarm").value
    iStorages = document.getElementById("selStorage").value

    /*if (document.getElementById("selStorage").value == "")
        iStorages = iFarms
    else
        iStorages = (iFarms*1) + (document.getElementById("selStorage").value*1)
    */

    spnCost = pspnCost
    var sParm = sAccID + "^" + iFarms + "^" + iStorages + "^" + sCouponNo + "^" + sSubType
    //alert(sParm)
    jsrsExecute("../rmtcalls/rmtServer.asp", fcomputeWizPrice, "getRmt", Array(sParm, "computePrice"), false);

}

function fcomputeWizPrice(sReturn) {
    if (sReturn.indexOf('No cost') > -1) {
        alert('No price setup for this range. Please input another value.');
        return;
    }
    var myArray = sReturn.split("^");
    //document.getElementById(spnCost).innerHTML = myArray[2]
    iGPrice = myArray[2]
    aAnswers[11] = myArray[2]
    appendtoTable()
    document.getElementById("mainSubsDiv").style.display = ''
}



function proceedSubscription(sAccID, sAccntType, sMaxProduct, iStoreMax, iStorageMax) {

    //var oForm = document.getElementById("frmOptionswiz")
    var oForm = document.getElementById("frmOptions")
    var iMaxProduct = 0
    oForm.hdnAccID.value = sAccID
    oForm.hdnAccType.value = sAccntType
    oForm.StoreMax.value = iStoreMax
    oForm.StorageMax.value = iStorageMax 
    if (sMaxProduct == 'BASIC')
        iMaxProduct = 1
    else if (sMaxProduct == 'PROFESSIONAL')
        iMaxProduct = 2
    else if (sMaxProduct == 'ULTIMATE')
        iMaxProduct = 3
    oForm.hdnMaxProduct.value = iMaxProduct

    if (oForm.hdnAccID.value != "")
        oForm.submit()
}

function getFeatures(objList) {
    resetfeatures()
    if (objList.id == 'selStorage') { // if no of storages
        if (objList.value > 0 && document.getElementById("selProducts").value <= 1)
            document.getElementById("selProducts").value = 2
    }

    calculateOptions(null, false)
    // alert(aAnswers)
    if ((aAnswers[12] == '170' || aAnswers[12] == '160') && aAnswers[13] == 'ULTIMATE') {
        document.getElementById("chkRisksA").checked = true
        document.getElementById("chkPlacards").checked = true
    }
    if ((aAnswers[12] == '170' || aAnswers[12] == '160') && (aAnswers[13] == 'ULTIMATE' || aAnswers[13] == 'PROFESSIONAL')) {
        document.getElementById("chkCalendar").checked = true
        document.getElementById("chkMoveStorage").checked = true

    }
    if ((aAnswers[12] == '170') && (aAnswers[13] == 'ULTIMATE' || aAnswers[13] == 'PROFESSIONAL')) {
        document.getElementById("chkMoveStorage").checked = true

    }
}

function resetfeatures() {
    document.getElementById("chkRisksA").checked = false
    document.getElementById("chkPlacards").checked = false
    document.getElementById("chkCalendar").checked = false
    document.getElementById("chkMoveStorage").checked = false


}

function resetAll() {
    document.getElementById("selFarm").selectedIndex = 0
    document.getElementById("selStorage").selectedIndex = 0
    document.getElementById("selProducts").selectedIndex = 0
    
    
    document.getElementById("chkRisksA").checked = false
    document.getElementById("chkPlacards").checked = false
    document.getElementById("chkCalendar").checked = false
    document.getElementById("chkMoveStorage").checked = false


}
