// JavaScript Document

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; 
    for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document;
    if (d.images) { 
        if(!d.MM_p) d.MM_p=new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; 
        for(i=0; i<a.length; i++)
            if (a[i].indexOf("#") != 0) {
                d.MM_p[j] = new Image;
                d.MM_p[j++].src = a[i];
            }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x;
    if (!d) d = document; 
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document;
        n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; 
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); 
    return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function changeStyleClass(test, sty) {
    test.className = sty;
}

function changeStyleAll(theMan, cName, wide, tall, margTop, margRight, margBottom, margLeft) {
   
    if (cName != '') {theMan.className = cName;};

    theMan.style.width = wide;
    theMan.style.height = tall;
    theMan.style.marginTop = margTop;
    theMan.style.marginRight = margRight;
    theMan.style.marginBottom = margBottom;
    theMan.style.marginLeft = margLeft;

}

function changeStyleSize(theMan, cName, wide, tall, margTop, margRight, margBottom, margLeft, onOff, topLink, bottomLink) {

    if (cName != '') { theMan.className = cName; };

    theMan.style.width = wide;
    theMan.style.height = tall;
    theMan.style.marginTop = margTop;
    theMan.style.marginRight = margRight;
    theMan.style.marginBottom = margBottom;
    theMan.style.marginLeft = margLeft;
    if (onOff == true) {
        topLink.visibility = false;
        bottomLink.visibility = true;
    }
    else {
        topLink.visibility = true;
        bottomLink.visibility = false;
    }
}


function newWindow(mypage, myname, w, h) {
    var win = null;
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += 'resizable=1,scrollbars=1,status=0,toolbar=0,menubar=0,location=0,titlebar=0';
    win = window.open(mypage, myname, settings);
    win.window.focus();
}

function PrintPage() { window.print(); }

