window.onload=handleOnLoad;
window.onresize=handleOnResize;

var delay = 120; 
var fObj; 

// Set visual effect
function setEffect(obj)
{
var target = obj.childNodes;
var tstarget;
for (i=0;i < target.length;i++)
{ 
  if(target[i].nodeName == 'P' && target[i].className == 'in2')
   {
    tstarget = target[i].childNodes;
    tstarget[0].style.color= "A1D1EB";	//SPAN tag for Name
    tstarget[2].style.color= "A3A3A3";  //SPAN tag for Message
    tstarget[4].style.color= "CCCCCC";  //SPAN tag for Time Stamp
    break;
   }
  else if(target[i].nodeName == 'P' && target[i].className == 'out2')
   {
   tstarget = target[i].childNodes;
   tstarget[0].style.color= "2D5D78"; //SPAN tag for Name
   tstarget[2].style.color= "808080"; //SPAN tag for Message
   tstarget[4].style.color= "666666"; //SPAN tag for Time Stamp
   break;
   }
   else if(target[i].nodeName == 'DIV')
   {
   tstarget = target[i].childNodes;
   tstarget[0].style.color= "3D3D3D"; //'a' tag for 'show older/newer message' button 
   }
}	//end for
fObj = obj;
var msgId=obj.getAttribute('href'); 
document.location=msgId;
setTimeout("resetEffect()", delay);
}

// No effect for outbox
function openOutbox(obj)
{
var msgId=obj.getAttribute('href'); 
document.location=msgId;
}

//resetEffect function
function resetEffect() 
{ 
 var target = fObj.childNodes;
 var tstarget;
 for (i=0;i < target.length;i++)
 { 
  if(target[i].nodeName == 'P' && target[i].className == 'in2')
   {
    tstarget = target[i].childNodes;
    tstarget[0].style.color= "009FEB";	//SPAN tag for Name
    tstarget[2].style.color= "000000";  //SPAN tag for Message
    tstarget[4].style.color= "999999";  //SPAN tag for Time Stamp
    break;
   }
  else if(target[i].nodeName == 'P' && target[i].className == 'out2')
   {
   tstarget = target[i].childNodes;
   tstarget[0].style.color= "009FEB";	//SPAN tag for Name
   tstarget[2].style.color= "FFFFFF";   //SPAN tag for Message
   tstarget[4].style.color= "999999";   //SPAN tag for Time Stamp
   break;
   }
   else if(target[i].nodeName == 'DIV')
   {
   tstarget = target[i].childNodes;
   tstarget[0].style.color= "FFFFFF"; //'a' tag for 'show older/newer message' button 
   }
 }	//end for
 
var msgId=fObj.getAttribute('href'); 
var statusId = msgId.substring(5);
var id = document.getElementById(statusId);
if (id)
	{
	id.style.visibility="hidden";
	}

}

// On Load function
function handleOnLoad()
{
hideLinks();
hideDiv();
readData();
}

function showDiv()
{
var msgroot = document.getElementById('msgroot');
msgroot.style.visibility="visible";
}

function hideDiv()
{
var msgroot = document.getElementById('msgroot');
msgroot.style.visibility="hidden";
}

// On resize function
function handleOnResize()
{
changeStyle();
hitBottom();
}

// Change stylesheet
function changeStyle() 
{
var w=screen.width;
var h=screen.height;
if(w>h) 
{
document.styleSheets[0].disabled = true;
document.styleSheets[1].disabled = false;
}
else
{
document.styleSheets[0].disabled = false;
document.styleSheets[1].disabled = true;
}
}

// Hit bottom
function hitBottom()
{
// Scroll to bottom
window.scrollTo(0, document.body.scrollHeight);
}

