$(document).ready(function() {
	var errorBlock = '<div id="screen" style="display:none;"><!-- --></div>';
	errorBlock += '<div id="altAlert" style="display:none;">';
	errorBlock += '	<span class="ttl"><strong>Some fields need correction</strong></span>';
	errorBlock += '  <div id="altAlertErrors"></div>';
	errorBlock += '  <a id="altAlertOKBtn" href="javascript:hideAlert();">OK</a>';
	errorBlock += '</div>';
	$('body').append(errorBlock);
	$('#altAlert').bgiframe();
	$('#screen').bgiframe();
	$("#screen").pngFix();
	
	var submittingBlock = '<div id="submitting" style="display:none;"><span>Submitting</span><br/><br/><br/><br/><img src="/img/imgFNA/blueOnWhiteAjaxLloader.gif" /></div>';
	$('body').append(submittingBlock);
	$('#submitting').bgiframe();
});

$(function() {
	init();
});

//-------------------------------------------------------------------------
function init() {
	var pageWidth = $(window).width();
	var pageHeight = $(document).height();
	$("#screen").css({width:pageWidth, height:pageHeight});
}

//-------------------------------------------------------------------------
function validateFormOnSubmit(theForm) {
var reason = "";

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// What fields you want to validate, and how to validate them.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	reason += validateEmpty(theForm.txtFirstname);
	reason += validateEmpty(theForm.txtLastname);
	reason += validateEmpty(theForm.txtOrganization);
  reason += validateEmail(theForm.txtEmail);

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  if (reason != "") {
			$("#altAlertErrors").html(reason);
			// fade in, then bind to the enter key.
			if ($.browser.msie) {
				if ($.browser.version >= 7) {
					$("#screen").show();
				}
			} else {
				$("#screen").show();
			}
			$("#screen").show();
			$("#altAlert").fadeIn(500, function () {
				$(document).bind('keyup', 'return', function (evt){hideAlert(); return false; });
				$(document).bind('keydown', 'return', function (evt){return false; });
			});
    return false;
  }
	$("#screen").show();
	$("#submitting").show();
  return true;
}

//-------------------------------------------------------------------------
function hideAlert() {
	// fade out, then unbind the endter key.
	$("#altAlert").fadeOut(250);
	if ($.browser.msie) {
		if ($.browser.version >= 7) {
			$("#screen").hide();
		}
	} else {
		$("#screen").hide();
	}
	$("#screen").hide();
  $(document).unbind('keydown', 'return', function (evt){return false; });
	$(document).unbind('keyup', 'return', function (evt){return false; });
}

//-------------------------------------------------------------------------
function validateEmpty(fld) {
    var error = "";
  	var tmpFldName;
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\?\/\|\{\}\!\#\$\%\^\&\*\+\']/g ;
		
		switch(fld.name)
		{
		case "txtFirstname":
			tmpFldName = "First Name"
			fld.value = fld.value.replace(illegalChars, '');
			break;   
		case "txtLastname":
			tmpFldName = "Last Name"
			fld.value = fld.value.replace(illegalChars, '');
			break;   
//		case "txtLastname":
//			tmpFldName = "Last Name"
//			fld.value = fld.value.replace(illegalChars, '');
//			break;
		case "txtOrganization":
			tmpFldName = "Organization"
			fld.value = fld.value.replace(illegalChars, '');
			break;
		default:
			tmpFldName = fld.name
		}
		
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "<p>The required field <strong>" + tmpFldName + "</strong> is empty.<p>"
    } else {
        fld.style.background = '#dff2ac';
    }
    return error;   
}

//-------------------------------------------------------------------------
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

//-------------------------------------------------------------------------
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
//		var tfld = fld.value.replace(/\s+/g,'');							// remove all spacess from a text string
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
//    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\?\/\|\{\}\!\#\$\%\^\&\*\+\']/ ;
    		
    if (tfld.value == "") {
        fld.style.background = 'Yellow';
        error = "<p>You didn't enter an <strong>email</strong>.</p>";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "<p>Please enter a valid <strong>email</strong>.</p/>";
    } else if (tfld.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "<p>The <strong>email</strong> contains illegal characters.</p>";
    } else {
        fld.style.background = '#dff2ac';
    }
		fld.value = tfld;
    return error;
}

//-------------------------------------------------------------------------
function validatePhone(fld) {
		// clear error messages.
    var error = "";
		var stripped = fld.value
		
		// remove all non digits from the field.
//		stripped = stripped.replace(/\D/g,'');

//		if (stripped == "") {
		// no phone number entered.
//      error = "You didn't enter a phone number.<br/>";
//      fld.style.background = 'Yellow';
//    } else 
		
		if (stripped != "" && stripped.length > 21) {
		// phone number is less than 10 digits.
			error = "<p>Your <strong>phone number</strong> must be less than 22 characters.</p>";
      fld.style.background = 'Yellow';
//		} else if (stripped.length == 11) {
		// if this is an 11 digit #, and the first digit is 1, then remove the 1.
//			stripped = stripped.replace(/^1/, '');
    } else {
			fld.style.background = '#dff2ac';	
		}
	 
		// change the form field iun question to the cleaned value.
//		fld.value = stripped;
    return error;
}
