$(document).ready(function() {
	$("input#partner").keyup(filter).keyup().parents('form').submit(validate);
	var input = $("input#partner");
	var position = input.position();
	var padding = input.css('paddingLeft').substring(0,input.css('paddingLeft').indexOf("px"));
	$("#suggestions").css('top',position.top+input.height()+padding*2+3).css('left',position.left).width(input.width()+padding*2+2)
	.fadeOut(0);
	$(".bodycontainer form input[type='button']").click(function(){$(this).parents('form').submit()});
});

var currSelection = -1;
var currPID = -1;

$(document).keydown(function(e) {
 	switch(e.keyCode) { 
		case 38: navigate(-1); break;
		case 40: navigate(1); break;
		case 13: $("#suggestions span").eq(currSelection).click(); break;
		case 27: clear(true); break; 
	}
});


function filter(e) {
	var term = $.trim($(this).val().toLowerCase());
	if(term.length==0) { clear(true); return false; }
	var keycode = e.keyCode;
	if (!(keycode == 9 || keycode == 13 || keycode == 16 || keycode == 17 || keycode == 18 
		  || keycode == 38 || keycode == 40 || keycode == 224 || keycode==27))
		{
			$.getJSON("../getPartners.php?q="+term+"&c="+COUNTRY, suggest);
		}
}

function suggest(data){
	var html = "";
	$.each(data, function(i,item){
		html += "<span type='"+item.discount+"' pid="+item.id+">"+item.name+"</span>";
	});
	if(html!="") {
		$("#suggestions").html(html).fadeIn("fast");
		stripe();
	}
}

function stripe() {
	$("#suggestions span")
		.mouseover(function() {
			currSelection = $("#suggestions span").index($(this));
			currPID = $(this).attr("pid");
			navigate(0);
		})
		.mouseout(function(){
			if(currSelection == $("#suggestions span").index($(this))) clear();
		})
		.click(function() {
			$("input#partner").val(html_entity_decode($(this).html()));
			clear(true);
		})
		.filter(":even").addClass("stripe");
		
	var span = $("#suggestions span[pid="+currPID+"]");
	clear();
	if(span.size()>0) {
		currSelection = $("#suggestions span").index(span);
		currPID = span.attr("pid");
		navigate(0);
	}
}

function navigate(dir) {
	var size = $("#suggestions span").size();
	currSelection += dir;
	if(currSelection<0) currSelection = 0;
	if(currSelection>=size) currSelection = size-1; 
	
	$("#suggestions span.over").removeClass("over");
	$("#suggestions span").eq(currSelection).addClass("over");
	currPID = $("#suggestions span.over").attr("pid");
}

function clear(hide) {
	currSelection = currPID = -1;
	$("#suggestions span.over").removeClass("over");
	if(hide) $("#suggestions").fadeOut("fast");
}

function validate() {
	$("input#code").val($.trim($("input#code").val()));
	$("input#partner").val($.trim($("input#partner").val()));
	
	var errors = new Array();
	
	/*if( $("input#code").val().length==0) {
		errors.push("assigned EPP Sales person code");
	}*/
	
	if( $("input#partner").val().length==0) {
		errors.push("company name");
	}
	
	$("span.error").html("");
	if(errors.length>0) {
		var html = "Please enter your ";
		for(var i=0; i<errors.length; i++) {
			if(i>0) html += " and ";
			html += errors[i];
		}
		html += ".";
		$("span.error").html(html);
	}
	
	return errors.length==0;
}

function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}
