var zipcodeLoc; //plot location for zipcode searching
var selectedNr = -1;

$(document).ready(function() {
	
	 var ajt = $('a.jt').cluetip({
			cluetipClass: 'jtip-resultpage', 
		    arrows: true, 
		    dropShadow: false,
		    sticky: true	,
		    mouseOutClose: false,
		    closePosition: 'title',
		    closeText: '<img src="/images/dc/btn-close.gif" alt="close" />',
			ajaxCache: false,
			positionBy: 'bottomTop',
			hideLocal: false,
			showTitle: true,
			leftOffset: 40,
			activation: 'click',
			width: 320
	});
	 
	$("#alfaRefine .close").click(function() {
		$("#alfaRefine").hide();
	});
	$(".openAlfaRefine").click(function() {
		$("#alfaRefine").css("left", $(".openAlfaRefine").offset().left - 27 + 'px');
		$("#alfaRefine").css("top", $(".openAlfaRefine").offset().top + 8 + 'px');
		$("#alfaRefine").show();
	});
	$(".distanceRefineOpen").click(function () {
		$("#distanceRefine").css("top", ($(".distanceRefineOpen").offset().top+6) + "px");
		$("#distanceRefine").css("left", ($(".distanceRefineOpen").offset().left-12) + "px");
		$("#distanceRefine").show();
	});
	$("#distanceRefine .close").click(function(){
		$("#distanceRefine").hide()
	});
	$(".infobox").hover(function() {
	},function() {
		$("#pb").hide();
	});

	/*
	if($("#refined").html().replace(/^\s+/,'') != "" || $("#refinel").html().replace(/^\s+/,'') != "" || $("#refinek").html().replace(/^\s+/,'') != "" || $("#refinec").html().replace(/<.+>/g,'').replace(/^\s+/,'') != ""){
		$("#refinetitle").show();
	}
	*/

	$(".infoRequest").click(function() {
		var top= $(this).offset().top;
		var left = $(this).offset().left;
	    var sendType = $(this).attr("name");
	    var showSendType = null;
	    if (sendType == "mail") {
	        showSendType = "mail naar";
	    }
	    else if (sendType == "fax") {
	        showSendType = "";
	    }
	    else {
	        showSendType = sendType;
	    }
	    
	    var hrefValue = $(this).attr("href");
	    var secondIndex = hrefValue.indexOf("#",1);
	    var lastIndex = hrefValue.lastIndexOf("#");
	    
	    var cid = hrefValue.substring(1, secondIndex);
	    var compName = escape(hrefValue.substring(secondIndex+1, lastIndex));
	    var sendContent = " "+hrefValue.substring(lastIndex+1, hrefValue.length);
	    loadReviewBox();
        $("#reviewWindow").css("width",450);
	    $("#reviewContainer").load("/ajax/resultsinformationrequest.jsp", {showsendtype: showSendType, sendtype: sendType, sendcontent: sendContent, companyId: cid, companyName: compName}, function() {
	    	
	    	$("#informationRequest .title").html(unescape(compName));
	    	$("#toCompany").val(unescape(compName));

	        $("form.writeInforRequest").submit(function() {
	            var inputs = [];
	            $(':input', this).each(function() {
	            	if (($(this).attr("name") != "nameDropDown") && ($(this).attr("name") != "optIn")) {
		                var val = this.value;
		                val = val.replace(/\n/g, "<br>").replace(/\n\n+/g, '<br><br>');
		                
		                if ($(this).attr("name") == "subject") {
		                	if (val == "op0")
		                		val = "";
		                }
		                
		                inputs.push(this.name + '=' + escape(val));
	            	}
	            });
	            inputs.push("gender=" + $("#nameDropDown").val());

	        	if ($("#optIn").attr("checked"))
	                inputs.push("optIn=true");
	        	else
	                inputs.push("optIn=false");
	        	
	        	inputs.push("resultsPage=true");
	        	var message = $("#message").val();
	        	
	        	if(message != ''){
	        		$.ajax({ 
	    				url: "/checkbadwords",
	    				type: "POST",
	    				data: "text=" + message,
	    				success: function(returnValue) { 
	    					if (returnValue.indexOf("ok") > -1) {
	    						sendInformation(inputs, cid);
	    					} else {
	    						$("#inforRequestErrorLine").load("/ajax/showerror.jsp?badWord=" + escape(returnValue));
	                            $("#inforRequestErrorLine").show();
	    					}
	    				}});	
	        	} else {
	        		sendInformation(inputs, cid);
	        	}
	        	
	        });
	        $("#userloc").remove();
	        $("#reviewWindow").css("top", top - 80 + "px");
	        $("#reviewWindow").css("left", left);
	        $("#reviewWindow").show();
	    });
	});
});

