// Single Date field validation

function isSingleValidDate(field)
 {
    
    if (field.value == "")    {
        alert("Not a valid format.\nTry again in 'DD/MM/YYYY' format.");
        field.focus();  
        return false;
    }
    //ok = "yes";
    
    if ( (field.value.length > 10) || (field.value.length < 8) )    {
        alert("Not a valid format.\nTry again in 'DD/MM/YYYY' format.");
        //field.value="";
        field.select();
        field.focus();        
        return true;
    }
    
    if( (field.value.length==8) || (field.value.length==9) ) 
    {
    i1 = -1;
    i2 = -1;
    count = 0;
    for (i=0; i<field.value.length; i++)    {
        if (i2 < 0)    {
            i1++;
        }
        else    {
            if (count < 2)    {
                i2++;
            }
        }
        if (field.value.substring(i,i+1) == "/")    {
            count++;
            if (i2 < 0	)    {
                i2 = i1;
            }
        }
    }
    
    }  // end if field =8 or 9
    
    if (field.value.length == 10)   {   
        if ( (field.value.substring(2,3) != "/") || (field.value.substring(5,6) != "/") )  

      {
        alert("Enter numeric values in the format DD/MM/YYYY");
        //field.value="";
        field.select();
        field.focus();
        return true;
      }
      else    
      {
          field1 = field.value.substring(0,2)+field.value.substring(3,5)+field.value.substring(6,10);            
          
      }    
    }  //end of else if length=10
    else 
        if ((field.value.length == 8) || (field.value.length == 9 )  && (count!=2))      
        {
	        field1 = field.value;
	    } 
    
    if  (( (field.value.length==8) || (field.value.length==9)  ) && (count==2))    
    {  
       
        day = field.value.substring(0,i1);
        month   = field.value.substring(i1+1,i2);
        //month = field.value.substring(0,i1);
        //day 	= field.value.substring(i1+1,i2);
        year  = field.value.substring(i2+1,i);
        
    }
    else
    {   
         //month=field1.substring(0,2);
         //day=field1.substring(2,4);
			day=field1.substring(0,2);
			month=field1.substring(2,4);
			year=field1.substring(4,8);
     }   
        if (isNotNumericVal(month) || isNotNumericVal(day) || isNotNumericVal(year))   
        {
        alert("Enter numeric values in the format 'DD/MM/YYYY'.");
        //field.value="";
        field.select();
        field.focus();
        return true; 
        }   
        
        if (((eval(day)>31) || (eval(day)<1))    ||
			((eval(month)>12) || (eval(month)<1)) ||
            ((eval(year)>9999) || (eval(year)<0)))    {
            alert("Not a valid date.\nTry again in 'DD/MM/YYYY' format.");                       
            //field.value="";
            field.select();
            field.focus();
            return true; 
        }
        
        else    { 
            if (eval(year)<1000)    {
                alert("Please enter the Year in 'YYYY' format.");
                //field.value="";
                field.select();
                field.focus();
                return true;
            } 
            
            if (((eval(month) == 4) || (eval(month) == 6) || 
                 (eval(month) == 9) || (eval(month) == 11)) &&
                ((eval(day) == 31)))    {
                alert("Not a valid Day for the Month specified.");
                //field.value="";
                field.select();
                field.focus();
                return true;
            }
            
            if (eval(month) == 2)    {
                if (eval(day) > 29)    {
                    alert("Not a valid Day for the Month specified.");
                    //field.value="";
                    field.select();
                    field.focus();
                    return true;
                }
                        
                
                leapyear = "no";
                if (year%4 == 0)        {
                    leapyear = "yes";
                    if (year%100 == 0)    {
                        leapyear = "no";
                        if (year%400 == 0)    {
                            leapyear = "yes";
                        }
                    }
                }
                
                if ((leapyear == "no") && (eval(day) == 29))  
                {
                    alert("Not a valid Day for the Month and Year specified.");
                    //field.value="";
                    field.select();
                    field.focus();
                    return true;
                }    
                
            } // end of if month=2
            
          } // end of else
            
         
if(day.indexOf("0")=="-1")
{
 if (day < 10)
 {
   day="0"+day;   
  } 
}
if(month.indexOf("0")=="-1")
{
if (month <10)
  {
    month="0"+month;
  }
}


//field.value=month+"/"+day+"/"+year;
field.value=day+"/"+month+"/"+year;
return false;
} // end of function


