// form validation functions
function validate_field()
{
    try {
        var iLen = validate_field.arguments.length;
        var oArgs = validate_field.arguments;
        if(iLen < 2)
        {
            alert("--Usage:  validate_field(FieldObject, TypeOfField)");
            return false;
        }

        var oFld, sType, oPwdVerify;
        oFld =  oArgs[0];
        sType = oArgs[1];
        if(iLen == 3) oPwdVerify = oArgs[2];

        if("object" != typeof(oFld)) return false;
        if("string" != typeof(sType)) return false;
        switch(sType.toLowerCase().trim().toString())
        {
            case 'combo':
                if(validate_combo(oFld)) return true;
                else return false;
                break;
            case 'checkbox':
                if(validate_checkbox(oFld)) return true;
                else return false;
                break;
            case 'radio':
                if(validate_radio(oFld)) return true;
                else return false;
                break;
            case 'date':
                if(validate_date(oFld.value)) return true;
                else return false;
                break;
            case 'time':
				if(validate_time(oFld.value)) return true;
				else return false;
				break;
            case 'text':
                if(validate_text(oFld.value)) return true;
                else return false;
                break;
            case 'integer':
                if(validate_integer(oFld.value)) return true;
                else return false;
                break;
            case 'decimal':
            case 'money':
                if(validate_decimal(oFld.value)) return true;
                else return false;
                break;
            case 'email':
                if(validate_email(oFld.value)) return true;
                else return false;
                break;
            case 'zip':
                if(validate_uszip(oFld.value)) return true;
                else return false;
                break;
            case 'password':
                if(validate_password(oFld.value, oPwdVerify.value)) return true;
                else return false;
                break;
            case 'phone':
                if(validate_phone(oFld.value)) return true;
                else return false;
                break;
            default:
                return false;
                break;
        }
    } catch(e) { alert(e.message); return false; }
}
function validate_combo(oCbo)
{
    if("object" != typeof(oCbo)) return false;
    var si = oCbo.selectedIndex;
    if(si <= 0) return false;
    else return true;
}
function validate_checkbox(oChk)
{
    if("object" != typeof(oChk)) return false;
    if(oChk.checked) return true;
    else return false;
}
function validate_radio(oRdo)
{
    if("object" != typeof(oRdo)) return false;
    var iLen = oRdo.length;
    for(var i = 0; i < iLen; i++)
    {
        if(oRdo[i].checked) return true;
    }
    return false;
}
function validate_date(sDt)
{
    if((sDt.trim() == "")||(! sDt.trim().isDate())) return false;
    else return true;
}
function validate_time(sTm)
{
	if((sTm.trim() == "")||(! sTm.trim().isTime())) return false;
	else return true;
}
function validate_text(sTxt)
{
    if(sTxt.trim() == "") return false;
    else return true;
}
function validate_integer(sInt)
{
    if((sInt.trim() == "")||(! sInt.trim().isInteger())) return false;
    else return true;
}
function validate_decimal(sDec)
{
    if((sDec.trim() == "")||(! sDec.trim().isDecimal())) return false;
    else return true;
}
function validate_email(sEml)
{
    if((sEml.trim() == "")||(! sEml.trim().isEmail())) return false;
    else return true;
}
function validate_password(sPwd,sPwdVerify)
{
    if(sPwd.trim() == "") return false;
    if(sPwd.trim().toString() != sPwdVerify.trim().toString()) return false;
    return true;
}
function validate_phone(sPhone)
{
    if((sPhone.trim() == "")||(! sPhone.trim().isPhone())) return false;
    else return true;
}
function validate_uszip(sZip)
{
    var zre = /^\d{5}$|^\d{5}-?\d{4}$/i;
    return (null != sZip.trim().match(zre));
}
// when assigned to the onkeypress event for a textarea it
// limits the amount of characters that a textarea can hold
function limit(oTextArea, iMaxChars)
{
	if("object" != typeof(oTextArea)) return false;
	if(isNaN(parseInt(iMaxChars))) return;
	// else
	if(oTextArea.value.trim().length >= parseInt(iMaxChars))
	{
		oTextArea.value = oTextArea.value.trim().substring(0,iMaxChars);
		alert("Only " + iMaxChars + " characters may be entered for this field!");
		if(document.all) { window.event.returnValue = false; window.event.cancelBubble = true; }
		return false;
	}
}
/* Useful string functions added to the string object */
// Add the trim function to the string object
String.prototype.trim = trim;
function trim()
{
    var re = /^\s*/ig;
    var re2 = /\s*$/ig;
    var tmp = new String(this);
    tmp = tmp.replace(re, "");
    return new String(tmp.replace(re2,""));
}
// rot13 string encryption function
String.prototype.enc = enc;
function enc()
{
	var text = new String(this);
	var rot13text_rotated = ""; /* the function will return this string */;

	for (i = 1 ; i < (text.length + 1); i++) {
	k = text.charCodeAt(i-1);
	if (k >= 97 && k <= 109) {
		k = k + 13;
	} else	if (k >= 110 && k <= 122) {
		k = k - 13;
	} else if (k >= 65 && k <= 77) {
		k = k + 13;
	} else if (k >= 78 && k <= 90) {
			k = k - 13;
		}
		rot13text_rotated = rot13text_rotated + String.fromCharCode(k);
	}
	return rot13text_rotated;
}
// Add the isTime function to the string object
String.prototype.isTime = isTime;
function isTime()
{
	var sTime = new String(this).trim();
	// tack on a leading zero so the regex doesnt fail if neccessary
	if(sTime.substring(0,2).indexOf(":") >= 0) sTime = "0" + sTime;
	var re = /(?:0[1-9]|1[0-2]):[0-5][0-9]\s{0,1}(?:AM|PM)/ig;
	return re.test(sTime);
}
// Add the isEmail function to the string object
String.prototype.isEmail = isEmail;
function isEmail()
{
    var emailAddress = new String(this);
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(emailAddress);
}
String.prototype.isPhone = isPhone;
String.prototype.isFax = isPhone;
function isPhone()
{
	var sPhone = new String(this);
	var fPhone = false;
	var re = /[^0-9]/ig;
	if(sPhone.match(re))
	{
		sPhone = sPhone.replace(re,"");
	}
	sPhone = sPhone.trim();
	if(sPhone.length < 10) return false;
	else return true;
}
String.prototype.isMoney = isUSDollars;
String.prototype.isDecimal = isUSDollars;
String.prototype.isInteger = isNumber;
function isUSDollars()
{
	var cash = new Number(this);
	if(isNaN(parseFloat(cash))) return false;
	else return true;
}
function isNumber()
{
	var i = new Number(this);
	if(isNaN(parseInt(i))) return false;
	else return true;
}

