
var venue1Price = 35;
var venue2Price = 35;

var venue1Discount = -5;
var venue2Discount = -5;

var memberDiscount = -5;
var nonUSAC = 5;     //additional for non-USAC members
var bothDiscount = -10;		// attend both venues

var eventName ="USAC Youth Regionals";
var venue1Name="Top and Lead Rope";
var venue2Name="Bouldering";

var venue3Name="";
var venue4Name="";
var venue5Name="";

var venueDateString = "3/18/2006"
var venueDate=new Date;
venueDate=returnDate(venueDateString);

var todayIs=new Date();  //todayIs.setDate(26);

preReg=true;
if (todayIs >  returnDate("3/19/2006")) preReg=false;  // closed at 0am on this day

// these are the displayed prices/cost
var venue1Cost=0;
var venue2Cost=0;

var subTotalCost=0;
var taxCost=0;
var totalCost=0;

var memberDiscountCost=0;
var venue1DiscountCost=0;
var venue2DiscountCost=0;
var nonUSACCost=0;

var taxRate=0;

var teeShirtSize = "not selected";
var sex= " gender not selected"

var venue1DivisionValid = false;
var venue1Division = "(no birth date)";
var venue2DivisionValid = false;
var venue2Division = "(no birth date)";

var age=0;

window.onload=formLoad;

function formLoad() {	
	displayNewPrice();
	};

function newPhone(field) {
	if (ValidatePhone(field.value)) {  //if good phone number, then format
		var s = field.value;
		s=s.replace(/[^0-9]/gi, "");  //keep only numbers, format into 3 seperate fields
		if (s.length == 7) pnumber = "(801) " + s.substr(0,3) + "-" +  s.substr(3)
		else pnumber = "(" + s.substr(0,3) + ") " + s.substr(3,3) + "-" + s.substr(6);
		field.value=pnumber;
		};
};

function newTee(field) {
	teeShirtSize=document.getElementById("teeShirtSize").options[document.getElementById("teeShirtSize").selectedIndex].value;
};

//-------------------------
function displayNewPrice() {
form=document.usac_eventSignup;

memberDiscountCost=0;
if (form.isMember.checked &&  form.venue2.checked) memberDiscountCost=memberDiscount;

sex = "gender not selected";
if (document.usac_eventSignup.sex[0].checked) sex="Male";
if (document.usac_eventSignup.sex[1].checked) sex="Female";

venue1Cost=0;
venue1DiscountCost=0;
if (form.venue1.checked) {
	venue1DiscountCost = venue1Discount;  //pre-reg discount
	venue1Cost = venue1Price;
	
	if (validateDate(form.climberDOB.value,"U","P"))  {
		age=getAgeOn(form.climberDOB.value,"12/31/2006");
	
		venue1Division = " (too old)";
		venue1DivisionValid=false;
		if (age <= 19) {venue1Division = " Junior";  venue1DivisionValid=true;};
		if (age <= 17) {venue1Division = " Youth A"; venue1DivisionValid=true;};
		if (age <= 15) {venue1Division = " Youth B"; venue1DivisionValid=true;};
		if (age <= 13) {venue1Division = " Youth C"; venue1DivisionValid=true;};
		if (age <= 11) {venue1Division = " Youth D"; venue1DivisionValid=true;};
		form.venue1Division.value = venue1Division + " - " + sex;
		}
	else {
		if (form.climberDOB.value=="") venue1Division="no birth date"
		else venue1Division = " (invalid birth date)";
		form.venue1Division.value = venue1Division  + " - " + sex;
		}
	}
else {
	form.venue1Division.value = "";
	};

venue2Cost=0;
venue2DiscountCost=0;
if (form.venue2.checked) {
	venue2DiscountCost = venue2Discount;
	venue2Cost = venue2Price;
	
	if (validateDate(form.climberDOB.value,"U","P"))  {
		age=getAgeOn(form.climberDOB.value,"12/31/2006");
	
		venue2Division = " (too old)";
		venue2DivisionValid=false;
		if (age <= 19) {venue2Division = " Junior";  venue2DivisionValid=true;};
		if (age <= 17) {venue2Division = " Youth A"; venue2DivisionValid=true;};
		if (age <= 15) {venue2Division = " Youth B"; venue2DivisionValid=true;};
		if (age <= 13) {venue2Division = " Youth C"; venue2DivisionValid=true;};
		if (age <= 11) {venue2Division = " Youth D"; venue2DivisionValid=true;};
		form.venue2Division.value = venue2Division  + " - " + sex;
		}
	else {
		if (form.climberDOB.value=="") venue2Division="no birth date"
		else venue2Division = " (invalid birth date)";
		form.venue2Division.value = venue2Division  + " - " + sex;
		}
	}
else {
	form.venue2Division.value = "";
	};

bothDiscountCost=0;
if ( form.venue1.checked && form.venue2.checked ) bothDiscountCost = bothDiscount;

nonUSACCost = 0;
if (!form.isUSAC.checked && form.venue1.checked)  nonUSACCost = nonUSAC;
if (!form.isUSAC.checked && form.venue2.checked)  nonUSACCost = nonUSACCost + nonUSAC;

	
subTotalCost = venue1Cost  + venue2Cost  + memberDiscountCost + venue1DiscountCost + venue2DiscountCost + nonUSACCost + bothDiscountCost;
taxCost = subTotalCost * taxRate;
totalCost = subTotalCost + taxCost;

form.quarryTotal.value = '$ ' + (venue1Cost + venue1DiscountCost + (bothDiscountCost/2)).toFixed(2);
form.frontTotal.value  = '$ ' + (venue2Cost + venue2DiscountCost + (bothDiscountCost/2) + memberDiscountCost).toFixed(2);

dispPrice2();

};

