
function checkform (form)
{
	
		if (form.first_name != null && (form.first_name.value == "" || form.first_name.value == "First Name") ) {
        alert( "Please enter first name.");
        form.first_name.focus();
        return false;
        }
		if (form.last_name != null && (form.last_name.value == "" || form.last_name.value == "Last Name") ) {
        alert( "Please enter last name.");
        form.last_name.focus();
        return false;
        }
		if (form.email != null && form.email.value == "") {
        alert( "Please enter email.");
        form.email.focus();
        return false;
        }
        if (form.title != null && form.title.value == "") {
        alert( "Please enter a title.");
        form.title.focus();
        return false;
        }
		if (form.subtitle != null && form.subtitle.value == "") {
        alert( "Please enter a summary.");
        form.subtitle.focus();
        return false;
        }
        if (form.content != null && form.content.value == "") {
        alert( "Please enter a content.");
        form.content.focus();
        return false;
        }
        if (form.release_date != null && form.release_date.value == "") {
        alert( "Please enter a release date.");
        form.release_date.focus();
        return false;
        }
        if (form.expiration_date != null && form.expiration_date.value == "") {
        alert( "Please enter an expiration date.");
        form.expiration_date.focus();
        return false;
        }
		if (form.post_date != null && form.post_date.value == "") {
        alert( "Please enter a post date.");
        form.post_date.focus();
        return false;
        }
		if (form.publish_date != null && form.publish_date.value == "") {
        alert( "Please enter a publish date.");
        form.publish_date.focus();
        return false;
        }
		if (form.summary != null && form.summary.value == "") {
        alert( "Please enter a summary.");
        form.summary.focus();
        return false;
        }
		
		if (form.release_date != null && form.expiration_date != null) {
			if (doDateCheck(form.release_date, form.expiration_date) == false ) {
			alert( "Release date must be before the expiration date.");
			return false;
			}
		}
		if (form.post_date != null && form.expiration_date != null) {
			if (doDateCheck(form.post_date, form.expiration_date) == false ) {
			alert( "Post date must be before the expiration date.");
			return false;
			}
		}
				
		if (form.product_name != null && form.product_name.value == "" ) {
        alert( "Please enter product name.");
        form.product_name.focus();
        return false;
        }
		
		if (form.sku != null && form.sku.value == "" ) {
        alert( "Please enter SKU.");
        form.sku.focus();
        return false;
        }
		
		if (form.dim_w != null && form.dim_w.value == "" ) {
        alert( "Please enter dimensions.");
        form.dim_w.focus();
        return false;
        }
		if (form.dim_w != null && form.dim_w.value != "") {
			form.dim_w.value = get_float(form.dim_w.value);
		}
		
		if (form.dim_h != null && form.dim_h.value == "" ) {
        alert( "Please enter dimensions.");
        form.dim_h.focus();
        return false;
        }
		if (form.dim_h != null && form.dim_h.value != "") {
			form.dim_h.value = get_float(form.dim_h.value);
		}
		
		if (form.dim_d != null && form.dim_d.value == "" ) {
        alert( "Please enter dimensions.");
        form.dim_d.focus();
        return false;
		}
		if (form.dim_d != null && form.dim_d.value != "") {
			form.dim_d.value = get_float(form.dim_d.value);
		}
		
		
		if (form.dealor_name != null && form.dealor_name.value == "") {
        alert( "Please enter dealor name.");
        form.dealor_name.focus();
        return false;
        }
		
		if (form.preference_y !=null && form.preference_y.checked == true && form.shipper_name != null && form.shipper_name.value == "") {
        alert( "Please enter shipper name.");
        form.shipper_name.focus();
        return false;
        }
		
		if (form.company_name != null && form.company_name.value == "") {
        alert( "Please enter vendor name.");
        form.company_name.focus();
        return false;
        }
		
		if (form.notification_email != null && form.notification_email.value == "") {
        alert( "Please enter notification email.");
        form.notification_email.focus();
        return false;
        }
		
		if (form.B_container_price != null && form.B_container_price.value == "") {
        alert( "Please enter B container price.");
        form.B_container_price.focus();
        return false;
        }
		
		if (form.B_container_price != null && form.B_container_price.value != "") {
			form.B_container_price.value = get_float(form.B_container_price.value);
		}
		
		if (form.C_container_price != null && form.C_container_price.value == "") {
        alert( "Please enter C container price.");
        form.C_container_price.focus();
        return false;
        }
		
		if (form.C_container_price != null && form.C_container_price.value != "") {
			form.C_container_price.value = get_float(form.C_container_price.value);
		}
		
		if (form.mt_commission_A != null && form.mt_commission_A.value == "") {
        alert( "Please enter level A MT commission.");
        form.mt_commission_A.focus();
        return false;
        }		
		
		if (form.mt_commission_A != null && form.mt_commission_A.value != "") {
			form.mt_commission_A.value = get_float(form.mt_commission_A.value);
		}
		
		if (form.mt_commission_B != null && form.mt_commission_B.value == "") {
        alert( "Please enter level B MT commission.");
        form.mt_commission_B.focus();
        return false;
        }		
		if (form.mt_commission_B != null && form.mt_commission_B.value != "") {
			form.mt_commission_B.value = get_float(form.mt_commission_B.value);
		}
		
		if (form.mt_commission_C != null && form.mt_commission_C.value == "") {
        alert( "Please enter level C MT commission.");
        form.mt_commission_C.focus();
        return false;
        }		
		if (form.mt_commission_C != null && form.mt_commission_C.value != "") {
			form.mt_commission_C.value = get_float(form.mt_commission_C.value);
		}
		
		if (form.headrep_commission_A != null && form.headrep_commission_A.value == "") {
        alert( "Please enter level A headrep commission.");
        form.headrep_commission_A.focus();
        return false;
        }		
		if (form.headrep_commission_A != null && form.headrep_commission_A.value != "") {
			form.headrep_commission_A.value = get_float(form.headrep_commission_A.value);
		}
		
		if (form.headrep_commission_B != null && form.headrep_commission_B.value == "") {
        alert( "Please enter level B headrep commission.");
        form.headrep_commission_B.focus();
        return false;
        }		
		if (form.headrep_commission_B != null && form.headrep_commission_B.value != "") {
			form.headrep_commission_B.value = get_float(form.headrep_commission_B.value);
		}
		
		if (form.headrep_commission_C != null && form.headrep_commission_C.value == "") {
        alert( "Please enter level C headrep commission.");
        form.headrep_commission_C.focus();
        return false;
        }		
		if (form.headrep_commission_C != null && form.headrep_commission_C.value != "") {
			form.headrep_commission_C.value = get_float(form.headrep_commission_C.value);
		}
		
		if (form.rep_commission_A != null && form.rep_commission_A.value == "") {
        alert( "Please enter level A rep commission.");
        form.rep_commission_A.focus();
        return false;
        }		
		if (form.rep_commission_A != null && form.rep_commission_A.value != "") {
			form.rep_commission_A.value = get_float(form.rep_commission_A.value);
		}
		
		if (form.rep_commission_B != null && form.rep_commission_B.value == "") {
        alert( "Please enter level B rep commission.");
        form.rep_commission_B.focus();
        return false;
        }		
		if (form.rep_commission_B != null && form.rep_commission_B.value != "") {
			form.rep_commission_B.value = get_float(form.rep_commission_B.value);
		}
		
		if (form.rep_commission_C != null && form.rep_commission_C.value == "") {
        alert( "Please enter level C rep commission.");
        form.rep_commission_C.focus();
        return false;
        }		
		if (form.rep_commission_C != null && form.rep_commission_C.value != "") {
			form.rep_commission_C.value = get_float(form.rep_commission_C.value);
		}
		
 		if (form.num_users != null && form.num_users.value != "") {
			var num_users1 = form.num_users.value;
			
			for (i=0; i<num_users1; i++) {
				first_name = document.getElementById("first_name"+i);
				if (first_name != null && (first_name.value == "" || first_name.value == "First Name")) {
				alert( "Please enter first name.");
				first_name.focus();
				return false;
				}
				last_name = document.getElementById("last_name"+i);
				if (last_name != null && (last_name.value == "" || last_name.value == "Last Name")) {
				alert( "Please enter last name.");
				last_name.focus();
				return false;
				}
				email = document.getElementById("email"+i);
				if (email != null && email.value == "") {
				alert( "Please enter email.");
				email.focus();
				return false;
				}
			}
		} 
		
		if (form.num_price_cats != null && form.num_price_cats.value != "") {
			var num_price_cats1 = form.num_price_cats.value;
			
			for (i=0; i<num_price_cats1; i++) {
				A_price = document.getElementById("A_price"+i);
				if (A_price != null && A_price.value == "") {
				alert( "Please enter level A price.");
				A_price.focus();
				return false;
				}
				if (A_price != null && A_price.value != "") {
				A_price.value = get_float(A_price.value);
				}
				
				B_price = document.getElementById("B_price"+i);
				if (B_price != null && B_price.value == "") {
				alert( "Please enter level B price.");
				B_price.focus();
				return false;
				}
				if (B_price != null && B_price.value != "") {
				B_price.value = get_float(B_price.value);
				}
				
				C_price = document.getElementById("C_price"+i);
				if (C_price != null && C_price.value == "") {
				alert( "Please enter level C price.");
				C_price.focus();
				return false;
				}
				if (C_price != null && C_price.value != "") {
				C_price.value = get_float(C_price.value);
				}
			}
		} 
		
		if (form.product_option_y != null && form.product_option_y.checked && form.num_options != null && form.num_options.value != "") {
			var num_options1 = form.num_options.value;
			
			for (i=0; i<num_options1; i++) {
				option_name = document.getElementById("option_name"+i);
				if (option_name != null && option_name.value == "") {
				alert( "Please enter option name.");
				option_name.focus();
				return false;
				}
				price_adj = document.getElementById("price_adj"+i);
				if (price_adj != null && price_adj.value == "") {
				alert( "Please enter price adjustment.");
				price_adj.focus();
				return false;
				}
				if (price_adj != null && price_adj.value != "") {
				price_adj.value = get_float(price_adj.value);
				}
			}
		} 

       return true;
}