// Checks if the value is Numeric.

function isNotNumericVal(val)	{
    var valid = "0123456789";
    var temp;

    for (var i=0; i<val.length; i++)	{
        temp = val.substring(i,i+1);
        if (valid.indexOf(temp) == "-1")    {
            return true;
        }
    }
    return false;
}

function isTimeGreater(t1,t2)
{

h1=t1.substring(0,2);
m1=t1.substring(3,5);
h2=t2.substring(0,2);
m2=t2.substring(3,5);
if(h1>h2)
{
alert("Begin time cannot be greate than end time");
return 0; 
}
else if((h1==h2)&&(m1>m2))
{
alert("Begin time cannot be greate than end time");
return 0;
}
}


// Single Time field validation
function isSingleValidTime(field)
 {

    var firsttwo=field.value.substring(0,2);
    var aftercolon=field.value.substring(3,5);

    for (nCounter = 0; nCounter < firsttwo.length;nCounter++)
		{
			var sCharValue = firsttwo.charAt(nCounter);
			if (!(isDigit(sCharValue)))
			{
				alert("Please enter digits for (HH) .");
				field.focus();
				return 0 ;
			}
	}
	for (nCounter = 0; nCounter < aftercolon.length;nCounter++)
			{
				var sCharValue = aftercolon.charAt(nCounter);
				if (!(isDigit(sCharValue)))
				{
					alert("Please enter digits for (MM) .");
					field.focus();
					return 0;
				}
	}

    if ((field.value.length > 5) || (field.value.length < 3) )
    {
		alert("Not a valid date format.\nTry again in 'HH:MM' format.");
        field.value="";
        field.focus();
        return 0;
    }

    if (field.value.length == 5)
    {
		if ((field.value.substring(0,2) > 23) || (field.value.substring(3,5) > 59))
		{
			alert("Not a valid  format.\nTry again in 'HH:MM' format.");
			field.value="";
			field.focus();
			return 0;
		}
		else
		{
			hour = field.value.substring(0,2)
			minutes = field.value.substring(3,5)
		}
    }

    if (field.value.length == 4 )
    {
		if ((field.value.substring(0,2) > 23) || (field.value.substring(2,4) > 59))
		{
			alert("Not a valid format.\nTry again in 'HH:MM' format.");
			field.value="";
			field.focus();
			return 0;
		}
		else
		{
			hour = field.value.substring(0,2)
			minutes = field.value.substring(2,4)
		}

    }
    if (field.value.length == 3 )
    {
			hour = field.value.substring(0,1)
			minutes = field.value.substring(1,2)
    }

if (hour < 10 && hour.substring(0,1) != 0)
	hour = "0" + hour;

if (minutes < 10 && minutes.substring(0,1) != 0)
	minutes = "0" + minutes;
if ((field.value.indexOf(":")) == -1 )
	field.value = hour+":"+minutes;   // + ":"  +seconds;
else
	field.value = hour+":"+ minutes;

if (field.value.indexOf("::") != -1 || field.value.indexOf(":::") != -1)
		{
			field.value="";
			alert("Not a valid format.\nTry again in 'HH:MM' format.");
			field.value="";
			field.focus();
		}
return 1;
} // end of function

function isDigit (c)  //checks if the character is a digit
{
	return ((c >= "0") && (c <= "9"));
}

function (sTextValue)
{

var nCounter;

for (nCounter = 0; nCounter < sTextValue.length; nCounter++)
{
var sCharValue = sTextValue.charAt(nCounter);
if(sCharValue== " ")
return true;

}
return false;

}

function isSpace(sTextValue)
{

var nCounter;

for (nCounter = 0; nCounter < sTextValue.length; nCounter++)
{
var sCharValue = sTextValue.charAt(nCounter);
if(sCharValue== " ")
return true;

}
return false;

}


function isBlank(value)
{
	if(value == "") 
	{
		return true;
	}
}

// Trims the whitespaces on either side of the data entered in the field.

function trim(fieldval)    {
    //fieldval = field.value;
    for (i=0; i<fieldval.length; i++)    {
        temp = fieldval.substring(i,i+1);
        if (temp != " ")    {
            break;
        }
    }
    fieldval = fieldval.substring(i,fieldval.length);
    fieldval = fieldval;
    for (i=fieldval.length; i>0; i--)    {
        temp = fieldval.substring(i-1,i);
        if (temp != " ")    {
            break;
        }
    }
    fieldval= fieldval.substring(0,i);
    return fieldval;
}

