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 checkCCCode(v) {
	if(!checkBlank(v)) { return false; }
	if(!checkNumeric(v)) { return false; }
	if(v.length!=3 && v.length!=4) { return false; }
	return true;
}

function checkCCNum(v) {
	if(!checkBlank(v)) { return false; }
	if(!checkNumeric(v)) { return false; }
	if(v.length!=13 && v.length!=15 && v.length!=16) { return false; }
	return true;
}

function checkCurrency(v) {
	var filter = /^\d*(\.\d{1,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 checkPhone(v) {
	var filter = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; 
	return filter.test(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 reactivateForm(resp) {
	Ext.get('btnFormSubmit').dom.disabled=false;
	Ext.get('form-response').update('<strong>'+resp+'</strong>');		
}

function fmContactSubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return false;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['comments'].value)) {
		highlightField('comments');
		alert("Please let us know how we can help you");
		reactivateForm('');
		return false;
	}
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: {
			what: 'fmContact',
			name: fm.elements['name'].value,
			company: fm.elements['company'].value,
			position: fm.elements['position'].value,
			email: fm.elements['email'].value,
			phone: fm.elements['phone'].value,
			comments: fm.elements['comments'].value			
		},
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get('fmContact').update('<p><b>Thank you!</b><br />We have received your contact and our friendly staff will be in touch soon.</p>');
			} else {
				reactivateForm('<b>Sorry!</b> There was an error and we did not receive your contact. Please try again.');
			}
		return false;
		},
		failure: function(resp,opt) {
			reactivateForm('<b>Sorry!</b> There was an error and we did not receive your contact. Please try again.');
			return false;
		}
	});
	return false;

}

function fmPaymentSubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['amount'].value)) {
		highlightField('amount');
		alert("Please enter the amount you wish to submit");
		reactivateForm('');
		return false;
	}
	if (!checkCurrency(fm.elements['amount'].value)) {
		highlightField('amount');
		alert("Please enter a valid amount. Do not include a dollar sign or commas.");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['invoiceNumber'].value)) {
		highlightField('invoiceNumber');
		alert("Please enter your invoice number");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['phone'].value)) {
		highlightField('phone');
		alert("Please enter your phone number");
		reactivateForm('');
		return false;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return false;
	}
	if(!checkCCNum(fm.elements['ccNum'].value)) {
		highlightField('ccNum');
		alert("Please enter a valid credit card number. Enter only numbers, no spaces or dashes.");
		reactivateForm('');
		return false;
	}
	if(!checkCCCode(fm.elements['ccCode'].value)) {
		highlightField('ccCode');
		alert("Please enter a valid card code. This should be either 3 or 4 digits.");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['ccName'].value)) {
		highlightField('ccName');
		alert("Please enter the name on your credit card billing statement");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['ccAddress'].value)) {
		highlightField('ccAddress');
		alert("Please enter the address on your credit card billing statement");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['ccCity'].value)) {
		highlightField('ccCity');
		alert("Please enter the city on your credit card billing statement");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['ccState'].value)) {
		highlightField('ccState');
		alert("Please enter the state on your credit card billing statement");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['ccZip'].value)) {
		highlightField('ccZip');
		alert("Please enter the zip code on your credit card billing statement");
		reactivateForm('');
		return false;
	}

	// record params
	var params = new Array();
	params['what'] = fm.id;
	els = Ext.get(fm.id).dom.elements;
	for(i=0;i<els.length;i++) {
		if(els[i].type!='submit') {
			pName = els[i].id;
			params[pName] = els[i].value;
		}
	}
	
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: params,
		success: function(resp,opt) {
			if(resp.responseText==1) {
				var ccLastFour = fm.elements['ccNum'].value.substr(fm.elements['ccNum'].value.length-4);				
				Ext.get(fm.id).update('<p><b>Thank you!</b><br />We have received your payment. Your card ending in '+ccLastFour+' has been charged $'+fm.elements['amount'].value+'.</p>');
			} else {
				reactivateForm(resp.responseText);
			}
		return false;
		},
		failure: function(resp,opt) {
			reactivateForm('<b>Sorry!</b> There was an error and we did not receive your payment. Please try again.');
			return false;
		}
	});
	return false;

}


function fmNeedsAnalysisSubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return false;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['describeBusiness'].value)) {
		highlightField('describeBusiness');
		alert("Please describe your business for us");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['improveSystem'].value)) {
		highlightField('improveSystem');
		alert("Please tell us how you would like to improve your phone system");
		reactivateForm('');
		return false;
	}

	// record params
	var params = new Array();
	params['what'] = fm.id;
	els = Ext.get(fm.id).dom.elements;
	for(i=0;i<els.length;i++) {
		if(els[i].type!='submit') {
			pName = els[i].id;
			params[pName] = els[i].value;
		}
	}
	
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: params,
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get(fm.id).update('<p><b>Thank you!</b><br />We have received your request and our friendly staff will be in touch soon.</p>');
			} else {
				reactivateForm('<b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
			}
		return false;
		},
		failure: function(resp,opt) {
			reactivateForm('<b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
			return false;
		}
	});
	return false;

}

function fmQuoteRequestSubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return false;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['needQuoteFor'].value)) {
		highlightField('needQuoteFor');
		alert("Please let us know what you need a quote for");
		reactivateForm('');
		return false;
	}

	// record params
	var params = new Array();
	params['what'] = fm.id;
	els = Ext.get(fm.id).dom.elements;
	for(i=0;i<els.length;i++) {
		if(els[i].type!='submit') {
			pName = els[i].id;
			params[pName] = els[i].value;
		}
	}
	
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: params,
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get(fm.id).update('<p><b>Thank you!</b><br />We have received your request and our friendly staff will be in touch soon.</p>');
			} else {
				reactivateForm('<b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
			}
		return false;
		},
		failure: function(resp,opt) {
			reactivateForm('<b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
			return false;
		}
	});
	return false;

}

function fmRateSubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['serviceDate'].value)) {
		highlightField('serviceDate');
		alert("Please enter the date we met with you");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['company'].value)) {
		highlightField('company');
		alert("Please enter the name of your company");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['gradeContent'].value)) {
		highlightField('gradeContent');
		alert("Please grade us for content");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['gradeEntertainment'].value)) {
		highlightField('gradeEntertainment');
		alert("Please grade us for entertainment");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['gradeApplication'].value)) {
		highlightField('gradeApplication');
		alert("Please grade us for application");
		reactivateForm('');
		return false;
	}
	
	// record params
	var params = new Array();
	params['what'] = 'fmRate';
	els = Ext.get('fmRate').dom.elements;
	for(i=0;i<els.length;i++) {
		if(els[i].type!='submit') {
			pName = els[i].id;
			params[pName] = els[i].value;
		}
	}
	
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: params,
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get(fm.id).update('<p><b>Thank you!</b><br />We have received your feedback and will be reviewing it soon.</p>');
			} else {
				reactivateForm('<b>Sorry!</b> There was an error and we did not receive your feedback. Please try again.');
			}
		return false;
		},
		failure: function(resp,opt) {
			reactivateForm('<b>Sorry!</b> There was an error and we did not receive your feedback. Please try again.');
			return false;
		}
	});
	return false;

}

function fmRequestDemoSubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return false;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return false;
	}

	// record params
	var params = new Array();
	params['what'] = fm.id;
	els = Ext.get(fm.id).dom.elements;
	for(i=0;i<els.length;i++) {
		if(els[i].type!='submit') {
			pName = els[i].id;
			params[pName] = els[i].value;
		}
	}
	
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: params,
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get(fm.id).update('<p><b>Thank you!</b><br />We have received your request and our friendly staff will be in touch soon.</p>');
			} else {
				reactivateForm('<b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
			}
		return false;
		},
		failure: function(resp,opt) {
			reactivateForm('<b>Sorry!</b> There was an error and we did not receive your request. Please try again.');
			return false;
		}
	});
	return false;

}

function fmYourStorySubmit(fm) {
	deactivateForm();
	clearHighlights();
	if (!checkBlank(fm.elements['orgName'].value)) {
		highlightField('orgName');
		alert("Please enter the name of your organization");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['name'].value)) {
		highlightField('name');
		alert("Please enter your name");
		reactivateForm('');
		return false;
	}
	if(!checkEmail(fm.elements['email'].value)) {
		highlightField('email');
		alert("Please enter a valid email address");
		reactivateForm('');
		return false;
	}
	if (!checkBlank(fm.elements['story'].value)) {
		highlightField('story');
		alert("Please tell us your story");
		reactivateForm('');
		return false;
	}
	Ext.Ajax.request({
		url: 'ajax/forms.php',
		params: {
			what: 'fmYourStory',
			orgName: fm.elements['orgName'].value,
			orgAddress: fm.elements['orgAddress'].value,
			name: fm.elements['name'].value,
			email: fm.elements['email'].value,
			story: fm.elements['story'].value			
		},
		success: function(resp,opt) {
			if(resp.responseText==1) {				
				Ext.get('fmYourStory').update('<p><b>Thank you!</b><br />We have received your story.</p>');
			} else {
				reactivateForm('<b>Sorry!</b> There was an error and we did not receive your story. Please try again.');
			}
			return false;
		},
		failure: function(resp,opt) {
			reactivateForm('<b>Sorry!</b> There was an error and we did not receive your story. Please try again.');
			return false;
		}
	});
	return false;

}