jQuery(document).ready(function(){

	//redirect to add to cart url on button click
	jQuery("button.list-add").click(function(){
		var size = jQuery("select[rel=" + jQuery(this).attr("id") + "]").val();
		var qty = jQuery("input[rel=" + jQuery(this).attr("id") + "]").val();
		if (size == "")
			//need german translation (move this into template, actually)
			alert(jQuery(".error-select525").html());
		else {
			//if no quantity entered ... assume 1
			if (!jQuery("input[rel=" + jQuery(this).attr("id") + "]").hasClass("focused") || qty == "")
				qty = 1;
			window.location=jQuery(this).attr("rel") + "super_attribute[525]=" + size + "&qty=" + qty;
		}
	return false;
	});

	jQuery(".autoship-parameters input").click(function(){
		jQuery("input[name=activate-autoship]").not(this).attr("checked","");
	})


	//blank out label on input field focus
	jQuery(".list-page-quantity").focus(function(){
		jQuery(this).val('').addClass('focused');
	}); 	

	//open new window for size chart (and external links)
	jQuery("a[rel=external]").click(function(){
		window.open(jQuery(this).attr('href'));
		return false;
	});
	jQuery(".list-page-size-link").click(function(){
		window.open(jQuery(this).attr('href'));
		return false;
	});
	jQuery(".view-page-size-link").click(function(){
		window.open(jQuery(this).attr('href'));
		return false;
	});

	//post data to script on newsletter signup
	jQuery(".newsletter-form-container .signupbutton").click(function(){
		jQuery.post(
			'/infusionsoft/signup.php',
			jQuery('#newsletter-validate-detail').serialize(), //form data
			function(data){
				jQuery('#newsletter-validate-detail').html('THANK YOU!');
			}
		);
		return false;
	});

	// fix checkout page
	jQuery("#onestepcheckout-form li, .col2-set, li.fields, li.wide").append('<div class="clear"></div>');

	bindCCSelect();

});


function getCCCode(ccType){
	var cType = new Array();
	cType['American Express'] = 'AE';
	cType['Visa'] = 'VI';
	cType['MasterCard'] = 'MC';
	cType['Discover'] = 'DI';
	cType['Maestro/Solo'] = 'SS';
	return cType[ccType];
} 
 
function IsValidCC(str) { //A boolean version
    if (GetCreditCardTypeByNumber(str) == '?') return false;
    return true;
}

function bindCCSelect(){
	// auto select credit card number
	jQuery('#paypal_direct_cc_number').blur(function(){
		
		var ccNum = jQuery(this).val();
		var ccType = GetCreditCardTypeByNumber(ccNum);
		var ccCode = getCCCode(ccType);
		
		//alert(ccNum+' '+ccType+' '+ccCode);
		jQuery('#paypal_direct_cc_type').val(ccCode);
	});
}

function GetCreditCardTypeByNumber(ccnumber) {
    var cc = (ccnumber + '').replace(/\s/g, ''); //remove space
 
    if ((/^(34|37)/).test(cc) && cc.length == 15) {
        return 'American Express'; //AMEX begins with 34 or 37, and length is 15.
    } else if ((/^(51|52|53|54|55)/).test(cc) && cc.length == 16) {
        return 'MasterCard'; //MasterCard beigins with 51-55, and length is 16.
    } else if ((/^(4)/).test(cc) && (cc.length == 13 || cc.length == 16)) {
        return 'Visa'; //VISA begins with 4, and length is 13 or 16.
    } else if ((/^(300|301|302|303|304|305|36|38)/).test(cc) && cc.length == 14) {
        return 'DinersClub'; //Diners Club begins with 300-305 or 36 or 38, and length is 14.
    } else if ((/^(2014|2149)/).test(cc) && cc.length == 15) {
        return 'enRoute'; //enRoute begins with 2014 or 2149, and length is 15.
    } else if ((/^(6011)/).test(cc) && cc.length == 16) {
        return 'Discover'; //Discover begins with 6011, and length is 16.
    } else if ((/^(3)/).test(cc) && cc.length == 16) {
        return 'JCB';  //JCB begins with 3, and length is 16.
    } else if ((/^(2131|1800)/).test(cc) && cc.length == 15) {
        return 'JCB';  //JCB begins with 2131 or 1800, and length is 15.
    }
    return '?'; //unknow type
}
 
