var InstIndex="";
var SymbIndex="";
var ExpIndex ="";
var TypeIndex="";
var StrikeIndex="";

var ExpiryDate="";
var OptionType="";
var InstrumentTypeVal="";
var SymbolVal="";
var StrikeVal="";
function fillSymbol(){
 if((document.chartfrm.instrument.options[document.chartfrm.instrument.selectedIndex].value)=="FUTIDX")
 {
//
// document.chartfrm.symbol.options.length =9;
  document.chartfrm.symbol.options.length =6;
//
 document.chartfrm.symbol.options[0].value ="";
 document.chartfrm.symbol.options[0].text ="--Select--";
 // display the first element in the select box which is SelectSymbol
 document.chartfrm.symbol.selectedIndex=0;

 document.chartfrm.symbol.options[1].value ="NIFTY";
 document.chartfrm.symbol.options[1].text ="NIFTY";
 document.chartfrm.symbol.options[2].value ="CNXIT";
 document.chartfrm.symbol.options[2].text ="CNXIT";
 document.chartfrm.symbol.options[3].value ="BANKNIFTY";
 document.chartfrm.symbol.options[3].text ="BANKNIFTY";
//
// document.chartfrm.symbol.options[4].value ="JUNIOR";
// document.chartfrm.symbol.options[4].text ="JUNIOR";
// document.chartfrm.symbol.options[5].value ="CNX100";
// document.chartfrm.symbol.options[5].text ="CNX100";
//
/*
 document.chartfrm.symbol.options[6].value ="NFTYMCAP50";
 document.chartfrm.symbol.options[6].text ="NIFTY MIDCAP 50";
 document.chartfrm.symbol.options[7].value ="MINIFTY";
 document.chartfrm.symbol.options[7].text ="MINI NIFTY";
 */
 document.chartfrm.symbol.options[4].value ="NFTYMCAP50";
 document.chartfrm.symbol.options[4].text ="NIFTY MIDCAP 50";
 document.chartfrm.symbol.options[5].value ="MINIFTY";
 document.chartfrm.symbol.options[5].text ="MINI NIFTY";
//
// document.chartfrm.symbol.options[8].value ="DEFTY";
// document.chartfrm.symbol.options[8].text ="DEFTY";
//


 document.chartfrm.expirydate.options.length =4;
 document.chartfrm.expirydate.selectedIndex=0;

 for (i=1;i<=3;i++)
  {
   document.chartfrm.expirydate.options[i].value = e[i];
   document.chartfrm.expirydate.options[i].text = e[i];
 }

	 document.chartfrm.optiontype.options.length =1;
 document.chartfrm.optiontype.options[0].value ="";
	 document.chartfrm.optiontype.options[0].text ="--Select--";
 document.chartfrm.optiontype.selectedIndex=0;

	 document.chartfrm.strikeprice.options.length =1;
 document.chartfrm.strikeprice.options[0].value ="";
	 document.chartfrm.strikeprice.options[0].text ="--Select--";
 document.chartfrm.strikeprice.selectedIndex=0;



 }
 else if((document.chartfrm.instrument.options[document.chartfrm.instrument.selectedIndex].value)=="FUTSTK")
 {

  val=1;
   for (i=1;i<s.length;i++)
  	  	 	 {
  	  	 	   //code to identify if symbol starts with NSE

  	  	 	   if(s[i].indexOf("NSE",0)==-1){
  	  	 	   val++;
  	  	 	   	 }

	        }

 // document.chartfrm.symbol.options.length =s.length;
  document.chartfrm.symbol.options.length = val;
	 document.chartfrm.symbol.options[0].value ="";
	 document.chartfrm.symbol.options[0].text ="--Select--";
	 document.chartfrm.symbol.selectedIndex=0;

	 /*for (i=1;i<s.length;i++)
	 	 {
	 	 document.chartfrm.symbol.options[i].value = s[i];
	 	 document.chartfrm.symbol.options[i].text = s[i];

     }*/

    val=1;
      for (i=1;i<s.length;i++)
	  	 	 {
	  	 	   //code to identify if symbol starts with NSE

	  	 	   if(s[i].indexOf("NSE",0)==-1){
	  	 	  document.chartfrm.symbol.options[val].value = s[i];
	  	 	 document.chartfrm.symbol.options[val].text = s[i];
	  	 	 val++;
	  	  	 }

	        }





document.chartfrm.expirydate.options.length =4;
 document.chartfrm.expirydate.selectedIndex=0;

 for (i=1;i<=3;i++)
  {
	 
   document.chartfrm.expirydate.options[i].value = e[i];
   document.chartfrm.expirydate.options[i].text = e[i];
 }

 document.chartfrm.optiontype.options.length =1;
	 document.chartfrm.optiontype.options[0].value ="";
	 document.chartfrm.optiontype.options[0].text ="--Select--";
	 document.chartfrm.optiontype.selectedIndex=0;

	 document.chartfrm.strikeprice.options.length =1;
	 document.chartfrm.strikeprice.options[0].value ="";
	 document.chartfrm.strikeprice.options[0].text ="--Select--";
 document.chartfrm.strikeprice.selectedIndex=0;

 }
 else if((document.chartfrm.instrument.options[document.chartfrm.instrument.selectedIndex].value)=="FUTINT")
  {

 //length has to only symbols which belongs to FUTINT


 var len =1;

  for (i=1;i<s.length;i++)
  	 	 {
  	 	   //code to identify if symbol starts with NSE

  	 	   if(s[i].indexOf("NSE",0)!=-1){
  	 	    len++;
  	 	 }

       }



  document.chartfrm.symbol.options.length =len;
 	 document.chartfrm.symbol.options[0].value ="";
 	 document.chartfrm.symbol.options[0].text ="--Select--";
 	 document.chartfrm.symbol.selectedIndex=0;

     len=1;
 	 for (i=1;i<s.length;i++)
 	 	 {
 	 	   //code to identify if symbol starts with NSE

 	 	   if(s[i].indexOf("NSE",0)!=-1){
 	 	  document.chartfrm.symbol.options[len].value = s[i];
 	 	 document.chartfrm.symbol.options[len].text = s[i];
 	 	 len++;
 	 	 }

       }

document.chartfrm.expirydate.options.length =e.length;
 document.chartfrm.expirydate.selectedIndex=0;

 for (i=1;i<e.length;i++)
  {
   document.chartfrm.expirydate.options[i].value = e[i];
   document.chartfrm.expirydate.options[i].text = e[i];
 }

  document.chartfrm.optiontype.options.length =1;
 	 document.chartfrm.optiontype.options[0].value ="";
 	 document.chartfrm.optiontype.options[0].text ="--Select--";
 	 document.chartfrm.optiontype.selectedIndex=0;

 	 document.chartfrm.strikeprice.options.length =1;
 	 document.chartfrm.strikeprice.options[0].value ="";
 	 document.chartfrm.strikeprice.options[0].text ="--Select--";
  document.chartfrm.strikeprice.selectedIndex=0;

 }
 else if((document.chartfrm.instrument.options[document.chartfrm.instrument.selectedIndex].value)=="OPTIDX")
 {
// document.chartfrm.symbol.options.length =7;
 document.chartfrm.symbol.options.length =6;
	 document.chartfrm.symbol.options[0].value ="";
	 document.chartfrm.symbol.options[0].text ="--Select--";
	 document.chartfrm.symbol.selectedIndex=0;

 document.chartfrm.symbol.options[1].value ="NIFTY";
 document.chartfrm.symbol.options[1].text ="NIFTY";
 document.chartfrm.symbol.options[2].value ="CNXIT";
 document.chartfrm.symbol.options[2].text ="CNXIT";
 document.chartfrm.symbol.options[3].value ="BANKNIFTY";
 document.chartfrm.symbol.options[3].text ="BANKNIFTY";



//
// document.chartfrm.symbol.options[4].value ="JUNIOR";
// document.chartfrm.symbol.options[4].text ="JUNIOR";
// document.chartfrm.symbol.options[5].value ="CNX100";
// document.chartfrm.symbol.options[5].text ="CNX100";
//document.chartfrm.symbol.options[6].value ="DEFTY";
// document.chartfrm.symbol.options[6].text ="DEFTY";
 document.chartfrm.symbol.options[4].value ="NFTYMCAP50";
 document.chartfrm.symbol.options[4].text ="NIFTY MIDCAP 50";
 document.chartfrm.symbol.options[5].value ="MINIFTY";
 document.chartfrm.symbol.options[5].text ="MINI NIFTY";
//

 document.chartfrm.expirydate.options.length =4;
  document.chartfrm.expirydate.selectedIndex=0;

  for (i=1;i<=3;i++)
   {
    document.chartfrm.expirydate.options[i].value = e[i];
    document.chartfrm.expirydate.options[i].text = e[i];
 }

 document.chartfrm.optiontype.options.length =3;
	 document.chartfrm.optiontype.options[0].value ="";
	 document.chartfrm.optiontype.options[0].text ="--Select--";
	 document.chartfrm.optiontype.options[1].value ="PE";
	 document.chartfrm.optiontype.options[1].text ="PE";
	 document.chartfrm.optiontype.options[2].value ="CE";
	 document.chartfrm.optiontype.options[2].text ="CE";

	 /*document.chartfrm.optiontype.options[3].value ="PE";
	 document.chartfrm.optiontype.options[3].text ="PE";
	 document.chartfrm.optiontype.options[4].value ="CE";
	 document.chartfrm.optiontype.options[4].text ="CE";*/
	 }
 else
 {

val=1;
   for (i=1;i<s.length;i++)
  	  	 	 {
  	  	 	   //code to identify if symbol starts with NSE

  	  	 	   if(s[i].indexOf("NSE",0)==-1){
  	  	 	   val++;
  	  	 	   	 }

	        }
 document.chartfrm.symbol.options.length =val;
 document.chartfrm.symbol.options[0].value ="";
	 document.chartfrm.symbol.options[0].text ="--Select--";
 document.chartfrm.symbol.selectedIndex=0;


 /*for (i=1;i<s.length;i++)
	 {
	 document.chartfrm.symbol.options[i].value = s[i];
	 document.chartfrm.symbol.options[i].text = s[i];

 }*/

   val=1;
        for (i=1;i<s.length;i++)
  	  	 	 {
  	  	 	   //code to identify if symbol starts with NSE

  	  	 	   if(s[i].indexOf("NSE",0)==-1){
  	  	 	  document.chartfrm.symbol.options[val].value = s[i];
  	  	 	 document.chartfrm.symbol.options[val].text = s[i];
  	  	 	 val++;
  	  	  	 }

	        }

 document.chartfrm.expirydate.options.length =4;
  document.chartfrm.expirydate.selectedIndex=0;

  for (i=1;i<=3;i++)
   {
    document.chartfrm.expirydate.options[i].value = e[i];
    document.chartfrm.expirydate.options[i].text = e[i];
 }

 document.chartfrm.optiontype.options.length =3;
 document.chartfrm.optiontype.options[0].value ="";
	 document.chartfrm.optiontype.options[0].text ="--Select--";
	 document.chartfrm.optiontype.options[1].value ="PE";
	 document.chartfrm.optiontype.options[1].text ="PE";
	 document.chartfrm.optiontype.options[2].value ="CE";
	 document.chartfrm.optiontype.options[2].text ="CE";

	 /*document.chartfrm.optiontype.options[3].value ="PE";
	 document.chartfrm.optiontype.options[3].text ="PE";
	 document.chartfrm.optiontype.options[4].value ="CE";
	 document.chartfrm.optiontype.options[4].text ="CE";*/


 }

fillStrikePrice();

}

