// isWhitespace (s)                    Check whether string s is empty or whitespace.
// isEmpty(s)                          Check whether string s is empty. 
// isDigit (c)                         Check whether character c is a digit. 
// isInteger (s [,eok])                True if all characters in string s are numbers.
// isEmail (s [,eok])                  True if string s is a valid email address.
// isDate (year, month, day)           True if string arguments form a valid date.
// isYear (s [,eok])                   True if string s is a valid Year number.
// isMonth (s [,eok])                  True if string s is a valid month between 1 and 12.
// isDay (s [,eok])                    True if string s is a valid day between 1 and 31.
// daysInFebruary (year)               Returns number of days in February of that year.
// isUrl (s)                           True if string s is a valid URL.
// hasMinLength(v,min)                 TRUE if string is grater than min length 

var whitespace = " \t\n\r";
var defaultEmptyOK = false


//#########################################################################
//############################ Empty String ###############################
//#########################################################################
// Check whether string s is empty.

function isEmpty(s)
{   
    return ((s == null) || (s.length == 0))
}

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 checkEmpty(field,msg)
{
	if(isWhitespace(field.value) || isEmpty(field.value) )
     {
                alert(msg);
                field.focus();
                return true;
     }
}
//#########################################################################
//################################ Email ##################################
//#########################################################################
// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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 sLength = s.length;

        // look for @
        while ((i < sLength) && (s.charAt(i) != "@"))
        { i++
        }

        if ((i >= sLength) || (s.charAt(i) != "@")) return false;
        else i += 2;

        // look for .
        while ((i < sLength) && (s.charAt(i) != "."))
        { i++
        }

        // there must be at least one character after the .
        if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
        else return true;
}

function checkMail(field,msg)
{
	if(!isEmail(field.value))
     {
                alert(msg);
                field.focus();
                return true;
     }
}
//#########################################################################
//############################## Integer ##################################
//#########################################################################

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   
    return ((c >= "0") && (c <= "9"))
}
// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function checkInteger(field,msg)
{
	if(!isInteger(field.value))
     {
                alert(msg);
				field.value="";
                field.focus();
                return true;
     }
}
//#########################################################################
//############################## Drop Down ################################
//#########################################################################
// Check whether drop down box s is not selected.
function isSelected(s)
{   
    return ((s == 0) || (s == "") || (s == "-1"))
}

// Returns true if string s is empty or 
// whitespace characters only.


function checkDropdown(field,msg)
{
	if(isSelected(field.value))
     {
                alert(msg);
                field.focus();
                return true;
     }
}
//#########################################################################
//############################## Upload Field #############################
//#########################################################################

function filterFileType(field, ext) 
{
if (field.value.indexOf('.' + ext) == -1) {
return false;
}
return true;
} // end function filterFileType

function checkUpload(field,msg,ext)
{
	if (!filterFileType(field,ext))
     {
                alert(msg);
                field.focus();
                return true;
     }
}
//#########################################################################
//############################## URL ######################################
//#########################################################################
function isUrl(s)
{
    var temp=new Array()
    if (s!="")
    {
        var pos1=s.length;
        var value=s;
        pos1=pos1-4
        value=value.substring(pos1,pos1+1)
        
        var pos2=StringTrim(s).indexOf(".",0);
        if (pos2 == -1 || pos2<3 ) 
        {
            return false;
        }
        
        var arrEndUrl=new Array()
        arrEndUrl=[".com",".net",".org",".edu",".uni",".in",".uk",".ac",".de"]
        for(i=0;i<arrEndUrl.length;i++)
        {
            pos2=(StringTrim(s)).indexOf(arrEndUrl[i],0);
            if(pos2!=-1)
                break;
            
        }
        if (pos2 == -1) 
        {
            return false;
        }
        
        var arrEndUrl=new Array()
        arrEndUrl=["http://","www.","ftp://","https://"]
        for(i=0;i<arrEndUrl.length;i++)
        {
            pos2=StringTrim(s).indexOf(arrEndUrl[i],0);
            if(pos2!=-1)
                break;
        }
        
        
        if (pos2 == -1) 
        {
            return false;
        }
        return true
    }   
}

