function clearAttributes(){
	for(var i=1; i<$('option').length; i++){
		if(!isNaN($('option')[i].value) && $('option')[i].value != ''){
			$('displayAttributes_' + $('option')[i].value).style.display = 'none';
		}
	}
}

function clearQuantities(){
	var attributeELEMENT = $('attribute_' + $('option').value);
	var aID = $('attribute_' + $('option').value).value;
	var oID = $('option').value;

	for(var i=1; i<attributeELEMENT.length; i++){
		if(!isNaN(aID) && aID != ''){
			$('displayQuantity_' + $('option').value + '_' + attributeELEMENT[i].value).style.display = 'none';
		}
	}

}

function changeAttribute(){
	clearQuantities();
	var oID = $('option').value;
	var aID = $('attribute_' + $('option').value).value;
	if($('displayQuantity_' + oID + '_' + aID) != null){
		$('displayQuantity_' + oID + '_' + aID).style.display = 'block';
	}
}

function changeSinglePhoto(photo){
	if($('photo_' + photo) != null) {
		var siblings = $('photo_' + photo).getParent().getChildren();
		siblings.setStyle('display', 'none');
	
		$('photo_' + photo).style.display = 'block';
		$('click').style.display = 'block';
		
		$('option').value = photo;
	}
}

function changeMultiPhoto(photo, joinID){
	var siblings = $('photo_' + photo).getParent().getChildren();
	siblings.setStyle('display', 'none');
	
	$('photo_' + photo).style.display = 'block';
	$('click_' + joinID).style.display = 'block';
}

function changeSingleOption(){
	clearAttributes();
	
	if($('option').value != '' && $('option').value != null){
		if($('displayAttributes_' + $('option').value) != null) {
			$('displayAttributes_' + $('option').value).style.display = 'block';	
		}
		changeSinglePhoto($('option').value);
	}
}

function changeMultiOption(){
	var oID = $('option').value;
	var siblings = $('container_' + oID).getParent().getChildren();
	siblings.setStyle('display', 'none');
	
	$('container_' + oID).style.display = 'block';
	
	clearAttributes();
	
	if($('option').value != '' && $('option').value != null){
		if($('displayAttributes_' + $('option').value) != null) {
			$('displayAttributes_' + $('option').value).style.display = 'block';	
		}
	}
}

function validateProduct(x){
	var returnVal = true;
	
	if( $$( '.option' ).length ) {

		if( $$( '.option select' )[0].value == '' ) {
			var theLabel = $$( '.option label' )[0].title;
			alert( 'Please choose the ' + theLabel + '.' );
			returnVal = false;
		}

		if( $$( '.attribute' ).length && returnVal) {

			if ( $$( '.attribute_select' ).length ) {
				for( var i = 0; i < $$( '.attribute' ).length; i++){
	
					if( $$( '.attribute' )[i].getStyle( 'display' ) == 'block' && $$( '#' + $$( '.attribute' )[i].id + ' div select' )[0].value == '' ){
						var theLabel = $$( '#' + $$( '.attribute' )[i].id + ' div label' )[0].title;
						alert( 'Please choose the ' + theLabel );
						returnVal = false;
					}
					
				}
	
				if ( $$( '.quantity' ) && returnVal) {
				
					for( var j = 0; j < $$( '.quantity' ).length; j++ ){
						if( $$( '.quantity' )[j].getStyle( 'display' ) == 'block' && $$( '#' + $$( '.quantity' )[j].id + ' select' )[0].value == '' ) {
							alert('Please choose your Quantity.');
							returnVal = false;
						} else if( $$( '.quantity' )[j].getStyle( 'display' ) == 'block' && $$( '#' + $$( '.quantity' )[j].id + ' select' )[0].value == 0 ) {
							alert('This product is currently out of stock.');
							returnVal = false;
						}
					}
				}
			} else if ( $$( '.quantity' ) && returnVal) {
	
				if( $$( '.quantity select' )[0].value == '' ) {
					alert('Please choose your quantity.');
					returnVal = false;
				} else if( $$( '.quantity select' )[0].value == 0 ) {
					alert('This product is currenly out of stock.');
					returnVal = false;
				}
			}
		}
		
	} else if ( $$( '.quantity' ).length && returnVal) {
		if( $$( '.quantity select' )[0].value == '' ) {
			alert('Please choose your quantity.');
			returnVal = false;
		} else if( $$( '.quantity select' )[0].value == 0 ) {
			alert('This product is currenly out of stock.');
			returnVal = false;
		}

	}
	
	return returnVal;

}

/*
function validateQuantity() {
	
	var theFieldName = '';
	var allSelects   = $$( '#product_detail_form select' );
	
	for ( var i = 0; i < allSelects.length; i++ ) {
		if( allSelects[i].name.split('_')[0] == 'quantity' && allSelects[i].getStyle( 'display' ) == 'block' ) {
			theFieldName = 'displayAttributes_' + allSelects[i].getAttribute( 'id' ).split('_')[1];
		}
		
	}
	
	if( $( theFieldName ) ) {
		if( $( theFieldName ).value == '') {
			alert('Please select a quantity to add this item to your cart.');
			return false;
		} else if( $( theFieldName ).value == 0) {
			alert('Product is currently out of stock.');
			return false;
		} else {
			return true;
		}
	} else {
		alert('Error: Quantity Not Founnd.');
		return false;
	}
}
*/
