/*
 * quickquote javascript infrastucture
 * 
 * sample quickquote data setup
 *
 * the following goes in your template, the quickquote 
 * javascript finds and reads it to configure dynamically:
 * 
 *	<span id='qq_data' style='display:none;'>
 *		<span class='qq_node'>
 *			<span class='qq_endpoint'>qq_8444</span>
 *			<span class='qq_productID'>8444</span>
 *		</span>
 *	</span>
 *
 * the above will, upon having qq_update triggered, query for prices for the current size selected
 *  for all product ids contained within htmlobjects with class 'qq_productID', and place the results
 *  in their corresponding qq_endpoint notes (assumed value is htmlobject ID).
 *
*/

/*
 * qq_update (triggered_instance_name)
 *
 * update all endpoints with new price information [by pid]
 *  for the specific instance of the quickquote widget named.
 *
*/

function qq_update(triggered_inst) {

	/* 
	 * the qq_data__.. quickquote data nodes are named to
	 *  correspond with the specific widget instance they support.
	 * 
	 * e.g,. "qq_data__c_category"
	 *
	 * note: see c_category.tpl code for details.
	 *
	*/
	var r_el = $('qq_data__' + triggered_inst);

	/* quickquote data package elements */
	var x = document.getElementsByClassName('qq_node', r_el);
	var q_afid = document.getElementsByClassName('qq_affiliateID', 
		r_el)[0].innerHTML;

	/* quickquote form elements */
	var q_w = parseFloat($F(triggered_inst + '_w')) + parseFloat($F(triggered_inst + '_w_x'));
	var q_h = parseFloat($F(triggered_inst + '_h')) + parseFloat($F(triggered_inst + '_h_x'));

	/* extract and store values in cookie for use on subsequent pages (category -> product, etc.) */
	var c_q_w = $F(triggered_inst + '_w');
	var c_q_wp = $F(triggered_inst + '_w_x');
	var c_q_h = $F(triggered_inst + '_h');
	var c_q_hp = $F(triggered_inst + '_h_x');
	
	nbb__create_cookie("c_q_w", c_q_w, 0);
	nbb__create_cookie("c_q_wp", c_q_wp, 0);
	nbb__create_cookie("c_q_h", c_q_h, 0);
	nbb__create_cookie("c_q_hp", c_q_hp, 0);
	/* carry on ... */

	var i;
	var q_pid_l = "";
	
	for(i=0;i<x.length;i++) {	
		var q_pid = document.getElementsByClassName('qq_productID', 
			x[i])[0].innerHTML;
		//alert("q_ep: " + q_ep + ", q_pid: " + q_pid + ", q_afid: " + q_afid);

		q_pid_l += q_pid + (i==(x.length-1)?"":",");
	} /* for */
	
	/* show loading div */
	nbb_aj_start();
	
	new Ajax.Request("/nbblib/ws/wslib.php?s=qq&f=pc&afid=" + q_afid +
		"&pid=" + q_pid_l + "&w=" + q_w + "&h=" + q_h + "&i=" + triggered_inst + 
		"&r=j&uid=" + new Date(), {
			method: 'post',
			onSuccess: function(t) {
				//alert("qq_resp: " + t.responseText);
				/* clear working div; we do it here in case something goes wrong below, but fails gracefully external to
				 *  this inline function;
				*/
				nbb_aj_stop();
				var qq_resp = eval('(' + t.responseText + ')');
				for(var j=0;j<qq_resp.qq_pc.q_pids.length;j++) {
					qq_set(qq_resp.qq_pc.q_pids[j].pid, 
					       qq_resp.qq_pc.q_pids[j].price, 
					       qq_resp.qq_pc.q_pids[j].inst);
				}				
			} /* onSuccess */
		}
	); /* Ajax.Request */
	
} /* qq_update */

function qq_set(pid, x, inst) {
	if (x == "" || x == "0" || x == "0.0" || x == "0.00") {
		x = "<span style='display:none;'>$0</span>*&nbsp;<a href='javascript:nbb_modal_dialog(\"/control/topic?console=1&p=pricing_popup\", 250, 100);'>[?]</a>";
	} else {
		x = Math.ceil(x);
		x = "$" + x;
		if (x.charAt(x.length-2) == '.') x += "0"; /* .20 instead of .2 */
		if (x.charAt(x.length-3) != '.') x += ".00"; /* 63.00 instead of 63 */
	}
	$(inst + "_" + pid).innerHTML = x;
}