// Enable display of view older/ newer links  
function showLinks()
{
var next = document.getElementById("viewnext");
if ( next != null )
{
next.setAttribute('class', "viewall");
next.style.outline = 'none';
document.getElementById('btnnext').style.visibility = 'visible'; 
}

var previous = document.getElementById("viewprevious");
if ( previous != null )
{
previous.setAttribute('class', "viewall");
previous.style.outline = 'none';
document.getElementById('btnprevious').style.visibility = 'visible';
}
}  

// Disable display of view older/ newer links  
function hideLinks()
{
var next = document.getElementById("viewnext");
if ( next != null )
{
next.setAttribute('class', "viewall_hidden");
document.getElementById('btnnext').style.visibility = 'hidden';
}

var previous = document.getElementById("viewprevious");
if ( previous != null )
{
previous.setAttribute('class', "viewall_hidden");
document.getElementById('btnprevious').style.visibility = 'hidden';
}
}  

// Discover smileys (Phase 1)
var pattern = /:|;|\)|_|-|\xa4|\x3d|&gt;|&lt;|\|/;
function smiley1(text1)
{
var result1=pattern.test(text1);
return result1;
}

// Discover smileys (Phase 2)
var set1 = [ 
{image:"#open#'smile'#close#",regex:/:\)|:-\)/g }, 
{image:"#open#'bigsmile'#close#",regex:/:D|:-D/g },
{image:"#open#'wink'#close#",regex:/;\)|;-\)/g }, 
{image:"#open#'grin'#close#",regex:/;D|;-D/g }, 
{image:"#open#'unhappy'#close#",regex:/:\(|:-\(/g }, 
{image:"#open#'tongue'#close#",regex:/:P|:p|:-P|:-p/g },
{image:"#open#'cry'#close#",regex:/;\(|;-\(|:\'\(/g },
{image:"#open#'neutral'#close#",regex:/:\||:-\|/g },
{image:"#open#'wondering'#close#",regex:/:-\\|:-\//g },
{image:"#open#'suprised'#close#",regex:/:O|:o|:-O|:-o/g },
{image:"#open#'kissing'#close#",regex:/:\*|:-\*/g },
{image:"#open#'irritated'#close#",regex:/:X|:-X|:x|:-x/g },
{image:"#open#'sarcastic'#close#",regex:/:&gt;|:-&gt;/g },
{image:"#open#'verycool'#close#",regex:/B\)|B-\)/g },
{image:"#open#'eyebrows'#close#",regex:/%\)|%-\)/g },
{image:"#open#'angry'#close#",regex:/:@|:-@/g },
{image:"#open#'mad'#close#",regex:/;&gt;|;-&gt;/g },
{image:"#open#'evil'#close#",regex:/&gt;\)|&gt;-\)/g },
{image:"#open#'nerd'#close#",regex:/8\)|8-\)/g },
{image:"#open#'heart'#close#",regex:/&lt;3/g },
{image:"#open#'coffee'#close#",regex:/c\[_\]|C\[_\]/g },
{image:"#open#'connive'#close#",regex:/&gt;\]|&gt;-\]/g },
{image:"#open#'denial'#close#",regex:/:S|:-S/g },
{image:"#open#'flirt'#close#",regex:/\;}|\;-}/g },
{image:"#open#'frustrated'#close#",regex:/&gt;O|&gt;o|&gt;-O|&gt;-o/g },
{image:"#open#'gum'#close#",regex:/:&amp;|:-&amp;/g },
{image:"#open#'idea'#close#",regex:/C\x3d|c\x3d/g }, 
{image:"#open#'innocent'#close#",regex:/O:\)|o:\)|O:-\)|o:-\)/g },
{image:"#open#'laugh'#close#",regex:/\|D|\|-D/g },
{image:"#open#'party'#close#",regex:/&lt;\|:\)|&lt;\|:-\)/g },
{image:"#open#'run'#close#",regex:/\xa4K|\xa4k/g },
{image:"#open#'silly'#close#",regex:/8-P|8-p/g },
{image:"#open#'sleep'#close#",regex:/\|-\)/g },
{image:"#open#'surprised'#close#",regex:/&lt;:O|&lt;:o|&lt;:-O|&lt;:-o/g },
];

