var CustomTextInput = (function(inputElement, defaultValue) {

    return function(inputElement, defaultValue) {
        var element,
        format,
        valid,
        status,
        defaultText,
        required;


        this.getElementID = function() {
            return element.getAttribute('id');
        };
		
		
        this.setElement = function(theElement) {
            element = theElement;
        };

        this.setFormat = function(str) {
            var start = str.indexOf('[') + 1;
            var end = str.indexOf(']');
            format = str.slice(start, end);
        };

        this.setRequired = function(bool) {
            required = bool;
        };

        this.isRequired = function() {
            return required;
        };

        this.setDefaultText = function(text) {
            defaultText = text;
        };

        this.getDefaultText = function() {
            return defaultText;
        };

        this.setValue = function(text) {
            element.value = text;
        };

        this.getValue = function() {
            return element.value;
        };

        this.setValid = function(newBool) {
            valid = newBool;
        };
        this.setStatus = function(newStatus) {
            status = newStatus;
        };

        this.getStatus = function() {
            return status;
        };
        this.isValid = function() {
            return valid;
        };
        this.getStatus = function() {
            return format;
        };

        this.validateFormat = function() {
            var bool;
            switch (format) {
            case "alpha":
                bool = alphaOnly(this.getValue());
                break;
            case "email":
                bool = emailFormat(this.getValue());
                break;
            case "num":
                bool = numbersOnly(this.getValue());
                break;
            case "alphanum":
                bool = isAlphaNumerical(this.getValue());
                break;
            case "date":
                bool = dateFormat(this.getValue());
                break;
            case "phone":
                bool = phoneFormat(this.getValue());
                break;
            case "any":
                bool = isClean(this.getValue());
                break;
            }

            return bool;
        };

        this.displayInvalid = function() {
            element.className = element.className.replace(" valid", " invalid");
            element.className = element.className.replace(" neutral", " invalid");
        };

        this.displayValid = function() {
            element.className = element.className.replace(" invalid", " valid");
            element.className = element.className.replace(" neutral", " valid");
        };

        this.displayNeutral = function() {
            element.className = element.className.replace(" invalid", "  neutral");
            element.className = element.className.replace(" valid", "  neutral");
        };

        this.validate = function() {
            if (this.getValue() === "" || this.getValue() === this.getDefaultText()) {
                if (this.isRequired()) {
                    this.setValid(false);
                    this.displayInvalid();
                } else {
                    this.setValid(true);
                    this.displayValid();
                }
            } else {
                if (this.validateFormat()) {
                    this.setValid(true);
                    this.displayValid();
                } else {
                    this.setValid(false);
                    this.displayInvalid();
                }
            }
        };
        


        this.addTriggers = function() {

            var bs = BrowserDetect.browser + ' verision ' + BrowserDetect.version;
            element.onfocus = function() {
                if (bs === 'Explorer version 7') {
                    element.select();
                }
                triggerFocused(this);
                validateForm();
            };

            element.onclick = function() {
                this.select();
                validateForm();
            };

/*
            element.onpropertychange = function(){
                triggerChanged(this);
            	validateForm();
            };
*/
            
            element.onblur = function() {
                javascript: triggerBlur(this);
                validateForm();
            };

            element.onkeyup = function() {
                triggerChanged(this);
                validateForm();
            };

            element.onkeypress = function() {
                triggerChanged(this);
                validateForm();
            };

            element.onkeydown = function() {
                triggerChanged(this);
                validateForm();
            };
        };

        this.blurred = function() {
            if (!this.isValid()) {
                if (this.getValue() === "") {
                    this.setValue(this.getDefaultText());
                }
            }

            if (!this.isRequired() && (this.getValue() === "" || !this.getValue())) {
                this.setValue(this.getDefaultText());
            }
        };

        this.focused = function() {
	        this.validate();
        };

        this.valueChanged = function() {
            this.validate();
        };

        //construct object
        this.setElement(inputElement);
        this.setValid(false);
        this.setStatus('inValid');
        this.setDefaultText(defaultValue);
        this.addTriggers();
        this.setFormat(element.className);
        this.validate();
        element.className = element.className + " customTextField neutral";

        element.setAttribute("autocomplete", "off");

        if (element.className.indexOf('required') !== -1) {
            this.setRequired(true);
            this.validate();
        } else {
            this.setRequired(false);
            if (format !== 'email') {
                this.validate();
            }
            this.displayValid();
            this.setValid(true);
        }

        if (element.value === defaultValue && required) {
            this.setValid(false);
            this.displayInvalid();
        }

    };
} ());