function displayProductOption(target) {
	if (target.value == 'y') {
		document.getElementById('product_options').style.display = 'block';
	}
	else {
		document.getElementById('product_options').style.display = 'none';
	}
}

num_options = 1;

function removeOption( option_num ) {
	$('option_row_'+option_num).remove();
}

function removeUOption( option_id, option_num ) {
	$('u_option_row_'+option_num).remove();
	$('u_num_options').value--;
	
	//To delete the option from DB
	var opt = {
		method: 'post',
		postBody: 'option_id='+option_id,
		onSuccess: function(t) {
		},
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found. ');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		} 
	}

	new Ajax.Request( '/admin/products/delete_option.php', opt);
}

function addOption() {
	
	new Insertion.Before( 'add_another_option', '<tr id="option_row_'+num_options+'"><td><input id="option_name'+num_options+'" name="option_name[]" size="20" maxlength="255" type="text" />\n'+
'				</td><td><input id="price_adj'+num_options+'" name="price_adj[]" size="15" maxlength="15" type="text" />\n'+
'				<a href="javascript:removeOption('+num_options+')">[Remove]</a></td>\n'+
'			</tr>\n' );
	num_options ++;
	$('num_options').value = num_options;

}

num_price_cats = 1;

function removePriceCat( cat_num ) {
        $('cat_row1_'+cat_num).remove();
	$('cat_row2_'+cat_num).remove();
	$('cat_row3_'+cat_num).remove();
	$('add_another_cat').style.display='table-row';
}

