
if (img_prefix == null) { alert("Configuration Error: img_prefix is not defined"); }
self.defaultStatus = "";
//---------------------------------------------------------------
function GetFormElementIndex(formelem) 
{
	for (var i=0;i<document.forms["applyupdate"].elements.length;i++)
 		if (formelem == document.forms["applyupdate"].elements[i])
  		return i;
	return -1;
}
//---------------------------------------------------------------
function SetSelected(formelem)
{
	//formelem.type != "select-one" || formelem.type != "checkbox" || formelem.type != "radio" || formelem.type != "hidden" &&
	if (formelem.type == "text" || formelem.type == "textarea")
	{
		formelem.select();
	}
}
//---------------------------------------------------------------
function CheckInput(formelem)
{
	//text-field & textarea & password
	if ((formelem.type=="password" || formelem.type == "textarea" || formelem.type == "text") && formelem.value != "" && formelem.value != "http://") 
	{
		if (document.images[formelem.name+"_"])
			document.images[formelem.name+"_"].src = img_prefix+"/icon_help_green.gif";
	}
	//select boxes
	else if (formelem.type == "select-one" && formelem.selectedIndex != -1) 
	{
		if(document.images[formelem.name+"_"])
			document.images[formelem.name+"_"].src = img_prefix+"/icon_help_green.gif";
	}
	//radio buttons
	else if (formelem.type == "radio")
	{
		var bOneChecked = false;
		var arrRadios = findallradiobuttons(formelem.name);
		for (var rd = 0; rd < arrRadios.length; rd++)
		{ //see if one is checked
			if (arrRadios[rd].checked) bOneChecked = true;
		}
		if (bOneChecked) document.images[formelem.name+"_"].src = img_prefix+"/icon_help_green.gif";
	}
	//checkbox
	else if (formelem.type == "checkbox" && document.images[formelem.name+"_"])
		document.images[formelem.name+"_"].src = img_prefix+"/icon_help_green.gif";
	else
		ResetFieldPointer(formelem);
	self.status = self.defaultStatus;
}
//---------------------------------------------------------------
function FillCombo(data)
{	/// function fills the subcategorycombo 
	//remove old options
	for (var i = document.applyupdate.subcategory.length ; i > 0 ; i--)
   	document.applyupdate.subcategory.options[0] = null;
	for(var i = 0; i < data.length; i+=2)
	{
     var optionName = new Option(data[i+1], data[i], false, false);
     var length = document.applyupdate.subcategory.length;
     document.applyupdate.subcategory.options[length] = optionName;
	}
	/// set it to -1, except when the OS is MAC, because IE on the MAC can't handle this.
	if(navigator.userAgent.indexOf('Mac') == -1) 
		document.applyupdate.subcategory.selectedIndex = -1;
	else 
		document.applyupdate.subcategory.options[0].selected = true;
	/// reset the subcatogory image pointer		
	if (document.images["subcategory_"])	document.images["subcategory_"].src = "/icon_help_blue.gif";		
}
//---------------------------------------------------------------
function Help(i)
{
	if (document.forms["applyupdate"].elements[eval(i-1)])
		document.forms["applyupdate"].elements[eval(i-1)].focus();	
	
	if (document.forms["applyupdate"].elements[eval(i-1)].type == "text")
		document.forms["applyupdate"].elements[eval(i-1)].select();	
	alert(arrHelpPersonApply[eval(i-1)]);		
}
//---------------------------------------------------------------
function HelpCounterApply(i)
{
	alert(arrHelpCounterApply[eval(i-1)]);
	document.forms["applyupdate"].elements[eval(i-1)].focus();	
	if (document.forms["applyupdate"].elements[eval(i-1)].type == "text")
		document.forms["applyupdate"].elements[eval(i-1)].select();	
}
//---------------------------------------------------------------
function HelpLogon(i)
{
	alert(arrHelpLogon[eval(i-1)]);
	document.forms["applyupdate"].elements[eval(i-1)].focus();	
	if (document.forms["applyupdate"].elements[eval(i-1)].type == "text")
		document.forms["applyupdate"].elements[eval(i-1)].select();	
}
//---------------------------------------------------------------
function ShowStatusHint(i)
{
	eIndex = GetFormElementIndex(i);
	self.status = arrHelp[eval(eIndex)];
}

function fillDropDown(combo, arr)
{
	combo.length = 0;
	for(i=0; i < arr.length; i++)
	{
	  var x = arr[i];
	  var indexOfDollar = x.indexOf('$');
		var subcategorycode = x.substring(0,indexOfDollar);
	  var subcategory = x.substring(indexOfDollar+1); 
  
	  combo[i] = new Option(subcategory);
		combo[i].value = subcategorycode;
	  combo.selectedIndex = -1;//true;
		//remove the checkmark of the subcategory
		if (document.images["subcategory_"])	document.images["subcategory_"].src = img_prefix+"/icon_help_blue.gif";
	}
}	

