//global variable for message
var v1="Select a valid option";

//This function validates the textfield for numeric value
function isNumeric(textField,type)
{
	var theChar,message,text,temp;
	var length,dotCount=0,perCount=0;
	var numeric;
		
	message = "Enter only numeric value.";
	numeric = true;	
	text = textField.value;
	length = text.length;
	
	//alert("text===" + text);
		 
	for(var i=0 ; i<length ; i++)	   //Iterate through entire text value.
	{
		theChar = text.substring(i,i+1);  //Extract a Character and check it.
		if(type == "real" && ((theChar<"0" || theChar>"9") && theChar != "."))
			
			numeric = false;
		else if(theChar == ".")           //check for multiple appearence of "."
			dotCount++;
		else if(type == "per" && ((theChar<"0" || theChar>"9") && theChar != "." && theChar=="%"))
		{				
			numeric = false;

		}
	//	else if(type=="per" && theChar == "%")
	//	{	
	//	
	//		perCount++;
	//	}	
			
		if(type == "integer" && (theChar<"0" || theChar>"9"))
		{
			message="Enter only numeric integer value."
			textField.value = "";
			textField.focus();
			numeric = false;
		}
	}
	

	if(!numeric || dotCount > 1 || perCount>1) 
	{
		alert(message);
		textField.value = "";
		textField.focus();
		return false;
	}
	else if(text.charAt(length-1) == "." )
	{
		if(text.length==1)
			textField.value="0";
//		else
//			textField.value = textField.value + "0";
	}
	
	/********************	
	if ((type == "per" && text.parseInt(textField.value,10) > 100) || (type == "per" && 
	text.parseInt(text.substring(0,length-1),10) > 100 && (text.charAt(text.length-1) == 	"%")))
	{
		
		alert("Less than 100%");
		textField.value = "";
		textField.focus();
		return false;
		
	}****************/
	if(type=="per")
	{
			var b,a="";
			b=text;
			
			if (b.charAt(b.length-1)=='%')
			{
				//alert('hj');

				
				for(var i=0;i<b.length-1;i++)
					a=a+b.charAt(i);
					
			b=a;
			}
		if(b>100)
		{
			alert("Enter percentage less than or equal to 100.");
			textField.value = '';
			textField.focus();
			return false;
		}
	}
	return true;
}



function isBlankNumeric(theField)  
{
	var theText = theField.value;
	var flag;
	flag=false;
	if (theText=="")
		{			
			message = "Please enter the Number.";
		}
	else
	if (theText=="0")
		{
			message = "Cannot Be Blank Or Zero  ";
		}		
	else
		{
			for (i=0;i<theText.length;i++)
				{
					if (theText.charAt(i)!=" ")
						{
							flag=true;
						}
				}
		message = "Please enter the Number.";
		}
//	message = "Please enter the Number.";
	if (flag==false)
	{	
		alert(message);
		theField.focus();
		return false;	
	}
	return true;
}


//This function checks for validates the textfields for  character data.
function isCharacter(theField)
{
	var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-, .0123456789 !@#$%^&*()_+|\`~`";
	var theText = theField.value;
	var mess = " ";
	var valid = true;
	var count=0;
	
	for(var i=0;i<theText.length;i++)
	{
		var aChar = theText.substring(i,i+1);
		if (aChar==" ")
		{
			count = count+1;
		}
		if(str.indexOf(aChar)< 0 && valid==true)
		{
			mess += "Enter only valid characters.\n";
			valid = false;
		}
	}
	if(count==theText.length)
	{
		mess +="Input valid Characters.\n";
		valid = false;
	}
	if(!valid)
	{
		alert(mess);
		theField.value="";
		theField.focus();
		return false;
	}
	return true;
}

function isBlank(theField)  
{
	var theText = theField.value;
	var flag;
	flag=false;
	if (theText=="")
		{
		}
	else
		{
			for (i=0;i<theText.length;i++)
				{
					if (theText.charAt(i)!=" ")
						{
							flag=true;
						}
				}
		}
	message = "Please enter the text.";
	if (flag==false)
	{	
		alert(message);
		theField.focus();
		return false;	
	}
	return true;
	
	
//	if (theText=="")
//	{	
//		alert(message);
//		theField.focus();
//		return false;	
//	}
//	return true;
}

function isBlankText(theField)  
{
	var theText = theField.value;
	var flag;
	flag=false;
	if (theText=="")
		{
		}
	else
		{
			for (i=0;i<theText.length;i++)
				{
					if (theText.charAt(i)!=" ")
						{
							return true;
						}
				}
		}
		return false;	
	
	return true;
}



function isNumericBlank(theField)  
{
	var theText = theField.value;
	message = "Please enter the value.";
	if (theText=="")
	{	
		alert(message);
		theField.focus();
		return false;	
	}
	return true;
}

