/* 
//Clean Form Validation was written from scratch by Marc Grabanski
// http://marcgrabanski.com/code/clean-form-validation
/* Under the Creative Commons Licence http://creativecommons.org/licenses/by/3.0/
	Share or Remix it but please Attribute the authors. */
//modified by d.b.

var cleanValidator = {
	init: function (settings) {
		this.settings = settings;
		this.form  = document.getElementById(this.settings["formId"]);
		formInputs = this.form.getElementsByTagName("input");
		
		// change color of inputs on focus
		for(i=0;i<formInputs.length;i++)
		{
			if(formInputs[i].getAttribute("type") != "submit") {
				input = formInputs[i];
				input.style.background = settings["inputColors"][0];
				input.onblur = function () {
					this.style.background = settings["inputColors"][0];
				}
				input.onfocus = function () {
					this.style.background = settings["inputColors"][1];
				}
			}
		};
	},
	validate: function () {
		error = '';
		var required    = this.settings["isRequired"];
		validationTypes = new Array("isRequired", "isEmail", "isNumeric", "isAgree");

		for(n=0; n<validationTypes.length; n++) {
			var x = this.settings[validationTypes[n]];
			if(x != null) {
				for(i=0; i<x.length; i++) 
				{
					inputField = document.getElementById(x[i]);
					switch (validationTypes[n]) {
						case "isRequired" :
						valid = !isRequired(inputField.value);
						errorMsg = ":: is a required field.";
						break;
						case "isEmail" :
						valid = isEmail(inputField.value);
						errorMsg = ":: is an invalid email address.";
						break;
						case "isNumeric" :
						valid = isNumeric(inputField.value);
						errorMsg = ":: can only be a number.";
						break;
						//d.b.
						case "isAgree" :
						valid = isAgree(inputField.value);
						errorMsg = ":: Please type 'agree' to indicate acceptance to Terms of Service.";
						break;
					}
					
					if(required != null && !valid &&  required.indexOf(x[i]) > -1) {
						error += x[i]+" "+errorMsg+"\n";
						inputField.style.background = this.settings["errorColors"][0];
						inputField.style.border = "1px solid "+this.settings["errorColors"][1];
					} else {
						inputField.style.background = this.settings["inputColors"][0];
						inputField.style.border = '1px solid';
					}
				}
			}
		}
		return error;
	},
	printError: function (error) {
		alert(error);
	}
};

// make sure indexOf works in IE
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}


// returns true if the string is not empty
function isRequired(str){
	return (str == null) || (str.length == 0);
}
// returns true if the 'agree' is typed in d.b.
function isAgree(str){
	if(isRequired(str)) return false;
	var re = /agree/;
	return re.test(str);
}

// returns true if the string is a valid email
function isEmail(str){
	if(isRequired(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

// returns true if the string only contains characters 0-9 and is not null
function isNumeric(str){
	if(isRequired(str)) return false;
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}

/// conteio specific stuff //////

  var widget    = 'charcoal';
  var divId     = 'widget-script';
  var infoLog   = 'http://www.conteio.com/scripts/empty.js?' 
  var widgetSrc = 'http://www.conteio.com/scripts/5w-'
  var agreed    = false;

  function chooseWidget(radio){
	widget = radio.id;
	// show the widget iff the user has agreed to the terms
	showWidgetText();
  }

  function getWidget(form){
	if(!agreed){
	   var error = cleanValidator.validate();
	   agreed = error.length < 1;
	   if(!agreed){
		cleanValidator.printError(error);
	   }else{
		var k = 0, src = infoLog;
		for(; k<form.elements.length; k++){
		  var name = form.elements[k].name, value = form.elements[k].value;
		  if(name != null && name.length > 0 && value.length > 0)
		  src += (name + '=' + value + '&');
		}
                src += ('widget=' + widget); 
		addScriptTo("head", src);
	   }
	}

	// show the widget iff the user has agreed to the terms
	showWidgetText();
	return false;
  }

  function showWidgetText(){
	if(agreed){
		document.getElementById(divId).innerHTML     =
		'&lt;!-- Copy the following text into your page to include the C3 Widget --&gt;<BR>' +
		'&lt;script type="text/javascript"  src="'+ widgetSrc + widget + '.js">&lt;/script>';
		document.getElementById(divId).style.display = 'block';
	}else{
		document.getElementById(divId).style.display = 'none';
	}
  }

function addScriptTo(elemName, scriptSrc)
{
   var elems = document.getElementsByTagName(elemName);
   if(elems){
      var script = document.createElement("script");
      script.setAttribute("type","text/javascript");
      script.setAttribute("src" , scriptSrc);
      elems.item(0).appendChild(script);
  }
}
