
// remove leading and trailing spaces
function Trim(s)
{
	var trimmed="";
	var leading = true;
	var trailing = true;
	
	// strip leading spaces
    for(var i = 0; i < s.length; i++) 
	{
        var c = s.charAt(i);
        if (c == ' ') 
        { 
			if (leading==false)
			{
				trimmed=trimmed+c;
			}
		}
		else
		{	
			leading=false;
			trimmed=trimmed+c;
		}
    }
    
    // strip trailing spaces
    for (var i = trimmed.length-1; i>0; i--) 
    {
		var c = trimmed.charAt(i);
		if (c != " ")
		{
			return trimmed;
		}
		else
		{
			trimmed=trimmed.substr(0,trimmed.length-1);
		}
    }
	
	return trimmed;
}

function submitIt(advform) {
	
	
	
	if ((Trim(advform.FirstName.value) == "") || (Trim(advform.LastName.value) == "")) {
		alert ("You must enter your first and last names");
		return false;
	}
	
	if  (Trim(advform.CompanyName.value) == "")  {
		alert ("You must enter your company name");
		return false;
	}
	
	if  (Trim(advform.uname.value) == "")  {
		alert ("You must enter a user name");
		return false;
	}

	if  (Trim(advform.Password.value) == "")  {
		alert ("You must enter a password");
		return false;
	}

	var invalid = " "; // Invalid character is a space

	if (advform.Password.value.indexOf(invalid) > -1) {
		alert("Password must not contain spaces");
		return false;
	}

	if  (Trim(advform.confPW.value) == "")  {
		alert ("Please enter your password twice");
		return false;
	}
	
	if  (Trim(advform.Password.value) != Trim(advform.confPW.value)) {
		alert ("Passwords do not match. Try again");
		return false;
	}
	
	if  (Trim(advform.EmailAddress.value) == "")  {
		alert ("You must enter an Email Address");
		return false;
	} 

// Email address entered - check for validity
	if (!validEmail(advform.EmailAddress)) {
			alert ("You must enter a valid Email Address");
			return false;
	}
		
	if  (!validPct(advform.asset_mutfunds,1)) {
		return false;
	}
	
	if (!validPct(advform.asset_annuities,2)) {
		return false;
	}
	
	//if  ((advform.asset_mutfunds.value == "")|| (parseInt(advform.asset_mutfunds.value) > 100) ) {
	//	alert ("You must enter a value between 0 and 100 for % Assets in  Mutual Funds");
	//	return false;
	//}
	
	//if  ((advform.asset_annuities.value == "")|| (parseInt(advform.asset_annuities.value) > 100) ) {
	//	alert ("You must enter a value between 0 and 100 for % Assets in Annuities");
	//	return false;
	//}
	if (advform.TypeFirm.value == "Individual Investor") {
		soip = advform.Investment.selectedIndex
		ahi  = advform.Income.selectedIndex
		itltm= advform.Num_transactions.selectedIndex
	
		if (advform.Investment.options[soip].value == "") {
		alert("You must choose Size of Investment Portfolio");
		return false;
		}
		if (advform.Income.options[ahi].value == "") {
		alert("You must choose Annual Household Income");
		return false;
		}
		if (advform.Num_transactions.options[itltm].value == "") {
		alert("You must choose number of Investment Transaction in the last 12 months");
		return false;
		}

		byear = parseInt(advform.Born.value);
//		alert (byear);
		if ((advform.Born.value == "") || (byear < 1900)){
			alert("You must enter a valid year of birth");
			advform.Born.focus();
			return false;
		}
	}
	
// Check "other" fields are filled in

	if (advform.TypeFirm.value == "Other") {
		if (advform.OTypeFirm.value == "") {
		  alert("Please specify type of firm");
		  return false;
		}
	}
	if (advform.Custodian.value == "Other") {
		if (advform.OCustodian.value == "") {
		  alert("Please specify primary custodian or broker/dealer");
		  return false;
		}
	}
	
	if (advform.Portfolio_Management.value == "Other") {
		if (advform.OPPMS.value == "") {
		  alert("Please specify primary portfolio management system");
		  return false;
		}
	}
	if (advform.Financial_Planning.value == "Other") {
		if (advform.OPFMS.value == "") {
		  alert("Please specify primary financial planning system");
		  return false;
		}
	}
	if (advform.Heard_About_Us.value == "Other") {
		if (advform.Heard_About_Other.value == "") {
		  alert("Please specify how you heard about us");
		  return false;
		}
	}
	
}

function validPct(amt,type) {
	
	if (type == "1") {
		fieldname = "% Assets in Mutual Funds";
	}
	if (type  == "2") {
		fieldname = "% Assets in Annuities";
	}
	
	if (Trim(amt.value) == "") { 
		alert ("You must enter a value between 0 and 100 for " + fieldname);
		//amt.focus();
		return false;
	}

	num = parseInt(amt.value);
//	alert ("Number =  " + num);
	
	for (i=0; i<amt.value.length; i++) {
		if ((amt.value.charAt(i) < "0") || (amt.value.charAt(i) > "9") ){
			alert ("You must enter a number between 0 and 100 for " + fieldname);
			return false;
		}
	}
	
	if (num < 0 || num > 100) {
		alert ("You must enter a value between 0 and 100 for " + fieldname);
			//amt.focus();
			return false;
	}
	
	return true;
}
// allow commas and numbers
function validAmt(amt) {
	
		/*if (amt.value == "") { 
		alert ("You must enter a number");
		amt.focus();
		return false;
	}*/

	for (i=0; i<amt.value.length; i++) {
		if (amt.value.charAt(i) == ",") {
			continue;
		}
		
		if ((amt.value.charAt(i) < "0") || (amt.value.charAt(i) > "9") ){
			alert ("You must enter numbers and commas only");
			return false;
		}
	}
	
	return true;
}


function validEmail(eml) {
	invalidChars = " /;,:"
	//alert(eml.value);

//	if  (eml.value == "")  {
//		alert ("You must enter an Email Address");
//		eml.focus();
//		return false;
//	}
		
	 for (i=0; i<invalidChars.length; i++) {
	 	badChar = invalidChars.charAt(i);
		if (eml.value.indexOf(badChar,0) > -1) { 
		//	alert ("You must enter a valid Email Address");
		//	eml.focus();
			return false;
		}
	 } // end for

	atPos = eml.value.indexOf("@",1)
	if (atPos == -1) {
	//	alert ("You must enter a valid Email Address");
	//	eml.focus();
		return false;
	}

	if (eml.value.indexOf("@",atPos+1) > -1)  {
	//	alert ("You must enter a valid Email Address");
	//	eml.focus();
		return false;
	}
	periodPos = eml.value.indexOf(".",atPos);

	if (periodPos == -1) {
	//	alert ("You must enter a valid Email Address");
	//	eml.focus();
		return false;
	}
	if (periodPos+3 > eml.value.length) {
	//	alert ("You must enter a valid Email Address");
	//	eml.focus();
		return false;
	}

	return true;
}


