//****************************************************************
// Created By 			:Devang Doshi
// Created Date			:11/07/2008
// Last Modified Date	:11/07/2008
// Page Description		:Collection Of Common Javascript Functions
//*****************************************************************



//**************** Digit Function**********************
// This Function Allows Only Digits (0-9) in Textbox.
//******************************************************

function digit(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	{
		return false;
	}
	return true;
}


//******************* Instruction For Implementation ********************
//write this in your textbox tag : onkeypress="return digit(event)"
//***********************************************************************

//**************** Digit Function End **********************


//**************** Digit Function 1**********************
// This Function Allows Only Digits (0-9) and . in Textbox.
//******************************************************


function digit1(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > 57))
	{
		return false;
	}
	return true;
}

//******************* Instruction For Implementation ********************
//write this in your textbox tag : onkeypress="return digit1(event)"
//***********************************************************************

//**************** Digit Function 1 End **********************



//**************** Alpha Function**********************
// This Function Allows Only Alphabets (A-Z and a-z and blank space) in Textbox.
//******************************************************

function alpha(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode!=32 && (charCode < 65 || charCode > 90)&&(charCode < 97 || charCode > 122))
	{
		return false;
	}
	return true;
}

//******************* Instruction For Implementation ********************
//write this in your textbox tag : onkeypress="return alpha(event)"
//***********************************************************************

//**************** Alpha Function End **********************


//**************** Alphanumeric Function**********************
// This Function Allows Only Alphabets (A-Z and a-z and blank space) and Digits (0-9) in Textbox.
//******************************************************

function alphanumeric(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if ( charCode!=32 &&(charCode < 48 || charCode > 57) && (charCode < 65 || charCode > 90 ) && (charCode < 97 || charCode > 122))
	{
		return false;
	}
	return true;
}


//******************* Instruction For Implementation ********************
//write this in your textbox tag : onkeypress="return alphanumeric(event)"
//***********************************************************************

//**************** Alphanumeric Function End **********************


//**************** Email Function**********************
// This Function Valifdates Email address in textbox
//******************************************************
function emailchk(obj)
{
	if(obj.value!="")
	{
		var email,AtPos,StopPos,Message
		email = obj.value
		AtPos = email.indexOf("@")
		StopPos = email.lastIndexOf(".")
		Message = ""		
		if (AtPos == -1 || StopPos == -1) 
		{
			Message = "Not a valid email address"
		}
		if (StopPos < AtPos) 
		{
			Message = "Not a valid email address"
		}
		if (StopPos - AtPos == 1) 
		{
			Message = "Not a valid email address"
		}
		if (Message!="")
		{
			alert(Message);
			obj.focus();
			return(false);
		}
	}
}


//******************* Instruction For Implementation ********************
//write this in your textbox tag : onBlur="return email(this)"
//***********************************************************************

//**************** Email Function End **********************


//**************** New Window **********************
// This Function Opens newwindow
//******************************************************

var win = null;
function NewWindow(mypage,myname,w,h,scroll)
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=0,status=0'
	win = window.open(mypage,myname,settings)
}

//******************* Instruction For Implementation ********************
//write this :javascript:NewWindow('page link','page title',800,600,1)
//***********************************************************************

//**************** New window Function End **********************

//**************** Check pincode **********************
// This Function Checks pincode
//******************************************************

//function pin(obj)
//{
//	if (obj.value.length<6 && obj.value.length!=0)
//	{
//		alert("Please Enter 6 Digit Number.");
//		obj.focus();
//		return (false);
//	}
//}

//******************* Instruction For Implementation ********************
//write this : onBlur="javascript:pin(this);"
//***********************************************************************

//**************** check pincode Function End **********************

