// JavaScript Document
function setMenuBlockHeight()
{
	document.getElementById('mainBodyBlock').style.position = 'relative';
	document.getElementById('mainBodyBlock').style.top = 'auto';
	document.getElementById('mainBodyBlock').style.left = 'auto';
	
	if(document.getElementById('leftNavBlock') && document.getElementById('mainBodyBlock'))
	{
		var intTotalWinHeight = windowSize();
		var intMainBodyHeight;
		if(document.getElementById('mainBodyBlock').clientHeight)
			intMainBodyHeight = document.getElementById('mainBodyBlock').clientHeight;
		else
			intMainBodyHeight = document.getElementById('mainBodyBlock').style.height;
		if(intTotalWinHeight && !isNaN(intTotalWinHeight))
		{
			if(intMainBodyHeight && !isNaN(intMainBodyHeight) && intMainBodyHeight < (intTotalWinHeight - 171))
			{
				document.getElementById('mainBodyBlock').style.height = (intTotalWinHeight - 171) + 'px';
				document.getElementById('leftNavBlock').style.height = (intTotalWinHeight - 221) + 'px';
			}
			else
				document.getElementById('leftNavBlock').style.height = (intMainBodyHeight - 50) + 'px';
		}
	}
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else {
    if( document.documentElement &&
        ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myHeight = document.documentElement.clientHeight;
    } else {
      if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
      }
    }
  }
  return myHeight;
}

function validateForm() {
	if( gloDirection == 'next' ) {
	
		for( t = 0; t < arrReqFields.length; t++ ) {
			if( document.profileform.elements[ arrReqFields[ t ] ] ) {
				if( document.profileform.elements[ arrReqFields[ t ] ].length > 1 && !document.profileform.elements[ arrReqFields[ t ] ].type ) {
		for( var w = 0; w < document.profileform.elements[ arrReqFields[ t ] ].length; w++ ) {
			if( document.profileform.elements[ arrReqFields[ t ] ][w].type.indexOf( 'select' ) > -1 ) {
							if(document.profileform.elements[arrReqFields[t]][w].options[document.profileform.elements[arrReqFields[t]][w].selectedIndex].value.length < 1){
					document.profileform.elements[ arrReqFields[ t ] ][w].focus();
								alert( 'This is a required field' );
					return false;
				}
			} else if( document.profileform.elements[ arrReqFields[ t ] ][w].type != 'hidden' ) {
				if( document.profileform.elements[ arrReqFields[ t ] ][w].value.length < 1 ) {
					document.profileform.elements[ arrReqFields[ t ] ][w].focus();
					alert( 'This is a required field' );
					return false;
				}
			}
		}
	} else if( document.profileform.elements[ arrReqFields[ t ] ].type.indexOf( 'select' ) > -1 ) {
						if( document.profileform.elements[arrReqFields[t]].options[document.profileform.elements[arrReqFields[t]].selectedIndex].value.length < 1){
				document.profileform.elements[ arrReqFields[ t ] ].focus();
							alert( 'This is a required field' );
				return false;
			}
	} else {
		if( document.profileform.elements[ arrReqFields[ t ] ].type != 'hidden' ) {
			if( document.profileform.elements[ arrReqFields[ t ] ].value.length < 1 ) {
				document.profileform.elements[ arrReqFields[ t ] ].focus();
				alert( 'This is a required field' );
				return false;
			}
		}
	}
}
		}
		
		// Length Validation
		for( k = 0; k < arrLenValFields.length; k++ ) {
			if( document.profileform.elements[ arrLenValFields[ k ] ] ) {
				if( document.profileform.elements[ arrLenValFields[ k ] ].type ) {
					if( document.profileform.elements[ arrLenValFields[ k ] ].type != 'hidden' ) 
						if( document.profileform.elements[ arrLenValFields[ k ] ].value.length > 1000 ) {
							document.profileform.elements[ arrLenValFields[ k ] ].focus();
							alert( 'Please ensure this field is no more than 1000 characters long.' );
							return false;
						}
				} else {
					for( s = 0; s < document.profileform.elements[ arrLenValFields[ k ] ].length; s++ ) {
						if( document.profileform.elements[ arrLenValFields[ k ] ][ s ].type != 'hidden' ) {
							if( document.profileform.elements[ arrLenValFields[ k ] ][ s ].value.length > 1000 ) {
								document.profileform.elements[ arrLenValFields[ k ] ][ s ].focus();
								alert( 'Please ensure this field is no more than 1000 characters long.' );
								return false;
							}
						}
					}
				}
			}
		}
		// End Length Validation
		
		// 5000 Length Validation
		for( k = 0; k < arr5000LenValFields.length; k++ ) {
			if( document.profileform.elements[ arr5000LenValFields[ k ] ] ) {
				if( document.profileform.elements[ arr5000LenValFields[ k ] ].type ) {
					if( document.profileform.elements[ arr5000LenValFields[ k ] ].type != 'hidden' ) 
						if( document.profileform.elements[ arr5000LenValFields[ k ] ].value.length > 5000 ) {
							document.profileform.elements[ arr5000LenValFields[ k ] ].focus();
							alert( 'Please ensure this field is no more than 5000 characters long.' );
							return false;
						}
				} else {
					for( s = 0; s < document.profileform.elements[ arr5000LenValFields[ k ] ].length; s++ ) {
						if( document.profileform.elements[ arr5000LenValFields[ k ] ][ s ].type != 'hidden' ) {
							if( document.profileform.elements[ arr5000LenValFields[ k ] ][ s ].value.length > 5000 ) {
								document.profileform.elements[ arr5000LenValFields[ k ] ][ s ].focus();
								alert( 'Please ensure this field is no more than 5000 characters long.' );
								return false;
							}
						}
					}
				}
			}
		}
		// End 5000 Length Validation
		
		// Multiple Checkbox Validation
		for( f = 0; f < arrMultiCheckboxValFields.length; f++ )
		{
			if(document.profileform.elements[arrMultiCheckboxValFields[f]])
			{
				if(document.profileform.elements[arrMultiCheckboxValFields[f]].length) // Multiple checkboxes
				{
					var colCheckfields = document.profileform.elements[arrMultiCheckboxValFields[f]];
					var bHasChecked = false;
					var bHasCheckboxes = false;
					for(var c = 0; c < colCheckfields.length; c++)
					{
						if(colCheckfields[c].type == 'checkbox')
						{
							bHasCheckboxes = true;
							if(colCheckfields[c].checked)
								bHasChecked = true;
						}
					}
					if(bHasCheckboxes && !bHasChecked)
					{
						alert('Please ensure you select at least one Occupational group.');
						return false;
					}
				}
			}
		}
		// End Multiple Checkbox Validation
		
		// Password validation
		if( document.profileform.password ) {
			if( document.profileform.password.length > 1 || document.profileform.reconfirm_password.length > 1 ) {
	if( document.profileform.password.length > 1 ) {
		for( var q = 0; q < document.profileform.password.length; q++ ) {
			if( document.profileform.password[ q ].type != 'hidden' ) {
				strPassword1 = document.profileform.password[ q ].value;
	objField = document.profileform.password[ q ];
			}
		}
	} else {
		strPassword1 = document.profileform.password.value;
		objField = document.profileform.password;
	}  
	if( document.profileform.reconfirm_password.length > 1 ) {
		for( var q = 0; q < document.profileform.reconfirm_password.length; q++ ) {
			if( document.profileform.reconfirm_password[ q ].type != 'hidden' )
				strPassword2 = document.profileform.reconfirm_password[ q ].value;
		}
	} else
		strPassword2 = document.profileform.reconfirm_password.value;
			} else {
	objField = document.profileform.password;
				strPassword1 = document.profileform.password.value;
	strPassword2 = document.profileform.reconfirm_password.value;
			}
			if( strPassword1 != strPassword2 ) {
				objField.focus();
				alert( 'Your passwords did not match.\nPlease re-enter them.' );
				return false;
}
		}
		//
		
	}
	return true;
}

