<!--
/*

  -------------------------------------------------------------------------------
 |  Copyright (C) 2005 Azalea Technology, LLC. All rights reserved.              |
 |-------------------------------------------------------------------------------|
 |  Unauthorized removal of this notice is considered a violation of the         |
 |  license agreement under which this Code may be used. This work is protected  |
 |  under United States copyright law and the similar law(s) of other countries  |
 |  under which such as work is afforded legal protection, and upon conviction   |
 |  of such a violation in a court of applicable jurisdiction, such person(s)    |
 |  may be subject to the maximum allowable penalty as permitted under such law. |
 |-------------------------------------------------------------------------------|
 |  You acknowledge and agree that information presented to you through this     |
 |  site (the "Web Site") is protected by all applicable copyrights, trademarks, |
 |  service marks, patents or other proprietary rights and laws, and by virtue   |
 |  of accessing the Web Site, except as expressly authorized by the Azalea      |
 |  Technology, LLC., you agree not to modify, rent, lease, loan, sell,          |
 |  distribute, store, or create derivative works based on the Web Site, in      |
 |  whole or in part.                                                            |
  ------------------------------------------------------------------------------- 
 |  Decrypting or otherwise decoding the following programming language code is  |
 |  strictly prohibited except as expressly authorized by Azalea Technology,     |
 |  LLC. Upon conviction of such a violation in a court of applicable            |
 |  jurisdiction, such person(s) may be subject to the maximum allowable penalty |
 |  as permitted under such law.                                                 |
  -------------------------------------------------------------------------------
          Module: form.js
         Version: 1.0
		 Purpose: Form independent form validate using getAttribute

      Programmer: Benjamin Roberts
                  Azalea Technology, LLC.
                  P.O. Box 131150
                  Tyler, TX 75713-1150
                  broberts@azaleatech.com

         License: HGR General Contractors, L.P.
                  Single Domain (www.hgrgc.com); 10/24/2005
  -------------------------------------------------------------------------------
*/