function C_Changed(typeControl, position) {
    var errorMessage, acres, harvestPrice, springPrice, actualYield, conFocus, altFocus, errorFlag, percentYield, percentPrice, expectedYield, harvestCheck, actualCheck;   
    var fieldPre = "ctl00_mainContent_";
    var proceed = false;
    var tempCont = document.getElementById(fieldPre + 'btnRefresh');
    if (typeControl == "Acres") {
        conFocus = document.getElementById(fieldPre + 'txtAcres');
        altFocus = document.getElementById(fieldPre + 'Acres');
        acres = conFocus.value;
        if (acres == parseInt(acres)) {
            if (acres > 0) { errorFlag = false; conFocus.value = Math.round(conFocus.value); }
            else {
                errorFlag = true;
                conFocus.value = altFocus.value;
                errorMessage = "Acres must be a number greater than 0.  Please adjust your selection and try again.";
            }
        }
        else {
            errorFlag = true;
            conFocus.value = altFocus.value;
            errorMessage = "Acres must be a whole number.  Please adjust your selection and try again.";
        }
    }
    else if (typeControl == "Price") {
        harvestCheck = document.getElementById(fieldPre + 'chkHarvest').checked;
        conFocus = document.getElementById(fieldPre + 'txtHarvestPrice_' + position);
        altFocus = document.getElementById(fieldPre + 'HarvestPrice_' + position);
        harvestPrice = conFocus.value;
        if (harvestPrice == parseFloat(harvestPrice)) {
            if (harvestPrice > 0) {
                errorFlag = false;
                conFocus.value = parseFloat(conFocus.value).toFixed(2);
                if (harvestCheck == true) {
                    var cropID = new Array(4);
                    var cropPrice = new Array(4);
                    var aCount = 0;
                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + position).value;
                    cropPrice[aCount] = parseFloat(conFocus.value).toFixed(2);
                    aCount++;
                    for (var i = 1; i <= 4; i++) {
                        if (i != position) {
                            if ((document.getElementById(fieldPre + 'Visible_' + i)) && (document.getElementById(fieldPre + 'txtHarvestPrice_' + i))){                            
                                for (var t = 0; t <= aCount - 1; t++) {                                
                                    if (document.getElementById(fieldPre + 'CropID_' + i).value == cropID[t]) {
                                        document.getElementById(fieldPre + 'txtHarvestPrice_' + i).value = cropPrice[t];
                                        proceed = true;
                                        //alert("1st If in Array|Main Counter=" + i + "|Array Item Count=" + aCount + "|Inner Counter=" +t);                                                                                   
                                    }
                                }                            
                                if (proceed == false) {
                                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + i).value;
                                    cropPrice[aCount] = parseFloat(document.getElementById(fieldPre + 'txtHarvestPrice_' + i).value).toFixed(2);
                                    aCount++;
                                    //alert("2nd If|Main Counter=" + i + "|Array Item Count=" + aCount);
                                }
                            }
                        }
                    }
                }
            }            
            else {
                errorFlag = true;
                conFocus.value = parseFloat(altFocus.value).toFixed(2);
                errorMessage = "Harvest Price must be greater than 0.  Please adjust your selection on Quote #" + position + " and try again.";
            }
        }
        else {
            errorFlag = true;
            conFocus.value = parseFloat(altFocus.value).toFixed(2);
            errorMessage = "Harvest Price must be a number.  Please adjust the Harvest Price on Quote #" + position + " and try again.";
        }
    }
    else if (typeControl == "Yield") {
        actualCheck = document.getElementById(fieldPre + 'chkActual').checked;
        conFocus = document.getElementById(fieldPre + 'txtActualYield_' + position);
        altFocus = document.getElementById(fieldPre + 'ActualYield_' + position);
        actualYield = parseFloat(conFocus.value).toFixed(1);
        
        if (actualYield == parseFloat(actualYield)) {
            if (actualYield >= 0) {
                errorFlag = false;
                conFocus.value = actualYield;
                if (actualCheck == true) {
                    var cropID = new Array(4);
                    var cropYield = new Array(4);
                    var aCount = 0;

                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + position).value;
                    cropYield[aCount] = parseFloat(conFocus.value).toFixed(1);
                    aCount++;

                    for (var i = 1; i <= 4; i++) {
                        if (i != position) {
                            if (document.getElementById(fieldPre + 'Visible_' + i)) {
                                for (var t = 0; t <= aCount - 1; t++) {
                                    if (document.getElementById(fieldPre + 'CropID_' + i).value == cropID[t]) {
                                        document.getElementById(fieldPre + 'txtActualYield_' + i).value = cropYield[t];
                                        proceed = true;
                                        //alert("1st If in Array|Main Counter=" + i + "|Array Item Count=" + aCount + "|Inner Counter=" +t);                                                                                   
                                    }
                                }
                                if (proceed == false) {
                                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + i).value;
                                    cropYield[aCount] = parseFloat(document.getElementById(fieldPre + 'txtActualYield_' + i).value).toFixed(1);
                                    aCount++;
                                    //alert("2nd If|Main Counter=" + i + "|Array Item Count=" + aCount);                                                                                      
                                }
                            }

                        }
                        else { }
                    }
                }
            }
            else {
                errorFlag = true;
                conFocus.value = altFocus.value;
                errorMessage = "Actual Yield must be greater than 0.  Please adjust the Actual Yield for Quote #" + position + " and try again.";
            }
        }
        else {
            errorFlag = true;
            conFocus.value = altFocus.value;
            errorMessage = "Actual Yield must be a number.  Please adjust the Actual Yield on Quote #" + position + " and try again.";
        }
    }
    else if (typeControl == "Percent") {
        actualCheck = document.getElementById(fieldPre + 'chkActual').checked;
        conFocus = document.getElementById(fieldPre + 'txtYieldPercent_' + position);
        altFocus = document.getElementById(fieldPre + 'txtActualYield_' + position);
        expectedYield = document.getElementById(fieldPre + 'txtExpectedYield_' + position).value;
        percentYield = Math.round(conFocus.value.replace("%", ""));
        actualYield = altFocus.value;

        if (percentYield == parseFloat(percentYield)) {
            if (percentYield > -100) {
                errorFlag = false;
                conFocus.value = percentYield + "%";
                altFocus.value = parseFloat(((1 + percentYield / 100)) * expectedYield).toFixed(1);
                if (actualCheck == true) {
                    var cropID = new Array(4);
                    var cropPercent = new Array(4);
                    var aCount = 0;

                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + position).value;
                    cropPercent[aCount] = percentYield;
                    aCount++;

                    for (var i = 1; i <= 4; i++) {
                        if (i != position) {
                            if (document.getElementById(fieldPre + 'Visible_' + i)) {
                                conFocus = document.getElementById(fieldPre + 'txtYieldPercent_' + i);
                                altFocus = document.getElementById(fieldPre + 'txtActualYield_' + i);
                                expectedYield = document.getElementById(fieldPre + 'txtExpectedYield_' + i).value;
                                percentYield = Math.round(conFocus.value.replace("%", ""));
                                for (var t = 0; t <= aCount - 1; t++) {
                                    if (document.getElementById(fieldPre + 'CropID_' + i).value == cropID[t]) {
                                        percentYield = cropPercent[t];
                                        proceed = true;
                                    }
                                }
                                if (proceed == false) {
                                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + i).value;
                                    cropPercent[aCount] = percentYield;
                                    aCount++;
                                }
                                conFocus.value = percentYield + "%";
                                altFocus.value = parseFloat(((1 + percentYield / 100)) * expectedYield).toFixed(1);
                            }
                        }
                    }
                }
                else {
                    altFocus.value = parseFloat(((1 + percentYield / 100)) * expectedYield).toFixed(1);
                    conFocus.value = percentYield + "%";
                }
            }
            else {
                errorFlag = true;
                errorMessage = "Percent of Yield must be greater than -100%.  Please adjust the Percent of Yield for Quote #" + position + " and try again.";
            }
        }
        else {
            errorFlag = true;
            errorMessage = "Percent of Yield must be a number.  Please adjust the Percent of Yield for Quote #" + position + " and try again.";
        }
    }
    else if (typeControl == "PricePercent") {
        harvestCheck = document.getElementById(fieldPre + 'chkHarvest').checked;
        conFocus = document.getElementById(fieldPre + 'txtPricePercent_' + position);
        altFocus = document.getElementById(fieldPre + 'txtHarvestPrice_' + position);
        springPrice = document.getElementById(fieldPre + 'lblSpringPrice_' + position).innerHTML;
        percentPrice = Math.round(conFocus.value.replace("%", ""));
        harvestPrice = altFocus.value;

        if (percentPrice == parseFloat(percentPrice)) {
            if (percentPrice > -100) {
                errorFlag = false;
                conFocus.value = percentPrice + "%";
                altFocus.value = parseFloat(((1 + percentPrice / 100)) * springPrice).toFixed(2);
                if (harvestCheck == true) {
                    var cropID = new Array(4);
                    var cropPercent = new Array(4);
                    var aCount = 0;

                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + position).value;
                    cropPercent[aCount] = percentPrice;
                    aCount++;

                    for (var i = 1; i <= 4; i++) {
                        if (i != position) {
                            if ((document.getElementById(fieldPre + 'Visible_' + i)) && (document.getElementById(fieldPre + 'txtHarvestPrice_'+ i))){                                                      
                                conFocus = document.getElementById(fieldPre + 'txtPricePercent_' + i);
                                altFocus = document.getElementById(fieldPre + 'txtHarvestPrice_' + i);
                                springPrice = document.getElementById(fieldPre + 'lblSpringPrice_' + i).innerHTML;
                                percentPrice = Math.round(conFocus.value.replace("%", ""));
                                for (var t = 0; t <= aCount - 1; t++) {
                                    if (document.getElementById(fieldPre + 'CropID_' + i).value == cropID[t]) {
                                        percentPrice = cropPercent[t];
                                        proceed = true;
                                    }
                                }
                                if (proceed == false) {
                                    cropID[aCount] = document.getElementById(fieldPre + 'CropID_' + i).value;
                                    cropPercent[aCount] = percentPrice;
                                    aCount++;
                                }
                                conFocus.value = percentPrice + "%";
                                altFocus.value = parseFloat(((1 + percentPrice / 100)) * springPrice).toFixed(2);
                            }
                        }
                    }
                }
                else {
                    altFocus.value = parseFloat(((1 + percentPrice / 100)) * springPrice).toFixed(2);
                    conFocus.value = percentPrice + "%";
                }
            }
            else {
                errorFlag = true;
                errorMessage = "Percent of Spring Price must be greater than -100%.  Please adjust the Percent of Spring Price for Quote #" + position + " and try again.";
            }
        }
        else {
            errorFlag = true;
            errorMessage = "Percent of Spring Price must be a number.  Please adjust the Percent of Spring Price for Quote #" + position + " and try again.";
        }
    }
    else if (typeControl == "ECY") {
        conFocus = document.getElementById(fieldPre + 'txtExpectedYield_' + position);
        altFocus = document.getElementById(fieldPre + 'ECY_' + position);
        expectedYield = conFocus.value;
        if (expectedYield == parseFloat(expectedYield) && expectedYield > 0){
            errorFlag = false;
            conFocus.value = parseFloat(expectedYield).toFixed(1);
        }
        else {
            errorFlag = true;
            conFocus.value = parseFloat(altFocus.value).toFixed(1);
            errorMessage = "Expected Yield/APH must be a number greater than 0.  Please asdjust the Expected County Yield for Quote #" + position + " and try again.";
         }
    }
    
    if (errorFlag == true) {alert(errorMessage); tempCont.click();  }
    else { tempCont.click(); }
    
    //if (errorFlag == true) { alert(errorMessage); }
    //return errorFlag;
    
}



        
 