//**************** website addresscheck **********************
// This Function validate website address
//******************************************************
function webcheck(obj)
{
	if(obj.value!="")
	{
		var email,AtPos,StopPos,Message
		email = obj.value
		AtPos = email.indexOf("www")
		StopPos = email.lastIndexOf(".")
		Message = ""
		if (AtPos == -1 || StopPos == -1 || http == -1) 
		{
			Message = "Not a valid Website address"
		}
		
		if (StopPos < AtPos) 
		{
			Message = "Not a valid Website address"
		}
		if (StopPos - AtPos == 1) 
		{
			Message = "Not a valid Website address"
		}
		if (Message!="")
		{
			alert(Message);
			obj.focus();
			return(false);
		}
	}
}
//******************* Instruction For Implementation ********************
//write this :javascript:onBlur="return webcheck(this)"
//set value="http://" of your textbox
//***********************************************************************
//**************** webcheck Function End **********************



//**************** uppercase ***************************
// This Function converts all characters to uppercase
//******************************************************

function uppercase(obj)
{
	obj.value=obj.value.toUpperCase();
}
//******************* Instruction For Implementation ********************
//write this :javascript:onKeyup="uppercase(this);"
//***********************************************************************



//**************** Maxlength in textarea ***************************
// This Function sets maxlength of text area
//******************************************************
function maxlength(obj,obj1)
{
	if(obj.value.length>obj1)
	{
		obj.value=obj.value.substring(0, obj1);	
	}
}
//******************* Instruction For Implementation ********************
//write this :javascript:onKeypress="maxlength(this,5);"
//***********************************************************************

//**************** Convert Date into Short DD-MMM-YYYY Format *****************************
// This Function convert date into long format - '27-Nov-2008'
//****************************************************************************



function dt_format(ifld,ofld)
{

	
	var monthn=new Array(12);
	monthn[0]="January";
	monthn[1]="February";
	monthn[2]="March";
	monthn[3]="April";
	monthn[4]="May";
	monthn[5]="June";
	monthn[6]="July";
	monthn[7]="August";
	monthn[8]="September";
	monthn[9]="October";
	monthn[10]="November";
	monthn[11]="December";
	
	if (document.getElementById(ifld).value!="")
	{
		var dinput=new Date();
		
		yr=(document.getElementById(ifld).value).slice(6,10);
		mon=((document.getElementById(ifld).value).slice(0,2))-1;
		day=(document.getElementById(ifld).value).slice(3,5);
		dinput.setFullYear(yr,mon,day);
		wday=(weekday[dinput.getDay()]);
		doutput=wday+", "+day+" "+monthn[mon]+" "+yr;
		//alert(doutput);
		document.getElementById(ofld).innerHTML=doutput;

	}
	
}

//******************* Instruction For Implementation ********************
//write this onChange="javascript:dt_format('<input>','<output>');"
//Replace <input> with the id in '' of the input field where this function is called from
//Replace <output> with the id in '' of the innerHTML where you want to display amount in words
//***********************************************************************


//**************** Convert Date into Long Format *****************************
// This Function convert date into long format - 'Thursday, November 27, 2008'
//****************************************************************************


function dt_format(ifld,ofld)
{
	
	
	var weekday=new Array(7);
	weekday[0]="Sunday";
	weekday[1]="Monday";
	weekday[2]="Tuesday";
	weekday[3]="Wednesday";
	weekday[4]="Thursday";
	weekday[5]="Friday";
	weekday[6]="Saturday";
	
	var monthn=new Array(12);
	monthn[0]="January";
	monthn[1]="February";
	monthn[2]="March";
	monthn[3]="April";
	monthn[4]="May";
	monthn[5]="June";
	monthn[6]="July";
	monthn[7]="August";
	monthn[8]="September";
	monthn[9]="October";
	monthn[10]="November";
	monthn[11]="December";
	
	if (document.getElementById(ifld).value!="")
	{
		var dinput=new Date();
		var ndy=new Array();
		ndy=(document.getElementById(ifld).value).split("/");
		//yr=(document.getElementById(ifld).value).slice(6,10);
		//mon=((document.getElementById(ifld).value).slice(0,2))-1;
		//day=(document.getElementById(ifld).value).slice(3,5);
		yr=ndy[2];
		mon=ndy[0]-1;
		day=ndy[1];
		dinput.setFullYear(yr,mon,day);
		wday=(weekday[dinput.getDay()]);
		doutput=wday+", "+day+" "+monthn[mon]+" "+yr;
		//alert(doutput);
		document.getElementById(ofld).innerHTML=doutput;

	}
	
}

