function checkRequired(form, alertStr){
	var fillError = false;
	var elementCount = form.elements.length;
	var checkedRadios = new Array();
	
	for(i = 0; i < elementCount; i++){
		
		formField = form.elements[i];
		if(formField.className == "required"){
			// which type of field is it?
			if(formField.type == "checkbox" && formField.checked == false){
				//alertStr = "checkbox alert";
				fillError = true;
				
			} else if(formField.type == "radio"){
				// check radios by finding out their name and then checking the radio group by name
				groupName = formField.name;
				if(in_array(checkedRadios, groupName)){
					// the whole group is already checked
				} else {
					// add radio group's name to the checked
					currLen = checkedRadios.length;
					checkedRadios[currLen] = groupName;
					// check the whole group
					radioGroup = form.elements[groupName];
					radioLength = radioGroup.length;
					if(!radioLength){
						// no length = only one option for the radio field
						if(!radioGroup.value){
							//alertStr = "radio: one option, unfilled";
							fillError = true;
						}
					} else {
						// more than one option, so loop through them all
						radioChecked = false;
						for(b = 0; b < radioLength; b++){
							if(radioGroup[b].checked == true){
								radioChecked = true;
								break;
							}
						}
						if(radioChecked == false){
							//alertStr = "radio alert";
							fillError = true;
						}
					}
				}
				
				// save values into a temporary array, so you know which groups belong together
				//if(radiosToCheck[groupName] != ""){
					/*groupCurrentLen = radiosToCheck[groupName].length;
					alert(groupCurrentLen);
					radiosToCheck[groupName][groupCurrentLen] = formField.value;*/
				/*} else {
					alert("uusi");
					//radiosToCheck[groupName][0] = formField.value;
				}*/
				
				/*radioLength = formField.length;
				if(!radioLength){
					alert(formField.value);
					// no length = only one option for the radio field
					if(!formField.value){
						alertStr = "radio: one option, unfilled";
						fillError = true;
					}
				} else {
					radioChecked = false;
					for(b = 0; b < formField.length; b++){
						alert("option ".b);
						if(formField[b].checked == true){
							radioChecked = true;
							break;
						}
					}
					if(radioChecked == false){
						alertStr = "radio alert";
						fillError = true;
					}
				}*/
				
			} else if(formField.type == "select" || formField.type == "select-one" || formField.type == "select-multiple"){
				selIndex = formField.selectedIndex;
				if(!formField.options[selIndex].value){
					//alertStr = "select alert";
					fillError = true;
				} else if (formField.options[selIndex].value == "-empty-"){
					// since option.text overwrites and empty option.value, you have to set a placeholder to know when an option is intended to be really empty...
					// this is the default "empty" value set by the CMS
					fillError = true;
				}
				
			} else if(!formField.value){
				// all other types are checked the same way
				//alertStr = "value alert";
				fillError = true;
			}
		}
		
		// stop looping if a required and unfilled field is already found
		if(fillError == true){
			break;
		}
		
	}
	
	// Alert, if a required field is not filled
	if(fillError == true){
		if(alertStr){
			alert(alertStr);
		} else {
			alert('Please fill all the required fields.');
		}
		return false;
	}
	
	return true;
}


function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict; 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
			}
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
				return true;
            }
        }
    }
    return false;
}