function addPriceCat() {

        new Insertion.Before( 'add_another_cat', '<tr id="cat_row1_'+num_price_cats+'">\n'+
'				<td><select id="buying_category'+num_price_cats+'" name="buying_category[]"><option value="AV">AV</option><option value="furniture" selected="selected">furniture</option></select></td>\n'+
'				<td>Level A</td>\n'+
'				<td><input id="A_price'+num_price_cats+'" name="A_price[]" size="15" maxlength="15" type="text" /></td>\n'+
'                               </tr>\n'+
'				<tr id="cat_row2_'+num_price_cats+'">\n'+
'                               <td></td>\n'+
'                               <td>Level B</td>\n'+
'                               <td><input id="B_price'+num_price_cats+'" name="B_price[]" size="15" maxlength="15" type="text" /></td>\n'+
'                               </tr>\n'+
'                               <tr id="cat_row3_'+num_price_cats+'">\n'+
'                               <td><a href="javascript:removePriceCat('+num_price_cats+')">[Remove]</a></td>\n'+
'                               <td>Level C</td>\n'+
'                               <td><input id="C_price'+num_price_cats+'" name="C_price[]" size="15" maxlength="15" type="text" /></td>\n'+
'                       </tr>\n' );
        num_price_cats ++;
        $('num_price_cats').value = num_price_cats;
	$('add_another_cat').style.display='none';
}

