// JavaScript Document
// this file contains common validator check functions using javascript
// Check whether string s is empty.
function isEmpty(s)
    {   
        var reWhitespace = /^\s+$/;
        return ((s == null) || (s.length == 0) || reWhitespace.test(s));
    }

//Check whether string s is e-mail address
function isEmail (s)
    {   
        var reEmail = /^([a-z0-9]+)(([\+\._-]{0,1})([a-z0-9]+))*([@]{1,1})([a-z]+[a-z0-9]*)(([\._-]{0,1})([a-z0-9]+))*(([.]{1,1})([a-z]+))+$/;
        if (isEmpty(s))
            {
                if (isEmail.arguments.length == 1) return false;
                else return (isEmail.arguments[1] == true);
            }
        else 
            {
				return reEmail.test(s);
            }
    }

//Check whether string s is alphanumeric value
function isAlphanumeric (s)
    {   
		var reAlphanumeric = /^[a-zA-Z0-9]+$/;

        if (isEmpty(s)) 
            {
                if (isAlphanumeric.arguments.length == 1) return false;
                else return (isAlphanumeric.arguments[1] == true);
            }
        else 
            {
                return reAlphanumeric.test(s);
            }
    }

//Check whether string s is numeric value
function isNumeric(s)
    {   
		var reNumber = /^([0-9])+$/;
        if (isEmpty(s))
            {
                if (isNeumeric.arguments.length == 1) return false;
                else return (isNumeric.arguments[1] == true);
            }
        else 
            {
                return reNumber.test(s);
            }
    }

//Check whether a valid date
function isDate (year, month, day)
    {   
        if (isEmpty(year) && isEmpty(month) && isEmpty(day)) return false;
	
		var dt = new Date(parseFloat(year), parseFloat(month)-1, parseFloat(day), 0, 0, 0, 0);
   		if (parseFloat(day) != dt.getDate()) { return false; }
   		if (parseFloat(month)-1 != dt.getMonth()) { return false; }
		if (parseFloat(year) != dt.getFullYear()&&parseFloat(year) != dt.getYear()) { return false; }

   		return true;
	
    }

//Check whether string s url
function isURL (s)
    {   
        //var reURL = /^(((ht|f){1}(tp:[/][/]){1})|((www.){1}))[-a-zA-Z0-9@:%_\+.~#?&//=]+$/;
		var reURL = /^((http(s?):[/][/]){1})[-a-zA-Z0-9@\-:%_\+.~#?&//=]+$/;
        if (isEmpty(s))
            {
                if (isURL.arguments.length == 1) return false;
                else return (isURL.arguments[1] == true);
            }
        else 
            {
      	    	return reURL.test(s);
            }
    }


function isTime(value)
 	{
 		var reTime=/^(([0-1]*[0-9])|([2][0-3]))[:]([0-5]*[0-9])$/; //24 HOURS FORMAT
 		if(isEmpty(value))
 			{
 				if (isTime.arguments.length == 1) return false;
                 else return (isTime.arguments[1] == true);			
 			}
 		else
 			{
 				return reTime.test(value);			
 			}	
 	}	

//check how many checkbox checked
function countCheckbox(ob) 
	{
		var checks = document.getElementsByName(ob);
		var totalchecked = 0;

		for ( i=0; i < checks.length; i++) 
			{
				if(checks[i].checked == true)
					{
						totalchecked=totalchecked+1;
					}
			}
		return totalchecked;
    }

//check maximum selected options of a checkbox list
function checkCheckbox(ob,maxcount)
	{
		if(countCheckbox(ob)>maxcount)
			{
				var checks = document.getElementsByName(ob);
				var totalchecked = 0;
				for ( i=0; i < checks.length; i++ ) 
					{
						if(checks[i].checked == true)
							{
								totalchecked=totalchecked+1;
							}
						if(totalchecked>maxcount)
							{
								checks[i].checked = false;
							}
					}
				return false;
			}
		else
			return true;
	}

//count and display character of a text field
function countCharacter(input,output,maxchar)
	{
		var count=document.getElementById(input).value.length;
		if(count<maxchar)
			{
				document.getElementById(output).value=count;
				return true;
			}
		else if (count>=maxchar)
			{
				document.getElementById(input).value=document.getElementById(input).value.substring(0, maxchar);
				document.getElementById(output).value=maxchar;
				return true;
			}
		else
			{
				document.getElementById(output).value=count;
				return false;	
			}
	}

function checkAll(chk)
	{
		for (i = 0; i < chk.length; i++)
		chk[i].checked = true ;
	}
	
function uncheckAll(chk)
	{
		for (i = 0; i < chk.length; i++)
		chk[i].checked = false ;
	}