/**
  * Function to check the given input is alphanumeric format or not
  */
function alphanumCheck(value) 
{
    var l_ch;
    var l_value;
    var l_index = 0;

	l_value = value;
 //   l_value = "" + trim(value);
/*
    if(isBlank(l_value)) 
	{
        return 0;
	}
*/
    if(l_value != "") 
	{
		while(l_index < l_value.length) 
		{
            l_ch = l_value.charAt(l_index);

            if((l_ch < 'a' || l_ch > 'z') 
				&& (l_ch < 'A' || l_ch > 'Z')
				&& (l_ch < '0' || l_ch > '9')
				&& (l_ch != '-')
				&& (l_ch != '/')
				&& (l_ch != ' ')
				&& (l_ch != ',')) 
			{
				return 0;
			}
            l_index++;
		}
        return 1;
	}
	return 0;
}


function isAllDigits(value) {
	var l_number = 0;
        var l_value = ""+value;

        if(l_value == "" || l_value.length == 0) {
                return 0;
	}

	for(var l_index = 0; l_index < l_value.length; l_index++) {
        	l_number = parseInt(l_value.charAt(l_index), 10);

	        if(isNaN(l_number) == true) {
        		return 0;
	        }
	}

	return 1;
}

function isLetter (c) //checks if the character is an alphabet
{   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}

function isDigit (c)  //checks if the character is a digit
{   
	return ((c >= "0") && (c <= "9"));
}

function isLetterOrDigit (c)  //checks if the character is an alphabet or digit
{   
	return (isLetter(c) || isDigit(c));
}


function isValidEmail(info)
{
	// check to see if email address is valid
	if (!validate_email(info.value))
	{
		alert('Your email address does not appear to be valid.  Please double check it.');
		info.select();
		info.focus();
		return false;
	}
	
	return true;
}

function validate_email(addr_email)
{
	if (addr_email.indexOf('@') < addr_email.lastIndexOf('.') && addr_email.indexOf('@') && is_valid_email_string(addr_email))
		return true;
	else
		return false;
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
	var k = 1; 
	var is_present=false;
    var sLength = s.length;
	//look for @ whether it is twice present in the string or not
	while(k < sLength)
	{
		if(s.charAt(k) == "@")
		{
			if(is_present == true)
			return false;
			is_present=true;
		}
		k++;
	}
	// look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    // look for .
    //i=sLength;
	while ((i < sLength) && (s.charAt(i) != "."))
    { i++;
    }
    // there must be at least two character after the .
    if ((i >= sLength - 2) || (s.charAt(i) != ".")) return false;
    else
	{
		i+=1;
		while(i < sLength)
		{
			var c = s.charAt(i);
			if (!(((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z"))))
			{	
				if(s.charAt(i) == ".")
				{
					var j=(sLength-1)-i;
					if (i ==(sLength-1) || j< 2 )	return false;
				}
				else
					return false;
			}
			i++
		}
		return true;
	}
		
}

// whitespace characters
var whitespace = " \t\n\r";
function isWhitespace (s)
{   var i;
    // Is s empty?
    if (isEmpty(s)) return true;
    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function containsinglequote(whichform,data,i)
{
	if (data != "")
	{
	
	var singlequotepos;
	singlequotepos = data.indexOf("'");
	
		if(singlequotepos != -1)
		{
			alert("Please do not use single quotes.");
			whichform.elements[i].select();
			whichform.elements[i].focus();
			return false;
		}
	}
	return  true;
		
}

function isBlankIndex(whichform, s , m)
{
//alert("isblank"+s);
	if (s!= "")
	{
	
		s= trimspace(s);
		//alert("s"+s);
		
		whichform.elements[m].value = s;
	}
	
	
	
	
		var len=s.length;
	//	alert("len"+len);
	
		if ( len == 0 )
			{
				alert("Field cannot be left blank.");
				whichform.elements[m].select();
				whichform.elements[m].focus();
				return false;
			}
		else
			{
			return true;
			}
	
}
var strtrim;
function trimspace(str)
{
	var len= str.length;
	if (len != 0)
	{
	for (var i=0;i<len;i++)
	{
		if(str.indexOf(" ")==0)
			str=str.substring(1,len);
	}
	strtrim=str;
	return strtrim;
	}
	
}