// JavaScript Document
// Removes leading whitespaces
function LTriming( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTriming( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function triming( value ) {
	
	return LTriming(RTriming(value));
	
}

function isvalidemail(value) {
	var emailexpr=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	return emailexpr.test(value);
}
//dd/mm/yyyy
function isvaliddate(value){
  var dateexpr=/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
  return dateexpr.test(value);   
}


function isvalidimgfile(value) {
	var fileexpr=/^.*(\.(gif|jpg|bmp|jpeg|GIF|JPG|BMP|JPEG))$/;
	return fileexpr.test(value);
}
function isvalidjpegfile(value) {
	var fileexpr=/^.*(\.(jpeg))$/;
	return fileexpr.test(value);
}
function isvalidvideofile(value) {
	var fileexpr=/^.*(\.(avi|dat|mgp|mpeg|flv|wmv|AVI|DAT|MPG|MPEG|FLV|WMV))$/;
	return fileexpr.test(value);
}

//USE: onKeyPress="return keyRestrict(event,'0123456789')"
function keyRestrict(e, validchars) { 
	var key='', keychar='';
	key = getKeyCode(e);
	if (key == null) return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) != -1)
	  return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	  return true;
	return false;
}
function getKeyCode(e) {
	if (window.event)
	return window.event.keyCode;
	else if (e)
	return e.which;
	else
	return null;
}
function url_check(url)
{
	var urlRegxp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	return urlRegxp.test(url);
}
function isProper(string)
{
	if (!string)
		return false;
	var iChars = "*|,\":<>[]{}`\';()@&$#% ";
	for (var i = 0; i < string.length; i++)
	{
		if (iChars.indexOf(string.charAt(i)) != -1)
			return false;
	}
	return true;
}
function $I(id)
{
	return document.getElementById(id);
}
function $V(id)
{
	return $I(id).value;
}
function AddToOptionList(OptionList, OptionValue, OptionText)
{
	// Add option to the bottom of the list
	OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}
function ClearOptions(OptionList)
{
	// Always clear an option list from the last entry to the first
	for (x=OptionList.length; x >= 0; x--)
	{
		OptionList[x] = null;
	}
}
function selcheckbox(initial,num)
{
	var check="";
	var id;
	for(var i=0;i<num;i++)
	{
		id=initial+i;
		if($I(id).checked)
			check+=$I(id).value+",";
	}
	return rtrim(check,",");
}
function numselcheckbox(initial,num)
{
	var count=0;
	var id;
	for(var i=0;i<num;i++)
	{
		id=initial+i;
		if($I(id).checked)
			count++;
	}
	return count;
}
function ltrim(str,char)
{
	if(str.length>0)
	{
		while(str.substring(0,1)==char)
			str=str.substring(1,str.length);
	}
	return str;
}
function rtrim(str,char)
{
	if(str.length>0)
	{
		while(str.substring(str.length-1,str.length)==char)
			str=str.substring(0,str.length-1);
	}
	return str;
}
function trim(str,char)
{
	return rtrim(ltrim(str,char),char);
}
function isValidImageFile(image)
{
	var ext=image.substring(image.length-4,image.length);
	var allowed=Array(".jpg",".gif",".png");
	for(var i=0;i<allowed.length;i++)
	{
		if(ext.toLowerCase()==allowed[i])
			break;
	}
	if(i==allowed.length)
		return false;
	else
		return true;
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }