

function ShowItem(itemID) {
    var x = document.getElementById(itemID);
    if (x)
        x.style.visibility = "visible";
    return true;
}

function HideItem(itemID) {

    var x = document.getElementById(itemID);
    if (x)
        x.style.visibility = "hidden";
    return true;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function ValidateEmail(address) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(address) == false) {
        return false;
    }
    else {
        return true;
    }
}


var _errorPanelShowing = 0;
var _panelFieldOriginalURl = "";
var _panelFieldOriginalText = "";

function QuickQuoteOnFocus(iD) {

    var _fieldName;
    var _prefixValue;
    var _requiredNoCharacters;
    var _messageToShow;

    /* get the id of the name from the array */
    for (i = 0; i < _noArrayItems; i++) {
        if (QuickQuoteArray[i][0] == iD) {
            /* located, get the columns */
            _fieldName = QuickQuoteArray[i][0];
            _prefixValue = QuickQuoteArray[i][1];
            _requiredNoCharacters = QuickQuoteArray[i][2];
            _messageToShow = QuickQuoteArray[i][3];
            break;
        }
    }

    // get the value of the field
    var _fieldValue = document.getElementById(_fieldName);
    // check 1. has the user clicked on the field with only the default prefix text ? if so, remove the prefix
    if (_fieldValue.value == _prefixValue)
    {
        _fieldValue.value = "";
    }

    // if the field has previously been flagged by an error on the submit and its showing red, then reset it to black;
//    if (_errorPanelShowing == 1) {

//        _panelFieldOriginalURl = document.getElementById("QuickQuoteValidatePanel").style.backgroundImage;
//        _panelFieldOriginalText = _panelField.innerHTML;
//        _panelFieldOriginalColor = _panelField.style.color;

//        var _panelField = document.getElementById("QuickQuoteValidatePanel");
//        _panelField.style.backgroundImage = _panelFieldOriginalURl;
//        _panelField.style.color = _panelFieldOriginalColor;
//        // _panelField.style.font = "0.9em";
//        _panelField.innerHTML = _panelFieldOriginalText;



//    }
    _fieldValue.style.color = "#636363";

}

function QuickQuoteOnBlur(iD) {

    var _fieldName;
    var _prefixValue;
    var _requiredNoCharacters;
    var _messageToShow;

    /* get the id of the name from the array */
    for (i = 0; i < _noArrayItems; i++) {
        if (QuickQuoteArray[i][0] == iD) {
            /* located, get the columns */
            _fieldName = QuickQuoteArray[i][0];
            _prefixValue = QuickQuoteArray[i][1];
            _requiredNoCharacters = QuickQuoteArray[i][2];
            _messageToShow = QuickQuoteArray[i][3];
            break;
        }
    }

    // get the value of the field
    var _fieldValue = document.getElementById(_fieldName);
    var _trimmedValue = new String(_fieldValue.value);
    _trimmedValue = _trimmedValue.trim();
    // check 1. has the user clicked away from the field with no data entered, or spaces, show the default
    if (_trimmedValue.length == 0) {
        _fieldValue.value = _prefixValue;
    }
    else {
        _fieldValue.value = _trimmedValue; // write the value back if the string has spaces in it
    }

}


function QuickQuoteOnChange(iD) {
}


function QuickQuoteValidate() {
    /* iterate through the array, determine if fields require attention before submission */
    var _fieldName;
    var _prefixValue;
    var _requiredNoCharacters;
    var _messageToShow;

    var _showWarning = 0;

    /* get the id of the name from the array */
    for (i = 0; i < _noArrayItems; i++) {
            _fieldName = QuickQuoteArray[i][0];
            _prefixValue = QuickQuoteArray[i][1];
            _requiredNoCharacters = QuickQuoteArray[i][2];
            _messageToShow = QuickQuoteArray[i][3];

            var _valueOfField = document.getElementById(_fieldName).value;
            // determine if the default value is still active, if so set the local value to empty
            if (_valueOfField == _prefixValue) {
                _valueOfField = "";
            }
             
            // if the array requires any mandatory data, check it
            if (_requiredNoCharacters == "VALIDATEEMAIL" && !ValidateEmail(_valueOfField)) {
                _showWarning = 1;
                break;            
            }
            else if (_requiredNoCharacters != "0") {
                // check no of characters
                if (_valueOfField.length < _requiredNoCharacters) {
                    // issue warning
                    _showWarning = 1;
                    break;
                }
            }
    }

    if (_showWarning == 1) {
        // set the top level div "QuickQuoteValidatePanel" to no background image

        var _panelField = document.getElementById("QuickQuoteValidatePanel");

        // NO TIME TO GET THIS TO WORK
        //            if(_panelFieldOriginalURl.length == 0)
        //            { // is it empty, fill it with current, which should be the default..
        //                _panelFieldOriginalURl = _panelField.style.backgroundImage;
        //                _panelFieldOriginalText = _panelField.innerHTML;
        //                _panelFieldOriginalColor = _panelField.style.color;
        //            }


        _panelField.style.backgroundImage = "url()"; // provide empty white image
        _panelField.style.color = "red";
        // _panelField.style.font = "0.9em";
        _panelField.innerHTML = _messageToShow;

        // set the field color to red:
        var _badField = document.getElementById(_fieldName);
        _badField.style.color = "red";
        _errorPanelShowing = 1;

        // insert a anchor point

        // jump to part of panel to show error
        // cant use ("jprototype.js");  in code, due to it making problems with the bottom scroller 
        // $('QuickQuoteValidatePanel').scrollTo(); 
        // DAMN, cant get this work in time !!!!
        // window.location = window.location + "/#QuickQuoteValidatePanelEvent";
        var currentHref = window.location.href;
        location.href = currentHref.substr(0, currentHref.lastIndexOf("#")) + "#QuickQuoteValidatePanelEvent";
        // location.href = location.href + '#QuickQuoteValidatePanelEvent'
        return false;

    }
    else {
        return true;
    }

    return false;
}

