var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
var NSX = (navigator.appName == "Netscape");
var IE4 = (document.all) ? true : false;

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr;

	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
	var p,i,x;

	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n);
	
	return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments;

	document.MM_sr=new Array;
	for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null){
			document.MM_sr[j++]=x;
			if(!x.oSrc) x.oSrc=x.src;
			x.src=a[i+2];
		}
}

function exitMessage() {
  alert("Flexible Plan does not endorse this website, its sponsor, or any of the policies, activities, products, or services offered on the site or by any advertiser on the site. The Privacy Policy of Flexible Plan Investments does not apply nor extend to this or other websites.");
}

var popupWindow = '';
function popupNewWindow(url, windowName, windowWidth, windowHeight, windowLeft, windowTop, closeFirst, isResizeable, menuBar, scrollBars)	{
	if (!windowWidth) {windowWidth = 700};
	if (!windowHeight) {windowHeight = 560};
	if (!windowLeft) {windowLeft = 200};
	if (!windowTop) {windowTop = 50};
	if (!isResizeable) {isResizeable = 'yes'};
	if (!menuBar) {menuBar = 'yes'};
	if (!scrollBars) {scrollBars = 'yes'};

	if (!popupWindow.closed && popupWindow.location && !closeFirst) {
		// Do nothing except set focus below.
	} else {
		popupWindow = window.open(url, windowName, 'alwaysraised=yes,toolbar=no,location=no,status=no,menubar=' +
			menuBar + ',scrollbars=' + scrollBars + ',resizable=' + isResizeable + ',height=' + windowHeight +
			',width=' + windowWidth + ',left=' + windowLeft + ',top=' + windowTop);
		if (!popupWindow.opener) {popupWindow.opener = self};
	}
	if (window.focus) {popupWindow.focus()};
}

function autofocus(field, limit, next, evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
		((evt.which) ? evt.which : 0));
	if (((charCode >= 48 && charCode <= 57) || (charCode >= 65 && charCode <= 90)
			|| (charCode >= 96 && charCode <= 105)) && field.value.length == limit) {
		field.form.elements[next].focus();
		if (field.form.elements[next].type == "text") {
			field.form.elements[next].select();
		}
	}
/*
   var str = '';
   for (i = 0; i < field.form.length; i++)
	   str += field.form.elements[i].name + "\n";
   alert(str)
*/
}

function getAttribs(el) {
	var props = new Array();
	for (var j = 0; j < el.attributes.length; j++) {
		props[j] = el.attributes[j].nodeName;
	}
	props.sort();
	
	return props.join('\n');
}

function iterateElements() {
	var myRefs = document.getElementById("menu").getElementsByTagName("div");

	for (var i = 0; i < myRefs.length; i++) {
		alert(i + ": " + myRefs[i].name);
	}
}

function addOption(theSel, theText, theValue) {
	var selParsedValue, selFamilyNumber, selStrategyCode, compareValue,
		compParsedValue, compFamilyNumber, compStrategyCode;
	var indexFound = false;

	selParsedValue = theValue.split(",");
	selFamilyNumber = selParsedValue[0];
	selStrategyCode = selParsedValue[1];

	var selLength = theSel.length;
	var newIndex = selLength;
	for (var i = 0; i <= selLength - 1; i++) {
		compareValue = theSel.options[i].value;
		compParsedValue = compareValue.split(",");
		compFamilyDesc = compParsedValue[0];
		compStrategyDesc = compParsedValue[1];

		if (selFamilyDesc < compFamilyDesc && selStrategyDesc < compStrategyDesc) {
			newIndex = i;
			break;
		}
	}

//	if (NSX) {
		addOptionNS(theSel, theText, theValue, newIndex);
//	} else if (IE4) {
//		addOptionIE(theSel, theText, theValue, newIndex);
//	}
}

function addOptionNS(theSel, theText, theValue, theIndex) {
	var moveText = new Array();
	var moveValue = new Array();
	var newOpt  = new Option(theText, theValue);
	var selLength = theSel.length;

	var toMoveCount = 0;
	for (var i = selLength - 1; i >= theIndex; i--) {
		moveText[toMoveCount] = theSel.options[i].text;
		moveValue[toMoveCount] = theSel.options[i].value;
		theSel.options[i].value = null;
		toMoveCount++;
	}

	selLength = theSel.length;
	theSel.options[selLength] = newOpt;
	var movedCount = selLength;
	for (i = toMoveCount - 1; i >= 0; i--) {
		theSel.options[movedCount].text = moveText[i];
		theSel.options[movedCount].value = moveValue[i];
		movedCount++;
	}

	if (NS4) history.go(0);
}

