//Checks for blanks
//Pass the field name 
//returns false if the conditions are not met and true if it is met
function checkDefault(defVal)
	{
		str_Value=defVal.value;
		for(count = 0; count < str_Value.length; count++)
        {
                if(str_Value == 'default')
                {
                        //alert("Pls select your country");
                        return false;
                }
        }
        return true;
	}

function checkblank(field)
{
			str_Value=field.value;
			if(str_Value.length==0)
			{
				int_Flag=0;
			}
			for(i=1;i<=str_Value.length ;i++)
			{
				if(str_Value.charAt(i-1) == " ")
				{
					int_Flag=0;
				}
				else
				{
					int_Flag = 1;
					break;
				}
			}
			if(int_Flag != 1)
			{
				return false;
			}
	return true;			
}

//validation for space
function checkspace(field)
{
        s_val = field.value;
        for(count = 0; count < s_val.length; count++)
        {
                if(s_val.charAt(count) == ' ')
                {
                        alert("Space not allowed");
                        return false;
                }
        }
        return true;
}

//Validation for port number before & after comma
function checkcomma(field)
{
	s_val = field.value;
	len = s_val.length;
        if(s_val.indexOf(",") > -1)
        {
                if(s_val.charAt(0) == ',' || s_val.charAt(len-1) == ',')
                {
                        //alert("Please enter a port number");
                        return false;
                }
        }
	return true;
}

//Prevents any other characters except numbers being entered - Works only with IE
//call the function on onkeypress of the textfield
function numKeyPress()
{
		if(event.keyCode >= 48 && event.keyCode <= 57)
		{
			return true;	
		}
		else return false;
}

//Checks for a valid number
//Pass the type,field name 
//if type = "+" it will validate for +ve numbers any other type will validate for all numbers
//returns false if the conditions are not met and true if it is met

function checknumber(type,field)
{
	object_value = field.value;
	var start_format = "";
        
    //Returns true if value is a number defined as  having an optional leading + or -.
    //   having at most 1 decimal point. otherwise containing only the characters 0-9.
	if (type == "+")
	{
		start_format = " .+0123456789";
	}
	else
	{
		start_format = " .+-0123456789";
	}
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
    
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
	return true;
}

//validation for URL
function checkurl(field)
{
        var strValue = field.value;
        var count;
        var len;
        if(strValue.indexOf(".") == -1)
        {
                return false;
        }
        else
        {
                count = strValue.lastIndexOf(".");
                len = strValue.length - 1;
                if(count >= len)
                {
                        return false;
                }
                else
                {
                        return true;
                }
        }
}

//checks for decimal point
function checkfordecimal(field)
{
        var num = field.value;
        if(num.indexOf(".") > -1)
        {
                alert("Only whole numbers are allowed");
                return false;
        }
        return true;
}

//Checks for special characters
//Pass the form field
//returns false if the conditions are not met and true if it is met
//The special charaters are given in the var splchar
//One can include their own special characters..
//better still modify this function and pass the special characters

function checkspecial(field)
{
	obj_val = field.value;
	var splChar = "+-=~!@#$%^&*()\"\';";
	
	for(var i=0; i<obj_val.length; i++)
	{
		check_char = splChar.indexOf(obj_val.charAt(i))
		//Returns value 1 if the special character listed in splChar is found
		if(check_char >= 1)
		{
			alert("Special characters not allowed");
			return false;
		}
	}

return true;
}


//Checks for special characters
//Pass the form field and a list of special characters you want to check against
//returns false if the conditions are not met and true if it is met

function checkspecial(field,s_spl)
{
	obj_val = field.value;
	for(var i=0; i<obj_val.length; i++)
	{
		check_char = s_spl.indexOf(obj_val.charAt(i))
		//Returns value 1 if the special character listed in splChar is found
		if(check_char >= 0)
		{
			alert("Special characters not allowed");
			return false;
		}
	}

return true;
}


