var xmlHttp;
var copySpan;

function trim(sString) {
	while (sString.substring(0,1) == ' ') sString = sString.substring(1, sString.length);
	while (sString.substring(sString.length-1, sString.length) == ' ') sString = sString.substring(0,sString.length-1);
	return sString;
}

function copyPortfolio(owner_id, portfolio_name, new_portfolio_name, new_amount) {
	//alert(owner_id + " | " + portfolio_name + " | " + document.getElementById(new_portfolio_name).value + " | " + document.getElementById(new_amount).value );
	var portfolio_id = new_portfolio_name.substring(new_portfolio_name.lastIndexOf('_')+1);
	copySpan = 'copy_text_' + portfolio_id;
	portfolio_name = portfolio_name.replace('#', '%23');
	//new_portfolio_name = new_portfolio_name.replace("test", "ryan");
	//alert(document.getElementById(new_portfolio_name).value.replace(/#/g, '%23'));
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="/inc/copyportfolio.php?owner_id="+owner_id+"&portfolio_name="+portfolio_name.replace(/#/g, '%23')+"&new_portfolio_name="+document.getElementById(new_portfolio_name).value.replace(/#/g, '%23')+"&new_amount="+document.getElementById(new_amount).value+"&portfolio_id="+portfolio_id;
	//alert(url);
	
	xmlHttp.onreadystatechange=copyPortfolioStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function copyPortfolioStateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById(copySpan).innerHTML=xmlHttp.responseText;
	} 
}

function checkEmail(str) { 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="/inc/checkemail.php?t="+str;

	xmlHttp.onreadystatechange=emailStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function emailStateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("spanemail").innerHTML=xmlHttp.responseText;
	} 
}

function checkUser(str) { 
	//alert(str);
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="/inc/checkuser.php?t="+str;

	xmlHttp.onreadystatechange=userStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function userStateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("spanuser").innerHTML=xmlHttp.responseText;
	} 
}

function GetXmlHttpObject() { 
	http_request = false;
   	if (window.XMLHttpRequest) { // Fireox, Safari, etc.
      	http_request = new XMLHttpRequest();
       	if (http_request.overrideMimeType) {
       		//set type accordingly to anticipated content type
       		//http_request.overrideMimeType('text/xml');
       		http_request.overrideMimeType('text/html');
       	}
   	} 
	else if (window.ActiveXObject) { // IE
       	try {
       		http_request = new ActiveXObject("Msxml2.XMLHTTP");
       	} 
		catch (e) {
       		try {
           		http_request = new ActiveXObject("Microsoft.XMLHTTP");
           	} catch (e) {}
       	}
    }
	return http_request;
}

function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 1) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	} 
	else
	return true;
}
function checkSelect(selectid, value1, id1, value2, id2, value3, id3, eitheror) {
	//alert(document.getElementById(selectid).options[document.getElementById(selectid).selectedIndex].value);
	if (document.getElementById(selectid).options[document.getElementById(selectid).selectedIndex].value == value1) {
		document.getElementById(id1).style.display = '';
		if (id2 != '') document.getElementById(id2).style.display = 'none';
		if (id3 != '') document.getElementById(id3).style.display = 'none';
	}
	else if (value2 != '' && document.getElementById(selectid).options[document.getElementById(selectid).selectedIndex].value == value2) {
		document.getElementById(id1).style.display = 'none';
		if (id2 != '') document.getElementById(id2).style.display = '';
		if (id3 != '') document.getElementById(id3).style.display = 'none';
	}
	else if (value3 != '' && document.getElementById(selectid).options[document.getElementById(selectid).selectedIndex].value == value3) {
		document.getElementById(id1).style.display = 'none';
		if (id2 != '') document.getElementById(id2).style.display = 'none';
		if (id3 != '') document.getElementById(id3).style.display = '';
	}
	else if (eitheror == 1) {
		if (id1 != '') document.getElementById(id1).style.display = 'none';
		if (id2 != '') document.getElementById(id2).style.display = 'none';
		if (id3 != '') document.getElementById(id3).style.display = 'none';
	}
}
function toggleVisibility(divid, linkdiv, visibletext, hiddentext) {
	if (linkdiv == undefined) linkdiv = "";
	
	if (document.getElementById(divid).style.display == 'none') {
		document.getElementById(divid).style.display = '';
		if (linkdiv != "" && visibletext != "") document.getElementById(linkdiv).innerHTML = visibletext;
	}
	else {
		document.getElementById(divid).style.display = 'none';
		if (linkdiv != "" && hiddentext != "") document.getElementById(linkdiv).innerHTML = hiddentext;
	}
}