function isText(theField, QNo)  
{
//	var theText = theField.value;
	message = "Please enter the value in " + QNo;
//	if (theText=="")
//	{	
//		alert(message);
//		theField.focus();
//		return false;	
//	}
//	return true;
	var theText = theField.value;
	var flag;
	flag=false;
	if (theText=="")
		{
		}
	else
		{
			for (i=0;i<theText.length;i++)
				{
					if (theText.charAt(i)!=" ")
						{
							flag=true;
						}
				}
		}
	if (flag==false)
	{	
		alert(message);
		theField.focus();
		return false;	
	}
	return true;
}



function checkRadios() {
 var el = document.forms[0].elements;
 var radiogroup;
	 
 for(var i = 0 ; i < el.length ; ++i) {
	
  if(el[i].type == "radio") {
   radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   //alert(el[i].name);
   var itemchecked = false;

	for(var j = 0 ; j < radiogroup.length ; ++j) {
    	if(radiogroup[j].checked) {
	itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
	
    alert("Please select an option ");
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
} 



function checkRadiosMatrix() {
 var el = document.forms[0].elements;
 var radiogroup;
	 
 for(var i = 0 ; i < el.length ; ++i) {
	//alert(i);
  if(el[i].type == "radio") {
   radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   //alert(el[i].name);
   var itemchecked = false;

	for(var j = 0 ; j < radiogroup.length ; ++j) {
    	if(radiogroup[j].checked) {
	itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
	//alert(el[i].id.substr(2,1));
    //alert(el[i].id.charAt(2));
   //alert(el[i].id);
    alert("Please select an option for row ("+el[i].id.substr(0,1)+").");
    //alert("Please select an option ");
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
} 

function isalpha(theField)
{
	var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var theText = theField.value;
	var mess = " ";
	var valid = true;
	var count=0;
	
	for(var i=0;i<theText.length;i++)
	{
		var aChar = theText.substring(i,i+1);
		if (aChar==" ")
		{
			count = count+1;
		}
		if(str.indexOf(aChar)< 0 && valid==true)
		{
			mess += "Enter only Alphabet.\n";
			valid = false;
		}
	}
	if(count==theText.length)
	{
		mess +="Input only alphabet.\n";
		valid = false;
	}
	if(!valid)
	{
		alert(mess);
		theField.value="";
		theField.focus();
		return false;
	}
	return true;
}

function isAlphanumeric(theField)
	{
	var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var theText = theField.value;
	flag=false;
	if (theText=="")
		{
		}
	else
		{
			for (i=0;i<theText.length;i++)
				{
				for (j=0;j<str.length;j++)
					{
					if (theText.charAt(i)==str.charAt(j))
						{
							flag=true;
						}
					}	
				}
		}
	message = "Cannot Enter Blank Or Numeric Value.";
	if (flag==false)
	{	
		alert(message);
		theField.focus();
		return false;	
	}
	return true;
	
}



function isBlankInd(theField,txt)  
{
	var theText = theField.value;
	var flag;
	flag=false;
	if (theText=="")
		{
		}
	else
		{
			for (i=0;i<theText.length;i++)
				{
					if (theText.charAt(i)!=" ")
						{
							flag=true;
						}
				}
		}
	message = "Please enter the "+txt +".";
	if (flag==false)
	{	
		alert(message);
		theField.focus();
		return false;	
	}
	return true;
	
	
//	if (theText=="")
//	{	
//		alert(message);
//		theField.focus();
//		return false;	
//	}
//	return true;
}


function checkBoxes()
{
	var el = document.forms[0].elements;
	var checkgroup;
	var checkname;
	var itemchecked = false;	 
	var flg = false;	 
	var i;
	checkname="";
	for(i= 0 ; i < el.length ; ++i) 
	{
		if(el[i].type == "checkbox")
		{
			if (flg == false)
			{
				checkname = el[el[i].name];
				flg=true;
			}
			checkgroup = el[el[i].name]; // get the whole set of radio buttons.
			if (checkgroup.checked)
			{
				itemchecked = true;
				break;
			}
		}	
	}
			if(!itemchecked) 
			{ 
				alert("Please select an option");
					checkname.focus();
				return false;
			}
	return true;
} 

function checkmultiRadios()
{
 var el = document.forms[0].elements;
 var radiogroup;
	 
 for(var i = 0 ; i < el.length ; ++i) {
	//alert(el.length);
	//alert(i); 
  if(el[i].type == "radio") {
   var itemchecked = false;


    	if(el[i].checked) {
	itemchecked = true;
	 break;
	}
   }
}
   if(!itemchecked) { 
    alert("Please select an option ");
    //if(el[i].focus)
     el[0].focus();
	return false;
   }
  //}
 //}
 return true;
}


function CheckBoxesCount()
{
	var el = document.forms[0].elements;
	var checkgroup;
	var cnt=0;
	var checkname;
	var itemchecked = false;	 
	var flg = false;	 
	var i;
	checkname="";
	for(i= 0 ; i < el.length ; ++i) 
	{
		if(el[i].type == "checkbox")
		{
			if (flg == false)
			{
				checkname = el[el[i].name];
				flg=true;
			}
			checkgroup = el[el[i].name]; // get the whole set of radio buttons.
			if (checkgroup.checked)
			{
				cnt=cnt+1;
			
			}
		}	
	}
	return cnt;
} 