// MAIN PROGRAM - VALIDATES FORM
function validate(form_obj){
    if(!form_obj){
        alert("FORM ERROR: The form supplied does not exist. Please check the form name.");
        return false;
    }
    
    if(!form_obj.elements){
        alert("FORM ERROR: The elements array of the form supplied could not be referenced. Please check the form name.");
        return false;
    }
    
    var x = form_obj.elements;
    var message = "";
    var errors = 0;
    var valid;
    
    var displayname, validate, expectedformat, required, visualcue, setcase, errormsg;
    var dependentcondition, dependenterror;
    
    var heading;
        heading  = "Form Validation Error Message\n\n";
        heading += "Your information cannot be sent because of the following errors. \n";
        heading += "Please correct these errors and try again. \n";
        heading += "_________________________________________________________________________       \n\n";
    
    var button_text = form_obj.Submit.value;
    if(isSpace(button_text)) button_text = "Submit";

	toggleFormButtonLock(form_obj,true,"Processing...");

	// Set the status of client-side javascript to "enabled" so that server-side formmail program will accept submission
	// Using this technique eliminates 99.9% of all Web form spam by forcing client-side javascript validation to take place
	if(form_obj._config_javascript_status){
		form_obj._config_javascript_status.value = "enabled";
	}

    for (var i=0;i<x.length;i++){
        validate = x[i].getAttribute("validate");
                       
        if(validate){
        
            displayname = x[i].getAttribute("displayname");
            expectedformat = x[i].getAttribute("expectedformat");
            visualcue = x[i].getAttribute("visualcue");
            required = x[i].getAttribute("required");
            setcase = x[i].getAttribute("setcase");
            errormsg = x[i].getAttribute("errormsg");
            dependentcondition = x[i].getAttribute("dependentcondition");
            dependenterror = x[i].getAttribute("dependenterror");
            
            if(visualcue){
                if(visualcue=="true") visualcue = true;
                else visualcue = false;
            }
            
            if(required){
                if(required=="true") required = true;
                else required = false;
            }
            
            switch(x[i].type){
                case "text":
                case "textarea":
                case "file":
                case "password":
                case "hidden":
                    
                    try{
                        expectedformat = expectedformat.toLowerCase();
                    }
                    catch(e){
                        expectedformat = "text";
                    }
                   
                    switch(expectedformat){
                        
                        case "text":                   
                            if(required) valid = !isSpace(x[i].value);
                            else valid = true;
                            
                            if(!valid){
                                errors++;
                                if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                                else{
                                    if(!displayname) displayname = x[i].name;
                                    message += "\n(" + (errors) + ") Missing response for " + displayname;
                                }
                            }
                            break;

                        case "email":
                            if(required) valid = validEmail(x[i].value);
                            else valid = isSpace(x[i].value) ? true : validEmail(x[i].value);
                            
                            if(!valid){
                                errors++;
                                if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                                else{
                                    if(!displayname) displayname = x[i].name;
                                    message += "\n(" + (errors) + ") Invalid or missing response for " + displayname;
                                }
                            }
                            break;

                        case "phone":
                            if(required) valid = validTelephoneNumber(x[i].value);
                            else valid = isSpace(x[i].value) ? true : validTelephoneNumber(x[i].value);
                            
                            if(!valid){
                                errors++;
                                if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                                else{
                                    if(!displayname) displayname = x[i].name;
                                    message += "\n(" + (errors) + ") Invalid or missing response for " + displayname;
                                }
                            }
                            break;

                        case "zip":
                            if(required) valid = isValidPostalCode("United States",x[i].value);
                            else valid = isSpace(x[i].value) ? true : isValidPostalCode("United States",x[i].value);
                            
                            if(!valid){
                                errors++;
                                if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                                else{
                                    if(!displayname) displayname = x[i].name;
                                    message += "\n(" + (errors) + ") Invalid or missing response for " + displayname;
                                }
                            }
                            break;

                        case "date":
                            if(required) valid = validDate(x[i].value);
                            else valid = isSpace(x[i].value) ? true : validDate(x[i].value);
                            
                            if(!valid){
                                errors++;
                                if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                                else{
                                    if(!displayname) displayname = x[i].name;
                                    message += "\n(" + (errors) + ") Invalid or missing response for " + displayname;
                                }
                            }
                            break;

                        default:
                            if(required) valid = !isSpace(x[i].value);
                            else valid = true;
                            
                            if(!valid){
                                errors++;
                                if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                                else{
                                    if(!displayname) displayname = x[i].name;
                                    message += "\n(" + (errors) + ") Missing response for " + displayname;
                                }
                            }
                            break;
                    }
                    
                    // Correct case for element value if requested
                    if(setcase&&x[i].value&&!isSpace(x[i].value)){
                        
                        try{
                            setcase = setcase.toLowerCase();
                        }
                        catch(e){
                            setcase = null;
                        }
                    
                        switch(setcase){
                            case "lower": x[i].value = x[i].value.toLowerCase();
                                break;
                            case "upper": x[i].value = x[i].value.toUpperCase();
                                break;
                            case "proper": x[i].value = toProperCase(x[i].value);
                                break;
                        }
                    }
                    
                    // Trim additional space if need from element value
                    if(x[i].value&&!isSpace(x[i].value)) x[i].value = trim(x[i].value);
                    
                    // Visually cue element as necessary and requested
                    if(visualcue){
                        try{
                            if(!valid) set_class_name(x[i].id,"textbox1_error");
                            else set_class_name(x[i].id,"textbox1");
                        }
                        catch(e){
                            alert("FORM ERROR: field \"" + x[i].name + "\" does not have a corresponding id tag. Please check the form.");
                        }
                    }
                    
                    // Assign focus and select text in form element as needed
                    try{
                        if(!valid){
                            x[i].focus();
                            x[i].select();
                        }
                    }
                    catch(e){
                        // do nothing
                    }
                    
                    break;

                case "checkbox":
                    var checkbox_obj;
                    var checkbox_name = x[i].name;                    
                    
                    // Determine whether this checkbox is part of a group or a single checkbox
                    // If part of a group, identify the group object (array); otherwise select the single object
                    if(eval("form_obj."+checkbox_name)) checkbox_obj = eval("form_obj."+checkbox_name);
                    else checkbox_obj = x[i];
                
                    valid = option_checked(checkbox_obj);
                
                    if(!valid){
                        errors++;
                        if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                        else{
                            if(!displayname) displayname = checkbox_obj.name;
                            message += "\n(" + (errors) + ") You must select at least one option from " + displayname;
                        }
                	}
                    break;

                case "radio":
                    var radio_obj;
                    var radio_name = x[i].name;
                    
                    // Determine whether this radio button is part of a group or a single radio button
                    // If part of a group, identify the group object (array); otherwise select the single object
                    if(eval("form_obj."+radio_name)) radio_obj = eval("form_obj."+radio_name);
                    else radio_obj = x[i];
                    
                    valid = option_checked(radio_obj);
                
                    if(!valid){
                        errors++;
                        if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                        else{
                            if(!displayname) displayname = radio_obj.name;
                            message += "\n(" + (errors) + ") You must select an option from " + displayname;
                        }
                	}
                    break;

                case "select-one":
                    if(required) valid = !isSpace(x[i].value);
                    else valid = true;
                    
                    if(!valid){
                        errors++;
                        if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                        else{
                            if(!displayname) displayname = x[i].name;
                            message += "\n(" + (errors) + ") You must select an option from " + displayname;
                        }
                	}
                    break;
                    
                case "select-multiple":
                    if(required) valid = !isSpace(x[i].value);
                    else valid = true;
                    
                    if(!valid){
                        errors++;
                        if(errormsg) message += "\n(" + (errors) + ") " + errormsg;
                        else{
                            if(!displayname) displayname = x[i].name;
                            message += "\n(" + (errors) + ") You must select at least one option from " + displayname;
                        }
                	}
                    break;
 
                default:
                    // this HTML element unknown and will not be validated
                    break;
                
            }
            
            // This portion of the code permits validation based on some conditional expression using the eval function
            if(dependentcondition){
                try{
                    valid = eval(dependentcondition);
                }
                catch(e){
                    valid = false;
                }
                
                if(valid){
                    errors++;
                    if(dependenterror) message += "\n(" + (errors) + ") " + dependenterror;
                    else{
                        if(!displayname) displayname = x[i].name;
                        message += "\n(" + (errors) + ") There is a problem with " + displayname;                            
                    }
                }
            }
        }
    }
    
    if(errors>0){
        message = heading + message;
		message += "\n\n" + errors + " error";
		if(errors>1) message += "s";
		message += " detected";
		alert(message);
		toggleFormButtonLock(form_obj,false,button_text);

		// Set the status of client-side javascript to "disabled"  because there was an error in the form
		// Using this technique eliminates 99.9% of all Web form spam by forcing client-side javascript validation to take place
		if(form_obj._config_javascript_status) form_obj._config_javascript_status.value = "disabled";

		return false;
	}  
	else if(errors<=0){
        if(form_obj.Email||form_obj.x_email){
        
            // Standard HTML forms use an email address field called "Email" and the checkout form uses an email
            // address field called "x_email" per the documentation on Authorize.Net's payment gateway.  This code
            // check to see which email field is being used
            var use_this_email_address;
            if(form_obj.Email) use_this_email_address = form_obj.Email.value;
            else use_this_email_address = form_obj.x_email.value;
        
            message  = "Reply-to Email Address Confirmation\n";
            message += "_______________________________________________\n\n";
            message += "Please confirm that:\n\n";
            message +=  use_this_email_address + " \n\n";
            message += "is your correct email address. Please note that\n";
            message += "this email address MUST be able to receive incoming\n";
            message += "internet email or you will NOT receive your registration\n";
            message += "confirmation. Enter only ONE email address.\n\n";
            message += "Thank you,\nHGR General Contractors\n";
            message += "_______________________________________________\n\n";
                            
    		if(confirm(message)) return true;
    		else{
    			toggleFormButtonLock(form_obj,false,button_text);
    			return false;
    		}
        }
        else alert("FORM ERROR: The form supplied does not contain a required form element named \"Email.\" Please check the form.");
	}
}