var IE = document.all?true:false;

function click_checkall(frm_nm)
{
	if(frm_nm == "index")
		var mytmpobj = document.fnoidx_frm;
	else
		var mytmpobj = document.fnostk_frm;

	var inst_type = mytmpobj.instrument_type.value;

	var see_all = mytmpobj.view_all.checked;
	if(see_all==true)
	{
		mytmpobj.sel_exp_date.disabled = true;
		mytmpobj.option_type.disabled = true;

		if(inst_type == "FUTIDX" || inst_type == "OPTIDX")
		{
			mytmpobj.sel_ind_price.disabled = true;
		}
		else
		{
			mytmpobj.sel_stk_price.disabled = true;
		}
	}
	else
	{
		if(inst_type=="FUTIDX" || inst_type == "FUTSTK")
		{
			mytmpobj.sel_exp_date.disabled = false;
			mytmpobj.option_type.disabled = true;
			if(inst_type == "FUTIDX" || inst_type == "OPTIDX")
			{
				mytmpobj.sel_ind_price.disabled = true;
			}
			else
			{
				mytmpobj.sel_stk_price.disabled = true;
			}
		}
		else
		{
			mytmpobj.sel_exp_date.disabled = false;
			mytmpobj.option_type.disabled = false;
			if(inst_type == "FUTIDX" || inst_type == "OPTIDX")
			{
				mytmpobj.sel_ind_price.disabled = false;
			}
			else
			{
				mytmpobj.sel_stk_price.disabled = false;
			}
		}
	}
}

function chk_instrument(frm_nm)
{
	if(frm_nm == "index")
		var mytmpobj = document.fnoidx_frm;
	else
		var mytmpobj = document.fnostk_frm;
	
	mytmpobj.view_all.checked=false;
	var inst_type = mytmpobj.instrument_type.value;
	if(inst_type=="FUTIDX" || inst_type == "FUTSTK")
	{
		mytmpobj.sel_exp_date.disabled = false;
		mytmpobj.option_type.disabled = true;
		if(inst_type=="FUTIDX")
			mytmpobj.sel_ind_price.disabled = true;
		else
			mytmpobj.sel_stk_price.disabled = true;
	}
	else
	{
		mytmpobj.sel_exp_date.disabled = false;
		mytmpobj.option_type.disabled = false;
		if(inst_type=="OPTIDX")
			mytmpobj.sel_ind_price.disabled = false;
		else
			mytmpobj.sel_stk_price.disabled = false;
	}
	
	if(inst_type=="FUTIDX" || inst_type == "OPTIDX")
	{
		var temp_symbol = mytmpobj.index_code.value;
		var res_symbol = temp_symbol.split("|");
		var symbol = res_symbol[1];
		var ind_id = res_symbol[0];
	}
	else
	{
		var symbol = mytmpobj.stock_code.value;
	}

	var inst_type = mytmpobj.instrument_type.value;

	if(inst_type != "")
	{
		if(!IE)
			var objXml = new XMLHttpRequest();
		else
			var objXml = new ActiveXObject("Microsoft.XMLHTTP");

		datafile = "/stocks/fno/query_tool/get_expdate.php?symbol="+symbol+"&inst_type="+inst_type;
		
		objXml.open("GET", datafile, true);
		objXml.setRequestHeader("Cache-Control","no-cache"); 
		objXml.onreadystatechange=function() 
		{
			if (objXml.readyState==4) 
			{
				if (objXml.status!=404)
				{
					if((objXml.responseText).length > 0)
					{
						if(IE)
						{
							var disp_str = "<select name='sel_exp_date' id='sel_exp_date' class='arial12black'><option value=''>Select</option>"+objXml.responseText+"</select>";
							mytmpobj.sel_exp_date.outerHTML = disp_str;
						}
						else
						{
							var disp_str = "<option value=''>Select</option>"+objXml.responseText;
							mytmpobj.sel_exp_date.innerHTML = disp_str;
						}
					}
				}
			}
		}
		objXml.send(null);
	}
}