//same as above but no message displayed
function checkspecialnomess(field,s_spl)
{
        obj_val = field.value;
        for(var i=0; i<obj_val.length; i++)
        {
                check_char = s_spl.indexOf(obj_val.charAt(i))
                //Returns value 1 if the special character listed in splChar is found
                if(check_char >= 0)
                {
                        return false;
                }
        }

return true;
}


//Clears all the form values of type text
function clearformT(Form)
{
			for(i=0; i<Form.elements.length; i++)
			{
				if(Form[i].type == "text")
					Form[i].value="";
				if(Form[i].type == "password")
					Form[i].value="";
                                if(Form[i].type == "radio")
                                        Form[i].checked = false;
			}
}

//checks for validity of the date
//takes in two parameters-form fields 
//checks for the format dd-mon-yyyy
//checks whether the second date is not greater than the first one
//A relevant alert is displayed inside this function so no need to give in the 
//calling page
//returns false if the conditions are not met and true if it is met

function  checkdate(curstr,str)
{
	curstr = curstr.value;
	var cdates = curstr.split("-")
	str = str.value;
	
	assarray = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"];
	dayarray = [31,29,31,30,31,30,31,31,30,31,30,31];
	count = 0;
	flag=0;
	cmoncount = 0;
	moncount = 0;
	var index=str.indexOf("-");

	if(index == -1)
	{
		alert("Please enter the date in dd-mon-yyyy");
		return false;
	}
	else
	{
		dates=str.split("-");
		months=dates[1];
		months=months.toLowerCase();
		flag1=0;
	

	for(i=0; i<assarray.length; i++)
	{
		if(assarray[i] == cdates[1])
		{
				cmonflag=i;
				break;
		}
		else
		{
				continue;
		}
	}//for
		
	for(i=0; i<assarray.length; i++)
	{

		if(assarray[i] == months)
		{
				flag=1;
				monflag = i;
				dayscheck = dayarray[count];
				break;
		}
		else
		{
			flag=0;
			continue;
		}
	}//for

	if(flag != 1)
	{
		flag1=1;
		alert("Enter the proper month"); 
		return false;
	}

	days=dates[0];
	years=dates[2];
	

	leap = years % 4
	if(days > dayscheck || days < 1)
	{
		alert("Enter the proper day");
		flag1=1;
		return false;
	}
	if(isNaN(days))
	{
		alert("Enter days in numerals");
		flag1=1;
		return false;
	}

	if(isNaN(years))
	{
		alert("Enter years in numerals");
		flag1=1;
		return false;;
	}

	if(years < 1997 )
	{
		alert("Enter a year greater than 1996");
		flag1=1;
		return false;
	}

	if(months =="feb" && leap ==0 && days >29)
	{
		alert("Check out for leap year");
		flag1=1;
		
	}

	if(months == "feb" && leap >0 && days >28)
	{
		alert("Enter the proper day for february month")
		flag1=1;
	}

	}//else
		//checking for a date less than or equal to current date				
		if (years > cdates[2])
		{
			alert("The salary revisal date cannot be greater than the current date");
			return false;
		}
		else if(years == cdates[2] && monflag > cmonflag)
		{
			alert("The salary revisal date cannot be greater than the current date");
			return false;
		}	
		else if(years == cdates[2] && monflag == cmonflag && days > cdates[0])
		{
			alert("The salary revisal date cannot be greater than the current date");
			return false;
		}
		return true;
	
} //function
//checks for two characters after the period in decimal numbers
function check_decimal(field)
{
	num = field.value;
	numvals = num.split(".");
	if(!numvals[1])
	{
		return true;
	}
	if(numvals[1].length > 2 )
	{
		alert("More than two characters are not allowed after the period")
		return false;
	}
	return true;
}	