function prevScreen(strURL) {
	gloDirection = 'prev';
	document.profileform.action = strURL;
}

function submitForm(strURL) {
	document.profileform.action = strURL;
}

function init() {
	// This will go through all the form fields and set the onFocus event to change the BG color of the field.
	/*
	for( var p = 0; p < document.profileform.length; p++ ) {
		if( document.profileform[ p ].name.length > 0 )
			document.profileform[ p ].onfocus = function () { this.style.backgroundColor = '#F9CC71'; }
	}

	// This will go through all the form fields and set the onBlur event to set the BG color of the field to nothing.
	for( var q = 0; q < document.profileform.length; q++ ) {
		if( document.profileform[ q ].name.length > 0 )
			document.profileform[ q ].onblur = function () { this.style.backgroundColor = ''; }
	}
	*/
	
	document.profileform.onsubmit = validateForm;

	for( q = 0; q < arrFields.length; q++ ) {
		if( document.profileform.elements[ arrFields[ q ] ] ) {
			if( document.profileform.elements[ arrFields[ q ] ].length > 1 ) {
				for( p = 0; p < document.profileform.elements[ arrFields[ q ] ].length; p++ ) {
					if( document.profileform.elements[ arrFields[ q ] ][ p ].type == 'hidden' ) {
						document.profileform.elements[ arrFields[ q ] ][ p ].name = 'blank';
					}
				}
			}
		}
	}
	if( document.profileform.categories )
		setCatSelectCounts();
}

