/**
 * Products Display script
 * Dependencies: Mootools v1.1+
 * @author Kevin Dew <kev@dewsolutions.co.uk>
 * @copyright Copyright Kevin Dew, 2007
 */

window.addEvent('domready', function()
{
        
        if((typeof optionsData == 'undefined') || !$('val_form'))
                return;
        
        var originalPrice = $('price_val').getText();

        var valNotAvailable = new Validation([], 'Please Note', 'error', 'val_form');
        var valOptions = new Validation([], valTopError, 'error', 'val_form');

        function handleOptChange()
        {
                //see which options are selected
                var currentOptions = {};
                
                var anyZero = false;
                
                //see if theres any absolute options
                $each($ES('input[id^=product_options]'), function(input)
                {
                        currentOptions[input.getProperty('id').substring(16)] = input.getValue();
                        
                });
                $each($ES('select[id^=product_options]'), function(select)
                {
                        if(select.getValue() != 0)
                                currentOptions[select.getProperty('id').substring(16)] = select.getValue();
                        else
                                anyZero = true;
                });
                
                if(!anyZero)
                {
                        //should be safe to clear errors here
                        valNotAvailable.clearErrors();
                        
                        //check for any disallowed combinations
                        $each(optionsData, function(opt)
                        {
                                var match = true;
                                $each(opt.options, function(value, key)
                                {
                                        if((typeof currentOptions[key] != 'undefined') && (currentOptions[key] != value))
                                                match = false;
                                });
                                
                                if(match)
                                {
                                        if(opt.not_available)
                                        {
                                                valNotAvailable.addError('', 'The option combination you\'ve selected is currently unavailable');
                                                valNotAvailable.makeErrorList();
                                        }
                                        
                                        if(opt.custom_price)
                                                $('price_val').setText(opt.price);
                                        else
                                                $('price_val').setText(originalPrice);        
                                }
                        })
                        
                }
                else if($('price_val').getText() != originalPrice)
                      $('price_val').setText(originalPrice);
        }


        $each($ES('select[id^=product_options]'), function(el)
        {
                el.addEvent('change', handleOptChange);
        });
        
        handleOptChange();
        
        //check options are all selected
        $('val_form').addEvent('submit', function(e)
        {
                
                var anyZero = false;
                        
                $each($ES('select[id^=product_options]'), function(select)
                {
                        if(select.getValue() == 0)
                                anyZero = true;
                });
                
								valOptions.clearErrors();
								var varCheckErrorsAK = false;
                if(anyZero) {
                        valOptions.addError('', 'Please select all options.');
												varCheckErrorsAK = true;
                }
								if (document.getElementById('prod_notification_name')) {
									if (document.getElementById('prod_notification_name').value == '') {
                        valOptions.addError('', 'Please enter you name.');
												varCheckErrorsAK = true;
									}
								}
								if (document.getElementById('prod_notification_email')) {
									if (document.getElementById('prod_notification_email').value == '') {
                        valOptions.addError('', 'Please enter you email address.');
												varCheckErrorsAK = true;
									}
								}
								
								if (varCheckErrorsAK == true) {
									valOptions.makeErrorList();
									new Event(e).stop();
								}
        });
        
});

