	// basic
	var taf_imported_emails = [];
	var taf_import_emails = '';
	var taf_uploaded = false;
    
    var is_taf = true;

/********************* RESET/SETUP FUNCTIONS *********************/    
    
    function taf_reset_hack() {
        using_groups = false;
        if (is_taf) {
            taf_reset_tips();
        } else {
            taf_reset_email_this();
        }
    }

	// reset tell a friend thickbox back to normal.
	function taf_reset_tips() {
        is_taf = true;

        document.getElementById('taf_email_message').readOnly = false;
        document.getElementById('taf_email_message').value = "";
        $('#taf-default-desc').css('display', 'block');
        $('#taf_emails_for_import').css('display', 'none');
		$('#taf-sent').css('display', 'none');
		$('#taf-sent-using-groups').css('display', 'none');
        set_header_image(taf_header_image);
        taf_reset_all();
	}
    
    // reset tell a friend thickbox back to normal.
	function taf_reset_email_this() {
        is_taf = false;
        document.getElementById('taf_email_message').readOnly = false;
        document.getElementById('taf_email_message').value = "Shop Lucky's favorite shoes - and now bags! - from your iPhone or online with Lucky At Your Service, the first-ever digital shopping concierge. See something you like? Buy it immediately online or we can track it down at a store near you, check availability, and even put it on hold for same-day pickup!";
        $('#taf-default-desc').css('display', 'none');
        $('#taf_emails_for_import').css('display', 'none');
		$('#taf-sent').css('display', 'none');
		$('#taf-sent-using-groups').css('display', 'none');

        set_header_image(email_this_header_image);
        taf_reset_all();
	}
    
    function set_header_image(header_image) {
        //set the right header image for the type of request we're processing
        //this saves us from having 2x as much html for simply changing
        //a header image out.
        var current_element;
        for (var i = 1; i <= 9; i++) {
            current_element = 'taf_header_image_' + i;
            
            //we have to check for null since some blocks are displayed
            //only if the user is logged in..
            if (document.getElementById(current_element) != null) {
                document.getElementById(current_element).src = header_image;
            }
        }//end for loop
    }
    
    function taf_reset_all() {

        
        $('#taf-send-mail-form').css('display', 'block');
		$('#taf-sent').css('display', 'none');
		$('#taf-sent-using-groups').css('display', 'none');
		$('#taf-import-account-login').css('display', 'none');
		$('#taf-group-saved').css('display', 'none');
        $('#taf_upload_contacts').css('display', 'none');
		$('#taf-groups-holder').css('display', 'none');
        
		document.getElementById('taf_email_addresses').value = '';
		
		// Reset TAF height to default in case user cancelled out
		// of another screen.
		$(document).ready(function() {
					$('#tafDefaultBody').attr({style: "height: auto;"});
			});
    }

/**************************************************************/    
    
	function taf_send() {
			// Set TAF box height to auto
			$(document).ready(function() {
					$('#tafDefaultBody').attr({style: "height: auto;"});
					});
	
		// check email and name
		var users_email = document.getElementById('taf_your_email').value;
		var users_name = document.getElementById('taf_your_name').value;
		var taf_errors = '';
        
		if (!is_valid_email(users_email)) {
			taf_errors += 'You must provide your e-mail address.' + "\n";
		}

		if (jQuery.trim(users_name) == '') {
			taf_errors += 'You must provide your name.' + "\n";
		}
        
		if (taf_uploaded == false) {
			taf_import_emails = document.getElementById('taf_email_addresses').value;
			if (taf_import_emails == '') {
				taf_errors += 'Please provide your friends\' email addresses.';
			}
		}

		if (strlen(taf_errors) > 0) {
			alert(taf_errors);
		} else {

			
            $('#taf-send-mail-form').css('display', 'none');
            $('#taf-show-processing').css('display', 'block');

            var from_name = document.getElementById('taf_your_name').value;
            var from_email = document.getElementById('taf_your_email').value;
			var taf_send_to_emails = document.getElementById('taf_email_addresses').value;
            
            var type = "taf";
            if (is_taf == false) {
                type = "email_this"
            }

            var taf_get_string = js_base_url + "/index.php/luckyays/action/sendTAFEmail/type/" + type + "/emails/" + escape(taf_send_to_emails) + "/from_name/" + escape(from_name) + "/from_email/" + escape(from_email) + "/email_message/" + escape(document.getElementById('taf_email_message').value);
            if (type == "email_this") {
                taf_get_string = taf_get_string + "/item_id/" + shoe_id + "/";
            }

			$.get(taf_get_string, function(data) {
				data = jQuery.trim(data);
				if (data != "true") {
                    $('#taf-show-processing').css('display', 'none');
                    $('#taf-send-mail-form').css('display', 'block');
					alert(data);
                    
				} else {
                    
					if (using_groups) {
                        // show them the sent/thank you page
                        $('#taf-show-processing').css('display', 'none');
                        $('#taf-sent-using-groups').css('display', 'block');
					} else {
                        // show them the sent/thank you page
                        $('#taf-show-processing').css('display', 'none');
                        $('#taf-sent').css('display', 'block');
					}
                    
				}
			});
		}
	}
    
/**************************************************************/    

	function taf_toggle_upload() {
        
		if (taf_uploaded) {

			var taf_send_mail_form = 'block';
            var taf_import_account_login = 'none';
			taf_uploaded = false;
            
		} else {

			var taf_send_mail_form = 'none';
            var taf_import_account_login = 'block';
			taf_uploaded = true;
		}

		$('#taf-send-mail-form').css('display', taf_send_mail_form);
		$('#taf-import-account-login').css('display', taf_import_account_login);
	}

