function get_quantity_cb(output) {

	decoded_output = output.replace(/^:/, "");
	decoded_output = output.replace(/Quantity/, "");
	
	var quantity = JSON.parse(decoded_output);
	
	html_output = '<select name="quantity" id="quantity" class="headtextCopy" style="background-color:#F8FBF3; font-weight:normal;" disabled="disabled" onchange="get_qty(this.value);">';
	html_output += '<option value="0">Choose Quantity</option>'; 
	
	for(var i=0; i<quantity.length; i++) {
		
		html_output += '<option value="'+quantity[i]['products_id']+'">'+quantity[i]['quantity']+'</option>'; 
	}
	
	html_output += '</select>';
	
	document.getElementById('opt_quantity').innerHTML = html_output;
	
	enable_quantity();
	
	if(!SEL_SHIPPING) {
		disable_shipping_options();
	}
}


function get_quantity(categories_id) {
	
	x_get_quantity(categories_id,get_quantity_cb);
	
}


function get_quant() {
	
	html_output = '<select name="quantity" id="quantity" class="headtextCopy" style="background-color:#F8FBF3; font-weight:normal;" disabled="disabled">';
	html_output += '<option value="0">Choose Quantity</option>'; 
	html_output += '</select>';
	
	document.getElementById('opt_quantity').innerHTML = html_output;
	
}


function enable_quantity() {
	
	document.getElementById('quantity').disabled = false;

}


function enable_poster_size(cat_id) {

	var offset_sizes = new Array('11" x 17"','18" x 24"','24" x 36"','27" x 39"');
	if(cat_id==1021) { // glosspaper			
		var offset_values = new Array(9254,9258,9263,9269);	
		var turn = new Array(9263,9262,9269,9268);			
	} else if(cat_id==1022) { // glosscover
		var offset_values = new Array(9256,9260,9266,9272);
		var turn = new Array(9266,9265,9272,9271);			
	}
	
	html_output = '<select name="poster_width" id="poster_width" class="style21" style="color:#000000;" onchange="getTurnaround();">\n';
	html_output += '	<option value="0">--------</option>\n';
	
	for(var i=0; i<offset_sizes.length; i++) {
		if(loaded==offset_values[i]) {
			html_output += '	<option value="'+offset_values[i]+'" selected="selected">'+offset_sizes[i]+'</option>\n';
		} else
			html_output += '	<option value="'+offset_values[i]+'">'+offset_sizes[i]+'</option>\n';
	}

	html_output += '</select>\n';
	
	document.getElementById('div_size').innerHTML = html_output;
	
}


function getTurnaround(value) {
	var turn_id = document.getElementById('poster_width').value;
	
	if(turn_id==9263) {
		html_output = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';	
		html_output += '	<tr>';	
		html_output += '		<td valign="top" width="28%"><div align="left" class="style89">Turnaround</div></td>';	
		html_output += '		<td width="72%">';	
		html_output += '			<table width="100%" border="0" cellspacing="3" cellpadding="0">';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9263" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">3-5 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9262" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">6-8 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '			</table>';																								
		html_output += '		</td>';	
		html_output += '	</tr>';																					
		html_output += '</table>';	
	} else if(turn_id==9266) {
		html_output = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';	
		html_output += '	<tr>';	
		html_output += '		<td valign="top" width="28%"><div align="left" class="style89">Turnaround</div></td>';	
		html_output += '		<td width="72%">';	
		html_output += '			<table width="100%" border="0" cellspacing="3" cellpadding="0">';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9266" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">3-5 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9265" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">6-8 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '			</table>';																								
		html_output += '		</td>';	
		html_output += '	</tr>';																					
		html_output += '</table>';
	} else if(turn_id==9269) {
		html_output = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';	
		html_output += '	<tr>';	
		html_output += '		<td valign="top" width="28%"><div align="left" class="style89">Turnaround</div></td>';	
		html_output += '		<td width="72%">';	
		html_output += '			<table width="100%" border="0" cellspacing="3" cellpadding="0">';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9269" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">3-5 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9268" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">6-8 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '			</table>';																								
		html_output += '		</td>';	
		html_output += '	</tr>';																					
		html_output += '</table>';	
	} else if(turn_id==9272) {
		html_output = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';	
		html_output += '	<tr>';	
		html_output += '		<td valign="top" width="28%"><div align="left" class="style89">Turnaround</div></td>';	
		html_output += '		<td width="72%">';	
		html_output += '			<table width="100%" border="0" cellspacing="3" cellpadding="0">';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9272" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">3-5 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '				<tr>';	
		html_output += '					<td width="7%" class="style2" valign="top"><input id="turnaround" name="turnaround" type="radio" value="9271" onclick="enable_quantity(); get_quantity(this.value);" /></td>';	
		html_output += '					<td width="93%" valign="top"><div align="left" class="style6">6-8 Business Days *</div></td>';	
		html_output += '				</tr>';	
		html_output += '			</table>';																								
		html_output += '		</td>';	
		html_output += '	</tr>';																					
		html_output += '</table>';
	} else {
		html_output = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';	
		html_output += '	<tr>';	
		html_output += '		<td width="28%" height="25"><div align="left" class="style89">Turnaround</div></td>';	
		html_output += '		<td width="72%" style="text-align:left;">3-5 Business Days</td>';
		html_output += '	</tr>';																					
		html_output += '</table>';
		
		enable_quantity(turn_id); 
		get_quantity(turn_id);
		
	}
	
	document.getElementById('div_turnaround').innerHTML = html_output;
}


