/*
	Program name : Library.js
	Description : Common library function
	functions :  AllowOnlyChar()		key press for allowing only char
				 AllowOnlyNum()			key press for allowing only number
				 SetFColor()			on focus of an input
				 SetLColor()			lost focus of an input
				 callCalender()			calling calendar
				 lpad()					padding left side
				 mydate()				key handler for date input
				 gtDay()				extracting day from date string
				 gtMonth()				extracting month from date string
				 gtYear()				extracting year from date string
				 dtos()					converting date to string
				 stod()					converting string to date
				 getCurTimeSeconds()	getting current time seconds
				 chkdate()				validating input date
	Author : Sinulal
	Date : 14 the Oct 2003 
*/

if (document.layers)
{
	document.captureEvents(Event.MOUSEDOWN);
}

//document.onmousedown=click;

function click()
{
if (event.button == 2)
	{
	alert("Right Click Disabled");
	return false;
	}
if (document.readyState!='complete')
	{
	alert('Please wait while loading the page')
	return false;
	}
}


function AllowOnlyChar()
{
	if((event.keyCode<97 || event.keyCode>122) && (event.keyCode<65 || event.keyCode > 90)) 
	{event.keyCode=0;}

}

function AllowOnlyNum()
{
	if((event.keyCode<=47 || event.keyCode>=59) && event.keyCode != 46 )
	{event.keyCode=0;}
}


function SetFColor()
{
	try
		{
		window.event.srcElement.style.backgroundColor = "#FFFFF0"
		tdStatus.innerText = window.event.srcElement.title
		}
	catch (e)
		{
		//nothing to do
		}
}
	
function SetLColor()
{
	try
		{
		window.event.srcElement.style.backgroundColor = "#ffffff"
		tdStatus.innerText = ""

		}
	catch (e)
		{
		//nothing to do
		}
}


function callCalender()
{
	try
		{
		if (event.keyCode == 113)
			{
				ReturnEl = window.event.srcElement
				window.open ("calendar.htm",null,"height=170,width=270,resizable=no,status=no,toolbar=no,menubar=no,left=360,top=200");
			}
		}
	catch (e)
		{
			alert('GetDt : ' + e.description)
		}
}
 
function lpad(strValue,nLength,strStuff)
{
	try
		{
		var mystring = new String(strValue)
		var newstring=''
		mystring=mystring.replace(' ','')
		nLength=nLength-mystring.length+1
		for(i=1;i<nLength;i++)
			{
				newstring= newstring+strStuff
			}
		return (newstring+strValue)
		}
	catch (e)
		{
			alert("lpad : " + e.description)			
		}
}

