/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * @category  Symmetrics
 * @package   Symmetrics_Lv
 * @author    symmetrics gmbh <info@symmetrics.de>
 * @author    Eduard Melnitskiy <eduard.melnitskiy@symmetrics.de>
 * @copyright 2011 symmetrics gmbh
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @link      http://www.symmetrics.de/
 */

/**
 * If Validator present we add new validator, which provide possibility to validate username and email from one field.  
 */
if (Validation) {
    Validation.addAllThese([ 
        [
            'validation-usernameemail',
            'Please enter a valid username. Username should contain at least 8 symbols and begin with an alpha character. Username contains only alphanumeric characters and symbols: -_',
            function(v,r){
                return Validation.get('IsEmpty').test(v) 
                || /^([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*@([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*\.(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]){2,})$/i.test(v) 
                || (/(^[a-zA-Z]{1,1}[0-9a-zA-Z_0-9\-]+$)/i.test(v) && v.length >= 8);
            }
        ],
        [
            'validation-username',
            'Please enter a valid username. Username should contain at least 8 symbols and begin with an alpha character. Username contains only alphanumeric characters and symbols: -_',
            function(v,r){
                return Validation.get('IsEmpty').test(v)
                || (/(^[a-zA-Z]{1,1}[0-9a-zA-Z_0-9\-]+$)/i.test(v) && v.length >= 8);
            }
        ],
        [
            'validation-customemail',
            'Please enter a valid username.',
            function(v,r){
                return  Validation.get('IsEmpty').test(v) 
                || /^([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*@([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*\.(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]){2,})$/i.test(v) 
                || (/(^[a-zA-Z]{1,1}[0-9a-zA-Z_0-9\-]+$)/i.test(v) && v.length >= 8)
                || true;
            }
        ],
        [
            'validate-password', 
            'Please enter 8 or more characters. Leading or trailing spaces will be ignored.', 
            function(v) {
                var pass = v.strip(); /*strip leading and trailing spaces*/
                return !(pass.length > 0 && pass.length < 8);
            }
        ],
        [
            'validate-new-password', 
            'Please enter 8 or more characters. Leading or trailing spaces will be ignored.', 
            function(v) {
                if (!Validation.get('validate-password').test(v)) return false;
                if (Validation.get('IsEmpty').test(v) && v != '') return false;
                return true;
            }
        ],
    ]);
};

document.observe("dom:loaded", function() {
    Object.extend(Validation, {
        insertAdvice : function(elm, advice){
            var container = $(elm).up('.field-row');
            if(container){
                Element.insert(container, {after: advice});
            } else if (elm.up('td.value')) {
                elm.up('td.value').insert({bottom: advice});
            } else if (elm.advaiceContainer && $(elm.advaiceContainer)) {
                $(elm.advaiceContainer).update(advice);
            }
            else {
                switch (elm.type.toLowerCase()) {
                    case 'checkbox':
                    case 'radio':
                        var p = elm.parentNode;
                        if(p) {
                            Element.insert(p, {'bottom': advice});
                        } else {
                            Element.insert(elm, {'after': advice});
                        }
                        break;
                    default:
                        Element.insert(elm.up(), {'after': advice});
                }
            }
        }
    })
});