function generateElements(num) {
	var fd1 = document.getElementById('divs1');
	var fd2 = document.getElementById('divs2');
	
	//if (num > 0) alert(num);
	
	for (x = 0; x < num; x++) {
		c = x + 1;
		var newdiv = document.createElement('div');
		var divIdName = 'divs1'+c;
		newdiv.setAttribute('id',divIdName);
		newdiv.innerHTML = "<input type=\"text\" name=\"name" + c + "\" class=\"textbox\"  style=\"margin-bottom:5px;\" />";
		fd1.appendChild(newdiv);
		
		var newdiv2 = document.createElement('div');
		var divIdName2 = 'divs2'+c;
		newdiv2.setAttribute('id',divIdName2);
		newdiv2.innerHTML = "<input type=\"text\" name=\"email" + c + "\" class=\"textbox\"  style=\"margin-bottom:5px;\" />";
		fd2.appendChild(newdiv2);
	}
	
	//if (num > 0) document.getElementById('attachlink').innerHTML = "Attach another photo";
}

function addInvite() {
	var count = document.forms[0].elements.length;
	var fd1 = document.getElementById('divs1');
	var fd2 = document.getElementById('divs2');
	var c = 1;
	var html = "";

	for (i = 0; i < count; i++) {
		var element = document.forms[0].elements[i]; 
		if (element.name.match("name")) {
			c++;
			//alert(element.name);
		}
	}
	//alert(c);
	//c=c+1
	
	var newdiv = document.createElement('div');
	var divIdName = 'divs1'+c;
	newdiv.setAttribute('id',divIdName);
	newdiv.innerHTML = "<input type=\"text\" name=\"name" + c + "\" class=\"textbox\"  style=\"margin-bottom:5px;\" />";
	fd1.appendChild(newdiv);
	
	var newdiv2 = document.createElement('div');
	var divIdName2 = 'divs2'+c;
	newdiv2.setAttribute('id',divIdName2);
	newdiv2.innerHTML = "<input type=\"text\" name=\"email" + c + "\" class=\"textbox\"  style=\"margin-bottom:5px;\" />";
	fd2.appendChild(newdiv2);
	
	//document.getElementById('attachlink').innerHTML = "Attach another photo";
}

function showPhotos(repproject_entry_id) {
	strURL = "photos.php?id=" + repproject_entry_id;
	window.open(strURL,'','status=yes, width=600, height=500, scrollbars=yes, resizable=yes'); void('');
}

function calcReturns() {
	var amount = document.getElementById("amount").value;
	var years = document.getElementById("years").value;
	var rate = document.getElementById("rate").value;
	var fee = document.getElementById("fee").value;
	
	if (amount >= 900000000000) {
		amount = 10000;
		document.getElementById('amount').value = 10000;
	}
	
	var amount_etf = amount;
	var amount_active = amount;
	var rate_etf = rate / 100;
	var rate_active = (rate - fee) / 100;
	
	for (x = 0; x < years; x++) {
		amount_etf = amount_etf * (1 + rate_etf);
		amount_active = amount_active * (1 + rate_active);
	}
	
	amount_etf = Math.round(amount_etf * 100) / 100;
	amount_active = Math.round(amount_active * 100) / 100;
	var diff = (amount_etf / amount_active - 1) * 100;
	diff = Math.round(diff * 100) / 100;
	document.getElementById("calcResults").innerHTML = '<b>Without fees:</b><br/>' + formatCurrency(amount_etf) + '<br/><br/><span style="color:#ff0000;"><b>With fees:</b><br/>' + formatCurrency(amount_active) + '</span><br/><br/><span style="color:#005500;"><b>Extra return:</b><br/>' + diff + '%</span>';

}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function charsRemaining(len, maxlen, spanId, textId) {
	if (len > maxlen) document.getElementById(textId).value = document.getElementById(textId).value.substring(0,maxlen);
	var remaining = maxlen - len;
	if (remaining < 0) remaining = 0;
	document.getElementById(spanId).innerHTML = remaining;

}