function mydate()    // keyhandler for date text box
{
	try
		{
		var aday = new Array(0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		var objdt = window.event.srcElement
		var yr=gtYear(objdt.value);
		var lp= yr % 4;
		if(lp==0)
			{ aday[2]=29 }
		if (event.keyCode==43)      //+ key
		{
			if (objdt.value!=='')
				{
				if (chkdate()==true)
					{
					nmonth= new Number(gtMonth(objdt.value));
					nday= new Number(gtDay(objdt.value));
					nyear= new Number(gtYear(objdt.value));
					if (nday+1>aday[nmonth])
						{
						if (nmonth==12)
							{nyear=nyear+1
							newdate='01' +'/01/'+nyear}
						else
							{newdate='01' +'/'+ lpad(nmonth+1,2,'0') +'/'+nyear}
						}
					else
						{
						newdate=lpad(nday+1,2,'0') +'/'+lpad(nmonth,2,'0')+'/'+nyear;
						}
					objdt.value=newdate;
					return
					}
				}
			else
				{
				var today
				today = new Date();
				newdate = lpad(today.getDate(),2,"0") + "/";
				newdate += lpad(today.getMonth() + 1,2,"0") + "/";
				newdate += today.getFullYear();
				objdt.value=newdate;
				}
			event.keyCode=0
		}
		else if (event.keyCode==45)  // - key
		{
			if (objdt.value!=='')
				{
				if (chkdate()==true)
					{
					nmonth= new Number(gtMonth(objdt.value));
					nday= new Number(gtDay(objdt.value));
					nyear= new Number(gtYear(objdt.value));
					if (nday==1)
						{
						if (nmonth==1)
							{nyear=nyear-1
							newdate='31' +'/12/'+nyear}
						else
							{newdate=lpad(aday[nmonth-1],2,'0') +'/'+ lpad(nmonth-1,2,'0') +'/'+nyear}
						}
					else
						{
						newdate=lpad(nday-1,2,'0') +'/'+lpad(nmonth,2,'0')+'/'+nyear;
						}
					objdt.value=newdate;
					return
					}
				}
			else
				{
				var today
				today = new Date();
				newdate = lpad(today.getDate(),2,"0") + "/";
				newdate += lpad(today.getMonth() + 1,2,"0") + "/";
				newdate += today.getFullYear();
				objdt.value=newdate;
				}
			event.keyCode=0
		}
		if((event.keyCode!=13 && event.keyCode!=27) && event.keyCode<=47 || event.keyCode>=59)
		{event.keyCode=0}
		s = new String(objdt.value);
		if (s.length==2 || s.length==5)
			{
			objdt.value = objdt.value + "/"
			}
		}
	catch (e)
		{
			alert("mydate : " + e.description)
		}
}


function gtDay(strDate)
{
	try
		{
		var mydate = new String(strDate)
		return (mydate.substring(0,2))
		}
	catch (e)
		{
			alert("gtDay :" + e.description)
		}
}

function gtMonth(strDate)
{
	try
		{
		var mydate = new String(strDate)
		return (mydate.substring(5,3))
		}
	catch (e)
		{
			alert('gtMonth : ' + e.description)
		}
}

function gtYear(strDate)
{
	try
		{
		var mydate = new String(strDate)
		return (mydate.substring(10,6))
		}
	catch (e)
		{
			alert('gtYear : '+ e.description)
		}
}

function dtos(strDate)
{
	try
		{
			var myDate=new String(strDate)
			if(myDate != "" && Number(myDate) != 0)
				{return (myDate.substring(10,6) + myDate.substring(5,3) + myDate.substring(0,2))}
			else
				{return ""}	
		}
	catch (e)
		{
			alert('dtos : ' + e.description)
		}
}

function stod(strDate)
{
	try
		{
		var myDate=new String(strDate)
		if(myDate != "" && Number(myDate) != 0)
			{return (myDate.substring(8,6) + "/" + myDate.substring(6,4) + "/" +  myDate.substring(4,0))}
		else	
			{return ""}	
		}
	catch (e)
		{
			alert('stod : ' + e.description)
		}
}


function getCurTimeSeconds()
{
	try
		{
			var objTempTm
			objTempTm = new Date()
			return (objTempTm.getSeconds()) 
		}
	catch (e)
		{
			alert('getCurTimeSeconds : ' + e.description)
		}
}

function chkdate()
{
	try
		{
			var tgDate = window.event.srcElement
			if (tgDate.value == '' )
				{return true}
			if (tgDate.value.length < 10)
				{
				alert("Input date in DD/MM/YYYY format");
				tgDate.focus();
				tgDate.select();
				return (false)
				}
			mon1=tgDate.value.substr(2,1);
			mon2=tgDate.value.substr(5,1);
			if( mon1 != "/" || mon2!= "/")
			{
				alert("Input date in DD/MM/YYYY format");
				tgDate.focus();
				tgDate.select();
				return(false);
			}
			dd=tgDate.value.substr(0,2);
			mm=tgDate.value.substr(3,2);
			yy=tgDate.value.substr(6,4);
			chk=isNaN(dd);
			if ( isNaN(dd) || isNaN(mm) || isNaN(yy))
				{
				alert("Input date in DD/MM/YYYY format");
				tgDate.focus();
				tgDate.select();
				return(false);
				}
			if ((dd==0) || (dd > 31) || (mm==0)	|| (mm >12) ||(yy==0) || (yy <=1900))
				{
				alert("Invalid Date");
				tgDate.focus();
				tgDate.select();
				return(false);
				}
			if (mm==2)
				{
				nleap= yy % 4 ;
				if (nleap!=0 && dd>28)
					{
					alert("Invalid Date");
					tgDate.focus();
					tgDate.select();
					return(false); 
					}
				else if (dd>29)
					{
					alert("Invalid Date");
					tgDate.focus();
					tgDate.select();
					return(false); 
					} 
				}
			else if (mm==4 || mm==6 || mm==9 || mm==11)
				{
				if (dd>30) 
					{
					alert("Invalid Date");
					tgDate.focus();
					tgDate.select();
					return(false); 
					}
				}
			return(true);
		}
	catch(e)
		{
			alert('valdate : '+ e.description)
		}
}


var objXMLHTTP
var intStartTime = 0
var objXMLHTTPTimer
var strXMLReturn
var strWindowsStat

function ChkSettings()
{
	try
		{
			// checking the client side scripting rights
			var objFso = new ActiveXObject("Scripting.FileSystemObject");
			return (true)
		}
	catch (e)
		{
			window.showModalDialog("SecurityProb.htm","","help:no;resizable:yes;status:no;scroll:yes;center:1;");
			return (false)
		}
}


function ExecuteXMLHTTP(strURL)
{
	var objTime
	var strURLToExecute = new String(strURL)	
	
	if ( ChkSettings() == false )
		{return ("")}
	try
		{
			strWindowsStat = window.status
			window.status = "Microsoft.XMLHTTP - Initializing"
			
			objTime = new Date()
			intStartTime = objTime.getSeconds()
			objXMLHTTP	= new ActiveXObject("Microsoft.XMLHTTP");
			objXMLHTTP.onreadystatechange = HandleStateChange
			
			chkXMLHTTPStatus()
			
			objXMLHTTP.open ("POST",strURLToExecute,false)
			objXMLHTTP.send()
			if (objXMLHTTP.readyState==4)
				{				
					strXMLReturn = objXMLHTTP.responseText		
					objXMLHTTP.abort()
				}			
			window.status = strWindowsStat
			return (strXMLReturn)
		}
	catch(e)
		{
		if (e.number == -2146827859)
			{
				
			}
		else
			{		
				alert('ExecuteXMLHTTP : ' + e.description)
			}
		}
}


function chkXMLHTTPStatus()
{
	try
		{
		var intCurTime = 0;
		intCurTime = getCurTimeSeconds();
		if (intCurTime-intStartTime>5)
			{
				window.clearTimeout(objXMLHTTPTimer)
				strXMLReturn = "ERROR|Server Timout"
				objXMLHTTP.abort()
			}
		else
			{
				objXMLHTTPTimer = window.setTimeout("chkXMLHTTPStatus()", .00001);
			}
		}
	catch (e)
		{
			alert('chkXMLHTTPStatus : ' + e.description)
		}
}

function HandleStateChange()
{
try
	{
	var intStatus = objXMLHTTP.readyState
	if (intStatus==0)
		{
			window.status = 'Microsoft.XMLHTTP - Connecting'
		}
	else if (intStatus==1)
		{
			window.status = 'Microsoft.XMLHTTP - Loading'
		}
	else if (intStatus==2)
		{
			window.status = 'Microsoft.XMLHTTP - Sending Request'
		}
	else if (intStatus==3)
		{
			window.status = 'Microsoft.XMLHTTP - Receiving Response'
		}
	else if (intStatus==4)
		{
			window.status = 'Microsoft.XMLHTTP - Completed'
		}
	}
catch (e)
	{
		alert('HandleStateChange : ' + e.description)
	}
}

/*
	----------------------------------------------  End of the Page ----------------------------------------
*/