// PRIMARY FUNCTIONS
///////////////////////////////////////////////////////////

var xmlHttp;

function git(i) {
	return document.getElementById(i);
}

function getAjaxObj() {
	var xmlHttp = null;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

// GLOBAL VARIABLES
///////////////////////////////////////////////////////////

var idStr = '';
var errStr = '';
var errArr = new Array();
	errArr[0] = 'Please complete all required [highlighted] fields.';
	errArr[1] = 'The email you entered does not appear to be valid.';
	errArr[2] = 'The email you entered does not appear to be valid.';
	errArr[3] = 'The zip code you entered is not valid.';
	errArr[4] = 'The state you entered is not in an acceptable format.';
	errArr[5] = 'The name you entered is invalid, please do not use any special characters.';
	errArr[6] = 'The date field is not in the proper format';
var destination = '/wp-content/themes/erikaworks/send.php';

// STANDARD CLASS
///////////////////////////////////////////////////////////

function apnd() {
	if (idStr != '') { idStr += '|'; }
	if (errStr != '' && errStr.substr(errStr.length - 1) != '|') { errStr += '|'; }
}

function markTrbl() {
	clrTrbl();
	if (idStr != '') {
		var ta = idStr.split('|');
		for (i = 0; i < ta.length; i++) {
			var x = git(ta[i]).className;
			if (x != '')
				x += ' ';
			x += 'flagged';
			git(ta[i]).className = x;
		}
		idStr = '';
		announce();
		errStr = '';
	}
}

function announce() {
	msg ='';
	x = errStr.split('|');
	for (i = 0; i < x.length; i++) {
		if (msg != '')
			msg += '\n';
		if (x[i] != '' && x[i] != null)
			msg += errArr[x[i]];
	}
	alert(msg);
}

function clrTrbl() {
	var y = Array('input','select','textarea');
	for (i = 0; i < y.length; i++) {
		var x = document.getElementsByTagName(y[i]);
		for (a = 0; a < x.length; a++) {
			var n = x[a].className;
			n = n.replace(/ flagged/g, '');
			n = n.replace(/flagged/g, '');
			x[a].className = n;
		}
	}
}

function val(x) {
	var flag = false;
	for (i = 0; i < arguments.length; i++) {
		if (!arguments[i]) { flag = true; }
	}
	if (flag) {
		markTrbl();
		return false;
	} else {
		clrTrbl();
		return true;
	}
}

function getForm(c) {
	if (c == undefined) {
		z = document;
	} else {
		z = git(c);
	}
	var y;
	var q = '';
	var cont;
	var z = git(c);
	var x = Array('input','select','textarea');
	for (a = 0; a < x.length; a++) {
		y = z.getElementsByTagName(x[a]);
		for (i = 0; i < y.length; i++) {
			var field = new getFieldObj(y[i]);
			validate(y[i]);
			if (field.name != undefined && field.name != '') {
				cont = true;
				if (field.isRadio() || field.isBox()) {
					if (!field.isChecked()) { cont = false; }
				}
				if (cont) {
					if (q != '') { q += '&'; }
					q += field.name + '=' + noAmp(field.value);
				}
			}
		}
	}
	if (errStr != '') {
		//a problem was detected so we will mark up the form fields and alert the user.
		markTrbl();
	} else {
		//good to go - process and send to destination file
		objAjax = new getAjaxObj();
		objAjax.onreadystatechange = function() {
			if (objAjax.readyState == 4) {
				z.innerHTML = objAjax.responseText;
			}
		}
		objAjax.open('POST', destination, true);
		objAjax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		objAjax.setRequestHeader('Content-length', q.length);
		objAjax.setRequestHeader('Connection', 'close');
		objAjax.send(q);
	}
}

function ckEnter(e) {
	if (e && e.which) {
		e = e;
		cCode = e.which;
	} else {
		e = event;
		cCode = e.keyCode;
	}
	if (cCode == 13) {
		return true;
	} else {
		return false;
	}
}

function noAmp(i) {
	return i.replace(/&/g, '^^');
}

function getFieldObj(x) {
	this.hasClass = function(c) {
		var cArr = x.className.split(' ');
		for (a = 0; a < cArr.length; a++) {
			if (cArr[a] == c)
				return true;
		}
	}
	
	this.isRadio = function() {
		return (x.type == 'radio') ? true : false;
	}
	
	this.isBox = function() {
		return (x.type == 'checkbox') ? true : false;
	}
	
	this.isChecked = function() {
		return (x.checked) ? true : false;
	}
	
	this.id = x.id;
	this.value = x.value;
	this.name = x.name
}

function validate(x) {
	var field = new getFieldObj(x);
	
	//Check for required field
	if (field.hasClass('req')) {
		if (field.value == '') {
			apnd();
			idStr += field.id;
			if (errStr.indexOf(0) == -1)
				errStr += 0;
		}
	}
	//Regex for Email
	if (field.hasClass('vemail')) {
		var Regex =/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!Regex.test(field.value)) {
			apnd();
			idStr += field.id;
			if (errStr.indexOf(1) == -1)
				errStr += 1;
		}
	}
	//Regex for Phone
	if (field.hasClass('vphone')) {
		var Regex = /^\(?[2-9]\d{2}[ \-\)] ?\d{3}[\- ]?\d{4}$/;
		var Regex2 = /^[2-9]\d{9}$/;
		if ((!Regex.test(field.value))&&(!Regex2.text($val))) {
			apnd();
			idStr += field.id;
			if (errStr.indexOf(2) == -1)
				errStr += 2;
		}
	}
	//Check for U.S. 5 digit zip code
	if (field.hasClass('vzip')) {
		var Regex = /^\d{5}$/;
		if (!Regex.test(field.value)) {
			apnd();
			idStr += field.id;
			if (errStr.indexOf(3) == -1)
				errStr += 3;
		}
	}
	//Check for state
	if (field.hasClass('vstate')) {
		var Regex = /^[a-zA-Z]{2}$/;
		if (!Regex.test(field.value)) {
			apnd();
			idStr += field.id;
			if (errStr.indexOf(4) == -1)
				errStr += 4;
		}
	}
	//Check for name	
	if (field.hasClass('vname')) {
		var Regex = /^[a-zA-Z\ ']*$/;
		if (!Regex.test(field.value)) {
			apnd();
			idStr += field.id;
			if (errStr.indexOf(5) == -1)
				errStr += 5;
		}
	}
	if (field.hasClass('vdate')) {
		var Regex = /^([\d]|1[0,1,2]|0[1-9])(\-|\/|\.)([0-9]|[0,1,2][0-9]|3[0,1])(\-|\/|\.)\d{4}$/;
		if (!Regex.test(field.value)) {
			apnd();
			idStr += field.id;
			if (errStr.indexOf(6) == -1)
				errStr += 6;
		}
	}

}