/* This function will finds the length of the given string and if it exceeds
   length then it displays an appropriate message. 
   data = string to be checked for length.
   upperLeng = specified string upper length.
   lowerLeng = specified string lower length.

*/
function checkoption(data)
{
 if(data.selectedIndex==0)
 {
   alert("Please Select "+FieldName);
   data.focus();
   return false;
 }
  else return true;
}
//Fenil -- For Store Selection
function checkuser(data1,data2)
{
 if(data1.selectedIndex==3 && data2.selectedIndex==0)
 {
   alert("Please Select "+FieldName);
   data2.focus();
   return false;
 }
  else return true;
}

//Fenil
function checkLength(data, upperLeng, lowerLeng)
{	
	if(data.value.length > upperLeng )
	{			
		alert("Length Of "+FieldName+" Should Not Be More Than "+upperLeng);		
		data.focus();
		return false
	}
	else if (data.value.length < lowerLeng)
	{	
		if (lowerLeng==1)			
			alert(FieldName+" Should Not Be Empty");
		else if (lowerLeng >1 && lowerLeng<11)
			alert(FieldName+" Atlest "+lowerLeng+" Character Long");
		data.focus();
		return false
	}
	else
		 return true
}

/*
	it checks the number words have been entered in the given field
*/
function checkWordCount(data, upperLeng, lowerLeng)
{
	strValue = data.value
	wordCount = strValue.split(" ")

	if (wordCount.length > upperLeng)
	{
		alert("number of words should not be more than  "+upperLeng +" in the "+data.name)
	data.focus()
		return false
	}
	else if(wordCount.length <= lowerLeng)
	{
		alert("number of words should be more than  "+lowerLeng +" in the "+data.name)
	data.focus()
		return false
	}
	else
	 return true
}

/*
  This function checks whether the data has been sent is numeric or string
  if data is not numeric then it displays an appropriate error message.
*/

function checkPhone(data, leng)
{	//40,41,45,32,44 for (,),-," ",, characters respectively
	var num,i
	result=true
	num=data.value;	
	if(num.length > leng)
	{
		alert(FieldName+"  should not exceed "+leng+" digits");
		data.focus()
		return false
	}
	else
	{
		for(i=0; i<num.length ; i++)
		{
			if((num.charCodeAt(i)!= 44 && num.charCodeAt(i)!= 32 && num.charCodeAt(i)!= 40 && num.charCodeAt(i)!= 41 && num.charCodeAt(i)!= 45 )&&( num.charCodeAt(i)<48 || num.charCodeAt(i)>57 ))
			{
				alert(FieldName+" is invalid")
				data.focus()
				result = false		
				break;
			}
			else
			{
				result = true
			}
		}
		return result
	}
	
}

/*
  This function checks whether the data has been sent is numeric or string
  if data is not numeric then it displays an appropriate error message.
*/

function checkNumeric(data,leng)
{	
	var num,i
	num=data.value;
	result = true

	if(num.length > leng)
	{
		alert("Block should not exceed "+leng+" digits")
		return false
	}
	else
	{
		for(i=0; i<num.length ; i++)
		{
			if( (num.charCodeAt(i)<48 || num.charCodeAt(i)>57) )
			{
				alert(FieldName+" Should Be Numeric Only")
				data.focus()
				result = false
				break;
			}
			else
			{
				result = true
			}
		}
		return result
	}
}

function validemail(frmName, field , msg)  
{
	var at="@"
	var dot="."
	
	str1 = eval("document."+frmName+"."+field);
	str = str1.value;
	
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	    alert(msg);
		str1.focus();
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	    alert(msg);
		str1.focus();
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert(msg);
		str1.focus();
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert(msg);
		str1.focus();
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(msg);
		str1.focus();
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert(msg);
		str1.focus();
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert(msg);
		str1.focus();
		return false
	 }

	 return true					
}

