/* ***************************************************** */
/* Item
/* ***************************************************** */

function changeImage(medium, large) {
    $('previewImage').src = medium;
    $('previewLink').href = large;
    $('previewLink').onclick = function() {
        window.open(large, '', 'location=no,scrollbars=yes,menubar=no,resizable=yes,status=no,width=800,height=600');
        return false;
    };
}

function changeConfigColor() {
    changeConfig('color', 'size', 0, 1);
}

function changeConfigSize() {
    changeConfig('size', 'color', 1, 0);
}

function changeConfig(lista, listb, indexa, indexb) {
    if (! configs) return;
    
    if ($(listb)) {
        
        var selected_listb = $F(listb);
        var quantity = 0;
        
        // Empty listb/quantity lists
        [listb, 'quantity'].each( function(id) {
            var list = $(id);
            var n = list.options.length;
            for (var i = 0; i < n; i++) {
                list.options[0] = null;
            }
            list.disabled = true;
        });
        
        // Get a unique, ordered array of listb value based on the config
        var values = $H();
        configs.each( function(config) {
            
            if (config[indexa] == $F(lista) || $F(lista) == "") {
                values[config[indexb]] = 1;
                if (selected_listb != "" && selected_listb == config[indexb]) {
                    $('config').value = config[3];
                    quantity = config[2];
                }
            }
        });
        values = values.keys();
        values.sort();
        
        // Populate listb
        var list = $(listb);
        list.options[0] = new Option('', '');
        values.each( function(value) {
            var selected = selected_listb == value;
            list.options[list.options.length] = new Option(value, value, selected, selected);
        });
        list.disabled = false;
        
        // Populate the quantity list
        if (quantity) {
            var list = $('quantity');
            for (var i = 1; i <= quantity; i++) {
                list.options[i-1] = new Option(i, i);
            }
            list.disabled = false;
        }
    }
    else {
            
        var quantity = 0;
        
        // Empty the quantity list
        var list = $('quantity');
        var n = list.options.length;
        for (var i = 0; i < n; i++) {
            list.options[0] = null;
        }
        list.disabled = true;
        
        // Find the quantity for the selected value
        configs.each( function(config) {
            
            if (config[indexa] == $F(lista)) {
                $('config').value = config[3];
                quantity = config[2];
            }
        });
        
        // Populate the quantity list
        if (quantity) {
            var list = $('quantity');
            for (var i = 1; i <= quantity; i++) {
                list.options[i-1] = new Option(i, i);
            }
            list.disabled = false;
        }
    }
}

function addToBag() {
    
    if ($('color') && $F('color') == "") {
        alert('Please select a color option above before purchasing the item.');
        return;
    }
    
    if ($('size') && $F('size') == "") {
        alert('Please select a size option above before purchasing the item.');
        return;
    }
    
    if ($F('quantity') == "") {
        alert('Please select the quantity above before purchasing the item.');
        return;
    }
    
    $('addToBagForm').submit();
}

function checkChars(value, validChars) {
    
    var valid = true;
    
    for (var i = 0; i < value.length && valid; i++) {
        if (validChars.indexOf(value.charAt(i)) == -1) {
            valid = false;
        }
    }

    return valid;
}

function checkCheckout(form) {
    
    // Do basic checks
    if (! checkEditForm(form)) return false;
    
	// don't check billing values if we're going through paypal
	if ($F('paypal_authorized') == 1) {
		return true;
	}
	
    // Make sure credit card number looks somewhat valid
    if ($F('billing_cardnumber').length < 13 || !checkChars($F('billing_cardnumber'), '0123456789- ')) {
        highlightField('billing_cardnumber', 1);
        displayError(form, 'Please enter a valid credit card number');
        return false;
    }
    
    // Make sure CVV looks somewhat valid
    if ($F('billing_cvmvalue').length < 3 || !checkChars($F('billing_cvmvalue'), '0123456789')) {
        highlightField('billing_cvmvalue', 1);
        displayError(form, 'Please enter a valid verification code');
        return false;
    }
        
    if (!$F('billing_address1').match(/\d+/)) {
        highlightField('billing_address1', 1);
        displayError(form, 'Please enter a valid billing address');
        return false;
    }
    
    return true;
}

function copyShipping() {
    
    ['name', 'address1', 'address2', 'city', 'zip', 'phone'].each( function(field) {
        if ($('shipping_' + field)) 
            $('billing_' + field).value = $('shipping_' + field).value;
    });
    
    if ($('shipping_state'))
        $('billing_state').selectedIndex = $('shipping_state').selectedIndex;
}

function giftSelect(id) {
    $$('img.giftcard').each( function(img) {
        if (img.id == id) {
            img.setStyle({ border: 'solid 1px black'});
        }
        else {
            img.setStyle({ border: 'solid 1px white'});
        }
    });
    $('card').value = id;
}

function checkGiftSelect(form) {
    
    // Do basic checks
    if (! checkEditForm(form)) return false;
	
	// Make sure the email address looks valid
	if ($F('recipient_email') == '' || !$F('recipient_email').match(/.+\@.+\..+/)) {
        highlightField('recipient_email', 1);
        displayError(form, 'Please enter a valid recipient email address');
        return false;
    }
    if ($F('from_email') == '' || !$F('from_email').match(/.+\@.+\..+/)) {
        highlightField('from_email', 1);
        displayError(form, 'Please enter a valid from email address');
        return false;
    }
    
    return true;
}

function checkGiftPurchase(form) {
    
    // Do basic checks
    if (! checkEditForm(form)) return false;
	
    // Make sure credit card number looks somewhat valid
    if ($F('billing_cardnumber').length < 13 || !checkChars($F('billing_cardnumber'), '0123456789- ')) {
        highlightField('billing_cardnumber', 1);
        displayError(form, 'Please enter a valid credit card number');
        return false;
    }
    
    // Make sure CVV looks somewhat valid
    if ($F('billing_cvmvalue').length < 3 || !checkChars($F('billing_cvmvalue'), '0123456789')) {
        highlightField('billing_cvmvalue', 1);
        displayError(form, 'Please enter a valid verification code');
        return false;
    }
        
    if (!$F('billing_address1').match(/\d+/)) {
        highlightField('billing_address1', 1);
        displayError(form, 'Please enter a valid billing address');
        return false;
    }
    
    return true;
}

YAHOO.util.Event.addListener(window, 'load', initRecommendDialog);
function initRecommendDialog() {

    var div = $('recommendDialog');
    if (! div) return;
    
    // Initialize the dialog
    var dialog = new YAHOO.widget.Dialog(div, {
        width: '280px',
        fixedcenter: true,
        visible: false, 
        constraintoviewport: true,
        buttons: [ { text: 'Send Email', handler: function() { this.submit() }, isDefault: true }, { text: 'Cancel', handler: function() { this.cancel(); }} ],
        postmethod: 'form',
        draggable: false
    });
    
    dialog.validate = recommendDialogValidate;
    
    // Render the dialog
    dialog.render();

    // Check for links to launch the dialog and attach an onclick event
    var links = $$('.' + div.id + 'Link');
    links.each( function(link) {
        YAHOO.util.Event.addListener(link, 'click', 
            function() { dialog.show() }, dialog, true);
        link.style.cursor = 'pointer';
    });
}

function recommendDialogValidate() {

    var data = this.getData();

    if (data.from == '' || !data.from.match(/.+\@.+\..+/)) {
        alert('Please enter a valid "From" address');
        return false;
    }
    
    if (data.to == '' || !data.to.match(/.+\@.+\..+/)) {
        alert('Please enter a valid "To" address');
        return false;
    }

    return true;
}

function recommendGreetingChange() {
    if ($F('recommendGreeting') == 'custom') {
        Element.show('recommendCustomGreetingField');
    }
    else {
        Element.hide('recommendCustomGreetingField');
        $('recommendCustomGreeting').value = '';
    }    
}
