
function ltrim(sValue)
{
   while(1)
   {
      if(sValue.substring(0, 1) != " ")
         break;
      sValue = sValue.substring(1, sValue.length);
   }
   return sValue;
}

function rtrim(sValue)
{
   while(1)
   {
      if(sValue.substring(sValue.length - 1, sValue.length) != " ")
         break;
      sValue = sValue.substring(0, sValue.length - 1);
   }
   return sValue;
}

function trim(sValue)
{
     var sTemp = ltrim(sValue);
   return rtrim(sTemp);
}
// Validates for number when value is not empty
function ValidateNumber(obj, mesg)
{
	var CheckNumber = /^\d*$/ig;
	
	if(!CheckNumber.test(trim(obj.value)))
	{
		alert("Please enter a valid value for " + mesg);
		obj.select();
		return false;
	}
	return true;
}
// Validates for decimal when value is not empty
function ValidateNumberFormat(elmObj, num, dec)
{
	var checkFloat = /\./;
	var CheckNumber = /^\d*\.?\d*$/ig;
	var alrt = "";
	//val = trim(elmObj.value).replace(/\s/g,"");
	val = trim(elmObj.value);

	if(!CheckNumber.test(val))
	{
		alert("Enter numeric value");
		elmObj.focus();
		return false;
	}

	dec = dec - 1;
	if(checkFloat.test(val))
	{
		arr = val.split(".");

		if(arr.length > 2)
			alrt = "Enter proper number";
		else
		{
			if(arr[0].length > num)
				alrt = "Enter "+ num +" digits for number\n";

			if(dec == -1)
			{
				if(arr[1].length > 0 || arr[1] == "")
					alrt = alrt + "Decimal values are not allowed\n";
			}
			else if(arr[1].length > dec)
			{
				alrt = alrt + "Enter "+ dec +" digits after decimal\n";
			}
		}

		if(alrt != "")
		{
			alert(alrt);
			elmObj.focus();
			return false;
		}
	}
	else if(val.length > num)
	{
		alert("Enter "+ num +" digits for number");
		elmObj.focus();
		return false;
	}

	return true;
}
//Email Validation
function ValidateEmail(emailOBj){
	emailfield = emailOBj.value;
	Restr = /^[a-z]([\.\_]?[a-z\d]+)*\@[a-z\d]+([\.\_\-][a-z\d]+)+$/ig;
	if (!Restr.test(emailfield)){
		alert("Please enter a valid E-mail.");
		emailOBj.select();
		return false;
	}
	return true;
}