//******************* Instruction For Implementation ********************
//write this onChange="javascript:dt_format('<input>','<output>');"
//Replace <input> with the id in '' of the input field where this function is called from
//Replace <output> with the id in '' of the innerHTML where you want to display amount in words
//***********************************************************************



//**************** Convert Amount into Words ***************************
// This Function converts amount in figures to Words till 9 digits
//**********************************************************************


function amt2words(fld,outp)
{
	var junkVal=document.getElementById(fld).value;
	junkVal=Math.floor(junkVal);
	var obStr=new String(junkVal);
	numReversed=obStr.split("");
	actnumber=numReversed.reverse();

	if(Number(junkVal) >=0)
	{
		//do nothing
	}
	else
	{
		return false;
	}
	if(Number(junkVal)==0)
	{
		document.getElementById(outp).innerHTML='Baht Zero Only';
		return false;
	}
	if(actnumber.length>9)
	{
		finalWord="Oops!!!! the number is too big to convert";
		document.getElementById(outp).innerHTML=finalWord;
		return false;
	}

	var iWords=["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"];
	var ePlace=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
	var tensPlace=['dummy', ' Ten', ' Twenty', ' Thirty', ' Fourty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' ];

	var iWordsLength=numReversed.length;
	var totalWords="";
	var inWords=new Array();
	var finalWord="";
	j=0;
	for(i=0; i<iWordsLength; i++)
	{
		switch(i)
		{
		case 0:
			if(actnumber[i]==0 || actnumber[i+1]==1 )
			{
				inWords[j]='';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]];
			}
			inWords[j]=inWords[j]+' Only';
			break;
		case 1:
			tens_complication(' ');
			break;
		case 2:
			if(actnumber[i]==0)
			{
				inWords[j]='';
			}
			else if(actnumber[i-1]!=0 && actnumber[i-2]!=0)
			{
				inWords[j]=iWords[actnumber[i]]+" Hundred and";
			}
			else
			{
				inWords[j]=iWords[actnumber[i]]+" Hundred ";
			}
			break;
		case 3:
			if(actnumber[i]==0 || actnumber[i+1]==1)
			{
				inWords[j]='';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]];
				inWords[j]=inWords[j]+" Thousand ";
			}
			break;
		case 4:
			tens_complication('Thousand');
			break;
		case 5:
			if(actnumber[i]==0 || actnumber[i+1]==1 )
			{
				inWords[j]='';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]];
				inWords[j]=inWords[j]+" Lakh ";
			}
			
			break;
		case 6:
			tens_complication('Lakh');
			break;
		case 7:
			if(actnumber[i]==0 || actnumber[i+1]==1 )
			{
				inWords[j]='';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]];
			}
			inWords[j]=inWords[j]+" Crore";
			break;
		case 8:
			tens_complication(' ');
			break;
		default:
			break;
		}
		j++;
	}

	function tens_complication(uvalue)
	{
		if(actnumber[i]==0)
		{
			inWords[j]='';
		}
		else if(actnumber[i]==1)
		{
			inWords[j]=ePlace[actnumber[i-1]]+" "+uvalue;
		}
		else
		{
			if(actnumber[i-1]==0)
			{
				
				inWords[j]=tensPlace[actnumber[i]]+" "+uvalue;
			}
			else
			{
				inWords[j]=tensPlace[actnumber[i]];
			}	
		}
	}
	inWords.reverse();
	for(i=0; i<inWords.length; i++)
	{
		finalWord+=inWords[i];
	}
	document.getElementById(outp).innerHTML='Baht, '+finalWord;
	//alert(finalWord);
}

//******************* Instruction For Implementation ********************
//write this :onChange="javascript:amt2words('<input>','<output>');"
//Replace <input> with the id in '' of the input field where this function is called from
//Replace <output> with the id in '' of the innerHTML where you want to display amount in words
//***********************************************************************

