//_______________________________________
//a trim property of string var strVar.value.trim()
//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
String.prototype.trim = function()
{
 // skip leading and trailing whitespace
 // and return everything in between
 //======================================
  return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

function is_email(strString)
{
	if (strString.length <= 2) return false;
	if (strString.indexOf("@") == -1)	return false;
	if (strString.indexOf(".") == -1) return false;
	if (strString.indexOf("@") != strString.lastIndexOf("@")) return false;
	
	return true;
}

//__________________________________________________________
//used in addnewport.php for assign the value to the textfield of txtPName
//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
function change_portfolio_to_rename()
{	

	var aObj = document.frmAdd.selPName[document.frmAdd.selPName.selectedIndex].text.trim();
	if (aObj.substring(aObj.length-10, aObj.length) == " (DEFAULT)")
	{
		aObj = aObj.substring(0, aObj.length-10);		
	}

	document.frmAdd.txtPName.value = aObj;
	document.frmAdd.txtPName.select();

	document.frmAdd.txtPName.focus();
}

//_____________________________________________________________________________
//when select from the drop down list of melect, it adds the selected abbrev to the search textfield
//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
function add_search_keyword()
{	
	var objSearch = document.frmAdd.melAbb[document.frmAdd.melAbb.selectedIndex].text.trim();	
	var objDisplayDes = document.frmAdd.melAbb[document.frmAdd.melAbb.selectedIndex].value.trim();
	if (objSearch == "Select Stocks") 
	{
		objSearch = "";
		objDisplayDes = "";
	}
	
	document.frmAdd.txtDisplayDes.value = objDisplayDes;
	document.frmAdd.txtSearch.value += " " + objSearch;	
}

//_______________________________________
//used for checking value when submit in show_det.php
//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
function check_show_det_value()
{
	document.frmSearch.txtSearch.value = document.frmSearch.txtSearch.value.trim();
	if (document.frmSearch.txtSearch.value == "")
	{
		alert("Enter a stock code to search please.");
		document.frmSearch.txtSearch.focus();
		return false;
	}
	return true;
}

function add_new_portfolio()
{
	//showModalDialog --- window.open	
	//var winPName = showModalDialog("add.php", "", "dialogWidth:240px;dialogHeight:155px;help:no;scroll:no;status:yes;resizable:no;");
	var winPName = window.open("add.php", "", "Width=240,Height=155,help=no,scroll=no,status=yes,resizable=no");
}

function check_new_portfolio_name()
{
	var strPName = document.frmAddPName.txtPName.value.trim();		
	 if (strPName=="")
	 { 
	 	alert('Portfolio Name is Required.');
	 	document.frmAddPName.txtPName.focus();
	 	return false;
	 }
	 else
	 {
	 	document.frmAddPName.hidAction.value='AddNewPortfolio';	 	
	 	opener.document.forms[0].hidPName.value = strPName;
		opener.document.forms[0].hidAction.value='AddNewPortfolio';
		opener.document.forms[0].action = "addnewport.php";
		opener.document.forms[0].submit();
	 	window.close();
	 	return false;
	 }
}

function func_valid_in_register()
{
	txt_password = document.form1.txt_password.value
	txt_confpass = document.form1.txt_confpass.value
	txt_fname = document.form1.txt_fname.value
	txt_lname = document.form1.txt_lname.value
	txt_loginid = document.form1.txt_loginid.value

	if (txt_password.length < 6)
	{
		alert("Please enter a Password with minimum of 6 characters")
		return false ;
	}

	if (txt_password !== txt_confpass)
	{
		alert("Passwords do not match")
		return false ;
	}

	if ((txt_fname == "" ) || (txt_lname == "" ) || (txt_loginid == "" ) || (txt_password == "" ) || (txt_confpass == "" ) )
	{
		alert("Please fill in all the details");
		return false;
	}

	if (document.form1.chkTerm.checked==false)
	{
		alert("Please read & accept the Terms of Usage of this website.")
		return false ;
	}

	document.form1.method="post";
	document.form1.action="register.php";
	document.form1.submit();
}

//used in sec_act.php to check the value of buy sell order forms
//==============================================
function check_unlisted_order()
{	
	txt_fname = document.frmSecAct.txt_fname.value.trim();
	txt_lname = document.frmSecAct.txt_lname.value.trim();
	txt_loginid = document.frmSecAct.txt_loginid.value.trim();
	txt_phone = document.frmSecAct.txt_phone.value.trim();
	txt_price = document.frmSecAct.txt_price.value.trim();
	txt_qty = document.frmSecAct.txt_qty.value.trim();

	if (txt_fname.length <= 2)
	{
		alert("Your first name must be longer than 2 letters");
		document.frmSecAct.txt_fname.select();
		document.frmSecAct.txt_fname.focus();
		return false ;
	}

	if (txt_lname.length <= 2)
	{
		alert("Your last name must be longer than 2 letters");
		document.frmSecAct.txt_lname.select();
		document.frmSecAct.txt_lname.focus();
		return false ;
	}
	
	if (is_email(txt_loginid)==false)
	{
		alert('Your email address is not entered correctly');
		document.frmSecAct.txt_loginid.select();
		document.frmSecAct.txt_loginid.focus();
		return false ;
	}

	if (txt_phone.length <= 5)
	{
		alert("Your phone number is not entered correctly");		
		document.frmSecAct.txt_phone.select();
		document.frmSecAct.txt_phone.focus();
		return false ;
	}
	
	
	if (formatCurrency(txt_price)<=0)		
	{
		alert("You have not entered the price correctly");		
		document.frmSecAct.txt_price.select();
		document.frmSecAct.txt_price.focus();
		return false ;
	}

	if (formatCurrency(txt_qty)<=999)
	{
		alert("Minimum amount of shares is 1000");
		document.frmSecAct.txt_qty.select();
		document.frmSecAct.txt_qty.focus();
		return false ;
	}

	document.frmSecAct.hidAction.value = "Order";	
	return true;	
}

function printme()
{	
	document.forms[0].method="post";
	document.forms[0].hidAction.value=document.forms[0].name;
	document.forms[0].action="printme.php";
	document.forms[0].submit();	
}

function pop_terms_or_privacy(strType)
{	
	if (strType=="t" || strType=="p" || strType=="c")
	{
		var winPName = window.open("document.php?t="+strType, "", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=455");
	}	
}

function check_cash_input_values()
{	
	strCName = document.forms[0].txtCompanyName.value.trim();
	strFValue = document.forms[0].txtFaceValue.value.trim();
	strIRate = document.forms[0].txtInterestRate.value.trim();
	if (strCName == "")
	{
		alert("Company Name is required");	
		document.forms[0].txtCompanyName.focus();
		return false;
	}

	if (strFValue == "")
	{
		document.forms[0].txtFaceValue = "0.00";
	}
	else
	{
		if (isNaN(strFValue))
		{
			alert("Please enter a numeric value for Face Value.");
			document.forms[0].txtFaceValue.select();
			document.forms[0].txtFaceValue.focus();
			return false;
		}
	}

	if (strIRate  == "")
	{		
		document.forms[0].txtInterestRate = "0.00";		
	}
	else
	{
		if (isNaN(strIRate))
		{
			alert("Please enter a numeric value for Interest Rate.");
			document.forms[0].txtInterestRate.select();
			document.forms[0].txtInterestRate.focus();
			return false;
		}
		else
		{
			if (strIRate>100 || strIRate<-100)
			{
				alert("Please enter a numeric value between -100 and 100 for Interest Rate.");
				document.forms[0].txtInterestRate.select();
				document.forms[0].txtInterestRate.focus();
				return false;			
			}
		}		
	}

	document.forms[0].hidAction.value='AddCash';
	document.forms[0].action='show_det.php';
}

function show_chart_option()
{
	//document.style.height = 600;	
	//window.resizeTo(610,470);

	if (document.getElementById)
	{  
		obj = document.getElementById("divOptions");			
		if (obj.style.visibility=="hidden")
		{
			obj.style.visibility = 'visible';
			window.resizeTo(610,720);
		}
		else
		{
			obj.style.visibility = 'hidden';
			window.resizeTo(610,540);
		}
	}
	else if (document.all)
	{
		obj = document.all["divOptions"];
		if (obj.style.visibility=="hidden")
		{
			obj.style.visibility = 'visible';
			window.resizeTo(610,720);
		}
		else
		{
			obj.style.visibility = 'hidden';
			window.resizeTo(610,540);
		}
	}
	else if (document.layers)
	{
		obj = document.layers["divOptions"];
		if (obj.visibility=="hidden")
		{
			obj.visibility = 'visible';
			window.resizeTo(610,720);
		}
		else
		{
			obj.visibility = 'hidden';
			window.resizeTo(610,540);
		}
	}
}

function check_add_stock()
{
	var aStr = "";
	if (document.frmAdm.hidAction.value=="AddStockCode")
	{	
		if (document.frmAdm.txtNSAbbrev.value.trim() == "")
		{
			alert('Code is required');
			document.frmAdm.txtNSAbbrev.select();
			document.frmAdm.txtNSAbbrev.focus();
			return false;
		}

		if (document.frmAdm.txtNSDes.value.trim() == "")
		{
			alert('Description is required');
			document.frmAdm.txtNSDes.select();
			document.frmAdm.txtNSDes.focus();
			return false;
		}
		
		if (document.frmAdm.txtNSEPS.value.trim()=="") aStr += "EPS\r\n";
		if (document.frmAdm.txtNSDPS.value.trim()=="") aStr += "DPS\r\n";
		if (document.frmAdm.txtNSOpen.value.trim()=="") aStr += "Open\r\n";
		if (document.frmAdm.txtNSLow.value.trim()=="") aStr += "Low\r\n";
		if (document.frmAdm.txtNSHigh.value.trim()=="") aStr += "High\r\n";
		if (document.frmAdm.txtNSClose.value.trim()=="") aStr += "Close\r\n";
		if (document.frmAdm.txtNSVolume.value.trim()=="") aStr += "Volume\r\n";
		if (aStr != "") 
		{
			aStr = "The following fields are not filled. \r\n"+aStr;
			aStr += "Please click OK to continue.";
			var aBool = confirm(aStr);
			if (aBool != true) return false;		
		}
	}
	return true;
}

function is_numeric(strString, strType)
{
	if (strType=="int") var strValidChars = "0123456789";
	if (strType=="flt") var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	//test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		 {
			 blnResult = false;
		 }
	}
	return blnResult;
}

