﻿/* TextAreaTools.js
* Tools to constrain the line width and rows of text boxes and alter pricing based on row count
*/
nucTextCost=0.00;
nucStart=0;
function FormatTextBox(textBox, maxrowlength, maxrows, firstlinecost, additionallinecost)
{
	if(textBox==null){
		textBox=document.additem.xfield1
		}
	
	//alert(document.additem.xfield1.value);
    var CurrentValue = textBox.value;
	
    CurrentValue = CurrentValue.replace("\r\n", "\n");
    CurrentValue = CurrentValue.replace("\r", "\n");
    CurrentValue = CurrentValue.replace("\n\n", "\n");
    var LineArray = CurrentValue.split("\n");
    var currentline;
    var temptail;
    var temphead;
    var changed = false;
    var TextCost = 0;
    var TrimmedArray;
    var currentlinelength;
    if (maxrowlength > 0)
    {
        for (loop = 0; loop < LineArray.length; loop++)
        {
            currentline = LineArray[loop];
            currentlinelength = currentline.length;
            if (currentlinelength > maxrowlength)
            {
                changed = true;
                temptail = currentline.substring(maxrowlength);
                temphead = currentline.substring(0, maxrowlength);
                LineArray[loop] = temphead;
                if (LineArray[loop + 1] == undefined)
                    LineArray[loop + 1] = temptail;
                else
                    LineArray[loop + 1] = temptail + LineArray[loop + 1];
            }
        }
    }
    TrimmedArray = LineArray;
    if (maxrows > 0 && LineArray.length > maxrows)
    {
        changed = true;
        TrimmedArray = LineArray.slice(0, maxrows);
    }
    if (TrimmedArray[0].length > 0)
    {
        TextCost = firstlinecost;
    }
    if (TrimmedArray.length > 1)
    {
        TextCost = (TextCost*1) + (TrimmedArray.length - 1) * additionallinecost;
    }
    if (changed)
        textBox.value = TrimmedArray.join("\n");
    
    var xfieldcostinput = document.getElementsByName(textBox.name + "cost");
	nucTextCost=TextCost*1;
    xfieldcostinput[0].value = TextCost;
	
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	
		if(aspPage=='configurePrd.asp' || aspPage=='Reconfigure.asp' || aspPage=='bto_Reconfigure.asp'){
			//see if read only
			
			if(textBox.readOnly!=true){
				
				New_calculateAll();
			}
		}
		else
		{
			if(aspPage=='viewPrd.asp'){
	  //		if(nucStart==0){
		//			nucBasePrice=document.getElementById("nucprice").value*1;	
		//		}
		//		nucStart=1;
		//		var nucPP=document.getElementById("nucprice").value*1;
		//    alert(nucBasePrice+" "+nucPP);
		//		var nucUPrice=nucBasePrice+nucTextCost;
		//		nucUPrice=nucUPrice.toFixed(2)
		//		document.getElementById("nucprice").value=nucUPrice;
				
		  curAmt=document.additem.currentValue0.value;
			totalAmt=New_FormatNumber(parseFloatEx(curAmt) + parseFloatEx(nucTextCost));
			document.additem.TLtotal.value = New_FormatNumber(parseFloatEx(nucTextCost));
			document.additem.TLGrandTotal.value = totalAmt;
		  document.additem.GrandTotal.value = totalAmt;
		  document.additem.TotalWithQD.value = totalAmt;

			}
			
			
	}
	//alert(aspPage);
}
