function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}
// validate the firstname , middle name , last name  , country, state ,city
function isEmpty(val) {
	if(trim(val) == "")
	{
	return true;
	}
	else
	{
		return false;
	}
}
function isValidName(val,option) {
    validRegExp = /^[a-zA-Z ]+$/i;
    val = trim(val);
    var flag = true;
    switch(option) {
        case 1:
            flag = val.length < firstname_min_length;            
        break;
        case 2:
            flag = val.length < middlename_min_length;
        break;
        case 3:
            flag = val.length < lastname_min_length;
        break;
        case 4:
            flag = val.length < fullname_min_length;
        break;
        case 5:
            flag = val.length < country_min_length;
        break;
        case 6:
            flag = val.length < state_min_length;
        break;
        case 7:
            flag = val.length < city_min_length;
        break;
    }
    if (val.search(validRegExp) == -1 || flag) {
      return false;
    } 
    return true;     
}
// validate the address 
function  isValidAddress(addr){
    validRegExp = /^[0-9a-zA-Z \s,.\#/-]+$/;
    addr = trim(addr);
    if (addr.match(validRegExp) == null || addr.length<address_min_length) {
      return false;
    } 
    return true;
}
// Check whether a desired valu e is selected in selection box or not
function  isSelected(val){
    val = trim(val);
    if (val.length<1 || val==0) {
      return false;
    } 
    return true;
}
// Check whether a valid telephone , mobile
function isValidPhone(val,option) {
    var flag=true;
    switch(option){
        case 1:
            validRegExp = /^[0-9 \.+-]+$/;
            flag = val.length<6;
        break;
        case 2:
            validRegExp = /^[0-9 \.+-]+$/;
            flag = val.length<10;
        break;
		case 3:
            validRegExp = /^[0-9 \.\,+-]+$/;
            flag = val.length<10;
        break;
		 
    }
   // search email text for regular exp matches
    val = trim(val);
    if (val.match(validRegExp) == null || flag) {
      return false;
    } 
    return true;     
}
// to validate zipcode
function isValidZipCode(val){
    val = trim(val);
    validRegExp = /^[0-9a-z A-Z]+$/;
    flag = val.length<5;
    if (val.match(validRegExp) == null || flag) {
      return false;
    } 
    return true;     
}
// to validate email
function isValidEmail(strEmail){
   validRegExp = /^[\w\.-]+@[a-z,A-Z,0-9-]+[\.]{1}[a-zA-Z]{2,}[[\.]?[a-zA-Z]{0,2}$/i;
   strEmail = trim(strEmail);
    if (strEmail.search(validRegExp) == -1 || strEmail < 6 ) {
      return false;
    } 
    return true; 
}
// validate multiple emails
function validateReceiptents(email){
    if (!email){
        return false;
    }
    var recep = email.split(',');
    for (i=0;i<recep.length;i++){
        if(!isValidEmail(recep[i])){
            return false;
        }
    }
    return true;    
}
//validate user name 
function isValidUserName(usr){
   validRegExp = /^[a-zA-Z0-9]+[\w]+$/;
    if (usr.search(validRegExp) == -1 || usr.length < 6 ) {
      return false;
    } 
    return true;     
}

//validate password 
function isValidPassword(val){
    val = trim(val);
    if (val.length < 6 ) {
      return false;
    } 
    return true;     
}

//to check whether password match with confirm password
function isPasswordMatch(password,confirm){
    password = trim(password);
    confirm  = trim(confirm);
    if (password!=confirm){
     return false;
    } 
    return true;     
}

// valid  creditcard name
function isValidCardName(val){
   validRegExp = /^[a-zA-Z ]+$/;
   if (val.search(validRegExp) == -1 || val.length < card_min_length ) {
      return false;
   } 
   return true;     
}


// valid creditcard number
function isValidCardNumber(val){
	validRegExp = /^[0-9]+$/;
   if (val.search(validRegExp) == -1 || val < 16 ) {
      return false;
   } 
   return true;     
}
function isNumber(val){
	validRegExp = /^[0-9]+$/;
   if (val.search(validRegExp) == -1 ) {
      return false;
   } 
   return true;     
}
function isMoney(val){
	validRegExp = /^[0-9 \.]+$/;
   if (val.search(validRegExp) == -1 ) {
      return false;
   } 
   return true;     
}
// valid creditcard number
function isValidCVVNumber(val){
	validRegExp = /^[0-9]+$/;
   if (val.search(validRegExp) == -1 || val < 3 ) {
      return false;
   } 
   return true;     
}
function validateMembership(memcount){
	for (i=1;i<=memcount;i++){
		if(document.getElementById('membership'+i).checked) {
			return true;
		}
	}
	return false;
}
function isValidDate(Day,Mn,Yr){
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);

    if(dt.getDate()!=Day){
        //alert('Invalid Date');
        return false;
        }
    else if(dt.getMonth()!=Mn-1){
    //this is for the purpose JavaScript starts the month from 0
        //alert('Invalid Date');
        return false;
        }
    else if(dt.getFullYear()!=Yr){
        //alert('Invalid Date');
        return false;
        }
    return true;
 }
function isDate(dte){
	var dte = dte.split("/");
	Day=dte[0];
	Mn =dte[1];
	Yr =dte[2];
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);

    if(dt.getDate()!=Day){
        //alert('Invalid Date');
        return false;
        }
    else if(dt.getMonth()!=Mn-1){
    //this is for the purpose JavaScript starts the month from 0
        //alert('Invalid Date');
        return false;
        }
    else if(dt.getFullYear()!=Yr){
        //alert('Invalid Date');
        return false;
        }
    return true;
 }
