

/*
 *	Global Functions
 */

// Returns true if string contains only whitespace characters
function isblank(s) {

	for(var i = 0; i < s.length; i++) {

		var c = s.charAt(i);

		if(c != '\n' && c != '\t') {

			return false;
		}
	}
	return true;
}

// Returns true if string contains only numeric characters
function isnumeric(s)
{
	return !isNaN(parseFloat(s));
}

// Returns reverse of a string
function reverse(s) {

	var rev = new String;

	var i = s.length;
	
	while(i--) {

		rev += s.charAt(i);
	}
	
	return rev;
}

function formatdate(obj, delKey, dir) {

	if(obj.value.length < 10) {

		if(delKey != 9) { //tab

			if(delKey != 8 && delKey != 46 && delKey != 16 && !(delKey > 36 && delKey < 41)) { //if the delete, backspace, shift, are not the keys that caused the keyup event.

				var fieldLen = obj.value.length;

				if((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <= 105)) {

					if(fieldLen == 2 || fieldLen == 5) {

						obj.value = obj.value + "/";
					}
				}
				else {

					if(dir == "up") {

						if(obj.value.length == 0) {

							obj.value = "";
						}
						else {

							obj.value = obj.value.substring(0, obj.value.length - 1);
						}
					}
				}
				obj.focus();
			}
		}
		else {

			if(dir == "down") {

				checkdate(obj.value);
			}
		}
	}
}

// Check the date syntax
function checkdate(s) {

	var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31);

	if(s.length != 10) {

		return false;
	}

	var mon = s.substring(0, 2);	// month
	var s1 = s.substring(2, 3);	// '/'
	var day = s.substring(3, 5);	// day
	var s2 = s.substring(5, 6);	// '/'
	var yr = s.substring(6, 10);	// year

	if(mon < 1 || mon > 12 || day < 1 || day > 31 || yr < 1900) {

		return false;
	}

	if(day > days[mon]){

		return false;
	}

	if(mon == 2) {

		var g = parseInt(yr/4);

		if(isNaN(g)) {

			return false;
		}

		if(day == 29 && (g != (yr/4))) {

			return false;
		}
	}

	return true;
}

/*
// Returns true if string matches the date pattern
function verifyre(regex, s) {

	return regex.test(s);
}

// Returns true if string matches the zip pattern
function verifyzip(s) {

	var exp = /\d[0-9]{5}(-[0-9]{4})/;

	return exp.test(s);
}

// Returns true if string matches the phone pattern
function verifyphone(s) {

	var shortUS = /\\(\\d{3}\\)\\d{3}-\\d{4}(x\\d{1,4}/;
	var longUS = /1-\\d{3}-\\d{3}-\\d{4}(x\\d{1,4}/;
	var intl = /\\+\\d{1,3}-\\d{1,4}-\\d{1,8}(x\\d{1,4}/;

	return exp.test(shortUS) || exp.test(longUS) || exp.test(intl);
}
*/

// Returns true if string matches the date pattern
function verifydate(s) {

	var exp = /\d{1,2}\/\d{1,2}\/\d{4}/;

	return exp.test(s) && checkdate(s);
}

// Returns true if string matches the email pattern
function verifyemail(s) {

	//var exclude = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	//var check = /@[\w\-]+\./;
	//var checkend = /\.[a-zA-Z]{2,3}$/;

	return s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1;
}

// Returns true if string matches the amount pattern
function verifyamt(s) {

	return s.search(/^\d*\.?\d{0,2}$/) != -1;
}

// Returns true if string matches the credit card number pattern
function verifycard(s) {

	var rev = reverse(s);
	
	var total = new Number(0);
	
	for(var i = 0; i < rev.length; i += 1) {

		var temp = 0;

		if(i % 2) {

			temp = rev.substr(i, 1) * 2;

			if(temp >= 10) {

				var split = new String(temp);
				temp = parseInt(split.substr(0, 1)) + parseInt(split.substr(1, 1));
			}
		}
		else {

			temp = rev.substr(i, 1);
		}	
		total += parseInt(temp); 
	}
	return total % 10;
}

/*
 *	Verify a field for emptiness
 */

function isempty(e) {

	return (e.value == null) || (e.value == "") || isblank(e.value);
}


// -----

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
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 isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.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){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