function dispPrice2() {

   venue1$ =  venue1Cost.toFixed(2);
   venue2$ =  venue2Cost.toFixed(2);
   total$  =  totalCost.toFixed(2);
   
   memberDiscountCost$ =  memberDiscountCost.toFixed(2);
   venue1DiscountCost$ =  venue1DiscountCost.toFixed(2);
   venue2DiscountCost$ =  venue2DiscountCost.toFixed(2);
   nonUSAC$ = nonUSAC.toFixed(2)
   
   costframe.document.open();  
   costframe.document.write('<div id="paydiv" style="background-color:white; layer-background-color:white; position:absolute; top:0px; left:20px; width:325px; height:115px; z-index:1">');	
   costframe.document.write('<table border=0 bordercolorlight=#dddddd bordercolordark=#ffffff  width=450px height=71 style="font-size:11px; font-family:sans-serif; color:#666666; letter-spacing:1pt;">');
   costframe.document.write('<tr><td><font color="#A63F00" ><p style="line-height: 5px">Venue</P><td width=69 align=right><font color="#A63F00" >Charge </td></tr>');   
   
   if (document.usac_eventSignup.venue1.checked)
      	costframe.document.write('<tr><td>' + venue1Name + ': ' + venue1Division + '/' + sex + '</td><td align=right>' + venue1$ + '</td></tr>');   

   if (nonUSACCost!=0 && document.usac_eventSignup.venue1.checked) 
   		costframe.document.write("<tr><td>&nbsp;&nbsp;USAC day membership fee</td><td align=right>" + nonUSAC$ + "</td></tr>");
   		
   if (preReg && document.usac_eventSignup.venue1.checked) 
   		costframe.document.write("<tr><td>&nbsp;&nbsp;Pre-Registration discount</td><td align=right>" + venue1DiscountCost$ + "</td></tr>");
		

   if (document.usac_eventSignup.venue2.checked) 
   		costframe.document.write('<tr><td>' + venue2Name + ': ' + venue2Division + '/' + sex + '</td><td align=right>' + venue2$ + '</td></tr>');
   
   if (nonUSACCost!=0 && document.usac_eventSignup.venue2.checked)
   		costframe.document.write("<tr><td>&nbsp;&nbsp;USAC day membership fee</td> <td align=right>  " + nonUSAC$ + "</td></tr>");
			
   if (preReg && document.usac_eventSignup.venue2.checked) 
   		costframe.document.write("<tr><td>&nbsp;&nbsp;Pre-Registration discount</td><td align=right>" + venue2DiscountCost$ + "</td></tr>");

   if (memberDiscountCost!=0)   
   		costframe.document.write("<tr><td>&nbsp;&nbsp;Front Membership discount</td><td align=right>" + memberDiscountCost$ + "</td></tr>");  
   		
   if (document.usac_eventSignup.venue1.checked && document.usac_eventSignup.venue2.checked)
   		costframe.document.write("<tr><td>Discount for both venues</td> <td align=right>  " + bothDiscountCost.toFixed(2) + "</td></tr>");
   
   costframe.document.write("<tr><td align=right><b>Total (tax included)</b></td><td align=right><b> $"+total$+"</b></td></tr></table> ");
   costframe.document.write("</div>");
   costframe.document.close();
};