function clear_summary() {
	
	document.getElementById('summary_stock').value = '';
	document.getElementById('summary_size').value = '';
	document.getElementById('summary_quantity').value = '';
	document.getElementById('unit_price').value = '';
	document.getElementById('total_price').value = '';
	document.getElementById('handling_fee').value = '';
	document.getElementById('shipping_price').value = '';
	document.getElementById('subtotal_price').value = '';
	
}


function get_qty_cb(output) {
	
	decoded_output = output.replace(/^:/, "");
	
	var quantity = JSON.parse(decoded_output);
	
	quant = quantity['products_qty'];
	
	document.getElementById('poster_quantity').value = quant;
	 
	if(!SEL_SHIPPING) {
		enable_shipping_options();
	}
	update_price(document.getElementById('quantity').value);
	
}


function get_qty(qty_id) {
	
	x_get_price(qty_id,get_qty_cb);
	
}


function get_size(size_id) {
	
	switch(size_id) {
		case '10241':
			var size = '8.5" x 11"';
			break;
			
		case '9256':
			var size = '11" x 17"';
			break;
			
		case '9260':
			var size = '18" x 24"';
			break;
			
		case '9266':
			var size = '24" x 36"';
			break;
			
		case '9272':
			var size = '27" x 39"';
			break;
			
		case '9265':
			var size = '24" x 36"';
			break;
			
		case '9271':
			var size = '27" x 39"';
			break;
	
		case '10244':
			var size = '8.5" x 11"';
			break;
		
		case '9254':
			var size = '11" x 17"';
			break;
			
		case '9258':
			var size = '18" x 24"';
			break;
			
		case '9263':
			var size = '24" x 36"';
			break;
			
		case '9269':
			var size = '27" x 39"';
			break;
			
		case '9262':
			var size = '24" x 36"';
			break;
			
		case '9268':
			var size = '27" x 39"';
			break;
			
	}	
		
	document.getElementById('poster_dimension').value = size;
	
}


function enable_storepickup() {
	html_output = '<input name="shipping" id="shipping" value="storepickup_storepickup" type="hidden" /><input name="zip_code" id="zip_code" value="90025" type="hidden" />';

	document.getElementById('storepickup').innerHTML = html_output;
	
	disable_upsshipping();
	calculate_shipping();
	//update_price(document.product_listing_form.quantity.value);
}


function disable_storepickup() {
  document.getElementById('storepickup').innerHTML = '';
}


function enable_upsshipping() {
	disable_storepickup();
	
	document.getElementById('zip_code').disabled = false;
	document.getElementById('zip_code').value = '';
	document.getElementById('shipping').disabled = false;
	
	//clear_summary();
}


function disable_upsshipping() {
	
	document.getElementById('zip_code').disabled = true;
	document.getElementById('zip_code').value = '';
	document.getElementById('shipping').disabled = true;

	//clear_summary();

}


function enable_shipping_options() {
	
	var shipping_method = document.product_listing_form.shipping_method;
	
	for(var i=0; i<shipping_method.length; i++)
	{
		shipping_method[i].checked=false;
		shipping_method[i].disabled=false;
	} 
	
	disable_storepickup();
	disable_upsshipping()
	clear_summary();
	
}


function disable_shipping_options() {
	
	var shipping_method = document.product_listing_form.shipping_method;
	
	for(var i=0; i<shipping_method.length; i++)
	{
		shipping_method[i].checked=false;
		shipping_method[i].disabled=true;
	} 
	
	clear_summary();
	
}


function update_price_cb(output) {
	
	decoded_output = output.replace(/^:/, "");
	
	var products_info = JSON.parse(decoded_output);
	
	// compute and format the price
	nf = new NumberFormat(products_info['products_price']);
	nf.setPlaces(2);
	var product_price = nf.toFormatted();
	nf = null;
	
	// compute and format the unit price
	nf = new NumberFormat(products_info['price_per_qty']);
	nf.setPlaces(2);
	var unit_price = nf.toFormatted();
	nf = null;
	
	document.getElementById('summary_size').value = document.getElementById('poster_dimension').value;
	document.getElementById('summary_quantity').value = document.getElementById('poster_quantity').value+' pcs';
	document.getElementById('unit_price').value = unit_price;
	document.getElementById('total_price').value = product_price;  
	document.product_listing_form.products_id.value = products_info['products_id']; 
	
	// for calculate shipping
	allow_to_calculate();
	//calculate_shipping();
	
	var discount_price = (parseFloat(product_price*0.05));
		
	// compute and format the price
	nf = new NumberFormat(discount_price);
	nf.setPlaces(2);
	var discount_price = nf.toFormatted();
	nf = null;
	
	if(PROMO=='Y') {
		document.getElementById('discount_price').value = discount_price; 
	}
	
}


function update_price(qty_id) {	

	if(qty_id=="0")
		clear_price();
	else 
		
		x_get_price(qty_id, update_price_cb);
		
}


function add_to_cart()
{
	var myForm      = document.product_listing_form;
	var products_id = document.product_listing_form.products_id.value;
	var total_price = document.product_listing_form.total_price.value;
	
	if(products_id!="0" && (total_price!="0.00" || total_price!="0" || total_price>0))
			myForm.submit();
	else {
		alert("Please select a product before placing an order.");
	}
	
}


function allow_to_calculate()
{
	var products_id = document.product_listing_form.products_id.value;
		
	if(!products_id || products_id=="0") {
		alert("Please choose a first a product before\ncalculating the shipping cost");
	}
	
	return false;
}
