/*
	$Revision: 260 $

	Copyright (c) 2005 NetTeam Consulting, All Rights Reserved.
	http://www.netteamconsulting.com/
*/

var NTSPopupBodyColor = "#EBEBEB";
var NTSFormName = "ntform";
var NTSFormUpdate = false;
var NTSDateFormatNum = 1;

var NTSMonthShortNames = new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );
var NTSMonthNames = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
var NTSWeekDayNames = new Array( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" );
var NTSDaysInMonthLookup = new Array( null, 31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

function showProfile( url ) {
	var profileWin = popupWin( url, 450, 400 );
	profileWin.focus();
}

function showFieldHelp( pgid, fid ) {
	var helpFWin = window.open( "Help.jsp?pgid=" + pgid + "&fid=" + fid, "", "menubar=no,toolbar=no,statusbar=no,scrollbars=yes,width=300,height=200" );
	if( helpFWin ) helpFWin.focus();
}

function showPageHelp( pgid ) {
	var helpPWin = window.open( "Help.jsp?pgid=" + pgid, "", "menubar=no,toolbar=no,statusbar=no,scrollbars=yes,width=400,height=300" );
	if( helpPWin ) helpPWin.focus();
}


// date checker stuff
function checkDate( ipName ) {
	if( ipName != "" ) {
		if( document.forms && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements[ ipName ] && document.forms[ NTSFormName ].elements[ ipName ].value != "" && isNaN( new Date( document.forms[ NTSFormName ].elements[ ipName ].value ) ) ) {
			alert( "The date you have entered (" + document.forms[ NTSFormName ].elements[ ipName ].value + ") is not in a recognised format. Please use the MM/DD/YYYY format (e.g.: For September 25th, 2000 use 09/25/2000." );
			document.forms[ NTSFormName ].elements[ ipName ].value = oldDate;
		}
	}
}
function showCal( ipName ) {
	if( document.forms && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements[ ipName ] ) {
		if( document.forms[ NTSFormName ].elements[ ipName ].disabled ) alert( "You cannot enter a date here at this time. Either this field has been disabled or you have Current ticked." );
		else {
			if( document.getElementById || document.all ) {
				var calWin = window.open( "../client/popupcal.html?" + ipName, "", "menubar=no,toolbar=no,statusbar=no,scrollbars=yes,width=175,height=170" );
			}
			else {
				alert( "The Calendar is not supported for this browser. Simply type the date in MM/DD/YYYY format in the box provided" );
			}
		}
	}
}

function toggleYNDesc( elname ) {
	if( document.forms && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements ) {
		var fe = document.forms[ NTSFormName ].elements;
		if( fe[elname + '_yn'] && fe[elname + '_desc'] ) {
			if( fe[elname + '_yn'].value == "Y" ) fe[elname + '_desc'].disabled = false;
			else {
				fe[elname + '_desc'].value = "";
				fe[elname + '_desc'].disabled = true;
			}
		}
	}
}

function doStdReset( sFormName ) {
	NTSFormUpdate = false;
	sFormNameToUse = NTSFormName;
	if( sFormName ) sFormNameToUse = sFormName;
	if( document.forms && document.forms[ sFormNameToUse ] ) document.forms[ sFormNameToUse ].reset();
}
function doStdCancel( sURL ) {
	var b = false;
	if( typeof( NTSFormUpdate ) != "undefined" && NTSFormUpdate ) {
		if( confirm( 'Are you sure you want to abandon your changes? If you are, click OK to abandon the changes.' ) ) b = true;
	}
	else b = true;
	if( b ) window.location = sURL;
}

function frmUpdate() {
	NTSFormUpdate = true;
}

function validateCurrency( frmObj ) {
	if( frmObj && frmObj.name ) {
		var bValid = isValidFieldData( frmObj.name, "currency", false );
		if( !bValid ) {
			alert( "The amount you have entered does not appear to be valid number. Please enter positive numbers only. Up to two decimal places are allowed. E.g. 300 or 2.99 or 0.12" );
			frmObj.value = "";
		}
	}
}

function validateDate( frmObj ) {
	if( frmObj && frmObj.name ) {
		var bValid = isValidFieldData( frmObj.name, "date", false );
		if( !bValid ) {
			if( NTSDateFormatNum == 3 ) alert( "The date you have entered does not appear to be valid. Please enter the date in the form of dd-MMM-yyyy. For example \"22-Mar-2006\" (without the quotes)" );
			else if( NTSDateFormatNum == 2 ) alert( "The date you have entered does not appear to be valid. Please enter the date in the form of dd/mm/yyyy. For example \"22/03/2006\" (without the quotes)" );
			else if( NTSDateFormatNum == 1 ) alert( "The date you have entered does not appear to be valid. Please enter the date in the form of mm/dd/yyyy. For example \"03/22/2006\" (without the quotes)" );
			frmObj.value = "";
			frmObj.focus();
		}
	}
}

function validateWholeNumber( frmObj ) {
	if( frmObj && frmObj.name ) {
		var bValid = isValidFieldData( frmObj.name, "integer", false );
		if( !bValid ) {
			alert( "The amount you have entered does not appear to be valid whole number. Please enter positive numbers only, with no decimal places are allowed. E.g. 300 or 99 or 12" );
			frmObj.value = "";
		}
	}
}

function showAddTask( parentIDURL ) {
	var p = "";
	if( parentIDURL ) p = parentIDURL;
	var w = window.open( "", "", "scrollbars=yes,menubar=no,toolbar=no,statusbar=no,width=400,height=250" );
	w.focus();
	var pd = w.document;
	pd.open();
	pd.writeln( "<html><body bgcolor=\"" + NTSPopupBodyColor + "\">Loading...</body></html>" );
	pd.close();
	w.location = "TaskAdd" + p;
}

function showAddProject( parentIDURL ) {
	var p = "";
	if( parentIDURL ) p = parentIDURL;
	var w = window.open( "", "", "scrollbars=yes,menubar=no,toolbar=no,statusbar=no,width=400,height=250" );
	w.focus();
	var pd = w.document;
	pd.open();
	pd.writeln( "<html><body bgcolor=\"" + NTSPopupBodyColor + "\">Loading...</body></html>" );
	pd.close();
	w.location = "ProjectAdd" + p;
}

function showAddDoc( parentIDURL ) {
	var p = "";
	if( parentIDURL ) p = parentIDURL;
	var w = window.open( "", "", "scrollbars=yes,menubar=no,toolbar=no,statusbar=no,width=400,height=200" );
	w.focus();
	var pd = w.document;
	pd.open();
	pd.writeln( "<html><body bgcolor=\"" + NTSPopupBodyColor + "\">Loading...</body></html>" );
	pd.close();
	w.location = "DocUpload" + p;
}
function getSelectedRadioProps( frmElName ) {
	var s = "";
	if( document.forms && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements[ frmElName ] ) {
		var el = document.forms[ NTSFormName ].elements[ frmElName ];
		if( el.length ) {
			for( var i = 0; i < el.length; i++ ) {
				if( el[i].checked ) {
					s = el[i].value;
					break;
				}
			}
		}
		else s = el.value;
	}
	return s;
}

function isRadioButtonSelected( frmElName ) {
	var checked = false;
	if( document && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements[ frmElName ] ) {
		var e = document.forms[ NTSFormName ].elements[ frmElName ];
		if( e.length ) {
			for( var i=0; i < e.length; i++ ) {
				if( e[i].checked ) { checked = true; }
			}
		}
		else if( e.checked ) checked = true;
	}
	return checked;
}

function popupWin( url, w, h ) {
	return popupNamedWin( url, "", w, h );
}

function popupNamedWin( url, name, w, h ) {
	var popupWin = window.open( "", name, "resizable=1,scrollbars=1,menubar=0,toolbar=0,statusbar=0,width=" + w + ",height=" + h );
	popupWin.focus();
	var pw = popupWin.document;
	pw.open();
	pw.writeln( "<html><body bgcolor=\"" + NTSPopupBodyColor + "\">Loading...</body></html>" );
	pw.close();
	if( url && url != "" ) popupWin.location.replace( url );
	return popupWin;
}

function howDoI() {
	var howPWin = window.open( "HowDoI", "", "menubar=no,toolbar=no,resizable=yes,statusbar=no,scrollbars=yes,width=500,height=350" );
	if( howPWin ) howPWin.focus();
}

function imgSwap( n, s ) {
	if( document.images && document.images[ n ] ) {
		var cur = document.images[ n ].src;
		if( cur ) {
			var i = cur.lastIndexOf( '/' );
			if( i ) {
				document.images[ n ].src = cur.substring( 0, i + 1 ) + s;
			}
		}
	}
}

function logout() {
	if( confirm( 'Are you sure you want to logout (any unsaved changes will be lost)?' ) ) window.location = '/logout';
}

// Field Validation
function isValidFieldData( field, type, mandatory ) {
	var success = false;
	if( document && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements[ field ] ) {
		// validate social security number
		if( type == "ssn" ) {
			var ssn = document.forms[ NTSFormName ].elements[ field ].value;
			if( ssn == "" && !mandatory ) success = true;
			else if( ssn != "" ) {
				// must be 9 digits with an optional couple of dashes in there, can't lead with 000
				var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
				var numDashes = ssn.split('-').length - 1;
				if( !( (matchArr == null ) || ( numDashes == 1 ) || ( parseInt( matchArr[1],10 ) == 0 ) ) )
					success = true;
			}
		}
		else if( type == "currency" ) {
			var cur = document.forms[ NTSFormName ].elements[ field ].value;
			if( cur == "" && !mandatory ) success = true;
			else if( cur != "" ) {
				if( !isNaN( cur ) && cur >= 0 ) {
					var re = /^([0-9])*\.([0-9]*)$/
					if( re.test( cur ) ) {
						if( RegExp.$2 ) {
							if( RegExp.$2.length <= 2 ) success = true;
						}
					}
				}
			}
		}
		else if( type == "integer" ) {
			var cur = document.forms[ NTSFormName ].elements[ field ].value;
			if( cur == "" && !mandatory ) success = true;
			else if( cur != "" ) {
				if( !isNaN( cur ) && cur >= 0 ) {
					var re = /^([0-9])*$/
					if( re.test( cur ) ) {
						if( RegExp.$1 ) success = true;
					}
				}
			}
		}
		else if( type == "date" ) {
			var objRegExp;
			var cur = document.forms[ NTSFormName ].elements[ field ].value;

			if( cur == "" && !mandatory ) success = true;
			else if( cur != "" ) {
				if( NTSDateFormatNum == 1 || NTSDateFormatNum == 2 ) {
					// mm/dd/yyyy , mm-dd-yyyy, or dd/mm/yyyy
					objRegExp = /^(\d{1,2})[\-\/\.](\d{1,2})[\-\/\.](\d{4})$/
				}
				else if( NTSDateFormatNum == 3 ) {
					// dd-MMM-yyyy
					// var re = /^([0-9][0-9]*)-([A-Za-z])([A-Za-z][A-Za-z])-([0-9][0-9][0-9][0-9]*)$/;
					objRegExp = /^([0-9]{1,2})-([A-Za-z])([A-Za-z]{2})-([0-9]{4})$/;
				}

				if( objRegExp.test( cur ) ) {
					if( NTSDateFormatNum == 1 || NTSDateFormatNum == 2 ) {
						var day;
						var year;
						var month;

						if( NTSDateFormatNum == 1 ) {
							var day = Number( RegExp.$2 );
							var month = Number( RegExp.$1 );
							var year = Number( RegExp.$3 );
						}
						else if( NTSDateFormatNum == 2 ) {
							var day = Number( RegExp.$1 );
							var month = Number( RegExp.$2 );
							var year = Number( RegExp.$3 );
						}

						if( !isNaN( day ) && !isNaN( month ) && !isNaN( year ) ) {
							if( year >= 1900 ) {
								if( month > 0 && month <=12 ) {
									if( month == 2 ) {
										if( isLeapYear( year ) ) {
											if( day <= 29 ) success = true;
										}
										else if( day <= 28 ) success = true;
									}
									else if ( day > 0 && day <= NTSDaysInMonthLookup[ month ] ) {
										success = true;
									}
								}
							}
						}
					}
					else if( NTSDateFormatNum == 3 ) {
						var day = RegExp.$1;
						var year = RegExp.$4;
						var month = -1;
						for( var i = 0; i < NTSMonthShortNames.length; i++ ) {
							if( NTSMonthShortNames[i] == (RegExp.$2).toUpperCase() + RegExp.$3 ) month = i;
						}
						var d = new Date( year, month, day );
						if( month > -1 && !isNaN( d ) ) {
							document.forms[ NTSFormName ].elements[ field ].value = d.getDate() + "-" + NTSMonthShortNames[ d.getMonth() ] + "-" + d.getFullYear();
							success = true;
						}
					}
				}
			}
		}
	}
	return success;
}

function toggleItemDisplay( id ) {
	var item = null;
	if( document.getElementById ) item = document.getElementById( id );
	else if( document.all ) item = document.all[id];
	else if( document.layers ) item = document.layers[id];
	
	if( item != null ) {
		if( item.style.display == 'none' ) item.style.display = '';
		else item.style.display = 'none';
	}
	return false;
}

function getCookie( sCookieName ) {
	var iStart = document.cookie.indexOf( sCookieName + "=" );
	var iLen = iStart + sCookieName.length + 1;
	if( !iStart && sCookieName != document.cookie.substring( 0, sCookieName.length ) ) return null;
	if( iStart == -1 ) return null;
	var iEnd = document.cookie.indexOf( ";", iLen );
	if( iEnd == -1 ) iEnd = document.cookie.length;
	return unescape( document.cookie.substring( iLen, iEnd ) );
}

function setCookie( sCookieName, sValue, sExpires, sPath, sDomain, bSecure ) {
	document.cookie = sCookieName + "=" + escape(sValue) + ( sExpires ? ";expires=" + expires.toGMTString():"" ) + ( sPath ? ";path=" + sPath:"" ) + ( sDomain ? ";domain=" + sDomain:"" ) + ( bSecure ? ";secure":"" );
}

function deleteCookie(sCookieName,sPath,sDomain) {
	if( getCookie( sCookieName ) ) document.cookie = sCookieName + "=" + ( sPath ? ";path=" + sPath:"" ) + ( sDomain ? ";domain=" + sDomain:"" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function checkTime( obj ) {
	var ok = false;
	if( obj ) {
		if( obj.value.length == 0 ) {
			ok = true;
		}
		else if( obj.value.length == 5 ) {
			if( !isNaN( obj.value.substr( 0, 2 ) ) && Number( obj.value.substr( 0, 2 ) ) <= 24 ) {
				if( !isNaN( obj.value.substr( 3, 2 ) ) && Number( obj.value.substr( 3, 2 ) ) <= 59 ) {
					if( obj.value.substr( 2, 1 ) == ":" ) {
						ok = true;
					}
				}
			}
		}
	}
	if( !ok ) {
		obj.value = "";
		alert( "Please enter a time in HH:MM format, or leave blank if unknown at this point" );
		obj.focus();
	}
}

function checkForPopupBlocker() {
	var b = false;
	var w = window.open( "", "", "width=1,height=1" );
	if( w ) { w.close(); }
	else b = true;
	return b;
}

// Check or uncheck all checkboxes with the specified field name
function checkAll( sFieldName, bCheck ) {
	if( document && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements[ sFieldName ] ) {
		var f = document.forms[ NTSFormName ].elements[ sFieldName ];
		if( f ) {
			if( f.length ) {
				for( var i = 0; i < f.length; i++ ) {
					f[i].checked = bCheck;
				}
			}
			else {
				f.checked = bCheck;
			}
		}
	}
}

// Determine if the specified Checkbox element name is checked or, if it's an array of Checkbox elements, any of them are checked
function isCheckboxSelected( frmElName ) {
	var checked = false;
	if( document && document.forms[ NTSFormName ] && document.forms[ NTSFormName ].elements[ frmElName ] ) {
		var e = document.forms[ NTSFormName ].elements[ frmElName ];
		if( e.length ) {
			for( var i=0; i < e.length; i++ ) {
				if( e[i].checked ) { checked = true; }
			}
		}
		else if( e.checked ) checked = true;
	}
	return checked;
}

// .cg 01Sep05 - Determine if the given year is a leap year
function isLeapYear( year ) {
	return new Date( year, 1, 29 ).getDate() == 29;
}

// .cg 05Nov05 - Draw help text tooltip
function drawTabHelp( el, ev, text, tipCSS, hoverCSS ) {
	if( el && el.className ) el.className = tipCSS;

	if( typeof( domTT_offsetX ) != "undefined" && typeof( domLib_userAgent ) != "undefined" ) {
		domTT_activate( el, ev, 'content', text, 'styleClass', hoverCSS, "maxWidth", "250" )
	}
}
function clearTabHelp( el, tipCSS ) {
	if( el && el.className ) el.className = tipCSS;
}

function getForm( frmName ) {
    if( frmName && frmName != "" && document.forms && document.forms[ frmName ] ) return document.forms[ frmName ];
    else return null;
}
function getFormElement( sFormName, sElementName ) {
	if( sFormName && sElementName ) {
		var f = getForm( sFormName );
		if( f ) {
			if( f.elements[ sElementName ] ) return f.elements[ sElementName ];
			else return null;
		}
		else return null;
	}
	else return null;
}

function twoDecimalNum( num ) {
	var pm = num < 0 ? "-" : "";
	var roundedNum = Math.abs( Math.round( num * 100 ) );
	var sRet = roundedNum.toString();
  	if( roundedNum < 10 ) sRet = "0.0" + sRet;
  	else if( roundedNum < 100 ) sRet = "0." + sRet;
	else sRet = sRet.substring( 0, ( sRet.length - 2 ) ) + "." + sRet.substring( ( sRet.length - 2 ), sRet.length );
	return pm + sRet;
}


function formatNumber( expr, decimals ) {
	var str = "" + Math.round( eval(expr) * Math.pow(10,decimals))
	while (str.length <= decimals) { str = "0" + str } 
	var decpoint = str.length - decimals; 
	var result = str.substring(0,decpoint);
	if (decimals) result += "." + str.substring(decpoint,str.length);
	return result;
}

function setOnlyClass( el, sClassName ) {
	if( el && sClassName ) {
		el.removeClass();
		el.addClass( sClassName );
	}
}

