// make a query url
function buildPOST(theFormName) { 
    theForm = document.forms[theFormName]; 
    var qs = '' 
    for (e=0;e<theForm.elements.length;e++) { 
        if (theForm.elements[e].name!='') { 
            var name = theForm.elements[e].name; 
            qs+=(qs=='')?'':'&' 
            qs+= name+'='+escape(theForm.elements[e].value); 
        } 
    } 
    //qs+="\n"; 

    return qs;
} 

// reduce blank space
function trim(str){
    for(var  i  =  0  ;  i<str.length  &&  str.charAt(i)==" "  ;  i++  )  ;
    for(var  j  = str.length;  j>0  &&  str.charAt(j-1)==" "  ;  j--)  ;
    if(i>j)  return  "";  
    return  str.substring(i,j);  
} 


function stopRKey(evt) {
   var evt = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
   if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}


//=======================================================================================
//=======================================================================================
/*=================================== Show contant ======================================*/
// show the Terms of Service
function showTerm(name){
	target=getField(name);
	target.setDisplay();
	target.setWidth(640); 
	target.setHeight(500); 

	target.setLeft((document.body.clientWidth/2)-(645/2));
	target.setTop((document.body.clientHeight/2)-(400/2));

	showhide();
	target.setInnerHTML("<table cellpadding='10' width='600' align='center'><tr><td align='right'><a onClick='noneDiv(termsDiv);showhide();'>Close</a></td></tr><tr><td align='center'><b>wall401k Terms of Service<b></td></tr><tr><td><p>1. Disclosure Statement</p><p>Although wall401k Ltd. carefully evaluates the portfolio recommendations created by the Efficient Frontier, which is based on customers inputted investment vehicles, wall401k Ltd. makes no representation regarding the likelihood or probability that any proposed investing plan will in fact achieve a particular investment goal. All investment recommendations are based on historical data, which is updated frequently. wall401k Ltd. is unable to predict or forecast market fluctuations or other uncertainties that may affect the value of any investment and is prohibited by law from making such predictions or projections. While this tool is designed to provide helpful investing guidance, as a self-directed investor, you are urged to educate yourself about investing in general. Monitoring and adjusting your effective portfolio to suite changing circumstances is your responsibility, and it is recommended that you reassess your portfolio on a  regular basis to meet your investment objectives.</p><p>2. The following is something I would like to put on our homepage:</p><p>Effective Frontier has been broadly used to assist large mutual fund managers to achieve optimal investment return. However, individual investors are not able to benefit from this renowned theory due to lack of tools. Not anymore. This unique on-line 401(k) portfolio optimizing tool provides most effective portfolio allocation by constructing Efficient Frontier of your own investment vehicles. And right now, it is FREE! So why not set up your account and try it today!</p><p>You only need to input your current available 401(k) investment funds, which might include 10-20 choices of funds or stocks. We will build the Efficient Frontier just for you. Say goodbye to apply-to-all diversified portfolio mix and start building your own effective portfolio today!</p></td></tr><tr><td>&nbsp;</td></tr></table>");
	target.src.onmousedown=function(){  
		update();
		grab(this);};
}

function showLoading(name){
	var layer = getField(name);
	var loading = getField("loading");
	showhide(); //show select input

	layer.setClass("opac");
	layer.setBgColor("Black");
	loading.setBgColor("White");
	layer.setVisible();	
	layer.setDisplay();

	layer.setWidth(document.body.clientWidth + document.body.scrollLeft);
	layer.setHeight(document.body.clientHeight + document.body.scrollTop);
	document.getElementById("loading").innerHTML = "<img src='../templates/default/images/portfolio/loading.gif' /> The optimization process is running...... <br />The result will be displayed within one minute.";
}

function hideLoading(name){
	var layer = getField(name);
	var loading = getField("loading");
	showhide(); //show select input

	layer.setHidden();	
	layer.setNone();
}

function showExplain(e){
	if (!e) e = window.event; // works on IE

	this.explain.setDisplay();
	this.explain.setVisible();	
	if (navigator.appName == "Microsoft Internet Explorer"){
		this.explain.setInnerHTML(
		"<div style='display:inline; visibility:visible' class='upperrightfade'>"+
		"<div style='display:inline; visibility:visible' id='lowerleftfade' class='lowerleftfade'>"+
		"<div style='display:inline; visibility:visible' id='shadowcorner' class='shadowcorner'>"+
			"<table cellspacing='4' cellpadding='4' class='explaintable' width='392'><tr><td>"+
			"<img src='../templates/default/images/portfolio/help.jpg' /> "
			+this.str+
			"</td></tr></table>"+
		"</div>"+
		"</div>"+
		"</div>");	
	}else{
		this.explain.setInnerHTML(
		"<table border='0' width='100%' cellspacing='0' cellpadding='0'>"+
		"<tr><td colspan='2' rowspan='2' class='explaintable2'>"+
			"<table cellspacing='4' cellpadding='4'><tr><td><p>"+
			"<img src='../templates/default/images/portfolio/help.jpg' /> "
			+this.str+
			"</p></td></tr></table></td>"+
		"<td class='upperrightfade2'></td></tr>"+
		"<tr><td class='shadowleft2'>&nbsp;</td></tr>"+
		"<tr><td class='lowerleftfade2'></td>"+
			"<td class='shadowbtm2'></td>"+
			"<td class='shadowcorner2'></td></tr></table>");	
	}
	this.explain.setWidth(400); 
	this.explain.setHeight(10);
	
	//alert(e.clientX + 400 - e.clientX +" "+(e.clientX + 400)+" "+document.body.clientWidth);
	if (e.clientX + 400 > document.body.clientWidth){
		this.explain.setTop(e.clientY + document.body.scrollTop);
		this.explain.setLeft(e.clientX - (e.clientX + 400 - document.body.clientWidth + 50));
	}else{
		this.explain.setTop(e.clientY + document.body.scrollTop);
		this.explain.setLeft(e.clientX + document.body.scrollLeft);
	}
}

