function CheckNull(theString, Label)
{
	if(jtrim(theString).length == 0 )
	{
		alert("You need to enter " + Label); 
		return false;
	}	
	return true;
}

function CheckForNull(theString, Label)
{
	if(jtrim(theString).length == 0 )
	{
	//	alert("You need to enter " + Label);
		return false;
	}	
	else
	{
	return true;
	}
}

function CheckLength(theString, label, minlength, maxlength)
{
	stringlength = theString.length;
	minlength=eval(minlength);
	maxlength=eval(maxlength);
	if (stringlength >= minlength && stringlength <= maxlength )
		return true;
		    
	if (minlength == 0)
	{
		alert(" The length of " + label + " cannot exceed " & maxlength);
		return false;
	}	
			
	if(minlength == maxlength)
	{
		alert(" The length of " + label + " should be " + minlength);
		return false;
	}	
	if ((maxlength - minlength) == 1)
	{	
		alert(" The length of " + label + " should be either " + minlength + " or " + maxlength);
		return false;
	}		
	alert(" The length of " + label + " should be between " + minlength + " and " + maxlength);
	return false;
}

function ValidateTextBox(theString, Label,  minlength, maxlength,datatype)
{
	var TestNotNull = CheckNull(theString, Label)
	
	
	if (TestNotNull)
	{
	 return CheckLength(theString, Label, minlength, maxlength);  
	}
	else
	{
		return false;
	}	    
	
	return true;
}


function ValidateDescription(theString, Label,  minlength, maxlength,datatype)
{
	//var TestNotNull = CheckNull(theString, Label)
	//if (TestNotNull)
	//{
	    return CheckLength(theString, Label, minlength, maxlength);
	//}
	//else
//	{
//		return false;
//	}	    
	
	return true;
}


function ValidateListBoxes(theString, Label)
{

	if(jtrim(theString).length == 0 || jtrim(theString) =='0' ||jtrim(theString) =='-999')
	{
		alert("You need to select " + Label);
		return false;
	}
	return true;
}


function jtrim(str)
{
	  var i= 0;
	  var ltrimedStr = ""
	  var rtrimedStr = ""
	  l=str.length;
	  for (i=0;i<l;i++)
	  {
	  	if (str.charAt(i) != " ")
	  	break;
	  }
	  ltrimedStr = str.substring(i,l);
     k = ltrimedStr.length;	
	  for(j=k-1;j>=0;j--)
	 {
		
	   if (ltrimedStr.charAt(j)!= " ")
	    {
	  	    break;
	    }  
	 }
	   
	 rtrimedStr = ltrimedStr.substring(0,j+1);
	//	return str.substring(i,l);
	  return rtrimedStr;
}
	
	
	
function Compare(compareWith,compareString,message)
{
	if(compareWith == compareString )
		return true;
	else
	{
		if (message != "" )
			alert (message);
		return false;	
	}	
}

function rtrim(strt)
{
	var n,s,i;
	n=strt.length;
	for(i=n-1;i>=0;i--)
	{
		s=strt.charAt(i);
		if (s!=" ") 
		{
			return strt.substring(0,i+1);
		}
	}
	return "";
}

function emailvalidate(emailval)
{
        var tempStr,count;  //to get the count of the string entered and tempStr is to store the individual character.
        var blnmail,blnperiod; //checking for "@" and "."
        var lastoccofperiod,maxthree;
        var ampcount=0;
        var amppos;
        var servername = 1;
        var dots;
        
        count=emailval.length;
        blnperiod = 1;
        maxthree = 1;
        specialchar=0
        lastoccofperiod = 0;
        if (count==0)
        {
                return false;
        }
        
        
        //check for special characters that are not to be there in the email add
     
        for(i=0;i<count;i++)
        {
                tempStr = emailval.charAt(i);
                //alert(tempStr)
                if ((i==0) && ((tempStr=="_")||(tempStr=="-")||(tempStr==".")||(tempStr=="@")))
                {
					
					alert("Invalid email");
					return false;
                }
                else
                {
					if ((tempStr >='a')&&(tempStr <='z'))
					{
					        specialchar=specialchar+1;
					}
					else
					{
					        if ((tempStr >='A')&&(tempStr <='Z'))
					        {
					                specialchar=specialchar+1;
					        }
					        else
					        {
					                if ((tempStr >= 0)&&(tempStr<=9))
					                {
					                        specialchar=specialchar+1;
					                }
					                else
					                {
					                        if ((tempStr=="_")||(tempStr=="-")||(tempStr==".")||(tempStr=="@"))
					                        {
					                                specialchar=specialchar+1;
					                        }
					                        else
					                        {
					                                alert("Invalid Email");
					                                return false;
					                        }
									}
							}
					}
				}
		}
        
        dots = emailval.indexOf("..");
        if (dots != -1)
        {
                alert("Invalid Email");
                return false;
        }
        
        espace = emailval.indexOf(" ");
        if (espace != -1)
        {
                alert("Invalid Email");
                return false;
        }
        
        lastoccofperiod = emailval.lastIndexOf(".");
        if (lastoccofperiod <= 0)
        {
                blnperiod = 0;
        }
        
        if (((count - lastoccofperiod) > 4)||((count - lastoccofperiod) < 3))
        {
                maxthree = 0;
        }
        
        for(i=0;i<=count;i++)
        {
                tempStr = emailval.charAt(i)
                if (tempStr=="@")
                        ampcount=ampcount + 1;
        }
        
        
        amppos = emailval.indexOf("@");
        if (emailval.charAt(amppos+1) == ".") 
                servername = 0;
                
        //checking whether the "@" sign is the last one
        if (count - emailval.charAt(amppos)< 5)
                servername = 0;
                
        if ((ampcount==1)&&(blnperiod==1)&&(maxthree==1)&&(servername==1))
        {
                blnmail=1;
        }
        else
        {
                blnmail=0;
        }
        
        //return blnmail;
        if (blnmail==0)
        {
                alert("Please Enter a valid Email Address")
				return false;
        }
		else
		{
			return true;
		}
}

