// JavaScript Document

$(document).ready(function(){

	// wake up the DreamWeaver MenuBar widget and the spry validation stuff
	var MenuBar2 = new Spry.Widget.MenuBar("MenuBar2", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
	var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "email");
	var spryconf2 = new Spry.Widget.ValidationConfirm("spryconfirm5", "sprytextfield1");
	var sprypassword1 = new Spry.Widget.ValidationPassword("sprypassword1");
	var spryconf1 = new Spry.Widget.ValidationConfirm("spryconfirm1", "sprypassword1");

	$("#register_form").validate({
		submitHandler: function(form) {

			//remove all the class add the messagebox classes and start fading
			$("#txtAjaxMessageBox").removeClass().addClass('messagebox').text('Validating....').fadeIn(900);
			//check the username exists or not from ajax 
			//@email='$email',@firmType='$firmType',@geo='$geo',@pwd='$pwd'
			$.getJSON("service.php",{ ajaxaction:"REGISTER",firmType:$('#slctFirmType').val(),geo:$('#slctGeoLocation').val(),
										email:$('#txtRegEmailAddress').val(),pass:$('#txtRegPassword').val(),
										srcId:$('#srcId').val(),ref:$('#ref').val() 
									}, function(jsondata) 
			{
				if("SUCCESS"==jsondata.message) //if correct login detail 
				{
					$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
					{
						//add message and change the class of the box and start fading
						$(this).html('Registration succeeded.  Please check for confirmation email.').addClass('messageboxok').fadeTo(900,1);
					});
					
				}
				else
				{
					$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
					{
						var msg;
						
						switch(jsondata.message)
						{
							case "UNACTIVATED":
								msg = "This email address has been registered but has not yet been activated.<br />Check your email for activation instructions.";
								break;
							case "DUPLICATE":
								msg = "This email address has previously been registered.<br />Click <a href=\"login.php\">HERE</a> to log in or reset password.";
								break;
							case "EXPIRED":
								msg = "Access for this email address has expired.<br />Please contact Engleman Associates to request an extension.";
								break;
							default:
								msg = "Unknown error processing registration: " + jsondata.message + ".<br />Please contact Engleman Associates for assistance";
								break;
							
						}
						//add message and change the class of the box and start fading
						$(this).html(msg).addClass('messageboxerror').fadeTo(900,1);
					});
				}
			}); 
			
			return false; // post form to report page if authed
		}

	});

});

