function getPropByLang(id,prop){
	var strLang = new String(document.getElementById(id).lang)
	var arrLang = strLang.split('~')
	for(iArrLang=0; iArrLang < arrLang.length; iArrLang++){
		var testValue = new String(arrLang[iArrLang])
		if(testValue.indexOf(prop +'=') == 0){
			var subArray = testValue.split('=')
			return subArray[1]
		}
	}
	return ''
}


var strAlphaNum = new String('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ ')
var strName = new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"+"' ")
var strFreeText = new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,!`*()-+:;\"'.?_ \r\n")
var strDOB = new String("0123456789/")

function validate(formName,forceSubmit){
	var myForm = document.getElementById(formName)
	for(iValidate = 0; iValidate < myForm.elements.length; iValidate++){
		myElement = myForm.elements[iValidate] 
		// required fields 
		if(getPropByLang(myElement.id,'pid') == 1 && myElement.value == 0){
			alert('The ' + getPropByLang(myElement.id,'fn') + ' you entered already exists\n\n.')
			myElement.focus()
			return false
		}
		// required fields 
		if(getPropByLang(myElement.id,'m') == 1 && !myElement.value){
			alert(getPropByLang(myElement.id,'fn') + ' must be filled in before submitting.')
			myElement.focus()
			window.scrollBy(0,-80)
			return false
		}
		// alpha numeric fields
		if(getPropByLang(myElement.id,'an') == 1){
			for(iAlphaNum = 0; iAlphaNum < myElement.value.length; iAlphaNum++){
				if( strAlphaNum.indexOf(myElement.value.charAt(iAlphaNum)) == -1){
					alert(getPropByLang(myElement.id,'fn') + ' may only contain letters, numbers and the underscore (_) character.')
					myElement.focus()
					window.scrollBy(0,-80)
					return false
				}
			}			
		}	
		// free text fields
		if(getPropByLang(myElement.id,'ft') == 1){
			for(iFt = 0; iFt < myElement.value.length; iFt++){
				if(strFreeText.indexOf(myElement.value.charAt(iFt)) == -1){
					alert(getPropByLang(myElement.id,'fn') + ' should not contain any special characters such as: \\ # [ ] { } < > ^')
					myElement.focus()
					window.scrollBy(0,-80)
					return false
				}
			}			
		}
		// DOB
		if(getPropByLang(myElement.id,'dob') == 1){
			for(iDb = 0; iDb < myElement.value.length; iDb++){
				if(strDOB.indexOf(myElement.value.charAt(iDb)) == -1){
					alert(getPropByLang(myElement.id,'fn') + ' should only contain numbers and / character')
					myElement.focus()
					window.scrollBy(0,-80)
					return false
				}
			}			
		}		
		
		// email
		if(getPropByLang(myElement.id,'em') == 1){
			var emailValue = myElement.value
			var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
			if (!filter.test(emailValue)){
				alert(getPropByLang(myElement.id,'fn') + ' must be a vaild e-mail address')
				myElement.focus()
				window.scrollBy(0,-80)
				return false
			}
		}
		// name
		if(getPropByLang(myElement.id,'nm') == 1){
			for(iNm = 0; iNm < myElement.value.length; iNm++){
				if(strName.indexOf(myElement.value.charAt(iNm)) == -1){
					alert(getPropByLang(myElement.id,'fn') + ' may only contain letters, dashes, or single quote marks.')
					myElement.focus()
					window.scrollBy(0,-80)
					return false
				}
			}			
		}	
		// duplicate
		if(getPropByLang(myElement.id,'dp') == 1){
			if(myElement.value != myForm.elements[iValidate-1].value){
				alert(getPropByLang(myElement.id,'fn') + ' must be the same as ' + getPropByLang(myForm.elements[iValidate-1].id,'fn'))
				myElement.focus()
				window.scrollBy(0,-80)
				return false
			}
		}
		// mandatory radio
		if(getPropByLang(myElement.id,'mr') == 1){
			var mrChecked = 0
			var myRadios = document.getElementsByName(myElement.name)
			for(iMr=0; iMr < myRadios.length; iMr++){
				if(myRadios.item(iMr).checked == true){
					mrChecked = 1
				}
			}
			if (mrChecked == 0){
				alert('Please check one of the buttons in the ' + getPropByLang(myElement.id,'fn') + ' area')
				myElement.focus()
				window.scrollBy(0,-80)
				return false
			}
		}
		//mandatory drop-down
		if(getPropByLang(myElement.id,'mdd') == 1){
			if(myElement.options[myElement.selectedIndex].value == ''){
				alert('Please select an option from the ' + getPropByLang(myElement.id,'fn') + ' list')
				myElement.focus()
				window.scrollBy(0,-80)
				return false
			}
		}
		//addup

		if(getPropByLang(myElement.id,'addup') == 1 && myElement.value != '4'){
			alert('This question must be answered correctly before submitting.')
			myElement.focus()
			window.scrollBy(0,-80)
			return false
		}

		// mandatory but dependent on another field
		if(getPropByLang(myElement.id,'dpid') != ''){
			var myDependentElement = document.getElementById(getPropByLang(myElement.id,'dpid') )
			var myType = myDependentElement.type
			if(myType == 'radio' && !myElement.value){
				if(myDependentElement.checked){
					alert('Please fill in the ' + getPropByLang(myElement.id,'fn') + ' field before submitting')
					return false
				}
			}
		}
		///////////////
			
	} // end for
		return true
}

function setDd(ddId,ddValue){
	var myElement = document.getElementById(ddId)
	for(iSetDd = 0; iSetDd < myElement.options.length; iSetDd++){
		if (myElement.options[iSetDd].value == ddValue){
			myElement.selectedIndex = iSetDd
			return
		}
	}
}

function populateCheckboxes(boxName,values){
	myBoxes = document.getElementsByName(boxName)
	valueString = new String(values)
	valueArray = valueString.split(',')
	for(i=0; i<myBoxes.length; i++){
		for(j=0; j<valueArray.length; j++){
			if(valueArray[j] == myBoxes[i].value){
				myBoxes[i].checked = true
			}
		}
	}
}

function setRadio(radioName,value){
	radios = document.getElementsByName(radioName)
	for(i=0;i < radios.length; i++){
		if(radios.item(i).value == value){
			radios.item(i).checked = true
		}
	}
}

function concatBoxValues(boxname) {
	boxString = new String('')
	boxes = document.getElementsByName(boxname)
	for(x=0; x < boxes.length; x++){
		if(boxes.item(x).checked){
			boxString += boxes.item(x).value + ','
		}
	}
	if(boxString.length > 0){
		boxString = boxString.substring(0,boxString.length-1)
	}
	return boxString
}