﻿var iHelpElOn = null;
//var iHelpTop=null;
var iHelpSide = null;
var nError;
var strError;
var nSpecialMan;
var bClearDateIfNotValid = false;
var vMonthDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var vMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
var vErrorHighlight = new Array(50);
var nErrorHighlight = 0;
var nRecommended = 0;
var strRecommended = "";
var ignoreRecommended = false;


function viewProfile() {
    alert("TEST");
}

function logOff() {
    var logOutButton = document.getElementById(prefix + "btnLogOff");
    logOutButton.click();
}


function highlight(field) {
    field.focus();
    field.select();
}

function removeSpaces(string) {
 return string.split(' ').join('');
}


function populateHiddenPostcode() {
    
    var hdnPostcode = document.getElementById("contentMaster_masterPage_mainContent_contentMainContent_hdnPostcode");
    var txtPostcode = document.getElementById("txtPostcode");
    var newValue = removeSpaces(txtPostcode.value);

    if ((newValue == "enterpostcode") || (newValue == "")) {
        alert("Please enter a valid postcode");
        return false;
    }
    if (newValue.length < 3) {
        alert("Please enter a valid postcode");
        return false;
    }

    if ((newValue.length == 3) || (newValue.length == 4)) {
        hdnPostcode.value = newValue;
        return true;
    }
    if (newValue.length == 6) {
        //We take the first 3 characters from the string
        hdnPostcode.value = newValue.substring(0, 3)
        return true;
    }
    if (newValue.length == 7) {
        //We take the first 4 characters from the string
        hdnPostcode.value = newValue.substring(0, 4)
        return true;
    }
    if (newValue.length > 7) {
        //We take the first 4 characters from the string
        hdnPostcode.value = newValue.substring(0, 4)
        return true;
    }
}

// Check if this is the first ime in this box
// if so clear it.
function checkFirstTime(textbox)
{
    if ("1" == GetDomValue(textbox, "icFirstTime"))
    {
        textbox.value = "";
        SetDomValue(textbox, "icFirstTime", "0");
    }
}

function GetDomValue(element, name)
{
    return element.getAttribute(name);
}

function SetDomValue(element, name, value)
{
    return element.setAttribute(name, value);
}

function cancelEnterKey() {
    if (13 == window.event.keyCode) {
        window.event.keyCode = 0;
    }
}

function doMouseOver(el, nHelpType) {
    if (null == nHelpType) {
        nHelpType = 1
    }

    iHelpSide = document.getElementById("icInteractiveHelpSide");

    if (iHelpSide == null) {
        return;
    }

    //need this for below
    iHelpSide.innerHTML = "&nbsp;"
    if (null != iHelpElOn && iHelpElOn.ICERRORHI != 1) {
        iHelpElOn.style.background = 'White';
    }

    if (null == el) {

        var iEl = window.event.srcElement;
    }
    else {
        var iEl = el;
    }

    if (null == iEl) { return; }
    if (iEl.disabled) { return; }


    // Help is based on icHelpId or icHelpText being in place
    if (1 == nHelpType) {
        if ((null == iEl.getAttribute("icHelpId")) || (-1 == iEl.getAttribute("icHelpId"))) {

            return;
        }
    }
    else {
        if ((null == iEl.getAttribute("icHelpText")) || ("" == iEl.getAttribute("icHelpText"))) {

            return;
        }
    }
    if (null == iEl.type || '' == iEl.type || iEl.type == 'radio') {
        iHelpElOn = null;
    }
    else {
        if ("1" != el.ICERRORHI) {
            iEl.style.background = '#CDD8F7';
            iHelpElOn = iEl;
        }

    }

    // Here we see if vHelp exists, if not we switch to vUserControlHelp

    var strHelp;
    if (1 == nHelpType) {
        var nHelpIndex = iEl.getAttribute("icHelpId");
        if ('undefined' == typeof vHelp) {
            strHelp = vUserControlHelp[nHelpIndex];
        }
        else {
            // Now we check if vHelp has enough elements - if not we use vUserControlHelp
            if (vHelp.length >= nHelpIndex) {
                strHelp = vHelp[nHelpIndex];
            }
            else {
                strHelp = vUserControlHelp[nHelpIndex];
            }
        }
    }
    else {
        strHelp = iEl.getAttribute("icHelpText");
    }

    iHelpSide.innerHTML = strHelp;
    //redraw

    event.cancelBubble = true;
}


function checkContactForm() {
    resetErrorForm();
    nError = 0;
    strError = "";
    strError += validateInputForm('aspnetForm');

    if (nError > 0) {
        icError(strError);
        return false;
    }
    return true;
}


function icError(stringToShow) {
    $.prompt("<span class='icBoldText'>Please fix the following before submitting again:<br /></span><span class='icErrorText'>"
         + stringToShow + "</span>", {
             buttons: { OK: 'Close' }, zIndex: 99999
         });
}


function resetErrorForm() {
    for (var i = 0; i < nErrorHighlight; i++) {
        vErrorHighlight[i].style.background = 'white';
        vErrorHighlight[i].ICERRORHI = 0;
    }
    nErrorHighlight = 0;
    strRecommended = "";
    nRecommended = 0;
    strError = "";
    nError = 0
}