var CustomTextAreaInput = (function(inputElement, defaultValue) {

    return function(inputElement, defaultValue) {
        var element,
        format,
        valid,
        status,
        defaultText,
        required;

        this.getElementID = function() {
            return element.getAttribute('id');
        };

        this.setElement = function(theElement) {
            element = theElement;
        };

        this.setFormat = function(str) {
            var start = str.indexOf('[') + 1;
            var end = str.indexOf(']');
            format = str.slice(start, end);
        };

        this.setRequired = function(bool) {
            required = bool;
        };

        this.isRequired = function() {
            return required;
        };

        this.setDefaultText = function(text) {
            defaultText = text;
        };
        this.getDefaultText = function() {
            return defaultText;
        };
        this.setValue = function(text) {
            element.value = text;
        };
        this.getValue = function() {
            return element.value;
        };
        this.setValid = function(newBool) {
            valid = newBool;
        };
        this.setStatus = function(newStatus) {
            status = newStatus;
        };

        this.getStatus = function() {
            return status;
        };
        this.isValid = function() {
            return valid;
        };
        this.getStatus = function() {
            return format;
        };

        this.validateFormat = function() {
            var bool;
            switch (format) {
            case "alpha":
                bool = alphaOnly(this.getValue());
                break;
            case "email":
                bool = emailFormat(this.getValue());
                break;
            case "num":
                bool = numbersOnly(this.getValue());
                break;
            case "alphanum":
                bool = isAlphaNumerical(this.getValue());
                break;
            case "date":
                bool = dateFormat(this.getValue());
                break;
            case "phone":
                bool = phoneFormat(this.getValue());
                break;
            case "any":
                bool = isClean(this.getValue());
                break;
            }

            return bool;
        };

        this.displayInvalid = function() {
            element.className = element.className.replace(" valid", " invalid");
            element.className = element.className.replace(" neutral", " invalid");
        };

        this.displayValid = function() {

            element.className = element.className.replace(" invalid", " valid");
            element.className = element.className.replace(" neutral", " valid");
        };

        this.displayNeutral = function() {
            element.className = element.className.replace(" invalid", "  neutral");
            element.className = element.className.replace(" valid", "  neutral");
        };

        this.validate = function() {
            if (this.getValue() === "" || this.getValue() === this.getDefaultText()) {
                if (this.isRequired()) {
                    this.setValid(false);
                    this.displayInvalid();
                } else {
                    this.setValid(true);
                    this.displayValid();
                }
            } else {
                if (this.validateFormat()) {
                    this.setValid(true);
                    this.displayValid();
                } else {
                    this.setValid(false);
                    this.displayInvalid();
                }
            }
        };

        this.addTriggers = function() {

            var bs = BrowserDetect.browser + ' verision ' + BrowserDetect.version;
            element.onfocus = function() {
                if (bs === 'Explorer version 7') {
                    element.select();
                }
                triggerFocused(this);
                validateForm();
            };

            element.onclick = function() {
                this.select();
                validateForm();
            };
            
            element.onpropertychange = function(){
                triggerChanged(this);
            	validateForm();
            };

            element.onblur = function() {
                javascript: triggerBlur(this);
                validateForm();
            };

            element.onkeyup = function() {
                triggerChanged(this);
                validateForm();
            };

            element.onkeypress = function() {
                triggerChanged(this);
                validateForm();
            };

            element.onkeydown = function() {
                triggerChanged(this);
                validateForm();
            };
        };

        this.blurred = function() {
            if (this.isRequired() && !this.isValid()) {
                if (this.getValue() === "") {
                    this.setValue(this.getDefaultText());
                }
            } else if (!this.isRequired() && (!this.getValue() || this.getValue() === "")) {
                this.setValue(this.getDefaultText());
                this.setValid(true);
                this.displayValid();
            }
        };

        this.focused = function() {
            //test this
            };

        this.valueChanged = function() {
            this.validate();
        };

        //construct object
        this.setElement(inputElement);
        this.setValid(false);
        this.setStatus('inValid');
        this.setDefaultText(defaultValue);
        this.addTriggers();
        this.setFormat(element.className);
        this.validate();
        element.className = element.className + " customTextArea neutral";

        if (element.className.indexOf('required') !== -1) {
            this.setRequired(true);
            this.validate();
        } else {
            this.setRequired(false);
            this.validate();
        }

        if (element.value === defaultValue && required) {
            this.displayInvalid();
        }
    };
} ());

