// Form Specific Validation and Calculation Functions
// Strings
var iAttendee = "Some or all of the attendee information is incomplete. Please check verify that a value is supplied for the first name, last name, and e-mail of each attendee."
var iFirm = "Some or all of the firm information is incomplete. Please verify that a value is supplied for the firm name, address, city, state or province, postal code, and phone number.";
var iPayment = "Some or all of the payment information is incomplete. Please verify that a credit card number and date exist if charging by credit card, a PO number exists if using a Purchase Order, or a check number exists if purchasing by check.";

function currencyFormatted(amount) {
        var i = parseFloat(amount);
        if(isNaN(i)) { i = 0.00; }
        var minus = '';
        if(i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if(s.indexOf('.') < 0) { s += '.00'; }
        if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
}

function checkRegFormCreditCardPONumber() {
  return true;
}

// These functions are specific to the RegForm.
// Changes to the form field names will likely result in errors.

// Validations is divided into validation sections (person1,person2,firm,payment)
// Attendee #1
function person1Complete() {
  if (
    (!isWhitespace(document.RegForm.FirstName1.value)) &&
    (!isWhitespace(document.RegForm.LastName1.value)) &&
    isEmail(document.RegForm.Email1.value, false)
  ) { return true; } else { return false; }
}

// Attendee #2
function person2Complete() {
  //alert ("1: " + ! isWhitespace(document.RegForm.FirstName2.value,false) + "\n2: " + ! isWhitespace(document.RegForm.LastName2.value,false) + "\n3: " + isEmail(document.RegForm.Email2.value));
  if (
    (!isWhitespace(document.RegForm.FirstName2.value)) &&
    (!isWhitespace(document.RegForm.LastName2.value)) &&
    isEmail(document.RegForm.Email2.value, false)
  ) { return true; } else { return false; }
}

// Firm
function firmComplete() {
  if (
    (!isWhitespace(document.RegForm.Address1.value)) &&
    (!isWhitespace(document.RegForm.City.value)) &&
    (!isWhitespace(document.RegForm.State.value)) &&
    (!isWhitespace(document.RegForm.PostalCode.value)) &&
    (!isWhitespace(document.RegForm.Phone.value))
  ) { return true; } else { return false; }
}

// Payment
function paymentComplete() {
  var cc = false;
  var normalizedCCN;
 
  
  if (!isWhitespace(document.RegForm.PaymentMethod.value)) {
	if (document.RegForm.PaymentMethod.value == 'American Express') { cc = true; }
	if (document.RegForm.PaymentMethod.value == 'VISA') { cc = true; }
	if (document.RegForm.PaymentMethod.value == 'MasterCard') { cc = true; }
	
	if (cc == true) {
	  // Validate Credit Card Number Exist and Expiration Date Exists
	  if (
	    (!isWhitespace(document.RegForm.CheckCardPONumber.value)) &&
	    (!isWhitespace(document.RegForm.ccMonth.value)) &&
	    (!isWhitespace(document.RegForm.ccYear.value))
	  ) { return true; } else { return false; }
	}
	if (document.RegForm.PaymentMethod.value == 'Check') {
	  // Validate Payment Methond for Check
      return true;
	}
	if (document.RegForm.PaymentMethod.value == 'PO') {
	  // Validate Payment Methond for PO
	  if (!isWhitespace(document.RegForm.CheckCardPONumber.value)) {
	    return true;
	  } else {
  	    return false;
  	  }
	}
	if (document.RegForm.PaymentMethod.value == 'Bill Me') {
	  return true;
	  // Validate Payment Methond for Check
	} else {
	   return false;
	}
  } else { return false;}
}

// ValidateRegForm
function validateRegForm() {
  // Clear the alertMessage as a global variable
  document.alertMessage = "";
  
  var alertMessage = "";

  // Attendee #1
  if (! person1Complete()) {
    alertMessage = alertMessage + iAttendee + "\n\n";
  }

  // Firm
  if (! firmComplete()) {
    alertMessage = alertMessage + iFirm + "\n\n";
  } 

  // Payment
  // Handle Individual Rate Selected
  if (! paymentComplete()) {
    alertMessage = alertMessage + iPayment + "\n\n";
  }

  if (alertMessage) {
    alert (alertMessage);
    document.submitForm = false;
    return 0;
  } else {
    document.submitForm = true;
    return 1;
  }
}

// Calculate Payment price for selected options of RegForm
// Calculate is called when:
// 1. When the page first loads (in this case person2 checking skipped using the skipFlag
// 2. A change is made to any of the radio buttons used to select the discount program
// 3. A change is made to any of the radio buttons used to select the course format
// 4. A change is made to any of the radio buttons used to select the product type
// 5. A change is made to the first,last, and e-mail fields for person2
function calculatePaymentAmount () {
  if (document.RegForm.SalePrice_RegularRate) {
    // calculatePaymentAmountSeminar();
    calculatePaymentAmountSeminar();
  } else {
    calculatePaymentAmountNoSeminar();
  }
}


function calculatePaymentAmountSeminar(skipFlag) {
  // Use hard-coded full object heirarchy to set initial value
  var myPayment;

  myPayment = 0;
 // myPayment = parseFloat(myPayment);
  
  // Handle Product: Seminar (Identified as Product[0], the zero element in the radio array)
  if (document.RegForm.Product[0].checked) {
  // Handle Product: Seminar (Identified as Product[0], the zero element in the radio array)
    // Handle Discount Program: Regular Rate (Identified as DiscountProgram[0] in the radio array)
    //   Regular Rate cannot be selected if person2 is filled out
    if (document.RegForm.DiscountProgram[0].checked) {
      if (person1Complete() && person2Complete()) {
        alert("Automatic Correction\nThe group rate is being automatically applied because the first, last, and e-mail for a second attendee have been entered.");
        document.RegForm.DiscountProgram[2].checked = true;
      } else {
        myPayment = document.RegForm.SalePrice_RegularRate.value;
      }
    }    
    // Handle Discount Program: Group Rate (Identified as DiscountProgram[1] in the radio array)
    //   Group Rate cannot be selected if person 2 is not filled out.
    if (document.RegForm.DiscountProgram[2].checked) {
      if (person1Complete() && person2Complete()) {
        myPayment = document.RegForm.SalePrice_GroupRate.value * 2;
      } else {
        alert("Automatic Correction\nThe Regular Rate is being automatically applied because the first, last, and e-mail for the second attendee have not been entered.");
        document.RegForm.DiscountProgram[0].checked = true;
        myPayment = document.RegForm.SalePrice_RegularRate.value;
      }

    }
    // Handle Government Rate (DiscountProgram[2]
    //   The Rate is double if a second attendee has been supplied
    if (document.RegForm.DiscountProgram[1].checked) {
      if (person1Complete() && person2Complete()) {
        myPayment = document.RegForm.SalePrice_GovRate.value * 2;
      } else {
        myPayment = document.RegForm.SalePrice_GovRate.value;
      }
    }
    
    // Handle New/Student Rate (DiscountProgram[3])
    //   The Rate is double if a second attendee has been supplied
    if (document.RegForm.DiscountProgram[3].checked) {
      if (person1Complete() && person2Complete()) {
        myPayment = document.RegForm.SalePrice_StudentRate.value * 2;
      } else {
        myPayment = document.RegForm.SalePrice_StudentRate.value;
      }
    }    
 
   
 
 
  }
  
  
  
  
  
  

  // Handle Home Study (Product[1])
  if (document.RegForm.Product[1].checked) {
  	if (document.RegForm.SalePrice_HomeStudy.value) {
  		if (person1Complete() && person2Complete()) {
  		//alert ("You order 2 set of Home Study");
        myPayment = document.RegForm.SalePrice_HomeStudy.value * 2;
      } else
    {
      myPayment = document.RegForm.SalePrice_HomeStudy.value;
    }
  }
  }

  // Handle Course Materials (Product[2])
  if (document.RegForm.Product[2].checked) {
    if (document.RegForm.SalePrice_CourseMaterials.value) {
      myPayment = document.RegForm.SalePrice_CourseMaterials.value;
    }
  }
//  myPayment = parseFloat(myPayment);
  document.RegForm.Payment.value = currencyFormatted(myPayment);

  return 1;
}




function calculatePaymentAmountNoSeminar(skipFlag) {
  // Use hard-coded full object heirarchy to set initial value
  var myPayment;
 
  myPayment = 0;
  
  // Handle Product: Seminar (Identified as Product[0], the zero element in the radio array)
  //if (document.RegForm.Product[0].checked) {
  // Handle Product: Seminar (Identified as Product[0], the zero element in the radio array)
    // Handle Discount Program: Regular Rate (Identified as DiscountProgram[0] in the radio array)
    //   Regular Rate cannot be selected if person2 is filled out
    //if (document.RegForm.DiscountProgram[0].checked) {
      //if (person1Complete() && person2Complete()) {
        //alert("Automatic Correction\nThe group rate is being automatically applied because the first, last, and e-mail for a second attendee have been entered.");
        //document.RegForm.DiscountProgram[1].checked = true;
      //} else {
        //myPayment = document.RegForm.SalePrice_RegularRate.value;
      //}
    //}    
    // Handle Discount Program: Group Rate (Identified as DiscountProgram[1] in the radio array)
    //   Group Rate cannot be selected if person 2 is not filled out.
    //if (document.RegForm.DiscountProgram[1].checked) {
     // if (person1Complete() && person2Complete()) {
      //  myPayment = document.RegForm.SalePrice_GroupRate.value * 2;
     // } else {
       // alert("Automatic Correction\nThe Regular Rate is being automatically applied because the first, last, and e-mail for the second attendee have not been entered.");
       // document.RegForm.DiscountProgram[0].checked = true;
       // myPayment = document.RegForm.SalePrice_RegularRate.value;
     // }
 
    //}
    // Handle Government Rate (DiscountProgram[2]
    //   The Rate is double if a second attendee has been supplied
    //if (document.RegForm.DiscountProgram[2].checked) {
    //  if (person1Complete() && person2Complete()) {
     //   myPayment = document.RegForm.SalePrice_GovRate.value * 2;
    //  } else {
    //   myPayment = document.RegForm.SalePrice_GovRate.value;
    //  }
   //}
    
    // Handle New/Student Rate (DiscountProgram[3])
    //   The Rate is double if a second attendee has been supplied
   // if (document.RegForm.DiscountProgram[3].checked) {
    //  if (person1Complete() && person2Complete()) {
   //     myPayment = document.RegForm.SalePrice_StudentRate.value * 2;
   //   } else {
    //    myPayment = document.RegForm.SalePrice_StudentRate.value;
    //  }
  //  }    
 // }
 
  // Handle Home Study (Product[1])
 // if (document.RegForm.Product[0].checked) {
 //   if (document.RegForm.SalePrice_HomeStudy.value) {
  //    myPayment = document.RegForm.SalePrice_HomeStudy.value;
  //  }
 // }
  
  
  
 if (document.RegForm.Product[0].checked) {
  	if (document.RegForm.SalePrice_HomeStudy.value) {
        myPayment = document.RegForm.SalePrice_HomeStudy.value;
    }
  }
 
  
  
  
  
  
  
 
  // Handle Course Materials (Product[2])
  if (document.RegForm.Product[1].checked) {
    if (document.RegForm.SalePrice_CourseMaterials.value) {
      myPayment = document.RegForm.SalePrice_CourseMaterials.value;
    }
  }

 
  document.RegForm.Payment.value = currencyFormatted(myPayment);
 
  return 1;
}

