Friday, May 14, 2010

JAVA SCRIPT

1./* Create A Text File(If Not Exists) Through Java Script Containing WorkstationId and Telephone No on BodyOnload */

/--------- Start------------/

function fileexist()
{
var file=new ActiveXObject("Scripting.FileSystemObject");
if(file.FileExists("C:\\wstationfile.txt"))
{
var tempFile=file.OpenTextFile("C:\\wstationfile.txt",1,false,0);
var line= tempFile.ReadAll();
var newindex=line.indexOf("/");
var oldindex=0;
var str,restofstr;
restofstr=line;
while (restofstr.indexOf("/")>0)
{
str=restofstr.substring(0,newindex);
restofstr=restofstr.substring(newindex+1,restofstr.length);
newindex=restofstr.indexOf("/");
if(str.substring(0,str.indexOf("-")).toUpperCase()=="STATIONID")
{
document.getElementById("txtstation").value=str.substring(str.indexOf("-") + 1,str.length);
}
else if (str.substring(0,str.indexOf("-")).toUpperCase()=="TELEPHONENO")
{
document.getElementById("txttelephone").value=str.substring(str.indexOf("-") + 1,str.length);
}
str="";
}
document.getElementById("txttelephone").readOnly=true;
document.getElementById("txtstation").readOnly=true;
}
else
{
document.getElementById("txttelephone").readOnly=false;
document.getElementById("txtstation").readOnly=false;
}
}
function createfile()
{
var create,file;
create=new ActiveXObject("Scripting.FileSystemObject");
file=create.CreateTextFile("C:\\wstationfile.txt",true);
var str="";
str="stationid-" + document.getElementById("txtstation").value + "/";
str = str + "telephoneno-" + document.getElementById("txttelephone").value + "/";
str= str + "user-" + document.getElementById("txtusername").value + "/";
str = str + "campaing-GERECOVERY/" ;
str = str + "Lastdate-" + Date() + "/";
file.WriteLine(str);
}
/-------End--------------/



2./* Purpose:Display Month in Combo on Change of Year */

/--------- Start------------/
function ChangeMonth(j)
{

var m_names = new Array("January", "February", "March","April", "May", "June", "July", "August", "September",
"October", "November", "December");
var date=new Date();
var curr_month=date.getMonth();
var curr_year=date.getFullYear();
var curr_nextyear=curr_year + 1;;
var sel_year=document.getElementById('selYear' + j);;
var list=document.getElementById('selMonth' + j);;
var i;

if (document.getElementById('selYear' + j).value==curr_nextyear)//Purpose:If Year is Next Year,Month will start from Jan till dec
{
list=document.getElementById('selMonth' + j);    
for(i = 0; i < 12; i++)    {      list.options[i] = new Option(m_names[i].toString(),i); } } if (document.getElementById('selYear' + j).value==curr_year)//Purpose:If Year is Current Year,Month will start from Current Month till Dec { list.options.length=0; for(i=curr_month;i < 12; i++) {     list.options[i] = new Option(m_names[i].toString(),i);     }     if (list.options.length >0 )
{
for(var l=list.options.length-1;l>=0;l--)
{
if(list.options[l].value=="")
{
list.remove(l);
}
}
}

}
/--------- End------------/

3./*Purpose:To Check Java Script is Enable or Not */
function CheckJavaScript() {

if (navigator.JavaEnabled())
{
alert("The javascript is available .")
return true;
}
else
{
return false ;
}
}

4./* Purpose:Allow Only Numbers From 0 to 9 */

function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
//charcode=45(-) For Negative

if (charCode > 31 && (charCode < 48 || charCode > 57) || (charCode == 45))
return false;

return true;
}

5./* Purpose:To Allow Alpha Numeric Characters */

function isAlphaNumeric(elem, helperMsg)
{
var alphaExp = /^[a-zA-Z0-9]+$/;
if (elem.value != "")
{
if(elem.value.match(alphaExp)){
return true;
}
else
{
alert(helperMsg);
elem.value="";
elem.focus();
elem.select();
return false;
}
}
}

6./* Purpose:To Allow EMail Validation */

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if (elem.value != "")
{
if(elem.value.match(emailExp)){
return true;
}
else
{
alert(helperMsg);
elem.focus();
elem.select();
return false;
}