<!-- Hide script from old browsers
/* COL CALCULATOR JAVASCRIPT */

	// Initiate Auto Suggest
		function InitAutoSuggests() {
			ResetCitySelection('FC');
			ResetCitySelection('TC');
			// From City Lookup
				var options = {
					script:"index.cfm?FuseAction=COLCalculatorII.CityLookUp&",
					varname:"Q",
					minchars:2,
					className:"autosuggest",
					delay:200,
					timeout:50000,
					cache:true,
					offsety:0,
					shownoresults:true,
					noresults:"Unable to find a matching city",
					json:true,
					maxresults:10,
					callback:function (obj) {
						var CityID = GetInList(obj.id,0,'|');
						var StateID = GetInList(obj.id,1,'|');
						var CountryID = GetInList(obj.id,2,'|');
						// Set Hidden Form Var
							document.getElementById('FromCityID').value = CityID;
							document.getElementById('FromStateID').value = StateID;
							document.getElementById('FromCountryID').value = CountryID;
					}
				};
				var AC1 = new bsn.AutoSuggest('FC', options);
				
			// To City Lookup
				var options = {
					script:"index.cfm?FuseAction=COLCalculatorII.CityLookUp&",
					varname:"Q",
					minchars:2,
					className:"autosuggest",
					delay:200,
					timeout:50000,
					cache:true,
					offsety:0,
					shownoresults:true,
					noresults:"Unable to find a matching city",
					json:true,
					maxresults:10,
					callback:function (obj) {
						var CityID = GetInList(obj.id,0,'|');
						var StateID = GetInList(obj.id,1,'|');
						var CountryID = GetInList(obj.id,2,'|');
						// Set Hidden Form Var
							document.getElementById('ToCityID').value = CityID;
							document.getElementById('ToStateID').value = StateID;
							document.getElementById('ToCountryID').value = CountryID;
					}
				};
				var AC2 = new bsn.AutoSuggest('TC', options);
		}
		
		
	// Submit COL Inputs
		function SubmitCOLInputs(FormName) {
			// Clear Error Msg	
				ClearErrorMsg();
			// Error Count
				var ErrorCnt = 0;
				var ErrorMsg = '';
				var ErrMsgObj = document.getElementById('COLInputModuleErrorMsg');
			// Set Form Info
				var FormObj = eval('document.' + FormName);
			// Get Form Input Values
				var Salary = FormObj.Salary.value;
				Salary = trim(Salary);
				
				var FromCityID = FormObj.FromCityID.value;
				var FromStateID = FormObj.FromStateID.value;
				var FromCountryID = FormObj.FromCountryID.value;
				
				var ToCityID = FormObj.ToCityID.value;
				var ToStateID = FormObj.ToStateID.value;
				var ToCountryID = FormObj.ToCountryID.value;
			// Determine if salary was submitted
				if (Salary.length == 0) {
					// NOT SUBMITTED
						// Set & Display Error
							ErrorCnt = ErrorCnt + 1;
							ErrorMsg = ErrorMsg + '<li>You must enter your current salary.</li>';
				}
				else {
					// SUBMITTED
						Salary = ConvertCurrencyToNumber(Salary);
						// Ensure user entered a salary
							if (Salary != parseInt(Salary)){
								// Set & Display Error
									ErrorCnt = ErrorCnt + 1;
									ErrorMsg = ErrorMsg + '<li>You salary must be a valid number.</li>';
							}
							else {
								if (Salary <= 0) {
									ErrorCnt = ErrorCnt + 1;
									ErrorMsg = ErrorMsg + '<li>Your salary must be greater than 0.</li>';
								}
							}
				}
			// Check for Errors
				if (ErrorCnt > 0) {
					// ERRORS
						
				}
				else {
					// NO ERRORS
						FormObj.Salary.value = Salary;
				}
			// Ensure user selected both a from and to city
				//alert('FromCityID: ' + FromCityID + ' ToCityID: ' + ToCityID);
				if ((trim(FromCityID).length > 0) && (trim(ToCityID).length > 0)) {
					// SELECTED BOTH
						// Ensure user selected cities from same country
							if (FromCountryID == ToCountryID) {
								// SAME COUNTRY
							}
							else {
								// Set & Display Error
									ErrorCnt = ErrorCnt + 1;
									ErrorMsg = ErrorMsg + '<li>Only cost of living comparisons within a country are allowed.  Please select cities from the same country.</li>';
							}
						
				}
				else {
					// DID NOT SELECT BOTH
						if ((trim(FromCityID).length == 0) && (trim(ToCityID).length  == 0)) {
							// NEITHER SELECTED
								ErrorCnt = ErrorCnt + 1;
								ErrorMsg = ErrorMsg + '<li>You must select a &ldquo;Moving From&rdquo; and &ldquo;Moving To&rdquo; city.</li>';
						}
						else {
							// ONE SELECTED
								if (trim(FromCityID).length == 0) {
									ErrorCnt = ErrorCnt + 1;
									ErrorMsg = ErrorMsg + '<li>You must select a &ldquo;Moving From&rdquo; city.</li>';
								}
								if (trim(ToCityID).length == 0) {
									ErrorCnt = ErrorCnt + 1;
									ErrorMsg = ErrorMsg + '<li>You must select a &ldquo;Moving To&rdquo; city.</li>';
								}
						}
				}
			
			// Check for Errors
				if (ErrorCnt > 0) {
					// ERRORS
						ErrMsgObj.innerHTML = '<ul>' + ErrorMsg + '</ul>';
						ErrMsgObj.style.display='block';
				}
				else {
					// NO ERRORS
						// Submit Form   
							FormObj.submit();
				}
		}	
		
		
	// Validate COL Questionnaire Input	
		function ValidateCOLQuestionnaireInput(FrmInputID,ErrorDspID){
			// Error Info
				var ErrorCnt = 0;
				var ErrorMsg = '';
				var ErrMsgObj = document.getElementById(ErrorDspID);
			// Clear Error Msg
				ClearCOLQuestionnaireErrorMsg(ErrorDspID,FrmInputID);
			// Form Input Info
				var FrmInputObj = document.getElementById(FrmInputID);
				var FrmInputValue = trim(FrmInputObj.value);
			// Determine if input was submitted
				if (FrmInputValue.length == 0) {
					// NOT SUBMITTED
						// Set & Display Error
							ErrorCnt = ErrorCnt + 1;
							ErrorMsg = ErrorMsg + 'You must provide a value for this input.';
				}
				else {
					// SUBMITTED
						FrmInputValue = ConvertCurrencyToNumber(FrmInputValue);
						// Ensure user entered a numeric value
							if (FrmInputValue != parseFloat(FrmInputValue)){
								// Set & Display Error
									ErrorCnt = ErrorCnt + 1;
									ErrorMsg = ErrorMsg + 'You must provide a numeric value for this input.';
							}
							else {
								if (FrmInputValue <= 0) {
									ErrorCnt = ErrorCnt + 1;
									ErrorMsg = ErrorMsg + 'You must provide a number greater than 0.';
								}
							}
				}
			// Check for Errors
				if (ErrorCnt > 0) {
					// ERRORS
						FrmInputObj.style.borderColor='red';
						FrmInputObj.style.borderWidth='2px';
						
						ErrMsgObj.innerHTML = ErrorMsg;
						ErrMsgObj.style.color = 'red';
						ErrMsgObj.style.fontWeight = 'bold';
						ErrMsgObj.style.display='block';
				}
				else {
					FrmInputObj.value=FrmInputValue;			
				}
			// Return Error Count
				return ErrorCnt;
		}
		
		
	// Reset COL Questionnaire Error Msg
		function ClearCOLQuestionnaireErrorMsg(ErrorDspID,FrmInputID) {
			var ErrMsgObj = document.getElementById(ErrorDspID);
			ErrMsgObj.style.display='none';
			ErrMsgObj.innerHTML = '';
			
			var FrmInputObj = document.getElementById(FrmInputID);
			FrmInputObj.style.borderColor='#ababab';
			FrmInputObj.style.borderWidth='1px';
		}
		
	// Submit COL Questionnaire
		function SubmitCOLQuestionnaire(FormName,QuestionCnt) {
			// Default Vars	
				var ErrorCnt = 0;
				var FrmInputID = '';
				var ErrorDspID = '';
			// Set Form Info
				var FormObj = eval('document.' + FormName);
			// Loop over Question List
				var i=1
				for (i=1; i<=QuestionCnt; i++) {
					FrmInputID = 'COLQ_' + i;
					ErrorDspID = 'COLQ_' + i + '_ErrorDsp';
					ErrorCnt = ErrorCnt + ValidateCOLQuestionnaireInput(FrmInputID,ErrorDspID);
				}
			// Determine if Errors were found
				if (ErrorCnt > 0) {
					// ERRORS
						if (ErrorCnt == 1) {
							// Single Error
								alert('There was a problem processing one of your COL inputs.  Please review the error message and make the necessary change.');
						}
						else {
							// More than 1 Error
								alert('There were problems processing your COL inputs.  Please review the error messages and make the necessary changes.');
						}
				}
				else {
					// NO ERRORS
						// Submit Form
							FormObj.submit();
				}
		}	
		
		
	// Convert Currency to Number	
		function ConvertCurrencyToNumber(value){
			//Default 
				num = 0;
			//Strip Dollar Sign
				value = value.replace(/\$/g,''); 
			// Strip Commas
				temp = new RegExp(",", "g");
				num = value.replace(temp, "");
			num = num.replace('\\', "");
			num = Number(num);
			return num;
		}
		
	// Reset Error Msg
		function ClearErrorMsg() {
			var ErrMsgObj = document.getElementById('COLInputModuleErrorMsg');
			ErrMsgObj.style.display='none';
			ErrMsgObj.innerHTML = '';
		}	
		
	// Reset City Selections
		function ResetCitySelection(CityType) {
			var FormName = 'Frm_COLCal_NA';
			// Set Form Info
				var FormObj = eval('document.' + FormName);
			// Determine City Type
				if (CityType == 'FC') {
					FormObj.FromCityID.value='';
					FormObj.FromStateID.value='';
					FormObj.FromCountryID.value='';
				}
				else {
					FormObj.ToCityID.value='';
					FormObj.ToStateID.value='';
					FormObj.ToCountryID.value='';
				}
		}	
		
		
		
