var OPTINPUT_No = 0;
var OPTINPUT_Yes = 1;

var EMPSTATUS_None = 0;
var EMPSTATUS_Retired = 1;
var EMPSTATUS_Employed = 2;
var EMPSTATUS_BusinessOwner = 3;
var EMPSTATUS_Homemaker = 4;
var EMPSTATUS_NotCurrentlyEmployed = 5;

var MARITALSTATUS_None = 0;
var MARITALSTATUS_Single = 1;
var MARITALSTATUS_Married = 2;
var MARITALSTATUS_Divorced = 3;
var MARITALSTATUS_Separated = 4;
var MARITALSTATUS_Widowed = 5;

var CLIENT_Client = 1;
var CLIENT_Spouse = 2;
var CLIENT_ClientSpouseJoint = 3;

var ViewOption_Advisor = 0;
var ViewOption_Broker = 1;

var digits = '0123456789';
var lowercaseLetters = 'abcdefghijklmnopqrstuvwxyz';
var uppercaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var whitespace = ' \t\n\r';
var decimalPointDelimiter = '.';
var invalidcharacters = '\'';


	function isZero(poControl){	
	  if (parseFloat(poControl.value) > 0) {
	    return false;
	  }
		if ((poControl.value == '') || (parseInt(poControl.value) == 0) || (isNaNEx(parseInt(poControl.value)))) {
			return true;
		}
		return false;
	}

	///////////////////////////////
	//isEmpty
	//	returns boolean indicating whether the object passed
	//	in is zero length string or not set at all
	///////////////////////////////
	function isEmpty(s){   
		s += '';
		return ((s == null) || (s.length == 0))
	}

	///////////////////////////////
	//isOptionSelected
	//	returns boolean indicating whether at least one
	//	option in an option group
	///////////////////////////////
  function isOptionSelected(poControl) {
	  if (poControl[0]) {
		  for (var llcounter = 0; llcounter < poControl.length; llcounter++) {
			  if (poControl[llcounter].checked) 
				  return true;
		  }
	  }
	  //There is only one option
	  else if (poControl) {
	    return (poControl.checked);
	  }	
	  return (isOptionSelected.arguments[1]==true);
	}

	///////////////////////////////
	//isComboSelected
	//	returns boolean indicating whether at least one
	//	option in an option group
	///////////////////////////////
	function isComboSelected(poControl) {
		var lsValue =  poControl.options[poControl.selectedIndex].value;
		if (isEmpty(lsValue))
			return (isComboSelected.arguments[1]==true);
		else
			return (true);
	}

	///////////////////////////////
	//stripInitialWhitespace
	//	equivalent to VB's LTrim$()
	///////////////////////////////
	function stripInitialWhitespace(s) {
		var i = 0;
	    s = convertString(s);
	    while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
	       i++;
	    
	    return s.substring (i, s.length);
	}
	
	function stripCharacter(psInput, psCharsToRemove){
		var i;
    var returnString = "";
		var lsCurrentChar;
		var lsInput = convertString(psInput);
		
			// Search through string's characters one by one.
			// If character is not in bag, append to returnString.
			for (i = 0; i < psInput.length; i++)
			{   
			    // Check that current character isn't whitespace.
			    lsCurrentChar = lsInput.charAt(i);
			    if (psCharsToRemove.indexOf(lsCurrentChar) == -1) 
						returnString += lsCurrentChar;
			}

			return returnString;	
	}
	
	function isNumberValid(poActiveControl, plMinimumValue, plMaximumValue, plNumberOfDecimalPlaces, pbFieldIsOptional) {
	var llValue;
	var lsValue;
		poActiveControl.value = stripCharacter(poActiveControl.value, ',')
		lsValue = stripInitialWhitespace(poActiveControl.value);	
		llValue = parseFloat(lsValue);

		if (isEmpty(lsValue)){
			poActiveControl.value = lsValue;
			if(pbFieldIsOptional)
				return true;
			else 
				return false;
		} 
		else if (isNaNEx(lsValue)) {
			return false;
		}
		else if (llValue < plMinimumValue) {
			return false;
		} 
		else if (llValue > plMaximumValue) {
			return false;
		} 
		else {
			poActiveControl.value = round(llValue,plNumberOfDecimalPlaces);
			return true;
		}	
	}
	
	///////////////////////////////
	//round
	//	round a number to specified number of decimal places
	///////////////////////////////
	function round(pdnumber, plDec) {
		pdnumber = convertNumeric(pdnumber)

		//if called with no decimal places parameter then 
		//round to whole number(plDec = 0)
		if(round.arguments.length == 1) 
			plDec = 0;
		else
			plDec = parseInt(plDec);

		return Math.round((pdnumber + .0000000001) * Math.pow(10,plDec)) / Math.pow(10,plDec);
	}

	///////////////////////////////
	//formatCurrency
	//	format a number a currency,
	//	optional parameters indicates whether to use cents
	///////////////////////////////
	function formatCurrency(pvnumber, pbshowcents) {
			var lsresult = '$' + formatInteger(Math.floor(convertNumeric(pvnumber)));
			if (pbshowcents) 
				lsresult += formatCents(convertNumeric(pvnumber));
	  
			return (lsresult);
	}

	///////////////////////////////
	//convertNumeric
	//	converts an unknown data type to a number
	//	equivalent to VB's Val()
	///////////////////////////////
	function convertNumeric(pvValue){
	var lvValue = stripNonDigits(pvValue);         //        , '$#%,');	
		return (lvValue - 0);
	}

	///////////////////////////////
	//convertString
	//	equivalent to VB's CStr$()
	///////////////////////////////
	function convertString(pvValue){
		return (pvValue + '');
	}

	///////////////////////////////
	//formatInteger
	//	format a number as XX,XXX,XXX
	///////////////////////////////
	function formatInteger(number) {
		number = convertString(number);
			
	  if (number.length <= 3)
	    return (number == '' ? '0' : number);
	  else {
	    var mod = number.length%3;
	    var output = (mod == 0 ? '' : (number.substring(0,mod)));
	    for (i=0 ; i < Math.floor(number.length/3) ; i++) {
	      if ((mod ==0) && (i ==0))
	        output+= number.substring(mod+3*i,mod+3*i+3);
	      else
	        output+= ',' + number.substring(mod+3*i,mod+3*i+3);
	    }
	    return (output);
	  }
	}
	
	///////////////////////////////
	//formatCents
	//	used by formatCurrency to display the cents portion of number
	///////////////////////////////
	function formatCents(amount) {
	  amount = Math.round( ( (amount) - Math.floor(amount) ) * 100);
	  return (amount < 10 ? '.0' + amount : '.' + amount);
	}

	///////////////////////////////
	//formatNumber
	//	used by formatNumber to display the cents portion of number
	///////////////////////////////
	function formatNumber(pvnumber) {
		var lsResult;
		var lsDecimal;
		
		if (isStringNumeric(pvnumber)){
			lsResult = formatInteger(Math.floor(convertNumeric(pvnumber)));
			
			lsDecimal = pvnumber - Math.floor(pvnumber);
			if (lsDecimal > 0){
				lsResult += lsDecimal;
			}
		} else {
			lsResult = pvnumber;
		}
		return lsResult;
	}

	///////////////////////////////
	//isNaNEx
	//	used so browsers that do not correctly
	//	support the builts isNaN function will
	//	behave correctly(eg. ie3)
	///////////////////////////////
	function isNaNEx(pvValue) {
		if (supports_isNaN()) 
			return (isNaN(pvValue));
		else						//isNaN ignores leading whitespace
			return (!isStringNumeric(stripInitialWhitespace(pvValue)));	
	}
			
	///////////////////////////////
	//isStringNumeric
	//	return true if the string contains characters 
	//	that generate a number
	///////////////////////////////
	function isStringNumeric(pvValue) {
		var lsInput = convertString(pvValue);
		var lsCurrentChar;			
		var lbFoundDecimal = false;
		
		if (lsInput == decimalPointDelimiter.toString())
			return false;
			
		for(var llCounter = 0; llCounter < lsInput.length; llCounter++) 
		{ 
			lsCurrentChar = lsInput.charAt(llCounter);

			if (!isDigit(lsCurrentChar)){ 
				if (lsCurrentChar == decimalPointDelimiter.toString()){
					if(lbFoundDecimal)
						return (false);
					else
						lbFoundDecimal = true;
				}else{
					return (false);
				}
			}	
		}
		return (true);
	}
	
	///////////////////////////////
	//isDigit
	//	return true the character is 0-9
	///////////////////////////////
	function isDigit(psChar){
		return (digits.indexOf(psChar) != -1)		
	}

	///////////////////////////////
	//fetchSelectedComboValue
	//	returns the value property of the combo selected
	//	option in an option group
	///////////////////////////////
	function fetchSelectedComboValue(poControl) {
		return poControl.options[poControl.selectedIndex].value;
	}

	///////////////////////////////
	//fetchSelectedOptionValue
	//	returns the value property of the combo selected
	//	option in an option group
	///////////////////////////////
	function fetchSelectedOptionValue(poControl) {
		for(var llCounter = 0; llCounter < poControl.length; llCounter++) {
			if(poControl[llCounter].checked) {
				return poControl[llCounter].value;
			}
		}
		return null;
	}

	/////////////////////////////////////////////////
	//	same as isNumberValid routine with
	//	extra lines to set poActiveControl.value
	//	to the min or max value when needed 
	/////////////////////////////////////////////////
	function isNumberWithinRange(poActiveControl, plMinimumValue, plMaximumValue, plNumberOfDecimalPlaces, pbFieldIsOptional) {
	var llValue;
	var lsValue;
		poActiveControl.value = stripCharacter(poActiveControl.value, ',')
		lsValue = stripInitialWhitespace(poActiveControl.value);	
		llValue = parseFloat(lsValue);
			if (isEmpty(lsValue)){
			poActiveControl.value = lsValue;
			if(pbFieldIsOptional)
				return true;
			else 
				return false;
		} 
		else if (isNaNEx(lsValue)) {
			return false;
		}
		else if (llValue < plMinimumValue) {
			poActiveControl.value = plMinimumValue;
			return false;
		} 
		else if (llValue > plMaximumValue) {
			poActiveControl.value = plMaximumValue;
			return false;
		} 
		else {
			poActiveControl.value = round(llValue,plNumberOfDecimalPlaces);
			return true;
		}	
	}

	///////////////////////////////
	//setFocusToSelectedOption
	//	Sets focus to first control in control array that is checked
	//	
	///////////////////////////////
	function setFocusToSelectedOption(poControl) {
		var llSelectedIndex = 0;
		for (var llcounter = 0; llcounter < poControl.length; llcounter++) {
			if (poControl[llcounter].checked) 
				llSelectedIndex = llcounter;
		}
		poControl[llSelectedIndex].focus();
	}

	//
	//	zeroPadNumber
	//		adds zeros to the beginning or end of pvInput
	//
	function zeroPadNumber(pvInput, plLength, pbPadAfter) {
	var lsInput = convertString(pvInput);

		for (var llCounter = lsInput.length; llCounter < plLength; llCounter++) {
			if (pbPadAfter) {
				lsInput += '0';
			}
			else {
				lsInput = '0' + lsInput;
			}			
		}	
		return lsInput;
	}

	function sendEmail(psEmailAddress,psSubject,psBody) {
	  var lsEmail = "mailto:" + psEmailAddress + "?subject=" + psSubject + "&body=" + encodeURIComponent(psBody);
		window.location.href = lsEmail;
	}

	function changeStatus(psStatus) {
	  if(psStatus)
  		window.status = psStatus;
  	else
  	  window.status = '';
  	  
		return true;
	}

	function grExcludedCharacters(poControl) {
	var lsName;

    lsName = stripInitialWhitespace(poControl.value);

		if (lsName.search(/^[^,&><':;"\?%~\\=]+$/) != -1) {
			return true;		
		}
		return false;
	}
	
	function grExcludedCharactersDescField(poControl) {
	var lsName;

    lsName = stripInitialWhitespace(poControl.value);
    //Don't allow " or a javascript error may occur with delete and edit links
	  if (lsName.search(/"/) != -1) {
	    var lsMsg = 'Please enter a new name or description.\n';
		  lsMsg += 'This name or description cannot use one of the non-alphanumeric characters entered.';
		  alert(lsMsg);
		  poControl.focus();
		  poControl.select();
		  return true;	
		}
		//Don't allow < with text after it or a "A potentially dangerous Request.Form value was 
		//detected from the client" error may occur in processing inputs
    if (lsName.search(/<.+/) != -1) {
	    var lsMsg = 'Please enter a new name or description.\n';
		  lsMsg += 'This name or description cannot use one of the non-alphanumeric characters entered.';
		  alert(lsMsg);
		  poControl.focus();
		  poControl.select();
		  return true;	
		}
		//Don't allow \ as the last character in a description or a javascript error may occur
    if (lsName.search(/[\\]$/) != -1) {
	    var lsMsg = 'Please enter a new name or description.\n';
		  lsMsg += 'This name or description cannot use one of the non-alphanumeric characters entered.';
		  alert(lsMsg);
		  poControl.focus();
		  poControl.select();
		  return true;	
		}
		return false;
	}

function defaultVisGeneric (psSectionName, pbVisible, pbDisplayBlock) {
var frm = document.frm;
var imgPlus = 'Img/ExpandSection.gif';
var imgMinus = 'Img/ContractSection.gif';
var imgSpacer = 'Img/Spacer.gif';
var imgAchtung = 'Img/Achtung.gif';
var bShowAchtung;
var bAchtungShownForSection;
var imgToggleSection;
var lsAllTags;
var laSectionName = new Array();

  laSectionName = psSectionName.split('~');

	for (var llSectionNumber=0; llSectionNumber < laSectionName.length; llSectionNumber++) 
	{
	  var lName = laSectionName[llSectionNumber];
	  if(lName && lName.length && (lName.length > 0))
	  {
		  lsAllTags = document.getElementsByName(lName);
		  imgToggleSection = eval("document.images['imgToggle" + lName + "']");
		  imgAchtungSection = eval("document.images['imgAchtung" + lName + "']");

		  if (lsAllTags.length > 0) {
		    bAchtungShownForSection = false;
  		
			  for (i=0; i < lsAllTags.length; i++) {
			    bShowAchtung = false;
  			
			    if (pbVisible) {			  
			      if (browserIsIE() || pbDisplayBlock)
				      lsAllTags[i].style.display = 'block';
  				  else 
  				    lsAllTags[i].style.display = 'table-row';
    				  
				    if (imgToggleSection)
				      imgToggleSection.src = imgMinus;
				  }
				  else {
				    lsAllTags[i].style.display = 'none';
    				
				    if (imgToggleSection) {
				      imgToggleSection.src = imgPlus;
				    }
				    if (imgAchtungSection && !bAchtungShownForSection) {			
				      bShowAchtung |= (lsAllTags[i].innerHTML.indexOf(imgAchtung) >= 0);
	            imgAchtungSection.src = bShowAchtung ? imgAchtung : imgSpacer;
  	          
	            if (bShowAchtung) {
	              bAchtungShownForSection = true;
	            }
	          } 
				  }
			  }
		  }
		}
	}
}

function toggleSectionVisGeneric(psSectionName, pbDisplayBlock) {
var lsAllTags = document.getElementsByName(psSectionName);
var imgPlus = 'Img/ExpandSection.gif';
var imgMinus = 'Img/ContractSection.gif';
var imgSpacer = 'Img/Spacer.gif';
var imgAchtung = 'Img/Achtung.gif';
var imgToggleSection = eval("document.images['imgToggle" + psSectionName + "']");
var imgAchtungSection = eval("document.images['imgAchtung" + psSectionName + "']");
var spanToggleSection = eval("document.getElementById('ToggleText" + psSectionName + "')");
var bShowAchtung = false;
var bSectionCurrentlyDisplayed;
var frm = document.frmMG;
var bExplodeView;

	for (i=0; i < lsAllTags.length; i++) {
	  bShowAchtung |= (lsAllTags[i].innerHTML.indexOf(imgAchtung) >= 0);

 	 	if (lsAllTags[i].style.display == '' || lsAllTags[i].style.display == 'none')
 	 	  bSectionCurrentlyDisplayed = false;
 	 	else
      bSectionCurrentlyDisplayed = true;
      
	  if (browserIsIE() || pbDisplayBlock) {
	    	if (!bSectionCurrentlyDisplayed)
 	 	      lsAllTags[i].style.display = 'block';
 	 	    else
		      lsAllTags[i].style.display = 'none';
		}
  	else {
  	    if (!bSectionCurrentlyDisplayed)
 	 	      lsAllTags[i].style.display = 'table-row';
 	 	    else
  		    lsAllTags[i].style.display = 'none';
    }
	}
	if (spanToggleSection) {
	  //Toggle More Less text if applicable
	  if (spanToggleSection.innerHTML == 'More')
	      spanToggleSection.innerHTML = 'Less';
	  else if (spanToggleSection.innerHTML == 'Less')
	      spanToggleSection.innerHTML = 'More';

	  //Toggle Show Hide text if applicable
	  if (spanToggleSection.innerHTML == 'Show')
	      spanToggleSection.innerHTML = 'Hide';
	  else if (spanToggleSection.innerHTML == 'Hide')
	      spanToggleSection.innerHTML = 'Show';
	}
	
	//Toggle Plus Minus images if applicable
	if (imgToggleSection) {
	  bExplodeView = imgToggleSection.src.indexOf(imgPlus) >= 0;
	  imgToggleSection.src = bExplodeView ? imgMinus : imgPlus;
	  
	  if (imgAchtungSection) {
	    imgAchtungSection.src = bExplodeView || !bShowAchtung ? imgSpacer : imgAchtung;
	  }
	}
}

function setSectionVis(pSection, pExplodeView) {
var frm = document.frmMG;
var s = frm.sSectionVisibility.value + '';
var quotedSection = "'" + pSection + "'";

var src = s.split(",");
var dst = new Array();

	if(pExplodeView) 
		dst.push(quotedSection);

	for (var i=0; i < src.length; i++)
		if(src[i] != quotedSection)
			dst.push(src[i]);	

	frm.sSectionVisibility.value = dst.toString();
}

function browserIsIE() {
var agt=navigator.userAgent.toLowerCase(); 
	
	 var is_ie = (agt.indexOf("msie") != -1);
	 return is_ie;
}

function isIPhoneUser() {
var agt=navigator.userAgent.toLowerCase(); 

  var is_iphone = (agt.indexOf('iphone') != -1);
	return is_iphone;
}

function setOptionValue(poControl, pValue) {
	for(llCounter = 0; llCounter < poControl.length; llCounter++) {
		if(poControl[llCounter].value == pValue) {
			poControl[llCounter].checked = true;
			return;
		}
	}
}

function setComboValue(poControl, pValue) {
	for(llCounter = 0; llCounter < poControl.options.length; llCounter++) {
		if(poControl.options[llCounter].value == pValue) {
			poControl.options[llCounter].selected = true;
			return;
		}
	}
}


// Yahoo Panel Popup Used on most Resource summary grids
// Specify pPanelWidth and pPanelHeight in px (i.e. 400px)
// Specify pPanelAlignment "tl", "tr", "bl", "br"
var YUIPanel =
{
  Panels : new Array(),
  PanelNames : new Array(),
  CurrentPanelX : new Array(),
  PanelSub : 0,
  PreviousPanelSub : 0,

  showHelpPanel : function(pPanelName, pPanelBodyName, pPanelHeader, pPanelWidth, pPanelHeight, pPanelAlignment, pHasClose) {

  var bAlreadyCreated = false;
  var isSafari = navigator.userAgent.indexOf("Safari") > -1;

    if (isSafari) {
      for (var i = 0; i < this.PanelNames.length; i++) {
        if (this.PanelNames[i] == pPanelName) {
           this.PanelSub = i;
           bAlreadyCreated = true;
        }
      }   
      if (this.PanelNames.length > 0)
        this.Panels[this.PreviousPanelSub].hide();   
    }
    else {
      if (this.PanelSub > 0)
        this.Panels[this.PanelSub - 1].hide();   
    }
    
    if (!bAlreadyCreated) {
      this.PanelNames[this.PanelSub] = pPanelName;
    
      this.Panels[this.PanelSub] = new YAHOO.widget.Panel(pPanelName, { 
       width:pPanelWidth, constraintoviewport:true, underlay:"shadow", close:pHasClose, visible:false, draggable:false, modal:false, context:[pPanelName, "tr", pPanelAlignment]} );

      //This keeps the panel from sliding to the farthest left possible on rendering the second time. yui bug?
      if (this.CurrentPanelX.length == 0)
        this.CurrentPanelX[0] = YAHOO.util.Dom.getX(pPanelName);
      else
        this.Panels[this.PanelSub].cfg.setProperty('x', this.CurrentPanelX[0]);
    }
    var body = document.getElementById(pPanelBodyName).innerHTML;

    this.Panels[this.PanelSub].setHeader(pPanelHeader);
    this.Panels[this.PanelSub].setBody(body);
    this.Panels[this.PanelSub].render();
    this.Panels[this.PanelSub].show();
   
    this.PreviousPanelSub = this.PanelSub;
    
    if (!bAlreadyCreated)
      this.PanelSub++;
  },
  
  hideHelpPanel : function(pPanelName) {
    var bAlreadyCreated = false;
    var isSafari = navigator.userAgent.indexOf("Safari") > -1;

    if (isSafari) {
      for (var i = 0; i < this.PanelNames.length; i++) {
        if (this.PanelNames[i] == pPanelName) {
           this.PanelSub = i;
           bAlreadyCreated = true;
        }
      }   
      if (this.PanelNames.length > 0)
        this.Panels[this.PreviousPanelSub].hide();   
    }
    else {
      if (this.PanelSub > 0)
        this.Panels[this.PanelSub - 1].hide();   
    }   
  }
  
};

function openPWWindow(psURL, psWindowName) {
	var windowHandle = window.open(psURL, psWindowName, "height=700,width=650,status=no,resizable=yes,scrollbars=yes");
	if (windowHandle.opener) {
    windowHandle.focus();
	}
}

function launchSupportCenterItem(psURL, psWindowName, pWidth, pHeight) {
    if (pWidth == null)
        pWidth = 650;

    if (pHeight == null)
        pHeight = 600;

    var windowProperties = 'height=' + pHeight + ',width=' + pWidth + ',menubar=no,location=no,status=no,resizable=yes,scrollbars=yes,top=0,left=0,screenX=0,screenY=0'

    windowHandle = window.open(psURL, psWindowName, windowProperties);
    if (windowHandle.opener) {
        windowHandle.focus();
    }
}

///////////////////////////////
// START Broker Version Validation
///////////////////////////////

///////////////////////////////
//containsInvalidBrokerData
//	checks to find the word "Plan" and "Planning" in a given string.
//	returns true if found.
///////////////////////////////
function containsInvalidBrokerData(pValue, pFieldDescription)
{
	var lMsg = "The data you entered into the " + pFieldDescription + " field contains either the word 'Plan' or the word 'Planning', which cannot be used in this version.  Please remove them before continuing.";
	var regex = /(^|\s{1})plan(ning)?(\s{1}|$)/im;
	
	var lContainsInvalidBrokerData = false;
	
	if (regex.test(pValue))
		lContainsInvalidBrokerData = true;
	
	if (lContainsInvalidBrokerData)
		alert(lMsg);
	
	return lContainsInvalidBrokerData;
}

///////////////////////////////
//checkForBrokerData
//	validates that no broker data was into the given control.
//	returns false if found and sets focus to the input.
///////////////////////////////
function checkForBrokerData(pViewOption, pControl, pFieldDescription)
{
	if (pControl)
	{
		if (pViewOption == ViewOption_Broker)
		{
			if (containsInvalidBrokerData(pControl.value, pFieldDescription)) {
				pControl.focus();
				pControl.select();
				return false;	
			}
		}
	}
	
	return true;
}

///////////////////////////////
// END Broker Version Validation
///////////////////////////////

 