$switch1 = "1";
$switch2 = "1";
$switch3 = "1";
$switch4 = "1";
$switch5 = "1";
function checkFirmType(){
	if (advp.TypeFirm.value == "Individual Investor") {
		if ($switch1 == "1") {
			$switch1 = "2";
			expandcontent('ap1');
		} 
	} else {
			if ($switch1 == "2") {
				expandcontent('ap1');
				$switch1 = "1";
			}
	}

	if (advp.TypeFirm.value == "Other") {
		if ($switch2 == "1") {
			$switch2 = "2";
			expandcontent('ap2');
		} 
	} else {
			if ($switch2 == "2") {
				expandcontent('ap2');
				$switch2 = "1";
			}
	}
}

function checkFPType(){
	if (advp.Financial_Planning.value == "Other") {
		if ($switch3 == "1") {
			$switch3 = "2";
			expandcontent('ap3');
		} 
	} else {
			if ($switch3 == "2") {
				expandcontent('ap3');
				$switch3 = "1";
			}
	}
}

function checkCustodian(){
	if (advp.Custodian.value == "Other") {
		if ($switch4 == "1") {
			$switch4 = "2";
			expandcontent('ap4');
		} 
	} else {
			if ($switch4 == "2") {
				expandcontent('ap4');
				$switch4 = "1";
			}
	}
}

function checkPPMS(){
	if (advp.Portfolio_Management.value == "Other") {
		if ($switch5 == "1") {
			$switch5 = "2";
			expandcontent('ap5');
		} 
	} else {
			if ($switch5 == "2") {
				expandcontent('ap5');
				$switch5 = "1";
			}
	}
}

function checkHowHeard() {
	if (advp.Heard_About_Us.value == "Other") {
		if ($switch2 == "1") {
			$switch2 = "2";
			expandcontent('ap6');
		} 
	} else {
			if ($switch2 == "2") {
				expandcontent('ap6');
				$switch2 = "1";
			}
	}
}

// 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.Company.value) == "")  {
		alert ("You must enter your company name");
		return false;
	}
			
	if  (Trim(advform.Username.value) == "")  {
		alert ("You must enter a user name");
		return false;
	}
	if  (Trim(advform.Password.value) == "")  {
		alert ("You must enter a password");
		return false;
	}
	
	if  (Trim(advform.email.value) == "")  {
		alert ("You must enter an Email Address");
		return false;
	} 

// Email address entered - check for validity
	if (!validEmail(advform.email)) {
			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;
		}
	}
}

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 a valid 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;
}

