
// whitespace characters
var whitespace = " \t\n\r";

var dtCh	= "/";
var minYear	= 1900;
var maxYear	= 2100;
		
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 isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or whitespace characters only.
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;
}

//  Check for valid numeric strings
function isNumeric(strString)
{
   	var strValidChars = "0123456789.-";
   	var strChar;
   	var blnResult = true;    
   	
   	//  test strString consists of valid characters listed above
   	for (i = 0; i < strString.length && blnResult == true; i++)
   	{
   		strChar = strString.charAt(i);
   		if (strValidChars.indexOf(strChar) == -1) blnResult = false;
   	}
   	
   	return blnResult;
}

function mask(str,textbox,loc,delim)
{
	var locs = loc.split(',');

	for (var i = 0; i <= locs.length; i++)
	{
		for (var k = 0; k <= str.length; k++)
		{
	 		if (k == locs[i])
	 		{
	  			if (str.substring(k, k+1) != delim)
	  			{
	   				if (event.keyCode != 8)
	   				{ 
	   					//backspace
	    					str = str.substring(0,k) + delim + str.substring(k,str.length);
       					}
	  			}
	 		}
		}
 	}
	
	textbox.value = str
}

function allowNumbers(e, decimal) 
{
	var key;
	var keychar;

	if (window.event) 
	{
   		key = window.event.keyCode;
	}
	else if (e) 
	{
   		key = e.which;
	}
	else 
	{
   		return true;
	}
	
	keychar = String.fromCharCode(key);

	if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) 
	{
   		return true;
	}
	else if ((("0123456789").indexOf(keychar) > -1)) 
	{
   		return true;
	}	
	else if (decimal && (keychar == ".")) 
	{ 
  		return true;
	}
	else
   		return false;
}

function checkEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
		return true;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return true;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		return true;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		return true;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		return true;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		return true;
	}

	if (str.indexOf(" ")!=-1)
	{
		return true;
	}

	return false;					
}

function getToday()
{
	var today = new Date();
	var month = today.getMonth()+1;
	var year = today.getYear();
	var day = today.getDate();
	if(day<10) day = "0" + day;
	if(month<10) month= "0" + month ;
	if(year<1000) year+=1900;

	var today = month + "/" + day + "/" + year;

	return today;
}

function getDateObject(dateString,dateSeperator)
{
	//This function return a date object after accepting 
	//a date string ans dateseparator as arguments
	var curValue=dateString;
	var sepChar=dateSeperator;
	var curPos=0;
	var cDate,cMonth,cYear;

	//extract day portion
	curPos=dateString.indexOf(sepChar);
	cDate=dateString.substring(0,curPos);
	
	//extract month portion				
	endPos=dateString.indexOf(sepChar,curPos+1);			
	cMonth=dateString.substring(curPos+1,endPos);

	//extract year portion				
	curPos=endPos;
	endPos=curPos+5;			
	cYear=curValue.substring(curPos+1,endPos);
	
	//Create Date Object
	dtObject=new Date(cYear,cMonth,cDate);	
	return dtObject;
}

function zeroPad(num,count)
{
	var numZeropad = num + '';

	while(numZeropad.length < count)
	{
		numZeropad = "0" + numZeropad;
	}

	return numZeropad;
}

function isDate(dtStr)
{
	jsDate = dtStr.split("/");

	year = jsDate[2];
	month = jsDate[0];
	day = jsDate[1];
	
    	var test = new Date(year, month-1, day);

    	if ((test.getFullYear() == year) && (month == test.getMonth()+1) && (day == test.getDate()))
        	return "";
    	else
        	return " is invalid";
}

function compareDate(fromDate, toDate)
{
	if((isDate(fromDate)=="") && (isDate(toDate)==""))
	{
		jsDate = fromDate.split("/");
		
		var dt1 = new Date(jsDate[2], jsDate[0], jsDate[1]);
		
		jsDate = toDate.split("/");
		
		var dt2 = new Date(jsDate[2], jsDate[0], jsDate[1]);

		var date1 = dt1.getFullYear() + zeroPad(dt1.getMonth(),2) + zeroPad(dt1.getDate(),2);
		var date2 = dt2.getFullYear() + zeroPad(dt2.getMonth(),2) + zeroPad(dt2.getDate(),2);
		
		if(dt1 > dt2)
			return true;
		else
			return false;
	}
}

function compareTime(from_h, from_m, to_h, to_m)
{	
	var from = from_h + "" + from_m;
	var to = to_h + "" +  to_m;
	
	if (parseInt(to) > parseInt(from)) 
		return true;
	else
		return false;
}