function showValidationDiv(obj,display) {
    if ( document.getElementById(obj) != null ){
        if (display == 'static')
            document.getElementById(obj).style.visibility='visible';
        else if (display == 'dynamic')	
            document.getElementById(obj).style.display='inline';
        else if (display == 'none')
            document.getElementById(obj).style.display='none';
    }            
}

function hideValidationDiv(obj,display) {
    if ( document.getElementById(obj) != null ){
        if (display == 'static')
            document.getElementById(obj).style.visibility='hidden';
        else if (display == 'dynamic')	
            document.getElementById(obj).style.display='none';
        else if (display == 'none')
            document.getElementById(obj).style.display='none';
    }
}

function highlightField(obj,errorStyleClass) {
    if (document.getElementById(obj) != null)
        document.getElementById(obj).className = errorStyleClass;
}

function resetFieldColor(obj,normalStyleClass) {
    if (document.getElementById(obj) != null)
        document.getElementById(obj).className = normalStyleClass;
}

function showValidationError(field, error, highlight, display, errorStyleClass) {
    showValidationDiv(error,display);
    if (highlight==true)
        highlightField(field,errorStyleClass);
}

function hideValidationError(field, error, highlight, display, normalStyleClass) {
    hideValidationDiv(error,display);
    if (highlight==true)
        resetFieldColor(field,normalStyleClass);
}

function showCompareValidationError(field1, field2,error, highlight, display) {
    showValidationDiv(error,display);
    if (highlight==true) {
        highlightField(field1);
        highlightField(field2);
    }
}

function hideCompareValidationError(field1, field2, error, highlight, display) {
    hideValidationDiv(error,display);
    if (highlight==true) {
        resetFieldColor(field1);
        resetFieldColor(field2);
    }
}

function validateRequiredField(field,error,highlight,display,normalStyleClass,errorStyleClass) {
        
    var elementType;
    var result = false;
    var processedRadios = new Array();
    if (document.getElementById(field) == null) {
        result = true;
    } else {
        // only validate a component that exists        
        var value=document.getElementById(field).value;
        if ((document.getElementById(field).type == "text") || (document.getElementById(field).type == "password") || (document.getElementById(field).type == "textarea")) {
            elementType = "textbox";
        } else {
            elementType = document.getElementById(field).type;
        }
        switch(elementType){
            case "textbox":
                if (value == null || (value.replace(/^\s\s*/, '').replace(/\s\s*$/, '')=='')) {
                    result = false;
                } else {
                    result = true;
                }
                break;
            case "checkbox":
                if (!document.getElementById(field).checked) {
                    result = false;
                } else {
                    result = true;
                }
                break
            case "radio":
                // need a special way of handling radio buttons
                var isRadioSelected = false;
                var result = true;
                if (!isInArray(processedRadios,document.getElementById(field).name)) {
                    for (j=0;j<document.forms[formName].elements[document.getElementById(field).name].length;j++){
                        if (document.forms[formName].elements[document.getElementById(field).name][j].checked==true) {
                            isRadioSelected = true;
                            break;
                        } 
                    }
                    if (!isRadioSelected) {
                        result = false;
                    }
                    processedRadios[processedRadios.length] = document.getElementById(field).name;
                }
                break
            case "select-one":
                var result = true;
                if (document.getElementById(field).selectedIndex == -1) {
                    result = false;
                } else {
                    if (document.getElementById(field).options[document.getElementById(field).selectedIndex].value == null || (document.getElementById(field).options[document.getElementById(field).selectedIndex].value == "") || isblank(document.getElementById(field).options[document.getElementById(field).selectedIndex].value)) {
                        result = false;
                    }
                }
                break
            default:
                    // no match
        }
        
        if (result == true) {
            hideValidationError(field, error, highlight, display,normalStyleClass);
        } else {
            showValidationError(field, error, highlight, display,errorStyleClass);
        }
    }    
    return result;
}

