/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
// 
// MANDATORY FIELDS CHECK JAVASCRIPT LIBRARY 
// created by Jarkko Tuomala on 7.2.2003 
// modified by Jarkko Tuomala on 26.6.2003 
//  - added Mandatory Field Class to replace old arguments system 
// modified by Jarkko Tuomala 26.10.2004 
//  - changed to change color of label instead of table background 
// modified by Jarkko Tuomala 5.11.2004 
//  - changed mandatory alternative check only one field from onblur event of field 
// 
// COMPONENTS NEEDED 
//         1) This script library 
//         2) MandatoryFields subform (inserted to end of form where you want to check mandatory fields) 
//        3) setMandatoryFields function on form for setting mandatory fields 
//        4) editable, allow multiple values, readonly ErrorMessage field with errorfield id on form 
//       to show error messages to user (optional) 
//        5) some CSS-styles for nice look out (#errorfield, .showerror and .hideerror) 
//    6) mode field on form to check if document is in edit/read mode 
//        7) jscriptLibrary.js with some common function used by this script library 
// 
// BROWSER SUPPORT 
// IE4 and higher (tested with IE4 and IE6) 
// NN6 and higher (tested with NN4 and NN6) 
// 
// HOW TO USE 
// Call MandatoryFields() function on document save. 
// 
// Function returns true if all fields are filled correctly. If some field is not filled correctly 
// it will be paint with error color. The quote is shown to user that fill wrongly filled fields. 
// 
// Mandatory fields must bet set by creating setMandatoryFields() function to form where mandatory 
// fields should be checked. See example later on this page. 
// 
// Add this formula to HTML Head Content on form or $$HTMLHead field to take use this script library: 
// db := "/"+@ReplaceSubstring(@Subset(@DbName;-1);" ":"\\";"+":"/"); 
// "[<script src=\""+db+"/mandatorycheck.js?OpenPage\" type=\"text/javascript\"></script>]" + @NewLine 
// 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

////////////////////////////////////////////////////////////////////// 
// Browser check 
////////////////////////////////////////////////////////////////////// 
var nn4 = window.Event ? true : false; 
var ie4 = document.all ? true : false; 
var dom1 = document.getElementById ? true : false; 

////////////////////////////////////////////////////////////////////// 
// Set Colors 
////////////////////////////////////////////////////////////////////// 
var errorColor = "#FF0000";   // Label Error Color 
var normalColor = "#000";     // Label Normal Color 
var fieldErrorColor = null; // Field Error Color 
var fieldColor = "#FFF";         // Field Normal Color 

////////////////////////////////////////////////////////////////////// 
// Mandatory Field Class - created by Jarkko Tuomala on 26.6.2003 
// 
// This is javascript 1.1 compatible way to introduce new class 
// in javascript 1.2 you do not need to do this but you can instead 
// of this compose a new array of objects like this: 
//         var aMandatoryFields = 
//          [ 
//                        {name: 'kentan nimi1', text: 'kuvaus1'}, 
//                        {name: 'kentan nimi2', text: 'kuvaus2'}, 
//                        {name: 'kentan nimi3', text: 'kuvaus3'}, 
//                ]; 
////////////////////////////////////////////////////////////////////// 
/************* 
* Constructor 
**************/ 
function MandatoryField(name, text) { 
        if (arguments.length > 0) { 
                this.init(name, text); 
        } 
} 
/************* 
* Init 
**************/ 
MandatoryField.prototype.init = function (name, text) { 
        this.name = name; 
        this.text = text; 
} 

/************* 
* FUNCTION setMandatoryFields 
* Overwrite this function on every form to set mandatory fields. 
**************/ 
function setMandatoryFields() { 
        var aMandatoryFields = new Array(0); 
        /*** 
        * example how to set field as mandatory 
        * aMandatoryFields.push(new MandatoryField("kentan nimi","viesti")); 
        ***/ 
        return aMandatoryFields; 
} 

////////////////////////////////////////////////////////////////////// 
// activeX(e) function created by Jarkko Tuomala on 15.7.2002 
// called by fields onblur event 
// changes style properties of field and labels 
// like color and background color 
// parameters: 
// -evt: Event (used by nn4) 
////////////////////////////////////////////////////////////////////// 
function activeX(evt) { 
        evt = (evt) ? evt : ((window.event) ? window.event : null); 
        if (evt) { 
                var field = getTargetElement(evt); 
                if (field) { 
                        if (MandatoryFields(field)) { 
                                changeColors(field, normalColor, null); 
                        } 
                } 
        } 
} 

////////////////////////////////////////////////////////////////////// 
// changeColors(field, labelcolor, fieldbgcolor) function created by Jarkko Tuomala on 26.10.2004 
// change the colors of error field and labels 
// parameters: 
// - field: the field to modify 
// - labelcolor: a new field label color 
// - fieldbgcolor: a new field background color (can be set as null) 
////////////////////////////////////////////////////////////////////// 
function changeColors(field, labelcolor, fieldbgcolor) { 
        var oRow = getParent(field,'div',[{attribute:'class',value:'row'}]); 
        if (oRow) { 
                var oLabel = getChildren(oRow,"label",[{attribute:'class',value:'mandatory'}]); 
        } 
        if (oLabel) { 
                var labelElem = getChildren(oLabel,"a"); 
                if (!labelElem) { 
                        labelElem = oLabel; 
                } 
        } 
        if (fieldbgcolor) { 
                // change field background color 
                field.style.backgroundColor = fieldbgcolor; 
        } 
        if (labelElem) { 
                // change label color 
                labelElem.style.color = labelcolor; 
        } 
        return true; 
} 