//-----------------------------------------------
function validateSignup (form) {

var OK = true;
var message = "The following item(s) are required";

	//----------------------------------------
	function testField(test, errorMessage) {
	// if test is false, then error condition
	// this function needs to be inside 'validateMembership' for the scope of message
		if (test) { message += errorMessage; OK = false; return false};
		return true;   }
		
// test for member data
testField(form.firstName.value == "" , "; participant first name");
testField(form.lastName.value == "" , "; participant last name");
	
testField(form.address.value == "", "; address");
testField(form.city.value == "", "; city");
testField(form.state.value == "", "; state");
testField(!validZip(form.zip.value), "; zip");
testField(!ValidatePhone(form.phone.value), "; phone number");

if (form.email.value != "") testField(!validEmail(form.email.value), "; valid email address" );

if (form.venue1.checked  || form.venue2.checked) {
	testField(!validateDate(form.climberDOB.value,"U","P"), "; climber birthdate");
	testField(teeShirtSize=="not selected","; tee shirt size");
	testField(!(form.sex[0].checked || form.sex[1].checked), "; sex");
	
	}

if (form.venue1.checked) testField(!venue1DivisionValid,"; valid youth divisioin");

testField(totalCost==0 , "; select venue item");


//verify all membership contracts checked
if (form.venue1.checked || form.venue2.checked) testField(!(form.Rule1.checked  ), "; must agree to all event conditions by checking the boxes"); 

// display error message if necessary, then exit, unless 'justdoit'
if (form.specialInst.value=="justdoit") OK=true;	
if (!OK ) { alert (message + ".") ; return false};

if ( form.climberDOB.value != ""  && getAgeOn(form.climberDOB.value,venueDateString) < 18 & (form.venue1.checked || form.venue2.checked) ) {
	alert("This climber is under 18.  They'll needed a waiver signed by their parent or legal guardian")
}

// passed validation, continue to PayPal
   
form.venue1Charge.value   = '$ ' + venue1Cost.toFixed(2);
form.venue1Discount.value = '$ ' + venue1DiscountCost.toFixed(2);

form.venue2Charge.value   = '$ ' + venue2Cost.toFixed(2);
form.venue2Discount.value = '$ ' + venue2DiscountCost.toFixed(2);
form.memberDiscountCost.value = '$ ' + memberDiscountCost.toFixed(2);

form.division.value = form.venue1Division.value;
if (form.division.value == '') form.division.value = form.venue2Division.value;

form.nonUSACCost.value     = '$ ' + nonUSACCost.toFixed(2);
form.bothDiscountCost.value = '$ ' + bothDiscountCost.toFixed(2);

form.TaxCharge.value      = '$ ' + taxCost.toFixed(2);
form.TotalCharge.value    = '$ ' + totalCost.toFixed(2);

form.age.value = age;  
     
if (form.needWaiver.checked) form.needWaiver.value="yes";   //else it's null

     
// set cookies for Waiver    
      var expdate = new Date ();
      expdate.setTime (expdate.getTime() + (1000 * 60 * 3 ));
      setCookie ("_ClimberLastName", form.firstName.value, expdate);
      setCookie ("_ClimberFirstName", form.firstName.value, expdate);
      setCookie ("_ClimberName", form.firstName.value + " " + form.lastName.value, expdate);
      setCookie ("_ClimberDOB", form.climberDOB.value, expdate);
      setCookie ("_ClimberAddress1", form.address.value, expdate);
      setCookie ("_ClimberCity", form.city.value, expdate);
      setCookie ("_ClimberState", form.state.value, expdate);
	  setCookie ("_ClimberZip", form.zip.value, expdate);
      setCookie ("_ClimberPhone", form.phone.value, expdate);
      setCookie ("_needWaiver", form.needWaiver.value, expdate);

	  c=window.location.href.substr(0,window.location.href.lastIndexOf("/")+1);
      setCookie ("_Redirection", c + "usac_thankyou1.htm", expdate);

// set fields for PayPal
	  form.first_name.value =    form.firstName.value;
	  form.last_name.value =     form.lastName.value;
	  form.amount.value =		 subTotalCost;
	  form.tax.value =		 		taxCost;
	  
	  form.item_number.value = 	eventName;
	  
	  form.item_name.value = eventName + " Registration - " + form.firstName.value + " " + form.lastName.value;
	  if (form.isMember.checked) form.item_name.value += " :Front Member";
	  if (form.isUSAC.checked) form.item_name.value += " :USAC Member";
     	  
	  form.on0.value='';
	  form.on1.value='';
	  
	  if (venue1DivisionValid  ) {
	  	form.on0.value = 'The Quarray - ' + venue1Name;	
	  	form.os0.value = form.venue1Division.value;  
	  	};
	  	
	  form.os1.value = "";
	  if (venue2DivisionValid) 	 {
			form.on1.value = 'The Front - ' + venue2Name;
			form.os1.value = form.venue2Division.value;	 
	  		};

	  if (form.os0.value == "") {
	  		form.os0.value=form.os1.value;
	  		form.on0.value=form.on1.value;
	  		form.os1.value="";
	  		form.on1.value="";
	  		};
  
	  form.success.value= window.location.href.substr(0,window.location.href.lastIndexOf("/")+1) + "usac_success.php";
	  form.cancel.value= window.location.href.substr(0,window.location.href.lastIndexOf("/")+1) + "usac_cancel.php";
		  
	  var s = form.phone.value;
	  s=s.replace(/[^0-9]/gi, "");  //keep only numbers, format into 3 seperate fields
	  if (s.length == 7) {
		form.night_phone_a.value = "801"
		form.night_phone_b.value = s.substr(0,3);
		form.night_phone_c.value = s.substr(3); 
		}
		else {
		form.night_phone_a.value = s.substr(0,3);
		form.night_phone_b.value = s.substr(3,3);
		form.night_phone_c.value = s.substr(6); 
		}
 
    window.onunload=null;

return OK;  

};



