// JavaScript Document


function CalcCost(frm) {
// three lines added 11-2-2010 to handle Audio (and Kindle) calculations below
    var BaseAudio = 14.00;  // amount to charge per book and dbook for the Audio option
    var BaseKindle = 12.00; //not used yet - Kindle option amount per book and dbook. Must uncomment the "AddOn5" section below
    var tmpcost = 0.0;      // temp variable used for calculations. Do not modify.


// Cost for Non-Destructive Book Count
    if (!isNaN(frm.books.value) && frm.books.value > 0) {
        frm.bookscost.value = ((frm.books.value*1) * frm.bookfee.value+15.00).toFixed(2);
    }else{
        frm.books.value = '0';
        frm.bookscost.value = '0.00';
    }
// Cost for Non-Destructive Pages
    if (!isNaN(frm.pages.value) && frm.pages.value > 49) {
        frm.pagescost.value = ((frm.pages.value*1) * frm.pagefee.value-5).toFixed(2);
    }else if (!isNaN(frm.pages.value) && frm.pages.value > 0) {
        // first 50 pages are free
        frm.pagescost.value = '0.00';
    }else{
        frm.pages.value = '0';
        frm.pagescost.value = '0.00';
    }
// Cost for Destructive Book Count
   if (!isNaN(frm.dbooks.value) && frm.dbooks.value > 0) {
        frm.dbookscost.value = ((frm.dbooks.value*1) * frm.dbookfee.value+8.00).toFixed(2);
    }else{
        frm.dbooks.value = '0';
        frm.dbookscost.value = '0.00';
    }
// Cost for Destructive Pages
    if (!isNaN(frm.dpages.value) && frm.dpages.value > 49) {
        frm.dpagescost.value = ((frm.dpages.value*1) * frm.dpagefee.value-3.50).toFixed(2);
    }else if (!isNaN(frm.dpages.value) && frm.dpages.value > 0) {
        // first 50 pages are free
        frm.dpagescost.value = '0.00';
    }else{
        frm.dpages.value = '0';
        frm.dpagescost.value = '0.00';
    }

// cost for options - added 10-20-2010
    var optcost = 0.0;
    var idx = 0;

    // for each option, verify it exists and is selected. If so, add the option amount
    if ((typeof(frm.AddOn2) == 'object') && (frm.AddOn2.checked == true)) {
        optcost += (AddAmount(frm.AddOn2.value) *1);
    }

    // for the select list, make sure we have a selected index
    if ((typeof(frm.AddOn3) == 'object') && (frm.AddOn3.selectedIndex > 0)) {
        optcost += (AddAmount(frm.AddOn3.options[frm.AddOn3.selectedIndex].value) *1);
    }

    if ((typeof(frm.AddOn4) == 'object') && (frm.AddOn4.checked == true)) {

	// 11-2-2010 add amount x book count and/or dbook count
        // initialize tmpcost each time or it will accumulate incorrectly
        tmpcost = 0.00;
        if ((BaseAudio > 0) && (!isNaN(frm.books.value) && frm.books.value > 0)) {
            // set the base amount x book count
            tmpcost += ((frm.books.value) * (BaseAudio) * 1)
        }
        // 11-2-2010 - repeat for dbook count
        if ((BaseAudio > 0) && (!isNaN(frm.dbooks.value) && frm.dbooks.value > 0)) {
            // add the base amount x dbook count
            tmpcost += ((frm.dbooks.value) * (BaseAudio) * 1)
        }
        // 11-2-2010 - update the AddOn with the new amount
        frm.AddOn4.value = UpdateAmount(frm.AddOn4.value, tmpcost);


        optcost += (AddAmount(frm.AddOn4.value) *1); 

    }

    if ((typeof(frm.AddOn5) == 'object') && (frm.AddOn5.checked == true)) {

        // 11-2-2010 - to have the "Kindle" option behave like the "Audio" option, un-comment out the below 8 lines

        /*
        tmpcost = 0.00;
        if ((BaseKindle > 0) && (!isNaN(frm.books.value) && frm.books.value > 0)) {
            tmpcost += ((frm.books.value) * (BaseKindle) * 1)
        }
        if ((BaseKindle > 0) && (!isNaN(frm.dbooks.value) && frm.dbooks.value > 0)) {
            tmpcost += ((frm.dbooks.value) * (BaseKindle) * 1)
        }
        frm.AddOn5.value = UpdateAmount(frm.AddOn5.value, tmpcost);
        */        

	optcost += (AddAmount(frm.AddOn5.value) *1);

	
    }

    if ((typeof(frm.AddOn6) == 'object') && (frm.AddOn6.checked == true)) {
        optcost += (AddAmount(frm.AddOn6.value) *1);
    }

    if ((typeof(frm.AddOn7) == 'object') && (frm.AddOn7.checked == true)) {
        optcost += (AddAmount(frm.AddOn7.value) *1);
    }
	
	if ((typeof(frm.AddOn10) == 'object') && (frm.AddOn10.checked == true)) {
        optcost += (AddAmount(frm.AddOn10.value) *1);
    }

    if ((typeof(frm.AddOn8) == 'object') && (frm.AddOn8.checked == true)) {

        // 11-4-2010 set the DVD option based on total page count
        var pageTotal = 0;
        // accumulate the total page count
        if (!isNaN(frm.pages.value)) {
          pageTotal += (frm.pages.value * 1);
        }
        if (!isNaN(frm.dpages.value)) {
          pageTotal += (frm.dpages.value * 1);
        }

       
        /*DVD disk - see bottom of page for 5,000-page cap
        */ 
	if (pageTotal > 8000) {
          frm.AddOn8.value = UpdateAmount(frm.AddOn8.value, 109.00)
          optcost += 109.00;
        } else if (pageTotal > 4000) {
          frm.AddOn8.value = UpdateAmount(frm.AddOn8.value, 86.00)
          optcost += 86.00;
        } else if (pageTotal > 3000) {
          frm.AddOn8.value = UpdateAmount(frm.AddOn8.value, 52.00)
          optcost += 52.00;
        } else  {
          frm.AddOn8.value = UpdateAmount(frm.AddOn8.value, 36.00)
          optcost += 36.00;
        } 
    }

    // for the radio button array, we need to check each array element
    if ((typeof(frm.NAddOn1) == 'object') && (frm.NAddOn1.length > 0)) {
        for(idx = 0; idx < frm.NAddOn1.length; idx++) {
            if (frm.NAddOn1[idx].checked == true) {
              optcost += (AddAmount(frm.NAddOn1[idx].value) *1);
              break;
            }
        }
    }

    frm.BasePrice.value = ((frm.bookscost.value * 1) + (frm.pagescost.value*1) + (frm.dbookscost.value * 1) + (frm.dpagescost.value*1)).toFixed(2);
    frm.total.value = ((frm.bookscost.value * 1) + (frm.pagescost.value*1) + (frm.dbookscost.value * 1) + (frm.dpagescost.value*1) + (optcost * 1)).toFixed(2);

}