/**************************************************************/    
    
	function taf_do_import_contacts() {

		var taf_import_email = document.getElementById('taf_import_email').value;
		var taf_import_email_password = document.getElementById('taf_import_email_password').value;
		var taf_errors = new Array();
		if (is_valid_email(taf_import_email) && strlen(taf_import_email_password) > 0) {

            //show our processing image
            $('#taf-import-account-login').css('display', 'none');
            $('#taf-show-processing').css('display', 'block');
            
            var taf_get_string = js_base_url + "/index.php/luckyays/action/importEmails/email/" + taf_import_email + "/password/" + escape(taf_import_email_password);

			$.get(taf_get_string, function(data) {
				data = jQuery.trim(data);
				if (data == 'wrong_domain') {

					alert('We cannot import contacts from the domain on email ' + taf_import_email + '. Please try again.');
                    
                    //remove our processing animation
                    $('#taf-show-processing').css('display', 'none');
                    $('#taf-import-account-login').css('display', 'block');
                    
				} else if (data == 'wrong_login') {
                    
					alert('Invalid e-mail address or password.  Please try again.');
                    
                    //remove our processing animation
                    $('#taf-show-processing').css('display', 'none');
                    $('#taf-import-account-login').css('display', 'block');
                    
				} else {
                    
                    //remove our processing animation
                    $('#taf-show-processing').css('display', 'none');
                    
					//show the results
					document.getElementById('taf-show-importable-contacts').innerHTML = data;
					$('#taf_emails_for_import').css('display', 'block');
					
					// Reset TAF height to default in case user cancelled out
					// of another screen.
						$(document).ready(function() {
							$('#tafDefaultBody').attr({style: "height: 550px;"});
							});
				}
			});
		} else {
			alert('Invalid e-mail address or password.  Please try again.');
		}
	}
    
/**************************************************************/

	function taf_import_selected_contacts() {
		taf_pre_using_import = true;
		var taf_pre_local_emails = document.getElementsByName('import_emails');

		// loop over checked emails
		for (var i = 0; i < taf_pre_local_emails.length; i++) {
			if (taf_pre_local_emails[i].checked) {
				// we're pushing so we can continue to import...
				taf_imported_emails.push(taf_pre_local_emails[i].value);
			}
		}

		$('#taf_emails_for_import').css('display', 'none');
		$('#taf-send-mail-form').css('display', 'block');
        
		if (document.getElementById('taf_email_addresses').value != '') {
			document.getElementById('taf_email_addresses').value += ', ' + taf_imported_emails.join(', ');
		} else {
			document.getElementById('taf_email_addresses').value = taf_imported_emails.join(', ');
		}
		$('#taf_import_email_content').css('display', 'block');
		$('#taf_pre_primary_buttons').css('display', 'block');
		$(document).ready(function() {
					$('#tafDefaultBody').attr({style: "height: auto;"});
			});
	}


/**************************************************************/    

	function taf_save_group() {

        $('#taf-sent').css('display', 'none');
        $('#taf-show-processing').css('display', 'block');
		
        var group_name = document.getElementById('taf_group_name').value;
		var send_emails = document.getElementById('taf_email_addresses').value;
        var get_string = js_base_url + "/index.php/luckyays/action/saveGroup/group_name/" + escape(group_name) + "/emails/" + escape(send_emails);
		$.get(get_string, function(data) {
            data = jQuery.trim(data);
			if (data != 'false') {
                $('#taf-show-processing').css('display', 'none');
				$('#taf-group-saved').css('display', 'block');

				document.getElementById('taf_group_name').value = '';
            }
		});
	}

/**************************************************************/    

    function taf_cancel_import() {
        
		$('#taf_emails_for_import').css('display', 'none');
		$('#taf-send-mail-form').css('display', 'block');
    }

/**************************************************************/    

	function taf_show_groups() {
		$('#taf-groups-holder').css('display', 'block');
	}

	function taf_cancel_groups() {
		$('#taf-groups-holder').css('display', 'none');
	}

/**************************************************************/  

	var using_groups = false;
	function taf_apply_groups() {
		using_groups = true;
		var groups = document.getElementsByName('groups_checked');
		var local_emails = '';
		for (var i = 0; i < groups.length; i++) {
			if (groups[i].checked) {
				local_emails += groups[i].value;
			}
		}
        
        //if there's data in the box, put a ',' in between the email addresses
        if (document.getElementById('taf_email_addresses').value != '') {
            document.getElementById('taf_email_addresses').value += ', ' + local_emails;
        } else {
            document.getElementById('taf_email_addresses').value += local_emails;
        }
		$('#taf-groups-holder').css('display', 'none');
		taf_uncheck_groups();
	}


/****************************UTILITIES**********************************/    

	function is_valid_email(str) {
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	}
    
	function strlen(string){
		return ("" + string).length;
	}

	function taf_check_all() {
		var field = document.import_email_form.taf_import_this_contact;
		for (i = 0; i < field.length; i++) {
			field[i].checked = true ;
		}
	}

	function taf_uncheck_all() {
		var field = document.import_email_form.taf_import_this_contact;
		for (i = 0; i < field.length; i++) {
			field[i].checked = false ;
		}
	}

	function taf_uncheck_groups() {
		var field = document.taf_import_checkboxes.groups_checked;
		for (i = 0; i < field.length; i++) {
			field[i].checked = false ;
		}
	}

/**************************************************************/    