function calculator()
{
	var strErr = "";
	var fltInitial = new String(document.frmCal.txtInitial.value.trim());
	rExp = /,/gi;
	fltInitial = fltInitial.replace(rExp, "");
	if (!is_numeric(fltInitial, "flt")||fltInitial=="") 
	{
		strErr += "Initial Contribution is required\r\n"; 
	}
	else 
	{
		document.frmCal.txtInitial.value = formatCurrency(fltInitial);
		fltInitial = parseFloat(fltInitial);		
	}
	
	var fltMonthly = document.frmCal.txtMonthly.value.trim();
	if (!is_numeric(fltMonthly, "flt")||fltMonthly=="") strErr += "Contribution Amount is required\r\n"; 

	var fltYield = document.frmCal.txtYield.value.trim();
	if (!is_numeric(fltYield, "flt") || fltYield=="" || fltYield>10) strErr += "Gross Estimated Income Yield allows Maximum 1 digit + 1 decimal place.\r\n";
		
	var fltTax = document.frmCal.txtTax.value.trim();
	if (!is_numeric(fltTax, "int") || fltTax=="") strErr += "Tax Rate allows 2 digits maximum, and is integer only\r\n";	
	var boolReinvest = ((document.frmCal.selReinvest[document.frmCal.selReinvest.selectedIndex].value=="yes"))?true:false;	
	
	var fltGrowth = document.frmCal.txtGrowth.value.trim();
	if (!is_numeric(fltGrowth, "flt") || fltGrowth=="" || fltGrowth>100) strErr += "Estimated Capital Growth allows Maximum 2 digits + 1 decimal place.\r\n";
	
	var intYear = document.frmCal.txtYear.value.trim();
	if (!is_numeric(intYear, "int") || intYear=="" || intYear>100) strErr += "Life of Portfolio - years allows Maximum 2 digits\r\n"; 
		
	if (strErr == "")
	{			
		return true;
	}
	else
	{
		alert(strErr);
		return false;
	}	
}

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 show_calculator_notes()
{
	intHeightAccum = 308;
	intHeightNoAccum = 288;
	intHeightNotes = 580;
	intWidth = 680;
	intWidthNotes = 680;
	var aStr = "";
	aStr += '			<table class=\"trBottom\" width=\"650\" style="visibility: inherit;" border=0 cellspacing=1 cellpadding=0 hspace=0 vspace=0 align="left" valign=top height="0">';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;&nbsp;</td><td class="trStockTitle" height="15" style="font-size:11px;"><strong>Calculator Notes</strong></td></tr>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Initial Contribution:</b> enter here the initial amount that you would use to start your portfolio. This can be as little as $1000.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Additional Contributions:</b> select a time period that you would like to make additional cash contributions to your portfolio.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Contribution Amount:</b> enter an amount that you feel you can add to your portfolio for the above selected time period.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Gross Estimated Income Yield:</b> this figure is an estimate of the gross Dividends you can expect from your NZ and Australian investments. If your portfolio is likely to have a greater weighting in High growth-Low dividend shares then insert a much lower figure.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Select your Tax Rate:</b> 33% is the normal tax rate that dividends in NZ are taxed on. If your tax rate is higher then you can manually change this rate.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Reinvest Income:</b> select here if you wish to reinvest your income from dividends or take out the tax-paid dividends in cash. Taking out dividends will impact the future value of your portfolio.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Estimated Capital Growth:</b> you can select a % figure that you believe the market will continue to grow at, on average over the life of your portfolio. Being conservative with market capital growth estimates will help take into account yearly fluctuations in the market.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Life of Portfolio  Years:</b> enter here the number of years you are likely to maintain this portfolio. The longer the life of the portfolio, the greater the effect that compounding will have on the outcome.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Calculated Portfolio Value:</b> after inputting values for the above fields, click on calculate to find out the calculated future value of your portfolio.</td></tr>';
	//aStr += '				<tr style="visibility: inherit;" height=1><td colspan="2"></td>';
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;"><b>Accumulated Income:</b> The amount displayed here is the total projected amount of tax paid dividends available to have been withdrawn over the period of the portfolio life.</td></tr>';
	aStr += '				<tr style="visibility: inherit;" height=3><td colspan="2"></td>';	
	aStr += '				<tr style="visibility: inherit;"><td>&nbsp;</td><td class="stdtext" style="font-size:11px;line-height:100%;">For advice on creating a share portfolio, <b>contact Somerset Smith Partners on 06-835-3126</b></td></tr>';
	aStr += '			</table>';
	
	if (document.getElementById)
	{  
		obj = document.getElementById("divNotes");		
		if (document.getElementById("divNotes").style.visibility=="hidden")
		{	
			obj.style.visibility = 'visible';			
			if (document.getElementById('tdAccumLabel')) 
			{				
				window.resizeTo(intWidthNotes,(intHeightNotes+15));				
			}
			else
			{				
				window.resizeTo(intWidthNotes,intHeightNotes);
			}			
			obj.innerHTML = aStr;
		}
		else
		{	
			obj.style.visibility = 'hidden';
			if (document.getElementById('tdAccumLabel')) 
			{
				
				window.resizeTo(intWidth,intHeightAccum);
			}
			else
			{				
				window.resizeTo(intWidth,intHeightNoAccum);
			}
			
			obj.innerHTML = "";
		}
	}
	else if (document.all)
	{
		obj = document.all["divOptions"];
		if (obj.style.visibility=="hidden")
		{
			obj.style.visibility = 'visible';
			window.resizeTo(intWidthNotes,intHeightNotes);
			//document.body.scroll="yes";
			obj.innerHTML = aStr;
		}
		else
		{
			obj.style.visibility = 'hidden';
			if (document.all["tdAccumLabel"].visibility=='visible') window.resizeTo(intWidth,intHeightAccum); else window.resizeTo(intWidth,intHeightNoAccum);
			document.body.scroll="no";
			obj.innerHTML = "";
		}
	}
	else if (document.layers)
	{
		obj = document.layers["divOptions"];
		if (obj.visibility=="hidden")
		{
			obj.visibility = 'visible';
			window.resizeTo(intWidthNotes,intHeightNotes);
			//document.body.scroll="yes";
			obj.innerHTML = aStr;
		}
		else
		{
			obj.visibility = 'hidden';
			if (document.layers["tdAccumLabel"].visibility=='visible') window.resizeTo(intWidth,intHeightAccum); else window.resizeTo(intWidth,intHeightNoAccum);
			//document.body.scroll="no";
			obj.innerHTML = "";
		}
	}
}

function reset_calculator()
{	
	document.frmCal.txtInitial.value = "50,000.00";
	document.frmCal.selPeriod.selectedIndex = 0;	
	document.frmCal.txtMonthly.value = 500;
	document.frmCal.txtYield.value = 6.5;		
	document.frmCal.txtTax.value = 33;
	document.frmCal.selReinvest.selectedIndex = 0;	
	document.frmCal.txtGrowth.value = 8.5;	
	document.frmCal.txtYear.value = 20;
	document.frmCal.submit();
}
