		function countChar(toBeSearched,toSearch)
		{
			var i,cnt;
			cnt = 0;

			for(i=0;i<toBeSearched.length;i++)
			{
				if(toBeSearched.charAt(i) == toSearch) cnt += 1
			}

			return cnt
		}

		function checkdate(a)
		{
				
				if(countChar(a,"/") != 2)
				{
					alert("Invalid Date")
				  return false
				}
				var arrDate  = a.split("/")
				var err=0
			
        d	= arrDate[0];// day
        b = arrDate[1];// month
        f = arrDate[2];// year
				
			
        if((isNaN(b))||(isNaN(f))||(isNaN(d)))
        {
        	err=1;
        }

        if (b<1 || b>12) err = 1;
        if (d<1 || d>31) err = 1;
        if (f<1900 || f>2099) err = 1;
        if (b==4 || b==6 || b==9 || b==11)
        {
            if (d==31) err=1;
        }
				
        if (b==2)
        {
            var g=parseInt(f/4)
            if (isNaN(g)) {
                err=1;
            }
            if (d>29) err=1;
            if (d==29 && ((f/4)!=parseInt(f/4))) err=1;
        }
				
        if (err==1) {
            alert('Please Enter Valid Date');
            return false;
        } else {
						return true
				}
		}
		function checkLength(str,len)
		{
			if(str.length > len)
			{
				return false
			}
				return true
		}
		
		function compareDates(objStartDate,objEndDate,strSt,strEd){
		
			startDate	= trim(objStartDate)
			endDate		= trim(objEndDate)

			if(typeof strSt == "undefined")
			{
				strSt = "Start date"
			}
	
			if(typeof strEd == "undefined")
			{
				strEd = "End date"
			}
			
			if(!isEmpty(startDate) && isEmpty(endDate)){
				return false
			}
		
			if(!isEmpty(endDate) && isEmpty(startDate)){
				return false
			}
			
			if(!isEmpty(startDate)){
				if(!checkdate(startDate)){
					return false
				}
			}
		
			if(!isEmpty(endDate)){
				if(!checkdate(endDate)){
					return false
				}
			}

			if(!isEmpty(endDate) && !isEmpty(startDate)){

				var arrSDate  = startDate.split("/")
				var arrEDate  = endDate.split("/")
				
				sDD	= arrSDate[0]
				sMM	= arrSDate[1]
				sYY	= arrSDate[2]
				
				eDD	= arrEDate[0]
				eMM	= arrEDate[1]
				eYY	= arrEDate[2]
				
				stDate = new Date(sYY,sMM-1,sDD)
				edDate = new Date(eYY,eMM-1,eDD)
								
				if(edDate < stDate){
					alert(strEd +" should be greater than the " + strSt +".")
					return false
				}
		
			}

			return true
		}

function zipcode(str)
{

	str = str.toLowerCase()

	if(str.length > 9)
	{
		alert("Zipcode should not be greater than 9 characters.")
		return false
	}

	var expFirst = new RegExp("^[0-9a-zA-Z]")
	if(!expFirst.test(str))
	{
		alert("First character should be an alphabet or number.")
		return false
	}

	var expMid = new RegExp("^[0-9a-zA-Z]([- 0-9a-zA-Z]*[0-9a-zA-Z])*$")
	if(!expMid.test(str))
	{
		alert("Incorrect zip code.")
		return false
	}

	var expLast = new RegExp("[0-9a-zA-Z]$")
	if(!expLast.test(str))
	{
		alert("Last character should be an alphabet or number.")
		return false
	}

	if(str.indexOf("--") != -1)
	{
		alert("Cannot use conseutive hypens in zip code.")
		return false
	}

	if(str.indexOf("  ") != -1)
	{
		alert("Cannot use consecutive spaces in zip code.")
		return false
	}

	if((str.indexOf(" ") != -1) && (str.indexOf("-") != -1))
	{
		alert("Use a single type of separator either hypen or space.")
		return false
	}

	return true

}