//Checks for numbers in the beginning of mailid
//Checks for @ and . in mailid
//Checks if the consecutive characters are @.
//Checks for more than one @
function checkmailid(field,s_spl)
{
        obj_val = field.value;
        num = 0;
        var flag;
        if(obj_val.charAt(0).indexOf("0") > -1 
		|| obj_val.charAt(0).indexOf("1") > -1 
		|| obj_val.charAt(0).indexOf("2") > -1 
		|| obj_val.charAt(0).indexOf("3") > -1 
		|| obj_val.charAt(0).indexOf("4") > -1 
		|| obj_val.charAt(0).indexOf("5") > -1 
		|| obj_val.charAt(0).indexOf("6") > -1 
		|| obj_val.charAt(0).indexOf("7") > -1 
		|| obj_val.charAt(0).indexOf("8") > -1 
		|| obj_val.charAt(0).indexOf("9") > -1)
        {
                flag = true;
        }
        for(var i=0; i<s_spl.length; i++)
        {
                check_char = obj_val.indexOf(s_spl.charAt(i))
                if(check_char < 1)
                {
                        flag = true;
                }
        }
        if(obj_val.charAt(check_char) == '.' && obj_val.charAt(--check_char) == '@')
        {
                flag = true;
        }
	for(c=0; c < obj_val.length; c++)
        {
                if(obj_val.charAt(c) == '@')
                {
                        num++;
                }
        }
        if(num > 1)
        {
                flag = true;
        }
        for(var k=0; k < obj_val.length;k++)
        {
        	if(obj_val.charAt(k) == '.' && obj_val.charAt(k + 1) == '.')
             {
              	//alert("Cannot have '.' in consecutive positions.");
                          flag = true;
             }
             else
             {
                         continue;
             }
        }
        if(flag == true)
        {
                alert("Mail id is invalid");
                return false;
        }
        return true;
}

function validateIp(fieldvalue)
{
  var s_val = fieldvalue;
  var dot_count = 0;

  var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

  for(var m=0; m<s_val.length; m++)
  {
        check_char = alpha.indexOf(s_val.charAt(m))
        //Returns value 1 if the special character listed in splChar is found
        if(check_char >= 1)
        {
                alert("Alphabets are not allowed.");
                return false;
        }
  }
 for(var k=0;k < s_val.length;k++)
  {
        if(s_val.charAt(k) == '.' && s_val.charAt(k + 1) == '.')
        {
                alert("Cannot have '.' in consecutive positions.");
                return false;
        }
        else
        {
          continue;
        }
  }
  for(var i=0;i < s_val.length;i++)
  {
        if(s_val.charAt(i) == '.')
        {
          dot_count = dot_count + 1;
        }
        else
        {
          continue;
        }
  }
 if(dot_count != 3)
  {
        alert("Invalid IP address. Please check the entries properly.");
        return false;
  }
  if(s_val.charAt(0) == '.' || s_val.charAt(s_val.length - 1) == '.')
  {
        alert("IP address should not start or end with a '.' ");
        return false;
  }
  for(var j = 0; j < s_val.length; j++)
  {
        if(s_val.charAt(j) == ' ')
        {
                alert("Space not allowed in IP address.");
                return false;
        }
  }
  var splChar = "`~!@#$%^&*()-_+={}[]|\\:;\"\'<>,?\/";
 for(var k=0; k<s_val.length; k++)
  {
       check_char = splChar.indexOf(s_val.charAt(k))
       //Returns value 1 if the special character listed in splChar is found
       if(check_char >= 1)
       {
            alert("Special characters are not allowed.");
            return false;
       }
  }
  if(s_val.indexOf('.') > -1 && dot_count > 2)
  {
        var number_bet_dots = s_val.split('.');

        for(var l=0;l < number_bet_dots.length;l++)
        {
                if(number_bet_dots[l] < 0 || number_bet_dots[l] > 255)
                {
                        alert("Number between '.' should be between 0 and 255.")
;
                        return false;
                }
        }
 }
        return true;
}

