function switchProductTab(tabs, active){

	if(active !='prod_desc'){
		if(document.getElementById('product_id').value==''){
			alert('You must save a Product Description before continuing.');
			return false;
		}
	}
	
	dojo.query('.tab_box', dojo.byId(tabs + '_container')).style('display','none');
	dojo.query('li', dojo.byId(tabs)).removeClass('active');
	
	dojo.byId(active + '_container').style.display = 'block';
	dojo.byId(active).className = 'active';
	
}

function validateProduct(form){

	var inputs = form.elements;
	var inputs_count = inputs.length;
	
	for(var i=0; i< inputs_count; i++){
		if(typeof(form.elements[i]) !='undefined' || form.elements[i].id !=''){
			if(/product_start_price*/.test(form.elements[i].id)){
				var start_price = form.elements[i];
			}
			if(/product_start_date*/.test(form.elements[i].id)){
				var start_date = form.elements[i];
			}
			if(/has_sc*/.test(form.elements[i].id)){
				var has_sizes_color = form.elements[i];
			}
		}
	}
	
	var divs = form.getElementsByTagName('div');
	
	for(var d=0; d < divs.length; d++){
		if(typeof(divs[d].id) !='undefined' || divs[d].id != null){
			if(/attributes*/.test(divs[d].id)){
				var attributes = divs[d];
				break;
			}
		}
	}

	var ul = attributes.getElementsByTagName('ul');
	
	for(var i=0; i< inputs_count; i++){
		if(typeof(inputs[i]) !='undefined' || inputs[i].id !=''){
		
			var element = inputs[i];
			var input_id = inputs[i].id;
			
			if(/product_title/.test(input_id)){
				if(!validate.string(element, 'Product Title', 100)){
					return false;
				}
			}
			
			if(/product_description/.test(input_id)){
				if(!validate.string(element, 'Product Description', 300)){
					return false;
				}
			}
			
			if(/product_keywords/.test(input_id)){
				if(!validate.string(element, 'Product Keywords', 300)){
					return false;
				}
			}
			
			if(/product_category1/i.test(input_id)){
				if(!validate.select(element, 'Product Category')){
					return false;
				}
			}
			
			if(/product_start_date/.test(input_id)){
				if(!validate.date(element, 'Product Start Date')){
					return false;
				}
			}
			
			if(/product_end_date/.test(input_id)){
				if(!validate.date(element, 'Product End Date')){
					return false;
				}
				
				var enddate = new Date (element.value);
				var startdate = new Date (start_date.value);
				
				if(startdate > enddate){
					validate.message(element, 'End date cannot pre-date start date');
					return false;
				}
			}
			
			if(/product_price/.test(input_id)){
				if(!validate.price(element, 'Product Price', 0)){
					return false;
				}
			}
			
			if(/product_offer/.test(input_id)){
				if(element.checked){
					if(!validate.price(start_price, 'Product Start Price', 0)){
						return false;
					}
				}
			}
			
			if(has_sizes_color.value==0){
				if(/product_quantity/.test(input_id)){
					if(!validate.number(element, 'Product Quantity')){
						return false;
					}
				}
			}
		}
	}
	
	//has sizes & colors
	if(has_sizes_color.value=='1'){
		
		for(var i=1; i < ul.length; i++){
			
			var li = ul[i].getElementsByTagName('li');
			
			var qty = li[0].getElementsByTagName('input');
			var size = li[1].getElementsByTagName('input');
			var color = li[2].getElementsByTagName('input');
			
			if(!validate.number(qty[0], 'Sizes and colors row ' + i + ' quantity ')){
				return false;
			}
			
			if(size[0].value =='' && color[0].value == ''){
				validate.message(size[0], 'Please specify a size or color for row ' + i);
				return false;
			}
		}
	}
	
	switchProductTab('product_tabs', 'prod_imgs');
	return true;
}