function isDisCode(str)
{
	var pattern = "^[a-zA-Z]([0-9a-zA-Z])*((=)?([+-]{0,1})([0-9])+)*$"
	var ex = new RegExp(pattern)

	if(ex.test(str))
	{
		return true
	}
	else
	{
		return false
	}
}

function isAlpha(str,allowspaces,allowquotes){

	var pattern = "^[\u00c0-\u01ffa-zA-Z](([' .]{0,1})([\u00c0-\u01ffa-zA-Z])*)*$"
	var ex = new RegExp(pattern)

	if(ex.test(str))
	{
		return true
	}
	else
	{
		return false
	}

	/*
	if(str == "") return false
	if(allowspaces == "" || typeof allowspaces == "undefined") allowspaces = 'y'
	if(allowquotes == "" || typeof allowquotes == "undefined") allowquotes = 'n'
	
	str = str.toLowerCase()

	if(allowspaces == 'y'){
		for(i =0; i < str.length; i++){
			if(str.charAt(i) != " "){
				if(allowquotes == 'y'){
					if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') && str.charAt(i) != "'"){
						return false
					}
				} else {
					if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')){
						return false
					}
				}
				
			}
		}
	}

	if(allowspaces == 'n'){
		for(i =0; i < str.length; i++){
			if(allowquotes == 'y'){
				if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') && str.charAt(i) != "'"){
					return false
				}
			} else {
				if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')){
					return false
				}
			}
		}
	}
	*/

	return true
}

function isAlphaNumeric(str,allowspaces,allowquotes)
{

	var pattern = "^[\u00c0-\u01ffa-zA-Z](([' .]{0,1})([\u00c0-\u01ffa-zA-Z0-9])*)*$"
	var ex = new RegExp(pattern)

	if(ex.test(str))
	{
		return true
	}
	else
	{
		return false
	}

	/*
	if(str == "") return false
	if(allowspaces == "" || typeof allowspaces == "undefined") allowspaces = 'y'
	if(allowquotes == "" || typeof allowquotes == "undefined") allowquotes = 'n'
	
	str = str.toLowerCase()

	if(allowspaces == 'y'){
		for(i =0; i < str.length; i++){
			if(str.charAt(i) != " "){
				if(allowquotes == 'y'){
					if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') && str.charAt(i) != "'"){
						return false
					}
				} else {
					if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')){
						return false
					}
				}
				
			}
		}
	}

	if(allowspaces == 'n'){
		for(i =0; i < str.length; i++){
			if(allowquotes == 'y'){
				if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') && str.charAt(i) != "'"){
					return false
				}
			} else {
				if(!(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')){
					return false
				}
			}
		}
	}
	*/

	return true
}
function alphaNum(str)
{
	var pattern = "^[\u00c0-\u01ffa-zA-Z0-9](([' .]{0,1})([\u00c0-\u01ffa-zA-Z0-9])+)*$"
	var ex = new RegExp(pattern)

	if(ex.test(str))
	{
		return true
	}
	else
	{
		return false
	}
}

function trim(str){
	strLength = str.length
	if(strLength == 0) return str
	while(str.charAt(0) == " "){
		str = str.substring(1,strLength)
		strLength = str.length
	}
	while(str.charAt(strLength-1) == " "){
		str = str.substring(0,strLength-1)
		strLength = str.length
	}
	return str
}

function trimArea(str){
	str = escape(str)
	strLength = str.length
	if(strLength == 0) return str
	while(str.substring(0,3) == "%20" || str.substring(0,6) == "%0D%0A"){
		if(str.substring(0,3) == "%20"){
			str = str.substring(3,strLength)
			strLength = str.length
		}
		if(str.substring(0,6) == "%0D%0A"){
			str = str.substring(6,strLength)
			strLength = str.length		
		}
	}
	while(str.substring(strLength-3,strLength) == "%20" || str.substring(strLength-6,strLength) == "%0D%0A"){
	
		if(str.substring(strLength-3,strLength) == "%20"){
			str = str.substring(0,strLength-3)
			strLength = str.length
		}
		if(str.substring(strLength-6,strLength) == "%0D%0A"){
			str = str.substring(0,strLength-6)
			strLength = str.length
		}		
	}

	return unescape(str)
}

