function str_trim(str) // strips of leading and following whitespaces from a string
{	
	if(str.length > 0)
		while(str.charAt(0)==' ')
			str = str.substr(1);
		
	if(str.length > 0)
		while(str.charAt((str.length - 1))==' ')
			str = str.substring(0, str.length-1);
	
	return str;
}

function DefaultFocus(ctrl_nm)
{
	if(document.getElementById(ctrl_nm))
	{
		obj = document.getElementById(ctrl_nm);
		obj.focus();
	}
}

function GeneratePass(temp,rand)
{
	var pattern = new Array('4321','3214','2143','1432','4231','2314','3142','1423','4123','1234','2341','3412','4132','1324','3241','2413');

	 var ranvar    = Math.round(Math.random() * 15);
	 var mypattern = pattern[ranvar];
	 
	 var temparr   = new Array();
	 
	 temparr[1] = temp.substr(0,8);
	 temparr[2] = temp.substr(8,8);
	 temparr[3] = temp.substr(16,8);
	 temparr[4] = temp.substr(24,8);
	 
	 passwordstr = rand+temparr[mypattern.substr(0,1)]+rand+temparr[mypattern.substr(1,1)]+rand+temparr[mypattern.substr(2,1)]+rand+temparr[mypattern.substr(3,1)]+mypattern;
	 
	 return passwordstr;
}

function GoToPage(page)
{
	window.document.location.href=page;
}

function ConfirmDelete(txt, page)
{
	var msg = "You Are About To Delete this " + txt + "! Continue?";

	if(confirm(msg))
		window.document.location.href=page;
}

function ConfirmImgDelete(txt,id,action,frm,mode)
{
	var msg = "You Are About To Delete this " + txt + "! Continue?";

	if(confirm(msg))
	{
		 if(id>0)
		 {
			frm.action = action;
			document.getElementById('mode').value = mode;
			document.getElementById('txtid').value = id;
			
			frm.submit();
		 }
		 else
			return false;
	}
}

function GoToPageByPost(pgaction, frm, id, mode)
{
	frm.action = pgaction;
	frm.txtid.value = id;

	if(mode)
		frm.mode.value = mode;
	frm.submit();
}

function validate_email(email_txt) // validates a string as a email id
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@([\\w].+)\.[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(email_txt);
}

function validate_integer(int_str) // validates a string as an integer (i.e. no decimal points)
{
	regExpr = new RegExp(/^\d*$/);
	var regex = new RegExp(regExpr);
	return regex.test(int_str);
}
function validate_real_nos(real_nos_str) // validates a string as a Real Number (i.e. with decimal points)
{
	regExpr = new RegExp(/^-?\d*(\.\d{1,2})?$/);
	var regex = new RegExp(regExpr);
	return regex.test(real_nos_str);
}
function CheckNum(obj) // validates a form ctrl for an integer
{	
	regExpr = new RegExp(/^\d*$/);

	if(str_trim(obj.value)=="" || !regExpr.test(obj.value))
		obj.value="0";		
}
function CheckRealNum(obj) // validates a form ctrl for a real number
{
	regExpr=new RegExp(/^-?\d*(\.\d{1,2})?$/);
	
	if(!regExpr.test(obj.value))
		obj.value="0";
}
function SetFocus(obj)
{
	obj.focus();
	obj.select();
}

