﻿/*
FIELD CHECKS

1. IsEmail(c,r)    -- is a valid address
2. IsPhonenumber(c,r)    -- is a valid 10 digit phone number may include " -.()"
2. IsZip(c,r)    -- is a valid 5 or 9 digit ZIP code
4. IsPlaintext(c,r) -- allow only numbers and english letters
5. IsInteger(c,r)    -- is an integer
6. IsNotHtml(c,r)    -- does not include markup
7. IsSecure(c,r)    -- is a secure password
8. IsEqual(c,m,r)    -- matches value of specified field
9. IsSelected(c) -- a valid option is selected
10. IsRequired(c) -- a required field has contents

ARGUMENT KEY

c = control-reference
r = required (0|1)
m = matched control reference

*/

// WARNING MESSAGE
var warning = "There are one or more errors or omitted fields in the form";

// CHECK VALIDATION: REVIEW ERRORS BEFORE SUBMIT    


// FORMAT TESTS:

// FORMAT TESTS: IS EMAIL
function IsEmail(c) {
	if (!c || c.search(/^[\w-\.]+@[\w-\.]+\.\w+$/) > -1) {
		return true;
	} else {
		return false;
	}
}


// FORMAT TESTS: IS PHONENUMBER
function IsPhonenumber(c) {
	var s = stripNonNumericCharacters(c);
	return (String(s).length >= 10 && String(s).length < 15);
}


function stripNonNumericCharacters(pstrSource) 
{ 
	var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, '');
    return m_strOut; 
}

function stripNonDecimalCharacters(pstrSource) {
	var validChars = "0123456789";
	var pstrDest = "";
	var bFoundPeriod = false;
	for (var i = 0; i <= pstrSource.length; i++) {
		if (String(validChars).indexOf(pstrSource.substr(i, 1)) != -1) {
			if (pstrSource.substr(i, 1) == ".") {
				if (bFoundPeriod == false) {
					pstrDest += pstrSource.substr(i, 1);
					bFoundPeriod = true;
				}
			}
			else {
				pstrDest += pstrSource.substr(i, 1);
			}
		}
	}
	return pstrDest;
}

//allows negative numbers
function stripNonDecimalCharacters1(pstrSource) {
    var validChars = "-0123456789.";
    var pstrDest = "";
    var bFoundPeriod = false;
    for (var i = 0; i <= pstrSource.length; i++) {
        if (String(validChars).indexOf(pstrSource.substr(i, 1)) != -1) {
            if (pstrSource.substr(i, 1) == ".") {
                if (bFoundPeriod == false) {
                    pstrDest += pstrSource.substr(i, 1);
                    bFoundPeriod = true;
                }
            }
            else {
                pstrDest += pstrSource.substr(i, 1);
            }
        }
    }
    return pstrDest;
}

// FORMAT TESTS: IS ZIP
function IsZip(c) {
	if (!c || c.search(/^\d\d\d\d\d(-\d\d\d\d)?$/) > -1) {
		return true;
	} else {
		return false;
	}
}

// FORMAT TESTS: IS PLAINTEXT
function IsPlaintext(c) {
	if (c.search(/[^\w]/) > -1) {
		return false;
	} else {
		return true;
	}
}

// FORMAT TESTS: IS INTEGER
function IsInteger(c) {
	if (c.search(/[^\d]/) > -1) {
		return false;
	} else {
		return true;
	}
}

// FORMAT TESTS: IS NOT HTML
function IsNotHtml(c) {
	if (c.search(/<[^>]+>/) > -1) {
		return false;
	} else {
		return true;
	}
}

// FORMAT TESTS: IS SECURE
function IsSecure(c) {
	if (
		(
			(c.search(/[A-Z]/) > -1) ||
			(c.search(/[a-z]/) > -1)
		) &&
		(c.search(/[\d]/) > -1) &&
		(c.length > 3)
	) 
	{
		return true;
	} else {
		return false;
	}
}

// FORMAT TESTS: IS EQUAL
function IsEqual(c, m) {
	if (!c || c == m.value) {
		return true;
	} else {
		return false;
	}
}


// FORMAT TESTS: IS SELECTED
function IsSelected(c) {
	if (c.selectedIndex > 0) {
		return true;
	} else {
		return false;
	}
}

function formatCurrency(strValue) {
	strValue = strValue.toString().replace(/\$|\,/g, '');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue * 100 + 0.50000000001);
	intCents = dblValue % 100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue / 100).toString();
	if (intCents < 10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length - (1 + i)) / 3); i++)
		dblValue = dblValue.substring(0, dblValue.length - (4 * i + 3)) + ',' +
		dblValue.substring(dblValue.length - (4 * i + 3));
	return (((blnSign) ? '' : '-') + '$' + dblValue + '.' + strCents);
}

function isDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}

	isDateMonth = matchArray[1]; // p@rse date into variables
	isDateDay = matchArray[3];
	isDateYear = matchArray[5];

	if (isDateMonth < 1 || isDateMonth > 12) { // check isDateMonth range
	//	alert("isDateMonth must be between 1 and 12.");
		return false;
	}

	if (isDateDay < 1 || isDateDay > 31) {
		//alert("isDateDay must be between 1 and 31.");
		return false;
	}

	if ((isDateMonth == 4 || isDateMonth == 6 || isDateMonth == 9 || isDateMonth == 11) && isDateDay == 31) {
		//alert("isDateMonth " + isDateMonth + " doesn`t have 31 days!")
		return false;
	}

	if (isDateMonth == 2) { // check for february 29th
		var isleap = (isDateYear % 4 == 0 && (isDateYear % 100 != 0 || isDateYear % 400 == 0));
		if (isDateDay > 29 || (isDateDay == 29 && !isleap)) {
			//alert("February " + isDateYear + " doesn`t have " + isDateDay + " days!");
			return false;
		}
	}
	return true; // date is valid
}

function isTime(strTime) {
	var strTime1 = /^(\d{1,2})(\:)(\d{2})\2(\d{2})(\ )\w[AM|PM|am|pm]$/;
	var strTime2 = /^(\d{1,2})(\:)(\d{2})(\ )\w[A|P|a|p]\w[M|m]$/;
	var strTime3 = /^(\d{1,2})(\:)(\d{1,2})$/;

	var strFormat1 = strTime.match(strTime1);
	var strFormat2 = strTime.match(strTime2);
	var strFormat3 = strTime.match(strTime3);
	// Check to see if it matches one of the
	//     3 Format Strings.


	if (strFormat1 == null && strFormat2 == null && strFormat3 == null) {
		return false;
	}


	else if (strFormat1 != null) {
		// Validate for this format: 3:48:01 PM


		if (strFormat1[1] > 12 || strFormat1[1] < 00) {
			return false;
		}


		if (strFormat1[3] > 59 || strFormat1[3] < 00) {
			return false;
		}


		if (strFormat1[4] > 59 || strFormat1[4] < 00) {
			return false;
		}
	}


	else if (strFormat2 != null) {
		// Validate for this format: 3:48 PM


		if (strFormat2[1] > 12 || strFormat2[1] < 00) {
			return false;
		}


		if (strFormat2[3] > 59 || strFormat2[3] < 00) {
			return false;
		}
	}


	else if (strFormat3 != null) {
		// Validate for this format: 15:48


		if (strFormat3[1] > 23 || strFormat3[1] < 00) {
			return false;
		}


		if (strFormat3[3] > 59 || strFormat3[3] < 00) {
			return false;
		}
	}
	return true;
}

function OnClickEventCode(s) {
	return s.replace('function', '').replace('onclick(event)', '').replace("{", "").replace("}").replace("javascript:", "").replace("undefined");
}



function checkDecimals(val, evt) {
    var txtControl = val
    var llength = txtControl.length;
    var fieldvalues = new Array(llength);

//    var evt = e || window.event;
    //    var keyPressed = evt.which || evt.keyCode;
    //e.preventDefault();
   // alert(window.event);
//    if (window.event!= undefined)
//        var keyPressed = window.event.keyCode; // IE
//    else
//        var keyPressed = e.which; // Firefox
    var keyPressed = evt.which || window.event.keyCode;    
        

//    if (txtControl.length > 0) {
//        if (keyPressed == 0x2E ) {
//            if (txtControl.indexOf('.')!=-1) {
//                keyPressed = 0;
//                return false;
//             //   e.preventDefault();
//            }
//        }
//    }

    if (txtControl.length > 1) {
        for (i = 0; i < llength; i++) {
            fieldvalues[i] = txtControl.slice(i, i + 1);
        }
        if (keyPressed == 0x2E ) {
            for (i = 0; i < llength; i++) {
                if (fieldvalues[i] == '.') {
                    keyPressed = 0;
                    return false;
                  //  e.preventDefault();
                }
                else {

                }
            }
        }
    }

    if (txtControl.length > 1) {
        for (i = 0; i < llength; i++) {
            if (fieldvalues[i] == '.') {
                var values = new Array(2);
                values = txtControl.split(".");
                var checkfourdigits = values[1];
                if (checkfourdigits.length > 3) {
                    if (((keyPressed != 0x2E)) && ((keyPressed != 0x08)))
                    {
                    keyPressed = 0;
                    return false;
                    }
                  //  e.preventDefault();
                }
            }
        }
    }

    //key = window.event.keyCode;
    pass = (((keyPressed >= 0x30) && (keyPressed <= 0x39)) || ((keyPressed == 0x2E)) || ((keyPressed == 0x08)));
   // pass = (((keyPressed >= 0x30) && (keyPressed <= 0x39)) || ((keyPressed == 0x2E)));
    if (pass == false) {
        keyPressed = 0;
        return false;
       // e.preventDefault();
    }
    else {
        return true;
    }
}
