﻿/*
Included are all files needs to run a javascript validation.
There is also a compressed version of this file in the release directory.
*/


/*******************
start common.js
/******************/

// JScript File
function browser () {
	if (window.XMLHttpRequest && !window.ActiveXObject){
		//mozilla, opera, etc.
		this.mozilla = true
	}
	else if (window.ActiveXObject) {
		// ie7 and below
		this.mozilla = false
	}			
}

//global variables
b = new browser();

/*******************
end common.js
/******************/


/*******************
start errorBox.js
/******************/

function errorBox(positionCode) {
    
    this.mousePosX = 0;
	this.mousePosY = 0;
	this.numErrors = 0;
	this.pcode = positionCode;
	
	this.initBoxCode();
	this.e = document.getElementById('errorBox');
	
	this.setOffsets(positionCode);
	this.init();
	
	eBoxControl = this;
}

errorBox.prototype.setOffsets = function(positionCode) {
    switch (positionCode) {
        case "tl":
            //top left
            this.xOffset = -this.e.offsetWidth;
	        this.yOffset = -this.e.offsetHeight;
            
            break;
            
        case "bl":
            //bottom left
            this.xOffset = -this.e.offsetWidth;
	        this.yOffset = 0;
            
            break;
        
        case "tr":
            this.xOffset = 0;
	        this.yOffset = -this.e.offsetHeight;
        
            break;
            
        default:
            //defaults to bottom left
            this.xOffset = -this.e.offsetWidth;
	        this.yOffset = 0;
            
            break;
    }
}

errorBox.prototype.init = function() {
    $('#errorBox').easydrag();
    
    cc = document.getElementById('errorClose');
    cc.onclick = function(e) {
        //need to find away around using eBoxControl
        eBoxControl.close();
        return false;
    }
    
    this.hide();
}