////////////////////////////////////////////////////////////////////// 
// MandatoryFields() function created by Jarkko Tuomala on 7.2.2003 
// checks mandatory fields 
// parameters: 
// - fields: the array of field names to check 
// - fieldNames: the array of descriptive names for checked fields 
////////////////////////////////////////////////////////////////////// 
function MandatoryFields() { 
        var bOneFieldCheck = false; 
        var aFields = new Array(0); 
        var fieldFocus = ""; 
        var AerrorFields = new Array(0); 
		var form = document.forms[0];
        if (arguments.length > 0) { 
            aFields.push(new MandatoryField(arguments[0].name,"error")); 
            bOneFieldCheck = true; 
        } else { 
			aFields = setMandatoryFields();
        }

        if (!bOneFieldCheck) { 
                var errorfield = getElement("errorfield"); 
                if (errorfield) { 
                        if (dom1) { 
                                errorfield.parentNode.className = "hideerror"; 
                        } else if (ie4) { 
                                errorfield.parentElement.className = "hideerror"; 
                        } 
                } 
        } 
		
        for (var i = 0; i < aFields.length; i++) { 
                var oField = form[aFields[i].name]; 
                var strType = oField.type; 
                
                // Check TEXT field 
                if (strType == "hidden" || strType == "text" || strType == "textarea") { 
                        if (oField.value == "") { 
                                AerrorFields[AerrorFields.length] = aFields[i].text; 
                                if (!bOneFieldCheck) { 
                                        var visiblefield = aFields[i].name; 
                                        if (form[visiblefield].type != "hidden") { 
                                                // change field color to error color 
                                                changeColors(form[visiblefield], errorColor, fieldErrorColor); 
                                                form[visiblefield].tabIndex = AerrorFields.length; 
                                                fieldFocus = (fieldFocus == "") ? form[visiblefield] : fieldFocus; 
                                        } 
                                } 
                        } 
                // Check DIALOG LIST field 
                } else if (strType == "select-one" || strType == "select-multiple") { 
                           var selectList = false; 
                         for (var j=0;j<oField.length;j++) { // check if any list value has chosen 
                                 if (oField.options[j].selected && oField.options[j].text != "" && escape(oField.options[j].text) != "%0A" && oField.options[j].text.indexOf("No Products found for this") == -1) { 
                                   var selectList = true; 
                                   break; 
                                 } 
                         } 
                  
                         if (!selectList) { // if none list value has chosen add this field to error list 
                                AerrorFields[AerrorFields.length] = aFields[i].text; 
                                if (!bOneFieldCheck) { 
                                        // change field color to error color 
                                        changeColors(oField, errorColor, fieldErrorColor); 
                                        oField.tabIndex = AerrorFields.length; 
                                        fieldFocus = (fieldFocus == "") ? oField : fieldFocus; 
                                } 
                         } 
                // Check RADIO BUTTON or CHECKBOX field 
                } else { 
                         var fieldOK = false; 
                         for (var j=0;j<oField.length;j++) { 
                                 if (oField[j].checked) { // check if any box is marked 
                                          fieldOK = true; 
                                         break; 
                                } 
                         } 

                         if (!fieldOK) { // if none box is marked add this field to error list 
                                AerrorFields[AerrorFields.length] = aFields[i].text; 
                                if (!bOneFieldCheck) { 
                                         changeColors(oField[0], errorColor, fieldErrorColor); 
                                        oField[0].tabIndex = AerrorFields.length; 
                                        fieldFocus = (fieldFocus == "") ? oField[0] : fieldFocus; 
                                } 
                         } 
                } 
        } 

        var tmp = AerrorFields.join(', '); 
        if (tmp != "") { 
                if (!bOneFieldCheck) { 
                        if (errorfield) { 
                                if (dom1) { 
                                        errorfield.parentNode.className = "showerror"; 
                                } else if (ie4) { 
                                        errorfield.parentElement.className = "showerror"; 
                                } 
                                errorfield.innerHTML = "Seuraavista kentistä puuttuu arvo: " + tmp + "."; 
                                if (document.getElementById('errorfieldlabel')) { 
                                        document.getElementById('errorfieldlabel').innerHTML = "Pakollisia tietoja puuttuu"; 
                                } 
                        } else { 
                                alert("Pakollisia tietoja puuttuu. Täytä seuraavat kentät:\n"+tmp); 
                        } 
                        if (fieldFocus != "") { 
                                if (fieldFocus.focus) { 
                                        fieldFocus.focus(); 
                                } 
                        } 
                } 
                return false; 
        } else { 
                return true; 
        } 
} 

