// put <script src="http://themingleportal.com/libs/valform.js?
// div,boxcolor,errcolor,imgcolor no #,imgboxstyle,t=c colourcap,o=t transparent"></scr ipt>
// anywhere in the form, or wherever you want captcha; 
// Use hexcolor for imgcolor but no '#'; for others #hex or color names are ok.
// if 4th(imgcolor) = '' don't use captcha;
// alt="?" T:text, N: Number, R: Radio Checkbox, E: Email, P:Phone
// erros in an alert box, or in div id if defined

scriptTags = document.getElementsByTagName("script");

for (i=0;i<scriptTags.length;i++){ 
	if (scriptTags[i].src.search('validateform.js') != -1) {
		var hc_params = scriptTags[i].src.split('?').pop().split(',');
		break;
	} 
}


var hc_div = hc_params[0];
var hc_color = hc_params[1];
var hc_errcolor = hc_params[2];
var hc_imgcolor = hc_params[3];
var hc_style = hc_params[4]; 
var hc_type = hc_params[5];
if (!hc_type) hc_type='x';

var hc_fimg = 'http://themingleportal.com/libs/fimage.php?';
hc_fimg += (hc_type == 'c' ? 't=c&' : '');
hc_fimg += (hc_params[6] == 't' ? 'o=t&' : '');
hc_fimg += (hc_imgcolor?'c='+hc_imgcolor+'&':'');
var hc_fields = '';
var hc_prad = '';
var hc_farad = {};
var hc_forms = document.forms[document.forms.length -1];
var myVar={};
var hc_sbm='';

// ==== BELOW FOR SECURITY QUESTION
var hc_a1, hc_a2, hc_a3,hc_aa;
var hc_fup = 8;
function hc_clc(){
	if (hc_type == 'c'){
		hc_aa = 10000 + Math.floor(Math.random()*90000);
	} else {
		hc_a1 = Math.floor(Math.random() * hc_fup) +1;
		hc_a2 = Math.floor(Math.random() * hc_fup) +1;
		hc_a3 = Math.floor(Math.random() * hc_fup) +1;
		hc_aa = (hc_a1 +'x' + hc_a2+ '%2b'+hc_a3);
	}
}

function hc_genimg(){
	hc_clc();
	hc_dwr = '<input type="text" name="hc_Result" size="4"' + (hc_style ? ' style="'+hc_style+'" ':'');
	hc_dwr += ' alt="S"  onfocus="this.value=\'\'" />';
	hc_dwr += '<img src="' + hc_fimg + 's=' + hc_aa +'" id="hc_IMG" align="top" />';
	document.write (hc_dwr);
}
if (hc_imgcolor) hc_genimg();

// ==== ABOVE FOR SECURITY QUESTION

function hc_errf(msg){
	hc_fields += '- ' + msg + '\n';
	myVar.style.background = hc_errcolor;
}
function trim(hc_str)
{
  return hc_str.replace(/^\s+|\s+$/g, '');
}

function validateForm(){
	hc_fields = '';
	hc_color = (hc_color?hc_color:'white');
	hc_errcolor=(hc_errcolor?hc_errcolor:'yellow');
	if (hc_forms) {
		for (i=0;i<hc_forms.length;i++){
	
			myVar = hc_forms[i]; 
			if (myVar.name.indexOf('[]') == -1)  myVar.value = trim(myVar.value); 
			switch (myVar.getAttribute('alt')){
				case 'T': 
					if (! myVar.value){
						hc_errf(myVar.name);
					} else {
						myVar.style.background = hc_color;
					}
					break;
				case 'N': 
					if (! myVar.value || isNaN(myVar.value)){
						hc_errf(myVar.name + ' is not a number');
					} else {
						myVar.style.background = hc_color;
					}
					break;
				case 'P': 
					phoneRe = /^(((\+?\d{1,3})(-| |\.)?)?)(\(?\d{3}\)?)(-| |\.)?(\d{3})(-| |\.)?(\d{4})(( *x| *ext) *\d{1,5}){0,1}$/;
					if (! myVar.value.match(phoneRe)){
						hc_errf(myVar.name + ' is incorrect');
					} else {
						myVar.style.background = hc_color;
					}
					break;
				case 'E':
					vpassed = myVar.value;
					if(vpassed.indexOf(".")==-1 || vpassed.indexOf("@")==-1){
						hc_errf(myVar.name + ' is not a valid email');
					} else {
						myVar.style.background = hc_color;
					}
					break;
				case 'R':
					if (hc_prad != myVar.name) {
						hc_prad = myVar.name;
						hc_farad[hc_prad] = false;
					}
					if (myVar.checked) {
						hc_farad[hc_prad] = true;
					}
					break;
				case 'S': 
					hc_r = (hc_type == 'c' ? hc_aa : (hc_a1*hc_a2+hc_a3));
					if (myVar.value != hc_r){
						hc_clc(); 
						document.images['hc_IMG'].setAttribute('src',hc_fimg + 's=' + hc_aa);
						hc_errf('Verification number'); 
					}
					break;
			}
		}
		for (k in hc_farad){
			if (!hc_farad[k]) {
				hc_fields += '- ' + k + ': choose one of the items\n';
			}
			hc_farad[k]=false;
		}
	
		if (! hc_fields) {
			return true; 
		} else {
			hc_fields = "PLEASE ENTER THE FOLLOWING INFORMATION:\n" + hc_fields;
			if (hc_div) {
				hc_fields = hc_fields.replace(/\n/g,'<br />');
				document.getElementById(hc_div).innerHTML=hc_fields;
			} else {
				alert(hc_fields); 
			}
			return false; 
		}
	} 
}

if (!hc_forms.getAttribute('name')) {
	hc_forms.setAttribute('name','h_hc' + Math.round(Math.random()*500)); hc_sbm = '';
}

if (! hc_forms.onsubmit && hc_forms.onsubmit != null) {
	hc_sbm = hc_forms.onsubmit;
}
hc_forms.onsubmit = function onsubmit(event) { return validateForm(); hc_sbm };