/*=================================== Visibility ======================================*/
// hidden all DIV tag object
function hiddenAlldiv(){
	var cells = document.getElementsByTagName('div');
	for (var i = 0; i < cells.length; i++){
		cells[i].style.display = 'none';
	}

}

// hidden specify DIV tag object
function noneDiv(obj_name){
	var obj = getField(obj_name).src;
	obj.style.display = 'none';
	document.body.style.filter="";
}

// hidden specify DIV tag object
function hiddenDiv(obj_name){
	var obj = getField(obj_name).src;
	obj.style.visibility = 'hidden';
	document.body.style.filter="";
}

//show hide "select" input
function showhide(){
	var tags = document.getElementsByTagName("select");
	for (var i=0; i<tags.length; i++) {
		var tag = tags[i];
	
		if (tag.style.display == "none") {
			tag.style.display = "inline";
		} else {
			tag.style.display = "none";
		}
	}
}

/*=================================== Changing ======================================*/
// change the tabs onclick
function tabsAction(form,tabNum,name){
	var tabName;

	for (var i=1;i<=tabNum;i++ ){
		tabName = name+i;

		if (form != i){
			document.getElementById(tabName).className = '';
		}else{		
			document.getElementById(tabName).className = "selected";
		}
	}
}

/*=================================== Mouse Action ======================================*/
// drag and drop
var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;

var dragobj = null;

function falsefunc() { return false; } // used to block cascading events

function init()
{
  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft;
      mousey = e.clientY + document.body.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }  
  }
}

function update(e)
{
  getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function grab(context)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  dragobj = context;
  dragobj.style.zIndex = 10; // move it to the top
  document.onmousemove = drag;
  document.onmouseup = drop;
  grabx = mousex;
  graby = mousey;
  elex = orix = dragobj.offsetLeft;
  eley = oriy = dragobj.offsetTop;
  update();
}