function isPosinteger(inputVal)
{
	inputStr=inputVal.toString();
	for(var i=0;i<inputStr.length;i++)
		{ 
			var oneChar=inputStr.charAt(i)
				
			if(oneChar<"0" || oneChar>"9")
			{
			 return false;
			}
		}
	return true;	
}	

function isinteger(inputVal)
{
	inputStr=inputVal.toString();
	for(var i=0;i<inputStr.length;i++)
		{ 
			var oneChar=inputStr.charAt(i)
			if(oneChar != ".")
			{
				if(oneChar<"0" || oneChar>"9")
				{
				 return false;
				}
			}
			if (oneChar == ".")
			{
				return false;
			}
		}
	//Indicates that the no entered is a valid one.
	return true;
}	


function Compare(compareWith,compareString,message)
{

	if(compareWith == compareString )
		return true;		
	else
	{
		if (message != "" )
		alert (message);
		return false;	
	}	

}

function IsValidMonthDay(mm,dd,message)
{
if(mm <= 12 )
{
	if(mm==1 || mm == 3 ||mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12)
		{
			if (dd >31)
			{
			alert('Enter a proper date for '+ message)
			return false;
			
			}
		}
	if(mm==4 || mm == 6 || mm == 9 || mm == 11 )
		{
			if (dd >30)
			{
			alert('Enter a proper date for '+message)
			return false;
			}
		}	
	if(mm==2)
		{	
			if (dd >29)
			{
			alert('Enter a proper date for '+ message)
			return false;
			}
		}		
	
return true;	
}
else
	{
	alert('Invalid Date');
	return false;
	}
}

function IsLeapYear(yyyy)
{
	if(yyyy % 4 == 0)
	{
	return true;
	}
	else
	{
		return false;	
	}
}

function IsdateValid(mm,dd,yyyy,message)
{
	if(IsValidMonthDay(mm,dd,message))
	{
		if (IsLeapYear(yyyy))
		{
			if (mm == 2)
			{
			if (dd > 29)
				{
				alert('Enter a proper date for '+message)
				return false;
				}
			else
				{
				return true;
				}
			}
		}
		else
		{
			if (mm == 2)
			{
			if (dd > 28)
				{
				alert('Enter a proper date for '+message)
				return false;
				}
			else
				{
				return true;
				}
			}
		}
		return true;
	}
	else
	{
		
		return false;
	}
	
}

function showImage(largeImage)
{
	window.open ('prodLargeImg.asp?largeImage='+largeImage,'newwind','left=0,top=0,width=400,height=400')
}



function isAlphaCharOrSpace(InString) {
  	if(InString.length!=1){
		return false;
	}

	InString=InString.toLowerCase();
    RefString=" abcdefghijklmnopqrstuvwxyz.";
	
	//alert(RefString.indexOf(InString,0));
	
	if (RefString.indexOf(InString,0) == -1){
		return false;
	}

	return true;
}


function Description(theField,label) {
     
	var fldValue = theField.length;
	//alert (fldValue);
	for (var i = 0; i < fldValue;  i++) {
		var ch = theField.substring(i,i+1);
	//	alert(ch);
        var ret = isAlphaCharOrSpace(ch);
		if (ret == 0) {
			alert( label + " cannot contain any characters other than alphabets and \nspaces");
            return(false);
            break;
        }
    }
	return(true);
}



//To check for characters and numbers
function ValidateUserName(theStr)
{
	// Get the value of the UserName text box
	var strUserName = theStr
	var strCurrentChar;

	// This variable will determine if our string is valid
	var bolValidUserName = true;

	// Step through the UserName one character at a time...
	for (var iLoop=0; iLoop < strUserName.length; iLoop++)
	{
		// Check to see if the current character is valid
		strCurrentChar = strUserName.substring(iLoop,iLoop+1);

		if (
			// if A-Z...
			(strCurrentChar >= 'A' && strCurrentChar <= 'Z')
			// or if a-z...
			|| (strCurrentChar >= 'a' && strCurrentChar <= 'z')
			// or if 0-9...
			|| (strCurrentChar >= '0' && strCurrentChar <= '9')
			// or if dash or underscore
			|| strCurrentChar == ' '
		   );

		   // We have a valid string... do nothing.
		  else
		   // We have an invalid string, set the flag
		   bolValidUserName = false;
	}
	return bolValidUserName;
}
