// javascript for order form calculations for piagrows.org
// Ken

debug = false;



/////////////////////////////////////////////////////////////
/*
        format(num) rounds numbers to nearest .01
        for price display $xx.xx
*/

function format(num)
{
   finished=Math.floor(num)+".";
   var base=100*(num-Math.floor(num))+0.5;
   finished += Math.floor(base/10);
   finished += Math.floor(base%10);
   return finished;
   }

////////////////////////////////////////////////////////////
function willSubmit(){
	if(document.cipaform.MemApp.checked){document.cipaform.member_button.checked = 'YES';}
}
////////////////////////////////////////////////////////////
/*
        doChanges() is the main function that does the updating of the form.
        It is triggered by changes made by user on the form     with onChange() event trigger.

        It figures row subtotals - by looping through the rows.
        First it checks to see if the signup for the event is checked.
        If it is, it checks the member status, then sets the
        price in the total column for that row.
        Then it adds this value to a running subtotal.
        When it's done with the rows, it rounds
        off the subtotal and assigns it to the total.

        Variables:
        s is the signup checkbox for each event
        n is non member price field name (hidden field)
        m is member price field name (hidden field)
        t is total price field name for the row
        subtot is a running subtotal used by the calculations, but not shown.
        total is the total of the order
*/



function doChanges()
{
   if(debug) {alert("doing changes");}
   limitTradeShowOnly();
   limitLunch();
   var subtot = 0;
        for(x=1;x<15;x++){
                s = "s"+x;
                n = "n"+x;
                m = "m"+x;
                t = "t"+x;
                if(document.cipaform[s].checked){
                        if(debug){alert("found checked button = "+x+"");}
                        // set price based on whether the user is member or non-member
                        if(document.cipaform.member_button.checked){
                                if(debug) {alert("found member");}
                                document.cipaform[t].value = format(document.cipaform[m].value);
                                }
                        else {
                                document.cipaform[t].value = format(document.cipaform[n].value);
                                if(debug) {alert("found non-member");}
                                }
                }
                // if the item isn't checked, then clear the total for the row.
                else {
			document.cipaform[t].value = 0;                 
		}
                // Number() is needed to make it a Number and not a string.
                // subtot variable keeps a running total of the subtotal as we go through the rows.
                subtot += Number(document.cipaform[t].value);
                if(debug) {alert("x is " + x + " and subtotal is " + subtot);}
        }

   // update total, rounding to nearest .01
   document.cipaform.total.value = format(subtot);
   if(debug) {alert("total is " + document.cipaform.total.value);}
}

////////////////////////////////////////////////////////////////
/*
        doOrder() is the final form submission
        triggered by the button at the bottom of the form.
        It totals the order one last time so that all
        variables are updated, then submits the form.
*/

function doOrder(){
        doChanges();
        if(debug) { alert("final total is" + document.cipaform.total.value); }
        document.cipaform.submit();
}

function clearAll(){
	document.cipaform.reset();
}

// work like a radio button... 
var clickee;
function toggleOnClick(clickee){
	if(clickee == document.cipaform["s1"]){ 
		document.cipaform["s2"].checked = false; 
		document.cipaform["s3"].checked = false;
		document.cipaform["s4"].checked = false;
	}
	if(clickee == document.cipaform["s2"]){  
		document.cipaform["s1"].checked = false; 
		document.cipaform["s3"].checked = false;
		document.cipaform["s4"].checked = false;
	}
	if(clickee == document.cipaform["s3"]){  
		document.cipaform["s1"].checked = false; 
		document.cipaform["s2"].checked = false;
		document.cipaform["s4"].checked = false;
	}
	if(clickee == document.cipaform["s4"]){  
		document.cipaform["s1"].checked = false; 
		document.cipaform["s2"].checked = false;
		document.cipaform["s3"].checked = false;
	}
}
// disable packages (s1-s4) if trade show selected
function limitTradeShowOnly(){
	if((document.cipaform["s6"].checked)||(document.cipaform["s7"].checked)){
		for(s=1;s<5;s++){
			document.cipaform["s"+s].checked = false;
			document.cipaform["s"+s].disabled = true;
		}
	}
	else{ 
		for(s=1;s<5;s++){
			document.cipaform["s"+s].disabled = false;
		}
	}
}
// if one of first 3 selected, then no lunch thurs or fri
function limitLunch(){
	anychecked= false;
	for(s=1;s<4;s++){
		if(debug){ alert("limitLunch checking s" + s); }
		if(document.cipaform["s"+s].checked)
		anychecked = true;
	}
	if(anychecked){
		document.cipaform["s6"].checked = false;
		document.cipaform["s7"].checked = false;
		document.cipaform["s10"].checked = false;
		document.cipaform["s13"].checked = false;
		document.cipaform["s6"].disabled = true;
		document.cipaform["s7"].disabled = true;
		document.cipaform["s10"].disabled = true;
		document.cipaform["s13"].disabled = true;
	}
	else{
		document.cipaform["s6"].disabled = false;
		document.cipaform["s7"].disabled = false;
		document.cipaform["s10"].disabled = false;
		document.cipaform["s13"].disabled = false;
	}
}


/* BELOW NOT USED */

/*
function toggleOnClick(){
	// if(s1,s3,s4,s5) checked, then set s6 on for late reg fee
	if ( document.cipaform["s1"].checked || document.cipaform["s3"].checked || 
	     document.cipaform["s4"].checked || document.cipaform["s5"].checked ){
		document.cipaform["s6"].checked = true;
		document.cipaform["t6"].value = 40;
	}
}
*/
/*
	if( document.cipaform["s1"].checked || document.cipaform["s3"].checked ||
		document.cipaform["s4"].checked || document.cipaform["s5"].checked || document.cipaform["s6"].checked)
		{
		document.cipaform["s7"].checked  = false;
		document.cipaform["s8"].checked  = false;
		doChanges();
		document.cipaform["s7"].disabled = true;
		document.cipaform["s8"].disabled = true;
	}else{
		document.cipaform["s7"].disabled = false;
		document.cipaform["s8"].disabled = false;
	}
//	if( document.cipaform["s7"].checked || document.cipaform["s8"].checked )
//		alert("Remember! The Exhibitor Showcase is included in the Seminar Package.");

}
*/



