﻿
// ---------------------------------------------------------------------------------------
// Add a new name line when the current amount has focus

function addFirstInvoiceLine() {

	$(buildInvoiceLine()).appendTo('#invoiceLinesPanel');
	$('#amount_' + invoiceLines).focus(function(event) { 
		addInvoiceLine(event);
	});
}

function addInvoiceLine(event) {

		$('#amount_' + invoiceLines).unbind();
		$(buildInvoiceLine()).appendTo('#invoiceLinesPanel');
		$('#amount_' + invoiceLines).focus(function(event) {
			addInvoiceLine(event);
		});
	if (event.target.value.length > 0) {
    }
}

function buildInvoiceLine() {

	var nameLine = ""
	
	invoiceLines = invoiceLines + 1;
	if (invoiceLines == 1) {
		nameLine += '<input id="invoice_' + invoiceLines + '" tabindex="9' + invoiceLines + '0" class="quickHelp"  />';
		nameLine += '<div class="tooltip"><p>The invoice number must start with ST and be followed by at least 4 digits.</p><hr /><p>The amount may have digits and "." only. If "." is present it must be followed by two digits.</p></div>';
	} else
		nameLine += '<input id="invoice_' + invoiceLines + '" tabindex="9' + invoiceLines + '0" />';
	
	nameLine += '<input id="amount_' + invoiceLines + '" tabindex="9' + invoiceLines + '2" />';

	return nameLine;
}


// ---------------------------------------------------------------------------------------
// Validate the form

// When validation is complete and correct, build a string that contains a list of all the
// registrants and add it to a hidden field on the form to be submitted.

function validate(form) {

    var errorText;
    var error = false;
    var venueErrorDisplayed = false;
    var invoiceErrorDisplayed = false;
    var inputFieldsPerLine = 2;

    //	Clear all errors from from the previous submission

    $('.errorText').remove();

    if (form.firstName.value == "") {
        $('<p class="errorText">Your first name is required<p>').insertAfter('#firstName');
        if (!error)
            form.firstName.focus();
        error = true;
    }

    if (form.lastName.value == "") {
        $('<p class="errorText">Your last name is required<p>').insertAfter('#lastName');
        if (!error)
            form.lastName.focus();
        error = true;
    }

    if (form.company.value == "") {
        $('<p class="errorText">A company name is required<p>').insertAfter('#company');
        if (!error)
            form.company.focus();
        error = true;
    }

    if (form.countryList.options[form.countryList.selectedIndex].text == "Select a country") {
    	$('<p class="errorText">A country name is required<p>').insertAfter('#countryList');
    	if (!error)
    		form.countryList.focus();
    	error = true;
    }

    if (form.address.value == "") {
        $('<p class="errorText">An address is required<p>').insertAfter('#address');
        if (!error)
            form.address.focus();
        error = true;
    }

    if (form.city.value == "") {
        $('<p class="errorText">A city is required<p>').insertAfter('#city');
        if (!error)
            form.city.focus();
        error = true;
    }

    if (form.stateList.options[form.stateList.selectedIndex].text == "Select a state or province") {
    	$('<p class="errorText">A state is required<p>').insertAfter('#stateList');
    	if (!error)
    		form.stateList.focus();
    	error = true;
    }

    if (form.zipCode.value == "") {
        $('<p class="errorText">A zip code is required<p>').insertAfter('#zipCode');
        if (!error)
            form.zipCode.focus();
        error = true;
    }

    if (form.telephone.value == "") {
        $('<p class="errorText">A telephone is required<p>').insertAfter('#telephone');
        if (!error)
            form.telephone.focus();
        error = true;
    }

    if (!form.email.value.match(/^.+@.+\..+$/)) {
        $('<p class="errorText">Email format is: id@domain.tld<p>').insertAfter('#email');
        if (!error)
            form.email.focus();
        error = true;
       }

	if (invoiceLines == 1 && (form.invoice_1.value == "" || form.amount_1.value == "")) {
		$('#invoiceLinesPanel').append('<p class="errorText">At least one invoice and amount is required.<p>')
		if (!error)
			form.invoice_1.focus();
		error = true;
	}

	$('#invoiceLinesPanel input').each(function(n) {
		if (n < (invoiceLines - 1) * inputFieldsPerLine) {
			if (this.value == "") {
				if (!invoiceErrorDisplayed) {
					$('#invoiceLinesPanel').append('<p class="errorText">Both the invoice number and the amount must be supplied.<p>')
					invoiceErrorDisplayed = true;
				}
				if (!error)
					form.invoice_1.focus();
				error = true;
			}
		}
	});

	$('#invoiceLinesPanel input[id^=invoice_]').each(function(n) {
		if (this.value != "") {
			if (this.value.match(/^[sS][tT]\d{4,}$/)) {
			} else {
				$('#invoiceLinesPanel').append('<p class="errorText">Invoice numbers start with ST and are followed by 4 or more digits.<p>')
				if (!error)
					form.invoice_1.focus();
				error = true;
			}
		}
	});

	$('#invoiceLinesPanel input[id^=amount_]').each(function(n) {
		if (this.value != "") {
			if (this.value.match(/^\d+(\.\d{2})?$/)) {
			} else {
				$('#invoiceLinesPanel').append('<p class="errorText">Amounts must consist of digits and "." only. If a "." is present it must be followed by 2 digits.<p>')
				if (!error)
					form.amount_1.focus();
				error = true;
			}
		}
	});

    if (error)
    	return (false);
    else {
    	buildInvoicesString();
    	setCountryAndState();
    	return (true);
    }
}

// ---------------------------------------------------------------------------------------
// Create a string with all the inovice payment information to pass on to the next page.

function buildInvoicesString() {

	var invoiceString = "";

	$('#invoiceLinesPanel input').each(function(n) {
		if (this.value != "") {
			if (n == 0) {
				invoiceString = this.value;
			} else {
				if (n % 2 != 0) {
					invoiceString += "::" + this.value;
				}
				else {
					invoiceString += "|" + this.value;
				}
			}
		}
	});
	$('#invoices').val(invoiceString);
}

// ---------------------------------------------------------------------------------------
// Convert the selected country name to the 2 character ISO 3166 code and set the
// value of the hidden countryCode field to it.

function setCountryAndState() {

	var countryName = "";
	var stateName = "";

	countryName = $('#countryList option:selected').text();
	$('#countryCode').val(char2CodeFromName(countryName));
	$('#country').val(countryName);
	stateName = $('#stateList option:selected').text();
	$('#stateCode').val(removeCountryCode(subdivisionCodeFromName(stateName)));
	$('#state').val(stateName);
}
