// used on member pixel CP form, memberPixelForm.cfm 

$(document).ready(function(){
	
	$('#clickTalkFormSubmit').click(function() {
		if(tmt.validator.validateForm('clicktalkform'))
			processLead('clicktalkform');
	});
	
	var contactFormName = 'contact';
	if($('#'+contactFormName).length > 0) {
		$('#informationFormSubmit').click(function() {
			clearErrorDisplay(contactFormName);
			if(tmt.validator.validateForm(contactFormName))
				processLead(contactFormName);
		});
		$('#pleasewait').hide();
		loadFieldOfStudy($('#passedInPcID').val());
	}
});


function clearErrorDisplay(formid) { 
	$('#' + formid + ' span[id^=error]').each(function(i,e){
		$(this).text('');
	});
} 


function loadFieldOfStudy(pcid) {
	var loadData = "method=getProgramCategories&returnformat=json";
	if(pcid != null) 
		loadData = loadData + "&pcid=" + pcid;
	
	$('#pcid').jselect({
		loadUrl: "/remote/ajaxFacadeGenericLeadDeliveryService.cfc",
		loadData: loadData,
		loadDataType: "json"
    });
}

function processLead(formid) { 
	var options = { 
	    url:       "/remote/ajaxFacadeGenericLeadDeliveryService.cfc?method=process&returnformat=json",         // override for form's 'action' attribute 
	    type:      "post",        // 'get' or 'post', override for form's 'method' attribute 
	    dataType:  "json",        // 'xml', 'script', or 'json' (expected server response type) 
	    //clearForm: true        // clear all form fields after successful submit 
	    //resetForm: true        // reset the form after successful submit 
	    beforeSubmit: processLeadPrep,
        success:       processLeadResponse  // post-submit callback 
	}; 
	$('#'+formid).ajaxSubmit(options); 
} 

function processLeadPrep(formData, jqForm, options) { 
    $('#' + jqForm.attr('id') + " #informationFormSubmit").hide();
    $('#' + jqForm.attr('id') + " #pleasewait").show();
    return true; 
} 

function processLeadResponse(data, status, xhr, jqform)  { 
	var message = '';
	var alertmessage = '';
	var formid = jqform.attr('id');
	
	$('.fielderror').each(function(i,e){
		$(this).text('');
	});
	
	if(data && !data.isOk) {
		for(var i=0; i<data.errors.length; i++) {
			if(data.errors[i].FIELDNAME != '') {
				if(formid == 'clicktalkform') {
					alertmessage = alertmessage + data.errors[i].MESSAGE + '\n';
				} else {
					$('#' + formid + ' #error' + data.errors[i].FIELDNAME.toLowerCase()).text(data.errors[i].MESSAGE);
				}
			}
		}
		$('#' + formid + "> input[name='leadid']").attr('value', data.leadid);
		$("#pleasewait").hide();
		$("#informationFormSubmit").show();
	} else if(data) {
		message = message + data.validMessage;
		//alert(data.leadid);
		jqform.attr('action', '/confirmation/');
		jqform.attr('method', 'post');
		
		$('#' + formid + "> input[name='culeadid']").attr('value', data.leadid);
		
		if(data.isOk) {
			$('#' + formid + "> input[name='isGoodLead']").attr('value', 1);
		}
		jqform.submit();
		//jqform.resetForm();
	}
	
	if(alertmessage.length != 0) {
		alert(alertmessage);
	}
	
} 

