﻿//-----------------------------------------------------------
// Original Author: Trevor Gee
// Date: 06 Febraury 2007
// Description: Defines the standard set of design structures
//
// Change History Goes Below...
// Author: ...
// Date: ...
// Description: ...
//-----------------------------------------------------------

//--------------------
// Validation Modules
//--------------------

// Required Module
function Required() {
	// Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        if (value == null || value == "") throw new Error(field + ": Is a required field.");
    }
}

// A numeric field
function Numeric() {
    // Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        if (value == null || value == "") return;
        if (value != parseFloat(value)) throw new Error(field + ": Is required to be numeric.");
    }
}

// Length Module
// input: limit = the maximum length of the field
function Length(limit) {
    // Modules variables
    var limit = limit;
    
    // Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        if (value == null || value == "") return;
        if (value.length > limit) throw new Error(field + ": Exceeds the maximum length of " + limit + ".");
    }
}

// Range Module
function Range(min, max) {
    // Module Variables
    var min = min;
    var max = max;

    // Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        if (value == null || value == "") return;
        if (value < min || value > max) throw new Error(field + ": Needs to be in the range " + min + " to " + max + ".");
    }
}

// Email Module
function Email() {
    // Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        if (value == null || value == "") return;
        var regExpression = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        if (!value.match(regExpression)) throw new Error(field + ": Is not a valid email address.");
    }
}

// Phone Module
function Phone() {
    // Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        if (value == null || value == "") return;
        var regExpression = "(\\(\\d{3}\\)|\\d{3}-)?\\d{8}";
        if (!value.match(regExpression)) throw new Error(field + ": Is not a valid telephone number. Phone numbers must be in the format (xx)xxxxxxxx.");
    }
}

// URL Module
function URL() {
    // Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        if (value == null || value == "") return;
        var regExpression = "//([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
        if (!value.match(regExpression)) throw new Error(field + ": Is not a valid URL. Prefix your URL with http://");
    }
}

// Validates a field as being chinese only
function Chinese() {
    // Validate a specific field
    // input: field = the field that we are evaluating
    // input: value = the value of the field
    this.validate = function(field, value) {
        var length = value.length;
		for (var i=0; i<length; i++) {
		    var character = value.substring(i,i+1);
	        if (this.getCode(character) != -1) {
	            throw new Error(field + ": This field must only be chinese characters.");
	        }
		}
    }
    
    // Get the character code
	this.getCode = function(tmp) {
        // Test the first ascii range
        for (var i=1;i<127;i++) if (unescape('%' + i.toString(16)) == tmp) return i;
        
        // Test the second range
        for (var i=160;i<=255;i++) if (unescape('%' + i.toString(16)) == tmp) return i;
        
        // Must be out of that range!
        return -1;
    }
}

//--------------------
// Validation Report
//--------------------

// Defines a class for holding the validation report
function ValidationReport() {
    // Private variables
    var firstError = true;
    var messages = "";
  
    // Validate Control
    // input: fieldName = The name of the field that is being validated
    // input: control = The name of the control being validated
    // input: errorControl = The control to indicate the error
    // input: rules = the array of error rules to apply to the control
    this.testControl = function(fieldName, control, errorControl, rules) {
        // Get the value that we are dealing with
        var value = control.value;
        
        // Test the control
        try {
            // Validate
            var length = rules.length;
            for (var i=0; i<length; i++) rules[i].validate(fieldName, control.value);        
        
            // if no error encountered
            this.unSetError(control); errorControl.innerHTML = "";
        }
        catch (e) {
            if (firstError) { control.focus(); firstError = false; }
            this.setError(control); errorControl.innerHTML = e.message;    
            messages += e.message + "<br />";
        }       
    }
    
    // Validate a combo control of its selected values
    // input: fieldName = The name of the field being examined
    // input: controlName = The name of the control in question
    // input: errorControl = The control to hold the error messages
    // input: unselected = The value of unselected
    this.testCombo = function(fieldName, controlName, errorControl, unselected) {
        // Retrieve the control name
        var value = $F(controlName);
        var control = $(controlName);
        
        // If the value is the unselected value, throw toys out of cot!
        if (value == unselected) {
            if (this.firstError) { control.focus(); firstError = false; }
            var errorMessage = fieldName + ": Is a required field.";
            errorControl.innerHTML = errorMessage;
            message += errorMessage + "<br />";
        } else errorControl.innerHTML = "";
     }
    
    // Set a control into error state
    // input: control = The control that we are setting
    this.setError = function(control) {
		control.style.border = "1px solid #CA0A0A";
	}
	
	// Unset a control from the error state
	// input: control = The control that we are setting
	this.unSetError = function(control) {
	    control.style.border = "1px solid #696969";
	}   

    // Checks to see if there was an error
    this.wasError = function() {
		return !(firstError);
    }

    // Get the current error messages    
    this.getMessages = function() {
        return messages;
    }
    
    // Clear the current error messages
    this.clearMessages = function() {
        messages = ""; firstError = true;
    }
}