var highlightedField='';
function highlightField(f) {
	Ext.get(f).applyStyles('background-color: #EFB3B5');
	Ext.get(f).dom.focus();
	highlightedField=f;
}

function clearHighlights() {
	if(highlightedField!='') {
		Ext.get(highlightedField).applyStyles('background-color: #ffffff;');
	}
}

function checkBlank(v) {
	return v.length>0;
}

function checkEmail(v) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(v);
}

function checkNumeric(v) {
	var filter = /^\d+$/;
	return filter.test(v);
}

function deactivateForm() {
	Ext.get('btnFormSubmit').dom.disabled=true;
	Ext.get('form-response').update('<img border="0" src="/img/loading.gif" width="250" height="5">');	
}

function reactivateForm(resp) {
	Ext.get('btnFormSubmit').dom.disabled=false;
	Ext.get('form-response').update(resp);		
}

function getFormData(fm) {
	params = new Array();
	els = Ext.get(fm).dom.elements;
	for(i=0;i<els.length;i++) {
		elName = els[i].name;
		elVal = els[i].type=='checkbox' ? els[i].checked : els[i].value;
		params[elName] = elVal;
	}
	return params;
}


/**** FORM SUBMISSIONS ******/

function formCESubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (fm.elements['courseName'].value=='not selected') {
		highlightField('courseName');
		alert("Please choose your course.");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['insurance-license-number'].value)) {
		highlightField('insurance-license-number');
		alert("Please enter your insurance license number");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['ssn-last-four'].value)) {
		highlightField('ssn-last-four');
		alert("Please enter the last four digits of your social security number");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['company'].value)) {
		highlightField('company');
		alert("Please enter the name of your company");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['phone'].value)) {
		highlightField('phone');
		alert("Please enter your phone number");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['address'].value)) {
		highlightField('address');
		alert("Please enter your address");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['city'].value)) {
		highlightField('city');
		alert("Please enter your city");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['state'].value)) {
		highlightField('state');
		alert("Please enter your state");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['zip'].value)) {
		highlightField('zip');
		alert("Please enter your zip code");
		reactivateForm('');
		return;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return;
	}

	// get params
	var params = getFormData(fm.id);
	params['what'] = fm.id;

	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: params,
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get('formCE').update(FormCEReplyAfterSubmit);
			} else {
				reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your contact. Please try again.');
			}
		},
		failure: function(resp,opt) {
			reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your contact. Please try again.');
		}
	});
}

function formContactSubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['contact-name'].value)) {
		highlightField('contact-name');
		alert("Please enter your name");
		reactivateForm('');
		return;
	}
	if(!checkEmail(fm.elements['contact-email'].value)) {
		highlightField('contact-email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return;
	}
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: {
			what: 'form-contact',
			name: fm.elements['contact-name'].value,
			company: fm.elements['contact-company'].value,
			email: fm.elements['contact-email'].value,
			phone: fm.elements['contact-phone'].value,
			message: fm.elements['contact-message'].value			
		},
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get('formContact').update(FormContactReplyAfterSubmit);
			} else {
				reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your contact. Please try again.');
			}
		},
		failure: function(resp,opt) {
			reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your contact. Please try again.');
		}
	});
}