function sendInformation(inputs, cid){
	$.ajax({
        url: "/SendInformationRequest",
        type: "POST",
        data: inputs.join('&'),
        success: function(returnValue) {
            returnValue = trim(returnValue);
            if (returnValue.match("ir.mailsend") != null) {
                ilLog('leadclicks.informationrequestsend', 10, cid, -1, -1, -1, '');

                if ($("#language").val() == 'fr') {	
                	$("#reviewContainer").html("<div style='padding: 0px 10px 20px 10px; text-align: center;'><h3 style='font-size: 14px; font-weight: bold;'>Merci, votre demande a &eacute;t&eacute; envoy&eacute;</h3><input type='button' value='Fermer' style='background:transparent url(../images/dc/btn-close-bg.gif) no-repeat scroll 0 0; width:88px; height:21px; margin:0 0 0 12px; padding:0 0 20px; border:0; color:#fff; font:bold 11px; cursor:pointer;' id='btnCloseReview'/></div>");
                }else if ($("#language").val() == 'en'){
                	$("#reviewContainer").html("<div style='padding: 0px 10px 20px 10px; text-align: center;'><h3 style='font-size: 14px; font-weight: bold;'>Thanks, your request is sent</h3><input type='button' value='Close' style='background:transparent url(../images/dc/btn-close-bg.gif) no-repeat scroll 0 0; width:88px; height:21px; margin:0 0 0 12px; padding:0 0 20px; border:0; color:#fff; font:bold 11px; cursor:pointer;' id='btnCloseReview'/></div>");
                }else {
                	$("#reviewContainer").html("<div style='padding: 0px 10px 20px 10px; text-align: center;'><h3 style='font-size: 14px; font-weight: bold;'>Bedankt, je aanvraag is verzonden</h3><input type='button' value='Sluiten' style='background:transparent url(../images/dc/btn-close-bg.gif) no-repeat scroll 0 0; width:88px; height:21px; margin:0 0 0 12px; padding:0 0 20px; border:0; color:#fff; font:bold 11px; cursor:pointer;' id='btnCloseReview'/></div>");
                }

                $("#btnCloseReview").click(function() {
                    $("#reviewWindow").remove();
                });
            } else {
            	$("#inforRequestErrorLine").load("/ajax/showerror.jsp?msg=" + returnValue);
                $("#inforRequestErrorLine").show();
                switch (returnValue) {
                    case "rir.invalidsubject":
                        $("#subject").css("border-color", "red");
                        $("#message").css("border-color", "#D1D1D1");
                        $("#name").css("border-color", "#D1D1D1");
                        $("#email").css("border-color", "#D1D1D1");
                        break;
                    case "rir.invalidmessage":
                        $("#subject").css("border-color", "#D1D1D1");
                        $("#message").css("border-color", "red");
                        $("#name").css("border-color", "#D1D1D1");
                        $("#email").css("border-color", "#D1D1D1");
                        break;
                    case "rir.invalidname":
                        $("#subject").css("border-color", "#D1D1D1");
                        $("#message").css("border-color", "#D1D1D1");
                        $("#name").css("border-color", "red");
                        $("#email").css("border-color", "#D1D1D1");
                        break;
                    case "rir.invalidmail":
                        $("#subject").css("border-color", "#D1D1D1");
                        $("#message").css("border-color", "#D1D1D1");
                        $("#name").css("border-color", "#D1D1D1");
                        $("#email").css("border-color", "red");
                        break;
                    default:
                        //alert("Information Error!");
                }
            }
        }
    });	
}

function closeBanner(){
	$("#dcBannerPlaceholder").hide();
}
function emailBannerSubscription(themeId, locationId, recognized) {
	$("#emailbannertxt").hide();
	if ($.cookie("il_uid") != null) {
		$.ajax({ 
			url: "/email/subscription",
			type: "POST",
			data: "themeId=" + themeId + "&locationId=" + locationId,
			success: function(returnValue) {
				if (returnValue.indexOf("ok")>-1){
					if(recognized){
						$("#emailbannerthankrecognizeduser").show();
					} else {
						$("#emailbannerthank").show();
					}
					
					$("#emailSubscribe").val("");
					$("#passwordAccount").val("");
				} else {
					// what should I do ?
				}
			}
		});
	} else {
		$("#emailbannersignup").show();
		if(recognized){
			$("#txtrecognized").show();
			$("#txtnotrecognized").hide();
		} else {
			$("#txtrecognized").hide();
			$("#txtnotrecognized").show();
		}
	}
}
function emailBannerSubscriptionNoLoggedIn(themeId, locationId) {
	$("#emailbannersignup").hide();
	
	if($("#passwordAccount").is(':visible')){
		
		$.ajax({ 
			url: "/login",
			type: "POST",
			data: "mail=" + $("#emailSubscribe").val() + "&pass=" + $("#passwordAccount").val(),
			success: function(returnValue) {
				if (returnValue.indexOf("ok")>-1){
					emailBannerSubscription(themeId, locationId, true);
				} else {
					if (returnValue.indexOf("mail.password.not.match")>-1){
						returnValue = "mail.password.not.match.simple";
					}
					$("#emailbannersignup").show();
					$("#txtrecognized").show();
					$("#txtnotrecognized").hide();
					
					var errorPageUrl = "/ajax/showerror.jsp?msg=" + returnValue;
					if (returnValue.indexOf("expired.account") > -1) {
						errorParams = returnValue.split("#");
						errorPageUrl = "/ajax/showerror.jsp?msg=" + errorParams[0] + "&userId=" + errorParams[1] + "&mail=" + errorParams[2];
					} 
					
					$("#errormessageshowerror").show();
					$("#errormessageshowerror").load(errorPageUrl);
					$("#emailSubscribe").focus();
				}
			}
		});
		
	} else {
		$.ajax({ 
			url: "/email/subscription/notloggedin",
			type: "POST",
			data: "themeId=" + themeId + "&locationId=" + locationId + "&email=" + $("#emailSubscribe").val(),
			success: function(returnValue) {
				if (returnValue.indexOf("ok")>-1){
					$("#emailbannerthank").show();
					$("#emailSubscribe").val("");
					$("#passwordAccount").val("");
				} else {
					if(returnValue.indexOf("pass.blank")>-1){
						$("#passwordAccountLabel").show();
						$("#passwordAccount").show();
						$("#passwordAccount").focus();
					}
					
					$("#emailbannersignup").show();
					$("#txtrecognized").show();
					$("#txtnotrecognized").hide();
					
					$("#errormessageshowerror").show();
					$("#errormessageshowerror").load("/ajax/showerror.jsp?msg=" + returnValue);
				}
			}
		});
	}
}