function validateRangeField(field,error,minValue,maxValue,highlight,display) {
    var valueText = document.getElementById(field).value;
    if (valueText == null || valueText == '') {
        showValidationError(field, error, highlight, display);
        return false;
    }
    var value = parseInt(valueText);
    if (minValue != null && maxValue !=null ) {
        if (value > maxValue || value < minValue ) {
            showValidationError(field, error, highlight, display);
            return false;
        }
        else {
            hideValidationError(field, error, highlight, display);
            return true;
        }	
    } else if (minValue == null && maxValue !=null) {
        if (value > maxValue) {
            showValidationError(field, error, highlight, display);
            return false;
        }
        else {
            hideValidationError(field, error, highlight, display);
            return true;
        }
    } else if (minValue != null && maxValue ==null) {
        if (value <minValue) {
            showValidationError(field, error, highlight, display);
            return false;
        }
        else {
            hideValidationError(field, error, highlight, display);
            return true;
        }
    }
}

function validateCompareFields(field1,field2,error,operator,highlight,display) {
    if (operator == 'eq')
        return validateEquality(field1,field2,error,highlight,display);
    else if (operator == 'not')
        return validateNotEquals(field1,field2,error,highlight,display);
}

function validateEquality(field1,field2,error,highlight,display) {
    var value1=document.getElementById(field1).value;
    var value2=document.getElementById(field2).value;
    
    if (value1 == value2) {
        hideCompareValidationError(field1, field2, error, highlight, display);
        return true;
    }
    else {
        showCompareValidationError(field1, field2, error, highlight, display);
        return false;
    }
}

function validateNotEquals(field1,field2,error,highlight,display) {
    var value1=document.getElementById(field1).value;
    var value2=document.getElementById(field2).value;
    
    if (value1 != value2) {
        hideCompareValidationError(field1, field2, error, highlight, display);
        return true;
    }
    else {
        showCompareValidationError(field1, field2, error, highlight, display);
        return false;
    }
}

function validateRegExp(field,error,pattern,highlight,display) {
    var value=document.getElementById(field).value;
    if (value == null || value == '') {
        hideValidationError(field, error, highlight, display);
        return true;
    }
    
    if (value.match(pattern) == null) {
        showValidationError(field, error, highlight, display);
        return false;
    }
    else {
        hideValidationError(field, error, highlight, display);
        return true;
    }
}

function validateLengthExactly(field, error, exactValue, highlight, display) {
    var value = document.getElementById(field).value;
    if (value.length != exactValue) {
        showValidationError(field, error, highlight, display);
        return false;
    } else {
        hideValidationError(field, error, highlight, display);
        return true;
    }
}

function validateLengthInterval(field, error, min, max, highlight, display) {
    var value = document.getElementById(field).value;
    var len = value.length;
    if (min != null && max !=null ) {
        if (len > max || len < min ) {
            showValidationError(field, error, highlight, display);
            return false;
        }
        else {
            hideValidationError(field, error, highlight, display);
            return true;
        }	
    } else if (min == null && max !=null) {
        if (len > max) {
            showValidationError(field, error, highlight, display);
            return false;
        }
        else {
            hideValidationError(field, error, highlight, display);
            return true;
        }
    } else if (min != null && max ==null) {
        if (len < min) {
            showValidationError(field, error, highlight, display);
            return false;
        }
        else {
            hideValidationError(error,display);
            if (highlight==true)
                resetFieldColor(field);
            return true;
        }
    }
}

function keyPressNumber(decimal) {
    var kc = window.event.keyCode;
    var t = window.event.srcElement.value ;

    if (decimal) {
        if (t.indexOf(',') != -1) {
            if( (kc >= 48 && kc <= 57) == false) {
                window.event.keyCode = 0;
            }
        }
        else {
            if ((kc >= 48 && kc <= 57 || kc == 44) == false) {
                window.event.keyCode = 0;
            }
        }
    }
    else {
        if ((kc >= 48 && kc <= 57) == false) {
            window.event.keyCode = 0;
        }
    }
}