function drag(e) // parameter passing is important for NS family 
{
  if (dragobj)
  {
    elex = orix + (mousex-grabx);
    eley = oriy + (mousey-graby);
    dragobj.style.left = (elex).toString(10) + 'px';
    dragobj.style.top  = (eley).toString(10) + 'px';
  }
  update(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function drop()
{
  if (dragobj)
  {
    dragobj.style.zIndex = 0;
    dragobj = null;
  }
  update();
  document.onmousemove = update;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}

//=======================================================================================
//=======================================================================================
/*=================================== Date selection====================================*/
var monthtext=['-MM-','01','02','03','04','05','06','07',
			   '08','09','10','11','12'];
var datetext=['-DD-','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15',
			  '16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'];
var yeartext=new Array();
	yeartext[0]="-YYYY-";

function setDefaultSel(year, month, date){
	var datefield=getField('daydropdown');
	var monthfield=getField('monthdropdown');
	var yearfield=getField('yeardropdown');	

	getField(year).src.selected = "yes";
	getField(month).src.selected = "yes";
	getField(date).src.selected = "yes";
}

function dateSelectInitial (){
	var today=new Date();
	var date=today.getDate();
	var month=today.getMonth();
	var year=today.getFullYear()-18;
	
	var datefield=getField('daydropdown');
	var monthfield=getField('monthdropdown');
		monthfield.src.onchange=function(){dateGen();}
	var yearfield=getField('yeardropdown');	
		yearfield.src.onchange=function(){dateGen();}
	var option;
	
	//===================== year title =====================
	option = document.createElement('option');
	option.text = yeartext[0];
	option.id = yeartext[0];
	option.value = '';
	try {
   		yearfield.src.add(option, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
   		yearfield.src.add(option); // IE only
	}
	//===================== year =====================
	for (i=1;i<100;i++){
		yeartext[i]=year;
		year--;
	}
	for(i=1; i<yeartext.length;i++){
		option = document.createElement('option');
		option.text = yeartext[i];
		option.id = 'y'+yeartext[i];
		option.value = yeartext[i];
		try {
    		yearfield.src.add(option, null); // standards compliant; doesn't work in IE
  		}
  		catch(ex) {
    		yearfield.src.add(option); // IE only
		}
	}

	//===================== month title =====================
	option = document.createElement('option');
	option.text = monthtext[0];
	option.id = monthtext[0];
	option.value = '';
	try {
   		monthfield.src.add(option, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
   		monthfield.src.add(option); // IE only
	}
	//===================== month =====================
	for(i=1; i<monthtext.length;i++){
		option = document.createElement('option');
		option.text = monthtext[i];
		option.id = 'm'+monthtext[i];
		option.value = monthtext[i];
		try {
    		monthfield.src.add(option, null); // standards compliant; doesn't work in IE
  		}
  		catch(ex) {
    		monthfield.src.add(option); // IE only
		}
	}

	//===================== date title =====================
	option = document.createElement('option');
	option.text = datetext[0];
	option.id = datetext[0];
	option.value = '';
	try {
   		datefield.src.add(option, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
   		datefield.src.add(option); // IE only
	}	
	//===================== date =====================
	for(i=1; i<datetext.length;i++){
		option = document.createElement('option');
		option.text = datetext[i];
		option.id = 'd'+datetext[i];
		option.value = datetext[i];
		try {
    		datefield.src.add(option, null); // standards compliant; doesn't work in IE
  		}
  		catch(ex) {
    		datefield.src.add(option); // IE only
		}
	}
}

function dateGen(){
	var datefield=getField('daydropdown');
	var monthfield=getField('monthdropdown');
	var yearfield=getField('yeardropdown');
	var option;
	
	var today=new Date();
	var date=today.getDate();
	var month=today.getMonth()+1;
	var year=today.getFullYear()-18;
	var monthSel=parseFloat(monthfield.src.options[monthfield.src.selectedIndex].value);
	var yearSel=parseFloat(yearfield.src.options[yearfield.src.selectedIndex].value);

	//========================= 18 yr before =========================
	if (yearSel==year){
		if (monthfield.src.length > month+1){
			for(i=monthfield.src.length; i>month+1;i--){
				monthfield.src.remove(monthfield.src.length-1);
			}
		}
		if (monthSel==month){
			if (datefield.src.length > date+1){
				for(i=datefield.src.length; i>date+1; i--){
					datefield.src.remove(datefield.src.length-1);
				}
			}
		}
	}else{
		//========================= 12 months =========================
		if (monthfield.src.length < 13){
			for(i=monthfield.src.length; i<13;i++){
				option = document.createElement('option');
				option.text = monthtext[i];
				option.id = 'm'+monthtext[i];
				option.value = monthtext[i];
				try {
					monthfield.src.add(option, null); // standards compliant; doesn't work in IE
				}
				catch(ex) {
					monthfield.src.add(option); // IE only
				}
			}
		}
		//========================= 31 days =========================
		if ((monthSel==1 || monthSel==3 || monthSel==5 || monthSel==7 || 
			monthSel==8 || monthSel==10 || monthSel==12) && datefield.src.length<32){

			//alert("Add to 31: "+datefield.src.length);
			for(i=datefield.src.length; i<32;i++){
				option = document.createElement('option');
				option.text = datetext[i];
				option.id = 'd'+datetext[i];
				option.value = datetext[i];
				try {
					datefield.src.add(option, null); // standards compliant; doesn't work in IE
				}
				catch(ex) {
					datefield.src.add(option); // IE only
				}
			}

		//========================= 30 days =========================
		}else if (monthSel==4 || monthSel==6 || monthSel==9 || monthSel==11){
			if (datefield.src.length<31){

				//alert("Add to 30: "+datefield.src.length);
				for(i=datefield.src.length; i<31;i++){
					option = document.createElement('option');
					option.text = datetext[i];
					option.id = 'd'+datetext[i];
					option.value = datetext[i];
					try {
						datefield.src.add(option, null); // standards compliant; doesn't work in IE
					}
					catch(ex) {
						datefield.src.add(option); // IE only
					}
				}
			}else if (datefield.src.length>30){
				//alert("reduce to 30: "+datefield.src.length);
				datefield.src.remove(datefield.src.length-1);
			}
		//========================= 28 or 29 days =========================
		}else if (monthSel==2){
			if (yearSel%4==0){
				if (datefield.src.length<30){
					//alert("Add to 29: "+datefield.src.length);
					for(i=datefield.src.length; i<30;i++){
						option = document.createElement('option');
						option.text = datetext[i];
						option.id = 'd'+datetext[i];
						option.value = datetext[i];
						try {
							datefield.src.add(option, null); // standards compliant; doesn't work in IE
						}
						catch(ex) {
							datefield.src.add(option); // IE only
						}
					}
				}else if (datefield.src.length>30){
					//alert("reduce to 29: "+datefield.src.length);
					for(i=datefield.src.length; i>30;i--){
						datefield.src.remove(datefield.src.length-1);
					}
				}
			}else if (datefield.src.length>29){
				//alert("reduce to 28: "+datefield.src.length);
				for(i=datefield.src.length; i>29;i--){
					datefield.src.remove(datefield.src.length-1);
				}
			}
		} 
	}
}