var highlightedField='';
function highlightField(f) {
	/*
	Ext.get(f).applyStyles('border: 2px solid #b92525');
	Ext.get(f).dom.focus();
	highlightedField=f;
	*/
}

function clearHighlights() {
	if(highlightedField!='') {
		Ext.get(highlightedField).applyStyles('border: 0;');
	}
}

function checkBlank(v) {
	return v.length>0;
}

function checkCreditCard(v) {
	var visaFilter = /^4[0-9]{15}$/;
	var mcFilter = /^5[1-5]{1}[0-9]{14}$/;
	var amexFilter = /^3[47]{1}[0-9]{13}$/;
	var discoverFilter = /^6011[0-9]{12}$/;
	
	if(visaFilter.test(v)) { return true; }
	if(mcFilter.test(v)) { return true; }
	if(amexFilter.test(v)) { return true; }
	if(discoverFilter.test(v)) { return true; }

	return false;
}

function checkDollar(v) {
	var filter = /^((\d*)|(\d*\.\d{2}))$/;
	return filter.test(v);	
}

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 clearDollarSign(v) {
  objRegExp = /\)|\(|[,]/g;
  v = v.replace(objRegExp,'');
  if(v.indexOf('$') >= 0){
    v = v.substring(1, v.length);
  }
  return v;
}

function deactivateForm() {
	Ext.get('btnFormSubmit').dom.disabled=true;
	Ext.get('form-response').update('<img border="0" src="/images/loading.gif" width="113" height="7">');	
}

function digitsOnly(v) {
	return v.replace (/[^\d]/g,"");
}

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;
}

function reactivateForm(resp) {
	Ext.get('btnFormSubmit').dom.disabled=false;
	Ext.get('form-response').update(resp);		
}

/********** FORM SUBMISSIONS *****************/


function formC3Submit(fm) {
	deactivateForm();
	clearHighlights();


	if (!checkBlank(fm.elements['fname'].value)) {
		highlightField('fname');
		alert("Please enter your first name");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['lname'].value)) {
		highlightField('lname');
		alert("Please enter your last name");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['dob'].value)) {
		highlightField('dob');
		alert("Please enter your date of birth");
		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");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['phone'].value)) {
		highlightField('phone');
		alert("Please enter your phone");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['insuranceCarrier'].value)) {
		highlightField('insuranceCarrier');
		alert("Please enter your Insurance Carrier");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['employer'].value)) {
		highlightField('employer');
		alert("Please enter your employer");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['jobTitle'].value)) {
		highlightField('jobTitle');
		alert("Please enter your job title");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['dateInjury'].value)) {
		highlightField('dateInjury');
		alert("Please enter the date the injury occurred");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['whereInjury'].value)) {
		highlightField('whereInjury');
		alert("Please enter where the injury occurred");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['activityInjury'].value)) {
		highlightField('activityInjury');
		alert("Please enter what you were doing when you were injured");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['howInjury'].value)) {
		highlightField('howInjury');
		alert("Please enter how the injury happened");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['natureInjury'].value)) {
		highlightField('natureInjury');
		alert("Please enter the nature of your injury");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['preventWork'].value)) {
		highlightField('preventWork');
		alert("Please enter whether the injury prevented you from working");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['medicalTreatment'].value)) {
		highlightField('medicalTreatment');
		alert("Please enter whether you received medical treatment");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['treatmentDate'].value)) {
		highlightField('treatmentDate');
		alert("Please enter the date of your first treatment");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['treatmentLocationName'].value)) {
		highlightField('treatmentLocationName');
		alert("Please enter the name of the location you received treatment");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['treatmentAddress'].value)) {
		highlightField('treatmentAddress');
		alert("Please enter the address of the location you received treatment");
		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) {
			respArr = Ext.util.JSON.decode(resp.responseText);
			if(respArr.success) {				
				Ext.get(fm.id).update('<br /><p><b>Thank you!</b><br />We have received your contact and an attorney will be in touch within 24 hours.');
			} else {
				reactivateForm('<br /><b>Sorry!</b> There was an error and your contact was not received. Please try again.');
			}
		},
		failure: function(resp,opt) {
			reactivateForm('<br /><b>Sorry!</b> There was an error and your contact was not received. Please try again.');
		}
	});	
}

function formContactSubmit(fm) {
	deactivateForm();
	clearHighlights();
	
	if (!checkBlank(fm.elements['myname'].value)) {
		highlightField('myname');
		alert("Please enter your name");
		reactivateForm('');
		return;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['phone'].value)) {
		highlightField('phone');
		alert("Please enter your phone number");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['comments'].value)) {
		highlightField('comments');
		alert("Please let us know how we can help you");
		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) {
			respArr = Ext.util.JSON.decode(resp.responseText);
			if(respArr.success) {				
				Ext.get(fm.id).update('<br /><p><b>Thank you!</b><br />We have received your contact and an attorney will be in touch within 24 hours.');
			} else {
				reactivateForm('<br /><b>Sorry!</b> There was an error and your contact was not received. Please try again.');
			}
		},
		failure: function(resp,opt) {
			reactivateForm('<br /><b>Sorry!</b> There was an error and your contact was not received. Please try again.');
		}
	});
}

function clearEnewsResponse() {
	Ext.get('enews-form-response').update('');	
}

function formEnewsSubmit() {
	
	Ext.get('enews-form-response').update('<img border="0" src="/images/loading.gif">');		
	em = Ext.get('enewsEmail').getValue()
	if(!checkEmail(em)) {
		Ext.get('enews-form-response').update('Please enter a valid email address.');
		return;
	}
		
	Ext.Ajax.request({
		url: '/ajax/forms.php',
		params: {
			what: 'fmEnews',
			email: em
		},
		success: function(resp,opt) {
			respArr = Ext.util.JSON.decode(resp.responseText);
			if(respArr.success) {
				Ext.get('enews-form-response').update('<b>Thank you!</b> We have added <i>'+em+'</i> to our enewsletter list.');		
				Ext.get('enewsEmail').dom.value='';
			} else {
				Ext.get('enews-form-response').update('<b>Sorry!</b> There was an error and your email address was not added. Please try again.');		
			}
		},
		failure: function(resp,opt) {
			Ext.get('enews-form-response').update('<b>Sorry!</b> There was an error and your email address was not added. Please try again.');
		}
	});
}
