var order_ids = new Array();
var interval = "";
if(contract_type.match(/min/)){
	interval_details = new Array();
	interval_details = contract_type.split(/-/);
	interval = interval_details[0];
}
	

function loadOrders()
{
	var form = document.ocp_chart;
	if(orders.length > 0)
	{
		var sym_orders = new Array();
		for(var o=0; o<orders.length; o++){
			var details = orders[o];


			if(details[0] != symbol || details[1] != contract)
				continue;

			//alert(details[0] + details[1]);

			var date_time = details[2];			//order date
			var time = "";
			var date = "";
			

			var price = details[4];			//order price
			var filled = 0;

			if(details[3] != ""){			//fill_date
				date_time = details[3];
				filled = 1;
			}
			if(details[5] != ""){			//fill_price
				price = details[5];
				filled = 1;
			}

			if(date_time.match(/ /)){
				var date_fields = new Array();
				date_fields = date_time.split(/ /);
				date = date_fields[0];
				time = date_fields[1];
			}else{
				date = date_time;
			}

			sym_orders.push([date,time,price,filled,details[11],details[6]]);
		}

		var max_length = data.length;
		if(max_date_bars < max_length)
			max_length = max_date_bars;

		var orders_inserted = 0;

		for(var o=orders_inserted; o<sym_orders.length; o++)
		{
			var date = sym_orders[o][0];
			var time = sym_orders[o][1];

			var after_hours = 0;
			var before_hours = 0;

			for(var d=0; d<max_length; d++)
			{
				var record = data[d].split("|");

				var record_date_time = record[8].toString();
				var record_time = "";
				var record_date = "";

				if(record_date_time.match(/ /)){
					var record_date_fields = new Array();
					record_date_fields = record_date_time.split(/ /);
					record_date = record_date_fields[0];
					record_time = record_date_fields[1];
				}else{
					record_date = record_date_time;
				}


				//if(d == 0)
				//	alert("Comparing: "+record_date+" vs "+date);

				if(date >= record_date)
				{
					if(record_time)
					{
						var record_time_detail = record_time.split(/:/);
						var record_time_start = record_time;
						var record_time_end = record_time;

						var start_hr = parseInt(record_time_detail[0]) - time_zone_diff;
						var start_min = record_time_detail[1];
						if(start_hr < 10){ start_hr = "0"+start_hr; }

						if(parseInt(start_min) < parseInt(interval)){				//11 < 30
							//alert(record_time_detail[1]+" < "+interval);
							start_min = "00";

							record_time_start = start_hr + ":" + start_min;
							record_time_end = start_hr + ":" + interval;

						}else if(parseInt(start_min) >= parseInt(interval)){		//46 > 15
							//alert(parseInt(record_time_detail[1])+" >= "+parseInt(interval));
							start_min -= (parseInt(start_min)%interval) + interval;
							if(start_min < 10){ start_min = "0"+start_min; }
							record_time_start = start_hr + ":" + start_min;

							var end_hr = parseInt(start_hr);
							var end_min = parseInt(start_min) + parseInt(interval);
							if(end_min >= 60){
								end_min -= 60;
								end_hr++;
							}

							if(end_min < 10){ end_min = "0"+end_min; }
							if(end_hr < 10){ end_hr = "0"+end_hr; }
							record_time_end =  end_hr +":"+ end_min;
							//alert(record_time_start);
							//alert(record_time_end);
						}


						//alert("Locating: "+time+" between "+record_time_start+" and "+record_time_end);
						if(time > record_time_end){
							//alert("Nope, After Hours\nPlacing Order on next day");
							after_hours = 1;
						} else if(time <= record_time_start) {
							//alert("Nope, Before Period");	
							continue;
						}
					} else {
						if(time > close_time)
							after_hours = 1;
						else if(time < open_time)
							before_hours = 1;
					}

					var price = sym_orders[o][2];
					var filled = sym_orders[o][3];
					var type = sym_orders[o][4];
					var desc = sym_orders[o][5];
			
					//if(record_time)
					//	alert("Found order on "+date+" "+time+" at "+price);
					//else
					//	alert("Found order on "+date+" at "+price);

					//alert("title_bar: "+title_bar_height);
					//alert("chart_height: "+chart_height);
					//alert("max_high: "+max_high);
					//alert("min_low: "+min_low);
					//alert("tick_scale: "+tick_scale);

					var range = (max_high - min_low) / chart_height;

					var price_loc = chart_height + title_bar_height - ((price - min_low) * tick_scale);
					var date_loc = chart_width - ( days_offset * spacer ) - ( spacer * ( d + 2 ) );
					if(after_hours || date > record_date)
						date_loc += (spacer / 2);
					if(before_hours)
						date_loc -= (spacer / 2);
					//alert("Range: "+range+" * Price: "+price+" = "+price_loc);
					//alert("Price Loc: "+price_loc+"\nDate Loc: "+date_loc);
	
					var ord_img = new Image(12,12);
					
					if(filled){
						if(type > 0){
							ord_img.src = "/images/img_filled_long.gif";
						}else{
							ord_img.src = "/images/img_filled_short.gif";
							price_loc -= 10;			//Adjust for the img
						}
					} else {
						if(type > 0){
							ord_img.src = "/images/img_unfilled_long.gif";
						}else{
							ord_img.src = "/images/img_unfilled_short.gif";
							price_loc -= 10;			//Adjust for the img
						}
					}


					ord_img.id = "ord_"+o;
					ord_img.style.position = "absolute";
					ord_img.style.zIndex = 3+o;
					ord_img.style.top = price_loc + "px";
					ord_img.style.left = date_loc + "px";
					ord_img.style.visibility = "visible";
					ord_img.style.width = "10px";
					ord_img.style.heigth = "10px";
					var status = "open";
					if(filled)
						status = "filled";
					ord_img.title = date + " " + time + "\n" + desc + " @ " + price + "\nStatus: " + status;


					if(price_loc < chart_height && date_loc >= 0){
						order_ids.push(ord_img.id);
						document.getElementById('ocp_spacer').appendChild(ord_img);
					}

					orders_inserted++;
					d = max_length;
				}
			}
		}
	}
}

function showOrders(checked_status)
{
	for(var i=0; i<order_ids.length; i++){
		var id = order_ids[i];

		if(checked_status)
			document.getElementById(id).style.visibility = "hidden";
		else
			document.getElementById(id).style.visibility = "visible";
	}
}