function isObject(objProbable){
	if(typeof objProbable == "object"){
	  return true
	} else {
	  return false
	}
}
function isEmpty(str){
	str = trim(str)
	if(str.length == 0){
		return true
	} else {
		return false
	}
}
function isNumeric(num,allowFloats){

	if(num == "") return false
	num = num.toLowerCase();
	//if(num.indexOf("+") != -1 || num.indexOf("-") != -1) return false
	if(num.indexOf("+") > 0 || num.indexOf("-") > 0) return false

	if(allowFloats == "" || typeof allowFloats == "undefined") allowFloats = 0

	if(allowFloats == 1){
		if(num.indexOf('e') == -1){
			if(num != parseFloat(num)){
				return false
			} else {
				return true
			}
		} else {
			return false
		}
	}

	if(allowFloats == 0){
		if(num.indexOf(".") != -1) return false
		if(num.indexOf('e') == -1){
			if(num != parseInt(num,10)){
				return false
			} else {

				return true
			}
		} else {
			return false
		}
	}
	
}

function checkDecimals(val,len)
{
	if(val.indexOf(".") != -1)
	{
		if((val.substring(0,val.indexOf("."))).length > parseInt(len,10))
		return false
	} 
	else 
	{
		if(val.length > parseInt(len,10))
		return false
	}
	return true
}

function isEmail(str) {

	if(str.length < 6)
	{
	   return false
	}
	else
	{
		var p = "^([0-9a-zA-Z](([-.\\w]{0,1})([0-9a-zA-Z])+)*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
		var m = new RegExp(p);
		if(m.test(str))
		{
			if(str.indexOf("..") != -1)
			{
				return false
			}
		}
		else
		{
		  return false
		}
	}
	/*  if(str.search("^([0-9a-zA-Z](([-.\\w]{0,1})([0-9a-zA-Z])+)*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$") == -1){

                       return false;
			}*/
	return true;
}

/*
function isEmail (s)
{	
	//^([0-9a-zA-Z](([-.\w]{0,1})([0-9a-zA-Z])+)*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
   if (isEmpty(s)) 
      // if (isEmail.arguments.length == 1) return defaultEmptyOK;
    //   else return (isEmail.arguments[1] == false);
   
    // 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;
	}
		
}*/
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 checkAll(formname,checkboxname,mainCheck) {

    if (eval("document."+formname+"."+mainCheck).checked == true)
    {
				if(eval("document."+formname+"."+checkboxname).length == undefined)
				{
					eval("document."+formname+"."+checkboxname).checked = true
				} 
				else 
				{
					for(i=0;i<eval("document."+formname+"."+checkboxname).length;i++)
					{
							eval("document."+formname+"."+checkboxname+"["+i+"]").checked = true;
					}
				}
		}
    else
    {
			if(eval("document."+formname+"."+checkboxname).length==undefined){
				eval("document."+formname+"."+checkboxname).checked = false
			} 
			else 
			{
				for(i=0;i<eval("document."+formname+"."+checkboxname).length;i++)
				{
						eval("document."+formname+"."+checkboxname+"["+i+"]").checked = false;
				}
			}
    }
}

function checkRequired(clickedField,visibleField,requiredField)
{
	doc 		= document.forms[0]
	objVisible 	= eval("doc." + visibleField)
	objRequired = eval("doc." + requiredField)
	
	if(clickedField.toLowerCase() == "v")
	{
		if(!objVisible.checked)
		{
			if(objRequired.checked) objRequired.checked = false
		}
	}
	if(clickedField.toLowerCase() == "r")
	{
		if(objRequired.checked)
		{
			objVisible.checked = true
		}
	}
}