function goToPerview(strURL) {
	//document.profileform.action = '<%=Request.ServerVariables("SCRIPT_NAME") & "?action=preview" & strQueryString%>';
	document.profileform.action = strURL;
}

function openTips() {
	objWin = window.open('user_profile_tips.asp', 'tips_win', 'width=400,height=455');
}
  
function toggleShowHide(objLayer) {
  if(objLayer.isshown == 'show')
    objLayer.isshown = 'hide';
  else
    objLayer.isshown = 'show';
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);

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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for ( i = 0; i < ( args.length - 2 ); i += 3 ) 
    if ( ( obj = MM_findObj( args[ i ] ) ) != null ) { 
      if( args[ i + 2 ] != '' )
        v = args[ i + 2 ];
      else
        v = obj.isshown;
      if ( obj.style ) { 
        obj = obj.style; 
        v = ( v == 'show' )?'block':( v == 'hide' )?'none':v;
      }
    obj.display = v;
    objLayer = MM_findObj( args[ i ] )
    toggleShowHide( objLayer );
    }
}

function showSubCategory() {
	if(document.profileform.categories.previousSelected)
	{
		var objPrevSelect = document.profileform.categories.options[document.profileform.categories.previousSelected];
		var objPrevDiv = document.getElementById(objPrevSelect.value);
		if(objPrevSelect && objPrevDiv)
			setSelectedNode(objPrevSelect, objPrevDiv);
	}
  for( var b = 0; b < document.arrCatLayers.length; b++ )
  	document.getElementById(document.arrCatLayers[b]).style.display = 'none';
	var objOption = document.profileform.categories.options[document.profileform.categories.selectedIndex];
  strShowLayer = objOption.value;
	if(document.getElementById(strShowLayer))
		document.getElementById(strShowLayer).style.display = 'block';
	document.profileform.categories.previousSelected = document.profileform.categories.selectedIndex;
}

function setCatSelectCounts() {
  for(var i = 0; i < document.profileform.categories.options.length; i++) {
		objOption = document.profileform.categories.options[i];
		objDiv = document.getElementById(objOption.value);
		if(objOption && objDiv)
	    setSelectedNode(objOption, objDiv);
  }
}

function setSelectedNode(objOptionNode, objDiv)
{
	var strValue = objOptionNode.value;
	if(strValue.length > 1) {
		if(objDiv.childNodes) {
			var intTotal = 0;
			for(var y = 0; y < objDiv.childNodes.length; y++) {
				var objF = objDiv.childNodes[y];
				if(objF.type == 'checkbox' && objF.checked)
					intTotal++;
			}
			var regExp = new RegExp('\\([\\d]+ Selected\\)', 'gi');
			if(regExp.test(objOptionNode.innerHTML))
				objOptionNode.innerHTML = objOptionNode.innerHTML.replace(regExp, '(' + intTotal + ' Selected)');
			else
				objOptionNode.innerHTML = objOptionNode.innerHTML + ' (' + intTotal + ' Selected)';
		}
	}
}

function setPasswordFields() {
	var arrFieldNames = new Array('password', 'reconfirm_password');
	for(var a = 0; a < arrFieldNames.length; a++)
		if(document.profileform.elements[arrFieldNames[a]])
			if(document.profileform.elements[arrFieldNames[a]].length && document.profileform.elements[arrFieldNames[a]].length == 2)
				for(var i = 0; i < document.profileform.elements[arrFieldNames[a]].length; i++)
					if(document.profileform.elements[arrFieldNames[a]][i].type == 'password')
						document.profileform.elements[arrFieldNames[a]][i].value = document.profileform.elements[arrFieldNames[a]][i==1?0:1].value;
}

function showObj( obj ) {
  objWin = window.open( '', 'objectWindow', '' );
	if( !objWin.document.body )
		objWin.document.write( '<html><head></head><body></body></html>' );

	objWin.document.body.style.font = '11px Ariel, Helvetica, Sans-serif';
	var strHTML = '<h2>' + obj + '</h2>';
	strHTML += '<table cellspacing="0" border="1">';
	var objRegExp = new RegExp( '<', 'g' );
	var objRegExpLB = new RegExp( '\\n', 'g' );
  for( o in obj ) {
		strHTML += '<tr>';
		strHTML += '<td valign="top">' + o + ': </td>';
		strHTML += '<td valign="top" style="color: #660000;">';
    try {
			strObjTxt = new String( obj[ o ] );
      strHTML += '<pre>' + strObjTxt.replace( objRegExp, '&lt;' ) + '</pre>';
    } catch( er ) {
      strHTML += '--ERROR [' + er + ']--';
    }
		strHTML += '</td>';
    strHTML += '</tr>';
  }
	strHTML += '</table>';
  strHTML += '<br />';
	objWin.document.body.innerHTML += strHTML;
}