function showStep(show,hide1,hide2,hide3) {
	document.getElementById('step'+show).style.display = '';
	document.getElementById('step'+hide1).style.display = 'none';
	document.getElementById('step'+hide2).style.display = 'none';
	document.getElementById('step'+hide3).style.display = 'none';
}

function swapStars(stars, maxstars, id) {
	var onImage = '/images/star.gif';
	var offImage = '/images/emptystar.gif';
	var count = 1;
	
	for (x = 1; x <= stars; x++) {
		document.getElementById(id+count).src=onImage;
		count++;
	}
	for (x = 1; x <= (maxstars-stars); x++) {
		document.getElementById(id+count).src=offImage;
		count++;
	}
}

function showVideo(vid, span, autostart) {
	if (vid == 1) {	
		file = "/vids/flv/intropitch.flv";
		image = "/vids/thumb/intro_l.gif";
	}
	else if (vid == 2) {
		file = "/vids/flv/lawofcompounding.flv";
		image = "/vids/thumb/lawofcompounding_l.gif";
	}
	else if (vid == 3) {
		file = "/vids/flv/wallstreetwantsyouatthetable.flv";
		image = "/vids/thumb/atthetable_l.gif";
	}
	else if (vid == 4) {
		file = "/vids/flv/wallstreetdoesntbeatthemarket.flv";
		image = "/vids/thumb/beatthemarket_l.gif";
	}
	else if (vid == 5) {
		file = "/vids/flv/part1diversifyintomarkets.flv";
		image = "/vids/thumb/part1_l.gif";
	}
	else if (vid == 6) {
		file = "/vids/flv/part2uselowcostetfs.flv";
		image = "/vids/thumb/part2_l.gif";
	}
	else if (vid == 7) {
		file = "/vids/flv/part3tuneuptheportfolio.flv";
		image = "/vids/thumb/part3_l.gif";
	}
	if (file != "" && image != "") {
		var s1 = new SWFObject("/inc/flashplayer/flvplayer.swf","single","280","210","7");
		s1.addParam("allowfullscreen","true");
		s1.addVariable("file",file);
		s1.addVariable("image",image);
		s1.addVariable("width","280");
		s1.addVariable("height","210");
		if (autostart == 1) s1.addVariable("autostart", "true");
		s1.write(span);
	}
}

function openVideo(vid, autostart) {
	if (autostart != 1) autostart = 0;
	window.open("/video?video="+vid+"&autostart="+autostart,"myvideo","menubar=0,resizable=0,status=0,width=380,height=320");
}

function USCanada(country) {
	if (country == 'US') $('.state').css({display:""});
	else $('.state').css({display:"none"});
	
	if (country == 'CA') $('.province').css({display:""});
	else $('.province').css({display:"none"});
}

function checkNewsletter(checked) {
	if (checked == false) alert('Portfolio notifications let you know when your portfolio requires action.  Unchecking this box will prevent you from receiving these important emails.');
}

function resetTiles() {
	$("#tile_1_link").attr("src","/image/block_smart.gif");
	$("#tile_2_link").attr("src","/image/block_fees.gif");
	$("#tile_3_link").attr("src","/image/block_invest.gif");
	$("#tile_4_link").attr("src","/image/block_watch.gif");
	$("#tile_5_link").attr("src","/image/block_advice.gif");
}