function chkMoreThanThree(obj) {
	var cnt = 0;
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].selected) {
			if(obj.options[i].value)
				cnt++;
		}
	}
	if(cnt > 3) {
		return false;
	}
	else
		return true;
} 
function getTotalSelection(obj) {
	var cnt = 0;
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].selected) {
			if(obj.options[i].value)
				cnt++;
		}
	}
	return cnt;
}
function isValidNumeric(val) {
    var flag=true;
	validRegExp = /^[0-9]+$/;
	val = trim(val);
    if (val.match(validRegExp) == null) {
      return false;
    } 
    return true;     
}
function isValidCCMonth(Mn,Yr) {
	var cdate = new Date();
	var cmnth = cdate.getMonth();
	var curyr = cdate.getFullYear();
	if(curyr == Yr && Mn < cmnth+1)
	{
			return false;
	}
    return true;
}
function check_url(address) {
  if ((address == "")|| (address.indexOf ('http://') == -1)|| (address.indexOf ('.') == -1))
      return false;
  return true;
}

function checkemail(mail)
{
		var str=mail.value;
		
		if(str!="")
		{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		 
		if (filter.test(str))
		{
			return true;
		}
			
		else
		{
			return false;
		}
		}
		else
		{
				return true;
		} 
	}	 

function checkweb(web)
{
	if(web.value !="" )
   	{
		var str=web.value;
		var j = new RegExp(); 
		
		j.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
		
		if (j.test(str))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else 
	{
		return true;
	}

}	 

function make_name(ctl)
{
	var c = ctl.value;
	var s = "";
	var val=ctl.value;
	if(val!="")
	{
		c=c.substr(0,1).toUpperCase() + c.substr(1,c.length-1).toLowerCase();
		val=c;
		while(c.indexOf(".")!=-1)
		{		
			s = s + c.substr(0,c.indexOf(".")) + "." + c.substr(c.indexOf(".")+1,1).toUpperCase();
			c = c.substr(c.indexOf(".")+2);
			val=s;
		}
		if(s !="") {
		val=s+c; 
		c=val;
		s="";
		}
		while(c.indexOf(" ")!=-1)
		{		
			s = s + c.substr(0,c.indexOf(" ")) + " " + c.substr(c.indexOf(" ")+1,1).toUpperCase();
			c = c.substr(c.indexOf(" ")+2);
			val=s;
		}
		if(s !="") {
		val=s+c; 
		c=val;
		s="";
		}
		while(c.indexOf(",")!=-1)
		{		
			s = s + c.substr(0,c.indexOf(",")) + "," + c.substr(c.indexOf(",")+1,1).toUpperCase();
			c = c.substr(c.indexOf(",")+2);
			val=s;
		}
		if(s !="") val=s+c; 
		ctl.value=val;
	}
}
function checkNumber(e)
{
	e = (e) ? e : (window.event) ? event : null;
  	if (e)
  	{
    		var c = (e.charCode) ? e.charCode :
                   	((e.keyCode) ? e.keyCode :
                   	((e.which) ? e.which : 0));

			// c==8 || c==46 || c==35 || c==36 ||
			if(c==13 || (c >=48 && c <=57))
			{
				return true;
			} else {
				c=0;
				return false;
			}
	}
}
function check_character(e,ctl)
{
	e = (e) ? e : (window.event) ? event : null;
  	if (e)
  	{
    		var c = (e.charCode) ? e.charCode :
                   	((e.keyCode) ? e.keyCode :
                   	((e.which) ? e.which : 0));

			if(c==45 || (c >=48 && c <=57))
			{
				alert("Please enter only characters here");
				c=0;
				ctl.focus();
				return false;
			} else {
				return true;
			}
	}
}
function check_number_ifen(e,ctl)
{
	e = (e) ? e : (window.event) ? event : null;
  	if (e)
  	{
    		var c = (e.charCode) ? e.charCode :
                   	((e.keyCode) ? e.keyCode :
                   	((e.which) ? e.which : 0));

			if(c==45 || c==13 || (c >=48 && c <=57))
			{
				return true;
			} else {
				alert("Please enter only numbers and \"-\" here");
				c=0;
				ctl.focus();
				return false;
			}
	}
}
function file_filter(files,val)
{
	if(val==1)
	{
		var ext = new Array("gif","jpg","jpeg","GIF","JPG","JPEG");
	}
	else if(val==2)
	{
		var ext = new Array("doc","DOC");
	}
	if(files!="")
	{
		for(i=0;i<ext.length;i++)
		{
			lastDot = ext[i];
			if(files.lastIndexOf(lastDot)>0 )
			{
				return true;
			}
		}
		return false;
	}
	else
	{
		return true;
	}
}	