var cc_result = -1;

function clearValue(clearInput, defaultValue)
{
    if (clearInput.value == defaultValue) clearInput.value = '';
}

$(document).ready(function()
{

    /* menu */
    $(document).ready(function(){
        $("ul.sf-menu").superfish({
            animation: {height:'show'},   // slide-down effect without fade-in
            delay:     1200               // 1.2 second delay on mouseout
        });
    });

	/* cv2 tooltip help */
	$('#cv2help').tooltip();
	
	/* basket comments tooltip */
	$('.tooltip').tooltip();

    /* cc type check */
    var cc_select = $('#ecom_cc_type');
    if (cc_select.length > 0) {
        ccTypeSelectShowHide(cc_select.val());
    }
    
    /* cc esortcode check */
    var cc_num = $('#ecom_cc_number');
    if (cc_num.length > 0) {
        var cc_num_validate = $('#escCCValidateCardNumber');
        var cc_num_val = cc_num.val();
        if (cc_num_val != '') {
            cc_num_validate.val(cc_num_val);
            ValidateCCThis();
            cc_result = 0;
        }
        cc_num.blur(function() {
            cc_num_validate.val(cc_num_val);
            ValidateCCThis();
            cc_result = 0;
        });
    }

    /* basket redirect reply */
/*
    var redirect_reply = $('#ecom_redirect_reply');
    if (redirect_reply.length > 0) {
        $.blockUI({
            message: redirect_reply,
            css: {  
                padding:        50, 
                margin:         0, 
                width:          '30%',  
                top:            '40%',  
                left:           '35%',  
                textAlign:      'center',  
                color:          '#000',  
                border:         '3px solid #aaa', 
                backgroundColor:'#fff', 
                cursor:         'wait' 
            }
        }); 
        setTimeout($.unblockUI, 1500);
    }
*/
    
    /* basket save */
    var basket_save = $('#button_ecom_basket_save');
    if (basket_save.length > 0) {
        basket_save.click(function() {
            $.blockUI({
                message: $('#ecom_basket_save'),
                css: {  
                    padding:        50, 
                    margin:         0, 
                    width:          '33%',  
                    top:            '40%',  
                    left:           '35%',  
                    textAlign:      'left',  
                    color:          '#000',  
                    border:         '3px solid #aaa', 
                    backgroundColor:'#fff', 
                    cursor:         'wait' 
                }
            });
        });
        
        $('#button_ecom_basket_cancel').click(function() {
            $.unblockUI();
        });
    }
    
    /* forgotten password */
    var forgot_password = $('#button_ecom_checkout_account_forgotten_password_save');
    if (forgot_password.length > 0) {
        forgot_password.click(function() {
            $.blockUI({
                message: $('#ecom_checkout_account_forgotten_password'),
                css: {  
                    padding:        50, 
                    margin:         0, 
                    width:          '33%',  
                    top:            '40%',  
                    left:           '35%',  
                    textAlign:      'left',  
                    color:          '#000',  
                    border:         '3px solid #aaa', 
                    backgroundColor:'#fff', 
                    cursor:         'wait' 
                }
            });
        });
        
        $('#button_ecom_checkout_account_forgotten_password_cancel').click(function() {
            $.unblockUI();
        });
    }

/*
    $('.ecom_categories_tree ul').accordion({ 
        active: false, 
        header: '.head', 
        navigation: true, 
        event: 'mouseover', 
        fillSpace: true, 
        animated: 'easeslide',
        autoheight: false,
        event: 'mouseover', 
        active: '.selected', 
        selectedClass: 'active', 
        animated: "bounceslide", 
        header: "dt"
    });
*/
/*
    $('.ecom_categories_tree ul').accordion({ 
        autoHeight: false,
        active: false, 
        navigation: true,
        event: 'mouseover'
    });
*/
});

function showHide (showOrHide, elementToChange) {
    if (showOrHide == 'show')
    {
        $('#'+elementToChange).show();
    }
    else
    {
        $('#'+elementToChange).hide();
    }
}

function openUrl(url)
{
	if (arguments.length > 1) {
		window.open(url, arguments[1]);
	} else {
    	window.open(url, '_self');
	}
}
function openUrlFromSelect(select)
{
	var url = select.options[select.options.selectedIndex].value;
	openUrl(url);
}
function submitOnChange(idForm)
{
    $('#' + idForm).submit();
}


function ccTypeSelectShowHide(selectOrValue)
{
    if (typeof(selectOrValue) == 'string') {
        var val = selectOrValue;
    } else if (selectOrValue != undefined) {
        var val = selectOrValue.options[selectOrValue.options.selectedIndex].value;
    } else {
        var val = 'rubbish';
    }

    if (
        val == 'Solo'
        || val == 'Switch'
        || val == 'American Express'
        || val == 'Maestro'
    ) {
        $('#ecom_cc_extra').show();
        if (val != 'American Express') {
            $('#ecom_cc_extra_issue').show();
        } else {
            $('#ecom_cc_extra_issue').hide();
        }
    } else {
        $('#ecom_cc_extra_issue').hide();
        $('#ecom_cc_extra').hide();
    }
}

function checkCc() {
    var cc_response = $('#escCCValidateValidationMessage');
    if (cc_response.val() == '') {
        alert('We are validating your credit card. Please try again in a few seconds.');
        if (cc_result < 0) {
            ValidateCCThis();
        }
        cc_result = 0;
    } else {
        //alert(cc_response.val());
        cc_result = 1;
    }
    if (arguments.length > 0 && cc_result > 0) {
        arguments[0].submit();
    } else if (cc_result > 0) {
        return true;
    } else {
        return false;
    }
}