
function getProductAttributes(type, save_state, product_id, suffix) {
    
	var opposite_type = 'size';
    
	if(type == 'size') {
    	opposite_type = 'color';
    }

    var type1 = dojo.byId(type + suffix);
    var type2 = dojo.byId(opposite_type + suffix);
    
    if(type == 'color') {
    	type2.disabled = true;
    }
    
    var url = '/products/get-product-attributes/type/' + type + '/pid/' + product_id + '/suffix/' + suffix + '/aid/' + type1.value + '/save/' + save_state;

	dojo.xhrGet({
		url: url,
		handleAs: 'json',
		load: function(data) {
			
			var quantity = dojo.byId('quantity_container' + suffix);
			quantity.innerHTML = data.product_quantity;

            if(type == 'color') {

                var size = document.getElementById('size_container' + suffix);
                size.innerHTML = data.fbml_size_element;

            }

			return;
		}
    });  
}

function addExtraRow(){

	var t = dojo.byId('attributes');
	var n = t.rows.length - 1;
	
	/*if(n > 0){
		var result = validateAttributeRow(t,n);
		if(result != true){
			alert(result);
			return false;
		}
	}
	*/
	var tr = t.insertRow(t.rows.length);
	var td_qty   = tr.insertCell(0);
	var td_size  = tr.insertCell(1);
	var td_color = tr.insertCell(2);
	var td_del   = tr.insertCell(3);
			
		td_qty.innerHTML   = '<input type="text" name="qty[]" size="5" />';
		td_size.innerHTML  = '<input type="text" name="size[]" size="15" />';
		td_color.innerHTML = '<input type="text" name="color[]" size="15" />';
		td_del.innerHTML   = '<input type="hidden" name="attribute_id[]" value="" /> <img src="'+IMAGE_PATH+'/xicon.gif" onclick="deleteAttribute(this,0);" title="Delete" alt="[X]" />';

		tr.cells[0].firstChild.focus();
}

function deleteAttribute(r,id) {
	
	var t = dojo.byId('attributes');
	var n = r.parentNode.parentNode.rowIndex;
	
	if(id !=''){
		if(confirm('Are you sure? You cannot undo this.')){
			dojo.xhrGet({
				url: '/products/remove-attribute/?id='+id,
				handleAs: 'json',
				load: function(data) {
					data = eval('('+data+')');
					if(parseInt(data) == 1){
						t.deleteRow(n);
					}
				}
			});  
		}
	}else{
		if(n == 1) return false;
		t.deleteRow(n);
		return false;
	}
}

function validateAttributeRow(t,n){
	
	var q = t.rows[n].cells[0].firstChild.value;
	var s = t.rows[n].cells[1].firstChild.value;
	var c = t.rows[n].cells[2].firstChild.value;
	
	if(q > 0 && (s !='' || c !='')){
		return true;
	}
	
	if(q == ''){
		return 'Quantity for row ' + n + ' is blank';
	}
	
	if(isNaN(q)){
		return 'Quantity for row ' + n + ' is invalid';
	}
	
	if(s =='' && c == ''){
		return 'Please specify a size or color for row ' + n;
	}
}

function validateAttributes(){
	
	var table = dojo.byId('attributes');
	var rows = table.rows.length;
	var result;
	
	if(rows == 1){
		return false;
	}	
	
	for(row=1; row < rows; row++){
		result = validateAttributeRow(table, row);
		if(result != true){
			break;
		}
	}
	
	if(result != true){
		setMessage(result, 'attributes_message', 'error', false);
		return false;
	}else{
		
		dojo.xhrPost ({
			url: '/products/attributes-post',
			handleAs: "json",
			form: 'product_attributes',
			load: function (data) {
				
				var result = eval ('('+data+')');
				if(parseInt(result) > 0){
					setMessage('Sizes and Colors Saved', 'attributes_message', 'success', true);
				}else{
					setMessage('Sizes and Colors Failed to save', 'attributes_message', 'error', false);
				}
			},
			error: function (error) {
				console.error ('Error: ', error);
			}
		});
		
		return true;
	}
}

