function isempty(str)
//  Trst for empty string
{ 
	if (String(str).length <= 0)
 	{
		return true;
 	}
 	else
 	{
 	    return false;
 	}
}		

function testnumericfloat(str)
// Test for numeric string with decimal point
{
    if (str == parseFloat(str)) 
    {
        return true;
    }
    else
    {
        return false
    }
}    
    
function testinteger(str)
// Test for integer
{
    if (str == parseInt(str)) 
    {
        return true;
    }
    else
    {
        return false
    }
}    

function removeSpaces(str) 
{
    return str.split(' ').join('');
}

function IsNumericString(str)
{
   // check that string only contains numbers (used for checking phone number / fax fields)
   var strChar;
   var strString = removeSpaces(str);
   var strValidChars = "0123456789";   
   
   // test strString consists of valid characters listed above
   for (i = 0; i < strString.length; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
           return false;
         }
   }
   
   return true;
}
   
function IsAlphabeticString(str)   
{
   //check that string only contains letters (used for checking name fields)
   var strChar;
   var strString = removeSpaces(str);
   var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'-&";   

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
            return false;
         }
   }
   
   return true;
}   

function checkDate(dateStr){
    var errorMessage = ""
    var dateCh= "/";
    var minYear=1900;
    var maxYear=2100;
	var daysInMonth = DaysArray(12)
	var pos1=dateStr.indexOf(dateCh)
	var pos2=dateStr.indexOf(dateCh,pos1+1)
	var strDay=dateStr.substring(0,pos1)
	var strMonth=dateStr.substring(pos1+1,pos2)
	var strYear=dateStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
	    errorMessage = "The date format should be : dd/mm/yyyy"
		return errorMessage
	}
	if (strMonth.length<1 || month<1 || month>12){
	    errorMessage = "Please enter a valid month"
		return errorMessage
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		errorMessage = "Please enter a valid day"
		return errorMessage
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
	    errorMessage = "Please enter a valid 4 digit year between " + minYear + " and " + maxYear
		return errorMessage
	}
	if (dateStr.indexOf(dateCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dateStr, dateCh))==false){
	    errorMessage = "Please enter a valid date"
		return errorMessage
	}
    return errorMessage
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function checkTime(timeStr){   
    var errorMessage = "";
    
    if (!/\d{2}:\d{2}/.test(timeStr))
    {
        errorMessage = "The time format should be hh:mm";
        return errorMessage;
    }
   
   timeStr = timeStr.split(':');
   
   if (timeStr[0] > 23)
   {
        errorMessage = "Please enter a valid hour";
		return errorMessage;
   }
   
   if (timeStr[1] > 59)
   {
        errorMessage = "Please enter a valid minute";
		return errorMessage;
   }
   
   return errorMessage;
}

function CheckBoxListSelect(cbControl, state, excludeOther)
{    
    var chkBoxList = document.getElementById(cbControl);
    var chkBoxListItems = chkBoxList.getElementsByTagName("input");
    var chkBoxLabels = chkBoxList.getElementsByTagName("label");
    
    for(var i=0;i<chkBoxListItems.length;i++) 
    {
        if (excludeOther == false || (excludeOther == true && chkBoxLabels[i].innerText != "Other"))
        {
            chkBoxListItems[i].checked = state;               
        }        
    }
        
    return false; 
}
