/*
    Calculation JavaScript for Membership Form
	GST Line 77
	PST Line 85
	
	July 2010 - PST exempt so changes to 0
	GST used as place holder for HST changed to 12%
*/

    var fee;
    var adminFee;
    var addLocFee;
    var addLocAdminFee;
    var gstIncluded;
    var totalMembershipFee = 0;
    var totalOtherFee = 0;
    var totalFees = 0;
    var otherFees = new Array();
    otherFees[0] = ['1']
otherFees[1] = ['15.00']
otherFees[2] = ['1']  // GSTApplicable array
otherFees[3] = ['0']  // GSTIncluded array
otherFees[4] = ['1']  // PSTApplicable array
otherFees[5] = ['0']  // PSTIncluded array


    // DynContact variables
    var next_index = 0;	// next index id
    var ContactArray = new Array();


function init(totalFee)
{
    this.totalFees = totalFee;

    calcFees(totalFee);
    calcOtherFees();

    // render initial dynamic form : data from the database
    if ( ContactArray.length > 0 ) {
	setForm();
    }
}
    

// calculate total fees
function calcFees(memFees)
{
    var totalFeeCharged = 0;
    var fees = 0;
    var otherFees = 0;
    this.totalFees = memFees;

    for (i=0; i< document.f_Form.f_MembershipType.length; i++) {
	// check for the selected option
	if ( document.f_Form.f_MembershipType[i].checked ) {
	    this.totalMembershipFee = parseFloat(this.totalFees);
	} // end if
    } // end for

    this.totalMembershipFee = Math.round(this.totalMembershipFee * 100)/100;
    
    //document.getElementById('MembershipFeeCharged').innerHTML = '<b>' + Currency(Math.round(this.totalMembershipFee * 100)/100) + '</b>';
    document.getElementById('MembershipFeeCharged').innerHTML = '<b>' + Currency(this.totalMembershipFee) + '</b>';
    
    getTotalFees();
    calcOtherFees();
}