function populate()
{
// Code to dynamically populate the drop downs

 	document.chartfrm.symbol.options.length =s.length;
 	document.chartfrm.expirydate.options.length = e.length;
 	document.chartfrm.strikeprice.options.length=1;


  // works in browsers which support HTML 4.0
 //document.chartfrm.instrument.disabled=true;

 	document.chartfrm.instrument.selectedIndex=0;
 	document.chartfrm.symbol.selectedIndex=0;
 	document.chartfrm.expirydate.selectedIndex=0;
 	document.chartfrm.optiontype.selectedIndex=0;
 	document.chartfrm.strikeprice.selectedIndex=0;


 for (i=1;i<s.length;i++)
 {
 	document.chartfrm.symbol.options[i].value = s[i];
 	document.chartfrm.symbol.options[i].text = s[i];
 }

 for (i=1;i<e.length;i++)
 {
  document.chartfrm.expirydate.options[i].value = e[i];
  document.chartfrm.expirydate.options[i].text = e[i];
 }

}


function fillStrikePrice()
{
 InstrumentTypeVal=document.chartfrm.instrument.options[document.chartfrm.instrument.selectedIndex].value;
 InstIndex=document.chartfrm.instrument.selectedIndex;
 SymbolVal=document.chartfrm.symbol.options[document.chartfrm.symbol.selectedIndex].value;
 SymbIndex=document.chartfrm.symbol.selectedIndex;

 ExpiryDate=document.chartfrm.expirydate.options[document.chartfrm.expirydate.selectedIndex].value;



 ExpIndex =document.chartfrm.expirydate.selectedIndex;

 if(InstrumentTypeVal=="FUTIDX"||InstrumentTypeVal=="FUTSTK"||InstrumentTypeVal=="FUTINT")
 {
 OptionType="-";
 TypeIndex=0;
 }
 else{
 OptionType=document.chartfrm.optiontype.options[document.chartfrm.optiontype.selectedIndex].value;
 TypeIndex=document.chartfrm.optiontype.selectedIndex;
 }



 if(InstIndex!=0)
 {
 if(SymbIndex!=0)
 {
 if(ExpIndex!=0)
 {

 if(TypeIndex!=0 && (InstrumentTypeVal!="FUTIDX"||InstrumentTypeVal!="FUTSTK"||InstrumentTypeVal!="FUTINT"))
 {
 unique_key=	InstrumentTypeVal+SymbolVal+ExpiryDate+OptionType

 if(!populateStrikePrice(unique_key))
 {
 alert("No Strike Price Is Available For The Selected Combination...Try Again");
			 document.chartfrm.strikeprice.options.length =1;
			 document.chartfrm.strikeprice.options[0].value ="";
			 document.chartfrm.strikeprice.options[0].text ="--Select--";
 document.chartfrm.strikeprice.selectedIndex=0;

 }
 }
 else if(TypeIndex==0 && (InstrumentTypeVal=="FUTIDX"||InstrumentTypeVal=="FUTSTK"||InstrumentTypeVal!="FUTINT"))
 {
 unique_key=InstrumentTypeVal+SymbolVal+ExpiryDate+OptionType;

 }

 }

 }

 }
}

