function setup_form() {

	$("#popup_form").html("");
	$("#popup_form").append("<p style=\"height:40px;\">To purchase this item, you must be a member of the <strong>DCFC Union Local #1138</strong>, the official Death Cab for Cutie fan club. If you're already a member, just login below. If not, <a href=\"http://www.dcfcunion.com\">sign up now!</a></p><br/>");
	$("#popup_form").append("<div id=\"fanclub_form\" style=\"float:none;margin:0 auto;\"><label id=\"fanclub_form_login\">Username </label><input type=\"input\" id=\"fanclub_user_login_popup\"/><br/><label id=\"fanclub_form_password\">Password </label><input type=\"password\" id=\"fanclub_user_pass_popup\"/><br/><br/><input type=\"button\" id=\"fanclub_submit_popup\" value=\"Login\"/>&nbsp;<input type=\"button\" id=\"fanclub_cancel_popup\" value=\"Cancel\"/></div>");
	



	$("#fanclub_submit_popup").click(function() {
	
             validate_user_popup();
	})

	$("#fanclub_cancel_popup").click(function() {
            disablePopup();	
        })


}

	
function setup_fanclub_container() {

		$("#fanclub_form_container").append('<p id=\'fanclub_copy\'>To purchase this item, you must be a member of the <strong>DCFC Union Local #1138</strong>, the official Death Cab for Cutie fan club.  If you\'re already a member, just login below.  If not, <a href=\'http://www.dcfcunion.com\' target=\'_blank\'>sign up now!</a>'); 
		
		var fanclub_form = document.createElement("div");
                fanclub_form.setAttribute("id", "fanclub_form");
                $("#fanclub_form_container").append(fanclub_form);

		var fanclub_form_label = document.createElement("label");
		fanclub_form_label.setAttribute("id", "fanclub_form_login");
		fanclub_form_label.innerHTML = 'Username&nbsp;';
		$("#fanclub_form").append(fanclub_form_label);
		
		var fanclub_user_login = document.createElement("input");
	        fanclub_user_login.setAttribute("id", "fanclub_user_login");
	        fanclub_user_login.setAttribute("type", "text");
		$("#fanclub_form").append(fanclub_user_login);

		$("#fanclub_form").append('<br/>');

		var fanclub_form_label = document.createElement("label");
		fanclub_form_label.setAttribute("id", "fanclub_form_password");
		fanclub_form_label.innerHTML = 'Password&nbsp;';
                $("#fanclub_form").append(fanclub_form_label);


		var fanclub_user_pass = document.createElement("input");
		fanclub_user_pass.setAttribute("id", "fanclub_user_pass");
		fanclub_user_pass.setAttribute("type", "password");
		$("#fanclub_form").append(fanclub_user_pass);

		$("#fanclub_form").append('<br/>');
		

		var fanclub_submit = document.createElement("input");
	        fanclub_submit.setAttribute("id", "fanclub_submit");
		fanclub_submit.setAttribute("type", "button");
		fanclub_submit.setAttribute("value", "Login");
		//fanclub_submit.setAttribute("onclick", "process_user()");
		$("#fanclub_form").append(fanclub_submit);

		$("#fanclub_submit").click(function() {
			validate_user();
		})




}


function validate_user() {

	//validate the user here
	if($("#fanclub_user_login").val() == '') { alert ('Please enter your username.'); return; }
	if($("#fanclub_user_pass").val() == '') { alert ('Please enter your password.'); return; }
	
	pass_hash = hex_md5($("#fanclub_user_pass").val());
	fanclub_user_hash = hex_md5($("#fanclub_user_login").val() + pass_hash);
	url = 'http://www.dcfcunion.com/verify.php?user=' + $("#fanclub_user_login").val() + '&md5=' + pass_hash;


	$('<img src=\'/images/dcfc_ajax_load.gif\' id=\'load_image\' />').insertAfter('#fanclub_form');
	$("#fanclub_submit").attr("disabled", "true"); 
	
	validate_from_popup = 0;

	var headElement = document.getElementsByTagName("head").item(0);
    var scriptElement = document.createElement("script");
    scriptElement.setAttribute("type", "text/javascript");
	scriptElement.setAttribute("charset", "utf-8");
    scriptElement.setAttribute("src", url);
    headElement.appendChild(scriptElement);

}

function validate_user_popup() {

	//validate the user here
	if($("#fanclub_user_login_popup").val() == '') { alert ('Please enter your username.'); return; }
	if($("#fanclub_user_pass_popup").val() == '') { alert ('Please enter your password.'); return; }

        pass_hash = hex_md5($("#fanclub_user_pass_popup").val());
        fanclub_user_hash = hex_md5($("#fanclub_user_login_popup").val() + pass_hash);
	url = 'http://www.dcfcunion.com/verify.php?user=' + $("#fanclub_user_login_popup").val() + '&md5=' + pass_hash;



	$("#fanclub_submit_popup").attr("disabled", "true");
	$("#fanclub_cancel_popup").attr("disabled", "true");
	$("#popup_form").html(" ");
	
	$('#popup_form').append('<p style=\"font-size:16px;margin-top:50px;\">Validating User Information...</p>');
    $('#popup_form').append('<img src=\'/images/dcfc_ajax_load_white.gif\' id=\'load_image_white\' />');

	validate_from_popup = 1;

	var headElement = document.getElementsByTagName("head").item(0);
    var scriptElement = document.createElement("script");
	scriptElement.setAttribute("type", "text/javascript");
    scriptElement.setAttribute("charset", "utf-8");
	scriptElement.setAttribute("src", url);
    headElement.appendChild(scriptElement);

}



//loading popup with jQuery magic!
function loadPopup(){


	//loads popup only if it is disabled
	if(popupStatus==0){


		$("#popup_background").css({
		"opacity": "0.7"
	});

	$("#popup_background").fadeIn("fast");
	$("#popup_form").fadeIn("fast");
	popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#popup_background").fadeOut("slow");
		$("#popup_form").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup_form").height();
	var popupWidth = $("#popup_form").width();
	
	//centering
	$("#popup_form").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$("#popup_background").css({
		"height": windowHeight
	});

}



function replace_links() {
	
	 $('a.fanclub').each(function(i){

		
		this.href = currentFanclubItems[this.id];
		$(this).removeAttr('id');
		
		$(this).unbind("click");

	})

    //set cookie so we don't have to do this again
	$.cookie('fanclub_member', 'valid', { path: '/' });


}


	 
function purchase_fanclub_item(asin) {

	currentAsin = asin;

	setup_form();
	centerPopup();  
 	loadPopup(); 

}


// Returns the basename of a path, then strip the suffix to get the filename
function get_page_filename(path) {

	return path.replace( /.*\//, "" ).split('.')[0];
}
	




function process_result(result) {


	if(fanclub_user_hash == result) {

		//valid user

		//set cookie so we don't have to do this again
		if(validate_from_popup == 1) {
			$.cookie('fanclub_member', 'valid', { path: '/' }); 
			window.location = currentFanclubItems[currentAsin];		
		} else {
		    $("#fanclub_form_container").html('');
	    	    $('.wba_add_to_cart_btn').show();
		    $('#ClothingSize').show();
	 	    replace_links();
		}

	} else {

		//invalid user
		if(validate_from_popup == 1) {
			$("#popup_form").html('');	

			
	   
			setup_form();
			$("#popup_form p").html('Sorry, that login information is not valid.  Please try again, or <a href=\'http://www.dcfcunion.com\'>join the fan club now.</a>');
		} else {
			$("#fanclub_form_container").html('');	
			setup_fanclub_container();	
		}
	}


}
