var qoe = "";
var items = [];

function contbtn(pgm) {
	var datastring = "";
	var inputid = 0;
	
	jQuery("#productfields span.errormsg").html("");
	
	jQuery('input[name="product"]').each(function () {
		jQuery(this).removeClass("error");
		jQuery(this).attr("id", inputid);
		if (jQuery(this).val() != "") {
			datastring += "&product=" + jQuery(this).val() + "&inputid=" + inputid;
		}
		inputid += 1;
	});
	
	var datastring_chk = "task=chkproducts" + datastring;
	var datastring_sbt = "task=addproducts" + datastring;
	
	var err_prod, num_errors = 0;
	
	// KAB 20100217 - changed type from POST to GET for performance improvement
	jQuery.ajax({
		type: "GET",
		url: pgm,
		data: datastring_chk,
		cache: false,
		success: function (err_prods) {
			err_prod = err_prods.split(";");
			
			for (var i = 0, ii = err_prod.length; i < ii; i += 1) {
				if (err_prod[i] != "") {
					num_errors += 1;
					var inputfield = err_prod[i].split(":");
					jQuery("#" + inputfield[0]).addClass("error");//("border", "2px solid red");
				}
			}
			if (num_errors > 0) {
				jQuery("#productfields span.errormsg").html("<span lang=\"Y\" key=\"check_products\">Please check the highlighted product numbers.</span>");
				window.scrollTo(0, 0);
				tran_SwitchLang(curLang, "#productfields span.errormsg");
			} else {

				// KAB 20100217 - changed type from POST to GET for performance improvement
				jQuery.ajax({
					type: "GET",
					url: "go170w.pgm",
					data: datastring_sbt,
					cache: false,
					success: function (msg) {
						window.location = "go171w.pgm";
					}
				});
			}
		}
	});
}

function addrows(obj) {
	var row = jQuery(obj).parents("tr");
	if (jQuery(row).next("tr").attr("class") == "lastrow") {
		jQuery('#addmore').trigger("click");
	}	
}

function quickorder1(pgm) {
	jQuery('input[name="continue"]').click(function () {
		contbtn(pgm);
	});
	
	
	jQuery(".addnew").blur(function () {
		addrows(this);
	});
}

function quickorder2(va) {
	
	jQuery(".restriction").each(function () {
		if (jQuery.trim(jQuery(this).children("div").html()) != "") {
			jQuery(this).show(1);
		} else {
			jQuery(this).hide(1);
		}
	});
	
	checkyourprice();
	
	jQuery(".techdata").click(function () {
		var dsstk = jQuery(this).attr("dsstk");
		var obj = this;
		var imgsrc = jQuery(obj).attr("src");
		if (imgsrc.indexOf("Expand-Details.gif") != -1) {
			/*if (jQuery("#techdata_" + dsstk).html() == "") {
				var datastring = "task=techdata&dsstk=" + dsstk;
				
				jQuery.ajax({
					type: "POST",
					url: "gp100w.pgm",
					cache: false,
					data: datastring,
					success: function (rtnhtml) {
						jQuery("#techdata_" + dsstk).html(rtnhtml);
						jQuery("#techdata_" + dsstk).show();
						contentheight();
				}
				});
			} else {*/
			jQuery("#techdata_" + dsstk).show();
			contentheight();
			//}
			imgsrc = imgsrc.replace(/Expand-Details\.gif/g, "Hide-Details.gif");
			jQuery(obj).attr("src", imgsrc);
			jQuery(obj).attr("alttxt", "hidedtlbtn");
			btnimgalttxt("input", "hidedtlbtn");
		} else {
			jQuery("#techdata_" + dsstk).hide();
			if (va != "1") {
				document.getElementById("container").style.height = jQuery(".techdata").attr("containerh") + "px";
			}
			imgsrc = imgsrc.replace(/Hide-Details\.gif/g, "Expand-Details.gif");
			jQuery(obj).attr("src", imgsrc);
			jQuery(obj).attr("alttxt", "expanddtlbtn");
			btnimgalttxt("input", "expanddtlbtn");
		}

    });
	
	jQuery("#skipitembtn").click(function () {
		window.location.href = jQuery(this).attr("link");
	});
	
	if (jQuery(".pricebox").length === 0) {
		jQuery('input[name="addtocart"]').css("display", "none");
		jQuery('input[name="back"]').css("display", "");
	} else {
		var qtyboxes = 0;
		var pricebox = 0;
		var priceboxes = jQuery(".pricebox").length;
		jQuery(".pricebox").each(function () {
			qtyboxes += jQuery(this).find(".qtyfield").length;
			
			pricebox += 1;
			
			if (pricebox == priceboxes && qtyboxes === 0) {
				jQuery('input[name="addtocart"]').css("display", "none");
				jQuery('input[name="back"]').css("display", "");
			}
		});
	}
}

function makeajaxcall(numitems, i) {
	//alert(items.length);
	var item = "";
    var dsstk = "";
	var qoe_dtl;

	item = items.pop();
	
	//alert(item);
	
	if (jQuery("#" + item).html !== null) {
		//dsstk = jQuery(this).attr("id");
		dsstk = item;
		qoe_dtl = {
			"seq": 0,
			"count": i,
			"numitem": 0,
			"numitems": numitems,
			"i": i,
			"gotourl1": "go180w.pgm?qoe=Y",
			"gotourl2": ""
		};
		addcart(dsstk, qoe_dtl);
	}
}

function addcartqoe(viewall, seq, numitem, numitems) {
	if (typeof seq == "undefined") {
		seq = "0";
	}
	
	if (typeof numitem == "undefined") {
		numitem = 0;
	}
	
	if (typeof numitems == "undefined") {
		numitems = 0;
	}
	
	var dsstk = "";
	var qoe_dtl, success;
	var count = 0;
	
	//alert("numitems: " + numitems);
	if (viewall == 1) {
		qoe = "A";
		jQuery(".box2").each(function () {
			var item = jQuery(this).attr("name");
			item = item.replace(/td_/, "");
			items[count] = item;
			count += 1;
			if (count == numitems) {
				makeajaxcall(numitems, 1);
			}
		});
	} else {
		qoe = "S";
		dsstk = jQuery('input[name="qty"]').parents(".box").attr("id");
		qoe_dtl = {
			"seq": seq,
			"count": 0,
			"numitem": numitem,
			"numitems": numitems,
			"gotourl1": "go180w.pgm?qoe=Y",
			"gotourl2": "go171w.pgm?seq=" + seq
		};
		success = addcart(dsstk, qoe_dtl);		
	}
}


function chgcntry(obj) {
	var datastring = "task=chgcntr&udkey=" + jQuery(obj).val();
	jQuery.ajax({
		url: "gp100w.pgm",
		type: "GET",
		cache: false,
		data: datastring,
		success: function () {
			contbtn("go170w.pgm");
		}
	});
}