function SetSubcategory(i)
{
	document.forms["applyupdate"].subcategoryvalue.value = document.forms["applyupdate"].subcategory.options[i].value;
}

function CheckValidCategory()
{
	alert("First Choose a Category.\nThen choose a Subcategory.")
	document.forms["applyupdate"].category.focus();	
}
//---------------------------------------------------------------
function CheckEmail(formelem)
{
	arrInvalidCharacters = new Array("?","#","$","%","^","&","*","(",")","!","~","[","]","{","}","\\","'","`",":",";","'","<",">",",","/","\\",")","+","=","|")
	var bValid = true;
	for (i = 0; i < arrInvalidCharacters.length; i++)
	{
		if (formelem.value.indexOf(arrInvalidCharacters[i]) != -1) 
		{//email address contains an invalid character
			bValid = false;
			break;
		}
	}

	if (formelem.value == "" || formelem.value.indexOf("@") == -1 || formelem.value.indexOf(".") == -1 || formelem.value.length<6) // powerful email validation
	{
		bValid = false;
	}
	
	return bValid;
}

function CheckEmail2(formelem)
{
	if (CheckEmail(formelem))
		SetValidFieldCheckMark(formelem);
	else
		ResetFieldPointer(formelem);
}
//---------------------------------------------------------------
function ResetFieldPointer(formelem)
{	//TO DO : account for checkboxes (there are none in the form now)
	
	//Because radio buttons (and checkboxes) can come in groups,
	//we should count all of these groups which are placed BEFORE the
	//current form element (formelem). This number should be subtracted
	//from the formelement index in order to correctly reset the field pointer (the image belonging to the field).
	var numgroups = 0; 	//numgroups will contain the number of radiobutton groups before the current element
	var numelements = 0;//numelements will contain the number of radiobuttons in all groups before the current element
	// 1. find the element index of formelem
	var index = GetFormElementIndex(formelem);
	// 2. iterate through the form up to the index of the current element and count all
	for (var ix = 0; ix < index ; ix++)
	{
		if (document.forms["applyupdate"].elements[ix].type == "radio")
		{ 
			var radioname = document.forms["applyupdate"].elements[ix].name;
			//skip all radiobuttons with the same name
			var radios = findallradiobuttons(radioname);
			//set the ix to the index of the last element in radios
			//because we want to continue iterating through the form
			//at the end of the radio group
			numelements += radios.length;
			ix = GetFormElementIndex(radios[radios.length - 1]);
			numgroups += 1;
		}
	}
	// set the correct image
	if (document.images[formelem.name+"_"]) 
		//get the index of formelem, add 1 (because we start numbering with 1.gif)
		//substract all radiobuttonELEMENTS and add all radiobuttonGROUPS
		document.images[formelem.name+"_"].src = img_prefix+"/icon_help_blue.gif";
}
//---------------------------------------------------------------
function CheckFullname() 
{
	arrInvalidCharacters = new Array("?","#","£","$","%","^","&","*","(",")","!","~","[","]","{","}","\\","'","`",":",";","'","<",">",",","/","\\",")","+","=","|","@",".","1","2","3","4","5","6","7","8","9","0","_")
	var bValid = true;
	for (i = 0; i < arrInvalidCharacters.length; i++)
	{
		if (formelem.value.indexOf(arrInvalidCharacters[i]) != -1) 
		{//name address contains an invalid character
			bValid = false;
			break;
		}
	}
	if (formelem.value == "" || formelem.value.length<2 ) // check field is not empty
	{
		bValid = false;
	}
	return bValid;
}
//---------------------------------------------------------------
function CheckUsername() 
{
	arrInvalidCharacters = new Array("?","#","$","%","^","&","*","(",")","!","~","[","]","{","}","\\","'","`",":",";","'","<",">",",","/","\\",")","+","=","|","@",".")
	var bValid = true;
	for (i = 0; i < arrInvalidCharacters.length; i++)
	{
		if (formelem.value.indexOf(arrInvalidCharacters[i]) != -1) 
		{//username address contains an invalid character
			bValid = false;
			break;
		}
	}
	if (formelem.value == "" || formelem.value.length<3 ) // check field is not empty
	{
		bValid = false;
	}
	return bValid;
}
//---------------------------------------------------------------
function PointToInvalidField(formelem)
{
	if (document.images[formelem.name+"_"])
		document.images[formelem.name+"_"].src = img_prefix+"/icon_help_red.gif";
}
//---------------------------------------------------------------
function SetValidFieldCheckMark(formelem)
{
	if (document.images[formelem.name+"_"]) 
		document.images[formelem.name+"_"].src = img_prefix+"/icon_help_green.gif";
}
//---------------------------------------------------------------
function CheckPasswords()
{
	return  (document.forms["applyupdate"].password1.value == document.forms["applyupdate"].password2.value && document.forms["applyupdate"].password1.value != "") 
}
//---------------------------------------------------------------
function ShowAlertAndSetPointerToInvalidField(formelem,x)
{
	SetSelected(formelem);
	//IE doesn't put the Invalid Pointer correctly AFTER the WARNING ALERT
	if (document.all) 		{	PointToInvalidField(formelem); alert(arrWarnings[x]);	PointToInvalidField(formelem);}
	if (document.layers) 	{ PointToInvalidField(formelem); alert(arrWarnings[x]); PointToInvalidField(formelem);}
	formelem.focus();
}
//---------------------------------------------------------------
function ShowAlertAndSetPointerToInvalidFieldCounter(formelem,x)
{
	formelem.focus();
	SetSelected(formelem);
	//IE doesn't put the Invalid Pointer correctly AFTER the WARNING ALERT
	if (document.all) 		{	PointToInvalidField(x); alert(arrCounterWarnings[x]);	PointToInvalidField(x);}
	if (document.layers) 	{ PointToInvalidField(x);	alert(arrCounterWarnings[x]);}
}
//---------------------------------------------------------------
function ShowAlertAndSetPointerToInvalidFieldLogon(formelem,x)
{
	formelem.focus();
	SetSelected(formelem);
	//IE doesn't put the Invalid Pointer correctly AFTER the WARNING ALERT
	if (document.all)    { PointToInvalidField(x); alert(arrLogonWarnings[x]);	PointToInvalidField(x);}
	if (document.layers) { PointToInvalidField(x); alert(arrLogonWarnings[x]);}
}
//---------------------------------------------------------------
function IsInSkipList(name)
{	//helper function of Validate()
	//function checks if NAME is in the list. If so => returns true, else returns false
	//ALL FORMELEMENTS appearing in the list will NOT be validated
	var arrDontCheckThese = new Array(
	"mode","referredby","interests"
	)
	for (var p = 0; p < arrDontCheckThese.length ; p++)
	{
		if (name == arrDontCheckThese[p]) return true;
	}
	return false;
}
//---------------------------------------------------------------
function validate()
{
	var bValidated = true;
	for (x = 0; x < document.forms["applyupdate"].elements.length; x++)
	{
		formelem = document.forms["applyupdate"].elements[x];
		if (formelem.type == "hidden" || IsInSkipList(formelem.name)) 
		{
			bValidated = true;//always validate hidden fields
		}
		else if (formelem.type == "radio")
		{
			bValidated = false;
			//find all radiobuttons which belong together
			var arrRadios = new Array();
			arrRadios =	findallradiobuttons(formelem.name);
			//now we have an array of radiobutton which belong together
			//see if one is checked
			for (var rd = 0; rd < arrRadios.length ; rd++)
			{
				if (arrRadios[rd].checked) bValidated = true;
			}
			if (!bValidated)
			{	
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				break;
			}
		}
		else if (formelem.name == "firstname") //firstname requires special validation
		{
			if (!CheckFullname(formelem))
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;
				break;		
			}
		}
		else if (formelem.name == "lastname") //lastname requires special validation
		{
			if (!CheckFullname(formelem))
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;
				break;		
			}
		}
		else if (formelem.name == "username") //username requires special validation
		{
			if (!CheckUsername(formelem))
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;
				break;		
			}
		}
		else if (formelem.name == "email") //email adress requires special validation
		{
			if (!CheckEmail(formelem))
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;
				break;		
			}
		}
		else if (formelem.name == "email2") //email adress2 requires special validation
		{
			if (!CheckEmail(formelem) || (document.forms["applyupdate"].elements[x].value != document.forms["applyupdate"].elements[x-1].value))
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;
				break;		
			}
		}
		else if(formelem.name == "password1" || formelem.name == "password2")
		{
			if (!CheckPasswords()) 
			{
				if (document.forms["applyupdate"].password1.value == "") 
					formelem = document.forms["applyupdate"].password1;
				else if (document.forms["applyupdate"].password2.value == "") 
					formelem = document.forms["applyupdate"].password2;
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;			
				break;
			}
		}
		else if (formelem.type == "select-one")
		{
			if (formelem.selectedIndex == -1)
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);			
				bValidated = false;
				break;		
			}
		}
		else if (formelem.type == "agecheck")
		{
			if(document.forms["applyupdate"].elements[x].value != "1") {
				ShowAlertAndSetPointerToInvalidField(formelem,x);			
				bValidated = false;
				break;		
			}
		}		
		else if (formelem.name == "accept") // accept friendscene's conditions or die!
		{
			bValidated = false;
			if (formelem.checked) bValidated = true;
			if (!bValidated)
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;
				break;
			}
		}
		else
		{
			if (formelem.value == "" || formelem.value == "http://" || formelem.value == null || formelem.value == -1)
			{
				ShowAlertAndSetPointerToInvalidField(formelem,x);
				bValidated = false;
				break;
			}
		}
	}
	//demonstration
	if (bValidated)
	{
		 document.forms["applyupdate"].submit();
		 document.forms["applyupdate"].bb.value='Please Wait...';
	}
}
//---------------------------------------------------------------
function ValidateCounter()
{
	var bValidated = true;
	for (x = 0; x < document.forms["applyupdate"].elements.length; x++)
	{
		formelem = document.forms["applyupdate"].elements[x];
		if (formelem.name == "overzicht") return;//don't check
		else
		{
			if (formelem.value == "" || formelem.value == "http://" || formelem.value == null || formelem.value == -1)
			{
				ShowAlertAndSetPointerToInvalidFieldCounter(formelem,x);
				bValidated = false;
				break;
			}
		}
	}
}
//---------------------------------------------------------------
function ToggleQuestionImage(img)
{
	document.images[img].src = img_prefix+"/question2.gif";
}
//---------------------------------------------------------------
function ToggleQuestionImage2(img)
{
	document.images[img].src = img_prefix+"/question.gif";
}
//---------------------------------------------------------------
function CheckCheckmarks()
{
	for (x=0; x< document.forms["applyupdate"].elements.length; x++)
	{
		var elem = document.forms["applyupdate"].elements[x];
		if (elem.type != "hidden") CheckInput(elem);
	}
}
//---------------------------------------------------------------
function findallradiobuttons(str)
{ //return an array of all radiobuttons (formelements) with the name "str"
	var arrRadios = new Array();
	var index = 0;
	for (var i=0;i<document.forms["applyupdate"].elements.length;i++)
	{
		var formelem = document.forms["applyupdate"].elements[i];
 		if (formelem.name == str)
		{
			arrRadios[index] = formelem;
			index++;
		}
	}
	return arrRadios;
}
//---------------------------------------------------------------
function reselectpreviousselection(main, sub)
{
	// initially deselect the category if there was no previously selected category
	// doesn't work in Opera in "form in table" mode, so select the first category instead
	//
	if (main == "") {
		document.forms["applyupdate"].category.options.selectedIndex = -1;
		if(document.forms["applyupdate"].category.options.selectedIndex != -1)
		if(navigator.userAgent.indexOf("Opera") >= 0)
			FillCombo(eval('arr'+document.forms["applyupdate"].category.options[0].value))
	}
	else
	{	//inspect all option values to find the one with the value prev_parentcategory
		for (var x = 0 ; x < document.forms["applyupdate"].category.options.length ; x++)
		{
			if (document.forms["applyupdate"].category[x].value == main)
			{
				document.forms["applyupdate"].category[x].selected = true;
				document.forms["applyupdate"].category[x] = new Option(
					document.forms["applyupdate"].category[x].text,
					document.forms["applyupdate"].category[x].value,
					false,
					true
				);
				document.forms["applyupdate"].category[x].selected = true;
				alert(document.forms["applyupdate"].category[x].selected);
				document.forms["applyupdate"].category.options.selectedIndex = x;
				var arrSubCategories = eval("arr" + main);
				var combo = document.forms["applyupdate"].subcategory;
				//remove old options
				combo.options.length = 0
				var p = 0;
				for (var y = 0; y < arrSubCategories.length ; y+=2)
				{
					var optionName = new Option(arrSubCategories[y+1], arrSubCategories[y], false, false);
					var length = document.applyupdate.subcategory.length;
					document.applyupdate.subcategory.options[document.applyupdate.subcategory.length] = new Option(
						arrSubCategories[y+1],
						arrSubCategories[y],
						true,
						arrSubCategories[y] == sub, // works for Opera in "form in table" mode
						arrSubCategories[y] == sub
					);
					alert(arrSubCategories[y] == prev_subcategory);
					

					combo[p] = new Option(arrSubCategories[y+1]);
					combo[p].value = arrSubCategories[y];
					p++;
				}
				for (var y = 0; y < combo.options.length ; y++)
				{
					if (combo[y].value == sub)
					{
						combo.selectedIndex = y; // works for NS and IE
						alert("selected " + combo[y].text);
						break;
					}
				}
				break;
			}
		}
	}
}
//------------------------------------------------------------------------------
