//$$$$$$$$$$$$$$$$$$$$$ TRINING FUNCTION $$$$$$$$$$$$$$$$$$$$$$$$$

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));
	
}

//////

function ruSure(objForm)
{
 
    var chk = false;
	for(i=0; i<objForm.elements.length; i++) 
	{
		if (objForm.elements[i].type=="checkbox") 
		{		 
			//objForm.elements[i].checked=objForm.chkStore.checked;
			if(objForm.elements[i].checked == true)
			{	
				chk = true ;
			}
			
		}
	}
	 
	if(chk == false)
	{
		alert("Please Select atleast one checkbox.");
		return;
	}
	else
	{
		objForm.submit();
	}

}
function checkall(objForm)
{
	var i=0;
	
	for(i=0; i<objForm.elements.length; i++) 
	{
		if (objForm.elements[i].type=="checkbox") 
		{
			objForm.elements[i].checked=objForm.check_all.checked;
		}
	}
}


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

//js functions

function isEMail(s)
	{
		if(((s.indexOf("@"))==-1) || ((s.indexOf("."))==-1))
		{
			return true ;  
		}else{
			var x=s;
			var ctr=0;
			var dot=0;
			for(j=0;j<x.length;j++)
			{
				if(x.charAt(j)=="@"){
					ctr++;
				}
				if(x.charAt(j)=="."){
					dot++;
				}
			}
			if(ctr!=1){
				return true;
			}else{
				return false;
			}
		}
	}
function valid_phone_format(phone_num){

var re= /\D/;
// test for this format: (xxx)xxx-xxxx
var re2 = /^\({1}\d{3}\)\d{3}-\d{4}/; 
// test for this format: xxx-xxx-xxxx
//var re2 = /^\d{3}-\d{3}-\d{4}/;

//for (i=0; i<nums.length;i++){
var num=phone_num;
//alert(num);alert(num.length);
var newNum;
 if (num != "" && re2.test(num)!=true){
   if (num != ""){
     while (re.test(num)){
     num = num.replace(re,"");
     }
   }
	//alert(num.length);   
  if (num.length != 10){
    return false
      }
   else {
     // for format (xxx)xxx-xxxx
     newNum = '(' + num.substring(0,3) + ')' + num.substring(3,6) + '-' + num.substring(6,10);
	 //alert("rajesh");
	 //alert (newNum);
     // for format xxx-xxx-xxxx
     // newNum = num.substring(0,3) + '-' + num.substring(3,6) + '-' + num.substring(6,10);
     return newNum;
     }
   }
   else{
   return num;
   }
 // }
}
  
	function isValidEmail(s)
	{
		var myRe;
		var myArr;
		myRe = new RegExp("\[^a-zA-Z0-9_@.-]");
		//myRe=/[`~\#\$\{\}\;\:\"@,.|></]/;
		myArr=myRe.test(s);
		if(myArr){ 
			return myArr
		}else{
			if(s.length<=3){
				return true;
			}
			ch=isEMail(s)
			if(ch){
				return true;
			}else{
				return false;
			}
		}
	}
	function isAlphaNumeric(s)
	{
		var myRe;
		var myArr;
		myRe = new RegExp("\[^a-zA-Z0-9_]");
		myArr=myRe.test(s);
		return myArr;	
	}
	function isCharacter(s)
	{
		var myRe;
		var myArr;
		myRe = new RegExp("\[^a-zA-Z]");
		myArr=myRe.test(s);
		return myArr;	
	}
	function isNumeric(s)
	{
		var myRe;
		var myArr;
		myRe = new RegExp("\[^0-9.]");
		myArr=myRe.test(s);
		return myArr;	
	}
	function isZip(s)
	{
		var myRe;
		var myArr;
		myRe = new RegExp("\[^0-9a-zA-Z]");
		myArr=myRe.test(s);
		return myArr;	
	}
	function isPhone(s)
	{
		var myRe;
		var myArr;
		myRe = new RegExp("\[^0-9/-/(/)]");
		myArr=myRe.test(s);
		return myArr;	
	}
	function isPhone_new(string) {
	if (string.search(/^([0-9]){3} [0-9]{3}-[0-9]{4}$/) != -1)
		return true;
	else
		return false;
}
function isPhone1(string) {
	if (string.search(/^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/) != -1)
		return true;
	else
		return false;
}
	function checkAddress(s)
	{
		var x=s;
		for(j=0;j<x.length;j++)
		{ 
			var char_code= x.charCodeAt(j);
			if(char_code==32 || (char_code>=65 && char_code<=90) || (char_code>=97 && char_code<=122) || (char_code>=48 && char_code<=57) || char_code==44 || char_code==45 ||char_code==46 || char_code==47 || char_code==35 || char_code==33 ||char_code==63 ||char_code==61 || char_code==10 || char_code==13 || char_code==39 || char_code==34)
			{
			}else{
				return true;
				break;
			}		
		}
	}
	function isCharwithSpc(s)
	{
		var x=s;
		for(j=0;j<x.length;j++)
		{ 
			var char_code= x.charCodeAt(j);
			if(char_code==32 || (char_code>=65 && char_code<=90) || (char_code>=97 && char_code<=122) || (char_code>=48 && char_code<=58) || char_code==46 || char_code==63 || char_code==33 || char_code==39  || char_code==44)
			{
			}else{
				return true;
				break;
			}		
		}
	}
	function isCity(s)
	{
		var x=s;
		for(j=0;j<x.length;j++)
		{ 
			var char_code= x.charCodeAt(j);
			if(char_code==32 || (char_code>=65 && char_code<=90) || (char_code>=97 && char_code<=122))
			{
			}else{
				return true;
				break;
			}		
		}
	}

function CommaFormattedPrice(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}
function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else
	{
              countfield.value = maxlimit - field.value.length;
    }
}
function addDays(SourceDate,NumDays,RetDateFormat)
{
	var DestDate;
	s_date_arr = SourceDate.split("-");
	var d=new Date(s_date_arr[0],s_date_arr[1]-1,s_date_arr[2]);
	d.setDate(d.getDate()+ parseInt(NumDays));
	var newdate = new Date(d.getFullYear(),d.getMonth(),d.getDate());
	var day=newdate.getDate();
	if (day < 10) {
		day= "0" + day;
	}
	var month=newdate.getMonth() + 1;
	if (month < 10) {
		month= "0" + month;
	}
	if(RetDateFormat=="US")
	{
		DestDate = month +"/"+day +"/"+ newdate.getFullYear();
	}
	else if(RetDateFormat=="MySql")
	{
	DestDate = newdate.getFullYear() + "-" +  month + "-" + day;
	}
	return DestDate;
}

function validpwd(str)
{
		invalidChars = " "
		if (str == "") 
		{
			return (false);
		}
		for (i=0; i<invalidChars.length; i++) 
		{
			badChar = invalidChars.charAt(i)
			if (str.indexOf(badChar,0) != -1) 
			{
				return (false);
			}
		}
		return(true);
}
function validEmailAddress(email)
{
		invalidChars = " /:,;~"
		if (email == "") 
		{
			return (false);
		}
		for (i=0; i<invalidChars.length; i++) 
		{
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) != -1) 
			{
				return (false);
			}
		}
		atPos = email.indexOf("@",1)
		if (atPos == -1) 
		{
			return (false);
		}
		if (email.indexOf("@",atPos+1) != -1) 
		{
			return (false);
		}
		periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) 
		{
			return (false);
		}
		if (periodPos+3 > email.length)	
		{
			return (false);
		}
			
		return (true);
}



