var targetItemID = 0;
sizeTypes = new Array();
sizeTypes['']

$(document).ready(function(){ 
	
	// they haven't done anything yet!
	$( '#store-js-error-msg' ).css( 'display', 'none' );

	// set the targetItemID var based on the id of the submitted form
	$( '.form-submit' ).click( function() {
		var rawID = this.id;
		var splitID = rawID.split( '-' );
		targetItemID = splitID[0];
	});
	
	// swap the item image, append to the price or description 
	// depending on the size option selected
	$( '.size-select' ).change( function() {
	
		var rawID = 0;
		var splitID = 0;
		
		var rel = '';
		var value = '';
		var selValue = '';
		var selectHTML = '';
		
		var newDescr = '';
		var photo = '';
		var newPhoto = '';
		var newPrice = '';
		var itemNum = '';
		var newItemNum = '';
		
		rawID = this.id;
		splitID = rawID.split( '-' );
		targetItemID = splitID[0];
		selValue = $( '#' + targetItemID + '-size option:selected' ).val();
		
		// populate the qty select menu with the appropriate options
		for(i=1; i <= itemV[targetItemID][selValue]['qty']; i++) {
			selectHTML += '<option value="'+i+'">'+i+'</option>';
		}
		$( '#' + targetItemID + '-qty' ).html(selectHTML);

		// modify the description content
		newDescr = '<p>' + $( '#' + targetItemID + '-default-description p' ).html() + itemV[ targetItemID ][ selValue ]['descr_addon'] + '</p>';
		$( '#' + targetItemID + '-description' ).html( newDescr );

		// swap the photo
		photo = $( '#' + targetItemID + '-default-photo' ).html();
		newPhoto = itemV[ targetItemID ][ selValue ]['photo'];
		if( !newPhoto ) newPhoto = photo;
		$( '#' + targetItemID + '-photo' ).attr( 'src', newPhoto );

		// modify the price
		newPrice = ( $( '#' + targetItemID + '-default-price' ).html() * 1) + ( itemV[ targetItemID ][ selValue ]['price_addon'] * 1);
		$( '#' + targetItemID + '-price' ).val( newPrice );
		$( '#' + targetItemID + '-display-price' ).html( '$' + newPrice.toFixed( 2 ) );

		// modify the item number
		itemNum = $( '#' + targetItemID + '-default-item-num').html();
		newItemNum = itemV[ targetItemID ][ selValue ]['item_no'];
		if( ! newItemNum ) newItemNum = itemNum;
		$( '#' + targetItemID + '-item-num' ).val( newItemNum );
		$( '#' + targetItemID + '-display-item-num' ).html( newItemNum );
	});
});

// pre-processor for the "Add to Cart" forms for FoxyCart
// we'll do our validation here, then decide if we continue the add to cart process
function fc_PreProcess() {
	var errCount = 0;
	var errMsg = '';
	
	// if there are size options, check to make sure they selected a size
	if( ! $( '#' + targetItemID + '-size option:selected' ).val() ) {
		++errCount;
		errMsg = '<li>Please select a size.</li>';
	}
	
	// check to make sure they selected a quantity
	if( ! $( '#' + targetItemID + '-qty option:selected' ).val() ) {
		++errCount;
		errMsg += '<li>Please select a quantity.</li>';
	}
	
	// either display an error message, or let the process continue
	if( errCount > 0 ) {
		errMsg = 'Please correct the following errors below: <ul>' + errMsg + '</ul>';
		document.getElementById( targetItemID + '-error-msg' ).innerHTML = errMsg;
		document.getElementById( targetItemID + '-error-msg' ).style.display = '';
		return false;
	} else {
		document.getElementById( targetItemID + '-error-msg' ).style.display = 'none';
		return true;
	}
}