function StringTrim(objvalue)
{
    var TestString = objvalue;
    var SpaceChar  = " ";
    while (TestString.charAt(0) == SpaceChar) {TestString = TestString.substr(1)};
    while (TestString.charAt(TestString.length-1) == SpaceChar) {TestString = TestString.substr(0,TestString.length-1)};
    return TestString.toString();
}


function checkUrl(field,msg)
{
	if(!isUrl(field.value))
     {
                alert(msg);
                field.focus();
                return true;
     }
}

//#########################################################################
//############################ Checkbox Array #############################
//#########################################################################

 //Function To validate checkbox array
function checkBoxArray(Fldname,Msg)
{
var len=Fldname.length;

var chk=-1;
for (var i = 0;i<len;i++)
{	
	if(Fldname[i].checked==true)
	{
		chk=i;
	}
//alert(document.Frm["normalCheckBoxes[]"][i].checked);
}
		if(chk==-1)
		{
			alert(Msg);
			return false;
		}	
		return true;
}

//########################function to check all checkbox / uncheck to all checkbox ################
//######################### 1. pass arguments as (formname, fieldname, true) --- check all checkbox
//######################### 2.  pass arguments as (formname, fieldname,false) --- uncheck all checkbox
function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{

if(!document.forms[FormName])
return;
var objCheckBoxes = document.forms[FormName].elements[FieldName];
if(!objCheckBoxes)
return;
var countCheckBoxes = objCheckBoxes.length;
if(!countCheckBoxes)
objCheckBoxes.checked = CheckValue;
else
// set the check value for all check boxes
for(var i = 0; i < countCheckBoxes; i++)
objCheckBoxes[i].checked = CheckValue;
}
//#########################################################################
//########################Select Al least one Option#######################
//#########################################################################


function checkOptButton(Fldname,Msg)
{
	// place any other field validations that you require here
	// validate myradiobuttons
	myOption = -1;
	for (i=0; i<Fldname.length; i++) 
	{
		if (Fldname[i].checked)
		{
			myOption = i;
		}
	}
	if (myOption == -1)
	{
		alert(Msg);
		return true;
	}

	/*
	alert("You selected button number " + myOption
	+ " which has a value of "
	+ Fldname[myOption].value);
	*/

}

//#########################################################################
//############################ Alphanumeric ###############################
//#########################################################################

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s))
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}
function isLetter (c)
{   
    return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c == "_") )
}



// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   
    return ((c >= "0") && (c <= "9"))
}

//#########################################################################
//######################## Non negative Integer ###########################
//#########################################################################

// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}
// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true

function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

//#########################################################################
//########################## Telephone Number #############################
//#########################################################################

function checktelephonenumber(s)
{
    var numVal=s;
    var len=numVal.length;
    if (len > 0) 
    {
        for ( var i=0 ; i<len ; i++)
        {
            var strVal=numVal.charAt(i);
            if ( strVal != 1 && strVal != 2 &&  strVal != 3&& strVal != 4&& strVal != 5&& strVal != 6&& strVal != 7&& strVal != 8&& strVal != 9&& strVal != 0 && strVal!='(' && strVal!=')' && strVal!='-' ) 
            {
                return false;
            }
        }
    }
    return true;
}

//#########################################################################
//############################### Fax Number ##############################
//#########################################################################

function checkfaxnumber(s)
{
    var numVal=s;
    var len=numVal.length;
    if (len > 0) 
    {
        for ( var i=0 ; i<len ; i++)
        {
            var strVal=numVal.charAt(i);
            if ( strVal != 1 && strVal != 2 &&  strVal != 3&& strVal != 4&& strVal != 5&& strVal != 6&& strVal != 7&& strVal != 8&& strVal != 9&& strVal != 0 && strVal!='(' && strVal!=')' && strVal!='-' ) 
            {
                return false;
            }
        }
    }
    return true;
}

function validate()
{
	if(checkEmpty(this.document.myform.f_name,"First Name cann't be empty"))
	{
		return false;
	}
}