var CustomSelectInput = (function(inputElement, defaultValue) {

    return function(inputElement, defaultValue) {

        var element,
        format,
        valid,
        status,
        defaultOption,
        required;
        var triggerEL;

        this.getElementID = function() {
            return element.getAttribute('id');
        };

        this.setElement = function(theElement) {
            element = theElement;
        };

        this.setFormat = function(str) {
            var start = str.indexOf('[') + 1;
            var end = str.indexOf(']');
            format = str.slice(start, end);
        };

        this.setRequired = function(bool) {
            required = bool;
        };

        this.isRequired = function() {
            return required;
        };

        this.setDefaultOption = function(text) {
            defaultOption = text;
        };
        this.getDefaultOption = function() {
            return defaultOption;
        };
        this.setValid = function(newBool) {
            valid = newBool;
        };
        this.setStatus = function(newStatus) {
            status = newStatus;
        };

        this.getStatus = function() {
            return status;
        };
        this.isValid = function() {
            return valid;
        };
        this.getStatus = function() {
            return format;
        };

        this.displayInvalid = function(trigger) {
            trigger.setAttribute('id', 'invalid');
        };

        this.displayValid = function(trigger) {
            trigger.setAttribute('id', 'valid');
        };

        this.validate = function(option, trigger) {
            if (this.isRequired()) {
                if (!option || option === this.getDefaultOption()) {
                    this.setValid(false);
                    return false;
                } else {
                    this.setValid(true);
                    return true;
                }
            } else {
                this.setValid(true);
                return true;
            }
        };

        this.setElement(inputElement);
        this.setValid(false);
        this.setStatus('inValid');
        this.setDefaultOption(defaultValue);

        if (element.className.indexOf('required') !== -1) {
            this.setRequired(true);
            this.validate();
        } else {
            this.setRequired(false);
            this.validate();
        }

    };
} ());


var CustomRadioInput = (function(radioGroup, radioGroupName, radioCont) {

    return function(radioGroup, radioGroupName, radioCont) {
        var listHolder,
        theStr,
        strLength,
        label,
        labelEL,
        str,
        valid,
        radioContainer,
        required;
        var labelArr = [];
        var radioArr = [];
        var checkCount = 0;

        this.getElementID = function() {
            return radioGroupName;
        };

        this.setRequired = function(bool) {
            required = bool;
        };

        this.isRequired = function() {
            return required;
        };

        this.setRadioContainer = function(e) {
            radioContainer = e;
        };

        this.getRadioContainer = function() {
            return radioContainer;
        };

        this.getRadioOptions = function() {
            return radioArr;
        };

        this.getLabels = function() {
            return labelArr;
        };

        this.setValid = function(bool) {
            valid = bool;
        };

        this.isValid = function() {
            return valid;
        };

        this.displayInvalid = function() {
            var li = this.getRadioContainer().getElementsByTagName('li');
            for (var j = 0; j < li.length; ++j) {
                var tl = li[j].childNodes[0];
                tl.className = 'radioBtnInvalid';
            }
        };


        this.getOptStr = function(str) {
            var start = str.indexOf('{') + 1;
            var end = str.indexOf('}');
            return str.slice(start, end);
        };

        this.selectMe = function(o) {

            var a = this.getRadioOptions();
            var l = o;
            var n = this.getOptStr(l.getAttribute('name'));
            var li = this.getRadioContainer().getElementsByTagName('li');

            //check/uncheck the inputs
            for (var c = 0; c < a.length; ++c) {
                var cr = a[c];
                var cv = cr.value;
                if (cv === n) {
                    cr.checked = true;
                    this.setValid(true);
                } else {
                    cr.checked = false;
                }
            }


            //switch selected label to radioBtnOn
            l.className = 'radioBtnOn';

            //switch any on labels to radioBtnOff
            for (var j = 0; j < li.length; ++j) {
                var tl = li[j].childNodes[0];
                var tn = this.getOptStr(tl.getAttribute('name'));
                if (tn !== n) {
                    tl.className = 'radioBtnOff';
                }
            }

        };

        this.setUpRadioGroup = function(radioArray) {
            var radioOpts = radioArray;
            for (var i = 0; i < radioOpts.length; ++i) {
                var e = radioOpts[i];
                listHolder = e.parentNode;
                theStr = listHolder.innerHTML;
                strLength = theStr.length;
                e.className = 'hidden';
                label = "";
                for (var x = strLength; x > 0; --x) {
                    if (theStr.charAt(x) === ">") {
                        label = theStr.substr(x + 1, strLength);
                        break;
                    }
                }
                var labelEL = new Element('label', {
                    id: 'radioLabel',
                    name: e.getAttribute('id') + "{" + e.value + "}"
                });

                labelEL.className = 'radioBtnOff';
                if (e.checked) {
                    ++checkCount;
                    labelEL.className = 'radioBtnOn';
                    this.setValid(true);
                }

                labelEL.wraps(e);
                labelEL.innerHTML += label;
                labelEL.setAttribute('onmouseup', 'javascript: triggerSelect(this);validateForm();');
                strLength = listHolder.innerHTML.length;
                str = listHolder.innerHTML;
                str = str.substr(0, strLength - label.length);
                listHolder.innerHTML = str;
                radioArr.push(e);
                labelArr.push(labelEL);
            }

            if (checkCount === 0 && this.isRequired()) {
                this.setValid(false);
                this.displayInvalid();
            }

            if (checkCount === 0 && !this.isRequired()) {
                this.setValid(true);
            }

        };

        this.setRequired(false);
        for (var i = 0; i < radioGroup.length; ++i) {
            var e = radioGroup[i];
            if (e.className.indexOf('required') !== -1) {
                this.setRequired(true);
            }
        }
        this.setRadioContainer(radioCont);
        this.setUpRadioGroup(radioGroup);
    };
} ());