function addOptionIE(theSel, theText, theValue, theIndex) {
	var newOpt = document.createElement("OPTION");
	newOpt.text = theText;
	newOpt.value = theValue;
//	theSel.add(newOpt);
}

function deleteOption(theSel, theIndex) {
	if (NSX) {
		deleteOptionNS(theSel, theIndex);
	} else if (IE4) {
		deleteOptionIE(theSel, theIndex);
	}	
}

function deleteOptionNS(theSel) {
	var selLength = theSel.length;
	if (selLength > 0) {
		theSel.options[theIndex] = null;
		if (NS4) history.go(0);
	}
}

function deleteOptionIE(theSel, theIndex) {
	var selLength = theSel.length;
	if (selLength > 0) {
		theSel.remove(theIndex);
	}
}

function moveOptions(theSelFrom, theSelTo) {
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	var i;

	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for (i = selLength - 1; i >= 0; i--) {
		if (theSelFrom.options[i].selected) {
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}

	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for (i = selectedCount - 1; i >= 0; i--) {
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}

	if (NS4) history.go(0);
}

function trim(aString)
{
  if (typeof(aString) != 'string')
    aString = aString.toString();

  var retString = '';

  var i = 0;
  while (aString.substr(i, 1) == ' ')
  {
	i++;
  }
  retString = aString.substr(i);

  var i = retString.length - 1;
  while (retString.substr(i, 1) == ' ')
  {
	i--;
  }
  retString = retString.substr(0, i + 1);

  return retString;
}

function fillOnlAppDemo(theForm) {
	if (theForm.txtFName.value.toLowerCase() == "sample") {
		theForm.txtFName.value = "Sample";
		theForm.txtLName.value = "Client";
		theForm.txtAddr.value = "1234 Main St";
		theForm.txtCity.value = "Anytown";
		theForm.lstState.value = "MI";
		theForm.txtZip.value = "48310";
		theForm.opgUSCtzn[0].checked = true;
		theForm.txtSSN1.value = "999";
		theForm.txtSSN2.value = "99";
		theForm.txtSSN3.value = "9999";
		theForm.txtHomeTel1.value = "248";
		theForm.txtHomeTel2.value = "555";
		theForm.txtHomeTel3.value = "1212";
		theForm.txtEMail.value = "web_errors@fpi.flexibleplan.com";
		theForm.txtBirthMo.value = "01";
		theForm.txtBirthDay.value = "14";
		theForm.txtBirthYr.value = "1965";
		theForm.txtEmployer.value = "Your Company";
		theForm.cmdContinue.focus();
	}
}

function fillStratChgDemo(theForm) {
	if (theForm.txtSSN1.value.toLowerCase() == "999") {
		theForm.txtSSN2.value = "99";
		theForm.txtSSN3.value = "9999";
		theForm.txtPlanNum.value = "SAMPLEA";
		theForm.txtEMail.value = "sample@flexibleplan.com";
		theForm.cmdContinue.focus();
	}
}

function fillDocPrepDemo(theForm) {
	if (theForm.txtPlnNm.value.toLowerCase() == "test plan") {
		theForm.txtPlnNm.value = "Test Plan";
		theForm.txtPlnSpnNm.value = "Test Sponsor";
		theForm.txtAddr.value = "1234 Pearl St";
		theForm.txtCity.value = "Boulder";
		theForm.lstState.value = "CO";
		theForm.txtZip.value = "80100";
		theForm.txtNumTrst.value = "2";
		theForm.txtNumTrstSign.value = "2";
		theForm.txtAssetEstmt.value = "3500000";
		theForm.txtFName.value = "Test";
		theForm.txtLName.value = "Contact";
		theForm.txtTel1.value = "806";
		theForm.txtTel2.value = "555";
		theForm.txtTel3.value = "1212";
		theForm.txtEMail.value = "contact@xyz.com";
		theForm.lstPlnType.value = "1";
		theForm.txtPstn.value = "Assistant";
		theForm.txtPstn.focus();
	}
}

function fillDocPrepDemoT(theForm) {
	if (theForm.txtPlnNm.value.toLowerCase() == "test plan") {
		theForm.txtPlnNm.value = "Test Plan";
		theForm.txtPlnSpnNm.value = "Test Sponsor";
		theForm.txtFName.value = "Test";
		theForm.txtLName.value = "Contact";
		theForm.txtTel1.value = "806";
		theForm.txtTel2.value = "555";
		theForm.txtTel3.value = "1212";
		theForm.txtEMail.value = "contact@xyz.com";
		theForm.lstPlnType.value = "1";
		theForm.txtPstn.value = "Assistant";
		theForm.cmdNext.focus();
	}
}