num_photos = 1;

function removePhoto( photo_num ) {
	$('photo_row_'+photo_num).remove();
}

function removeUPhoto( photo_id, photo_num ) {
	$('photo_thumb'+photo_num).remove();
	
	//To delete the option from DB
	var opt = {
		method: 'post',
		postBody: 'photo_id='+photo_id,
		onSuccess: function(t) {
		},
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found. ');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		} 
	}

	new Ajax.Request( '/admin/products/delete_photo.php', opt); 
}

function addPhoto() {
	
	//new Insertion.Before( 'add_another_user','<div>tttt</div>');
	
	 new Insertion.Before( 'add_another_photo', '<div style="margin-top:10px" id="photo_row_'+num_photos+'">\n'+
'			<input type="file" id="photo'+num_photos+'" name="photo[]" size="30">\n'+
'		  <a href="javascript:removePhoto('+num_photos+')">[Remove]</a>\n'+
'			</div>\n' ); 
				
	num_photos ++;
	$('num_photos').value = num_photos;
}

function displayShipper(target) {
	if (target.value == 'y') {
		document.getElementById('shipper').style.display = 'block';
	}
	else {
		document.getElementById('shipper').style.display = 'none';
	}
}

function setBillingAddress(checkbox) {
		if (checkbox.checked == true) {
			b_address = $('b_address');
			b_city = $('b_city');
			b_state = $('b_state');
			b_zip = $('b_zip');
			b_address.value = $('p_address').value;
			b_city.value = $('p_city').value;
			b_state.value = $('p_state').value;
			b_zip.value = $('p_zip').value;
			if (b_address.value != 'Street') { 
				b_address.removeClassName('light');
			}
			if (b_city.value != 'City') { 
				b_city.removeClassName('light');
			}
			if (b_state.value != 'State') { 
				b_state.removeClassName('light');
			}
			if (b_zip.value != 'Zip') { 
				b_zip.removeClassName('light');
			}
		}
}

function setShippingAddress(checkbox) {
		if (checkbox.checked == true) {
			s_address = $('s_address');
			s_city = $('s_city');
			s_state = $('s_state');
			s_zip = $('s_zip');
			s_address.value = $('p_address').value;
			s_city.value = $('p_city').value;
			s_state.value = $('p_state').value;
			s_zip.value = $('p_zip').value;
			if (s_address.value != 'Street') { 
				s_address.removeClassName('light');
			}
			if (s_city.value != 'City') { 
				s_city.removeClassName('light');
			}
			if (s_state.value != 'State') { 
				s_state.removeClassName('light');
			}
			if (s_zip.value != 'Zip') { 
				s_zip.removeClassName('light');
			}
		}
}

