var color = null;
var size = null;

String.prototype.isValidEmail = function() { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(this));};
function showTab(id){
	var allTabs = $('tabs').getElementsByTagName('a');	
	for(var i=0; i<allTabs.length; i++){
		allTabs[i].parentNode.className = '';
		$('tab-'+(i+1)).style.display='none';
	}
	allTabs[id-1].parentNode.className = 'active';	
	$('tab-'+id).style.display='block';
}

function selectColor(el, id){
    //$$('#product-colors a').invoke('removeClassName')
	var allColors = $('product-colors').getElementsByTagName('a');
	for(var i=0; i<allColors.length; i++){
		allColors[i].className = '';
	}
	el.className = 'active';	
	$('color').value = id;
	$('order-button').href = '/cart?action=add&id=' + $('id').value + '&color=' + $('color').value + '&size=' + $('size').value;
	$('choice-color').innerHTML = ', цвят ' + el.getElementsByTagName('span')[0].innerHTML;
	
	color = id;
}

function selectSize(el, id){
	var allColors = $('product-sizes').getElementsByTagName('a');
	for(var i=0; i<allColors.length; i++){
		allColors[i].className = '';
	}
	el.className = 'active';
	$('size').value = id;
	$('order-button').href = '/cart?action=add&id=' + $('id').value + '&color=' + $('color').value + '&size=' + $('size').value;
	$('choice-size').innerHTML = ', размер ' + el.innerHTML;
	
	size = id;
}

function showImage(newSrc, el) {
    $A($$('#thumbs a')).invoke('removeClassName', 'active');
	$$('#big-image img')[0].src = newSrc;
	el.addClassName('active');
}

/*---------------------*/
function change_color(select_box, loop_counter) {
    loc = '/cart?action=edit_contents&sub_action=color' + 
          '&item_num=' + loop_counter + '&new_color=' + $F(select_box)
    window.location = loc;
}
function change_size(select_box, loop_counter) {
    loc = '/cart?action=edit_contents&sub_action=size' + 
          '&item_num=' + loop_counter + '&new_size=' + $F(select_box)
    window.location = loc;
}
/*---------------------*/
document.observe('dom:loaded', function() {
    //Fire all blink fields
    $A($$('input.blink[type=text]')).each(function(input) {
        if(input.title!='' && $F(input)!=input.title) {
            return;
        }
        input.title = $F(input);
        input.onfocus = function(ev) {
            if(input.value == input.title) {
                $(input).value = '';
            }
        }
        input.onblur = function(ev){
            if(input.value == '') {
                $(input).value = input.title;
            }
        }
        $(input).up('form').observe('submit', function(e) {
            if(input.title==input.value) {
                input.value = '';
            }
        });
    });
    //Magic focus :-)
    $$('form.focused input', 'form.focused textarea').each(function(field) {
        if($F(field)=='') {
            field.focus();
            throw $break;
        }
    });
});
/*---------------------*/
var FormValidator = Class.create();
FormValidator.prototype = {
    succes: false,
    errorMsg: '',
    errorField: {},
    errorsContainer: [],
    rules: {
        notEmpty     : /(\w|\W)+/,
        validMail    : /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/,
        alphaNumeric : /(\w|\d)*/,
        numeric      : /[0-9]*/
    },
    initialize: function(form, options, callback, settings) {
        this.settings = Object.extend({
            handleAllErrors: false, //if true you should supply callback function, which takes array with all errors
            fireEvent: 'submit'     //Event, which will start the validation, without the prefix 
        }, settings || {});
        this.form = $(form);
        this.options = options;
        this.callback = callback || this.onError;
        Event.observe(form, this.settings.fireEvent, this.observer.bindAsEventListener(this));
    },
    observer: function(event) {
        this.succes = true;
        this.options.each(this.fieldsIterator.bind(this));
        if(!this.succes) {
            if(!this.settings.handleAllErrors) {
                this.callback(this.errorMsg, this.errorField);
            } else {
                this.callback(this.errorsContainer);
            }
            Event.stop(event);
            this.errorsContainer = []
        }
    },
    fieldsIterator: function(opt) {
        field = eval("this.form."+opt.field);
        //If the rule is function - pass the value to it, if string - test the regex from rules with this key
        if( (typeof opt.rule=='function' && !opt.rule($F(field))) || (typeof opt.rule=='string' && !this.rules[opt.rule].test($F(field))) ) {
            this.errorMsg = opt.errorMsg;
            this.errorField = field;
            this.succes = false;
            if(!this.settings.handleAllErrors) throw $break; // Handle only one first error
            else this.errorsContainer.push({'msg': this.errorMsg, 'field':this.errorField}); //collect all errors and then send them to callback 
        }
    },
    //Default error function
    onError: function(errorMsg, errorField) {
        alert(errorMsg);
        errorField.focus();
    }
};
function fancy_edit(link) {
    //Show input and hide junk
    var input = $(link).previous().previous();
    if($F(input)=='Няма информация') {
        $(input).value = '';
    }
    input.show().focus();
    $(link).hide().previous().hide();
    return false;
}