function formString(formname,checkboxname,start)
{
		len = checkboxname.length
		pos = checkboxname.indexOf("*")
		
		var chkbox = ""
		if(pos == 0)
		{
			chkbox = start + "" + checkboxname.substring(1,checkboxname.length)
		} 
		if(pos == (len-1))
		{
			chkbox =  checkboxname.substring(0,checkboxname.length-1) + "" + start
		}
		if(pos > 0 && pos < len)
		{
			chkbox =  checkboxname.substring(0,(pos)) + start + checkboxname.substring(pos+1,len)
		}
		if(pos == -1)
		{
			chkbox =  checkboxname
		}
		return "document."+formname+"."+chkbox
}

function checkAllDiff(formname,checkboxname,mainCheck) {


		var start = 0
		if(checkboxname.indexOf("*") == -1) 
		{
			checkAll(formname,checkboxname,mainCheck)
			return false
		}

    if (eval("document."+formname+"."+mainCheck).checked == true)
    {
			while(typeof eval(formString(formname,checkboxname,start)) == "object")
			{
				eval(formString(formname,checkboxname,start)).checked = true
				start += 1
			}
		}
    else
    {
			while(typeof eval(formString(formname,checkboxname,start)) == "object")
			{
				eval(formString(formname,checkboxname,start)).checked = false
				start += 1
			}	
    }
}
function checkURL(url)
{
		url = trim(url);

	  if(url.length < 8)
	  {
	      return false
	  }
	  if(url.indexOf("..") != -1)
	  {
	      return false
	  }
	  else if(url.indexOf("__") != -1)
	  {
	      return false
	  }
	  else if(url.charAt(url.length - 1) == "." || url.charAt(url.length - 1) == "-" || url.charAt(url.length - 1) == "_")
	  {
	      return false
	  }
	  else if(url.indexOf(".") == -1)
	  {
	      return false
	  }
	  else if(url.indexOf("--") != 1)
	  {
	      return false
	  }
	  else
	  {
		  var pattern  = "^(http(s?)\\:\\/\\/){0,1}[a-zA-Z0-9]([a-zA-Z0-9\\.\\-\\._]+){2,}([\\.\\-\\_]){0,1}([a-zA-Z0-9]+){2,}((\\/)([a-zA-Z0-9\\-\\._\\/])+){0,}$"
		  var rgExp  = new RegExp(pattern);	
		  if (!rgExp.test(url))
		  {
		      return false
		  }
	  }
	  return true;
}
function checkPhone(countryCode,areaCode,subscriberCode)
{
	if(!isEmpty(countryCode.value))
	{
		var pattern  = "^([\\d]{1,5})$"
		var rgExp  = new RegExp(pattern);	
		if (!rgExp.test(countryCode.value))
		{
			alert("Invalid Country Code\nPlease enter numeric values only")
			countryCode.focus()
			return false
		}
	}
	else
	{
		alert("Please enter the Country Code")
		countryCode.focus()
		return false
	}
	if(!isEmpty(areaCode.value))
	{
		var pattern  = "^([\\d]{1,5})$"
		var rgExp  = new RegExp(pattern);	
		if (!rgExp.test(areaCode.value))
		{
			alert("Invalid Area Code\nPlease enter numeric values only")
			areaCode.focus()
			return false
		}
	}
	else
	{
		alert("Please enter the Area Code")
		areaCode.focus()
		return false
	}
	if(!isEmpty(subscriberCode.value))
	{
		var pattern  = "^([\\d]+(-{0,1}[\\d]+)*)+$"
		var rgExp  = new RegExp(pattern);	
		if (!rgExp.test(subscriberCode.value))
		{
			alert("Invalid Subscriber Code\nPlease enter numeric values only")
			subscriberCode.focus()
			return false
		}
	}
	else
	{
		alert("Please enter the Subscriber Code")
		subscriberCode.focus()
		return false
	}
		return true
}
