	// version 1.0.2
	// built 11:41 AM 31/08/2011
	// by Jacob - DeviceMedia
	// 

	var fieldInfo = new Array();
	var allForms = new Array();
	function checkFinalFields(obj){
		


		var error = "";
		var firstfield;
		var previous_error = "\t";


		for(i=0; i < allForms[obj.sourceIndex].length; i++){
			inp = allForms[obj.sourceIndex][i];
			inp.blur();
		}
		



		for(i=0; i < allForms[obj.sourceIndex].length; i++){
			inp = allForms[obj.sourceIndex][i];
			if(inp.className.split("_")[0] == "required"){



			c = inp.className.split("_");
			

			if(c.length < 2 || c[2] != "success"){
				if(!firstfield){
					firstfield = inp;
				}
				if(typeof(fieldInfo[inp.name]) != "undefined" && typeof(fieldInfo[inp.name]['error']) != "undefined" && fieldInfo[inp.name]['error'] != "undefined"){
					if(previous_error.indexOf("\t"+fieldInfo[inp.name]['error']+"\t") == -1 ){
						error += " - "+fieldInfo[inp.name]['error']+"\n";
						previous_error += ""+fieldInfo[inp.name]['error']+"\t";
					}
				}


			}
			}


		}
		if(error != ""){
			alert("Fill in the missing fields.\n"+error);
			if(firstfield.type != 'hidden' && firstfield.disabled == false){
				firstfield.focus();
			}
			return false;
		}
	}



	var jsdatepickLoaded = false;

	function validateInputs(){

		inputs = document.getElementsByTagName("input");
		for(i=0; i < inputs.length; i++){
			replaceFields(inputs[i]);
		}

		myTags = new Array("input","textarea","select");



		for(azz=0; azz < myTags.length; azz++){

		inp  = document.getElementsByTagName(myTags[azz]);
		
		for(i=0; i < inp.length; i++){
			c = inp[i].className;
			c = c.split("_");
			type = c[1];
	


		

			if(inp[i].title){
				inp[i].title += "&";
				inf = inp[i].title.split("&");
				fieldInfo[inp[i].name] = new Array();

				for(infI=0; infI < inf.length; infI++){
					tmp = inf[infI].split('=');
					fieldInfo[inp[i].name][tmp[0]] = tmp[1];
				}

				if(fieldInfo[inp[i].name]['title']){
					inp[i].title = fieldInfo[inp[i].name]['title'];
				} else {
					inp[i].title = "";
				}
			}



			if(c[0] == "optional" || c[0] == "required"){

				//if(c[0] == "required" && inp[i].form){


	
					if(inp[i].form){

						if(!inp[i].form.onsubmit){
							inp[i].form.onsubmit = function(){ return checkFinalFields(this) };
							allForms[inp[i].form.sourceIndex] = new Array();
						}

						allForms[inp[i].form.sourceIndex].push(inp[i]);
					}


				addErrorEvents(inp[i]);

				//}





	/*

				if(inp[i].onfocus){
					inp[i].myonFocus = inp[i].onfocus;
				}

				inp[i].onfocus = function(){
					if(this.myonFocus){
						this.myonFocus();
					}


					c = this.className;
					c = c.split("_");
					type = c[1];


					this.className = ""+c[0]+"_"+type+"_active";

				} 
	


				if(inp[i].onblur){
					inp[i].myonblur = inp[i].onblur;
				}

				inp[i].onblur = isValidField;

*/


				if(inp[i].value){
					checkValidFields(inp[i]);
				}
				if(inp[i].type =="radio" && inp[i].checked && inp[i].onclick){
					inp[i].click();
				}
				if(inp[i].type =="checkbox" && inp[i].checked && inp[i].onclick){
					inp[i].checked = false;
					inp[i].click();
				}



			}


		}
	}
	}




	var isValidField = function(obj){
		checkValidFields(obj);
	}

	function checkValidFields(myObj)
				{

					if(!myObj){
						myObj = this;
					}


					c = myObj.className;
					c = c.split("_");
					type = c[1];
					obj = myObj;
					valid = true;

					if(type == "website"){

						if(obj.value.substring(0,7) != "http://" && obj.value.length > 0){
							obj.value = "http://"+obj.value;
						}
						if(obj.value == "http://"){
							obj.value = "";
						}
						if(!obj.value.match(/^(http\:\/\/([a-z0-9\_\-]+)\.(.*?))$/)){
							valid = false;
						}
					}

					if(type == "phone"){
						z = obj.value.toString().toLowerCase();
						z = z.replace(/[a-z]/g,"").toString();
						obj.value = z;

						z = obj.value; 
 
						z = z.split(" ").join("");
						z = z.split("-").join("");
						z = z.split(".").join("");
						z = z.split("(").join("");
						z = z.split(")").join("");

					

						if(z){
							obj.value = "("+z.substring(0,3)+") "+z.substring(3,6)+" "+z.substring(6)+"";
						}
						if(obj.value.length < 14){
							valid = false;
						}
					}
					if(type == "zipcode" || type == "postalcode"){

						z = obj.value;

						// fix all the typos with holding shift.

						z = z.split("!").join("1");
						z = z.split("@").join("2");
						z = z.split("#").join("3");
						z = z.split("$").join("4");
						z = z.split("%").join("5");
						z = z.split("^").join("6");
						z = z.split("&").join("7");
						z = z.split("*").join("8");
						z = z.split("(").join("9");
						z = z.split(")").join("0");


						american_postal = obj.value;
						if(american_postal.match(/[0-9]+/g) && american_postal.length == 5){

							american_postal = american_postal.replace(/[a-z]/g,"");
							american_postal = american_postal.toString();
							american_postal = american_postal.split(" ").join("");

						} else {
							american_postal = "";
						}

						z = z.split(" ").join("").toUpperCase();
						obj.value = z.substring(0,3)+" "+z.substring(3);
						if(american_postal.length == 5){
							obj.value = american_postal;
						}




						if(obj.value.match(/[0-9][0-9][0-9][0-9][0-9]/)){ 			// zipcode is american
						} else if(obj.value.match(/[A-Z][0-9][A-Z][\s][0-9][A-Z][0-9]/)){	// zipcode is canada
						} else { valid = false;	} 						// else is false.



					}
					
					if(type == "email"){
						if(!obj.value.match(/^([a-z0-9\.\_\=]+)\@([a-z0-9\-\.]+)\.([a-z][a-z]+)$/i) ){
							valid = false;
						}
					}
					if(type ==  "date"){
						if(!obj.value.match(/^([0-3][0-9][0-9][0-9]\-[0-1][0-9]\-[0-3][0-9])/i)){
							// max year can be: 3000, max month can be: 19, max day can be: 39
							valid = false;
						}
					
					}
					if(type == "radio"){
						valid = false;
						if(obj.form){
							for(a=0; a < obj.form[obj.name].length; a++){
								if(obj.form && obj.form[obj.name][a].checked){
									valid = true;
								}
							}
							if(valid){
								for(a=0; a < obj.form[obj.name].length; a++){
									obj.form[obj.name][a].className = ""+c[0]+"_"+type+"_success";
								}
							} else {
								for(a=0; a < obj.form[obj.name].length; a++){
									obj.form[obj.name][a].className = ""+c[0]+"_"+type+"_error";
								}
							}
						}
					}
					if(type == "checkbox"){
					

						if(obj.checked == false){
							valid = false;
						}
					}
					if(type == "dateselect"){
						if(obj.value == '00' || obj.value == '0000'){
							valid = false;
						}
					}
					if(type == "number"){
						if(!obj.value.match(/^([0-9]+)$/i)){
							valid = false;
						}
					}


					if((myObj.value == "" || valid == false) && c[0] == "required" ){
						myObj.className = "required_"+type+"_error";

						if(document.getElementById(myObj.name+"_error") != null){
							document.getElementById(myObj.name+"_error").style.display = "";
						}
					} else if(c[0] == "required") {
						myObj.className = "required_"+type+"_success";
						if(document.getElementById(myObj.name+"_error") != null){
							document.getElementById(myObj.name+"_error").style.display = "none";
						}

					} else if(myObj.value != "" && valid == false){
						myObj.className = ""+c[0]+"_"+type+"_error"; // is filled in, but not valid
					} else {
						myObj.className = ""+c[0]+"_"+type+"";
					}

					if(myObj.myonblur){
						myObj.myonblur();
					}

				}
			

	function addErrorEvents(inp){

				c = inp.className;
				c = c.split("_");
				type = c[1];



				if(inp.focus){
					inp.myonFocus = inp.focus;
				}

				inp.onfocus = function(){
					if(this.myonFocus){
						this.myonFocus();
					}


					c = this.className;
					c = c.split("_");
					type = c[1];


					this.className = ""+c[0]+"_"+type+"_active";

				} 

				if(inp.onblur){
					inp.myonblur = inp.onblur ;
				}

				inp.onblur = function(){
					if(this.myonblur){
						this.myonblur();
					}
					isValidField(this);
				}
				




	}

	function loadFiles(){

			scripts = document.getElementsByTagName('script');
			for(i=0; i < scripts.length; i++){
				if(scripts[i].src.indexOf('validateInputs.js') > 0){
					myPath = scripts[i].src.split('validateInputs.js')[0];
				}
				if(scripts[i].src.indexOf('jsDatePick.custom.js') > 0){
					jsdatepickLoaded = true;
				}
			}

			cssFile = document.createElement('link');
			cssFile.type = "text/css";
			cssFile.rel = "stylesheet";
			cssFile.href = myPath+"css/jsDatePick_ltr.css";


			cssFile2 = document.createElement('link');
			cssFile2.type = "text/css";
			cssFile2.rel = "stylesheet";
			cssFile2.href = myPath+"css/fields.css";


			sc = document.createElement('script');
			sc.src = myPath+"jsDatePick.custom.js";
			sc.async = true;
			var fire = false;
			sc.onreadystatechange = function(){
				if(fire == false){
					fire = true;
					setTimeout(validateInputs, 100);
				}
			}
			sc.onload = function(){
				if(fire == false){
					fire = true;
					setTimeout(validateInputs, 100);
				}
			}

			
			myHead = document.getElementsByTagName('head')[0];
			myHead.appendChild(sc);
			myHead.appendChild(cssFile);
			myHead.appendChild(cssFile2);




	}

	function replaceFields(inp){

				c = inp.className;
				c = c.split("_");
				type = c[1];

				

				if(type == "province" && inp.tagName == "INPUT"){
					sel = document.createElement("select");
					sel.title = inp.title;
					sel.name = inp.name+'_selectbox';
	
					sel.className = inp.className;


		optn = document.createElement("OPTION");
		optn.value = "";		optn.text = "";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "AB";		optn.text = "Alberta";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);


		optn = document.createElement("OPTION");
		optn.value = "BC";		optn.text = "British Columbia";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "MB";		optn.text = "Manitoba";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "NB";		optn.text = "New Brunswick";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);


		optn = document.createElement("OPTION");
		optn.value = "NL";		optn.text = "Newfoundland and Labrador";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);


		optn = document.createElement("OPTION");
		optn.value = "NS";		optn.text = "Nova Scotia";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "NT";		optn.text = "Northwest Territories";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "NU";		optn.text = "Nunavut";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "ON";		optn.text = "Ontario";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "PE";		optn.text = "Prince Edward Island";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "QC";		optn.text = "Quebec";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "SK";		optn.text = "Saskatchewan";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);

		optn = document.createElement("OPTION");
		optn.value = "YT";		optn.text = "Yukon";	
		if(inp.value == optn.value){ optn.selected = true; } 
		sel.options.add(optn);
		
		us_states = new Array(
							  new Array("AL","Alabama"),
							  new Array("AK","Alaska"),
							  new Array("AZ","Arizona"),
							  new Array("AR","Arkansas"),
							  new Array("CA","California"),
							  new Array("CO","Colorado"),
							  new Array("CT","Connecticut"),
							  new Array("DE","Delaware"),
							  new Array("DC","District of Columbia"),
							  new Array("FL","Florida"),
							  new Array("GA","Georgia"),
							  new Array("HI","Hawaii"),
							  new Array("ID","Idaho"),
							  new Array("IL","Illinois"),
							  new Array("IN","Indiana"),
							  new Array("IA","Iowa"),
							  new Array("KS","Kansas"),
							  new Array("KY","Kentucky"),
							  new Array("LA","Louisiana"),
							  new Array("ME","Maine"),
							  new Array("MD","Maryland"),
							  new Array("MA","Massachusetts"),
							  new Array("MI","Michigan"),
							  new Array("MN","Minnesota"),
							  new Array("MS","Mississippi"),
							  new Array("MO","Missouri"),
							  new Array("MT","Montana"),
							  new Array("NE","Nebraska"),
							  new Array("NV","Nevada"),
							  new Array("NH","New Hampshire"),
							  new Array("NJ","New Jersey"),
							  new Array("NM","New Mexico"),
							  new Array("NY","New York"),
							  new Array("NC","North Carolina"),
							  new Array("ND","North Dakota"),
							  new Array("OH","Ohio"),
							  new Array("OR","Oregon"),
							  new Array("SC","South Carolina"),
							  new Array("RI","Rhode Island"),
							  new Array("SD","South Dakota"),
							  new Array("TN","Tennessee"),
							  new Array("TX","Texas"),
							  new Array("UT","Utah"),
							  new Array("VT","Vermont"),
							  new Array("VA","Virginia"),
							  new Array("WA","Washington"),
							  new Array("WV","West Virginia"),
							  new Array("WI","Wisconsin"),
							  new Array("WY","Wyoming")
							 );

					for (var n = 0; n < us_states.length; n++) {
						optn = document.createElement("OPTION");
						optn.value = us_states[n][0];		optn.text = us_states[n][1];	
						if(inp.value == optn.value){ optn.selected = true; } 
						sel.options.add(optn);
					}


					inp.parentNode.appendChild(sel);
					inp.parentNode.removeChild(inp);
					inp = sel;

				
					
				}
				if(type == "dateselect"){

					d = inp.value.split('-');
					dayval = d[2];
					monthval = d[1];
					yearval = d[0];


					name2 = inp.name;
					name3 = "";
					if(inp.name.indexOf('[') > 0){
						name2 = inp.name.substring(0, inp.name.indexOf('['));
						name3 = inp.name.substring(inp.name.indexOf('['));
					}


					months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
					sel1 = document.createElement("select");
					sel1.name = name2+'_month'+name3;
					sel1.className = inp.className;
					sel1.title = inp.title;
					sel1.myTarget = inp;

					optn = document.createElement("OPTION");
					optn.value = '00';
					optn.text = ' ';
					sel1.options.add(optn);

					for(i=0; i < months.length; i++){
						a = i+1;
						if(a < 10){
							a = "0"+a;
						}

						optn = document.createElement("OPTION");
						optn.value = a;
						if(a == monthval){ optn.selected = true; }
						optn.text = months[i];
						sel1.options.add(optn);
					}

					sel2 = document.createElement("select");
					sel2.name = name2+'_day'+name3;
					sel2.className = inp.className;
					sel2.title = inp.title;
					sel2.myTarget = inp;

					optn = document.createElement("OPTION");
					optn.value = '00';
					optn.text = ' ';
					sel2.options.add(optn);


					for(i=1; i <= 31; i++){
						a = i;
						if(a < 10){
							a = "0"+a;
						}

						optn = document.createElement("OPTION");
						optn.value = a;
						optn.text = i;
						if(a == dayval){ optn.selected = true; }
						sel2.options.add(optn);
					}



					sel3 = document.createElement("select");
					sel3.name = name2+'_year'+name3;
					sel3.className = inp.className;
					sel3.title = inp.title;

					year = new Date().getFullYear() + 4;

					optn = document.createElement("OPTION");
					optn.value = '0000';
					optn.text = ' ';
					sel3.options.add(optn);
					sel3.myTarget = inp;

					for(i=year; i >= 1920; i--){
						a = i;


						optn = document.createElement("OPTION");
						optn.value = a;
						if(a == yearval){ optn.selected = true; }
						optn.text = i;
						sel3.options.add(optn);
					}


					inp.parentNode.appendChild(sel1);
					inp.parentNode.appendChild(sel2);
					inp.parentNode.appendChild(sel3);

					sel3.onchange = function(){
						x = this.myTarget.value.split('-');
						if(!x[1]){
							x[1] = "00";
						}
						if(!x[2]){
							x[2] = "00";
						}
						this.myTarget.value = this.value+"-"+x[1]+"-"+x[2];
					}

					sel1.onchange = function(){
						x = this.myTarget.value.split('-');
						if(!x[0]){
							x[0] = "0000";
						}
						if(!x[2]){
							x[2] = "00";
						}
						this.myTarget.value = x[0]+"-"+this.value+"-"+x[2];
					}



					sel2.onchange = function(){
						x = this.myTarget.value.split('-');
						if(!x[0]){
							x[0] = "0000";
						}
						if(!x[1]){
							x[1] = "00";
						}
						this.myTarget.value = x[0]+"-"+x[1]+"-"+this.value;
					}



					inp.className = "";
					inp.title = "";
					inp.style.display = "none";


				}


				if(type == "date"){
					myDateFormat = inp.dateFormat ? inp.dateFormat : "%Y-%m-%d";

					new JsDatePick({
						useMode:2,
						target: inp,
						dateFormat: myDateFormat

						/*selectedDate:{				This is an example of what the full configuration offers.
						day:5,						For full documentation about these settings please see the full version of the code.
						month:9,
						year:2006
					},
					yearsRange:[1978,2020],
					limitToToday:false,
					cellColorScheme:"beige",
					dateFormat:"%m-%d-%Y",
					imgPath:"img/",
					weekStartDay:1*/
				});

				}

	}

	window.onload = loadFiles;
