
$(function () {
	$(".notInitial, .errorMessage, #status").hide();
	$("#registerDriver").click(function () {
		var errorMessage = '';
		$(".errorMessage").hide('slow');
		if($("#driverLastName").val() == '') {
			errorMessage = "Please enter a Last Name";
		}
		if(!isNumeric($("#driverSSN").val()) || $("#driverSSN").val().length != 4) {
			if(errorMessage != '') { errorMessage += "<BR>"; }
			errorMessage += "Please enter the last four digits of your SSN";
		}
		if(errorMessage != '') {
			$("#errorMessage").html(errorMessage).show('slow');
		}
		else {
			$("#driverRegForm").hide('slow');
			$("#status").html("<img src='../images/loading.gif' height='25px'><BR>Searching...").show("slow");
			var driverData = $("#driverRegForm").serialize();
			$.ajax({
				type: "POST",
				url: 'resources/registerDriverFind.php',
				data: driverData,
				success: function(data) {
					var display = '';
					switch(data.substring(0,6)) {
						case 'FoundR':
							display = "You are already Registered!<BR>Please use your email address and password to";
							display += " <a href='login.html'>Login</a>";
							break;
						case 'FoundN':
							display = "You are in the system!<BR>But, you haven't been registered.<BR><BR>";
							$("#driverNeedRegForm").show('slow');
							//alert(data.substring(10));
							$("#driverNeedDriverID").val(data.substring(10));
							//alert($("#driverNeedDriverID").val());
							break;
						case 'NotFou':
						default:
							display = "I'm sorry.<BR>You were not found.<BR>";
							display += "<BR>Please complete the <a href='../app-pre.html'>Online Application</a> or contact your agent.";
							break;

					}
					$("#status").html(display);
					//var goBackString = "<BR><BR><div id='bigLink' class='linkSpan' onClick='backToOne()'>Go Back</div>";
					//$("#status").append(goBackString);
					$("#status").show('slow');
				},
				error: function() {
					$("#status").hide('slow');
					$("#driverRegForm").show('slow');
					$("#derrorMessage").html("There was an error searching for your driver record. Please try again").show('slow');

				}
			});
		}
		return false;
	});

	$("#driverRegisterComp").click(function () {
		var errorMessage = '';
		$(".errorMessage").hide();
		if($("#driverNeedEmail").val() == '') {
			errorMessage = "Please Enter your Email Address";
		}
		else if(!echeck($("#driverNeedEmail").val())) {
			errorMessage = "Please enter a valid Email Address";
		}
		if($("#driverNeedPassword").val() == '') {
			if(errorMessage != '') { errorMessage += "<BR>"; }
			errorMessage += "Please enter a password";
		}
		if($("#driverNeedPassword").val() != $("#driverNeedPasswordConf").val()) {
			if(errorMessage != '') { errorMessage += "<BR>"; }
			errorMessage += "Your passwords do not match";
		}
		if(errorMessage != '') {
			$("#errorMessage").html(errorMessage).show('slow');
		}
		else {
			$("#driverNeedRegForm").hide('slow');
			$("#status").html("<img src='../images/loading.gif' height='25px'><BR>Registering...").show("slow");
			var driverData = $("#driverNeedRegForm").serialize();
			$.ajax({
				type: "POST",
				url: 'resources/registerDriver.php',
				data: driverData,
				success: function(data) {
					var displayString = ''
					if(data == 'Complete') {
						displayString = "Registration Complete!<BR><BR><a href='login.html'>Login Here</a>";
						$("#errorMessage").html('').hide();
					}
					else {
						$("#driverNeedRegForm").show('slow');
						$("#errorMessage").html("There was an error processing your registration. Please try again").show('slow');
						displayString = data;
					}
					$("#status").html(displayString);
				},
				error: function() {
					$("#status").hide('slow');
					$("#driverNeedRegForm").show('slow');
					$("#errorMessage").html("There was an error processing your registration. Please try again").show('slow');

				}
			});
		}
		return false;
	});

	$("#userReqButton").click(function () {
		var errorMessage = '';
		$(".errorMessage").hide();
		if($("#userEmail").val() == '') {
			errorMessage = "Please Enter your Email Address";
		}
		else if(!echeck($("#userEmail").val())) {
			errorMessage = "Please enter a valid Email Address";
		}
		if($("#userPassword").val() == '') {
			if(errorMessage != '') { errorMessage += "<BR>"; }
			errorMessage += "Please enter a password";
		}
		if($("#userPassword").val() != $("#userPasswordConf").val()) {
			if(errorMessage != '') { errorMessage += "<BR>"; }
			errorMessage += "Your passwords do not match";
		}
		if($("#userFirstName").val() == '') {
			if(errorMessage != '') { errorMessage += "<BR>"; }
			errorMessage += "Please enter your First Name";
		}
		if($("#userLastName").val() == '') {
			if(errorMessage != '') { errorMessage += "<BR>"; }
			errorMessage += "Please enter your Last Name";
		}

		if(errorMessage != '') {
			$("#errorMessage").html(errorMessage).show('slow');
		}
		else {

			$("#userReqForm").hide('slow');
			$("#status").html("<img src='../images/loading.gif' height='25px'><BR>Requesting...").show("slow");
			var driverData = $("#userReqForm").serialize();
			//alert(driverData);
			$.ajax({
				type: "POST",
				url: 'resources/user-request.php',
				data: driverData,
				success: function(data) {
					var displayString = ''
					if(data == 'Complete') {
						displayString = "Thank You.<BR>Your request has been sent. You will receive a confirmation e-mail and a response within 2 business days.";
						displayString += "<BR><BR><a href='../index.html'>Wall Street Home</a>";
						$("#errorMessage").html('').hide();
					}
					else {
						$("#userReqForm").show('slow');
						$("#errorMessage").html("There was an error processing your registration. Please try again").show('slow');
						displayString = data;
					}
					$("#status").html(displayString);
				},
				error: function() {
					$("#status").hide('slow');
					$("#userReqForm").show('slow');
					$("#errorMessage").html("There was an error processing your registration. Please try again").show('slow');

				}
			});
		}
		return false;
	});

	$("#userEmail").change(function() {
		$.ajax({
			type: "GET",
			url: 'resources/checkUserEmailValid.php?email=' + this.value,
			success: function(data) {
				if(data == 'CanUse')
				{
					$("#userEmail").data('valid', 'true');
					$("#status").hide('slow');
					$("#errorMessage").hide('slow');
					$("#userReqButton").removeAttr('disabled');
				}
				else
				{
					$("#userEmail").data('valid', 'false');
					$("#userReqButton").attr('disabled','disabled');
					$("#status").html("This email address is already registered. <BR>Please use your email address and password to <a href='login.html'>Login</a>").show('slow');
					$("#userReqForm").show('slow');
				}
			},
			error: function() {
				$("#status").hide('slow');
				$("#userReqForm").show('slow');
				$("#errorMessage").html("There was an error verifying the validity of this e-mail address. Please try again").show('slow');

			}
		});
	});
});

