var highlightedField='';
function highlightField(f) {
	Ext.get(f).applyStyles('border: 2px solid #518195');
	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" style="width:33px; height:8px">');	
}

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);		
}

function formContactSubmit(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(!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['dateOfArrival'].value)) {
		highlightField('dateOfArrival');
		alert("Please enter your arrival date");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['dateOfDeparture'].value)) {
		highlightField('dateOfDeparture');
		alert("Please enter your departure date");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['numAdults'].value)) {
		highlightField('numAdults');
		alert("Please enter the number of adults");
		reactivateForm('');
		return;
	}
	if (!checkBlank(fm.elements['numChildren'].value)) {
		highlightField('numChildren');
		alert("Please enter the number of children");
		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 request and our team will contact you soon.');
				window.scrollTo(0,0);
			} else {
				reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
			}
		},
		failure: function(resp,opt) {
			reactivateForm('<br /><b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
		}
	});
}

