// Functions for verifying the online registration form.
// Requires loading of validator1.js and utils.js.
// Registration Option fields:
// span, id=priceX: price for each option
// type=text, name/id=num_qtyX: quantity for each option
// span, id=early_reg_price: early registration discount
// span, id=early_reg_qty: calculated early registration qty
// type=hidden, name/id=hid_early_reg_qty: calculated early registration qty
// span, id=price_paypal: calculated PayPal fee
// type=checkbox, name/id=chk_paypalfee: user will pay PayPal fee
// span, id=total: calculated total price
// type=hidden, name/id=hid_total: calculated total price
// Early Reg Qty and Total have a span for display,
// and a hidden field to pass the value in the request variables.

if (! document.getElementById ) {
   alert("Your browser is not modern enough for this web page." +
		"Please use a more modern brower, like MS IE 5.x or later\n" +
		"or Netscape 6.x or later or Mozilla Firefox");
}

var g_numregs = 0;

var g_early_reg = false;

function CheckEarlyRegDate(year, mon, day, test){
	var frm = document.forms[0];
	var today = new Date();
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	today.setMilliseconds(0);
	var deadline = new Date(year, mon, day);

	//testing of early reg expiration.
	// Sets today to one month past deadline.
	if ( test > 0 ){
		today.setDate(1);
		today.setMonth(deadline.getMonth() + 1);
	}

	if ( today <= deadline) { g_early_reg = true; }
}


function format_dollars(value){
// Make sure the number has 2 decimal places.
	var num1 = value * 100;
	var num2 = Math.round(num1);
	var num3 = num2 / 100;
	var str1 = num3.toString();
	var i1 = str1.indexOf(".");
	var d1 = 0;
	// Determine number of existing decimal places.
	if (i1 > 0){
		d1 = str1.length - i1 - 1;
	}
	if ( d1 == 0 ) { return(str1 + ".00"); }
	else if (d1 == 1) { return(str1 + "0"); }
	else { return(str1); }

}


function UpdateTotal(display_alert) {
	var result = true;
	var ii = 0;
	var jj = 0;
	var frm = document.forms[0];
	var numctrls = frm.elements.length;
	var ctrl_price = "";
	var ctrl_qty = "";
	var ctrl_total = "";
	var ctrl_ppfee = "";
	var qtyname = "";
	var qtyvalue = "";
	var pricename = "";
	var pricevalue = "";
	var errs = "";
	var total = 0;
	g_numregs = 0;
	
	// Total all items.
	// control named num_qtyX = quantity entered by user.
	// control named priceX = price of each item (-5 or 25).

	ctrl_total = document.getElementById("total");
	ctrl_total.innerHTML = ""
	frm.hid_total.value = "";
	ctrl_ppfee = document.getElementById("price_paypal");
	ctrl_ppfee.innerHTML = "";
	for (ii=0; ii < numctrls; ii++){
		ctrl_qty = frm.elements[ii];
		if (ctrl_qty.type == "text"){
	      qtyname = ctrl_qty.name;
		   if (qtyname.substring(0,7) == "num_qty") { 
				// Get Qty for this option
				qtyvalue = ctrl_qty.value;
				ctrl_qty.className = "";
				jj += 1;
				if (! isblank(qtyvalue) ) {
					// Check for valid Qty value
					if ( isnumeric(qtyvalue) && qtyvalue >= 0) {
						// Get Price for this option
						pricename = "price" + jj;
						ctrl_price = document.getElementById(pricename);
						if ( ctrl_price ) {
							pricevalue = ctrl_price.innerHTML;
							// Add positive price amounts to the totals
							if (pricevalue * 1.0 > 0) {
								if (qtyvalue > 0) {
									g_numregs += qtyvalue * 1.0;
									total += pricevalue * qtyvalue;
								}
							// Special handling for discount amounts
							} else {
								// Ignore discount if nothing else selected
								if ( total > 0) {
									total += pricevalue * qtyvalue;
								} // if total > 0
							} // else price is negative
						}
					} else { // Invalid value
            		result = false;
						ctrl_qty.className = "err";
					}
				} // if not blank
			} // if qty control
	   } // if textbox
	}  // for each control

	if (document.getElementById("early_reg_qty")) {
		document.getElementById("early_reg_qty").innerHTML = "0";
		frm.hid_early_reg_qty.value = 0;
	}
	if (! result) {
	   if ( display_alert ) {
	   	alert("Invalid Quantities, see values with red border");
		}
	} else if ( total != 0) {
		// Add Early Reg discount
	   if ( g_early_reg ) {
			document.getElementById("early_reg_qty").innerHTML = g_numregs;
			frm.hid_early_reg_qty.value = g_numregs;
			total += g_numregs * document.getElementById("early_reg_price").innerHTML
		}
		ppfee = format_dollars((total + 0.30) / 0.978) - total;
		ctrl_ppfee.innerHTML = format_dollars(ppfee);
		if (frm.chk_paypalfee.checked){
			total = (total * 1.0) + (ppfee * 1.0);
		}
		ctrl_total.innerHTML = format_dollars(total);
	   frm.hid_total.value = format_dollars(total);
	}
	return result;
}


