$(document).ready(function(){
	var scriptdate = new Date();
	var modaldate;
	
	window.onbeforeunload = function(){
		$.ajax({url: "/cgi-bin/logger.cgi", async: false, cache: false, processData: true, data: {"time": (new Date().getTime() - scriptdate.getTime())}});
	};

	$("#bidForm").submit(function(){
		var html = "";
		if((($.trim($("#bidForm input[name=bidder]").val()) != "") && ($.trim($("#bidForm input[name=password]").val()) != "")) || ($.cookie('purple-bid') != null) ){
			return true;
		}else{
			if($("#credentials-dialog").attr("id") == null){
				$("body").append("<div id=\"credentials-dialog\"></div>");
			}
			
			modaldate = new Date();
			html += "<p class=\"ui-state-error\"><span class=\"ui-icon ui-icon-alert\"></span>In order to place a bid you must log in to your account using your Account or Email and password.</p>";
			html += "<form id=\"credentialform\" action=\"/\" method=\"get\">";
			html += "<div>";
			html += ($.trim($("#bidForm input[name=bidder]").val()) == "" ? "<label class=\"block-label\" for=\"mccbnum\">Account or Email</label>" : "");
			html += "<label class=\"block-label\" for=\"mccpw\">Password</label>" + ($.trim($("#bidForm input[name=bidder]").val()) == "" ? "<br />" : "");
			html += ($.trim($("#bidForm input[name=bidder]").val()) == "" ? "<input id=\"mccbnum\" type=\"text\" name=\"bidder\" />" : "");
			html +=	"<input id=\"mccpw\" type=\"password\" name=\"password\" value=\"\" />";
			html += "</div>";
			html += "<div class=\"buttons-container\">";
			html += "<div>";
			html += "<input id=\"credentials-confirm\" type=\"button\" name=\"confirm\" value=\"Submit Bids\" />";
			html +=	"<input id=\"credentials-review\" type=\"button\" name=\"review\" value=\"Review Bids\" />";
			html += "</div>";
			html += "</div>";
			html += "</form>";
			$("#credentials-dialog").html(html);
			$("#credentials-dialog").dialog({modal: true, draggable: false, resizable: false, width: 450, title: "Confirm Bidding Credentials", close: function(event, ui){
				$.ajax({url: "/cgi-bin/logger.cgi", async: false, cache: false, data: {"modal": $("#credentials-dialog").dialog("option", "title"), "time": (new Date().getTime() - modaldate.getTime())}});
			}});
			return false;
		}
	});
	
	$("#credentialform").live("submit", function(){return false;});
	
	$("#credentialform input[type=button]").live("click keypress", function(event){
		$("#bidForm input[name=password]").val($.trim($("#mccpw").val()));
		
		if($("#mccbnum").is(":visible")){
			$("#bidForm input[name=bidder]").val($.trim($("#mccbnum").val()));
		}
		
		$("#bidForm").append("<input type=\"hidden\" name=\"" + $("#" + event.target.id).attr("name") + "\" value=\"" + $("#" + event.target.id).val() + "\" />");
		$("#credentials-dialog").dialog("close");
		$("#bidForm").submit();
	});
	
	$("#dropdown-login").bind("click keypress", function(event){
		if(event.type.match(/click/i) || event.which == 32 || event.which == 13){
			$("#login").toggle();
		}
		return false;
	});

	$("#searchQuery").focus(function(){
		if(($.trim($(this).val()) == "") || ($.trim($(this).val()).match(/search/i) != null)){
			$(this).val("");
			$(this).css("color", "black");
		}
	});

	$("#searchQuery").blur(function(){
		if(($.trim($(this).val()) == "") || ($.trim($(this).val()).match(/search/i) != null)){
			$(this).val("Search");
			$(this).css("color", "gray");
		}
	});

	$("#bnum").focus(function(){
		if(($.trim($(this).val()) == "") || ($.trim($(this).val()).match(/account\s*or\s*email/i) != null)){
			$(this).val("");
			$(this).css("color", "black");
		}
	});

	$("#bnum").blur(function(){
		if(($.trim($(this).val()) == "") || ($.trim($(this).val()).match(/account\s*or\s*email/i) != null)){
			$(this).val("Account or Email");
			$(this).css("color", "gray");
		}
	});
	
	$("#pwalt").focus(function(){
		$(this).hide();
		$("#bpwd").show();
		$("#bpwd").focus();
	});
	
	$("#pwalt").blur(function(){
		if(($.trim($(this).val()) != "") && ($.trim($(this).val()).match(/password/i) == null)){
			$("#bpwd").val($.trim($(this).val()));
			$(this).val("Password");
			$(this).hide();
			$("#bpwd").show();
		}
	});

	$("#bpwd").blur(function(){
		if(($.trim($(this).val()) == "")){
			$(this).val("");
			$("#pwalt").val("Password");
			$(this).hide();
			$("#pwalt").show();
		}
	});

	$("#email").focus(function(){
		if(($.trim($(this).val()) == "") || ($.trim($(this).val()).match(/email\s*address/i) != null)){
			$(this).val("");
			$(this).css("color", "black");
		}
	});

	$("#email").blur(function(){
		if(($.trim($(this).val()) == "") || ($.trim($(this).val()).match(/email\s*address/i) != null)){
			$(this).val("Email address");
			$(this).css("color", "gray");
		}
	});

	$("#prevpage").click(function(){
		$("#pages input[name=nwpage]").val("p" + $("#pages input[name=ppage]").val());
		$("#pages").submit();
	});

	$("#nextpage").click(function(){
		$("#pages input[name=nwpage]").val("p" + $("#pages input[name=npage]").val());
		$("#pages").submit();
	});

	$(".refreshitem").click(function(){
		$("#rform input[name=rnum]").val($(this).attr("href"));
		$("#rform").submit();
		return false;
	});

	$("#mcategories select").change(function(){
		if($.trim($("#mcategories select option:selected").val()) != ""){
			self.location = $.trim($("#mcategories select option:selected").val());
		}
	});

	$("#checkall").click(function(){
		$("#selectform input[type=checkbox]").attr("checked", "checked");
		return false;
	});

	$("#uncheckall").click(function(){
		$("#selectform input[type=checkbox]").removeAttr("checked");
		return false;
	});

	$("#search").submit(function(){
		if(($.trim($("#searchQuery").val()) != "") && ($.trim($("#searchQuery").val()).match(/search/i) == null)){
			return true;
		}else{
			alert("Enter a value to search for");
			return false;
		}
	});

	$("#accountActions").submit(function(){
		var error = "";
		error += (($.trim($("#bnum").val()).match(/^\d+|[\w!#$%&'*+\/=?^`{|}~-]+(?:\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i) == null) ? (($.trim($("#bnum").val()) == "" || $.trim($("#bnum").val()).match(/customer\s*no\.\/email/i) != null) ? "Customer number or email address is required" : "Invalid customer number or email address") : "");
		error += ((error != "") ? "\n" : "");
		error += (($.trim($("#bpwd").val()).match(/^[\w!"#$%&*+,\/\:'=?@-]{2,50}$/i) == null) ? (($.trim($("#bpwd").val()) == "") ? "Password is required" : "Invalid password") : "");

		if(error == ""){
			return true;
		}else{
			alert(error);
			return false;
		}
	});

	$("#newsletter").submit(function(){
		if($.trim($("#email").val()).match(/^[\w!#$%&'*+\/=?^`{|}~-]+(?:\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i) != null){
			return true;
		}else{
			alert("Invalid email address format");
			return false;
		}
	});
	
	if($(".sorted").length > 0){
		$(".sorted").tablesorter();
	}

	if($.trim($("#rform input[name=mnlistrnum]").val()).match(/^[A-Z]{0,2}\d{4}$/i) != null){
		window.location.hash = "row" + $("#rform input[name=mnlistrnum]").val();
	}
	
	$("#navigation .ui-icon").css("display", "inline-block");
	$("#sidebar .block-label").hide();
	$("#sidebar input").blur();
	$("#sidebar #bpwd").blur();
});

