var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validPhoneChars = digits + phoneNumberDelimiters;
var digitsInPhoneNumber = 10;
var digitsInExtension = 4;
var decimalPointDelimiter = ".";

function valstr(str) {  // validate string, other name - trim ;)
	str = str.replace(/^[ ]+/, "");
	str = str.replace(/[ ]+$/, "");
	return str.toLowerCase();
}

function validationAlert(msg, badInput) {
	alert(msg);
	if(badInput.style.display == '') {
		badInput.focus();
		badInput.select();
	}
}

function isNumber(num) {
	if(num.search("^-?[0-9.]+$") != -1)
		return true
	return false
}

function checkRadio(radio) {
	var i;

	if(radio.length) {
		for (i = 0 ; i < radio.length ; i++) {
			if (radio[i].checked) {
				return radio[i].value;
			}
		}
	}
	else {
		return radio.value
	}
}

function re(str, regexp) {
  if (str.match(regexp)) {
  	return true;
  } else {
  	return false;
	}
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    //if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, start looking at character position 1 
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function isPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInPhoneNumber)
}

function isFloat(s)
{   
    var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);
    
    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function validatePhone(phoneNum) {
	var phone = '';
	var ext = '';
	var digitsOnly = '';
		
	digitsOnly = stripCharsNotInBag(phoneNum.value, digits);		
	if (digitsOnly.length > 0) {
		if (digitsOnly.length < digitsInPhoneNumber) {
			alert("This field must be at least a 10 digit number (i.e (123)456-7890 x123).  Extension is optional.");
			phoneNum.focus();
			return false;
		}
		else {
			phone = reformat(digitsOnly.substring(0, digitsInPhoneNumber), '(', 3, ') ', 3, '-', 4);
			ext = digitsOnly.substring(digitsInPhoneNumber, digitsInPhoneNumber+digitsInExtension);
			//if (ext.length == 0) 
				phoneNum.value = phone;
			//else
			//	phoneNum.value = phone + " x" + ext;
			//return true;	
		}
	}	
	else {
		if (phoneNum.value.length > 0) {
			alert('This field must be at least a 10 digit number (i.e (123)456-7890 x123).  Extension is optional.');
			phoneNum.focus();
			return false;	
		}	
	}		
}

function isPhone(s) {
	var phone = '';
	var ext = '';
	var digitsOnly = '';
	
	if (isEmpty(s) != true) {
		if (isEmpty(s)) 
		   if (isPhone.arguments.length == 1) return defaultEmptyOK;
		   else return (isPhone.arguments[1] == true);

			digitsOnly = stripCharsNotInBag(s.value, digits);		
	
		if (digitsOnly.length > 0) {
			if (digitsOnly.length < digitsInPhoneNumber) {
				return false;
			}
			else {
				phone = reformat(digitsOnly.substring(0, digitsInPhoneNumber), '(', 3, ') ', 3, '-', 4);
				ext = digitsOnly.substring(digitsInPhoneNumber, digitsInPhoneNumber+digitsInExtension);
				s.value = phone;
				return true;	
			}
		}
	}
	return true;	
}

function isURL (s) {
   if (isEmpty(s)) 
       if (isURL.arguments.length == 1) return defaultEmptyOK;
       else return (isURL.arguments[1] == true);
    
    // the url must begin with www.
    var i = 1;
    var sLength = s.length;

    // look for www.
    if ((s.substring(0, 4)) == "www.")
		i = 5;
	else
		return false;

    // look for second .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the second .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// Removes all characters which do NOT appear in string bag from string s.
function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

// Removes all characters which appear in string bag from string s.
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 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 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 minYear=1900;
    var maxYear=2200;
    var dtCh= "/";
	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);
		alert("Please enter a valid 4 digit year");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}

    return true;
}