/*************************************************************************
    This code is from Dynamic Web Coding at dyn-web.com
    Copyright 2007-10 by Sharon Paine 
    See Terms of Use at www.dyn-web.com/business/terms.php
    regarding conditions under which you may use this code.
    This notice must be retained in the code as is!
*************************************************************************/

// Validation object/props declared in cart_fns.js, dn_pay_val.js

function addValidationMessage(msg) {
    Validation.error_messages.push(msg);
}

function addValidationField(fld) {
    Validation.error_fields.push(fld);
}

function displayErrorInfo(form) {
    var len = Validation.error_messages.length;
    if ( len ) {
        var el, fld, msg;
        var errorEl = document.createElement("div");
        errorEl.id = 'errorDiv';
        form.parentNode.insertBefore(errorEl, form);
        el = document.createElement('h3');
        el.appendChild( document.createTextNode( Validation.msg_header ) );
        errorEl.appendChild(el);
        for (var i=0; i<len; i++) {
            msg = Validation.error_messages[i];
            el = document.createElement('p');
            el.appendChild( document.createTextNode(msg) );
            errorEl.appendChild(el);
        }
        len = Validation.error_fields.length;
        for (i=0; i<len; i++) {
            fld = Validation.error_fields[i];
            if ( !dw_Util.hasClass(fld, 'error') ) {
                dw_Util.addClass(fld, 'error');
            }
        }
    }
}

// to remove error field/message
function deleteErrorEntry(fld, msg) {
    var len, i;
    if (fld) {
        len = Validation.error_fields.length;
        for (i=0; i<len; i++) {
            if ( Validation.error_fields[i] == fld ) {
                dw_Util.removeClass(fld, 'error');
                Validation.error_fields.splice(i, 1);
            }
        }
    }
    if (msg) {
        len = Validation.error_messages.length;
        for (i=0; i<len; i++) {
            if ( Validation.error_messages[i] == msg ) {
                Validation.error_messages.splice(i, 1);
            }
        }
    }
}

// remove all error fields/messages
function clearErrorInfo() {
    var fld, len = Validation.error_fields.length;
    for (var i=0; i<len; i++) {
        fld = Validation.error_fields[i];
        dw_Util.removeClass(fld, 'error');
    }
    Validation.error_fields = [];
    Validation.error_messages = [];
    clearErrorDisplay();
}

function clearErrorDisplay() {
    var errorEl = document.getElementById('errorDiv');
    if ( errorEl ) {
        errorEl.parentNode.removeChild(errorEl);
        window.location.hash = '';
    }
}

/////////////////////////////////////////////////////////////////////
//  

function isUSCurrency(val) {
    var re = /^\d*(\.\d{2})?$/;
    if ( re.test(val) ) {
        return true;
    }
    return false;
}

// credit to ppk's textarea length script (www.quirksmode.org/book/examplescripts.html)
function setupMaxLength(fld, num) {
    if ( document.createElement ) {
        var el = document.createElement("div");
        el.className = 'counter';
        fld.parentNode.appendChild(el);
        fld.maxLength = num || 1000;
        el.innerHTML = '<span>0</span>/' +  fld.maxLength + ' (maxlength - excess will be truncated)';
        fld.counterEl = el.getElementsByTagName('span')[0];
        fld.onkeyup = fld.onchange = monitorTextareaLength;
        fld.onkeyup();
    }
}

function monitorTextareaLength() {
    if ( this.counterEl && typeof this.counterEl.innerHTML != 'undefined' ) {
        this.counterEl.innerHTML = this.value.length;
    }
    if ( this.value.length >= this.maxLength ) {
        this.counterEl.className = 'overmax';
        //this.value = this.value.slice(0, this.maxLength);
    } else  {
        this.counterEl.className = '';
    }
}