function setCurrentShippingTab(current) {
	$('shipping_history_a').style.backgroundPosition='top left';
	$('shipping_history_span').style.backgroundPosition='top right';
	$('shipping_history_span').style.color='#666';
	if ( $('shipping_mt_comm_a') != null ) {
		$('shipping_mt_comm_a').style.backgroundPosition='top left';
		$('shipping_mt_comm_span').style.backgroundPosition='top right';
		$('shipping_mt_comm_span').style.color='#666';
	}
	
	if ( $('shipping_headrep_comm_a') != null ) {
		$('shipping_headrep_comm_a').style.backgroundPosition='top left';
		$('shipping_headrep_comm_span').style.backgroundPosition='top right';
		$('shipping_headrep_comm_span').style.color='#666';
	}
	
	if ( $('shipping_rep_comm_a') != null ) {
		$('shipping_rep_comm_a').style.backgroundPosition='top left';
		$('shipping_rep_comm_span').style.backgroundPosition='top right';
		$('shipping_rep_comm_span').style.color='#666';
	}
	
	$('shipping_history').hide();
	$('shipping_mt_comm').hide();
	$('shipping_headrep_comm').hide();
	$('shipping_rep_comm').hide();
	
	$(current+"_a").style.backgroundPosition='0 -125px';
	$(current+"_span").style.backgroundPosition='100% -125px';
	$(current+"_span").style.color='black';
	$(current).show();
	//document.getElementById('shipping_history_a').style.backgroundPosition='100% -125px'
}

function changeOrderQuantity(current) {
	orderId = current.id.substr(15);
	if (current.innerHTML == "Edit") {
		$("quantity_span"+orderId).hide();
		$("quantity_input"+orderId).show();
		current.innerHTML = "Save";
	}
	else {
		//alert( document.getElementById("quantity_input"+current.id.substr(15)).value );
		quantity = $("quantity_input"+orderId).value;
		quantity_shipped = $("quantity_shipped_span"+orderId).innerHTML;
		if ( parseInt(quantity) < parseInt(quantity_shipped) ) {
			alert("Order quantity " + quantity + " is less than shipped quantity " + quantity_shipped + " !");
			return;
		}
		$("quantity_span"+orderId).innerHTML = quantity;
		$("due"+orderId).innerHTML = quantity - quantity_shipped;
		$("quantity_span"+orderId).show();
		$("quantity_input"+orderId).hide();
		current.innerHTML = "Edit";
		
		//To update order quantity in DB
		var opt = {
			method: 'post',
			postBody: 'order_id='+orderId+'&quantity='+quantity+'&po_id='+$("po_id").value,
			onSuccess: function(t) {
				//alert(t.responseText);
				$("add_shipping_container").update(t.responseText);
			},
			on404: function(t) {
				alert('Error 404: location "' + t.statusText + '" was not found. ');
			},
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			} 
		}
	
		new Ajax.Request( '/admin/orders/update_order_quantity.php', opt);
	}
}

function leftNav(navNum) {

	if($('lNav'+navNum).style.display == 'none') { 
	
		document.images['lNav'+navNum+'_IMGSymbol'].src='/images/arraw_down.gif'; 
		Element.show('lNav'+navNum);
		} else {
		document.images['lNav'+navNum+'_IMGSymbol'].src='/images/arraw_right.gif'; 
		Element.hide('lNav'+navNum);
		}
}

function update_cat_col(cat_id, cat_name, vendor_id, vendor_name) {
	//alert('ha');
	//$('contentpage').hide();
	//$('prod_display_page').show();
	
	$('catcol_header').update(cat_name);

	var opt = {
		method: 'post',
		postBody: 'cat_id='+cat_id+'&vendor_id='+vendor_id+'&vendor_name='+vendor_name,
		onSuccess: function(t) {
			$('prod_list').update(t.responseText);
			// wipe out previous selection
			$('prodcol').update( "<div class='biggray'>choose a product from left</div>" );
		},
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found. ');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		} 
	}

	new Ajax.Request( '/ajax/update_cat_col.php', opt); 
	
}