function AddAmount(value)
{
  // added 10-20-2010
  // parse "amount" from "value"; if "amount" is a positive number, return it. Otherwise, return the default zero

   var Amt = 0.0;//initialize as number
   var Temp = ParseAmount(value); 
  
  // if there is a positive numeric amount, assign it to the return variable
  if (!isNaN(Temp) && Temp > 0) {
    Amt = (Temp * 1).toFixed(2);
  }

  // return the results
  return Amt;
}

function ParseAmount(value)
{
  // added 10-20-2010 - utility function to actually parse the "amount" portion from the "value" portion

  
  // make sure we have a string to parse
  if (typeof(value) == 'string')
  {
    // split value into an array
    var TempList = value.split('^');
    // if there is an amount, it will be in element 1 of the array
    if (TempList.length > 1) {
        // "amount array" element exists, trim spaces and return
        return trim(TempList[1]);
    } else {
        // no "amount" found, return empty string
        return '';
    }
  } else {
      // no ""value", return empty string
      return '';
  }
}

function trim(str)
{
  // added 10-20-2010 - utility function to insure no leading or trailing spaces
  // ref: http://blog.stevenlevithan.com/archives/faster-trim-javascript
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


function UpdateAmount(value,amt)
{
// added 11-2-2010 - update the "^" amt

  // make sure we have a string to parse
  if (typeof(value) == 'string')
  {
    // split value into an array
    var TempList = value.split('^');
    // if there is an amount, it will be in element 1 of the array
    if (TempList.length > 1) {
        // clear "amount array" 
        TempList[1] = amt;
        // concatonate array back to string using "^" delimiter
        return (TempList.join("^"));
    } else {
        // didn't previously exist so create it
        return value + '^' + amt;
    }

  } else {
      // no ""value", return empty string
      return '';
  }

}

function getPos(value,offset)
{

 // added 11-2-2010 - locate the position of the "^" in the string
 var i = 0;   // character position of the "^" if found
 var cnt = 0; // "^" counter
 
 for(i = 0; i < value.length; i++) 
 {
  if (value.charAt(i) == '^') 
  {
    cnt += 1;
    if (cnt == offset)
    {
      return i;
    }
  }
 }
 return i;
}


function PackageForm(frm) {
    
    var emsg = '';

    /*
    new stuff 8-31-2010 to verify book and page count
    ========================
    General JavaScript notes
    ========================
    - "isNAN" is the JavaScript "Is Not A Number" function to test whether or not a value is a number.
    - the "!" is the logic "not" operator
      So "!isNAN" literally tests if the value is not, not a number. 
      In human-speak, "!isNAN" test if a value *is* a number

    - "&&" is the logic "And" operator. 
      
    - "||" is the logic "OR" operator. 

    - "==" is a test for "equal", "!=" is the test for "not equal"
    */


    /* added 2-8-2011 */
    var BookCnt = 0; // initial value
    if (!isNaN(frm.books.value) && frm.books.value > 0) {
        // accumulate non-destructive books
        BookCnt += (frm.books.value * 1);
    }

    if (!isNaN(frm.dbooks.value) && frm.dbooks.value > 0) {
        // accumulate destructive books
        BookCnt += (frm.dbooks.value * 1);
    }

    // check accumulated book count limit
    if (BookCnt > 30) {
        emsg += "Sorry! Maximum number of books per order is 30.\n\n";
    }


    // test the non-destructive book/pages combinations
    if ((!isNaN(frm.books.value) && frm.books.value > 0) && (isNaN(frm.pages.value) || (!isNaN(frm.pages.value) && frm.pages.value < 1))) {
        // books is a number ANDALSO it is greater than zero ANDALSO pages is either not a number OR a number less than zero 
        emsg += "You must enter the total page count for Non-Destructive scanning before continuing.\n\n";
    }else if ((!isNaN(frm.pages.value) && frm.pages.value > 0) && (isNaN(frm.books.value) || (!isNaN(frm.books.value) && frm.books.value < 1))) {
        // pages is a number ANDALSO the number is greater than zero ANDALSO books is either not a number OR a number less than zero 
        emsg += "You must enter the total book count for Non-Destructive scanning before continuing.\n\n";
    }
    

    // repeat for destructive book/page combinations
    if ((!isNaN(frm.dbooks.value) && frm.dbooks.value > 0) && (isNaN(frm.dpages.value) || (!isNaN(frm.dpages.value) && frm.dpages.value < 1))) {
        // books is a number ANDALSO it is greater than zero ANDALSO pages is either not a number OR a number less than zero 
        emsg += "You must enter the total page count for Destructive scanning before continuing.\n\n";
    }else if ((!isNaN(frm.dpages.value) && frm.dpages.value > 0) && (isNaN(frm.dbooks.value) || (!isNaN(frm.dbooks.value) && frm.dbooks.value < 1))) {
        // pages is a number ANDALSO the number is greater than zero ANDALSO books is either not a number OR a number less than zero
        emsg += "You must enter the total book count for Destructive scanning before continuing.\n\n";
    }


    // there are no invalid combinations; make sure there is at least one valid combination 
    if ((emsg == '') && (((frm.books.value * 1) + (frm.pages.value * 1) + (frm.dbooks.value * 1) + (frm.dpages.value * 1)) < 2)) {
        // emsg EQUALS empty so there were no explicitly invalid combinations but there are no valid combinations either
        emsg += "Please enter the total number of books and pages you require scanned.\n\n";
    }

    
	
    // start new code 1-20-2011 DVD drive
    if ((typeof(frm.AddOn8) == 'object') && (frm.AddOn8.checked == true)) {

        // accumulate page count
        var pageTotal = 0;
        // accumulate the total page count
        if (!isNaN(frm.pages.value)) {
          pageTotal += (frm.pages.value * 1);
        }
        if (!isNaN(frm.dpages.value)) {
          pageTotal += (frm.dpages.value * 1);
        }

        if (pageTotal > 3000) {
          emsg += "Sorry! DVD disk cannot contain more than 3,000 pages.\n\n";
        }
    }
    // end new code 1-20-2011

	
    // if emsg NOT EQUAL to empty, display message; otherwise, allow post
    if (emsg != '') {
        alert(emsg);
        return false;

    }else{
        CalcCost(frm);
        frm.AddOn1.value = "books(ND)="+frm.books.value+", pages(ND)="+frm.pages.value+", books(D)="+frm.dbooks.value+", pages(D)="+frm.dpages.value;
        frm.Price.value = frm.BasePrice.value;
        return true;
    }
}