function CheckData(parameterobj,SendName)
{ 
	
	// ---- /^([a-zA-Z0-9._,\#-]|\s)+$/ig;	
	//var CheckBlank = /^[a-zA-Z#][a-zA-Z0-9\.\s\\\/\#\&\,-_]*$/ig;
	var CheckBlank = /^[a-zA-Z0-9\.\s\\\/\#\&\,-_]+$/ig;
	if(trim(parameterobj.value) == "" ){

		alert("Please enter " + SendName );
		parameterobj.focus();
		return false;
	}
	if((parameterobj.value.charAt(0) == " ") || (!CheckBlank.test(trim(parameterobj.value)))){
		
		alert("Please enter a valid " + SendName);
		parameterobj.focus();
		return false;
	}
	return true;
}
function funDateDiff(param1, param2)
{
	startDate = param1.value;
	tmpDtOne = startDate.indexOf("/");
	tmpDtTwo = startDate.lastIndexOf("/");
	strDay = startDate.substring(0, tmpDtOne);
	strMon = startDate.substring(tmpDtOne+1, tmpDtTwo);
	strYear = startDate.substring(tmpDtTwo+1);
	startDate = strMon + "/" + strDay + "/" + strYear;
	   
	endDate = param2.value;
	tmpDtOne = startDate.indexOf("/");
	tmpDtTwo = startDate.lastIndexOf("/");
	strDay = startDate.substring(0, tmpDtOne);
	strMon = startDate.substring(tmpDtOne+1, tmpDtTwo);
	strYear = startDate.substring(tmpDtTwo+1);
	endDate = strMon + "/" + strDay + "/" + strYear;
	   
	startDate = new Date(startDate);
	endDate = new Date(endDate);
  	   
	if(startDate > endDate)
		return false;

	return true;
}
function checkDate(dvalue) // Date in (MM/DD/YYYY) Format
{
 	var arr = new Array(3);
       	 if(dvalue!='')
	{
		if( dvalue.split("/").length == 3 )
		{
			arr = dvalue.split("/");			
		}
		else
		{
			alert("Enter the date in MM/DD/YYYY format ");
			return false;
		}
		
		var year = arr[2];
		var month = arr[0];
		var day = arr[1];
       

		if( isNaN(year) || isNaN(month) || isNaN(day) )
		{
			alert('Please enter valid date');
			return false;
		}

		if(month<1 || month>12)
		{
			alert('Month should be 1 to 12');
			return false;
		}

		if(day<1 || day>31) 
		{
			alert('Day should be 1 to 31');
			return false;
		}

		/*if (year.length !=4 || !(year > 1900) )
		{
			alert('Year should be 4 digits (YYYY format)');
			return false;
		}*/
        if (!(year.length == 2 || year.length == 4))
		{
			alert('Year should be YY or YYYY format');
			return false;
		}
		
		if(month==4 || month==6 || month==9 || month==11)
		{
			if(day==31)
			{
				alert('Day 31 is not available in '+ month +' month.');
				return false;
			}
		}

		if(month==2)
		{
			var g=parseInt(year/4);
			/*if(isNaN(g))
			{
				alert('This is not a valid leap year.');
				return false;
			}	*/
			if( (day>29) )
			{
				alert('This is not a valid leap year.');
				return false;
			}	
		}
		return true;
	}
}
//Phone Validation
function validatePhone(pno,Statext)
{
	var Exp0 = /\-/g
	var Exp1 = /\s/g
	var Exp2 = /(^\b\d{3,3}\-\d{3,3}\-\d{4,4}\b$)|^\b\d{10,10}\b$/
	var Exp3 = /(^\b\d{3,3}\s+\d{3,3}\s+\d{4,4}\b$)|^\b\d{10,10}\b$/
	var Exp4 = /^\b\d{10,10}\b$/
	
	if (trim(pno.value) == ""){
		alert("Please enter a valid  " + Statext + " number. \nThe number should be 10 digits \nin length with or without spaces or\ndashes in between  \nExamples :\n999 999 9999  \n999-999-9999 \n9999999999");
		//pno.focus;
		return false;	
	}
    if (Exp0.test(trim(pno.value))) {
		if(!Exp2.test(trim(pno.value))){
			//alert("Length of "+ Statext +" Number must be 10 digits. \nThe Format should be in either of the ways \nxxx-xxx-xxxx  \nxxx xxx xxxx  \nxxxxxxxxxx");
			alert("Please enter a valid  " + Statext + " number. \nThe  number should be 10 digits \nin length with or without spaces or\ndashes in between  \nExamples :\n999 999 9999  \n999-999-9999 \n9999999999");
		return false;	
		}
	}
	else if(Exp1.test(trim(pno.value))){
		if(!Exp3.test(trim(pno.value))){
		//alert("Length of "+ Statext +" Number must be 10 digits. \nThe Format should be in either of the ways \nxxx-xxx-xxxx  \nxxx xxx xxxx  \nxxxxxxxxxx");
				alert("Please enter a valid  " + Statext + " number. \nThe  number should be 10 digits \nin length with or without spaces or\ndashes in between  \nExamples :\n999 999 9999  \n999-999-9999 \n9999999999");
		return false;	
		}
	}
	else{
		if(!Exp4.test(trim(pno.value))){
		//alert("Length of "+ Statext +" Number must be 10 digits. \nThe Format should be in either of the ways \nxxx-xxx-xxxx  \nxxx xxx xxxx  \nxxxxxxxxxx");
		alert("Please enter a valid  " + Statext + " number. \nThe  number should be 10 digits \nin length with or without spaces or\ndashes in between  \nExamples :\n999 999 9999  \n999-999-9999 \n9999999999");
		return false;	
		}
	}
	return true;
}	
// check AddressData
function CheckAddressData(parameterobj,SendName)
{ 	
	var CheckBlank = /^[a-zA-Z0-9\.\s\\\/\#\&\,-_]+$/ig;
	if(trim(parameterobj.value) == "" ){
      alert("Please enter the valid " + SendName + " for Location .");
		parameterobj.focus();
		return false;
	}
	if((parameterobj.value.charAt(0) == " ") || (!CheckBlank.test(trim(parameterobj.value)))){
		
		alert("Please enter a valid Address for the Location.");
		parameterobj.focus();
		return false;
	}
	return true;
}
function CheckNumber(parameterobj,SendName)
{
	var CheckNumber1 = /^\d*$/ig;
	if(!CheckNumber1.test(parameterobj.value))
	{
		alert("Please enter valid "+SendName + ".");
		parameterobj.focus();
		return false;
	}
	
	return true;
}
//Is Numeric Validation
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;
}

function ajaxObject(url, callbackFunction) {  
    var that=this;        
    this.updating = false;  
    this.abort = function() {    
        if (that.updating) {      
            that.updating=false;      
            that.AJAX.abort();      
            that.AJAX=null;    
        }  
    }  
    this.update = function(passData,postMethod) {     
        if (that.updating) { return false; }    
        that.AJAX = null;                              
        if (window.XMLHttpRequest) {                    
        that.AJAX=new XMLHttpRequest();                  
        } 
        else 
        {                                        
            that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");    
        }                                                 
        if (that.AJAX==null) {                                   
            return false;                                   
        } else {      
            that.AJAX.onreadystatechange = function() {          
                if (that.AJAX.readyState==4) {                       
                    that.updating=false;                          
                    that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);                  
                    that.AJAX=null;                                                 
                }                                                            
           }                                                              
           that.updating = new Date();                                    
           if (/post/i.test(postMethod)) {        
                var uri=urlCall+'?'+that.updating.getTime();        
                that.AJAX.open("POST", uri, true);        
                that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");        
                that.AJAX.setRequestHeader("Content-Length", passData.length);        
                that.AJAX.send(passData);      
               
                } else {        
                    var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());         
                    that.AJAX.open("GET", uri, true);                                     
                    that.AJAX.send(null);   
                    }                    
                    return true;                                                 
                }                                                                             
            }  
            var urlCall = url;          
            this.callback = callbackFunction || function () { };
  }
  
  // Validates for decimal when value is not empty
