Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Fields calc newfield data

Status
Not open for further replies.

Katiris

Programmer
Feb 23, 2005
25
0
0
US
I'm sure this has been addressed before but what I'm looking for is a way to calculate amount based on the number of days * perday amount. My fields are dynamic on the form as I do multiple updates with one submit.

This script was written with hardcoded fieldname and I need it to read the dynamic field name and perform the operation. Can any one guide me thru this. Javascript is not my strong point. My fields names on the form are like this ( From_#sectionid#, To_#sectionid#, perday#sectionid# and amount#sectionid# )

function calculate()
{

var errMsg ="";
if(document.Form.From_901.value=="")
{
errMsg = "Please enter From Date for Line# 901";
}

if(document.cForm.From_901.value=="")
{
errMsg = errMsg + "\nPlease enter To Date for Line# 901";
}

if(document.Form.perday_901.value!="" )
{}
else
{
errMsg = errMsg + "\nPlease enter Amount/Day for Line# 901";
}

if(errMsg!="")
{
alert(errMsg);
return false;
}


if(document.Form.perday_901.value!="" && document.Form.From_901.value!="" && document.Form.To_901.value!="" &&
validateUSDateWithoutAlert(document.Form.From_901)=="t" &&
validateUSDateWithoutAlert(document.Form.To_901)=="t")
{


//if(eval(getunformatCurrency(document.closing900ShortSectionForm.sectionId901PerDayAmount))<=0)
//{
// alert("Please enter the per/day amount for Line# 901");
// return false;
//}

var strFrom=document.Form.From_901.value;
var strTo=document.Form.To_901.value;
strFrom= strFrom.replace(/\s/gi,"");
strTo= strTo.replace(/\s/gi,"");
var d1=0;
var d2=0;
var strTo1 = "";
var fromDt = new Date (strFrom);
var toDt = new Date (strTo);

if(fromDt.getTime() >= toDt.getTime())
{
strTo1 = strFrom.substring(0,3);
strTo=strTo1 +"01" + strFrom.substring(5,strFrom.length);
}
else
{
var strMonth=strFrom.substring(0,2);
var strDay="";
if(strMonth=="01" || strMonth=="03" || strMonth=="05" || strMonth=="07" || strMonth=="08" || strMonth=="10" || strMonth=="12")
{
strDay="31";
}
else if(strMonth=="04" || strMonth=="06" || strMonth=="09" || strMonth=="11" )
{
strDay="30";
}
else
{
strDay="28";
}
strTo1 = strFrom.substring(0,3);
strTo=strTo1 + strDay + strFrom.substring(5,strFrom.length);
}
d1=strFrom.substring(3,5);
d2=strTo.substring(3,5);
/*var diffdates =eval(differenceOfDates(document.Form.From_901.value,document.Form.To_901.value));*/
var diffdates = d2-d1;//eval(differenceOfDates(strFrom,strTo));
if(diffdates > 0)
{
diffdates = diffdates + 1;
}
else
{
diffdates = diffdates - 1;
}

// Modified by Prathap on 16th sep 2004
/*if(eval(diffdates)<=0)
{
alert("From Date should always be less than To Date for Line# 901");
document.Form.From_901.value="";
document.Form.To_901.focus();
return false;
}*/
// End of the modifications.

var amtValue = eval(getunformatCurrency(document.Form.perday_901))*eval(diffdates);

if(eval(amtValue) > 999999999.99)
{
alert("Sorry, Amount Value cannot be greater than 999,999,999.99. Please reduce the per / day amount for Line# 901");
document.Form.perday_901.value = "";
document.Form.perday_901.focus();
return false;
}
else
{
/*document.cForm.Amount_901.value = eval(getunformatCurrency(document.Form.perday_901))*eval((differenceOfDates(document.Form.From_901.value,document.Form.To_901.value)));
formatCurrency(document.Form.Amount_901);*/
document.Form.Amount_901.value = eval(getunformatCurrency(document.Form.perday_901))*eval(diffdates);
formatCurrency(document.Form.Amount_901);

}

}
}

function validateForSubmission()
{
var frm = document.closing900ShortSectionForm;
var res = "false";
if(document.closing900ShortSectionForm.sectionId901POC.checked==true ||
document.closing900ShortSectionForm.sectionId902POC.checked==true || document.closing900ShortSectionForm.sectionId903POC.checked==true ||
document.closing900ShortSectionForm.sectionId904POC.checked==true ||
document.closing900ShortSectionForm.sectionId901BPOC.checked==true ||
document.closing900ShortSectionForm.sectionId902BPOC.checked==true ||
document.closing900ShortSectionForm.sectionId903BPOC.checked==true ||
document.closing900ShortSectionForm.sectionId904BPOC.checked==true)
{
res = "true";
}

if(res=="false" && (document.closing900ShortSectionForm.sectionId901Check.checked==true || document.closing900ShortSectionForm.sectionId902Check.checked==true || document.closing900ShortSectionForm.sectionId903Check.checked==true ||
document.closing900ShortSectionForm.sectionId904Check.checked==true ||
document.closing900ShortSectionForm.sectionId901LPOC.checked==true ||
document.closing900ShortSectionForm.sectionId902LPOC.checked==true ||
document.closing900ShortSectionForm.sectionId903LPOC.checked==true ||
document.closing900ShortSectionForm.sectionId904LPOC.checked==true))
{
res = "true";
}

if(res=="false" && (document.closing900ShortSectionForm.sectionId901PocAmount.value!="" || document.closing900ShortSectionForm.sectionId902PocAmount.value!="" || document.closing900ShortSectionForm.sectionId903PocAmount.value!="" ||
document.closing900ShortSectionForm.sectionId904PocAmount.value!=""))
{
res = "true";
}

if(res=="false" && (document.closing900ShortSectionForm.sectionId901Amount.value!="" || document.closing900ShortSectionForm.sectionId902Amount.value!="" || document.closing900ShortSectionForm.sectionId903Amount.value!="" ||
document.closing900ShortSectionForm.sectionId904Amount.value!=""))
{
res = "true";
}

if(res=="false" && flag!="0")
{
res = "true";
}

if(res=="false" && (frm.sectionId901FromDate.value!="" || frm.sectionId901ToDate.value!="" || frm.sectionId901PerDayAmount.value!="" || frm.sectionId902Months.value!="" || frm.sectionId902To.value!="" || frm.sectionId903Months.value!="" || frm.sectionId903To.value!="" || frm.sectionId904To.value!=""))
{
res = "true";
}

if(res=="false")
{
alert("Please enter data for at least one line");
return false;
}

//901
if(((frm.sectionId901BPOC.checked==true || frm.sectionId901LPOC.checked==true) && ((frm.sectionId901PocAmount.value==""))))
{
alert("Please enter the POC amount for Line# 901");
return false;
}
if(!frm.sectionId901BPOC.checked && !frm.sectionId901LPOC.checked && frm.sectionId901PocAmount.value != "")
{
alert(BPOCLPOCMsg + "901");
return false;
}
//902
if(((frm.sectionId902BPOC.checked==true || frm.sectionId902LPOC.checked==true) && ((frm.sectionId902PocAmount.value==""))))
{
alert("Please enter the POC amount for Line# 902");
return false;
}
if(!frm.sectionId902BPOC.checked && !frm.sectionId902LPOC.checked && frm.sectionId902PocAmount.value != "")
{
alert(BPOCLPOCMsg + "902");
return false;
}
//903
if(((frm.sectionId903BPOC.checked==true || frm.sectionId903LPOC.checked==true) && ((frm.sectionId903PocAmount.value==""))))
{
alert("Please enter the POC amount for Line# 903");
return false;
}
if(!frm.sectionId903BPOC.checked && !frm.sectionId903LPOC.checked && frm.sectionId903PocAmount.value != "")
{
alert(BPOCLPOCMsg + "903");
return false;
}
//904
if(((frm.sectionId904BPOC.checked==true || frm.sectionId904LPOC.checked==true) && ((frm.sectionId904PocAmount.value==""))))
{
alert("Please enter the POC amount for Line# 904");
return false;
}
if(!frm.sectionId904BPOC.checked && !frm.sectionId904LPOC.checked && frm.sectionId904PocAmount.value != "")
{
alert(BPOCLPOCMsg + "904");
return false;
}
if((document.closing900ShortSectionForm.sectionId901POC.checked==true || document.closing900ShortSectionForm.sectionId901Check.checked==true) &&(document.closing900ShortSectionForm.sectionId901LPOC.checked==false) &&(document.closing900ShortSectionForm.sectionId901Amount.value==""))
{
alert("Please enter the amount for Line# 901");
return false;
}
if(document.closing900ShortSectionForm.sectionId901Amount.value !="" && !document.closing900ShortSectionForm.sectionId901Check.checked && !document.closing900ShortSectionForm.sectionId901POC.checked)
{
alert(CheckMsg + "901");
return false;
}
if((document.closing900ShortSectionForm.sectionId902POC.checked==true || document.closing900ShortSectionForm.sectionId902Check.checked==true) && (document.closing900ShortSectionForm.sectionId902LPOC.checked==false) && (document.closing900ShortSectionForm.sectionId902Amount.value==""))
{
alert("Please enter the amount for Line# 902");
return false;
}
if(document.closing900ShortSectionForm.sectionId902Amount.value !="" && !document.closing900ShortSectionForm.sectionId902Check.checked && !document.closing900ShortSectionForm.sectionId902POC.checked)
{
alert(CheckMsg + "902");
return false;
}
if((document.closing900ShortSectionForm.sectionId903POC.checked==true || document.closing900ShortSectionForm.sectionId903Check.checked==true) && (document.closing900ShortSectionForm.sectionId903LPOC.checked==false) && (document.closing900ShortSectionForm.sectionId903Amount.value==""))
{
alert("Please enter the amount for Line# 903");
return false;
}
if(document.closing900ShortSectionForm.sectionId903Amount.value !="" && !document.closing900ShortSectionForm.sectionId903Check.checked && !document.closing900ShortSectionForm.sectionId903POC.checked)
{
alert(CheckMsg + "903");
return false;
}
if((document.closing900ShortSectionForm.sectionId904POC.checked==true || document.closing900ShortSectionForm.sectionId904Check.checked==true) && (document.closing900ShortSectionForm.sectionId904LPOC.checked==false) && (document.closing900ShortSectionForm.sectionId904Amount.value==""))
{
alert("Please enter the amount for Line# 904");
return false;
}
if(document.closing900ShortSectionForm.sectionId904Amount.value !="" && !document.closing900ShortSectionForm.sectionId904Check.checked && !document.closing900ShortSectionForm.sectionId904POC.checked)
{
alert(CheckMsg + "904");
return false;
}
//added
if((frm.sectionId901FromDate.value=="" || frm.sectionId901ToDate.value=="") && (frm.sectionId901Amount.value!="" || frm.sectionId901PocAmount.value!="" ))
{
alert("Please enter the date for Line# 901");
return false;
}

//added
if(frm.sectionId901FromDate.value!="" && frm.sectionId901ToDate.value!="" && frm.sectionId901PerDayAmount.value=="" )
{
alert("Please enter the amount/day for Line# 901");
return false;
}

/*var diffdates =eval(differenceOfDates(document.closing900ShortSectionForm.sectionId901FromDate.value,document.closing900ShortSectionForm.sectionId901ToDate.value));*/

var strFrom=document.closing900ShortSectionForm.sectionId901FromDate.value;
var strTo=document.closing900ShortSectionForm.sectionId901ToDate.value;
strFrom= strFrom.replace(/\s/gi,"");
strTo= strTo.replace(/\s/gi,"");
var d1=0;
var d2=0;
var strTo1 = "";
var fromDt = new Date (strFrom);
var toDt = new Date (strTo);

if(fromDt.getTime() >= toDt.getTime())
{
strTo1 = strFrom.substring(0,3);
strTo=strTo1 +"01" + strFrom.substring(5,strFrom.length);
}
else
{
var strMonth=strFrom.substring(0,2);
var strDay="";
if(strMonth=="01" || strMonth=="03" || strMonth=="05" || strMonth=="07" || strMonth=="08" || strMonth=="10" || strMonth=="12")
{
strDay="31";
}
else if(strMonth=="04" || strMonth=="06" || strMonth=="09" || strMonth=="11" )
{
strDay="30";
}
else
{
strDay="28";
}
strTo1 = strFrom.substring(0,3);
strTo=strTo1 + strDay + strFrom.substring(5,strFrom.length);
}
d1=strFrom.substring(3,5);
d2=strTo.substring(3,5);
var diffdates = d2-d1;

if(eval(diffdates)!=0)
{
if(frm.sectionId901FromDate.value!="" && frm.sectionId901ToDate.value!="" && frm.sectionId901PerDayAmount.value!="" && (frm.sectionId901Amount.value=="" && frm.sectionId901PocAmount.value=="" && frm.sectionId901LPOC.checked==false))
{
alert("Please enter the amount for Line# 901");
return false;
}
}


//addded
if(frm.sectionId902Months.value=="" && (frm.sectionId902Amount.value !="" || frm.sectionId902PocAmount.value !="" || frm.sectionId902To.value !=""))
{
alert("Please enter the month for Line# 902");
return false;
}

if(frm.sectionId903Months.value=="" && (frm.sectionId903Amount.value !="" || frm.sectionId903PocAmount.value !="" || frm.sectionId903To.value !=""))
{
alert("Please enter the month for Line# 903");
return false;
}
//added

if(frm.sectionId902To.value!="" && (frm.sectionId902Amount.value=="" && frm.sectionId902PocAmount.value=="" && frm.sectionId902LPOC.checked==false))
{
alert("Please enter the amount for Line# 902");
return false;
}

if(frm.sectionId903To.value!="" && (frm.sectionId903Amount.value=="" && frm.sectionId903PocAmount.value=="" && frm.sectionId903LPOC.checked==false))
{
alert("Please enter the amount for Line# 903");
return false;
}

if(frm.sectionId904To.value!="" && (frm.sectionId904Amount.value=="" && frm.sectionId904PocAmount.value=="" && frm.sectionId904LPOC.checked==false))
{
alert("Please enter the amount for Line# 904");
return false;
}

if(frm.sectionId902To.value=="" && (frm.sectionId902Amount.value!="" || frm.sectionId902PocAmount.value!=""))
{
alert("Please enter the payee for Line# 902");
return false;
}

if(frm.sectionId903To.value=="" && (frm.sectionId903Amount.value!="" || frm.sectionId903PocAmount.value!=""))
{
alert("Please enter the payee for Line# 903");
return false;
}

if(frm.sectionId904To.value=="" && (frm.sectionId904Amount.value!="" || frm.sectionId904PocAmount.value!=""))
{
alert("Please enter the payee for Line# 904");
return false;
}


var errMessage902 = "";
if(frm.sectionId902Months.value!="" && frm.sectionId902To.value=="")
{
errMessage902 = "Please enter the payee for Line# 902";
}
if(frm.sectionId902Months.value!="" && (frm.sectionId902Amount.value=="" && frm.sectionId902PocAmount.value==""))
{
errMessage902 = errMessage902 + "\nPlease enter the amount for Line# 902";
}

if(errMessage902!="")
{
alert(errMessage902);
return false;
}

var errMessage903 = "";
if(frm.sectionId903Months.value!="" && frm.sectionId903To.value=="")
{
errMessage903 = "Please enter the payee for Line# 903";
}
if(frm.sectionId903Months.value!="" && (frm.sectionId903Amount.value=="" && frm.sectionId903PocAmount.value==""))
{
errMessage903 = errMessage903 + "\nPlease enter the amount for Line# 903";
}

if(errMessage903!="")
{
alert(errMessage903);
return false;
}

var checkBoxResult = checkBoxForArrayValues();
if(checkBoxResult!="true")
{
alert(checkBoxResult);
return false;
}

var checkBoxResultPOC = checkBoxForPOCArrayValues();
if(checkBoxResultPOC!="true")
{
alert(checkBoxResultPOC);
return false;
}

var resForAmount = checkForEmptyAmount();
if(resForAmount!="true")
{
alert(resForAmount);
return false;
}

submitForm();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top