function backToOne() {
	$(".notInitial").hide();
	$("#initialQuestion").show('slow');
	$("#driverRegForm").show('slow');
	$("#driverLastName").val('');
	$("#driverSSN").val('');

	$("#userEmail").val('');
	$("#userPassword").val('');
	$("#userPasswordConf").val('');
	$("#userFirstName").val('');
	$("#userLastName").val('');

	$("#errorMessage").html('').hide();
	$("#status").html('').hide();
	changeLegend("How are you associated with Us?");
}

function driverRegistration() {
	$("#initialQuestion").hide('slow');
	$("#driverRegistration").show('slow');
	$("#goBackLink").show('slow');
	changeLegend("Are you currently signed on with Us?");
}

function driverRegistration2() {
	$("#driverRegistration").hide('slow');
	$("#driverRegistration2").show('slow');
	$("#driverLastName").focus();
	changeLegend("Please Enter your Information");
}

function userRequestDisplay() {
	$("#initialQuestion").hide('slow');
	$("#userRequest").show('slow');
	$("#goBackLink").show('slow');
	$("#userEmail").focus();
	changeLegend("Please Enter Your Information");
}

function changeLegend(string) {
	$("fieldset > legend").fadeOut('slow', function() {
		$("fieldset > legend").html(string).fadeIn('slow');
		});
}

function isNumeric(strString) {

var strValidChars = "0123456789";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++)    {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
	}
}
return blnResult;
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false
	}

	return true
}