﻿function activatefocus(elementname) {
    return;
}

var optionalfields; // this is an array used to declare controls as optional, from server side.

function showinfobox(elementname) {
    //alert('focus' + elementname);
    var controlName = (elementname.split("$")[3]).substring(3);
    //alert('focus' + controlName);
    var divs = document.getElementsByTagName('div');
    var isErrored = false;
    for (i = divs.length - 1; i >= 0; i--) { // work backwards, to get ErrorInfo first

        var divName = divs[i].getAttribute('name');

        if (divName == controlName + '_FocusInfo' && !isErrored) {
            divs[i].style.visibility = 'visible';
        }
        else if (divName == controlName + '_ErrorInfo' && divs[i].style.visibility == 'visible') {
            divs[i].style.visibility = 'hidden';
        }
    }
}


function hideinfobox(elementname) {
    //alert('focus' + elementname);
    var controlName = (elementname.split("$")[3]).substring(3);
    //alert('focus' + controlName);
    var divs = document.getElementsByTagName('div');
    var isErrored = false;
    var isFirstError = true;
    for (i = 0; i < divs.length; i++) {

        var divName = divs[i].getAttribute('name');

        if (divName != null) {
            if (divName == controlName + '_FocusInfo') {
                //alert(divName + '_FocusInfo ' );
                divs[i].style.visibility = 'hidden';
            }
            else if (divName == controlName + '_ErrorInfo' && isErrored == true && isFirstError == true) {

                divs[i].style.visibility = 'visible';
                isErrored = false;
                //alert('showError ' + isErrored);
            }
            else if (divName.match('_ErrorInfo$') && divs[i].style.visibility == 'visible') {
                isFirstError = false;
            }
            else if (divName == (controlName + '_OkErrorIconInfo') && divs[i].style.visibility == 'visible' && divs[i].className == 'validationerror') { // hide all other infos
                isErrored = true;
                //alert('isErrored ' + isErrored);
            }
        }
    }
}

function activateerror(elementname, message) {
    //alert('error');
    var controlName = (elementname.split("_")[3]).substring(3);
    var divs = document.getElementsByTagName('div');

    for (i = 0; i < divs.length; i++) {

        var divName = divs[i].getAttribute('name');
        if (divName != null) {
            //alert('error ' + divName);
            if (divName == controlName + '_OkErrorIconInfo') {
                divs[i].style.visibility = 'visible';
                divs[i].className = 'validationerror';
            }
            else if (divName == controlName + '_FocusInfo') {
                divs[i].style.visibility = 'hidden';
            }
            else if (divName == controlName + '_ErrorInfo') {
                divs[i].style.visibility = 'visible';
            }
            else if (divName == controlName + '_ErrorBody') {
                divs[i].innerHTML = message;
            }
        }
    }
    //checkprevious(elementname, true);
}

function activatevalid(elementname) {

    if (elementname == null)
        return;

    var controlName = elementname.split("_")[3].substring(3);
    var divs = document.getElementsByTagName('div');

    var dateErrorCount = 0;
    var dateDoValidate = false;
    var params = new Array();
    for (i = 0; i < divs.length; i++) {

        var divName = divs[i].getAttribute('name');
        if (divName != null) {
            //var validator = document.getElementById(divs[i].childNodes[0].id);
            //alert(validator);
            if (divName == controlName + '_ErrorInfo') {
                divs[i].style.visibility = 'hidden';
            }
            else if (divName == controlName + '_OkErrorIconInfo') {
                divs[i].style.visibility = 'visible';
                divs[i].className = 'validationok';

                /*if (controlName == "PropertyValue" | controlName == "SelfCertify") {
                //alert(elementname);
                //var o = document.getElementsByName(elementname);
                //alert(o.value);
                //alert(divs[i].childNodes);
                //var a = document.getElementById(divs[i].childNodes[0].id);
                //alert(a);
                //params.push(controlName, con
                var req = new XMLHttpRequest();
                req.open("GET", window.location + "?GetMortBrokerDet=true", true);
                req.send(null);
                }*/
            }
        }
    }
}

function SetDivStyle(ctl, OldStyle, NewStyle) {
    // Sets style of div in which control ctl is contained
    if (ctl == null) {
        return;
    }
    if (ctl.nodeName == "DIV" && (ctl.className == OldStyle || ctl.className == NewStyle)) {
        ctl.className = NewStyle;
        return;
    }
    SetDivStyle(ctl.parentNode, OldStyle, NewStyle);
}

function SetDivBackColour(ctl, Col) {
    // Sets background colour of div in which control is contained
    if (ctl.nodeName == "DIV" && ctl.className == "form2") {
        ctl.style.backgroundColor = Col;
        return;
    }
    SetDivBackColour(ctl.parentNode, Col);
}

