function changeDisplay(id, visibility, display)
{	

	  obj = document.getElementsByTagName("div");
	  if(obj[id] != null)
	  {
		     obj[id].style.visibility = visibility;
		     obj[id].style.display = display;
	  }
}
	
function changeObjDisplay(id,objType, visibility, display)
{
    if(objType =="select")		
	   obj = document.getElementsByTagName("select");
	else if(objType =="radio")
	   obj = document.getElementsByTagName("radio");
	   
	if(obj[id] != null)
	{
	     obj[id].style.visibility = visibility;
	     obj[id].style.display = display;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function showSections(){
     var regPage1 = document.getElementById('usRegPage1');
     var regPage2 = document.getElementById('usRegPage2');
     var regPage3 = document.getElementById('usRegPage3');
     //var coRegPage = document.getElementById('usCoRegPage');
     var coRegPage = document.getElementById('updateProfileForm');
     
     if(regPage1 != null){
       showMobile(document.registerForm);
     }
     else if(regPage2 != null){
       showHHSections(document.registerForm);
     }
     else if(regPage3 != null){
       // showNotChiefWageEarner(document.registerForm);
       showNotStatedPI(document.registerForm);
       showNotStated(document.registerForm);
       showWorkFulltime(document.registerForm);
     }
     else if(coRegPage != null){
     	showUpdateMobile(document.updateProfileForm); 
     }
    else { }
}
	
    function hideChildren()
    {
	     var maxChild=7;
         for (var i=1;  i < maxChild; i++){
	         var childDivBaseLbl="child_id_div_";
	         if(document.getElementById(childDivBaseLbl+i) != null)
	         {
                   document.getElementById(childDivBaseLbl+i).value="hide";
             }
            
         }
    }

    function flickChildSectionOnOff()
    {
          changeDisplay('r2id3', 'visible','block');
          hideChildren();
          changeDisplay('r2id3', 'hidden', 'none');
    }


	
	function showHHSections(form)
	{	
	   //isParent(form);
	   showHHSize(form); 
	   showChildSection(form);
	   //testWriteMacros();
	}
	

    function reDisplayHHSections(form)
    {
	   //isParent(form);
	   showChildSection(form);
	   showHHSize(form); 
	   //testWriteMacros();
	}
	
	
	function validateRegPageOne()
	{
	    var countryId=0;
	    if(document.registerForm.country_id != null)
		   countryId = document.registerForm.country_id.options.selectedIndex;
		
		//alert("validateCountry countryId: "+countryId);
		if(countryId == 255){
		     alert("Please select a country.");
		     return false; 
		}else
		  return true;
	}
	
	function showHispSection(form)
	{
	   var i=0;
	   if(document.registerForm.ethnicity_id.options != null)
		    i=document.registerForm.ethnicity_id.options.selectedIndex;
		if (i > 1){
			changeDisplay('hispbox','visible','block');
		}		
		else {

			changeDisplay('hispbox','hidden','none');
			return false;

		}
	}

	function popup (url) {
  		window.open(url, 'new', 'scrollbars=yes, width=420, height=375');
	}
	
	
	function checkAgreeTerms() {	
		if (!document.registerForm.rules.checked){
			alert('To complete your registration, please check the box and agree to the Website Terms and Conditions and Privacy Policy');
			return false;
			}
		else {
			return true;
		}
	}
	
	
	function openUsHispanic() 
	{
		window.location="http://us.lightspeedpanel.com/es/join/register";
	}
	
	
	function openFrca() 
	{
		window.location="http://ca.lightspeedpanel.com/fr/register/join.html";
	}

	function isChildrenYes(form) 
	{
		var sChildren = document.registerForm.presence_children_id[0].checked;
		//alert("isParent: value:"+sParent+":");
		if(sChildren == true) 
        {
		    //alert("isParent: "+sParent);
            //flickChildSectionOnOff();
			changeDisplay('r2id2','visible','block');
			//alert(document.registerForm.child_0005.options.selectedIndex);
			//document.registerForm.child_0005.options.selectedIndex=0;
			//changeDisplay('r2id3','visible','none');
			//changeDisplay('child_take_survey_div','visible','block');
			//showChildStats(); //change number of children displayed
			return true;
		}
		else 
        {
        	changeDisplay('r2id2','hidden','none');
			changeDisplay('r2id3','hidden','none');
        	document.registerForm.presence_children_id[0].checked = false;
			document.registerForm.child_0005.options.selectedIndex = 0;
			document.registerForm.child_0609.options.selectedIndex = 0;
			document.registerForm.child_1012.options.selectedIndex = 0;
			document.registerForm.child_1317.options.selectedIndex = 0;
        	//changeDisplay('child_take_survey_div','hidden','none');
            //alert("isParent :"+sParent+":- hiding children and child section");
          //  hideChildren();
			//flickChildSectionOnOff();
			//changeDisplay('child_take_survey_div','hidden','none');
                       // changeDisplay('r2id3','hidden','none');    
                        //resetRegForm(form);  
			//return true;
			
		}
		
		return false;
    }
    
//Mobile questions scripts added.


 function showUpdateMobile(form) {
	 
 	if(document.updateProfileForm.mobile_contact[0].checked || document.updateProfileForm.flag_sms_alert[0].checked)
 	{
 			changeDisplay('mob_text','visible','block');
 	}
 	else
 	{
 		changeDisplay('mob_text','hidden','none');

 	}
	}

 	function keyRestrictPhone1(e, validchars) {
		var key='', keychar='';
		key = getKeyCode(e);
		if (key == null) 
			return true;
		 keychar = String.fromCharCode(key);
		 keychar = keychar.toLowerCase();
		 validchars = validchars.toLowerCase();
		 //alert(document.registerForm.postal_code.value.length);
	  if (document.registerForm.phone1.value.length==4)
				document.registerForm.phone2.focus();
	  if (validchars.indexOf(keychar) != -1)
		  return true;
	 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		 return true;
	 return false;
	}
	function keyRestrictPhone2(e, validchars) {
		var key='', keychar='';
		key = getKeyCode(e);
		if (key == null) 
			return true;
		 keychar = String.fromCharCode(key);
		 keychar = keychar.toLowerCase();
		 validchars = validchars.toLowerCase();
	  if (document.registerForm.phone2.value.length==3)
				document.registerForm.phone3.focus();
	 if (validchars.indexOf(keychar) != -1)
		  return true;
	 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		 return true;
	 return false;
	}

	function keyRestrictPhone3(e, validchars) {
		var key='', keychar='';
		key = getKeyCode(e);
		if (key == null) 
			return true;
		 keychar = String.fromCharCode(key);
		 keychar = keychar.toLowerCase();
		 validchars = validchars.toLowerCase();
	 if (validchars.indexOf(keychar) != -1)
		  return true;
	 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		 return true;
	 return false;
	}

	function keyRestrictPostcode(e, validchars) {
		var key='', keychar='';
		key = getKeyCode(e);
		if (key == null) 
			return true;
		 keychar = String.fromCharCode(key);
		 keychar = keychar.toLowerCase();
		 validchars = validchars.toLowerCase();
	 if (validchars.indexOf(keychar) != -1)
		  return true;
	 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		 return true;
	 return false;
	}
	function showOther(form)
	{
		//alert('Hello' + document.registerForm.mobile_company_id.options.selectedIndex);
	  if(document.registerForm.mobile_company_id.options.selectedIndex == 7) {
		//alert('Hello' + document.registerForm.mobile_company_id.options.selectedIndex);
		changeDisplay('mobileother', 'visible','block');
		} 	
		else 
		{
			changeDisplay('mobileother','hidden','none');	

		}
	}

	 function getKeyCode(e)
		{
		 if (window.event)
			return window.event.keyCode;
		 else if (e)
			return e.which;
		 else
			return null;
		}

	function showWorkFulltime(form)
	{
	  if(document.registerForm.employment_id.options.selectedIndex == 0 || document.registerForm.employment_id.options.selectedIndex == 1 || document.registerForm.employment_id.options.selectedIndex == 4) {
		//alert('Hello' + document.registerForm.employment_id.options.selectedIndex);
		changeDisplay('fulltimework', 'hidden','none');
		} 	
		else 
		{
			changeDisplay('fulltimework','visible','block');	
		}
	}

	function cieOccupation(form)
	{
	  if(document.registerForm.chief_occupation_id.options.selectedIndex == 1)
		{
		//alert('Hello' + document.registerForm.employment_id.options.selectedIndex);
		changeDisplay('cieself', 'visible','block');
		} 	
		else 
		{
			changeDisplay('cieself','hidden','none');	
		}
		if(document.registerForm.chief_occupation_id.options.selectedIndex == 8)
		{
		//alert('Hello' + document.registerForm.employment_id.options.selectedIndex);
		changeDisplay('ciearmed', 'visible','block');
		} 	
		else 
		{
			changeDisplay('ciearmed','hidden','none');	
		}
		if(document.registerForm.chief_occupation_id.options.selectedIndex == 14)
		{
		//alert('Hello' + document.registerForm.employment_id.options.selectedIndex);
		changeDisplay('cieowner', 'visible','block');
		} 	
		else 
		{
			changeDisplay('cieowner','hidden','none');	
		}
		if(document.registerForm.chief_occupation_id.options.selectedIndex == 16)
		{
		//alert('Hello' + document.registerForm.employment_id.options.selectedIndex);
		changeDisplay('cieengineer', 'visible','block');
		} 	
		else 
		{
			changeDisplay('cieengineer','hidden','none');	
		}
		if(document.registerForm.chief_occupation_id.options.selectedIndex == 18)
		{
		//alert('Hello' + document.registerForm.employment_id.options.selectedIndex);
		changeDisplay('ciehosp', 'visible','block');
		} 	
		else 
		{
			changeDisplay('ciehosp','hidden','none');	
		}
	}

function validatePostalCode() {
		if (document.registerForm.postal_code1.value=="") {
			alert('Please enter postal code.');
			document.registerForm.postal_code1.focus();
			return false;
		}
		else if (document.registerForm.postal_code2.value=="") {
			alert('Please enter postal code.');
			document.registerForm.postal_code2.focus();
			return false;
		}
		else {
	  		document.registerForm.submit();
		}
}

//used by multi-language registration
function showChiefOccupation(form) {
	if (form.chief_employment_id.value==5) {
		form.chief_occupation_id_1.value = 15;
		changeDisplay('chiefocc', 'hidden','none');
	}
	else if (form.chief_employment_id.value==6) {
		form.chief_occupation_id_1.value = 15;
		changeDisplay('chiefocc', 'hidden','none');
	}
	else if (form.chief_employment_id.value==7) {
		form.chief_occupation_id_1.value = 16;
		changeDisplay('chiefocc', 'hidden','none');
	}
	else if (form.chief_employment_id.value==8) {
		form.chief_occupation_id_1.value = 18;
		changeDisplay('chiefocc', 'hidden','none');
	}
	else if (form.chief_employment_id.value==255) {
		form.chief_occupation_id_1.value = 255;
		changeDisplay('chiefocc', 'hidden','none');
	}
	else {
		if(form.chief_occupation_id_1.value.length == 0) {
			form.chief_occupation_id_1.value = 255;
		}
		changeDisplay('chiefocc', 'visible','block');
	}
}

function showOccupation(form) {
	//alert("employment: "+form.employment_id.value);
	if (form.employment_id.value==5) {
		//since 15,16 are not displayed in dropdown list, this assignment zeros out the field
		form.occupation_id_1.value = 15;
		changeDisplay('occ', 'hidden','none');
	}
	else if (form.employment_id.value==6) {
		form.occupation_id_1.value = 15;
		changeDisplay('occ', 'hidden','none');
	}
	else if (form.employment_id.value==7) {
		form.occupation_id_1.value = 16;
		changeDisplay('occ', 'hidden','none');
	}
	else if (form.employment_id.value==8) {
		form.occupation_id_1.value = 18;
		changeDisplay('occ', 'hidden','none');
	}
	else if (form.employment_id.value==255) {
		form.occupation_id_1.value = 255;
		changeDisplay('occ', 'hidden','none');
	}
	else {	
		if (form.occupation_id_1.value.length == 0) {
			form.occupation_id_1.value = 255;
		}
		changeDisplay('occ', 'visible','block');	
	}
	showIndustry(form);
}

function showIndustry(form) {
	//alert("occupation: "+form.occupation_id_1.value);
	//alert("occupation length: "+form.occupation_id_1.value.length);
	if (form.occupation_id_1.value.length > 0 && form.occupation_id_1.value!=15 && form.occupation_id_1.value!=16 && form.occupation_id_1.value!=17 && form.occupation_id_1.value!=255) {
		changeDisplay('ind', 'visible','block');
	}
	else {
		form.industry_id.value = 255;
		changeDisplay('ind', 'hidden','none');
	}
}

function showNotStated(form) {
	if (form.household_income_id_1.value == 99 || form.household_income_id_1.value == 100) {
  		changeDisplay('r2id4', 'visible','block');
	} 	
	else {
		changeDisplay('r2id4','hidden','none');	
	}
}

function showNotStatedPI(form) {
	if(form.personal_income_id_1.value == 99 || form.personal_income_id_1.value == 100) {
		changeDisplay('r2id5', 'visible','block');
	} 	
	else {
		changeDisplay('r2id5', 'hidden', 'none');	
	}
}

function keyRestrictMobile(form, e, validchars) 
{
	
	var key='', keychar='';
	key = getKeyCode(e);
	if (key == null)
	{
		return true;
	}

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
  	if (form.mobile_address.value.length == 0 && (keychar == "0" || keychar == "1"))
  	{
		return false;
	}

 	if (validchars.indexOf(keychar) != -1 || getKeyCode(e) == 8 || getKeyCode(e) == 0)
 	{
		return true;
	}

 	return false;
}

function validateHouseholdAndOccupation(form) {
	//alert('form.household_income_id_1.value: '+form.household_income_id_1.value);
	// if (form.household_income_id_1.value == 99 || form.household_income_id_1.value == 100) {
			// form.household_income_id.value = form.household_income_id_2.value;
	// }
	// else {
			form.household_income_id.value = form.household_income_id_1.value;
	// }
	//alert('household_income_id: '+form.household_income_id.value);	
	
	//alert('form.personal_income_id_1.value: '+form.household_income_id_1.value);
	/*
	if (form.personal_income_id_1.value == 99 || form.personal_income_id_1.value == 100) {
		form.personal_income_id.value = form.personal_income_id_2.value;
	}
	else {
		form.personal_income_id.value = form.personal_income_id_1.value;
	}
	*/
	//alert('personal_income_id: '+form.personal_income_id.value);
	/*
	if (form.employment_id.value==5) {
		form.occupation_id.value = 15;
	}
	else if (form.employment_id.value==6) {
		form.occupation_id.value = 15;
	}
	else if (form.employment_id.value==7) {
		form.occupation_id.value = 16;
	}
	*/
	/*
	else {
		form.occupation_id.value = form.occupation_id_1.value
	}
	*/
	//alert('occupation_id: '+form.occupation_id.value);
	/*
	if (form.chief_employment_id.value==5) {
		form.chief_occupation_id.value = 15;
	}
	else if (form.chief_employment_id.value==6) {
		form.chief_occupation_id.value = 15;
	}
	else if (form.chief_employment_id.value==7) {
		form.chief_occupation_id.value = 16;
	}
	else {
		form.chief_occupation_id.value = form.chief_occupation_id_1.value
	}
	//alert('chief_occupation_id: '+form.chief_occupation_id.value);
	
	if(form.chief_wage_earner_1 != null && form.chief_wage_earner_1[1].checked) {
		form.chief_wage_earner.value = 0;
	}
	*/
}

function showMobile(form) 
{

	if(form.mobile_contact[0].checked || form.flag_sms_alert[0].checked) {
		changeDisplay('mpnmpc', 'visible', 'block');
	}
	else{
		changeDisplay('mpnmpc', 'hidden', 'none');
	}
}

function doShowMobileAddress(flagMobile, flagSMS) 
{
	if (flagMobile==1 || flagSMS == 1)  {
		changeDisplay('mpnmpc', 'visible', 'block');
	} else {
		changeDisplay('mpnmpc','hidden','none');
	}
	
}

// REGISTER (registerPage2)

// Show only "ncluding yourself, how many people live in your household?"
// (household_size_id)
// for US and CA if the number of people in household is
// more than 1 and hide all the rest if displayed
// onChange() event
function showHHSizeCA_US() 
{
	var presenceChildren = document.registerForm.presence_children_id;
		
	// How many people in household including yourself
	var householdSize = document.getElementById('household_size_id');
	// How many children under the age of 18
	var childId = document.getElementById('child_id');
	// Are you a parent of at least one child
	var isParent = document.getElementById('isParent');
	var children = childId.value;

    // Get the size of household here
    var i = 0;
    if(householdSize != null)
    {
	     i = householdSize.value;
	}

	// DIV number of children (containing child_id)
	var numChildren = document.getElementById('num_children');
	// If the household size is greater than one
	// and not "Please Select":
	if (i > 1 && i != 255)
	{

		if (children == 255)
		{
			children = 0;
		}
		
		// Num of children could be at max of
		// household size - 1
		if (children >= i)
		{
			children = i - 1;
			childId.value = i - 1			
		}

		if (children > 0)
		{
			isParent.style.display = 'block';
		}

		// Make the "child_id" select where you are
		// able to select the number of children in
		// household visible if it is not visible
		// If the page is reloaded the display is
		// empty, take care of it here too
		if (numChildren.style.display == "none")
		{
			numChildren.style.display = "block";
			// For IE; if the child_id displays
			// empty choice, show 0
			if (children.length == 0)
			{
				childId.value = 255;
			}
			
			// More than one child - display the texts
			// to enter the child (ren) info
			if (children > 0)
			{
				showChildSectionUS_CA(children);
			}
		}
		// If it was visible it could mean that there was a
		// change and somebody changed the number of people
		// in household.
		// In this case, change the number of children to
		// householdpeople minus one if needed
		else
		{			
			showChildSectionUS_CA(children);
		}

	}
	// If the number of people in household is
	// one or "Please Select" hide everything
	// starting from number of children and
	// down
	else 
	{
		isParent.style.display = 'none';
	
		numChildren.style.display = 'none';

		childId.value = 255;
		presenceChildren[0].checked = false;
		presenceChildren[1].checked = false;

		document.getElementById('child').innerHTML = "";
		return false;
	}
}

// Changing the registration process to match
// the US registration process
// onChange() for "How many are children under the age of 18?"
// select (child_id)
function isParentChildren() 
{
	var isParent = document.getElementById("isParent");

    var num = 0;
	var chilId = document.getElementById("child_id");
	// How many children under the age of 18?
    if(chilId != null)
    {
	   num = chilId.value;
	}
	
	if (num == 255)
	{
		num = 0;
	}

	// At least one person (user) int he household;
	// the num of childeren is at most num of people
	// in household minus one
	var householdSize = document.getElementById('household_size_id');
	var numChild =  householdSize.value - 1;

    if(num > numChild) 
    {	    
        chilId.value = numChild;
		num = numChild;
	} 

	// some children
	if(num > 0)
	{

   		isParent.style.display = 'block';
   		showChildSectionUS_CA(num);

	}
	else
	{
	   	var presenceChildren = document.registerForm.presence_children_id;
	   	presenceChildren[0].checked = false;
		presenceChildren[1].checked = false;

	   	isParent.style.display = 'none';

	   	if (householdSize.value < 2)
	   	{
	   		document.getElementById('num_children').style.display = 'none';
	   	}

       	document.getElementById('child').innerHTML = "";
	}

}

// Show children if the parent of at
// least one (for US and CA so far)
function showChildSectionUS_CA(num) 
{

	var presenceChildren = document.registerForm.presence_children_id;

	// if the number is null, the function is called
	// onChange() for presence children
	if (num == null)
	{
		var chilId = document.getElementById("child_id");
		// How many children under the age of 18?
	    if(chilId != null)
	    {
	   		num = chilId.value;
		}
	}
		
	// If the check box is checked off to "Yes"    
	if (presenceChildren[0].checked)
    {
		showChildren(num);
	}
	else
	{
		// Hide all children if "No"
		document.getElementById('child').innerHTML = "";
	}

}

// END OF REGISTER

// UPDATE PROFILE

function checkIsParent(numChildOriginal, presenceChildren)
{
	var householdSize = document.editBackgroundHouseholdInfoForm.household_size_id;

	if (numChildOriginal == 0 || presenceChildren > -1)
	{
		if (householdSize.value > 1)
		{
			changeDisplay('isParent', 'visible', 'block');
		}

	}
	
	if (presenceChildren == 1)
	{
		changeDisplay('children', 'visible', 'block');
	}
	else
	{
		changeDisplay('children', 'hidden', 'none');
	}
}

function checkSize(form, numChildOriginal, householdSizeOriginal)
{
	var household_size = form.household_size_id;
	var household_size_value = household_size.value;
	
	if (household_size_value == 255)
	{
		household_size_value = 0;
	}
	
	var hhs = household_size_value - householdSizeOriginal;

	if (hhs < 0 && (household_size_value - numChildOriginal < 1))
	{
		household_size.value = householdSizeOriginal;
		
		alert("If you want to downsize the size of the\n" +
	  		  "household, please, remove some child(ren) first.");
	  	return;
	}

	if (household_size_value < 2)
	{
		changeDisplay('isParent', 'hidden', 'none');
		changeDisplay('children', 'hidden', 'none');
	}
	else
	{
		changeDisplay('isParent', 'visible', 'block');
	}

}

function showChildrenUpdate(isParent)
{

	if (isParent.value == 1)
	{
		changeDisplay('children', 'visible', 'block');
	}
	else
	{
		changeDisplay('children', 'hidden', 'none');
	}
}

function dispPhone()
{
	var mobile_contact = document.editCommunicationForm.mobile_contact[0];
	var mobile_contact_value = mobile_contact.checked;
	
	var flag_sms_alert = document.editCommunicationForm.flag_sms_alert[0];
	var flag_sms_alert_value = flag_sms_alert.checked;
	
	var mpnmpc = document.getElementById('mpnmpc');
	
	if (mpnmpc != null && (mobile_contact_value || flag_sms_alert_value))
	{
		mpnmpc.style.visibility = 'visible';
	    mpnmpc.style.display = 'block';
	}
	else
	{
		mpnmpc.style.visibility = 'hidden';
	    mpnmpc.style.display = 'none';
	    
	    document.editCommunicationForm.mobile_address.value = '';
	}
	
}

function restrictto1000(comments)
{

	var txt = comments.value;
	if (txt.length > 1000)
	{
		txt = txt.substring(0, 1000);
		comments.value = txt;
	}

}

// END OF UPDATE PROFILE
/*	
function changeSpanDisplay(id, visibility, display)
{	
	//alert(" changeSpanDisplay span id: "+id);
  	obj = document.getElementsByTagName("span");
  	if(obj[id] != null)
  	{
	     obj[id].style.visibility = visibility;
	     obj[id].style.display = display;
  	}
}

function showChildStats(form) {
	var numChild = calcAllowedNumChild(form);
	form.child_id.value = numChild;
	//alert("numChild: "+numChild);
	if(numChild == "1") {			
		changeDisplay('child_id_div_1','visible','block');
		changeDisplay('child_id_div_2','hidden','none');
		changeDisplay('child_id_div_3','hidden','none');
		changeDisplay('child_id_div_4','hidden','none');
		changeDisplay('child_id_div_5','hidden','none');
		//changeDisplay('child_id_div_6','hidden','none');
		return true;
	}
	if(numChild == "2") {	
		changeDisplay('child_id_div_1','visible','block');
		changeDisplay('child_id_div_2','visible','block');
		changeDisplay('child_id_div_3','hidden','none');
		changeDisplay('child_id_div_4','hidden','none');
		changeDisplay('child_id_div_5','hidden','none');
		//changeDisplay('child_id_div_6','hidden','none');
		return true;
	}
	if(numChild == "3") {
		changeDisplay('child_id_div_1','visible','block');
		changeDisplay('child_id_div_2','visible','block');
		changeDisplay('child_id_div_3','visible','block');
		changeDisplay('child_id_div_4','hidden','none');
		changeDisplay('child_id_div_5','hidden','none');
		changeDisplay('child_id_div_6','hidden','none');
		return true;
	}
	if(numChild == "4") {
		changeDisplay('child_id_div_1','visible','block');
		changeDisplay('child_id_div_2','visible','block');
		changeDisplay('child_id_div_3','visible','block');
		changeDisplay('child_id_div_4','visible','block');
		changeDisplay('child_id_div_5','hidden','none');
		//changeDisplay('child_id_div_6','hidden','none');
		return true;
	}
	if(numChild == "5") {
		changeDisplay('child_id_div_1','visible','block');
		changeDisplay('child_id_div_2','visible','block');
		changeDisplay('child_id_div_3','visible','block');
		changeDisplay('child_id_div_4','visible','block');
		changeDisplay('child_id_div_5','visible','block');
		//changeDisplay('child_id_div_6','hidden','none');
		return true;
	}
	else {
		changeDisplay('child_id_div_1','hidden','none');
		changeDisplay('child_id_div_2','hidden','none');
		changeDisplay('child_id_div_3','hidden','none');
		changeDisplay('child_id_div_4','hidden','none');
		changeDisplay('child_id_div_5','hidden','none');
		//changeDisplay('child_id_div_6','hidden','none');
		return true;					
    }
}	     	    

function showChildSection(form) {
    var num=0;

    if(form.child_id != null)
	   num = form.child_id.value;
	
	//alert("showChildSection: num : "+num);  
	 
	var numChild =  form.household_size_id.value - 1;
	//alert("showChildSection: num child: "+numChild);
 
    if(document.getElementById("child_id") != null && num > numChild) {	    
        form.child_id.value = numChild
		num = numChild;
	} 
	//alert("showChildSection : final num: "+num);    
	
	if(num > 0)
	{
	   	  changeDisplay('r2id3','visible','block');
	   	  //changeDisplay('child_take_survey_div','visible','block');
	      calcAllowedNumChild(form);
		  showChildStats(form); //change number of children displayed
	 }
	 else
	 {
	   	//changeDisplay('r2id2','hidden','none');
	   	changeDisplay('r2id3','hidden','none');
       	changeDisplay('child_id_div_1','hidden','none');
       	changeDisplay('child_id_div_2','hidden','none');
       	changeDisplay('child_id_div_3','hidden','none');
       	changeDisplay('child_id_div_4','hidden','none');
       	changeDisplay('child_id_div_5','hidden','none');
       	//changeDisplay('child_id_div_6','hidden','none');
       	//changeDisplay('child_take_survey_div','hidden','none');
	 }	 
}

function showNotChiefWageEarner(form) {
  //alert(form.chief_wage_earner[1].checked);
  if(form.chief_wage_earner_1 != null && form.chief_wage_earner_1[1].checked) {
	changeDisplay('r2id6', 'visible','block');
	showChiefOccupation(form);
  } 	
  else {
	changeDisplay('r2id6','hidden','none');	
  }
}

function calcAllowedNumChild(form) {
	var maxChild=5;
	var numChild=0;
	var hhSize =0;
    if(document.getElementById("child_id") != null)	    
    	numChild = form.child_id.value;
		//alert(document.registerForm.child_id.options.selectedIndex);
		//alert(numChild);
    if(document.getElementById("household_size_id") != null)  
		hhSize = form.household_size_id.value;
	
	if(hhSize == 255)
		hhSize=0;
	
	if(numChild >= hhSize)
	    numChild = hhSize -1;
	
	if(numChild < 0)
	    numChild=0;

	return numChild;
}

    function showChildSections(form)
	{
	   //alert('showHHSections household sections');	
	   //isParent(form);
	   showHHSize();
	   isChildrenYes(form) ;
	   //showChildDetails(form); 
	   if ( document.registerForm.presence_children_id[0].checked == true )
	   {
		   numberChildren1(form);
		   numberChildren2(form);
		   numberChildren3(form);
		   numberChildren4(form);
	   }
	   
	   //showChildDetails(form);
	   //changeDisplay('r2id3','visible','block');
	   //testWriteMacros();
	}

	function numberChildren1(form)
	{
        //var i=0;
        if(	document.registerForm.child_0005 != null)
		     i1 = document.registerForm.child_0005.options.selectedIndex;
		 if(document.registerForm.child_0609 != null)
		     i2 = document.registerForm.child_0609.options.selectedIndex;
		 if(document.registerForm.child_1012 != null)
		     i3= document.registerForm.child_1012.options.selectedIndex;
		  if(document.registerForm.child_1317 != null)
		     i4 = document.registerForm.child_1317.options.selectedIndex;
		hhSize = document.registerForm.household_size_id.options.selectedIndex;
		i = i1 + i2 + i3 + i4;
		if (i >= hhSize) {
			i = hhSize - (1 +  i2 + i3 + i4 );
			document.registerForm.child_0005.options.selectedIndex = i;
		}
		showChildDetails(form)
  }
function numberChildren2(form)
	{
        //var i=0;
        if(	document.registerForm.child_0005 != null)
		     i1 = document.registerForm.child_0005.options.selectedIndex;
		 if(document.registerForm.child_0609 != null)
		     i2 = document.registerForm.child_0609.options.selectedIndex;
		 if(document.registerForm.child_1012 != null)
		     i3= document.registerForm.child_1012.options.selectedIndex;
		  if(document.registerForm.child_1317 != null)
		     i4 = document.registerForm.child_1317.options.selectedIndex;
		hhSize = document.registerForm.household_size_id.options.selectedIndex;
		i = i1 + i2 + i3 + i4;
		if (i >= hhSize) {
			i = hhSize - (1 +  i1 + i3 + i4);
			document.registerForm.child_0609.options.selectedIndex = i;
		}
		showChildDetails(form)
    }

	function numberChildren3(form)
	{
        //var i=0;
        if(	document.registerForm.child_0005 != null)
		     i1 = document.registerForm.child_0005.options.selectedIndex;
		 if(document.registerForm.child_0609 != null)
		     i2 = document.registerForm.child_0609.options.selectedIndex;
		 if(document.registerForm.child_1012 != null)
		     i3= document.registerForm.child_1012.options.selectedIndex;
		  if(document.registerForm.child_1317 != null)
		     i4 = document.registerForm.child_1317.options.selectedIndex;
		hhSize = document.registerForm.household_size_id.options.selectedIndex;
		i = i1 + i2 + i3 + i4;
		if (i >= hhSize) {
			i = hhSize - (1 +  i2 + i1 + i4);
			document.registerForm.child_1012.options.selectedIndex = i;
		}
		showChildDetails(form)
	 }

	function numberChildren4(form)
	{
        //var i=0;
        if(	document.registerForm.child_0005 != null)
		     i1 = document.registerForm.child_0005.options.selectedIndex;
		 if(document.registerForm.child_0609 != null)
		     i2 = document.registerForm.child_0609.options.selectedIndex;
		 if(document.registerForm.child_1012 != null)
		     i3= document.registerForm.child_1012.options.selectedIndex;
		  if(document.registerForm.child_1317 != null)
		     i4 = document.registerForm.child_1317.options.selectedIndex;
		hhSize = document.registerForm.household_size_id.options.selectedIndex;
		i = i1 + i2 + i3 + i4;
		if (i >= hhSize) {
			i = hhSize - (1 +  i2 + i3 + i1);
			document.registerForm.child_1317.options.selectedIndex = i;	
		}
		showChildDetails(form)
    }
    
     function calcAllowedChildID()
 {
 
      	if(document.getElementById("household_size_id") != null)  
          hhSize = document.registerForm.household_size_id.options.selectedIndex;
		if(document.getElementById("child_id") != null)  
          numChild = document.registerForm.child_id.options.selectedIndex;
   	if(hhSize == 255)
	hhSize=0;
  		if(numChild >= hhSize)
           numChild = hhSize -1;
	if(numChild < 0)
     		numChild=0;
    	document.registerForm.child_id.options.selectedIndex = numChild ;
 }

function showHHSize(form) 
{
    
    var i=0;
    if(form.household_size_id != null)
    {
	     i = form.household_size_id.value;
	}

	if (i > 1 && i != 255)
	{	
		changeDisplay('r2id1','visible','block');
		showChildSection(form);
	}
	else 
	{
		changeDisplay('isParent', 'display', 'none');
	
		changeDisplay('r2id1', 'hidden', 'none');
		//changeDisplay('r2id2','hidden','none');
		changeDisplay('r2id3', 'hidden', 'none');
		//changeDisplay('child_take_survey_div','hidden','none');
		form.child_id.value=0;
		changeDisplay('child_id_div_1', 'hidden', 'none');
		changeDisplay('child_id_div_2', 'hidden', 'none');
		changeDisplay('child_id_div_3', 'hidden', 'none');
		changeDisplay('child_id_div_4', 'hidden', 'none');
		changeDisplay('child_id_div_5', 'hidden', 'none');
		changeDisplay('child_id_div_6', 'hidden', 'none');
		return false;
	}
}

function showChildDetails(form)
{
	//alert(s);
   //var numChild = calcAllowedNumChild();
 
    var s =   document.registerForm.child_id.options.selectedIndex ;
  //  var i = 1;
   // var h = document.registerForm.household_size_id.options.selectedIndex;
   // if (s >= h) 
   // 	s = h - i;	
    var numChild = s;
    	 
		if(numChild == "1") {
			changeDisplay('r2id3','visible','block');
			changeDisplay('child_id_div_1','visible','block');
			changeDisplay('child_id_div_2','hidden','none');
			changeDisplay('child_id_div_3','hidden','none');
			changeDisplay('child_id_div_4','hidden','none');
			changeDisplay('child_id_div_5','hidden','none');
			return true;
		}
		 if(numChild == "2") {
			changeDisplay('r2id3','visible','block');
			changeDisplay('child_id_div_1','visible','block');
			changeDisplay('child_id_div_2','visible','block');
			changeDisplay('child_id_div_3','hidden','none');
			changeDisplay('child_id_div_4','hidden','none');
			changeDisplay('child_id_div_5','hidden','none');
			return true;
		}
		if(numChild == "3") {
			changeDisplay('r2id3','visible','block');
			changeDisplay('child_id_div_1','visible','block');
			changeDisplay('child_id_div_2','visible','block');
			changeDisplay('child_id_div_3','visible','block');
			changeDisplay('child_id_div_4','hidden','none');
			changeDisplay('child_id_div_5','hidden','none');
			return true;
		}
		if(numChild == "4") {
			changeDisplay('r2id3','visible','block');
			changeDisplay('child_id_div_1','visible','block');
			changeDisplay('child_id_div_2','visible','block');
			changeDisplay('child_id_div_3','visible','block');
			changeDisplay('child_id_div_4','visible','block');
			changeDisplay('child_id_div_5','hidden','none');
			return true;
		}
		if(numChild == "5" || numChild == "6") {
			changeDisplay('r2id3','visible','block');
			changeDisplay('child_id_div_1','visible','block');
			changeDisplay('child_id_div_2','visible','block');
			changeDisplay('child_id_div_3','visible','block');
			changeDisplay('child_id_div_4','visible','block');
			changeDisplay('child_id_div_5','visible','block');
			return true;
		}
	
 		     else
           {
			changeDisplay('r2id3','hidden','none');
			changeDisplay('child_id_div_1','hidden','none');
			changeDisplay('child_id_div_2','hidden','none');
			changeDisplay('child_id_div_3','hidden','none');
			changeDisplay('child_id_div_4','hidden','none');
			changeDisplay('child_id_div_5','hidden','none');
			return true;					
           }
 }	    

function resetRegForm(form) 
{
       
      var isParent = document.registerForm.presence_children_id[0].checked;
      //alert("resetRegForm()  isParent value:"+isParent+":");  
      
      if(isParent == false)
      {
        
        //preserve current form object values
        var maritalStatus = form.marital_status_id.options.selectedIndex; 
        var hhSize = document.registerForm.household_size_id.options.selectedIndex;
        var numChild = document.registerForm.child_id.options.selectedIndex;
        //alert("resetRegForm - maritalStatus : "+maritalStatus+" hhSize: "+hhSize+" numChild: "+numChild+" isParent"+isParent);
        //reload form
        window.location.reload();
        //alert("resetRegForm after reload - maritalStatus : "+maritalStatus+" hhSize: "+hhSize+" numChild: "+numChild+" isParent"+isParent);
        //reset form values
      
        document.registerForm.marital_status_id.options.selectedIndex=maritalStatus; 
        document.registerForm.household_size_id.options.selectedIndex=hhSize;
        document.registerForm.child_id.options.selectedIndex=numChild;
        document.registerForm.presence_children_id[0].checked=false;
        document.registerForm.presence_children_id[1].checked=true;
        reDisplayHHSections(form);
      }
         

}

*/