function saveProductTitle(){
	
	var product_title = dojo.byId('product_title').value;

	if((dojo.byId('product_id').value == '') && (product_title != '')){
		
		navigate(SOCIAL_WEB_PATH  + '/products/save-title/t/' + encodeURIComponent(product_title), CONTENT_TYPE_JSON,
			
			function(obj){
		
				if(obj.data.product_id > 0){
					dojo.byId('product_id').value = obj.data.product_id;
					dojo.byId('relative_id').value = obj.data.product_id;
					dojo.byId('image_type').value = 1;
					dojo.byId('shipping_product_id').value = obj.data.product_id;
				}else{
					alert(obj.data.error);
					return false;
				}
			}
		);
	}
}

function startDateCal(elem){
	calendarStart.select(dojo.byId('product_start_date'), 'startDate', 'MM/dd/yyyy', dojo.byId('product_start_date').value);
}

function endDateCal(elem){
	calendarEnd.select(dojo.byId('product_end_date'), 'endDate', 'MM/dd/yyyy', dojo.byId('product_end_date').value);
}

function getSubCat(cat,level){

	switch(level){
        case 2: var url = SOCIAL_WEB_PATH + '/products/get-categories/l/2/c1/' + cat.value; break;
        case 3: var url = SOCIAL_WEB_PATH + '/products/get-categories/l/3/c1/' + dojo.byId('product_category1').value + '/c2/' + cat.value; break;
    }

    var category_select = dojo.byId('product_category'+level);
		category_select.options.length = 0;

	if(level == 2){
		dojo.byId('product_category3').options.length = 0;
		dojo.byId('category3').style.display='none';
	}

	navigate(url, CONTENT_TYPE_JSON,
		
		function(obj){

			if(obj.data == null || typeof(obj.data.categories)=='undefined'){
				//dojo.byId('category2').style.display='none';
				dojo.byId('category3').style.display='none';
			}else{
				dojo.byId('category'+ level).style.display='block';
			}
			
			var i = 1;
			
			category_select.options[0] = new Option('Select Sub Category', '');
			if(obj.data.categories != null) {
				dojo.forEach(obj.data.categories, function(category){
		      		category_select.options[i] = new Option(category.option_text, category.option_value);
		      		i++;
			    });
			}
		}
    );
	
}

function sizesColors(show){
	
	if(show==true){
		dojo.byId('qty').style.display='none';
		dojo.byId('sc-row-options').style.display='block';
		dojo.byId('qty_sc').style.display='block';
		dojo.byId('has_sc').value='1';
		addRow();
	}else{
		dojo.byId('qty').style.display='block';
		dojo.byId('sc-row-options').style.display='none';
		dojo.byId('qty_sc').style.display='none';
		dojo.byId('has_sc').value=0;
	}
}

function addRow(){

	var row 	= document.createElement('ul');
	var qty 	= document.createElement('li');
	var size 	= document.createElement('li');
	var color 	= document.createElement('li');
	var del 	= document.createElement('li');
	var atrid 	= document.createElement('li');
	
	qty.innerHTML   = '<input type="text" name="qty[]" style="width:50px;" />';
	size.innerHTML  = '<input type="text" name="size[]" style="width:106px;" />';
	color.innerHTML = '<input type="text" name="color[]" style="width:106px;" />';
	del.innerHTML   = '<img src="' + IMAGE_PATH + '/xicon.gif" title="Delete" alt="[X]" style="cursor:pointer;" onclick="deleteAttributeRow(this);" />';
	atrid.innerHTML = '<input type="hidden" name="attribute_id[]" value="" />';
	
	row.appendChild(qty);
	row.appendChild(size);
	row.appendChild(color);
	row.appendChild(del);
	row.appendChild(atrid);
	
	dojo.byId('attributes').appendChild(row);
}

function deleteAttributeRow(row){
	
	var attributes = row.parentNode.parentNode.parentNode;
		attributes.removeChild(row.parentNode.parentNode);
		
	var rows = attributes.getElementsByTagName('ul');

	if(rows.length == 1){
		sizesColors(false);
		return;
	}
}

