/*

	Philadelphia Crematories, Inc Core JavaScript Library
	Copyright (c) 2009 AppWare, LLC. All Rights Reserved. (www.appware.biz)
	Module: user.js
	Modified: 21-Apr-2009
	Created: 15-Feb-2007
	Required includes: main.js

*/


//	module public variables
	var userLoginFormName = "login";


//	submits login form
	function userLogin() {

//		initialize valid flag
		var bValid = true;

//		set form object
		var f = document.forms[userLoginFormName];

//		trim text and textarea fields
		trimFields(f);

//		validate fields (reverse tab order)
		var oFld, sFld, oFirstBad, sMsg = "";

		oFld = f.userId;
		sFld = oFld.value;
		if ( !userIsId(sFld) ) {
			bValid = false;
			oFirstBad = oFld;
			sMsg = "- Your Funeral Director identification code is missing or invalid\n" + sMsg;
		}

//		did form validate?
		if ( !bValid ) {
			sMsg = "Unable to login for the following reason(s):\n\n" + sMsg;
			window.alert(sMsg);
			oFirstBad.select();
		}

//		set return value
		return bValid;

	}
//	--------------------




//	returns true if string is valid funeral director Id format
	function userIsId(s)
	{

		// set regular expressions
		var regExps = new Array(4);
		regExps[0] = /^\d{4}$/; // NJ format
		regExps[1] = /^[A-Z]{2}00\d{4}000$/; // NJ format
		regExps[2] = /^FD\d{6}[A-Z]{1}$/; // PA format
		regExps[3] = /^[A-Z]{1}\d{8}$/; // DE format
		regExps[4] = /^\d{6}$/;
		regExps[5] = /^[A-Z]{2}\d{6}$/;
		regExps[6] = /^\d{6}[A-Z]{1}$/;

		// iterate through regular expressions
		for ( var i = 0; i < regExps.length; i++ )
		{
			var rx = regExps[i];
			if ( rx.test(s.toUpperCase()) ) return true;
		}

		// return
		return false;

	}




//	Adds user to log file
	function userAdd() {

//		initialize valid flag
		var bValid = true;

//		set form object
		var f = document.forms["add"];

//		trim text and textarea fields
		trimFields(f);

//		validate fields (reverse tab order)
		var oFld, sFld, oFirstBad, sMsg = "";

		oFld = f.userEmail;
		sFld = oFld.value;
		if ( sFld.length < 6 ) {
			bValid = false;
			oFirstBad = oFld;
			sMsg = "- Your email address is missing or invalid\n" + sMsg;
		}

		oFld = f.userFHMailAddress;
		sFld = oFld.value;
		if ( sFld.length <= 0 ) {
			bValid = false;
			oFirstBad = oFld;
			sMsg = "- Your Funeral Home mailing address is missing\n" + sMsg;
		}

		oFld = f.userFHName;
		sFld = oFld.value;
		if ( sFld.length <= 0 ) {
			bValid = false;
			oFirstBad = oFld;
			sMsg = "- Your Funeral Home name is missing\n" + sMsg;
		}

//		did form validate?
		if ( !bValid ) {
			sMsg = "Unable to submit information for the following reason(s):\n\n" + sMsg;
			window.alert(sMsg);
			oFirstBad.select();
		}

//		set return value
		return bValid;

	}


//	Skip adding user to user log file
	function userSkipAdd() {

//		redirect to FD home
		window.location="http://www.philadelphiacrematories.com/fd";
		return false;

	}