// TRIM FUNCTIONS
	function trim(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	function ltrim(stringToTrim) {
		return stringToTrim.replace(/^\s+/,"");
	}
	function rtrim(stringToTrim) {
		return stringToTrim.replace(/\s+$/,"");
	}
	function StripWhiteSpace(stringToTrim) {
		var StrippedString;
	  	StrippedString = stringToTrim.replace(/^\s*/, "").replace(/\s*$/, ""); 
	  	StrippedString = StrippedString.replace(/\s{2,}/, " "); 
	  	return StrippedString;
	}		
		
	
	
	/*/											
	GetInList() 
	
	takes three parameters: 
	
	list = string containing the actual list
	index = the index to be set
	delim = the delimiter used to create the list
	
	/*/
	
	/*/
	/ / PURPOSE:
	/ /		To extract an entry from a list.
	/ /
	/ / COMMENTS:
	/ /		Use index to specify which item in the list.  Also, delim
	/ /		must match whatever the delimeter is.  Returns false if
	/ /		the index is not found or the value if it was, or the
	/ /		original value if it's not a list.
	/*/
	
	function GetInList (list, index, delim)
	{
		var flag = false, curr = 0;
		var posStart = "", posStop = "";
	
		// first, look for at least one occurance of the delimeter
		// if we can't find one, then just return the original
		if(list.indexOf(delim) == -1) return list;
	
		// alright, let's go through the string one character at a time
		for(x=0; x<list.length; x++)
		{
			/*/
			/ / We process if we find a delimeter, or we already found
			/ / a delimeter before and reached the end of the string.
			/*/
			if( (list.substr(x, 1) == delim) || (flag && (x == (list.length - 1))) )
			{
				// increment the current index if we need to
				if(index > 0) curr++;
	
				/*/ are we looking for the end or begining of the index? /*/
				if(flag)
				{	/*/ ending /*/
	
					/*/
					/ / Record the index for extraction later.  Remember, we want
					/ / the char before the delim, so we're done with this cycle.
					/ / But, we don't do this for the last index because there is
					/ / not delimeter for us to track, so we add one.
					/*/
					if(x == (list.length - 1))
						posStop = x + 1;
					else
						posStop = x;
					break;
				}
				else
				{	/*/ beginning /*/
	
					// did we find a match?
					if(curr == index)
					{
						/*/
						/ / We are on the index the caller wants.
						/ / So we record this for extraction later.
						/*/
	
						/*/ flag indicates we found the start /*/
						flag = true;
	
						/*/
						/ / Now, here's the tricky part.  If we're not on the first
						/ / index (0) we want posStart to be one greater than the
						/ / current iteration to pass up the delimeter; however,
						/ / if we are on the first index, we want posStart to be the
						/ / beginning and posStop to be what posStart was supposed to be.
						/*/
						if(curr == 0)
						{	/*/ zero /*/
							posStart = 0;
							posStop  = x;
	
							// we have the data we need
							break;
						}
						else
							/*/ non-zero /*/
							posStart = x + 1;
					}
				}
			}
		}
	
		/*/ if we made it here w/o flag being set, then we didn't find the index /*/
		if(!flag)
			return false;
		else
		{	/*/ we have what we need to extract the index /*/
	
			// return the data back to the caller
			return list.substring(posStart, posStop);
		}
	}				
// End hiding script from old browsers -->