function display_product (prod_id, vendor_name, vendor_id) {
	
	var opt = {
		method: 'post',
		postBody: 'prod_id='+prod_id+'&vendor_name='+vendor_name+'&vendor_id='+vendor_id,
		onSuccess: function(t) {
			$('prodcol').update(t.responseText);
		},
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found. ');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		} 
	}

	new Ajax.Request( '/ajax/display_product.php', opt); 
}

function submitShippingForm ( actionPage) {
	form = document.forms['fm_shipping_item'];
	
	if (form.shipment_date.value == "") {
		alert( "Please enter shipment date.");
		form.shipment_date.focus();
		return false;
	}
	
	if (!checkdate(form.shipment_date)) {
		form.shipment_date.focus();
		return false;
	}
	
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: Form.serialize(form),
		// Handle successful response
		asynchronous: false,
		onSuccess: function(t) {
			response = t.responseText;
			if ( response.substr(0,5) == "ERROR" ) {
				alert(t.responseText);
			}
			else {
				$("shippings").update(response.substr(1));
				$("shippingLook").hide();
				if ( response.substr(0,1) == "T" ) {
					$("po_status").update("shipped");
				}
				else {
					$("po_status").update("partially-shipped");
				}
			}
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}

	Element.hide('ei_fm_steps');
	Element.show('ei_fm_waiting');
	new Ajax.Request( actionPage, opt);
	Element.hide('ei_fm_waiting');
	Element.show('ei_fm_steps');

        opt = {
                // Use POST
                method: 'post',
                // Send this lovely data
                postBody: 'po_id='+form.po_id.value,
                // Handle successful response
                onSuccess: function(t) {
                        response = t.responseText;
                        if ( response.substr(0,5) == "ERROR" ) {
                                alert(t.responseText);
                        }
                        else {
                                $("orders").update(t.responseText);
                        }
                },
                // Handle 404
                on404: function(t) {
                        alert('Error 404: location "' + t.statusText + '" was not found.');
                },
                // Handle other errors
                onFailure: function(t) {
                        alert('Error ' + t.status + ' -- ' + t.statusText);
                }
        }

        new Ajax.Request( "/admin/orders/get_orders.php", opt);

	return true;
}

function changeCommissionPayment(comm_type, shipment_id) {
	$("commission_title").update(comm_type+" Commission");
	$("commission_amount").update("$"+$(comm_type+"Comm"+shipment_id).innerHTML);
	$("comm_type").value = comm_type;
	$("shipment_id").value = shipment_id;
	paid=$(comm_type+"Paid"+shipment_id).innerHTML;
	if (paid == "No" ) {
		$("paid_no").checked = true;
		$("paid_date").value = "";
		$("notes").value = "";
	}
	else {
		$("paid_yes").checked = true;
		$("paid_date").value = paid;
		$("notes").value = $(comm_type+"PaidInfo"+shipment_id).innerHTML;
	}
	$("commissionLook").show();
}

function submitCommissionForm ( actionPage) {
	form = document.forms['fm_commission_item'];
	
	if (form.paid_date.value == "" && form.paid_yes.checked) {
		alert( "Please enter pay date.");
		form.paid_date.focus();
		return false;
	} 
	
	if (!checkdate(form.paid_date)) {
		form.paid_date.focus();
		return false;
	} 
	
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: Form.serialize(form),
		// Handle successful response
		onSuccess: function(t) {
			response = t.responseText;
			if ( response.substr(0,5) == "ERROR" ) {
				alert(t.responseText);
			}
			else {
				$("shippings").update(t.responseText);
				comm_type = $("comm_type").value;
				if ( comm_type == "Rep" ) {
					current = "shipping_rep_comm";
				}
				else if ( comm_type == "Headrep" ) {
					current = "shipping_headrep_comm";
				}
				else {
					current = "shipping_mt_comm";
				}
				
				setCurrentShippingTab(current);
				$("commissionLook").hide();
			}
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}

	Element.hide('ei_fmc_steps');
	Element.show('ei_fmc_waiting');
	new Ajax.Request( actionPage, opt);
	Element.hide('ei_fmc_waiting');
	Element.show('ei_fmc_steps');

	return true;
}

