/* set Cookies for quotes*/

var TBnewsRSS = "";
var TBstockQuotes = "";
var TBzipCode = "";
var TBNewsRefreshInterval = "";
var TBStockRefreshInterval = "";
var TBWeatherRefreshInterval = "";
var TBTrafficRefreshInterval = "";

//checking and parsing old cookie
if (document.cookie && document.cookie != "") {
    var old_cookie_data = unescape(document.cookie);
    var old_split = old_cookie_data.split(";");
    for (i = 0; i < old_split.length; i++){

        if(old_split[i].indexOf("TBstockQuotes_cookie") > -1) {
            var TBstockQuotes_cookie = old_split[i];
            var TBstockQuotes_cookie_split = TBstockQuotes_cookie.split("=");
            TBstockQuotes = TBstockQuotes_cookie_split[1];
        }
        if(old_split[i].indexOf("TBStockRefreshInterval_cookie") > -1) {
            var TBStockRefreshInterval_cookie = old_split[i];
            var TBStockRefreshInterval_cookie_split = TBStockRefreshInterval_cookie.split("=");
            TBStockRefreshInterval = TBStockRefreshInterval_cookie_split[1];
        }

    }
    setDefaultValues();
} else {
    setDefaultValues();
}

function setDefaultValues() {
    if ((TBstockQuotes == null) || (TBstockQuotes == "")) {
        TBstockQuotes = "ONGC.NS,RELIANCE.NS,NTPC.NS,TCS.NS,INFOSYSTC.NS,WIPRO.NS";
    }
   if ((TBStockRefreshInterval == null) || (TBStockRefreshInterval == "")) {
        TBStockRefreshInterval = "1000";
    }
}

function autofill() {

    document.tinyPortal.TBstockQuotes.value = unescape(TBstockQuotes);
    document.tinyPortal.TBStockRefreshInterval.value = unescape(TBStockRefreshInterval);
}

function set_cookies() {
    TBstockQuotes = escape(document.tinyPortal.TBstockQuotes.value);
    TBStockRefreshInterval = escape(document.tinyPortal.TBStockRefreshInterval.value);

    var date = new Date();
    date.setTime(date.getTime()+(365*24*60*60*1000)); //1 year
    var expires = "; expires="+date.toGMTString();

    document.cookie = "TBstockQuotes_cookie=" + TBstockQuotes + expires + "; path=/";
    document.cookie = "TBStockRefreshInterval_cookie=" + TBStockRefreshInterval + expires + "; path=/";
}

function set_cookies1(quote) {
    TBstockQuotes = escape(quote);
    TBStockRefreshInterval = escape(document.tinyPortal.TBStockRefreshInterval.value);

    var date = new Date();
    date.setTime(date.getTime()+(365*24*60*60*1000)); //1 year
    var expires = "; expires="+date.toGMTString();

    document.cookie = "TBstockQuotes_cookie=" + TBstockQuotes + expires + "; path=/";
    document.cookie = "TBStockRefreshInterval_cookie=" + TBStockRefreshInterval + expires + "; path=/";
}