function calcOtherFees(qty)
{
	
     if ( typeof(this.otherFees[0]) != 'undefined' ) {
	var itemQty = 1;

	tempItemQuantityVar = "f_ItemQuantity[]";
	itmQty = document.f_Form[tempItemQuantityVar];
	
	var length = this.otherFees[0].length;
	var OtherFees = 0;
	// New GST rate as of January 1, 2008
	var gstRate = 0.00
	var gstTotal = 0;
	var gstApplicable = false;
	var gstIncluded = false;
	var gstStr = '';
	//var gstFlag = false;
	var pstRate = 0.0;
	var pstTotal = 0;
	var pstApplicable = false;
	var pstIncluded = false;
	var pstStr = '';
	//var pstFlag = false;

	var TotalRate = 0; // gst and pst

	tempOtherVar = "f_OtherFee[]";
	checkOtherFee = document.f_Form[tempOtherVar];

	for ( i = 0; i < this.otherFees[0].length; i++ ) {
		try {
			if ( checkOtherFee[i].checked == true ) {
				
				//document.getElementById('testResult').innerHTML += 'test: ' + i + " -- " + checkOtherFee[i].checked + '<br />';
					
				if ( this.otherFees[2][i] == 1 ) {
					gstApplicable = true;
				}
				if ( this.otherFees[3][i] == 1 ) {
					gstIncluded = true;
				}
				if ( this.otherFees[4][i] == 1 ) {
				       pstApplicable = true;
				}
				if ( this.otherFees[5][i] == 1 ) {
				       pstIncluded = true;
				}
				
				//alert('gst Applicable: ' + gstApplicable + '\n' + 'gst Included: ' + gstIncluded + '\n' + 'pst Applicable: ' + pstApplicable + '\n' + 'pst Included: ' + pstIncluded);

				if ( gstApplicable && gstIncluded && pstApplicable && !pstIncluded ) {
					baseFee = getBaseFee(this.otherFees[1][i], gstRate);
					pstTotal = baseFee * pstRate;
				}
				else {
					if ( pstApplicable ) {
						if ( ! pstIncluded ) {
							pstTotal = this.otherFees[1][i] * pstRate;
						}
					}
				}

				
				if ( pstApplicable && pstIncluded && gstApplicable && !gstIncluded ) {
					baseFee = getBaseFee(this.otherFees[1][i], pstRate);
					gstTotal = baseFee * gstRate;
				}
				else {
					if ( gstApplicable ) {
						if ( ! gstIncluded ) {
							gstTotal = this.otherFees[1][i] * gstRate;
						}
					}
				}

				TotalRate = pstTotal + gstTotal;
				TotalRate = Math.round(TotalRate * 100)/100;

				
				OtherFees = ( parseFloat(this.otherFees[1][i]) + parseFloat(TotalRate) ) * itmQty[i].value;
				
				// calculate total of all checked items
				this.totalOtherFee = this.totalOtherFee + OtherFees;
				
				//alert('totalOtherFee = ' + this.totalOtherFee);
				
				// reset value
				gstApplicable = false;
				gstIncluded = false;
				pstApplicable = false;
				pstIncluded = false;
				gstTotal = parseFloat(0);
				pstTotal = parseFloat(0);
				TotalRate = parseFloat(0);
				
			}
		}
		catch( myError ) {
		    for ( j = 0; j < document.f_Form.getElementsByTagName('input').length; j++ ) {
		            if ( document.f_Form.getElementsByTagName('input').item(j).name == "f_OtherFee[]" && document.f_Form.getElementsByTagName('input').item(j).checked == true) {
				if ( this.otherFees[2][i] == 1 ) {
					gstApplicable = true;
				}
				if ( this.otherFees[3][i] == 1 ) {
					gstIncluded = true;
				}
				if ( this.otherFees[4][i] == 1 ) {
				       pstApplicable = true;
				}
				if ( this.otherFees[5][i] == 1 ) {
				       pstIncluded = true;
				}
				
				if ( gstApplicable && gstIncluded && pstApplicable && !pstIncluded ) {
					baseFee = getBaseFee(this.otherFees[1][i], gstRate);
					pstTotal = baseFee * pstRate;
				}
				else {
					if ( pstApplicable ) {
						if ( ! pstIncluded ) {
							pstTotal = this.otherFees[1][i] * pstRate;
						}
					}
				}

				
				if ( pstApplicable && pstIncluded && gstApplicable && !gstIncluded ) {
					baseFee = getBaseFee(this.otherFees[1][i], pstRate);
					gstTotal = baseFee * gstRate;
				}
				else {
					if ( gstApplicable ) {
						if ( ! gstIncluded ) {
							gstTotal = this.otherFees[1][i] * gstRate;
						}
					}
				}
				
				TotalRate = pstTotal + gstTotal;
				TotalRate = Math.round(TotalRate * 100)/100;

				OtherFees = ( parseFloat(this.otherFees[1][i]) + parseFloat(TotalRate) ) * itmQty.value;
				
				// calculate total of all checked items
				this.totalOtherFee = this.totalOtherFee + OtherFees;

				// reset value
				gstApplicable = false;
				gstIncluded = false;
				pstApplicable = false;
				pstIncluded = false;
				gstTotal = parseFloat(0);
				pstTotal = parseFloat(0);
				TotalRate = parseFloat(0);

				/*
				OtherFees = parseFloat(OtherFees) + parseFloat(this.otherFees[1][i]);
				this.totalOtherFee += parseFloat(OtherFees);
				OtherFees = parseFloat(0);

				if ( this.otherFees[3][i] == 0 ) {
					gstFlag = true;
				}

				if ( gstFlag ) {
					gstStr = ' <font color="red">(GST added)</font>';
				// New GST rate as of January 1, 2008
					gst = 0.05;
				}
				else {
					gst = 0;
				}
				*/
			    }
		      }
		
		} 
		finally {
			//alert('finally');
		}

	}

	//document.getElementById('testResult3').innerHTML = "<b>Other Fee Charged: " + this.totalOtherFee + "</b><br />";

	this.totalOtherFee = Math.round(this.totalOtherFee * 100)/100;
	
	if ( ! isNaN(this.totalOtherFee) ) {
		//document.getElementById('OtherFeeCharged').innerHTML = '<b>' + Currency(Math.round(this.totalOtherFee * 100)/100) + gstStr + '</b>';
		document.getElementById('OtherFeeCharged').innerHTML = '<b>' + Currency(this.totalOtherFee) + gstStr + '</b>';
	}
	else {
		this.totalOtherFee = 0;
		document.getElementById('OtherFeeCharged').innerHTML = '<b>$0.00</b>';
	}

	getTotalFees();

	// resets value to 0.00
	this.totalOtherFee = parseFloat(0);
    }
}

function getBaseFee(baseFee, rate)
{
	// New GST rate as of January 1, 2008
	//var gstRate = 0.05;

	return baseFee / ( 1 + rate);
}

function getTotalFees()
{
    var totalMembershipCharged = this.totalMembershipFee;
    var totalOtherFeeCharged = this.totalOtherFee;
    var totalFeeCharged = 0;

    //alert('totalMembershipCharged = ' + totalMembershipCharged + ' -- totalOtherFeeCharged = ' + totalOtherFeeCharged);
    totalFeeCharged = parseFloat(totalMembershipCharged) + parseFloat(totalOtherFeeCharged);

    totalFeeCharged = Math.round(totalFeeCharged * 100)/100;

    if ( ! isNaN(totalFeeCharged) ) {
    	//document.getElementById('TotalFeeCharged').innerHTML = '<b>' + Currency(Math.round(totalFeeCharged * 100)/100) + '</b>';
    	document.getElementById('TotalFeeCharged').innerHTML = '<b>' + Currency(totalFeeCharged) + '</b>';
    }
    else {
    	document.getElementById('TotalFeeCharged').innerHTML = '<b>$0.00</b>';
    }
}

