/*

	EI Associates Projects JavaScript library
	(c) 2007 EI Associates. All Rights Reserved
	Developed by AppWare, LLC. (www.appware.biz)
	Module: projects.js
	Modified: 18-Jul-2207
	Created: 11-Jul-2007

*/




//	Returns string with leading and trailing spaces trimmed
	function trim(s) {
		return s.replace(/^\s*|\s*$/g,"");
	}
//	--------------------




//	Returns true if login form validates
	function frmLogin_IsValid() {

//		Initialize error message
		var ErrMsg = "";

//		Set form
		var f = document.frmLogin;

//		Trim text fields
		var oName = f.txtUserName;
		var oPwd = f.pwdUserPassword;
		oName.value = trim(oName.value);
		oPwd.value = trim(oPwd.value);

//		Validate fields
		if ( !IsUserName(oName.value) ) {
			ErrMsg = ErrMsg + "     - Your user name is missing or invalid\n";
		}
		if ( !IsPassword(oPwd.value) ) {
			ErrMsg = ErrMsg + "     - Your password is missing or invalid\n";
		}

//		Did form validate?
		if ( ErrMsg.length > 0 ) {
			window.alert("The following error(s) occurred while processing your request:\n\n" + ErrMsg);
			oName.focus()
			return false;
		}
		else {
			return true;
		}

	}
//	--------------------




//	Returns true if user name validates
	function IsUserName(s) {

//		Set and test regular expression
		var oRX = /^\w{5,32}$/;
		if ( oRX.test(s) ) {
			return true;
		}
		return false;

	}
//	--------------------




//	Returns true if password validates
	function IsPassword(s) {

//		Set and test regular expression
		var oRX = /^\w{5,32}$/;
		if ( oRX.test(s) ) {
			return true;
		}
		return false;

	}
//	--------------------




//	Returns true if new user form validates
	function frmUserNew_IsValid() {

//		Initialize error message
		var ErrMsg = "";

//		Set form
		var f = document.frmUserNew;

//		Trim text fields
		var oDesc = f.txtUserDescription;
		var oName = f.txtUserName;
		var oPwd = f.txtUserPassword;
		oDesc.value = trim(oDesc.value);
		oName.value = trim(oName.value);
		oPwd.value = trim(oPwd.value);

//		Validate fields
		if ( oDesc.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The description is missing\n";
		}
		if ( !IsUserName(oName.value) ) {
			ErrMsg = ErrMsg + "     - The login name is missing or invalid\n";
		}
		if ( !IsPassword(oPwd.value) ) {
			ErrMsg = ErrMsg + "     - The password is missing or invalid\n";
		}

//		Did form validate?
		if ( ErrMsg.length > 0 ) {
			window.alert("The following error(s) occurred while processing your request:\n\n" + ErrMsg);
			oDesc.focus()
			return false;
		}
		else {
			return true;
		}

	}
//	--------------------




//	Returns true if edit user form validates
	function frmUserEdit_IsValid() {

//		Initialize error message
		var ErrMsg = "";

//		Set form
		var f = document.frmUserEdit;

//		Trim text fields
		var oDesc = f.txtUserDescription;
		var oName = f.txtUserName;
		var oPwd = f.txtUserPassword;
		oDesc.value = trim(oDesc.value);
		oName.value = trim(oName.value);
		oPwd.value = trim(oPwd.value);

//		Validate fields
		if ( oDesc.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The description is missing\n";
		}
		if ( !IsUserName(oName.value) ) {
			ErrMsg = ErrMsg + "     - The login name is missing or invalid\n";
		}
		if ( !IsPassword(oPwd.value) ) {
			ErrMsg = ErrMsg + "     - The password is missing or invalid\n";
		}

//		Did form validate?
		if ( ErrMsg.length > 0 ) {
			window.alert("The following error(s) occurred while processing your request:\n\n" + ErrMsg);
			oDesc.focus()
			return false;
		}
		else {
			return true;
		}

	}
//	--------------------




//	Returns true if new project form validates
	function frmProjectNew_IsValid() {

//		Initialize error message
		var ErrMsg = "";

//		Set form
		var f = document.frmProjectNew;

//		Trim text fields
		for ( var i = 0; i < f.elements.length; i++ ) {
			var e = f.elements[i];
			if ( e.type == "text" || e.type == "textarea" || e.type == "password" ) {
				var s = e.value;
				e.value = trim(s);
			}
		}

//		Validate fields
		e = f.txtProjectName;
		if ( e.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The name is missing\n";
		}
		e = f.txtProjectJobNumber;
		if ( e.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The job number is missing\n";
		}
		e = f.txtProjectStatus;
		if ( e.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The status is missing\n";
		}
		e = f.txtProjectPreBidDate;
		if ( e.value.length > 0 ) {
			if ( ! isDate(e.value) ) {
				ErrMsg = ErrMsg + "     - The pre-bid date is not a valid format (m/d/yyyy)\n";
			}
		}
		e = f.txtProjectBidDueDate;
		if ( e.value.length > 0 ) {
			if ( ! isDate(e.value) ) {
				ErrMsg = ErrMsg + "     - The bid due date is not a valid format (m/d/yyyy)\n";
			}
		}

//		Did form validate?
		if ( ErrMsg.length > 0 ) {
			window.alert("The following error(s) occurred while processing your request:\n\n" + ErrMsg);
			f.txtProjectName.focus()
			return false;
		}
		else {
			return true;
		}

	}