function checkRedio(var1,var2) {
	   if(document.getElementById(var1).checked==true) {
		 document.getElementById(var1).checked=true;
		 document.getElementById(var2).checked=false;
		}
	   if(document.getElementById(var2).checked==true) {
		 document.getElementById(var1).checked=true;
		 document.getElementById(var2).checked=false;
		}

	   if(var1=='MarketPrice')
	     document.getElementById("buyOrSellPrice").disabled=true;
	   else
	     document.getElementById("buyOrSellPrice").disabled=false;
     }
 function checkRediosellbuy(var1,var2) {
	   if(document.getElementById(var1).checked==true) {
		 document.getElementById(var1).checked=true;
		 document.getElementById(var2).checked=false;

document.getElementById("transactionType").value=var1;

		}
	   if(document.getElementById(var2).checked==true) {
		 document.getElementById(var1).checked=true;
		 document.getElementById(var2).checked=false;
document.getElementById("transactionType").value=var2;
		}


     }


	function numbersonly(e) {
	   var unicode=e.charCode? e.charCode : e.keyCode
	   if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
	      if (unicode<48||unicode>57) //if not a number
	          return false //disable key press
	      }
	  }

	function numbersonly1(e) {
	   var unicode=e.charCode? e.charCode : e.keyCode
	   if (unicode!=46 && unicode!=8){ //if the key isn't the backspace key (which we should allow)
	      if ((unicode<48||unicode>57)) //if not a number
	          return false //disable key press
	      }
	  }

	function onKeyPressBlockNumbers(e) {
		var key = window.event ? e.keyCode : e.which;
		var keychar = String.fromCharCode(key);
		reg = /\d/;
		return !reg.test(keychar);
	 }

	function confirmSubmit() {
var type=document.getElementById("transactionType").value;
var quote=document.getElementById("stockQuote").value;
var quantity=document.getElementById("buyOrSellQty").value;
		if(document.getElementById("buyOrSellQty").value=="") {
		  alert("Buy Quantity should not be Blank");
		  return false;
		  }
		else if(document.getElementById("LimitPrice").checked==true && document.getElementById("buyOrSellPrice").value=="") {
		  alert("Plz Enter Buy Limit Price");
		  return false;
		  }
		else {
			var agree=confirm("Are you sure to "+ type +" " +quantity+ " stock of "+quote );
			if (agree)
				return true ;
			else
				return false ;
		  }
	  }






			var setcolor=null;
			var quotes="";
			var TBstockQuote=TBstockQuotes;
			var textQuote="";
			function windowClose()
			   {
			   return window.close();
			   }


			function deleteSelectSymbolQuote(symbol)

				{


			var agree=confirm("Are you sure to want tp delete  "+ symbol  );
			var symbolcomma=symbol+",";
			var symbollast=","+symbol;

			if (agree)
				{
					if(TBstockQuote.indexOf(symbolcomma)>-1 && symbolcomma!="")
					{
					TBstockQuote=TBstockQuote.replace(symbolcomma,"");
					set_cookies1(TBstockQuote);
					}
					else
					{
					if(TBstockQuote.indexOf(symbollast)>-1 && symbollast!="")
					{
					TBstockQuote=TBstockQuote.replace(symbollast,"");
					set_cookies1(TBstockQuote);

					}
					else
					{
						alert("At least One Symbol Must be there")
					}

				}}
				else
				 return false;
				}



			function getSelectSymbolQuotes(symbolsString)
			   {
			   if(TBstockQuote.indexOf(textQuote)>-1 && textQuote!="") {
                 //alert("Already Exist");
				 }
			   else {
				 TBstockQuotes= textQuote + TBstockQuote;
				 }
				 set_cookies1(TBstockQuotes);
			   }

 			function getSelectSymbolQuote(checkid,symbol)
			   {
			   if(!document.getElementById(checkid).checked) {
				 if(TBstockQuote.indexOf(symbol)>-1 && quotes.indexOf(symbol)>-1) {
				   var sym=symbol+",";
     			   TBstockQuote=TBstockQuote.replace(sym,"");
				   quotes=quotes.replace(symbol,"");
				   }
                 }
               else {
			     if(TBstockQuote.indexOf(symbol)>-1) {
                    //alert("Already Exist");
					}
				 else {
				    quotes=quotes+symbol;
					TBstockQuote=symbol+","+TBstockQuote;
					}
                 }
			   }

 function blankText(textId) {
    document.getElementById(textId).value="";
 }

 function selectSymbol(checkid,symbolrowid)
  {
   if(!document.getElementById(checkid).checked) {
     document.getElementById(symbolrowid).bgColor="#cccccc";
     }
   else {
   document.getElementById(symbolrowid).bgColor="#0066FF";
     }
  }

 function findSymbol(symbolsString)
 {

  var symbolchar=document.getElementById("symbolcode").value;
  var symbolarray = symbolsString.split(",");

  if(setcolor!=null) {
    document.getElementById(setcolor).bgColor="#cccccc";
    document.getElementById(setcolor+"chk").checked=false;
   }
  if(symbolarray!=null && symbolarray.length>0) {
     for(var i=0; i<symbolarray.length; i++) {
	     var symbolstr=symbolarray[i].substring(0,symbolchar.length);
	     if(symbolstr.toLowerCase()==symbolchar.toLowerCase()) {
	       var symbol=symbolarray[i];
		   scrollToRow(symbol);
		   setcolor=symbol;
		   document.getElementById(symbol).bgColor="#0066FF";
		   document.getElementById(symbol+"chk").checked=true;
		   textQuote=symbol+",";
		   break;
		   }
	    }
    }
  return true;
 }

 function findSymbolByName(symbolNamesString,symbolsString)
 {

  var symbolnamechar=document.getElementById("symbolname").value;
  var symbolnamearray = symbolNamesString.split(",");
  var symbolarray = symbolsString.split(",");

  if(setcolor!=null) {
    document.getElementById(setcolor).bgColor="#cccccc";
    document.getElementById(setcolor+"chk").checked=false;
   }
  if(symbolnamearray!=null && symbolnamearray.length>0) {
     for(var i=0; i<symbolnamearray.length; i++) {
	     var symbolname=symbolnamearray[i].substring(0,symbolnamechar.length);
	     if(symbolname.toLowerCase()==symbolnamechar.toLowerCase()) {
	       var symbol=symbolarray[i];
		   scrollToRow(symbol);
		   setcolor=symbol;
		   document.getElementById(symbol).bgColor="#0066FF";
		   document.getElementById(symbol+"chk").checked=true;
		   document.getElementById("symbolcode").value=symbol;
		   textQuote=symbol+",";
		   break;
		   }
	    }
    }
  return true;
 }


  function scrollToRow(symid)
    {
    var row = document.getElementById(symid);
    row.scrollIntoView(false);
    }

	function showDiv()
    {
		element1 = document.getElementById('symbolshow');
	   if (element1.style.display == 'block')
		  {
		      element1.style.display = 'none';
		  }
		else
		  {
			  element1.style.display = 'block';
			  element1.style.position='absolute';
			  element1.style.left='20%';
			  element1.style.top='10%';
			  element1.style.width='520px';
			  element1.style.height='440px';
			  element1.style.border='none';
          }
    }



function SaveMarketWatch()
{
element1 = document.getElementById('marketwatch');
	   if (element1.style.display == 'block')
		  {
		      element1.style.display = 'none';
		  }
		else
		  {
			  element1.style.display = 'block';
			  element1.style.position='absolute';
			  element1.style.left='30%';
			  element1.style.top='30%';
			  element1.style.width='220px';
			  element1.style.height='150px';
			  element1.style.border='3px';
	document.marketwatch.marketwatch1.value = unescape(TBstockQuotes);
        document.marketwatch.method="post";


          }





}
function hidediv()
{
element1 = document.getElementById('marketwatch');
	   if (element1.style.display == 'block')
		  {
		      element1.style.display = 'none';
		  }
}

function setMarketWatch(symbol)
{

alert(symbol);
//alert(TBstockQuote);
//TBstockQuotes=symbol;
//alert(TBstockQuotes);
set_cookies1(symbol);

}


