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_txt_title = xhanch_get_obj("txt_title");
		var tmp_txt_anc_1 = xhanch_get_obj("txt_anc_1");
		var tmp_txt_url_1 = xhanch_get_obj("txt_url_1");
		var tmp_txt_anc_2 = xhanch_get_obj("txt_anc_2");
		var tmp_txt_url_2 = xhanch_get_obj("txt_url_2");
		var tmp_txt_category = xhanch_get_obj("txt_category");
		var tmp_txa_contents = xhanch_get_obj("txa_contents");
		var tmp_txa_comments = xhanch_get_obj("txa_comments");

		/*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_txt_title.value == ""){
			tmp_txt_title.focus();
			xhanch_catch_error("Please type in your title");
		}
		if(tmp_txt_anc_1.value == ""){
			tmp_txt_anc_1.focus();
			xhanch_catch_error("Please type in your anchor text 1");
		}
		if(tmp_txt_url_1.value == ""){
			tmp_txt_url_1.focus();
			xhanch_catch_error("Please type in your URL 1");
		}
		if(tmp_txt_anc_2.value == ""){
			tmp_txt_anc_2.focus();
			xhanch_catch_error("Please type in your anchor text 2");
		}
		if(tmp_txt_url_2.value == ""){
			tmp_txt_url_2.focus();
			xhanch_catch_error("Please type in your URL 2");
		}
		if(tmp_txt_category.value == ""){
			tmp_txt_category.focus();
			xhanch_catch_error("Please type in your category");
		}
		if(tmp_txa_contents.value == ""){
			tmp_txa_contents.focus();
			xhanch_catch_error("Please type in your contents");
		}

		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_txt_title = xhanch_get_obj("txt_title");
	var tmp_txt_anc_1 = xhanch_get_obj("txt_anc_1");
	var tmp_txt_url_1 = xhanch_get_obj("txt_url_1");
	var tmp_txt_anc_2 = xhanch_get_obj("txt_anc_2");
	var tmp_txt_url_2 = xhanch_get_obj("txt_url_2");
	var tmp_txt_category = xhanch_get_obj("txt_category");
	var tmp_txa_contents = xhanch_get_obj("txa_contents");
	var tmp_txa_comments = xhanch_get_obj("txa_comments");

	/*tmp_dtp_start.value = "";
	tmp_txt_name.value = "";*/
	tmp_txt_title.value = "";
	tmp_txt_anc_1.value = "";
	tmp_txt_url_1.value = "http://";
	tmp_txt_anc_2.value = "";
	tmp_txt_url_2.value = "http://";
	tmp_txt_category.value = "";
	tmp_txa_contents.value = "";
	tmp_txa_comments.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_txt_title = xhanch_get_obj("txt_title");
		var tmp_txt_anc_1 = xhanch_get_obj("txt_anc_1");
		var tmp_txt_url_1 = xhanch_get_obj("txt_url_1");
		var tmp_txt_anc_2 = xhanch_get_obj("txt_anc_2");
		var tmp_txt_url_2 = xhanch_get_obj("txt_url_2");
		var tmp_txt_category = xhanch_get_obj("txt_category");
		var tmp_txa_contents = xhanch_get_obj("txa_contents");
		var tmp_txa_comments = xhanch_get_obj("txa_comments");
		
		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),*/
			"txt_title": encodeURIComponent(tmp_txt_title.value),
			"txt_anc_1": encodeURIComponent(tmp_txt_anc_1.value),
			"txt_url_1": encodeURIComponent(tmp_txt_url_1.value),
			"txt_anc_2": encodeURIComponent(tmp_txt_anc_2.value),
			"txt_url_2": encodeURIComponent(tmp_txt_url_2.value),
			"txt_category": encodeURIComponent(tmp_txt_category.value),
			"txa_contents": encodeURIComponent(tmp_txa_contents.value),
			"txa_comments": encodeURIComponent(tmp_txa_comments.value)
		};

		if(!xhanch_ajax_query("order-submit-sh.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";
}