function get_stprices()
{
	var inst_name = document.fnostk_frm.instrument_type.value;
	var option_type = document.fnostk_frm.option_type.value;
	if(inst_name == "OPTSTK")
	{
		var sc_id = document.fnostk_frm.stock_code.value;
		var sel_exp_date = document.fnostk_frm.sel_exp_date.value;
		if(sel_exp_date != "")
		{
//			document.fnostk_frm.sel_stk_price.innerHTML = "Loading...";

			if(!IE)
				var objXml = new XMLHttpRequest();
			else
				var objXml = new ActiveXObject("Microsoft.XMLHTTP");

			datafile = "/stocks/company_info/get_strikeprices.php?sc_id="+sc_id+"&exp_date="+sel_exp_date+"&instrument="+inst_name+"&option_type="+option_type;
			
			objXml.open("GET", datafile, true);
			objXml.setRequestHeader("Cache-Control","no-cache"); 
			objXml.onreadystatechange=function() 
			{
				if (objXml.readyState==4) 
				{
					if (objXml.status!=404)
					{
						if((objXml.responseText).length > 0)
						{
							if(IE)
							{
								var disp_str = "<select name='sel_stk_price' id='sel_stk_price' class='arial12black'><option value=''>Select</option>"+objXml.responseText+"</select>";
								document.fnostk_frm.sel_stk_price.outerHTML = disp_str;
							}
							else
							{
								var disp_str = "<option value=''>Select</option>"+objXml.responseText;
								document.fnostk_frm.sel_stk_price.innerHTML = disp_str;
							}
//							var disp_str = "<select name='sel_stk_price' id='sel_stk_price' class='arial12black'><option value=''>Select</option>"+objXml.responseText+"</select>";
//							document.fnostk_frm.sel_stk_price.innerHTML = disp_str;
						}
					}
				}
			}
			objXml.send(null);
		}
		else
		{
			alert("Please select a date!!!");
			document.getElementById("sel_exp_date").focus();
			return false;
		}
	}
	else
	{
		document.getElementById("option_type").disabled = true;
		document.getElementById("sel_strike_price").disabled = true;
	}
}

function get_indprices()
{
	var inst_name = document.fnoidx_frm.instrument_type.value;
	var option_type = document.fnoidx_frm.option_type.value;
	if(inst_name == "OPTIDX")
	{
		var temp_symbol = document.fnoidx_frm.index_code.value;
		var res_symbol = temp_symbol.split("|");
		var sc_id = res_symbol[1];
		var ind_id = res_symbol[0];
		var sel_exp_date = document.fnoidx_frm.sel_exp_date.value;

		if(sel_exp_date != "")
		{
			document.getElementById("ind_str_price").innerHTML = "Loading...";

			if(!IE)
				var objXml = new XMLHttpRequest();
			else
				var objXml = new ActiveXObject("Microsoft.XMLHTTP");

			datafile = "/stocks/company_info/get_strikeprices.php?sc_id="+sc_id+"&exp_date="+sel_exp_date+"&instrument="+inst_name+"&option_type="+option_type;
			
			objXml.open("GET", datafile, true);
			objXml.setRequestHeader("Cache-Control","no-cache"); 
			objXml.onreadystatechange=function() 
			{
				if (objXml.readyState==4) 
				{
					if (objXml.status!=404)
					{
						if((objXml.responseText).length > 0)
						{
							var disp_str = "<select name='sel_ind_price' id='sel_ind_price' class='arial12black'><option value=''>Select</option>"+objXml.responseText+"</select>";
							document.getElementById("ind_str_price").innerHTML = disp_str;
						}
					}
				}
			}
			objXml.send(null);
		}
		else
		{
			alert("Please select a date!!!");
			document.getElementById("sel_exp_date").focus();
			return false;
		}
	}
	else
	{
		document.getElementById("option_type").disabled = true;
		document.getElementById("sel_strike_price").disabled = true;
	}
}

function post_fnoidxfrm()
{
	selindex = document.fnoidx_frm['index_code'].selectedIndex;
	index_name = document.fnoidx_frm['index_code'].options[selindex].text;

	var see_all = document.fnoidx_frm.view_all.checked;
	//var symbol = document.fnoidx_frm.index_code.value;
	var temp_symbol = document.fnoidx_frm.index_code.value;
	var res_symbol = temp_symbol.split("|");
	var symbol = res_symbol[1];
	var ind_id = res_symbol[0];

	if(temp_symbol == "")
	{
		alert("Please select an Index!!!");
		document.fnoidx_frm.index_code.focus();
		return false;
	}

	var inst_name = document.fnoidx_frm.instrument_type.value;
	if(inst_name == "")
	{
		alert("Please select an Instrument!!!");
		document.fnoidx_frm.instrument_type.focus();
		return false;
	}

	var see_all = document.fnoidx_frm.view_all.checked;

	if(see_all==false)
	{
		var sel_exp_date = document.fnoidx_frm.sel_exp_date.value;

		var option_type = document.fnoidx_frm.option_type.value;
		var strike_price = document.getElementById("sel_ind_price").value;

		if(inst_name == "")
		{
			alert("Please select Instrument Type!!!");
			document.fnoidx_frm.instrument_type.focus();
			return false;
		}

		if(sel_exp_date == "")
		{
			alert("Please select Expiry Date!!!");
			document.fnoidx_frm.sel_exp_date.focus();
			return false;
		}

		if(inst_name == "OPTIDX" && option_type == "")
		{
			alert("Please select Option Type!!!");
			document.fnoidx_frm.option_type.focus();
			return false;
		}

		if(inst_name == "OPTIDX" && strike_price == "")
		{
			alert("Please select Strike Price!!!");
			document.fnoidx_frm.sel_strike_price.focus();
			return false;
		}
		else
		{
			document.fnoidx_frm.user_sel_price.value=strike_price;
		}
	}

	document.fnoidx_frm.see_all.value=see_all;
	document.fnoidx_frm.ind_id.value=ind_id;
	document.fnoidx_frm.sc_id.value=symbol;
	document.fnoidx_frm.short_name.value=index_name;
	document.fnoidx_frm.post_flag.value="true";
	
	if(see_all==true)
	{
		document.fnoidx_frm.target = "_blank";
		document.fnoidx_frm.action="/stocks/fno/query_tool/displayall.php";
	}
	else
	{
		document.fnoidx_frm.action="/stocks/fno/pricechart_ind.php";
	}
	
	document.fnoidx_frm.submit();
	return false;
}

