<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="Stock-O-Matic" author="Monika" author_email="monika.dasari@gmail.com" width="310" height="300" scrolling="true" scaling="false" category="finance" description="Stock-O-Matic displays almost real time updates of indian stock prices for 'both' NSE and BSE. Features- search for scrip codes, side by side comparison with previous day closing prices, automatic updates every ten minutes, simple and lightweight interface. Currently works well with Firefox and Google Chrome. (Full compatibility with IE is being worked upon). Note: Do not forget to put your 'browser name and version' while posting your rants of type 'this *** just doesn't work!' Disclaimer: This uses content from publicly available sources. Stock prices sometimes may not be accurate for the things which are not under my control." screenshot="http://hosting.gmodules.com/ig/gadgets/file/115964082140481649472/myBseNseStocks.png" thumbnail="http://hosting.gmodules.com/ig/gadgets/file/115964082140481649472/myBseNseStocks.png" directory_title="Stock-O-Matic">
<Require feature="settitle"/>
<Require feature="dynamic-height"/>
<Require feature="setprefs" /> 
<MayRequire type="browser" value="firefox" min_version="1.5"/>
</ModulePrefs>
<UserPref name="myScrips" display_name="Scrip Code" 
datatype="list" default_value="wipro|telco" required="true"/>
<Content type="html">
<![CDATA[
<html>
<head>
<style type="text/css">
.submitbutton 
{
border:1px solid #E1B167;
color:black;
background-color:#F3DD9A;
font-size:8pt;
margin-top:5px;
/*padding:3px 6px;*/
vertical-align:middle;
}
.submitbutton:hover 
{
/*background:white none repeat scroll 0 0;*/
cursor:pointer;
}
.stocks
{
width: 500px;
align:left;
margin: 0 auto;
background-color:#FAE5B0;
}
.stocks th
{
font-size:11px;
font-weight:bold;
font-family:arial;
color:black;
}
.stocks tr.even
{
background-color: #FFFFE5;
}

.stocks tr.odd
{
background-color: #FDF6E3;
}

/*.stocks tr:hover
{
background-color: #fff;
}*/

.stocks td
{
padding: 1px 1px;
border-bottom: 1px dotted #999;
text-align:center;
}

</style>
<script language="Javascript1.2">
//declare variables
var prefs = new _IG_Prefs(__MODULE_ID__);

var arrScrips = new Array();
var count = 0;
var interval = 600000;
var userScrips = prefs.getArray("myScrips");
_IG_RegisterOnloadHandler(createTable);
//getScrips();
setInterval("getScrips()", interval);

function createTable()
{
createObjects();
var newrow;
for (i=0; i<userScrips.length; i++)
{
//alert(arrScrips[userScrips[i]].code);
newrow = document.getElementById("tblScrips").insertRow(-1); count++;
newcell = newrow.insertCell(-1); newcell.innerHTML = '<B>' + arrScrips[userScrips[i]].code.toUpperCase() + '</B>';
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[userScrips[i]].nse_curr;
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[userScrips[i]].nse_prev;
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[userScrips[i]].bse_curr;
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[userScrips[i]].bse_prev;
//newrow.setAttribute('style', (count%2?"background-color:#CEEBE8":"background-color:#9FB9B9"));
newrow.setAttribute('class', (count%2?"even":"odd"));
newrow.setAttribute('id', arrScrips[userScrips[i]].code);
}
_IG_AdjustIFrameHeight();
getScrips();
}

function insertRow(code)
{
arrScrips[code] = new objScrip(code);
newrow = document.getElementById("tblScrips").insertRow(-1); count++;
newcell = newrow.insertCell(-1); newcell.innerHTML = '<B>' + arrScrips[code].code.toUpperCase() + '</B>';
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[code].nse_curr;
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[code].nse_prev;
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[code].bse_curr;
newcell = newrow.insertCell(-1); newcell.innerHTML = arrScrips[code].bse_prev;
//newrow.setAttribute('style', (count%2?"background-color:#CEEBE8":"background-color:#9FB9B9"));
newrow.setAttribute('class', (count%2?"even":"odd"));
newrow.setAttribute('id', arrScrips[code].code);

}

function createObjects()
{
//load arrScrips objects from user prefs
for (i=0; i<userScrips.length; i++)
{
arrItem = userScrips[i];
arrScrips[arrItem] = new objScrip(arrItem);
}

}

function deleteRows()
{
var tab = document.getElementById("tblScrips") ; 
//alert(tab.tBodies[0].rows.length); alert(userScrips.length);
if(tab.tBodies[0].rows.length > 1) 
{
for( i=tab.tBodies[0].rows.length; i > 1; i-- )
{
tab.tBodies[0].deleteRow(i-1) ;
}
count = 0;
}
}

//Function to display latest quotes of scrips.
function displayScrips(str, code){
var parsestring = new Array();

//---------------------------NSE-BSE Current Values--------------------
var re = new RegExp(/<(.*)>Current<\/td>\s*<(.*)><b>(\d+.\d+)/g);
parsestring=str.match(re);
if(parsestring == null){
arrScrips[code].nse_curr = "N.A";
arrScrips[code].bse_curr = "N.A";
arrScrips[code].nse_prev = "N.A";
arrScrips[code].bse_prev = "N.A";
} else {
var len = parsestring[0].length;
var i = parsestring[0].indexOf("<b>");
arrScrips[code].nse_curr = parsestring[0].substring(i+3, len);
arrScrips[code].bse_curr = RegExp.$3;

//---------------------------NSE-BSE Prev Close Values--------------------
re = new RegExp(/<(.*)>Prev Close<\/td>\s*<(.*)><b>(\d+.\d+)/g);
parsestring=str.match(re);
len = parsestring[0].length;
i = parsestring[0].indexOf("<b>");
arrScrips[code].nse_prev = parsestring[0].substring(i+3, len);
arrScrips[code].bse_prev = RegExp.$3;

//---------------------------Date and time--------------------
var re1 = new 
RegExp(/<(.*)>Date.*<\/td>\s*<(.*)><b>(\d+\/\d+\/\d+\s*\d+:\d+:\d+\s*\w+)/g);
parsestring=str.match(re1);
_gel('updateddate').innerHTML = "Just Updated On: " + RegExp.$3;
}

document.getElementById(code).cells[1].innerHTML = arrScrips[code].nse_curr;
document.getElementById(code).cells[2].innerHTML = arrScrips[code].nse_prev;
document.getElementById(code).cells[3].innerHTML = arrScrips[code].bse_curr;
document.getElementById(code).cells[4].innerHTML = arrScrips[code].bse_prev;
}


//--------------- functions ---------------------------
//Function to fetch content to get latest quotes of scrips
function getScrips() 
{
userScrips = prefs.getArray("myScrips");
//alert(userScrips.length);
for (i=0; i<userScrips.length; i++)
{
//alert(arrScrips[userScrips[i]].code);
_IG_FetchContent('http://content.icicidirect.com/research/topsearchquotes.asp?icicicode=' + arrScrips[userScrips[i]].code, _IG_Callback(displayScrips, arrScrips[userScrips[i]].code));
}
}

//Scrip object definition
function objScrip(code){
this.code = code;
this.nse_curr = 0;
this.bse_curr = 0;
this.nse_prev = 0;
this.bse_prev = 0;
}

function findQuote()
{
if((document.getElementById('Text1').value).trim() == '')
{
alert('Please enter the company name to search');
document.getElementById('Text1').focus();
return false;
}
_IG_FetchContent('http://content.icicidirect.com/findsymbolchart.asp?Symbol='+document.getElementById('Text1').value, _IG_Callback(displaySearchResult));

}

function displaySearchResult(responseText)
{
document.getElementById('tblScrips').style.display = "none";
document.getElementById('finddiv').style.display = "none";
document.getElementById('divflink').style.display='none';
document.getElementById('divhelp').style.display='none';
//document.getElementById('lblscrip').style.display = "none";

var symbols_text = new Array();
//var re = new RegExp('<td\s+class="content"\s*>([^<>]+)</td>',"g");
var re = /<td\s+class="content"\s*>([^<>]+)<\/td>/g;
//symbols_text=responseText.match(re);

var arrcodes = new Array();
var str_table = '';
while(symbols_text=re.exec(responseText))
{
arrcodes.push(symbols_text[1]);
}
//alert(arrcodes.length);
if(!arrcodes.length)
{
//document.getElementById('searcherror').innerHTML = "No records found.";
document.getElementById('finddiv').style.display = "none";
document.getElementById('divflink').style.display = "block";
//document.getElementById('lblscrip').style.display = "block";
str_table += '<div style="color:red;">Opps..It seems that there are no scrips listed for this code.<br/>Try modified search.</div><input class="submitbutton" type="button" onClick="goBack();" value="Back" name="Back" id="btnBack">';
}
else
{
str_table='<form name="frm_search_result"><table class="stocks" style="border:0px solid #000000;width:100%;font-size:10px; font-family:arial;color:black;text-align:left;" bgcolor="#FFFFFF" border="0"><caption style="font-size:11px;font-weight:bold;font-family:arial" >Search Results for "'+document.getElementById('Text1').value+'"</caption><tr><th>&nbsp;</th><th>Code</th><th>Company Name</th></tr>';
count1 = 0;
for(var i=0; i< arrcodes.length; i+=2)
{
count1++;
classname = (count1%2?"even":"odd");
//alert(classname);
str_table += '<tr class='+classname+'><td style="text-align:left;"><input type="checkbox" value="'+arrcodes[i]+'" name="chkbox"/></td><td style="text-align:left;">'+arrcodes[i]+'</td><td style="text-align:left;">'+arrcodes[i+1]+'</td></tr>';
}
str_table +='</table><input class="submitbutton" type="button" id="btnadd" value="Add" onClick="submitCode()">&nbsp;<input class="submitbutton" type="button" onClick="goBack();" value="Back" name="Back" id="btnBack"></form>';
}
//str_table += '<input class="submitbutton" style="margin-top:5px;" 

document.getElementById('search_result').style.display = "block";
document.getElementById('search_result').innerHTML = str_table;
}

function goBack()
{
document.getElementById('search_result').style.display = "none";
document.getElementById('tblScrips').style.display = "block";
//document.getElementById('lblscrip').style.display = "block";
document.getElementById('finddiv').style.display = "none";
document.getElementById('divflink').style.display = "block";
document.getElementById('divhelp').style.display='block';
}

function submitCode()
{
var selected_codes = prefs.getString("myScrips");
for(var i=0; i < document.frm_search_result.chkbox.length; i++)
{
if(document.frm_search_result.chkbox[i].checked)
{
selected_codes += "|" + document.frm_search_result.chkbox[i].value;
insertRow(document.frm_search_result.chkbox[i].value);
}
}
prefs.set("myScrips", selected_codes);
goBack();
getScrips();
}

String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}

function enableFind() {
	document.getElementById('finddiv').style.display='block';
	document.getElementById('divflink').style.display='none';
}
</SCRIPT>
</head>
<body>
<div style="display:none;" id="search_result"></div>
<TABLE class="stocks" id="tblScrips" style="border:0px solid #000000;width:100%;font-size:10px; font-family:arial;color:black;" 
cellspacing="1" cellpadding="2">
<caption style="font-size:11px;font-weight:bold;font-family:arial" id="updateddate">Just Updated On:</caption>
<TR>
<th>Scrip Code</th>
<th>NSE Current</th>
<th>NSE PrevClose</th>
<th>BSE Current</th>
<th>BSE PrevClose</th>
</TR>
</TABLE>
<div id="divflink"><input type="button" class="submitbutton" onClick="return enableFind();" value="Add New Scrip"/></div>
<div id="finddiv" style="display:none;" >
<input id="Text1" style="margin-top:5px;" size="10" type="text" value="" name="Symbol"/>
<input id="Submit1" class="submitbutton" style="margin-top:-5px;" type="button" onclick="findQuote();return false;" value="Find Scrip Code" name="subSymbol"/>
</div>
<div id="divhelp" style="font-family:arial;font-size:9px">
<br />
* Currently works on Firefox 2+, Google Chrome
<br />
* To delete scrips use Edit settings on a gadget menu bar.
</div>

</body>
</html>

]]>
</Content>
</Module> 