function ValidateNumericFormat(elmObj, num, dec)
{
	var checkFloat = /\./;
	var CheckNumber = /^\d*\.?\d*$/ig;
	var alrt = "";
	//val = trim(elmObj.value).replace(/\s/g,"");
	val = trim(elmObj.value);

	if(!CheckNumber.test(val))
	{
		alert("Please enter valid Number");
		elmObj.focus();
		return false;
	}

	dec = dec - 1;
	if(checkFloat.test(val))
	{
		arr = val.split(".");

		if(arr.length > 2)
			alrt = "Please enter valid Number";
		else
		{
			if(arr[0].length > num)
				alrt = "Please enter value as per the specified format";

			if(dec == -1)
			{
				if(arr[1].length > 0 || arr[1] == "")
					alrt = "Please enter value as per the specified format";
			}
			else if(arr[1].length > dec)
			{
				alrt = "Please enter value as per the specified format";
			}
		}

		if(alrt != "")
		{
			alert(alrt);
			elmObj.focus();
			return false;
		}
	}
	else if(val.length > num)
	{
		alert("Please enter value as per the specified format");
		elmObj.focus();
		return false;
	}

	return true;
}

function ValidateValueFormat(elmObj, num, dec)
{
	var checkFloat = /\./;
	var CheckNumber = /^\d*\.?\d*$/ig;
	var alrt = "";
	//val = trim(elmObj.value).replace(/\s/g,"");
	val = trim(elmObj.value);

	if(!CheckNumber.test(val))
	{
		alert("Please enter a valid Number");
		elmObj.focus();
		return false;
	}

	dec = dec - 1;
	if(checkFloat.test(val))
	{
		arr = val.split(".");

		if(arr.length > 2)
			alrt = "Please enter a valid Number";
		else
		{
			if(arr[0].length > num)
				alrt = "Please enter a valid number(XXX)";

			if(dec == -1)
			{
				if(arr[1].length > 0 || arr[1] == "")
					alrt = "Please enter a valid number(XXX)";
			}
			else if(arr[1].length > dec)
			{
				alrt = "Please enter a valid number(XXX)";
			}
		}

		if(alrt != "")
		{
			alert(alrt);
			elmObj.focus();
			return false;
		}
	}
	else if(val.length > num)
	{
		alert("Please enter a valid number(XXX)");
		elmObj.focus();
		return false;
	}

	return true;
}