var CustomCheckBoxInput = (function(checkBox, values) {
    return function(checkBox, values) {

        var isValid,
        checkValue,
        uncheckValue,
        isChecked,
        checkHolder,
        isRequired;


        this.setChecked = function(bool) {
            isChecked = bool;
        };

        this.getChecked = function() {
            return isChecked;
        };

        this.setCheckValue = function(value) {
            checkValue = value;
        };

        this.getCheckValue = function() {
            return checkValue;
        };

        this.setUnCheckValue = function(value) {
            uncheckValue = value;
        };

        this.getUnCheckValue = function() {
            return uncheckValue;
        };

        this.getValues = function(string) {
            this.setCheckValue(string.split('::')[0]);
            this.setUnCheckValue(string.split('::')[1]);
        };

        this.setRequired = function(bool) {
            isRequired = bool;
        };

        this.setValid = function(bool) {
            isValid = bool;
        };

        this.isValid = function(bool) {
            return isValid;
        };

        this.validate = function() {
            if (isRequired && !isChecked) {
                this.setValid(false);
                this.displayInValid();
            } else if (isRequired && isChecked) {
                this.setValid(true);
            } else if (!isRequired) {
                this.setValid(true);
            }
        };

        this.displayInValid = function() {
            checkHolder.className = 'checkboxInvalid';
            checkBox.value = uncheckValue;
            checkBox.removeAttribute('checked');
        };

        this.displayUnchecked = function() {
            checkHolder.className = 'checkboxOff';
            checkBox.value = uncheckValue;
            checkBox.removeAttribute('checked');
        };

        this.displayChecked = function() {
            checkHolder.className = 'checkboxOn';
            checkBox.value = checkValue;
            checkBox.setAttribute('checked','checked');

        };

        this.toggleCheck = function() {
            if (isChecked) {
            	checkBox.value = uncheckValue;
                this.setChecked(false);
                this.displayUnchecked();
                this.setValid(false);
                if(isRequired){
                	this.displayInValid();
                }
            } else {
            	checkBox.value = checkValue;
                this.setValid(true);
                this.setChecked(true);
                this.displayChecked();
            }
        };

        this.constructCheckbox = function(element) {
            if (element.className.indexOf('required') !== -1) {
                this.setRequired(true);
            } else {
                this.setRequired(false);
            }

			checkHolder = new Element('label', {
				id: checkBox.getAttribute('id'),
                name: checkBox.getAttribute('id')
            });
            
            checkBox.removeAttribute('onclick');

			switch(checkBox.checked){
				case true:
					checkHolder.className = 'checkboxOn';
					checkBox.setAttribute('checked','checked');
					this.setValid(true);
					this.setChecked(true);
				break;
				case false:
					checkHolder.className = 'checkboxOff';
					checkBox.removeAttribute('checked');
					this.setValid(false);
					this.setChecked(false);
                	if(isRequired){
                		this.displayInValid();
                	}
				break;
			}
			
            checkHolder.wraps(element);
            checkHolder.onmouseup = function() {
                triggerCheck(this);
            };
            

            element.setStyle('display', 'none');

        };

        this.getValues(values);
        this.constructCheckbox(checkBox);
    }
    
} ());