function validateInputForm(strFormName) {
    if (null != iHelpElOn) {
        iHelpElOn.style.background = 'White';
    }

    strError = '';
    strRecommended = ''
    var objForm = document.getElementById(strFormName);
    var iCol = objForm.getElementsByTagName("input");

    for (i = 0; i < iCol.length; i++) {
        var oElement = iCol[i];

        if ("text" == oElement.type.toLowerCase()) {

            var strName = oElement.getAttribute("icMandatory");

            if (null != strName) {
                strError = strError + checkMandatoryField(oElement.getAttribute("id"), strName);
            }
            else {
                if (!ignoreRecommended) {
                    //is recommended?
                    strName = oElement.getAttribute("icRecommended");

                    if (null != strName) {
                        strRecommended = strRecommended + checkRecommendedField(oElement.getAttribute("id"), strName);
                    }
                }
            }
        }
    }

    var iCol = objForm.getElementsByTagName("textarea");

    for (i = 0; i < iCol.length; i++) {
        var oElement = iCol[i];
        var strName = oElement.getAttribute("icMandatory");
        if (null != strName) {
            strError = strError + checkMandatoryField(oElement.getAttribute("id"), strName);
        }
    }
    iCol = objForm.getElementsByTagName("select");
    for (i = 0; i < iCol.length; i++) {
        var oElement = iCol[i];

        var strName = oElement.getAttribute("icMandatory");
        if (null != strName) {
            strError = strError + checkMandatoryList(oElement.getAttribute("id"), strName, oElement.icNull);
        }
    }
    return strError;
}

function highlightErrorField(iField) {

    iField.style.background = 'yellow';
    iField.ICERRORHI = 1;
    vErrorHighlight[nErrorHighlight++] = iField;

}

function highlightRecommendedField(iField) {
    iField.style.background = 'orange';
    iField.ICERRORHI = 1;
    vErrorHighlight[nErrorHighlight++] = iField;
}

function checkOtherTextField(strField, strDisplay, rowForOther) {
    if (getFormElement(rowForOther) != null) {
        if (getFormElement(rowForOther).style.display != 'none') {
            var iField = getFormElement(strField);
            if (null != iField) {
                if ('' == iField.value) {
                    highlightErrorField(iField);
                    nError++;
                    return '<LI>A value must be specified for ' + strDisplay + '.';
                }

            }
        }
    }
    return '';
}

function checkMandatoryField(strField, strDisplay) {
    var iField = document.getElementById(strField);
    if (null != iField) {
        if ('' == iField.value || 'NON' == iField.value || 'NOT' == iField.value) {
            highlightErrorField(iField);
            nError++;
            return '<LI>A value must be specified for ' + strDisplay + '.';
        }

    }

    return '';
}

function checkRecommendedField(strField, strDisplay) {
    var iField = document.getElementById(strField);

    if (null != iField) {
        if ('0' == iField.value || '' == iField.value || 'NON' == iField.value || 'NOT' == iField.value) {
            highlightRecommendedField(iField);
            nRecommended++;
            return '<LI>A value is recommended for ' + strDisplay + '.';
        }

    }

    return '';
}

function checkMandatoryValue(strValue, strDisplay) {
    if ('' == strValue || 'NON' == strValue || 'NOT' == strValue) {
        nError++;
        return '<LI>A value must be specified for ' + strDisplay + '.';
    }

    return '';
}


function checkMandatoryNumericField(strField, strDisplay) {
    var iField = document.getElementById(strField);
    if (null != iField) {
        if ('' == iField.value) {
            highlightErrorField(iField);
            nError++;
            return '<LI>A value must be specified for ' + strDisplay + '.';
        }

        alert(isPositiveReal(iField.value));
        if (!isPositiveReal(iField.value)) {
            highlightErrorField(iField);
            nError++;
            return '<LI>' + strDisplay + ' should be a positive real number.';
        }
    }
    return '';
}


function checkMandatoryNumericFieldAllowNegative(strField, strDisplay) {
    var iField = document.getElementById(strField);
    if (null != iField) {
        if ('' == iField.value) {
            highlightErrorField(iField);
            nError++;
            return '<LI>A value must be specified for ' + strDisplay + '.';
        }
        if (!isReal(iField.value)) {
            highlightErrorField(iField);
            nError++;
            return '<LI>' + strDisplay + ' should be a real number (negatives allowed).';
        }
    }
    return '';
}

function checkMandatoryList(strField, strDisplay, strNullValue) {
    var iField = document.getElementById(strField);

    if (null != iField) {
        if (0 == iField.options.length) {
            highlightErrorField(iField);
            nError++;
            return '<LI>A value must be selected for ' + strDisplay + '.';
        }
        else {
            if (iField.value == strNullValue || iField.value == '' || 'NON' == iField.value || 'NOT' == iField.value) {
                highlightErrorField(iField);
                nError++;
                return '<LI>A value must be selected for ' + strDisplay + '.';
            }

        }

    }
    return '';
}