function ValidateText(strname, strvalue){
	var result = true;
	var ii = 0;

	if (isblank(strvalue)) { return true;}
	strvalue = StringTrim(strvalue);

   if (strname.substring(0,4) == "txt_") {
		if (strname.indexOf("name") > 1) {
			if (! isname(strvalue) ) {result = false;}
		} else if (strname.indexOf("street") > 1) {
			if (! isname(strvalue) ) {result = false;}
		} else if (strname.indexOf("city") > 1) {
			if (! isname(strvalue) ) {result = false;}
		} else if (strname.indexOf("state") > 1) {
			if (! isstate(strvalue) ) {result = false;}
		} else if (strname.indexOf("zip") > 1) {
			if (! iszip(strvalue) ) {result = false;}
		} else if (strname.indexOf("phone") > 1) {
			if (! isphone(strvalue) ) {result = false;}
		} else if (strname.indexOf("email") > 1) {
			if (! isemail(strvalue) ) {result = false;}
		}
	} else if (strname.substring(0,4) == "num_") {
		if (! isnumeric(strvalue)) {result = false;}
	}
	return result;
}


function ValidateQtys(){
	var frm = document.forms[0];
	var numpersons = 0;
	var maxpersons = 0;
	var result = true;
	var ctrl = "";
	var ctrlname = "";
	var ctrlvalue = "";
	var ii = 0;
	var numctrls = frm.elements.length;
	var done = false;

	// Get the number of persons
	while ( ! done ) {
		ii += 1;
		ctrlname = "txt_firstname" + ii;
		ctrl = frm.elements[ctrlname];
		if ( ctrl ) {
			maxpersons += 1;
			ctrlvalue = ctrl.value;
			if (StringTrim(ctrlvalue) != "" ) { 
				numpersons += 1;
			}
		} else { done = true;}
	} // while

	// Verify each Qty is <= number of persons
	for (ii=0; ii < numctrls; ii++){
		ctrl_qty = frm.elements[ii];
		if (ctrl_qty.type == "text"){
	      qtyname = ctrl_qty.name;
			if ( qtyname != "num_hosp_offer") {
			   if (qtyname.substring(0,4) == "num_") {
					qtyvalue = StringTrim(ctrl_qty.value);
					if (isnumeric(qtyvalue) && qtyvalue > numpersons ) {
						ctrl_qty.className = "err";
						result = false;
					}
				} // if num_qty
			} // if not num_hospitality_offer
		} // if text
	} // for each control
	return(result);
}  // ValidateQtys


function ValidateControls(){
	var success = true;
	var ii = 0;
	var frm = document.forms[0];
	var numctrls = frm.elements.length;
	var ctrl = "";
	var ctrlname = "";
	var ctrlvalue = "";
	var numbad = 0;
	var errs = "";
	
	for (ii=0; ii < numctrls; ii++){
		ctrl = frm.elements[ii];
		ctrl.className = "";
	}

	if ( ! ValidateRequired() ) { errs = "Missing required data\n";}
	if ( ! UpdateTotal(false) ) { errs += "Invalid Quantity\n";}
	if ( ! ValidateQtys() ) { errs += "Quantity greater than number of persons\n";}

	// Validate all text controls.
	// name prefix "txt_" means it should contain text.
	// name prefix "num_" means it should be a positive number.

	for (ii=0; ii < numctrls; ii++){
		ctrl = frm.elements[ii];
		if (ctrl.type == "text"){
	      ctrlname = ctrl.name;
			ctrlvalue = ctrl.value;
		   if (! ValidateText(ctrlname, ctrlvalue)) { 
					ctrl.className = "err";
					numbad += 1;
			}
	   }
	}
	if ( numbad > 0) {
		errs += "Invalid data\n";
	}
	if ( errs != "" ) {
		alert(errs + "See values with red border");
		success = false;
	}
	return success;
}

function ValidateRequired(){
	var success = true;
	var frm = document.forms[0];
	var reqnames = frm["required_fields"].value;
	var aryreqname = reqnames.split(",");
   var arylen = aryreqname.length;
	var ii = 0;
	var ctrlname = "";
	var ctrl = "";

	for (ii = 0; ii < arylen; ii++){
		ctrlname = aryreqname[ii];
		ctrl = frm[ctrlname];
		ctrl.className = "";
		if (ctrl && isblank(ctrl.value) ) { 
			success = false;
			ctrl.className = "err";
		}
	}
	return success;
}


function ValidatePage() {
	var ii = 0;
	var frm = document.forms[0];
	var firstname2 = "";
	var lastname1 = "";
	var lastname2 = "";
	var temp = "";

	// Check for invalid values.
	if (! ValidateControls() ) { return false;}
	temp = frm["hid_total"].value;
	if ( temp == "" || temp == "0.00") {
		alert("Please select an Attendance option."); 
		return false;
	}
	return true;
}
