function blankchk(x,str)
{
	if (Trim(x.value)=="" || x.value==null)
	{
		alert(str+" field cannot be blank. Please fill a valid value !");
		x.value=Trim(x.value);
		x.focus();
		return false;
	}
	return true;
}

function Trim(s) 
{
	while((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{
		s = s.substring(1,s.length);
	}
	while((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{
		s = s.substring(0,s.length-1);
	}
	return s;
}

function numchk(x,str)
{
	var num=/[0-9]+/;
	numflag=x.value.match(num);
	if(x.value!="")
	{
		if(numflag != x.value)
		{
			alert("Please enter numbers only in "+str);
			x.focus();
			x.select();
			return false;
		}
		if(isNaN(x.value))
		{
			alert("Please enter proper Value");
			x.focus();
			return false;
		}
	}
	return true;
}

function numchkdash(x,str)
{
	var num=/[0-9\-\/]+/;
	numflag=x.value.match(num);
	if(x.value!="")
	{
		if(numflag != x.value)
		{
			alert("Please enter numbers only without space in "+str);
			x.focus();
			x.select();
			return false;
		}
	}
	return true;
}



function CheckSpace(x,str)
{
	var alpha=/[" "]+/;
	if(x.value!="")
	{
	    alphaflag=x.value.match(alpha);	
	    if(alphaflag == x.value)
		{
			alert(str+ ": Should be without space only.")
			x.focus();
 			x.select();
			return false;
	    }
	}    
	return true;
}

function CheckAlpha(x,str)
{
	var alpha=/[a-zA-Z]+/;
	if(x.value!="")
	{
	    alphaflag=x.value.match(alpha);	
	    if(alphaflag != x.value)
		{
			alert(str+ ": Should be alphabets without space only.")
			x.focus();
 			x.select();
			return false;
	    }
	}    
	return true;
}

function CheckAlphaSpace(x,str)
{
	var alpha=/[a-zA-Z" "]+/;
	if(x.value!="")
	{
	    alphaflag=x.value.match(alpha);	
	    if(alphaflag != x.value)
		{
			alert(str+ ": Should be alphabets with/without space only.")
			x.focus();
 			x.select();
			return false;
	    }
	}    
	return true;
}

function CheckAlphaNumeric(x,str)
{
	var alpha=/[a-zA-Z0-9" "]+/;
	if(x.value!="")
	{
	    alphaflag=x.value.match(alpha);	
	    if(alphaflag != x.value)
		{
			alert(str+ ": Should be alphaNumeric with/without space only.")
			x.focus();
 			x.select();
			return false;
	    }
	}    
	return true;
}

//Validating Pin No. (Max Length = 10
function CheckPinLengh(x,str)
{
    if(x!="")
    {               
        //Check For Length (xxxxxx)
        if(x.value.length<6 && x.value.length!=0)
        {
            alert(str+": Should have minimum length of 6 characters.");
            x.focus();
            x.select();
            return false;
        }      
    }
     return true;
}

//Validating Contact No. Min Length = 8
function CheckPhoneNoLength(x,str)
{
    if(x!="")
    {               
        if(x.value.length < 8)
        {
            alert(str + ": Should have minimum length of 8 characters.");
            x.focus();
            x.select();
            return false;
        }      
             
    }
     return true;
}

function CheckValueRange(x,str,min,max)
{
    var totalLength=x.value.length;
    if(totalLength<min || totalLength>max)
    {
        alert("Invalid " + str);
        x.focus();
        return false;
    }
    
    // Start with Zero's in the Field
    if(x.value.charAt(0)==0 && x.value.charAt(1)==0 ) 
    {
        alert("Invalid " + str);
        x.focus();
        x.select();
        return false; 
    }
    return true;
} 

function emailchk(x)
{
	var email =/[-a-zA-Z0-9_\.]+@[-a-zA-Z0-9]+\.[-a-zA-Z\.]+/;
	var eflag = x.value.match(email);
	if(eflag!=x.value)
	{
		alert("Please enter a valid Email.")
		x.focus();
		x.select();
		return false;
	}
	return true;
}

function dropdownchk(x,str)
{
	if(x.selectedIndex==0)
	{
		alert("Select a valid option from Field "+str+"!");
		x.focus();
		return false;
	}		 				
	return true;
}

function datechk(dd,mm,yyyy)
{
	switch(chkdate(dd,mm,yyyy))
	{
		case 1:
			alert("Invalid From Day");
			return false;
			break;
		case 2:
			alert("Invalid From Month");
			return false;				
			break;
		case 3:
			alert("Invalid From Year");
			return false;
			break;
		case 4:
			alert("This month has only 30 days");
			return false;
			break;
		case 5:
			alert("This is a leap year. Feb has only 29 days");
			return false;
			break;
		case 6:
			alert("This is not a leap year. Feb has only 28 days");
			return false;
			break;
		case 7:
			alert("This year is a leap year. Feb has only 29 days");
			return false;
			break;
		case 8:
			alert("The \"date\" cannot be greater than today\'s date");
			return false;
			break;
	}
	return true;
}
	
function chkdate(dd,mm,yyyy)
{
	var dt=new Date();
	invdate=mm.value+"/"+dd.value+"/"+yyyy.value;
	var invdate=new Date(invdate);
	sysdate = dt.getUTCMonth()+1 + '/' + dt.getUTCDate() + '/' + dt.getUTCFullYear();
	var sysdate=new Date(sysdate);
	if(dd.value==0 || dd.value>31)
		return 1;
	if(mm.value==0 || mm.value>12)
		return 2;
	if(yyyy.value==0)
		return 3;
	if((mm.value==4||mm.value==6||mm.value==9||mm.value==11) && (dd.value>30))
		return 4;
	if(mm.value==2 && dd.value>29 && yyyy.value%400==0)
		return 5;
	if(mm.value==2 && dd.value>28 && yyyy.value%4!=0)
		return 6;
	if(mm.value==2 && dd.value>29 && yyyy.value%4==0 && yyyy.value%400!=0)
		return 7;
	if (sysdate < invdate)
		return 8;
}