function post_fnostkfrm()
{
	selindex = document.fnostk_frm['stock_code'].selectedIndex;
	sel_company = document.fnostk_frm['stock_code'].options[selindex].text;

	var see_all = document.fnostk_frm.view_all.checked;
	var symbol = document.fnostk_frm.stock_code.value;

	if(symbol == "")
	{
		alert("Please select a Company!!!");
		document.fnostk_frm.stock_code.focus();
		return false;
	}

	var inst_name = document.fnostk_frm.instrument_type.value;
	if(inst_name == "")
	{
		alert("Please select an Instrument!!!");
		document.fnostk_frm.instrument_type.focus();
		return false;
	}

	var see_all = document.fnostk_frm.view_all.checked;

	if(see_all==false)
	{
		var sel_exp_date = document.fnostk_frm.sel_exp_date.value;
		var option_type = document.fnostk_frm.option_type.value;
		var strike_price = document.fnostk_frm.sel_stk_price.value;

		if(inst_name == "")
		{
			alert("Please select Instrument Type!!!");
			document.fnostk_frm.instrument_type.focus();
			return false;
		}

		if(sel_exp_date == "")
		{
			alert("Please select Expiry Date!!!");
			document.fnostk_frm.sel_exp_date.focus();
			return false;
		}

		if(inst_name == "OPTIDX" && option_type == "")
		{
			alert("Please select Option Type!!!");
			document.fnostk_frm.option_type.focus();
			return false;
		}

		if(inst_name == "OPTIDX" && strike_price == "")
		{
			alert("Please select Strike Price!!!");
			document.fnostk_frm.sel_strike_price.focus();
			return false;
		}
		else
		{
			document.fnostk_frm.user_sel_price.value=strike_price;
		}
	}

	document.fnostk_frm.see_all.value=see_all;
	document.fnostk_frm.sc_id.value=symbol;
	document.fnostk_frm.short_name.value=sel_company;
	document.fnostk_frm.post_flag.value="true";

	if(see_all==true)
	{
		document.fnostk_frm.target = "_blank";
		document.fnostk_frm.action="/stocks/fno/query_tool/displayall.php";
	}
	else
	{
		document.fnostk_frm.action="/stocks/fno/pricechart.php";
	}

	document.fnostk_frm.submit();
	return false;
}

function post_stkadvfrm(search_type)
{
	if(search_type == "company")
	{
		var comp_name = document.stkadv_frm.comp_name.value;
		if(comp_name == "Search by Company" || comp_name == "")
		{
			alert("Please enter a Company Name!!!");
			document.stkadv_frm.comp_name.focus();
			return false;
		}

		if(comp_name.length < 3)
		{
			alert("Please enter minimum 3 characters to search");
			document.stkadv_frm.comp_name.focus();
			return false;
		}
		
		//document.stkadv_frm.action = "/stocks/advice/compsearch.php";
	}
	else if(search_type == "analyst")
	{
		var celeb_name = document.stkadv_frm.celeb_name.value;
		if(celeb_name == "")
		{
			alert("Please select an Analyst!!!");
			document.stkadv_frm.celeb_name.focus();
			return false;
		}
		document.stkadv_frm.sel_celeb.value = celeb_name;
		document.stkadv_frm.action = "/stocks/advice/display_more.php";
	}
	else if(search_type == "broker")
	{
		var brok_name = document.stkadv_frm.brok_name.value;
		if(brok_name == "")
		{
			alert("Please select a Broker!!!");
			document.stkadv_frm.brok_name.focus();
			return false;
		}
		document.stkadv_frm.sel_brok.value = brok_name;
		document.stkadv_frm.action = "/stocks/advice/display_more.php";
	}
	else if(search_type == "view_all")
	{
		document.stkadv_frm.action = "/stocks/advice/display_more.php";
	}

	document.stkadv_frm.search_flag.value = search_type;
	document.stkadv_frm.submit();
	return false;

}