var product_id = 0;

function order_form_set_pid(tmp_id){
	product_id = tmp_id;
}

function order_form_validate(){
	try{
		var tmp_dtp_start = xhanch_get_obj("dtp_start");
		var tmp_txt_name = xhanch_get_obj("txt_name");
		var tmp_txa_title = xhanch_get_obj("txa_title");
		var tmp_txa_short_desc = xhanch_get_obj("txa_short_desc");
		var tmp_txa_long_desc = xhanch_get_obj("txa_long_desc");
		var tmp_txa_category = xhanch_get_obj("txa_category");
		var tmp_txt_tags = xhanch_get_obj("txt_tags");
		var tmp_txt_url = xhanch_get_obj("txt_url");
		var tmp_txa_directory = xhanch_get_obj("txa_directory");

		if(tmp_dtp_start.value == ""){
			tmp_dtp_start.focus();
			xhanch_catch_error("Please select a start date");
		}
		if(tmp_txt_name.value == ""){
			tmp_txt_name.focus();
			xhanch_catch_error("Please type in your name");
		}
		if(tmp_txa_title.value == ""){
			tmp_txa_title.focus();
			xhanch_catch_error("Please type in your title");
		}
		if(tmp_txa_short_desc.value == ""){
			tmp_txa_short_desc.focus();
			xhanch_catch_error("Please type in your short description");
		}
		if(tmp_txa_long_desc.value == ""){
			tmp_txa_long_desc.focus();
			xhanch_catch_error("Please type in your long description");
		}
		if(tmp_txa_category.value == ""){
			tmp_txa_category.focus();
			xhanch_catch_error("Please type in your category");
		}
		if(tmp_txt_tags.value == ""){
			tmp_txt_tags.focus();
			xhanch_catch_error("Please type in your tags");
		}
		if(tmp_txt_url.value == ""){
			tmp_txt_url.focus();
			xhanch_catch_error("Please type in your URL");
		}

		return true;
	}catch(e){
		alert(xhanch_get_error(e));
		return false;
	}
}

function order_form_reset(){
	var tmp_dtp_start = xhanch_get_obj("dtp_start");
	var tmp_txt_name = xhanch_get_obj("txt_name");
	var tmp_txa_title = xhanch_get_obj("txa_title");
	var tmp_txa_short_desc = xhanch_get_obj("txa_short_desc");
	var tmp_txa_long_desc = xhanch_get_obj("txa_long_desc");
	var tmp_txa_category = xhanch_get_obj("txa_category");
	var tmp_txt_tags = xhanch_get_obj("txt_tags");
	var tmp_txt_url = xhanch_get_obj("txt_url");
	var tmp_txa_directory = xhanch_get_obj("txa_directory");

	tmp_dtp_start.value = "";
	tmp_txt_name.value = "";
	tmp_txa_title.value = "";
	tmp_txa_short_desc.value = "";
	tmp_txa_long_desc.value = "";
	tmp_txa_category.value = "";
	tmp_txt_tags.value = "";
	tmp_txt_url.value = "";
	tmp_txa_directory.value = "";
}

function order_form_submit(){
	try{
		if(!order_form_validate())
			return;
		xhanch_ajax_disp_prg("order_form");
		
		var tmp_dtp_start = xhanch_get_obj("dtp_start");
		var tmp_txt_name = xhanch_get_obj("txt_name");
		var tmp_txa_title = xhanch_get_obj("txa_title");
		var tmp_txa_short_desc = xhanch_get_obj("txa_short_desc");
		var tmp_txa_long_desc = xhanch_get_obj("txa_long_desc");
		var tmp_txa_category = xhanch_get_obj("txa_category");
		var tmp_txt_tags = xhanch_get_obj("txt_tags");
		var tmp_txt_url = xhanch_get_obj("txt_url");
		var tmp_txa_directory = xhanch_get_obj("txa_directory");
		
		var xhanch_misc = {
			"exec": "order_form_submit_exec",
			"exec_done": "xhanch_ajax_disp_prg('order_form', false);"
		};
		
		var xhanch_dat_post = {
			"product_id": product_id.toString(),
			"dtp_start": encodeURIComponent(tmp_dtp_start.value),
			"txt_name": encodeURIComponent(tmp_txt_name.value),
			"txa_title": encodeURIComponent(tmp_txa_title.value),
			"txa_short_desc": encodeURIComponent(tmp_txa_short_desc.value),
			"txa_long_desc": encodeURIComponent(tmp_txa_long_desc.value),
			"txa_category": encodeURIComponent(tmp_txa_category.value),
			"txt_tags": encodeURIComponent(tmp_txt_tags.value),
			"txt_url": encodeURIComponent(tmp_txt_url.value),
			"txa_directory": encodeURIComponent(tmp_txa_directory.value)
		};

		if(!xhanch_ajax_query("order-submit.php", xhanch_dat_post, xhanch_misc))
			setTimeout("order_form_submit()", 1000);
	}catch(e){
		xhanch_ajax_hnd_err("order_form", e);
	}
}

function order_form_submit_exec(reply){
	try{
		xhanch_catch_error(xhanch_xml_msg(reply, "error"));
		
		var nds_res = xhanch_xml_nds_by_tag(reply, "info");
		if(nds_res.length > 0){
			//xhanch_ajax_disp_msg("order_form", "info", xhanch_xml_nd_val(nds_res[0]));
			order_form_reset();
			location.href = "shopping-cart.php";
		}else
			xhanch_ajax_disp_prg("order_form", false);
	}catch(e){
		xhanch_ajax_hnd_err("order_form", e);	
	}	
	location.href = "#flag_order_form";
}