function addErrorToSummaryIfNecessary(isValid, error,vs) {

    if (isValid == true)
        return;
    
    var errorMsg  = document.getElementById(error).innerHTML;
    if (vs.popup == false) {
        if (document.getElementById(existingPanelId) != null) {
            // replace an existing panel
            if (!isSummmaryHeaderUpdated && document.getElementById('clientValidationSummaryHeaderContainer')!= null) {
                document.getElementById(existingPanelId).innerHTML = document.getElementById('clientValidationSummaryHeaderContainer').innerHTML;
                isSummmaryHeaderUpdated = true;
            }
            document.getElementById(existingPanelId).innerHTML += '<SPAN class="' + document.getElementById('clientValidationSummary').className + '">' + errorMsg + '</SPAN><br>';
        } else {
            if(document.getElementById('clientValidationSummary') != null) {
                document.getElementById('clientValidationSummary').innerHTML +=  errorMsg + '<br>';
            }
        }
        
        if (isSummmaryHeaderUpdated && document.getElementById('clientValidationSummaryHeaderContainer')!= null) {
            showSummaryHeader(false);
            showSummary(false);
        } else {
            showSummaryHeader(true);
            showSummary(true);
        }
    } else {
        vs.summary += '* ' + errorMsg + '\n';
    }
}

function addErrorMessageToSummaryIfNecessary(isValid, errorMessage,vs) {
    if (isValid == true)
        return;
    
    var errorMsg  = errorMessage;
    if (vs.popup == false) {
        if (document.getElementById(existingPanelId) != null) {
            // replace an existing panel
            if (!isSummmaryHeaderUpdated && document.getElementById('clientValidationSummaryHeaderContainer')!= null) {
                document.getElementById(existingPanelId).innerHTML = document.getElementById('clientValidationSummaryHeaderContainer').innerHTML;
                isSummmaryHeaderUpdated = true;
            }
            document.getElementById(existingPanelId).innerHTML += '<SPAN class="' + document.getElementById('clientValidationSummary').className + '">' + errorMsg + '</SPAN><br>';
        } else {
            if (document.getElementById('clientValidationSummary') != null) {
                document.getElementById('clientValidationSummary').innerHTML +=  errorMsg + '<br>';
            }
        }
        // show header
        showSummaryHeader(true);
        //show summary
        showSummary(true);
    } else {
        vs.summary += '* ' + errorMsg + '\n';
    }
}

function clearValidationSummary(vs) {
    if (vs.popup == false) {
        //hide summary
        showSummary(false);
        if (document.getElementById('clientValidationSummary') != null) {
            document.getElementById('clientValidationSummary').innerHTML="";
        }
        // hide header
        showSummaryHeader(false);
        if (document.getElementById(existingPanelId) != null) {
            document.getElementById(existingPanelId).innerHTML = "";
            isSummmaryHeaderUpdated = false;
        }
    } else {
        vs.summary = '';
    }
}

function showPopupIfNecessary(vs) {
    if (vs.popup == true && vs.summary != '')
        window.alert(vs.summary);
}

function showSummaryHeader(isShow) {
    // show or hide summary header
    if (document.getElementById('clientValidationSummaryHeaderContainer') != null) {
        if (isShow) {
            document.getElementById('clientValidationSummaryHeaderContainer').style.visibility = "visible";
            document.getElementById('clientValidationSummaryHeaderContainer').style.display = "block";
        } else {
            document.getElementById('clientValidationSummaryHeaderContainer').style.visibility = "hidden";
            document.getElementById('clientValidationSummaryHeaderContainer').style.display = "none";
        }
    }
}

function showSummary(isShow) {
    // show or hide summary 
    if (document.getElementById('clientValidationSummaryContainer') != null) {
        if (isShow) {
            document.getElementById('clientValidationSummaryContainer').style.visibility = "visible";
            document.getElementById('clientValidationSummaryContainer').style.display = "block";
        } else {
            document.getElementById('clientValidationSummaryContainer').style.visibility = "hidden";
            document.getElementById('clientValidationSummaryContainer').style.display = "none";
        }
    }
}

function isInArray(arrayObject,searchObj) 
{
    var result = false;
    for(var i = 0; i < arrayObject.length; i++) {
        if (arrayObject[i] == searchObj) {
            result = true;
            break;
        }
    }
    return result;
}

function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}