function checkEmail(data)
 {
		//strEmailId	=  data.value;		
		var msg = "Please enter valid "+FieldName+" address.";
		var at="@"
		var dot="."
		
		str1 = eval(data);
		str = str1.value;
		
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		//HERE EMAIL ID IS AN OPTIONAL FIELD IF NO DATA THEN NO VALIDATION.
/*		if (str == "")
		{
			return true // DONT VALIDATE EMAIL FIELD
		}
*/		if(str != "")
        	{
		    //checks that last character is not "."
/*		    	if( str.lastIndexOf(".") == data.value.length - 1)
            	{  
					alert("Please correct "+FieldName)
					data.focus()
					return false
		    	}
*/		    	
					if (str.indexOf(at)==-1){
						alert(msg);
						str1.focus();
					   return false
					}
				
					else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
						alert(msg);
						str1.focus();
					   return false
					}
				
					else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
						alert(msg);
						str1.focus();
						return false
					}
				
					else if (str.indexOf(at,(lat+1))!=-1){
						alert(msg);
						str1.focus();
						return false
					 }
				
					 else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
						alert(msg);
						str1.focus();
						return false
					 }
				
					 else if (str.indexOf(dot,(lat+2))==-1){
						alert(msg);
						str1.focus();
						return false
					 }
					
					 else if (str.indexOf(" ")!=-1){
						alert(msg);
						str1.focus();
						return false
					 }
					else
		 				return true     
            }
         else
            {
            	/*alert("Please correct "+FieldName)
		    	data.focus()	
		    	return false*/
				return true;
            }             
 }
function checkSpace(data){
	var Mystring=new String(data.value)
	var Idx=Mystring.search(" ") 
	if (Idx==-1){
	 return true}
	 else{
	 alert ("Please Remove The Space")
	 data.focus()
	 return false
	 }
}

function checkURL(data)
 {
		strEmailId	=  data.value;
		//HERE EMAIL ID IS AN OPTIONAL FIELD IF NO DATA THEN NO VALIDATION.
		if (strEmailId == "")
		{
			return true // DONT VALIDATE EMAIL FIELD
		}

		 if(strEmailId.indexOf(".") > 1)
             {
		    //checks that last character is not "."
		    if( strEmailId.lastIndexOf(".") == data.value.length - 1)
                {  
			alert("Please correct URL")
			data.focus()
			return false
		    }
		    else
		 	return true     
             }
             else
             {
                alert("Please correct URL")
		    data.focus()	
		    return false	

             }
             
          }
          
          
function checkZip(data, leng)
{	//40,41,45,32,44 for " " characters respectively
	var num,i
	result=true
	num=data.value;	
	if(num.length > leng)
	{
		alert(FieldName+" Should Not Be Exceed "+leng+" Digits");
		data.focus()
		return false
	}
	else
	{
		for(i=0; i<num.length ; i++)
		{
			if(num.charCodeAt(i)!= 32&&( num.charCodeAt(i)<48 || num.charCodeAt(i)>57 ))
			{
				alert(FieldName+" is invalid")
				data.focus()
				result = false		
				break;
			}
			else
			{
				result = true
			}
		}
		return result
	}
	
}
function ValidImage(x)
{		
				var y=x.value;					
				var imglen=y.length;
				var imgdotpos=y.lastIndexOf(".");
				var imgext=y.substring(imgdotpos+1,imglen);			
				if((imgext!="jpg")&& (imgext!="gif")&&(imgext!="bmp"))
				{
					alert("For "+FieldName+", Please select jpg or gif or bmp image only")
					x.select();
					x.focus();
					return false;
				}
				return true;
	}	
function ValidPDF(x)
{		
				var y=x.value;					
				var imglen=y.length;
				var imgdotpos=y.lastIndexOf(".");
				var imgext=y.substring(imgdotpos+1,imglen);			
				if(imgext!="pdf")
				{
					alert("For "+FieldName+", Please select pdf file only")
					x.select();
					x.focus();
					return false;
				}
				return true;
	}	
