var formValidate = {
    // var mandatoryFields=Array('name', 'blog', 'email', 'iagree');
    // var mandatoryCheckAlt=Array('platform', 'category', 'blogger_type');

    // vars 
    bad: Array(),
    urlRE : /^(http(s)?)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?[\/\w.\?\=\&\#]*$/i,
    emailRE : /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i,

    // objects
    mandatoryFields : {
        name:         Array('mandatory'),
        blog:         Array('mandatory', 'validate'),
        email:        Array('mandatory', 'validate'),
        platform:     Array('mandatory'),
        category:     Array('mandatory'),
        blogger_type: Array('mandatory'),
        iagree:       Array('checked')
    },
    mandatoryCheckAlt : {
        platform:      'platform_other',
        category:      'category_other',
        blogger_type:  'blogger_type_other'
    },
    validateFields : {
        email:       'emailRE',
        blog:        'urlRE'
    },

    // functions
    checked: function(field) {
        var fc=document.getElementById(field);
        return fc.checked;
    },

    mandatory: function(field) {
        var fv=document.getElementById(field).value;
        var alts=this.mandatoryCheckAlt;
        // this needs to be done better
        if(fv=='') return false;
        if(fv==0) {
            var f=eval("alts."+field);
            if(f!==undefined) { 
                var af=document.getElementById(f);
                if(af!==null) {
                    if(af.value!='') {
                        return true;
                    }
                } else {
                    // alert('returning false for '+field);
                    return false;
                }
                // alert('maybe someday: '+af);
            }
            // alert('returning false for '+field);
            return false;
        }
        // alert('returning true for '+field);
        return true;
    },

    validate: function(field) {
        var valPatternName=eval("this.validateFields."+field);
        var valPattern=eval("this."+valPatternName);
        // alert('validating '+field+' with '+valPattern);
        var theValue=document.getElementById(field).value;
        return this.validatePattern(valPattern, theValue);
    },

    validatePattern: function(pattern, toValidate) {
	    return pattern.test(toValidate);
    },

    validateForm: function() {
        this.bad=Array();
        formManipulate.removeError('error-holder');
        formManipulate.resetStyles();
        var mandatoryFields=this.mandatoryFields;
        for (i in mandatoryFields) {
            // alert('you want to validate '+i+' or put another way (I hope) '+mandatoryFields[i]);
            var toDo=mandatoryFields[i];
            for(f=0; f<toDo.length; f++) {
                // alert('I want to '+toDo[f] + ' to '+i);
                var func=toDo[f];
                if(eval("this."+func+"(i)")==false) {
                    this.bad.push(i);
                    break;
                }
            }
        }
        if(this.bad.length > 0) {
            // alert(this.bad);
            for(i in this.bad) {
                // alert(this.bad[i]);
                document.getElementById('label_'+this.bad[i]).style.color="#ff8c26";
            }
            var eText='There were some errors filling out your form';
            formManipulate.showError(eText, 'eH3', 'error-holder');
            // ff7d17
        } else {
            document.getElementById('widget_request').submit();
        }
    }
}

var formManipulate = {
    toggleShowField: function(id, onoff) {
        document.getElementById(id).style.display=onoff;
    },
    showError: function(info, id, eParent) {
        var eText=document.createTextNode(info);
        var eH3=document.createElement('h3');
        eH3.setAttribute('class','alert');
        eH3.setAttribute('className','alert');
        eH3.id=id;
        eH3.appendChild(eText);
        document.getElementById(eParent).appendChild(eH3);
    },
    removeError: function(eParent) {
        document.getElementById(eParent).innerHTML='';
    },

    toggleFieldDiv: function(id, onoff) {
        var div=document.getElementById(id+'_disp');
        // div.style.display=onoff;
        var field=document.getElementById(id);
        if(onoff=='on') {
            div.style.display='block';
            field.focus();
            field.select();
        } else {
            div.style.display='none';
        }
    },

    showOther: function(id) {
        var option=document.getElementById(id).value;
        if(option==='0') {
            this.toggleFieldDiv(id+'_other', 'on');
        } else {
            this.toggleFieldDiv(id+'_other', 'off');
        }
    },

    resetStyles: function() {
        // reset label styles
        var mandatoryFields=formValidate.mandatoryFields;
        for(i in mandatoryFields) {
            // find the class to get the color instead of hard coding it
            document.getElementById('label_'+i).style.color='#495358';
        }
    }

}