CSSRule('.message', {fontWeight:'normal', fontSize:'12px'});
CSSRule('.addform', {width:'370px', margin:'15px'});
CSSRule('.addform td', {fontFamily:'Tahoma', fontSize:'12px', whiteSpace:'nowrap'});
CSSRule('li', {fontFamily:'Tahoma', fontSize:'14px', whiteSpace:'nowrap',paddingTop:'3px',paddingLeft:'10px'});
CSSRule("input[disabled='disabled']", {color:'#FF0000', cursor:'default'});

function errorHighlight(elm) {
	elm.style.background = '#FF3939';
	elm.style.color = 'white';
	elm.style.fontWeight = 'bolder';
}

function remHighlight(elm) {
	elm.style.backgroundColor = '#FFFFFF';
	elm.style.color = 'black';
	elm.style.fontWeight = 'normal';
}

function checkEmail(elm) {
	if (!testEmail(elm.value) && trim(elm.value) > ''){
		errorHighlight(elm);
		elm.focus();
	} else {
		remHighlight(elm);
	}
}

var newUserControlObj = {
	licenseNumber : $('licenseNumber'),
	email : $('email'),
	resultContainer : {},

	init : function () {
		$('confirmEmail').observe('blur', function () {
			checkEmail($('confirmEmail'));
		});
		$('email').observe('blur', function () {
			checkEmail($('email'));
		});
		$('firstName').observe('blur', function () {
			if ($('firstName') > '') remHighlight($('firstName'));
		});
	},
	/* This method generates the form for the logon */
	licenseLookup : function(license) {
		if (this.licenseNumber == license || license == '') return;
		this.licenseNumber = license;

		ajaxObj = new Ajax.Request('/wwwroot/custmaint/ajax/licenseLookup.cfm?licensenumber=' + license,
			{
				onComplete : function(transport){
					var results = transport.responseText.replace("&amp;","&").evalJSON();

					if (typeof results == 'string') {
						alert(results);
						errorHighlight($('licenseNumber'));
					} else {
						newUserControlObj.resultContainer = results;
						remHighlight($('licenseNumber'));
					}
				}
				,onFailure : function(){alert('failure');}
				,onException:function(req,exception) {
					alert("Exception Occured" + " " + exception);
				}
			}
		);
	},
	
	formSubmit : function () {
		/* ------------------- Form Verification -----------------*/
		var errorMessages = [];

		/* License Number required */
		if ($('licenseNumber').value == '') {
			errorMessages.push('&bull;Please enter a valid state license number.<br>');
			errorHighlight($('licenseNumber'));
		} else {
			remHighlight($('licenseNumber'));	
		}

		/* First name required */
		if ($('firstName').value == '') {
			errorMessages.push('&bull;Please enter a first name.<br>');
			errorHighlight($('firstName'));
		} else {
			remHighlight($('firstName'));
		}
		
		/* Last name must be entered */
		if ($('lastName').value == '') {
			errorMessages.push('&bull;Please enter a last name.<br>');
			errorHighlight($('lastName'));
		} else {
			remHighlight($('lastName'));
		}

		/* Email must be entered */
		if ($('email').value == '') {
			errorMessages.push('&bull;Please enter a valid email address.<br>');
			errorHighlight($('email'));
		} else {
			remHighlight($('email'));
		}

		/* Confirm Email must be entered */
		if ($('confirmEmail').value != $('email').value) {
			errorMessages.push('&bull;Email and Confirm email must match.<br>');
			errorHighlight($('confirmEmail'));
		} else {
			remHighlight($('confirmEmail'));
		}

		/* Is the does the last name entered match the last name on the license?  It MUST */
		if ($('lastName').value > '') {
			if ($('lastName').value.toUpperCase() != newUserControlObj.resultContainer.lastname.toUpperCase()) {
				alert($('lastName').value.toUpperCase() + " : " + newUserControlObj.resultContainer.lastname);
				errorMessages.push('&bull;Lastname must match state license.<br>');
				errorHighlight($('lastName'));
			} else {
				remHighlight($('lastName'));
			}
		}
		
		if (errorMessages.length == 0) {
			var newUserObj = newUserControlObj.resultContainer;
			
			newUserObj.legalName = newUserObj.firstname + ' ' + newUserObj.lastname;

			newUserObj.firstname = $('firstName').value;
			newUserObj.lastname = $('lastName').value;
			newUserObj.email = $('email').value;

			/* This is for if we ever want to get signup emails and reply */
			/*ajaxObj = new Ajax.Request('/wwwroot/custmaint/trialuser.cfm',*/
			ajaxObj = new Ajax.Request('/wwwroot/custmaint/signup/confirmemail.cfm',
				{
					method : 'post'
					,postBody : Object.toQueryString(newUserObj)
					,onComplete : function(transport){
						var msgContainer = '<div style="text-align:left;color:#003300;font-size:14px"><b>A request to register for your free trial of IRA Suite has been sent to our sales team.<br />  You will be contacted via email with your new login name and password.<br />Thank you and we look forward to doing business with you.</b><br></div>';
			
						myForm = createForm({
							innerHTML:msgContainer,
							overlay:true,
							shadowWidth:0,
							shadowOpacity:45,
							padding:15
						});
						myForm.onEnterKey = function () {
							myForm.close();
						};
						myForm.setTitle('Sign-Up Confirmation');
						myForm.style.padding='10px';
						myForm.button.innerHTML = 'Ok';
						myForm.show().center();
						
					}
					,onException:function(req,exception) {
						alert("Exception Occured" + " " + exception);
					}
				}
			);
		} else {
			var msgContainer = '<div style="text-align:left;color:#cc0000;font-size:16px"><b>Please fix the following errors to continue:</b><br><br>';
			msgContainer += errorMessages.join('');
			msgContainer += '</div>';

			myForm = createForm({
				innerHTML:msgContainer,
				overlay:true,
				shadowWidth:0,
				shadowOpacity:45,
				padding:15
			});
			myForm.onEnterKey = function () {
				myForm.close();
			};
			myForm.setTitle('Errors with Sign-Up');
			myForm.style.padding='10px';
			myForm.button.innerHTML = 'Ok';
			myForm.show().center();
		}
	}
};

Event.observe(window, 'load', function () {
	newUserControlObj.init();
});