Error.prototype.report = reportErr;
function reportErr()
{
    if(! DEBUG) return;
    var err = this;
    var str = "";
    for(x in err)
    {
        if(x == "report") continue;
        // else
        str += x + " is " + err[x] + "\n";
    }
    alert(str);
}
function daysInFeb(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
}
String.prototype.isDate = isDate;
function isDate()
{
    var dtStr = new String(this);
    var re = /[^0-9]/ig;
    var minYear = 1970, maxYear = 3000;
    var dayArray = DaysArray(12);
	var mo = dtStr.substr(0,2).replace(re,"");
	var dy = dtStr.substr(3,2).replace(re,"");
	var yr = dtStr.substr(6,4).replace(re,"");
	if(mo.trim() == "" || dy.trim() == "" || yr.trim() == "") return false;
	if(mo < 1 || mo > 12) return false;
	if(mo == 2 && dy > daysInFeb(yr)) return false;
	if(dayArray[mo] < dy || dy < 0) return false;
	if(yr > maxYear || yr < minYear) return false;
	// else
	return true
}
var sDefaultPrompt = "";
function findInCombo(oCbo)
{
    if("object" != typeof oCbo) return false;
    var opts = oCbo.options;
    var len = opts.length;
    var sTxt = prompt("Enter text to search for:",sDefaultPrompt);
    if(sTxt == null) return false;
    if(sTxt.trim() == "") return false;
        sDefaultPrompt = sTxt;
    var re = new RegExp(sTxt.toLowerCase()+"\\S*\\s*");
    re.ignoreCase = true;
    re.global = true;
    var si = (oCbo.selectedIndex <= 0) ? 0 : (parseInt(oCbo.selectedIndex) + 1);
    for(var i = si; i < len; i++)
    {
        if(opts[i].text.toLowerCase().match(re))
        {
            oCbo.selectedIndex=i;
            return;
        }
    }
    for(var i = 0; i < si; i++)
    {
        if(opts[i].text.toLowerCase().match(re))
        {
            oCbo.selectedIndex=i;
            return;
        }
    }
    alert(sTxt + " could not be found!");
}

// debug functions
function showForm(frm)
{
	if("object" != typeof(frm)) return;
	var str = "";
	var len = frm.length;
	for(var x = 0; x < len; x++)
	{
		//str += frm[x].name + "  " + frm[x].maxLength + "<br />\n";
		str += frm[x].name + "<br />\n";
	}
	var winx = window.open();
	winx.document.writeln(str);
	winx.focus();
}