// returns number as string in $xxx,xxx.xx format.
function Currency(anynum) {
        anynum = "" + eval(anynum)  //evaluate (in case an expression sent)
        intnum = parseInt(anynum)  //isolate integer portion
        intnum = Math.abs(intnum)
        intstr = ""+intnum
        //add comma in thousands place.
        if (intnum >= 1000) {
                intlen = intstr.length
                temp1=parseInt(""+(intnum/1000))
                temp2=intstr.substring(intlen-3,intlen)
                intstr = temp1+","+temp2

        }
        if (intnum >= 1000000) {
                intlen = intstr.length
                temp1=parseInt(""+(intnum/1000000))
                temp2=intstr.substring(intlen-7,intlen)
                intstr = temp1+","+temp2

	}

	decnum = Math.abs(parseFloat(anynum)-parseInt(anynum)) //isolate decimal portion
        decnum = decnum * 100 // multiply decimal portion by 100.
        decstr = "" + Math.abs(Math.round(decnum))
        if (decstr.length>2) {decstr=decstr.substring(0,2)}
        while (decstr.length < 2) {decstr="0"+decstr}
        retval = intstr + "." + decstr
        if (anynum < 0) {
                retval="("+retval+")"
        }

	return "$"+ retval;
} // end Currency()


	
	<!--

// Somehow fields in contact information section gets wipe out totally
// erasing all the values in the text field.
// To resolve this resetDefaultValues function is created to
// populate all the dynamic fields with its appropriate
// values from the array ContactArray[]
function resetDefaultValues(what) 
{
	// reset form
	window.document.f_Form.reset();


	var index = 0;
	var index1 = 0;
	var index2 = 0;
	var index3 = 0;
	var index4 = 0;
	var index5 = 0;
	var index6 = 0;
	var index7 = 0;
	var index8 = 0;
	var index9 = 0;
	var index10 = 0;
	var index11 = 0;
	var index12 = 0;
	var index13 = 0;
	var str = '';
	
	
	for (var i=0, j=what.elements.length; i<j; i++) {
	    myType = what.elements[i].type;
	    myName = what.elements[i].name;
	    myVal  = what.elements[i].value;

	    if (myType == 'hidden' || myType == 'text') {
		    if ( myName == 'f_DynContactTitle[]' ) {
				what.elements[i].value = ContactArray[0][index];
				index++;
			}
			if ( myName == 'f_DynContactName[]' ) {
				what.elements[i].value = ContactArray[1][index1];
				index1++;
			}
			if ( myName == 'f_DynContactLastName[]' ) {
				what.elements[i].value = ContactArray[2][index2];
				index2++;
			}
			if ( myName == 'f_DynContactPrimaryEmail[]' ) {
				what.elements[i].value = ContactArray[3][index3];
				index3++;
			}
			if ( myName == 'f_DynContactSecondaryEmail[]' ) {
				what.elements[i].value = ContactArray[4][index4];
				index4++;	
			}
			if ( myName == 'f_DynContactDirectPhoneLine[]' ) {
				what.elements[i].value = ContactArray[5][index5];
				index5++;
			}
			if ( myName == 'f_DynContactCellPhone[]' ) {
				what.elements[i].value = ContactArray[6][index6];
				index6++;
			}
			if ( myName == 'f_DynContactFax[]' ) {
				what.elements[i].value = ContactArray[7][index7];
				index7++;
				}
			if ( myName == 'f_DynContactID[]' ) {
				what.elements[i].value = ContactArray[8][index8];
				index8++;
			}
			if ( myName == 'f_DynContactHomePhone[]' ) {
				what.elements[i].value = ContactArray[9][index9];
				index9++;
			}
			if ( myName == 'f_DynContactPagerNumber[]' ) {
				what.elements[i].value = ContactArray[10][index10];
				index10++;
			}
			if ( myName == 'f_DynContactPrimaryContact[]' ) {
				if ( ContactArray[11][index11] == '1' ) {
					what.elements[i].checked = 'true'
				}
				else {
					what.elements[i].checked = 'false'
				}
				//what.elements[i].checked = ContactArray[11][index11];
				index11++;
			}
			if ( myName == 'f_DynContactContactMethod[]' ) {
				what.elements[i].value = ContactArray[12][index12];
				index12++;
			}
			
			if ( myName == 'f_DynContactSalutation[]' ) {
				what.elements[i].value = ContactArray[13][index13];
				index13++;
			}

			// newly added contact is set to undefined when the form is reset
			if ( what.elements[i].value == 'undefined' ) {
				what.elements[i].value = '';
			}
		}
	}
}
//-->