function populateStrikePrice(unique_key)
{

	avail_flag=false;

 // since zero element text is SelectStrikePrice and we need to fill from first element
 // hence counter =1


 document.chartfrm.strikeprice.options.length =1;
 document.chartfrm.strikeprice.options[0].value ="";
 	 document.chartfrm.strikeprice.options[0].text ="--Select--";
 document.chartfrm.strikeprice.selectedIndex=0;

 counter=1;

	for (i=1;i<u.length;i++)
	 {
	 	 if(u[i].indexOf(unique_key) != -1)
	 	 {
	 	 avail_flag=true;

	 	 document.chartfrm.strikeprice.options.length = counter + 1;
 		 document.chartfrm.strikeprice.options[counter].value = u[i].substring(unique_key.length,u[i].length),u[i].substring(unique_key.length,u[i].length);
	 document.chartfrm.strikeprice.options[counter].text = u[i].substring(unique_key.length,u[i].length),u[i].substring(unique_key.length,u[i].length);
	 counter++;
	 	 }

	 }

 if(!avail_flag)
 {
 return false;
 }
 else
 {
 return true;

 }
}

 function finalcheck(unique_key)
 {
 avail_flag=false;

	for (i=1;i<u.length;i++)
	 {
		 	 if(u[i].indexOf(unique_key) != -1)
		 	 {
		 	 avail_flag=true;

				 break;
		 	 }

		 }

	 if(!avail_flag)
	 {
	 return false;
	 }
	 else
	 {
	 return true;

	 }


 }



 function validate()
 {

 //final check ..required since any change during final submission will
 // unnecessarily invoke the server side program.... only the combinations
 //available needs to be checked at the server end...
 // wrong combinations will increase unwarranted calls to controller servlet

		InstrumentTypeVal=document.chartfrm.instrument.options[document.chartfrm.instrument.selectedIndex].value;
		 InstIndex=document.chartfrm.instrument.selectedIndex;
		 if(InstIndex!=0)
	 {
	 SymbolVal=document.chartfrm.symbol.options[document.chartfrm.symbol.selectedIndex].value;
		 SymbIndex=document.chartfrm.symbol.selectedIndex;

		 if(SymbIndex!=0)
		 {
		 ExpiryDate=document.chartfrm.expirydate.options[document.chartfrm.expirydate.selectedIndex].value;
			 ExpIndex =document.chartfrm.expirydate.selectedIndex;

			 if(ExpIndex!=0)
			 {
			 if(InstrumentTypeVal=="FUTIDX"||InstrumentTypeVal=="FUTSTK"||InstrumentTypeVal=="FUTINT")
			 		 {
			 		 OptionType="-";
			 		 TypeIndex=0;
			 		 StrikePriceVal="-";
			 		 StrikeIndex=0;

			 		 if(SymbolVal.match(/&/g))
						 encryptedSymbolVal=SymbolVal.replace("&","%26");
			 		 //SymbolVal=SymbolVal.replace("&","%26");
			 		 unique_key=InstrumentTypeVal+encryptedSymbolVal+ExpiryDate+OptionType+StrikePriceVal;
			 		// alert("Final Submission :"+InstrumentTypeVal+SymbolVal+ExpiryDate+OptionType+StrikePriceVal);
			 		 document.chartfrm.key.value=unique_key;
						 document.chartfrm.symbol.value=SymbolVal;


			 		 return true; // to me made true
			 		 }
			 		 else{
			 		 OptionType=document.chartfrm.optiontype.options[document.chartfrm.optiontype.selectedIndex].value;
			 		 TypeIndex=document.chartfrm.optiontype.selectedIndex;

			 		 if(TypeIndex!=0)
			 			{
			 			 StrikePriceVal=document.chartfrm.strikeprice.options[document.chartfrm.strikeprice.selectedIndex].value;
						 StrikeIndex=document.chartfrm.strikeprice.selectedIndex;

						 if(StrikeIndex !=0)
						 {

						 unique_key=InstrumentTypeVal+SymbolVal+ExpiryDate+OptionType+StrikePriceVal;
						  //alert(unique_key);
						 if(finalcheck(unique_key))
						 {

						 if(SymbolVal.match(/&/g))
			 		 	SymbolVal=SymbolVal.replace("&","%26");
						 document.chartfrm.key.value=unique_key+".00";
						// alert( document.chartfrm.key.value=unique_key+".00");
						 document.chartfrm.symbol.value=SymbolVal;
						 return true; // to return true
						 }
						 else{
						 alert("No Strike Price Is Available For The Selected Combination...Try Again");
 							return false;
 							}

						 }
						 else
						 {
						 	alert("Please Select Strike Price Before You Proceed");
	 	 					return false;
						 }

			 			}
			 			else
			 			{
			 			alert("Please Select Option Type Before You Proceed");
	 	 			return false;

			 			}

			 }


			 }
			 else
			 {
			 alert("Please Select Expiry Date Before You Proceed");
	 	 return false;
			 }


		 }
		 else
		 {
		 	alert("Please Select The Symbol Before You Proceed");
	 return false;
		 }

	 }
	 else
	 {
	 alert("Please Select The Instrument Type Before You Proceed");
	 return false;
	 }


 }