function smiley2(text2)
{
//If smiley combination is part of html code, don’t parse for smiley
text2=text2.replace("src=","#src#");
var j=set1.length-1;
do
{
text2=text2.replace(set1[j].regex,set1[j].image);
var result2=pattern.test(text2);
if(result2==0)
break;
}
while (j--)
var imgStr = "<img class=";
text2=text2.replace(/#open#/g, imgStr);
text2=text2.replace(/#close#/g, " src='transparent.png'/>");
//Revert back source string.
text2=text2.replace("#src#","src=");
return text2;
}

var lines;
var lineCount;
var intervalId;
var lastChild;
var startTime;
function readData()
{
lines = [];
lineCount = 0;
intervalId = "";
lastChild = "";
startTime = new Date();
var datFile = new XMLHttpRequest();
datFile.onreadystatechange = function()
{
if (datFile.readyState==4) 
{
   var allText = datFile.responseText;
   lines = allText.split('\n');
   intervalId = setInterval("populateMsgs();", 1);
}
}
datFile.open("GET", "file://C:/system/data/cv.dat", true);
datFile.send(null);
}

// Format of data file.
// IN(0)/OUT(100) | LTR(0)/RTL(100) | ID | NAME | RECEIVED(0)/NEW(100) | ATTACHMENT(100) | TS | BODY |
// IN(0)/OUT(100) | LTR(0)/RTL(100) | ID | NAME | SENT(0)/OUTBOX(100)  | ATTACHMENT(100) | TS | BODY |
function populateMsgs()
{
var msgroot = document.getElementById('msgroot');
var line = lines[lineCount];

if(line.replace(/\s/g,"") == "")
	{ 
	return;
	} 

var fields = [];
fields = line.split(0x200C);
var msgRow;
if ( fields[0] == 100 )
	{
	// outgoing
	if ( fields[4] == 100 )
		msgRow = createOutgoing(fields[1], fields[2], fields[3], fields[5], fields[6], fields[7], fields[8])
	else
		msgRow = createSent(fields[1], fields[2], fields[3], fields[5], fields[6], fields[7], fields[8])
	}
else
	{
	// incoming
	if ( fields[4] == 100 )
		msgRow = createNew(fields[1], fields[2], fields[3], fields[5], fields[6], fields[7], fields[8])
	else
		msgRow = createIncoming(fields[1], fields[2], fields[3], fields[5], fields[6], fields[7], fields[8])
	}

msgroot.appendChild(msgRow);

lineCount++;
if ( lineCount == ( lines.length - 1 ) )
	{
	handleLoadComplete();
	}
}

function handleLoadComplete()
{
changeStyle();
clearInterval(intervalId);
setTimeout("simulateLoadComplete();", 5);
}

function simulateLoadComplete()
{
var loadcomplete=document.getElementById("loadcomplete");
var clickevent=document.createEvent("MouseEvents");
clickevent.initEvent("click", true, true);
loadcomplete.dispatchEvent(clickevent);
showDiv();
showLinks();
hitBottom();
}

var attachrtl = "<img class='attachrtl' src=\'transparent.png\'>";
var attachltr = "<img class='attachltr' src=\'transparent.png\'>";

function createNew(rtl, msgid, party, clip, ts, body, direction)
{
var str = "<table class='msgbody'><tr><td dir=\'#DIRECTION\' ><div onclick=\"setEffect(this)\" href=View/#ID class='#DCLASS'><b class='in1'></b><p class='in2'><span class=\'#NCLASS\' >#NAME:</span>&nbsp;<span name='msg' class='msg'>#MSG</span>&nbsp;&nbsp;<span class=\'#TCLASS\'>#TS</span></p><b class='in3'></b></div></td><td id='#STATUSID' class='status'><div class='unread'/></td></tr></table>";
var attach;
str = str.replace("#STATUSID", msgid);
if ( direction == 100 )
	{
	str = str.replace("#DIRECTION", "rtl"); 
	str = str.replace("#NCLASS", "name_rtl");
	var tmpClass = "ts_in_rtl";
	if( rtl != 100 )
	    {
	    tmpClass =tmpClass + "' dir='ltr";
	    }
        str = str.replace("#TCLASS", tmpClass);
	attach = attachrtl;
	var phNo = /^[+]/;
	if (phNo.test(party))
	  {
	  party = party.replace("+","");
	  var tmpParty = "<span dir='";
	  if ( rtl == 100 )
	       {
	       tmpParty = tmpParty + "ltr'>+</span>" + party;
	       }
	  else
	       {
	       tmpParty = tmpParty + "rtl'>+</span>" + party;
	       }
	  party = tmpParty;
	  }
	}
else
	{
	str = str.replace("#DIRECTION", "ltr");
	str = str.replace("#NCLASS", "name_ltr");
	str = str.replace("#TCLASS", "ts_in");
	attach = attachltr;
	}
	
if ( rtl == 100 )
	{
	str = str.replace("#DCLASS", "in_rtl");
	}
else	
	{
	str = str.replace("#DCLASS", "in_ltr");
	}
str = str.replace("#ID", msgid);
str = str.replace("#NAME", party);
str = str.replace("#TS", ts);
if ( clip == 100 )
	{
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else if ( clip == 110 )
	{
	if((rtl!=100) && smiley1(body))
           body=smiley2(body);
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else
	{
	if((rtl!=100) && smiley1(body))
           body=smiley2(body);
	str = str.replace("#MSG", body);
	}
var row = document.createElement();
row.innerHTML = str;
return row;
}

function createOutgoing(rtl, msgid, party, clip, ts, body, direction)
{
var str = "<table class='msgbody'><tr><td class='status'><div class='outgoing'/></td><td dir=\'#DIRECTION\' ><div onClick=\"openOutbox(this)\" href=Outbox class='#DCLASS'><b class='out1'></b><p class='out2'><span class=\'#NCLASS\' >#NAME</span>&nbsp;<span name='msg' class='msg'>#MSG</span>&nbsp;&nbsp;<span style='visibility:hidden' class=\'#TCLASS\'>#TS</span></p><b class='out3'></b></div></td></tr></table>";
var attach;
if ( direction == 100 )
	{
	str = str.replace("#DIRECTION", "rtl");
	str = str.replace("#NCLASS", "name_outbox_rtl");
	str = str.replace("#TCLASS", "ts_outbox_rtl");	
	attach = attachrtl;
	}
else
	{
	str = str.replace("#DIRECTION", "ltr");
	str = str.replace("#NCLASS", "name_outbox_ltr");
	str = str.replace("#TCLASS", "ts_outbox");	
	attach = attachltr;
	}
if ( rtl == 100 )
	{
	str = str.replace("#DCLASS", "outbox_rtl");
	}
else	
	{
	str = str.replace("#DCLASS", "outbox_ltr");
	}

str = str.replace("#NAME", party);
str = str.replace("#TS", ts);
if ( clip == 100 )
	{
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else if ( clip == 110 )
	{
	if((rtl!=100) && smiley1(body))
           body=smiley2(body);
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else
	{
	if((rtl!=100) && smiley1(body))
            body=smiley2(body);
	str = str.replace("#MSG", body);
	}
var row = document.createElement();
row.innerHTML = str;
return row;
}

function createIncoming(rtl, msgid, party, clip, ts, body, direction)
{
var str = "<table class='msgbody'><tr><td dir=\'#DIRECTION\' ><div onclick=\"setEffect(this)\" href=View/#ID class='#DCLASS'><b class='in1'></b><p class='in2'><span class=\'#NCLASS\' >#NAME:</span>&nbsp;<span name='msg' class='msg'>#MSG</span>&nbsp;&nbsp;<span class=\'#TCLASS\'>#TS</span></p><b class='in3'></b></div></td><td class='status'></td></tr></table>";
var attach;
if ( direction == 100 )
	{
	str = str.replace("#DIRECTION", "rtl");
	str = str.replace("#NCLASS", "name_rtl");
	var tmpClass = "ts_in_rtl";
	if( rtl != 100 )
	    {
	    tmpClass = tmpClass + "' dir='ltr";
	    }
        str = str.replace("#TCLASS", tmpClass);
	attach = attachrtl;
	var phNo = /^[+]/;
	if (phNo.test(party))
	  {
	  party = party.replace("+","");
	  var tmpParty = "<span dir='";
	  if ( rtl == 100 )
	       {
	       tmpParty = tmpParty + "ltr'>+</span>" + party;
	       }
	  else
	       {
	       tmpParty = tmpParty + "rtl'>+</span>" + party;
	       }
	  party = tmpParty;
	  }
	}
else
	{
	str = str.replace("#DIRECTION", "ltr");
	str = str.replace("#NCLASS", "name_ltr");
	str = str.replace("#TCLASS", "ts_in");
	attach = attachltr;
	}
if ( rtl == 100 )
	{
	str = str.replace("#DCLASS", "in_rtl");
	}
else	
	{
	str = str.replace("#DCLASS", "in_ltr");
	}


str = str.replace("#ID", msgid);
str = str.replace("#NAME", party);
str = str.replace("#TS", ts);
if ( clip == 100 )
	{
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else if ( clip == 110 )
	{
	if((rtl!=100) && smiley1(body))
           body=smiley2(body);
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else
	{
	if((rtl!=100) && smiley1(body))
           body=smiley2(body);
	str = str.replace("#MSG", body);
	}
var row = document.createElement();
row.innerHTML = str;
return row;
}

function createSent(rtl, msgid, party, clip, ts, body, direction)
{
var str = "<table class='msgbody'><tr><td class='status'></td><td dir=\'#DIRECTION\' ><div onClick=\"setEffect(this)\" href=View/#ID class='#DCLASS'><b class='out1'></b><p class='out2'><span class=\'#NCLASS\' >#NAME</span>&nbsp;<span name='msg' class='msg'>#MSG</span>&nbsp;&nbsp;<span class=\'#TCLASS\'>#TS</span></p><b class='out3'></b></div></td></tr></table>";
var attach;
if ( direction == 100 )
	{
	str = str.replace("#DIRECTION", "rtl");
	str = str.replace("#NCLASS", "name_rtl");
	var tmpClass = "ts_out_rtl";
	if( rtl != 100 )
	    {
	    tmpClass =tmpClass + "' dir='ltr";
	    }
        str = str.replace("#TCLASS", tmpClass);

	attach = attachrtl;
	}
else
	{
	str = str.replace("#DIRECTION", "ltr");
	str = str.replace("#NCLASS", "name_ltr");
	str = str.replace("#TCLASS", "ts_out");
	attach = attachltr;
	}
	
if ( rtl == 100 )
	{
	str = str.replace("#DCLASS", "out_rtl");
	}
else	
	{
	str = str.replace("#DCLASS", "out_ltr");
	}

str = str.replace("#ID", msgid);
str = str.replace("#NAME", party);
str = str.replace("#TS", ts);
if ( clip == 100 )
	{
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else if ( clip == 110 )
	{
	if((rtl!=100) && smiley1(body))
           body=smiley2(body);
	var newBody = attach + "<br>" + body;
	str = str.replace("#MSG", newBody);
	}
else
	{
	if((rtl!=100) && smiley1(body))
           body=smiley2(body);
	str = str.replace("#MSG", body);
	}
var row = document.createElement();
row.innerHTML = str;
return row;
}

// End of file
