var highlightedField='';
function highlightField(f) {
	Ext.get(f).applyStyles('border: 2px solid #263692');
	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="/img/loading.gif" width="250" height="5">');	
}

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].value;
		params[elName] = elVal;
	}
	return params;
}

function reactivateForm(resp) {
	Ext.get('btnFormSubmit').dom.disabled=false;
	Ext.get('form-response').update(resp);		
}

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) {
				window.location.href='/donate/thank-you/';
			} 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 formDonationSubmit(fm) {
	deactivateForm();
	clearHighlights();
	
	fm.elements['donation-amount'].value = clearDollarSign(fm.elements['donation-amount'].value);
	fm.elements['cc-num'].value = digitsOnly(fm.elements['cc-num'].value);

	if (!checkBlank(fm.elements['donation-amount'].value)) {
		highlightField('donation-amount');
		alert("Please enter the amount you wish to donate");
		reactivateForm('');
		return;
	}
	if (!checkDollar(fm.elements['donation-amount'].value)) {
		highlightField('donation-amount');
		alert("Please enter the amount you wish to donate");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['cc-fname'].value)) {
		highlightField('cc-fname');
		alert("Please enter the first name on your credit card");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['cc-lname'].value)) {
		highlightField('cc-lname');
		alert("Please enter the last name on your credit card");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['cc-address'].value)) {
		highlightField('cc-address');
		alert("Please enter the billing address for your credit card");
		reactivateForm('');
		return;
	}
	if(!checkBlank(fm.elements['cc-city'].value)) {
		highlightField('cc-city');
		alert("Please enter the billing city for your credit card");
		reactivateForm('');
		return;
	}
	if(!checkBlank(fm.elements['cc-state'].value)) {
		highlightField('cc-state');
		alert("Please enter the billing state for your credit card");
		reactivateForm('');
		return;
	}
	if(!checkBlank(fm.elements['cc-zip'].value)) {
		highlightField('cc-zip');
		alert("Please enter the billing zip code for your credit card");
		reactivateForm('');
		return;
	}
	if(!checkEmail(fm.elements['contact-email'].value)) {
		highlightField('contact-email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return;
	}
	if(!checkCreditCard(fm.elements['cc-num'].value)) {
		highlightField('cc-num');
		alert("Please enter a valid credit card number");
		reactivateForm('');
		return;
	}
	if(!checkBlank(fm.elements['cc-code'].value) || fm.elements['cc-code'].value.length<3) {
		highlightField('cc-code');
		alert("Please enter a valid card code for your credit card (3 or 4 digits)");
		reactivateForm('');
		return;
	}
	
	// get params
	var params = getFormData('formDonation');
	params['what'] = 'form-donation';
	
	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('formDonation').update(FormDonationReplyAfterSubmit);
				window.scrollTo(0,0);
			} else {
				reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your donation.<br />This is the message we received from your card processor:<br /><b>'+respArr.reason+'</b><br />Please try again.');
			}
		},
		failure: function(resp,opt) {
			respArr = Ext.util.JSON.decode(resp.responseText);
			reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your donation.<br />This is the message we received from your card processor:<br /><b>'+respArr.reason+'</b><br />Please try again.');
		}
	});
}
