
//
//  Validation JAVA Script Code
//
//  Aug. 2006, version 1.0
//
//

function DisableUpload(theForm, bCheckExists)
{
    if(bCheckExists == '1') {
        if(theForm.elements['photo1'].value == "") {
            alert("The first photos will be the main photo. Please enter one.");
            return false;
        }
    }
    theForm.elements['submit'].value = "Working...";
    theForm.elements['submit'].disabled=true;
    
    return true;
}

function UploadPlans(theForm, bCheckExists)
{
    if(bCheckExists == '1') {
        if(theForm.elements['photo1'].value == "") {
            alert("There are no floor plans to upload.");
            return false;
        }
    }
    theForm.elements['submit'].value = "Working...";
    theForm.elements['submit'].disabled=true;
    
    return true;
}

function PerformSubAction(theForm, sConfirm) {
    var sMessage;
    //alert(theForm.myAction.value);
    switch(theForm.myAction.value)
    {
        case "0":
            return false;
        case "1":
            // Edit record...
            theForm.action = "./edithome.php";            
            break;
        case "2":
            // Add Photos...
            theForm.action = "./addphotos.php";            
            break;
        case "3":
            // Add Floor Plans...
            theForm.action = "./addplans.php";            
            break;
        case "4":
            sMessage = "Confirm deletion of " + sConfirm + "?";    
            if(confirm(sMessage) != 0)
                theForm.action = "./delete.php";
            else
                return false;
            break;
        default:
            return false;    
    }
    return true;
}


function CheckFileExists(theForm, checkElement, ErrorElement) {
    var elem = document.getElementById(ErrorElement);

    if(theForm.elements[checkElement].value == "")
    {
       elem.firstChild.nodeValue = "At least one photo is required.";
       return false;
    }
    else
       elem.firstChild.nodeValue = "Standby, uploading photo(s)...";

}

function CheckElements(theForm, ErrorElement) {
    var why = "";
    var elem = document.getElementById(ErrorElement);
    
       
    if(theForm.community.value == "0")
    {
       elem.firstChild.nodeValue = "Please select a Community.";
       return(false);
    }
    if(isEmpty(theForm.movein.value) == "1")
    {
       elem.firstChild.nodeValue = "Please enter a Move In Date.";
       return false;
    }
    if(isEmpty(theForm.address.value) =="1")
    {
       elem.firstChild.nodeValue = "Please enter Property Address.";
       return false;
    }
    if(isEmpty(theForm.city.value) =="1")
    {
       elem.firstChild.nodeValue = "Please enter a City.";
       return false;
    }    
    if(theForm.state.value == "0")
    {
       elem.firstChild.nodeValue = "Please select a State.";
       return false;
    }
    if(theForm.county.value == "0")
    {
       elem.firstChild.nodeValue = "Please select a County.";
       return false;
    }
    if(isEmpty(theForm.zipcode.value) =="1")
    {
       elem.firstChild.nodeValue = "Please enter Zip Code.";
       return false;
    }
    if(isEmpty(theForm.price.value) =="1")
    {
       elem.firstChild.nodeValue = "You must enter a Price.";
       return false;
    }
    if(isEmpty(theForm.sqft.value) =="1")
    {
       elem.firstChild.nodeValue = "Please enter Total Square Feet.";
       return false;
    }
    if(theForm.bedrooms.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter Bedrooms.";
       return false;
    }
    if(theForm.baths.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter Baths.";
       return false;
    }
    if(theForm.stories.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter Stories.";
       return false;
    }
    if(theForm.garage.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter Garage(s).";
       return false;
    }
    if(theForm.livingareas.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter number of Living Areas.";
       return false;
    }
    if(theForm.diningareas.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter number of Dining Areas.";
       return false;
    }
    if(theForm.master.value == "0")
    {
       elem.firstChild.nodeValue = "Please choose Master Bedroom location.";
       return false;
    }
    
    if(theForm.lottype.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter Lot Type";
       return false;
    }
    if(isEmpty(theForm.lotsize.value) =="1")
    {
       elem.firstChild.nodeValue = "Please enter Lot Size.";
       return false;
    }
    if(theForm.status.value == "0")
    {
       elem.firstChild.nodeValue = "Please enter Property Status";
       return false;
    }
    if(isEmpty(theForm.contact.value) =="1")
    {
       elem.firstChild.nodeValue = "Please enter a Contact.";
       return false;
    }
    why = checkEmail(theForm.email.value);
    if(why != "")
    {
       elem.firstChild.nodeValue = why;
       return false;
    }
    why = checkPhone(theForm.phone.value);
    if(why != "")
    {
       elem.firstChild.nodeValue = why;
       return false;
    }
    //why += checkEmail(theForm.email.value);
    //why += checkPhone(theForm.phone.value);
    //why += checkPassword(theForm.password.value);
    //why += checkUsername(theForm.username.value);
    //why += isEmpty(theForm.notempty.value);
    //why += isDifferent(theForm.different.value);
    //for (i=0, n=theForm.radios.length; i<n; i++) {
    //    if (theForm.radios[i].checked) {
    //        var checkvalue = theForm.radios[i].value;
    //        break;
    //    } 
    //}
    //why += checkRadio(checkvalue);
    //why += checkDropdown(theForm.choose.selectedIndex);
    return true;
}

function CheckForm(theForm, ErrorElement) {
    if(CheckElements(theForm, ErrorElement) == false)
        return false;
}

function CheckWholeForm(theForm, ErrorElement) {
    if(CheckFileExists(theForm, "photo1", ErrorElement) == false)
    	return false;
    if(CheckElements(theForm, ErrorElement) == false)
        return false;
    return true;        	
}

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an E-mail address.";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid E-mail address.";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The E-Mail address contains illegal characters.";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Include an area code.\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng) {
var error = 0;
  if (strng.length == 0) {
     error = 1;
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list.\n";
    }    
return error;
}    