errorBox.prototype.show = function(event) {
    this.e.style.display = "block";
    this.setOffsets(this.pcode);
    
    if (!b.mozilla) {
		this.mousePosX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		this.mousePosY = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (b.mozilla) {
		this.mousePosX = event.clientX + window.scrollX;
		this.mousePosY = event.clientY + window.scrollY;
	}
    	
    this.e.style.top = this.mousePosY+ this.yOffset +'px';
    this.e.style.left = this.mousePosX+ this.xOffset +'px';
    
    if(!b.mozilla) {
        //hideSelects();
    }
    
}

errorBox.prototype.hide = function() {    
    this.e.style.top = -10000+'px';
    this.e.style.left = -10000+'px';
    
    if(!b.mozilla) {
        //showSelects();
    }
    
}

errorBox.prototype.close = function() {
    this.hide();
    this.clearErrors();
}

errorBox.prototype.addError = function(errorId,msg) {
    errorList = document.getElementById("errorContainer");
    
    newError = document.createElement("div");
    newError.className = "error";
    newError.setAttribute("id",errorId);
    newError.innerHTML = msg;


    errorList.appendChild(newError);
    this.numErrors++;
}

errorBox.prototype.clearErrors = function() {
    errorList = document.getElementById("errorContainer");
    errorList.innerHTML = "";
}

errorBox.prototype.removeError = function(errorId) {
    errorList = document.getElementById("errorContainer");
    
    try {
        errorList.removeChild($(errorId));
        this.numErrors--;
        if (this.numErrors = 0) {
            this.close();
        }
   }
   catch(e) {}
}

errorBox.prototype.initBoxCode = function() {
    var bCode = "";
    
    var bElement = document.createElement("div");
    bElement.setAttribute("id","errorBox");
    bElement.className = "errorBox";
    
    bCode += "<div id=\"errorTitle\">";
    bCode += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
    bCode += "<tr>";
    bCode += "<td class=\"errorTitle\">ALERT!</td>";
    bCode += "<td style=\"padding: 0px 10px 0px 0px;\"><div id=\"errorClose\"></div></td>";
    bCode += "</tr>";
    bCode += "</table>";
    bCode += "</div>";
    bCode += "<div id=\"errorMsg\" class=\"errorMsg\">";
    bCode += "The following is a list of things you must complete before sbmmitting your request:";
    bCode += "</div>";
    bCode += "<div id=\"errorContainer\" class=\"errorContainer\">";
    bCode += "</div>";
    
    bElement.innerHTML = bCode;
    
    $("#errorBox").remove();
    
    document.body.appendChild(bElement);
}

function hideSelects() {
    sArray = document.getElementsByTagName("select");
    
    for(var x = 0; x < sArray.length; x++) {
        sArray[x].style.display = "none";
    }
}

function showSelects() {
    sArray = document.getElementsByTagName("select");
    
    for(var x = 0; x < sArray.length; x++) {
        sArray[x].style.display = "";
    }
}


/*******************
end errorBox.js
/******************/


/*******************
start formField.js
/******************/

function formField(fieldId,errorMsg,validationType,defaultValue) {
    this.fId = fieldId;
    this.e = document.getElementById(fieldId);
    this.value = this.e.value;
    this.errorMsg = errorMsg;
    this.validationType = validationType;
    this.defaultValue = defaultValue;
}

formField.prototype.hasValue = function() {
    if (this.value != "") {
        return true;
    }
    else {
        return false;
    }
}

formField.prototype.validEmail = function() {
    if (this.e.value.search(/^[a-zA-Z.0-9]+[\w\-]*[@]([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,5}$/) == -1) {
        return false;
    }
    else {
        return true;
    }
}

formField.prototype.validPhone = function() {
    if (this.e.value.search(/((\(\d{3}\) ?)|(\d{3}(-|.)))?\d{3}(-|.)\d{4}/) == -1) {
        return false;
    }
    else {
        return true;
    }
}

formField.prototype.isCheckedSingle = function() {
    if (this.e.checked) {
        return true;
    }
    else {
        return false;
    }
}

formField.prototype.validNumber = function() {
    if (this.e.value.search(/^[0-9]/) == -1) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.minLength = function(length) {
    if (this.e.value.length < length) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.maxLength = function(length) {
    if (this.e.value.length > length) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.equalsLength = function(length) {
    if (this.e.value.length != length) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.checkDefault = function() {
    if (this.e.value == this.defaultValue) return false;
    
    return true;
}

formField.prototype.validate = function() {
    switch(this.validationType) {
        case "email":
            return this.validEmail();
            break;
           
        case "singlecheck":
            return this.isCheckedSingle();
            break;
            
        case "null":
            return this.hasValue();
            break;

        case "phone":
            return this.validPhone();
            break;
        
        case "number":
            return this.validNumber();
            break;
        
        case "shortZip":
            var validNumber = this.validNumber();
            var validLength = this.equalsLength(5);
            var valid = validNumber && validLength ? true : false;
            
            return valid;
            
            break;
        
        case "hasDefault":
            var notDefault = this.checkDefault();
            var hasVal = this.hasValue();
            var valid = notDefault && hasVal ? true : false;
            
            return valid;
            
            break;
            
        default:
            return this.hasValue();
            break;
    
    }
    
}
/*******************
end formField.js
/******************/


/*******************
start formValidator.js
/******************/

// JScript File


function formValidator(boxPosition) {
    this.fields = new Object();
    this.ebox = new errorBox(boxPosition);
    this.fErrors = new Object();
    this.comparedFields = new Object();
    this.radioButtons = new Object();
    this.exclamation = "<strong>!</strong>&nbsp;&nbsp;&nbsp;";
}

formValidator.prototype.addErrorMessage = function(errorID,errorMsg,hasExclamation) {
    var errorSet = new Object();

    if(hasExclamation) {
        errorMsg = this.exclamation + errorMsg;
    }

    errorSet.errorID = errorID;
    errorSet.hasExclamation = hasExclamation;
    errorSet.errorMsg = errorMsg;
    
    this.fErrors[errorID] = errorSet;
}

formValidator.prototype.addField = function(fieldId,errorMsg,hasExclamation,validationType,defaultValue) {
    if(hasExclamation) {
        errorMsg = this.exclamation + errorMsg;
    }
    this.fields[fieldId] = new formField(fieldId,errorMsg,validationType,defaultValue);
}

formValidator.prototype.removeField = function(fieldId) {
    this.fields[fieldId] = null;
}

formValidator.prototype.addComparedFields = function(setName,fieldId1, fieldId2, errorMsg) {
    var fieldSet = new Object();
    fieldSet.field1 = new formField(fieldId1,"",null);
    fieldSet.field2 = new formField(fieldId2,"",null);
    fieldSet.errorMsg = errorMsg;
    
    this.comparedFields[setName] = fieldSet;
}

formValidator.prototype.addRadioButtons = function(name,errorMsg) {
    var radioSet = new Object();
    var radioArray = document.getElementsByName(name);
    
    for (var i = 0; i < radioArray.length; i++) {
        radioSet[i] = radioArray[i].checked;
    }
    
    this.radioButtons[name] = new Object();
    this.radioButtons[name].radioSet = radioSet;
    this.radioButtons[name].errorMsg = errorMsg;
}

formValidator.prototype.validate = function(event) {
    var valid = true;
    this.ebox.clearErrors();
    
    var hasValues = this.checkFields();
    var sameValues = this.compareFields();
    var validRadios = this.checkRadios();
    var forcedErrors = this.checkForcedErrors();
    
    valid = hasValues && sameValues && validRadios && forcedErrors ? true : false;
    
    if(!valid) {
        this.ebox.show(event);
        return false;
    }
    else {
        return true;
    }
}

formValidator.prototype.checkForcedErrors = function() {
    var result = true;
    
    for(fError in this.fErrors) {
        if (fError != 'extend') {
            f = this.fErrors[fError];

            this.ebox.addError(f.errorID,f.errorMsg);
            result = false;
        }
    }
    
    return result;
}

formValidator.prototype.checkFields = function() {
    var result = true;
    
    for(field in this.fields) {
        if (field != 'extend') {
            f = this.fields[field];
            
            if (!f.validate()) {
                result = false;
                this.ebox.addError(f.id,f.errorMsg);
            }
        }
    }
    
    return result;
}

formValidator.prototype.compareFields = function() {
    var result = true;
    
    for(fieldSet in this.comparedFields) {
        if (fieldSet != 'extend') {
            var fSet = this.comparedFields[fieldSet];
            
            if(fSet.field1.value != fSet.field2.value) {
                result = false;
                this.ebox.addError(fieldSet,this.exclamation + fSet.errorMsg);
            }
        }
    }
    
    return result;
}

formValidator.prototype.checkRadios = function() {
    var result = true;
    
    for(var radioSet in this.radioButtons) {
        if(radioSet != 'extend') {
            var rSet = this.radioButtons[radioSet].radioSet;
            var hasChecked = false;
            
            for(var radio in rSet) {
                if(radio != 'extend') {
                    if (rSet[radio]) hasChecked = true;
                }
            }
            
            if (!hasChecked) {
                result = false;
                this.ebox.addError(radioSet,this.exclamation + this.radioButtons[radioSet].errorMsg);
            }
        }
    
    }
    
    return result;
}


/*******************
end formValidator.js
/******************/






