var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function valid(field, format) {
//alert(field.name + ' - ' + field.id);
if(!format.test(field.value)) {
field.style.border = "2px solid red";
if(field.type == "select-one")
field.style.color = "red";
return false;
}
else {
field.style.border = "1px solid #7f9db9";
field.style.padding = "2px";
if(field.type == "select-one")
field.style.color = "black";
return true;
}
}
function validateSearch(form) {
var errors = new Array();
var errStr;
var textFormat = /^[^<>#%&;\$\*\+\?\=\!\:\|\\\/\[\]\{\}\(\)\^]{0,30}$/;
var zipFormat = [/^(\d{5}){0,1}$/, /^(\d{5}-\d{4}){0,1}$/];
var bedFormat = /^\d{0,1}$/;
var costFormat = /^[0-9.]{0,10}$/;
var stateFormat = /^[a-zA-Z]{2}$/;
//alert('propertyIDView: ' + form.propertyIDView.value);//scr #786
if(form.propertyIDView == null ||form.propertyIDView.value == '') {
if(!valid(form.state, stateFormat))
errors.push("State is required if Property ID is not entered.");
}
//if(!valid(form.bedrooms, bedFormat))
// errors.push("The Bedrooms field must contain a single digit number");
if(!valid(form.maxCost, costFormat))
errors.push("The Cost field must contain a number.");
if(errors[0] != null){
errStr = "There is a problem with your input, please correct the fields in red before completing your search. \n\n";
errStr = errStr + "Details: \n";
for(var i in errors)
errStr = errStr + errors[i] + "\n";
alert(errStr);
return false;
}
return true;
}
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(field, 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){
field.style.border = "2px solid red";
return "The Date Available field is required and must be in the form MM/DD/YYYY."
}
if (strMonth.length<1 || month<1 || month>12){
field.style.border = "2px solid red";
return "Please enter a valid month. Required format is MM/DD/YYYY.";
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
field.style.border = "2px solid red";
return "Please enter a valid day. Required format is MM/DD/YYYY.";
}
if (strYear.length != 4 || year==0 || year
maxYear){
field.style.border = "2px solid red";
return "Please enter a valid 4 digit year between 1900 and 2100. Required format is MM/DD/YYYY.";
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
field.style.border = "2px solid red";
return "Please enter a valid date";
}
return true
}
function validateAdd(form) {
//alert(form.id);
var errors = new Array();
var errStr;
var textFormat = /^[^<>#%&;\$\*\+\?\=\!\:\|\\\/\[\]\{\}\(\)\^]{0,30}$/;
var addressFormat = /^[^<>%&;\$\*\+\?\=\!\:\|\\\[\]\{\}\(\)\^]{0,50}$/;
var requiredAddressFormat = /^[^<>%&;\$\*\+\?\=\!\:\|\\\[\]\{\}\(\)\^]{1,50}$/;
var reqTextFormat = /^[^<>#%&;\$\*\+\?\=\!\:\|\\\/\[\]\{\}\(\)\^]{1,30}$/;
var zipFormat = /^([0-9]{5}){1}$|\d{5}-\d{4}$/;
var bedFormat = /^[0-9]{0,2}$/;
var bathFormat = /^(\d+\.?\d*){0,5}$/;
var emptybedFormat = /[1-9]{1}/;
var numberFormat = /^[0-9.]{0,10}$/;
var dropdownFormat = /^(?!default)/;
var yearFormat = /^(\d{4})?$/;
var phoneFormat = /^(\d{10})?$|\d{3}-\d{3}-\d{4}$/;
var reqPhoneFormat = /^(\d{10})$|\d{3}-\d{3}-\d{4}$/;
var emailFormat = /^([\w\.\-\+]+@[\w\.\-]+\.[a-zA-z]{2,4})?$/;
var quantityFormat = /^(\d+)$/;
var flag = 1;
var emptyflag = 0;
var returnValue;
//required fields
if(!valid(form.propertyTypeCode, dropdownFormat))
errors.push("Property Type is a required field.");
if(!valid(form.address1, requiredAddressFormat))
errors.push("Address 1 is a required field and it cannot contain special characters.");
if(!valid(form.address2, addressFormat))
errors.push("Address 2 cannot contain special characters.");
if(!valid(form.agencyId, dropdownFormat))
errors.push("Agency is a required field.");
if(!valid(form.city, reqTextFormat))
errors.push("City is a required field and it cannot contain special characters.");
if(!valid(form.state, dropdownFormat))
errors.push("State is a required field.");
if(!valid(form.county, reqTextFormat))
errors.push("County is a required field and it cannot contain special characters.");
if(!valid(form.zip, zipFormat))
errors.push("The Zip Code field is required and must be in the form 12345 or 12345-1234.");
returnValue = isDate(form.dateAvailable, form.dateAvailable.value);
if (returnValue == true)
{
;
}
else
{
errors.push(returnValue);
}
if (!valid(form.numUnits, quantityFormat))
errors.push("The Quantity field is required");
if(!valid(form.managementAgentPhone, reqPhoneFormat))
errors.push("The management agent phone number field is required and must be of the format 123-456-7890.");
//bedrooms
// for (i in bedrooms) {
// if(!valid(bedrooms[i], bedFormat) && flag) {
// errors.push("The Bedrooms fields must contain a single digit number.");
// flag = 0;
// }
// if(!emptyflag && emptybedFormat.test(bedrooms[i].value))
// emptyflag = 1;
// }
// if(!emptyflag && flag){
// errors.push("At least one of the vacant bedroom fields must be entered.");
// for(j in bedrooms) bedrooms[j].style.border = "2px solid red";
// }
//reoId or projectNumber
flag = 1;
emptyflag = 0;
if(!valid(form.reoId, textFormat) && flag) {
errors.push("Special characters are not allowed in the REO ID field.");
flag = 0;
}
if(!emptyflag && reqTextFormat.test(form.reoId.value))
emptyflag = 1;
if(!valid(form.projectNumber, textFormat) && flag) {
errors.push("Special characters are not allowed in the REO ID field.");
flag = 0;
}
if(!emptyflag && reqTextFormat.test(form.projectNumber.value))
emptyflag = 1;
/* if(!emptyflag && flag){
errors.push("Either a REO ID or a Project Number is required.");
form.reoId.style.border = "2px solid red";
form.projectNumber.style.border = "2px solid red";
} */
//other fields
if(!valid(form.numberOfBaths, bathFormat))
errors.push("The Bathrooms field must contain a number");
if(!valid(form.cost, numberFormat))
errors.push("The Cost field must contain a number.");
if(!valid(form.squareFeet, numberFormat))
errors.push("The Square Feet field must contain a number.");
if(!valid(form.yearBuilt, numberFormat))
errors.push("The Year Built field must contain a four digit number.");
if(!valid(form.orgId, textFormat))
errors.push("Special characters are not allowed in the Org Id field.");
if(!valid(form.projectName, textFormat))
errors.push("Special characters are not allowed in the Project Name field.");
if(!valid(form.managementAgent, textFormat))
errors.push("Special characters are not allowed in the Management Agent field.");
if(!valid(form.managementAgentEmail, emailFormat))
errors.push("The email address field must of the format 'user@domain.ext'.");
if(errors[0] != null){
errStr = "There is a problem with your input, please correct the fields in red before submitting the form. \n\n";
errStr = errStr + "Details: \n";
for(var i in errors)
errStr = errStr + errors[i] + "\n";
alert(errStr);
return false;
}
form.state.disabled=false;
return true;
}