function submitConfirmForm ( actionPage) {
	form = document.forms['fm_confirm_item'];
	
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: Form.serialize(form),
		// Handle successful response
		onSuccess: function(t) {
			response = t.responseText;
			if ( response.substr(0,5) == "ERROR" ) {
				alert(t.responseText);
			}
			else {
				$("orders").update(t.responseText);
				$("add_shipping_button").show();
				$("po_status").update("confirmed");
				$("confirmLook").hide();
				$("confirm_po_btn").hide();
			}
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}

	Element.hide('ei_fmd_steps');
	Element.show('ei_fmd_waiting');
	new Ajax.Request( actionPage, opt);
	Element.hide('ei_fmd_waiting');
	Element.show('ei_fmd_steps');

	return true;
}

function closePO ( po_id ) {
	
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: 'po_id='+po_id,
		// Handle successful response
		onSuccess: function(t) {
			response = t.responseText;
			if ( response.substr(0,5) == "ERROR" ) {
				alert(t.responseText);
			}
			else {
				$("po_status").update("closed");
				$("close_po").hide();
			}
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}

	new Ajax.Request( "/admin/orders/close_po.php", opt);
	
	return true;
}

function updateCart_Dearlor() {
	var dealor_id = $('fm_dealor_id').value;
	var buying_cat;
	var buying_level;
	
	var opt = {
		method: 'post',
		postBody: 'dealor_id='+dealor_id,
		asynchronous: false,
		onSuccess: function(t) {
			buying_cat = t.responseText;
			$('sp_buying_cat').update(t.responseText);
			$('fm_buying_cat').value = buying_cat;
		},
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found. ');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		} 
	}

	new Ajax.Request( '/ajax/getBuyingCat.php', opt); 

	var opt1 = {
		method: 'post',
		postBody: 'dealor_id='+dealor_id,
		onSuccess: function(t) {
			buying_level = t.responseText;
			$('fm_buying_level').selectedIndex =buying_level;

			//buying_level_array = new Array('A', 'B', 'C');
			//$('buying_level').value = buying_level_array[buying_level];
			updateCart_display();
		},
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found. ');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		} 
	}

	new Ajax.Request( '/ajax/getBuyingLevel.php', opt1); 

}

function updateCart_display() {
	var buying_cat = $('fm_buying_cat').value;
	var buying_level = $('fm_buying_level').value;
	//alert('buying_cat is 1'+buying_cat);
	//alert('buying_level is 1'+buying_level);
	//postBody: 'buying_cat='+buying_cat+'&buying_level='+buying_level,
	form = document.forms['viewcartform'];
	
	var opt = {
		method: 'post',
		postBody: Form.serialize(form),
		onSuccess: function(t) {
			$('cartContainer').update(t.responseText);
		},
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found. ');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		} 
	}

	new Ajax.Request( '/ajax/updateCart_display.php', opt); 
	
}

function cartCheckout() {
	rep_id = $('fm_rep_id').value;
	dealor_id = $('fm_dealor_id').value;
	buying_level = $('fm_buying_level').value;
	ready_for_checkout = $('ready_for_checkout').value;
	buying_cat_price = $('buying_cat_price').value;
	
	if ( dealor_id == '' ) {
		alert ('Please choose the dealor that you are placing order for.');
	} else if ( ready_for_checkout != 'yes' ) {
		alert ( 'This order can not be processed. \n\n You didn\'t reach the trigger price for one or more vendors. If you are a rep, please change buying level. If you are a dealor, please add more to your cart or contact your rep to change the buying level. ' );
	} else if ( buying_cat_price != 'ok' ) {
		alert ( 'This order can not be processed. \n\n One or more items dont have price for the buying category of the dealer.' );
	} else {
		window.location = "/order/checkout.php?rep_id="+rep_id+"&dealor_id="+dealor_id+"&buying_level="+buying_level+'&ready_for_checkout='+ready_for_checkout;
	}
}