function deleteExistingAttribute(row, attrib_id, product_id){
	
	if(confirm( 'Are you sure? You cannot undo this')){
		if(attrib_id > 0 && product_id > 0){
			

            navigate(SOCIAL_WEB_PATH + '/products/remove-attribute/attribute_id/' + attrib_id + '/product_id/' + product_id, CONTENT_TYPE_JSON,

				function(obj){
					if(obj.data == 1){
						deleteAttributeRow(row);
					}else{
						validate.message(row, 'Could not delete size/color');
					}
						
				}
			);
			
		}else{
			validate.message(row, 'Invalid data provided to delete sizes/color');
			return false;
		}
	}
	
	return;
}

function checkWeight(val){
	if(val == 'custom'){
		
		dojo.byId('display_package_weight').style.display='none';
		dojo.byId('display_custom_weight').style.display='block';
		
		dojo.byId('shipping_weight_pounds').value = '';
		dojo.byId('shipping_weight_ounces').value = '';
	}else{
		
		dojo.byId('display_package_weight').style.display='block';
		dojo.byId('display_custom_weight').style.display='none';
	}
}

function showFlatRate(flat_rate){
	if(flat_rate.checked){
		dojo.byId('display_shipping_usps').style.display='none';
		dojo.byId('shipping_flat_rate_value').disabled=false;
		dojo.byId('shipping_flat_rate_value').focus();
	}
}

function shippingIncluded(shippingIncluded){
	if(shippingIncluded.checked){
		dojo.byId('display_shipping_usps').style.display='none';
		dojo.byId('shipping_flat_rate_value').disabled=true;
	}
}

function showShippingUSPS(shipping_usps){
	if(shipping_usps.checked){
		dojo.byId('display_shipping_usps').style.display='block';
		dojo.byId('shipping_flat_rate_value').disabled=true;
	}
}

function validateShipping(shipping_form){

	return true;
	
	
	
	
	var shipping_methods = dojo.byId('shipping').shipping_method;
	
	var shipping_calc_checked = shipping_methods[0].checked;
	var shipping_flat_checked = shipping_methods[1].checked;
	var shipping_incl_checked = shipping_methods[2].checked;
	
	if(!shipping_calc_checked && !shipping_flat_checked && !shipping_incl_checked){
		alert('Please select a shipping option');
		return false;
	}
	
	if(shipping_calc_checked){
		
		document.getElementById('shipping_method_value').value='2';
		var pounds = dojo.byId('shipping_weight_pounds').value;
		var ounces = dojo.byId('shipping_weight_ounces').value;
		var width  = dojo.byId('shipping_size_width').value;
		var length = dojo.byId('shipping_size_length').value;
		var height = dojo.byId('shipping_size_height').value;
		
		if(width == '' || length == '' || height == ''){
			alert('Please enter valid dimensions');
			return false;
		}
		
		if(width == '0' || length == '0' || height == '0'){
			alert('Please enter valid dimensions');
			return false;
		}
		
		if(pounds == '' || length == '' || height == ''){
			alert('Please enter valid dimensions');
			return false;
		}
		
		if(pounds == '0' || length == '0' || height == '0'){
			alert('Please enter valid dimensions');
			return false;
		}
		
		if(isNaN(pounds)){
			alert('Please enter valid pounds');
			return false;
		}
		
		if(ounces !=''){
			if(isNaN(ounces)){
				alert('Please enter valid ounces');
				return false;
			}
		}
		
		if(isNaN(width) || isNaN(length) || isNaN(height)){
			alert('Please enter valid dimensions');
			return false;
		}
	}
	
	
	
	if(shipping_flat_checked){
		document.getElementById('shipping_method_value').value='1';
		if(dojo.byId('shipping_flat_rate_value').value==''){
			alert('Please enter a flat rate');
			return false;
		}
		if(isNaN(dojo.byId('shipping_flat_rate_value').value)){
			alert('Please enter a valid flat rate');
			return false;
		}
	}
	
	if(dojo.byId('relative_id').value == ''){
		alert('Please save product description form');
		return false;
	}
	
	return true;
}