function CheckCurrent(ElementName) {
    var el = document.getElementById(ElementName);
    // Do not do validation for TextBox if field value is empty string or null
    if (el.type == "text" && (el.value == null || el.value == "")) {
        return;
    }

    if (el.type == "select-one") {
        ElementName = ElementName.replace("ddl", "");
    }
    if (el.type == "radio") {
        ElementName = ElementName.replace("Left", "");
        ElementName = ElementName.replace("Right", "");
        ElementName = ElementName.replace("radio", "Radio");
    }

    var validator = document.getElementById(ElementName + "Validator");
    if (el.type == "radio") {
        var radioId = validator.id.replace("RadioValidator", "");
        var radioLeft = document.getElementById(radioId + "radioLeft");
        var radioRight = document.getElementById(radioId + "radioRight");
        if (radioLeft.checked || radioRight.checked) {
            activatevalid(radioId);
        }
        else {
            activateerror(radioId, "");
        }
        return;
    }

    ValidatorValidate(validator);
    if (validator.isvalid) {
        // Control value is valid, so enable all other controls
        //ChangeControlsState(el.id, false);
        SetDivStyle(el, "form2error", "form2")
    }
    else {
        // Control value is invalid, so disable all other controls
        //ChangeControlsState(el.id, true);
        //el.focus();
        /* Set JavaScript events dynamically, such that as soon as this field is
        now correct the field will validate correctly and other fields will be enabled.
        Also corrects a Firefox issue with onblur firing */
        if (el.type == "text") {
            el.onkeyup = function() { CheckCurrent(this.id); };
        }
        else if (el.type == "select-one") {
            el.onchange = function() { CheckCurrent(this.id); };
        }
        SetDivStyle(el, "form2", "form2error");
    }
}

function ChangeControlsState(ElementName, Disable) {    
    /* For all text boxes, dropdown lists and radio buttons (except ElementName), 
    set disabled property to be value of Disable parameter */   
    var elem = document.getElementById("aspnetForm").elements;
    for (var i = 0; i < elem.length; i++) {
        if ((elem[i].type == "text" || elem[i].type == "select-one" || elem[i].type == "radio") && elem[i].id != ElementName) {
            elem[i].disabled = Disable;
        }
    }
}

function checkprevious(elementname, skipValidation) {
    //alert('checkprevious ' + elementname + ' ' + skipValidation);
    var controlName = (elementname.split("_")[3]).substring(3);
    //alert('Current control name ' + controlName);
    var divs = document.getElementsByTagName('div');

    var index = 1;
    if (navigator.appName == 'Microsoft Internet Explorer')
        index = 0; // ie looks at a different index to other browsers

    //alert('divs.length ' + divs.length);
    var hasReachedFocusedCtl = false;
    var firstError;
    //var isNextDDDLValid = false;
    for (divIndex = 0; divIndex < divs.length; divIndex++) {
        var divName = divs[divIndex].getAttribute('name');

        if (divName != null) {
            //alert('divName '+divName);
            if (checkparentvisible(divs[divIndex], 1) == false) {
                continue; // if any of the divs parents are hidden, dont error/ok boxes
            }
            if (divName.match('^validatorDiv')) {
                if (hasReachedFocusedCtl == false) {
                    var validator = document.getElementById(divs[divIndex].childNodes[index].id);
                    /*alert(elementname);
                    alert(divs[divIndex].childNodes[index].id);
                    alert(validator.id);*/
                    var validatorName = (validator.id.split("_")[3]).substring(3);

                    if (validatorName.match(controlName)) { // only check fields above this one
                        hasReachedFocusedCtl = true;
                    }
                    else if (divName == 'validatorDivRadio') {
                        //alert('validator.id ' + validator.id);
                    if (validator.id.match('IsMobilePhone_$') || validator.id.match('CreditGradeCheck_$'))
                            continue;

                        if (skipValidation != true) {
                            var radioId = validator.id.replace('RadioValidator', '');
                            //alert('radioId ' + radioId);
                            var radioLeft = document.getElementById(radioId + 'radioLeft');
                            var radioRight = document.getElementById(radioId + 'radioRight');
                            //alert('radioLeft.checked ' + radioLeft.checked);
                            if (radioLeft.checked || radioRight.checked)
                                activatevalid(radioId);
                            else
                                activateerror(radioId, 'This field is required');
                        }
                    }
                    else if (skipValidation != true)
                    //                        ValidatorValidate(validator);

                        try {
                        ValidatorValidate(validator);
                    }
                    catch (err) {
                       // alert("Could not fire ValidatorValidate for control : " + controlName + ". " + err.description);
                    }
                }
            }
            else if (divName.match('_OkErrorIconInfo$')) {

                if (firstError != false && divs[divIndex].style.visibility == 'visible' && divs[divIndex].className == 'validationerror' && divName != 'IsMobilePhone_OkErrorIconInfo$')
                    firstError = true;

            }
            else if (divName.match('_ErrorInfo$')) {
                if (firstError == true) {
                    divs[divIndex].style.visibility = 'visible';
                    firstError = false;
                    //if (hasReachedFocusedCtl)
                    //    return;
                }
                else {
                    divs[divIndex].style.visibility = 'hidden';
                }
            }

        }
    }
}

function checkisrequired(divName, suffix) {
    var isrequired = true;
    if (divName.match('WorkMobile' + suffix) || divName.match('IsMobilePhone' + suffix)) {
        isrequired = false; // always optional fields
    }
    else if (optionalfields != null) {
        for (var j = optionalfields.length - 1; j >= 0; --j) {
            if (divName.match(optionalfields[j] + suffix)) {
                isrequired = false; //this field is dynamically declared optional
            }
        }
    }
    return isrequired;
}

function checkparentvisible(node, index) {
    if (index > 7) // only so up 5 levels (may need to increase sometime)
        return true;
    else if (node.style.display == 'none')
        return false;
    else
        return checkparentvisible(node.parentNode, index + 1); //  recursively call this method
}

