$(document).ready(function(){
	$('#test_drive_dialog').dialog({
		draggable:false,
		resizable:false,
		bgiframe:true,
		modal:true,
		autoOpen:false,
		height:620,
		width:769,
		open:function(){
			init_test_drive_validate().resetForm();
		},
		close:function(){
			$('#test_drive_form')[0].reset();
		}
	});
	
	$('.test_drive').click(function() {
		$.ajax_fw(function(){
			return {
				run: 'send_message',
				params: {
					class_name: 'exclusion',
					function_name: 'get_test_drive_surveys',
					message:{ category_id: TESTDRIVE_CAT_ID },
					sendmessage_callback:function(ajax){
						var i;
						var s_html = '';
						if(ajax.get_status()===0){
							for(i=0;i<ajax.data.length;i++){
								s_html += '<option value="' + ajax.data[i]['event_type_id'] + '">' + ajax.data[i]['survey_name'] + '</option>';
							}
						}
						$('#survey_name').html(s_html);
						$('#test_drive_dialog').dialog('open');
					}
				}
			}
		});
	});

	$('#test_drive_cancel').click(function(){
		$('#test_drive_dialog').dialog('close');
	});
	
	$('#test_drive_send').click(function(){
		if (init_test_drive_validate().form()){
			$('#test_drive_form').ajax_fw(function(){
				return {
					run:'send_form',
					params:{
						class_name:'exclusion',
						function_name:'send_invite',
						sendform_callback: function(o_return){
							if(o_return.get_status()===0){
								if($('#test_drive_contact:checked').length > 0){
									$.post(	"main.php?c=exclusion&f=subscribe",
										{
											email: $('#test_drive_email').val(),
											first_name: $('#test_drive_firstname').val(),
											category_id: $('#test_drive_category').val(),
											double_optin: "false"
										}
									);
									var exdate=new Date();
									exdate.setDate(exdate.getDate() + 1825);
									document.cookie="hide_sub=true; expires="+exdate.toUTCString();
								}
								$('#test_drive_dialog').dialog('close');
                                clicky.goal("Test Drive");
								$.wmui_notify('Thanks, your<br />Test Drive invitation is on the way!.');
							}else if(o_return.get_status()===1){
								alert("We were unable to send the Test Drive to that account, please check the email address and try again.");
							}else{
								alert("An error has occured.");
							}
						}
					}
				};
			});
		}else{
			//alert('We have found some problems with what you have entered, please fix and try again.');
		}
	});
	$('#outer').show();
});
function init_test_drive_validate(){
	return $('#test_drive_form').validate({
		rules: {
			test_drive_firstname:'required',
			test_drive_lastname:'required',
			test_drive_email: {
				required: true,
				email: true
			}
		},
		messages: {
			test_drive_firstname:'required',
			test_drive_lastname:'required',
			test_drive_email: {
				required: 'required',
				email: 'invalid'
			}
		}
	});
}