//	--------------------




//	Returns true if edit project form validates
	function frmProjectEdit_IsValid() {

//		Initialize error message
		var ErrMsg = "";

//		Set form
		var f = document.frmProjectEdit;

//		Trim text fields
		for ( var i = 0; i < f.elements.length; i++ ) {
			var e = f.elements[i];
			if ( e.type == "text" || e.type == "textarea" || e.type == "password" ) {
				var s = e.value;
				e.value = trim(s);
			}
		}

//		Validate fields
		e = f.txtProjectName;
		if ( e.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The name is missing\n";
		}
		e = f.txtProjectJobNumber;
		if ( e.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The job number is missing\n";
		}
		e = f.txtProjectStatus;
		if ( e.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - The status is missing\n";
		}
		e = f.txtProjectPreBidDate;
		if ( e.value.length > 0 ) {
			if ( ! isDate(e.value) ) {
				ErrMsg = ErrMsg + "     - The pre-bid date is not a valid format (m/d/yyyy)\n";
			}
		}
		e = f.txtProjectBidDueDate;
		if ( e.value.length > 0 ) {
			if ( ! isDate(e.value) ) {
				ErrMsg = ErrMsg + "     - The bid due date is not a valid format (m/d/yyyy)\n";
			}
		}

//		Did form validate?
		if ( ErrMsg.length > 0 ) {
			window.alert("The following error(s) occurred while processing your request:\n\n" + ErrMsg);
			f.txtProjectName.focus()
			return false;
		}
		else {
			return true;
		}

	}
//	--------------------




//	Returns true if new file form validates
	function frmFile_IsValid() {

//		Initialize error message
		var ErrMsg = "";

//		Set form
		var f = document.frmFile;

//		Trim text fields
		for ( var i = 0; i < f.elements.length; i++ ) {
			var e = f.elements[i];
			if ( e.type == "text" || e.type == "textarea" || e.type == "password" ) {
				var s = e.value;
				e.value = trim(s);
			}
		}

//		Validate fields
		if ( f.radFileCategory[0].checked ) {
			if ( f.txtFileDescription.value.length == 0 ) {
				ErrMsg = ErrMsg + "     - The description is missing\n";
			}
		}
		if ( f.filFileName.value.length == 0 ) {
			ErrMsg = ErrMsg + "     - You must select a file to upload\n";
		}

//		Did form validate?
		if ( ErrMsg.length > 0 ) {
			window.alert("The following error(s) occurred while processing your request:\n\n" + ErrMsg);
			f.radFileCategory[0].focus();
			return false;
		}
		else {
			ShowProgress();
			return true;
		}

	}
//	--------------------




//	Toggles visibility of file description
	function frmFile_ToggleDescription(index) {
		if ( index == 0 ) {
			document.getElementById("fileCategory1").style.display = "none";
			document.getElementById("fileCategory0").style.display = "block";
		}
		else {
			document.getElementById("fileCategory0").style.display = "none";
			document.getElementById("fileCategory1").style.display = "block";
		}
	}
//	--------------------




//	Toggles visibility of client project file list
	function toggleFileList(index) {
		var flId = "fl" + index;
		var imgId = "img" + index;
		if ( document.getElementById(flId).style.display == "block" ) {
			document.getElementById(flId).style.display = "none";
			document.getElementById(imgId).src = "http://www.eiassoc.com/res/images/icons/plus_icon.gif";
			document.getElementById(imgId).title = "Expand File List";
		}
		else {
			document.getElementById(flId).style.display = "block";
			document.getElementById(imgId).src = "http://www.eiassoc.com/res/images/icons/minus_icon.gif";
			document.getElementById(imgId).title = "Collapse File List";
		}
	}
//	--------------------




//	Returns true if string is valid date (m/d/yyyy format)
	function isDate(s) {

//		Split date string
		var sParts = s.split("/");

//		Convert date parts to numbers
		var iMM = new Number(sParts[0]);
		if ( isNaN(iMM) ) { return false; }
		if ( iMM < 1 || iMM > 12 ) { return false; }

		var iDD = new Number(sParts[1]);
		if ( isNaN(iDD) ) { return false; }
		if ( iDD < 1 || iDD > 31 ) { return false; }

		var iYYYY = new Number(sParts[2]);
		if ( isNaN(iYYYY) ) { return false; }
		if ( iYYYY < 1 || iYYYY > 9999 ) { return false; }
		var sYYYY = sParts[2];
		if ( sYYYY.length != 4 ) { return false; }

//		Set return value
		return true;

	}
//	--------------------




