var mpbTemplate = "<table id='mpb' class='hoverpop' cellspacing='0' cellpadding='0'>" +
	"<tr><td class='tl'></td><td class='t'></td><td class='tr'></td></tr>" + 
	"<tr><td class='l'></td><td class='content'></td><td class='r'></td></tr>" +
	"<tr><td class='bl'></td><td class='b' align='center'></td><td class='br'></td></tr></table>";

var il_review = null;
var il_title = null;
var il_rating = null;
var il_optIn = null;
var il_cid = null;
var il_rid=0;
var il_operation=null;

$.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
});

$(document).ready(function() {
	
	$(".tomTomRouteDetail").click(function() {

		var company_name= $(this).attr("name");
		var thisWidth=$(this).width();
		var offset = $(this).offset();
		var obj_pos = $(this).position();
		
		if($("#planRouteBox .route-txt-error").length>0){
			$("#planRouteBox .route-txt-error").hide();
		}
			
		$("#planRouteBox .route-txt").addClass("noFocus");
		$("#planRouteBox .route-txt").css("font-weight", "normal");
		$("#planRouteBox .route-txt").val($("#planRouteBox #fromBoxValue").val());
		$("#planRouteBox").show();

		$(".route-spn").html(company_name);
		$("#planRouteBox").css("top", (obj_pos.top-40) + "px");
		$("#planRouteBox").css("left", (obj_pos.left+ thisWidth +1) + "px");
		if ($("#planRouteBox .route-txt").val() == "" || $("#planRouteBox .route-txt").val()==undefined) {
			$("#planRouteBox .route-txt").val($("#planRouteBox #fromBoxValue").val());
		}
		
		$("#planRouteBox .route-txt").focus(function(){
			if($(this).attr("value") == $("#planRouteBox #fromBoxValue").val()){
				$(this).attr("value", "");
				$(this).css("font-style","normal");
				$(this).css("font-weight","bold");
				$(this).removeClass("noFocus");
			}
		});
		$("#planRouteBox form").submit(function(){
	
			if ( ($("#planRouteBox .route-txt").val() == "") || ($("#planRouteBox .route-txt").val()== $("#planRouteBox #fromBoxValue").val()) ) {
				$("#planRouteBox .route-txt-error").show()
				return false;
			}
			else
			{
				submitTomTomRouteDetail();
			}
		});
		$("#planRouteBox #planRouteButton").click(function(){
	
			if ( ($("#planRouteBox .route-txt").val() == "") || ($("#planRouteBox .route-txt").val()== $("#planRouteBox #fromBoxValue").val()) ) {
				$("#planRouteBox .route-txt-error").show()
				return false;
			}
			else
			{
				submitTomTomRouteDetail();
			}
		});
		$("#planRouteBox .cancelLink").click(function() {$("#planRouteBox").hide();});
		$(document).click(removeTomTomRouteBoxDetail);
		
		
		return false;
	});
	
	$("#moreDescription").click(function() {
		$("#moreDescription").hide();
		$("#wholeDescription").show();
		return false;
	});
	
	$("#lessDescription").click(function() {
			$("#wholeDescription").hide();
			$("#moreDescription").show();
			return false;
		});
	
	$("#moreCategories").click(function() {
		$("#moreCategories").hide();
		$(".allCategories").show();
		return false;
	});	
	
	$("#lessCategories").click(function() {
		$(".allCategories").hide();
		$("#moreCategories").show();
		return false;
	});
	
	$(".moreOpeningHours").click(function() {
		$(".moreOpeningHours").hide();
		$(".allDaysOfTheWeek").show();
		$(".lessOpeningHours").show();
		return false;
	});	

	$(".lessOpeningHours").click(function() {
		$(".lessOpeningHours").hide();
		$(".allDaysOfTheWeek").hide();
		$(".moreOpeningHours").show();
		return false;
	});	

	$("#searchNearbyLink").click(function() {
		$(this).addClass('clicked').next().slideDown();
			$("#searchNearByWhat").val($("#searchNearByText").val());
		return false;
	});
	
	$("#searchNearbyCancelButton").click(function() {
		$("#searchNearbyBox").slideUp();
		$("#searchNearbyLink").removeClass('clicked');
		$("#searchNearByWhat").addClass("noFocus");
		$("#searchNearByWhat").css("font-weight","normal");
		return false;
	});
	
	$("#searchNearByWhat").focus(function(){
		if($(this).attr("value") == $("#searchNearByText").val()){
			$(this).attr("value", "");
			$(this).css("font-style","normal");
			$(this).css("font-weight","bold");
			$(this).removeClass("noFocus");
		}
	});
	
	$("#addDetailsLink").click(function() {
		$("input:radio").attr("checked", false);
		$("#addDetailsText").css("color", "#555555");
		$("#addDetailsPopup").show();
		$("#addDetailsPopup").css("top", ($(window).height()-206)/2+"px");
		$("#addDetailsPopup").css("left", ($(window).width() - 359)/2+"px");
		$(document).click(removeAddDetailsBox);
		return false;
	});
	
	function removeAddDetailsBox(event){
		if ($(event.target).parents("#addDetailsPopup").length < 1) {
			$("#addDetailsPopup").hide();
			$(document).unbind("click", removeAddDetailsBox);
		}
	};
	
	$("#addDetailsCancel").click(function() {
		$("#addDetailsPopup").hide();
		return false;
	});
		
	$("#addDetailsContinue").click(function() {

		var optionSelected = $("input[name=addDetailOptions]:checked").val();
		
		if(optionSelected == "option1"){
			$("#addDetailsPopup").hide();
			registerFeedbackRequestClick();
			return false;
		}
		else if(optionSelected == "option2"){
			$(this).attr("href",$("input[name=addDetailOptions]:checked").attr("title"));
			$("#addDetailsPopup").hide();
		}else{
			$("#addDetailsText").css("color", "#FF0000");
		}
	});
	
	$("#planRoute #planRouteButton").click(function() {
		if ($("#planRoute .routeLocation").val().length > 0) {
			var url = $("#planRoute").attr("action");
			$("#planRoute").attr("action", url.replace("/home/", "/" + $("#planRoute .routeLocation").val() + "/"));
			$("#planRoute").submit();
			
			setTimeout(function() {
				$("#planRoute").attr("action", url);
			}, 1000);
		}
		return false;
	});

	$(".reviewDesc").focus(function(){
		 if($(this).attr("value") == $("#reviewDescDefault").val()){
		//	$(this).attr("value", "");
		//	$(this).css("font-style","normal");
	//		$(this).css("font-weight","bold");
			$(this).addClass("noFocus");
		}
	});
	
	$(".reviewDesc").keydown(function(){
		if($(this).attr("value") == $("#reviewDescDefault").val()){
			$(this).attr("value", "");
			$(this).removeClass("noFocus");
		}
	});
	$(".reviewDesc").click(function(){
		$(this).keydown();
	})
	$(".reviewDesc").blur(function(){
		if($(this).attr("value") == $("#reviewDescDefault").val()){
			$(this).addClass("noFocus");
		}
	});

	$(".moreIlocalReview").click(function(){
		$(".reviewMore").show();
		$(this).hide();
		$(".lessIlocalReview").show();
//		$(document).bind("click", hideReviewBox);
		return false;
	});
	
	$(".moreKeywords").click(function() {
		$("#" + $(this).attr("name") + "_less").toggle();
	    $("#" + $(this).attr("name") + "_more").toggle();
	    return false;
	});
	
	$(".lessIlocalReview").click(function(){
		$(".reviewMore").hide();
		$(this).hide();
		$(".moreIlocalReview").show();
		return false;
	});
	
	$(".moreScrapedReview").click(function(){
		$(".scrapedMore").show();
		$(this).hide();
		$(".lessScrapedReview").show();
		return false;
	});
	
	$(".lessScrapedReview").click(function(){
		$(".scrapedMore").hide();
		$(this).hide();
		$(".moreScrapedReview").show();
		return false;
	});
	
	/* 
	$("#openWriteReviewBtn").click(function() {
		$(".review-detail").find(".commentTable").html("");
		openWriteReviewForm();
	});
	
	
	$("#closeWriteReviewBtn").click(function() {
		$("#closeWriteReviewBtn").hide();
		$("#openWriteReviewBtn").show();
		$("#writeReview").hide();
	});
	
	
	if(window.location.href.indexOf("#write_review") > -1 && $("#openWriteReviewBtn").is(':visible')) {
		$("#openWriteReviewBtn").click();	
	}
	*/
	
    $("#closebtn").click(function() {
    	var url = window.location.href;
    	if(url.indexOf('#') > -1) url = url.substring(0,url.indexOf('#'));
    	if(url.indexOf('?') > -1) url = url.substring(0,url.indexOf('?'));
    	window.location = url+"#reviews";
    }); 
	
	$(".detailWritereviewLink").click(function() {
		$("#openWriteReviewBtn").hide();
		$("#closeWriteReviewBtn").show();
		$("#writeReview").show();
        setTimeout('$(".my-review .reviewTitle").focus();', 100);
    });
	
	$("#cancelreview").click(function() {
		$("#rating").val(0);
		$('.writereview .star').css('background-image','url("/img/star_off_big.png")');
		$('#writeReview').slideUp();
	});
	
	$("form.writereview #submitreview").click(function() {
//        if ($(".my-review form.writereview .reviewTitle").val() == "") {
 //       	submitReviewError(0, "incorrectreview");
        //} else
        	if ($(".my-review form.writereview .rating").val() == "" || $(".my-review form.writereview .rating").val() < 1) {
        	submitReviewError(0, "incorrectrating");
        } else {
        	il_operation="addreview";
            submitReview(0, "addreview");
        }
    });
	
	$(".editreview").click(function() {
        $(".review-detail .commentTable").hide();
        $(".review-detail .editreview").hide();
        $(".review-detail .editcomment").hide();

        $(this).parents(".review-detail").find(".editreviewform").show();
        $(".editreviewform .cancelEditBtn").click(function() {
            $(this).parents(".review-detail").find(".editreviewform").hide();
            $(this).parents(".review-detail").find(".editreview").show();
            $(".review-detail .editreview").show();
            $(".review-detail .editcomment").show();
        });
        $(".editreviewform .publishEditBtn").click(function() {
            if ($(this).parents(".editreviewform").find(".editTitle").val() == "") {
               submitReviewError($(this).parents(".review-detail").attr("id"), "incorrectedit");
            } else {
               submitReview($(this).parents(".review-detail").attr("id"), "editreviewform");
            }
        });
    });
	
	$(".deletecomment").click(function() {
        $(".hoverpop").remove();
        var deleteCid = $(this).parents(".comment-detail").attr("title");
        var deleteRid = $(this).parents(".review-detail").attr("id");
        $("body").append(mpbTemplate);
        $("#mpb").css("width", "185px");
        $("#mpb .content").css("text-align", "center");
        $("#mpb .l").html("<div class='n' style='margin-top:12px;'></div>");
        $("#mpb .l").css("vertical-align", "top");        

        $("#mpb .content").load("/ajax/confirmpopup.jsp?confirmationMessage=general.confirm",function() {
        	$("#mpb .deleteYes").click(function() {
                $("#mpb").remove();

                  $.ajax({
                      url: "/deleteComment",
                      type: "POST",
                      data: "commentId=" + deleteCid + "&reviewId=" + deleteRid,
                      success: function(returnValue) {
                          if (returnValue.indexOf('done') > -1) {
                              var parameters = "type=deletecomment&commentid=" + deleteCid;

                              $.ajax({
                                  url: "/ajax/closereview.jsp",
                                  type: "POST",
                                  data: parameters,
                                  success: function(html) {
                                      $("#comment_" + deleteCid).html(html);
                                      $(".bigCloseReview").click(function() {
                                          window.location.reload();
                                      });
                                  }
                              });
                          } else {
                              alert("delete failed");
                          }
                      }
                  });
              });
              $("#mpb .deleteNo").click(function() {
                  $("#mpb").remove();
                  return false;
              });
        });

        $("#mpb").css("top", ($(this).offset().top - 17) + "px");
        $("#mpb").css("left", ($(this).offset().left + $(this).width() + 15) + "px");
        $("#mpb").show();
    });
	
	$(".deletereview").click(function() {
        $(".hoverpop").remove();
        var deleteRid = $(this).parents(".review-detail").attr("id");
        $("body").append(mpbTemplate);
        $("#mpb").css("width", "185px");
        $("#mpb .content").css("text-align", "center");
        $("#mpb .l").html("<div class='n' style='margin-top:12px;'></div>");
        $("#mpb .l").css("vertical-align", "top");
        
        $("#mpb .content").load("/ajax/confirmpopup.jsp?confirmationMessage=general.confirm",function() {
        	$("#mpb .deleteYes").click(function() {
        		$("#mpb").remove();

	            $.ajax({
	                url: "/deleteReview",
	                type: "POST",
	                data: "reviewId=" + deleteRid + "&companyId=" + $("#companyId").val() + "&countryISO2=" + $("#countryISO2").val(),
	                success: function(returnValue) {
	                    if (returnValue.indexOf('done') > -1) {
	                        var parameters = "type=deletereview&reviewid=" + deleteRid;
	
	                        $.ajax({
	                            url: "/ajax/closereview.jsp",
	                            type: "POST",
	                            data: parameters,
	                            success: function(html) {
	                                $("#" + deleteRid).html(html);
	                                $(".bigCloseReview").click(function() {
	                                    window.location.reload();
	                                });
	                            }
	                        });
	                    } else {
	                        alert("delete failed");
	                    }
	                }
	            });
	        });
	        $("#mpb .deleteNo").click(function() {
	            $("#mpb").remove();
	            return false;
	        });
        });
        $("#mpb").css("top", ($(this).offset().top - 17) + "px");
        $("#mpb").css("left", ($(this).offset().left + $(this).width() + 15) + "px");
        $("#mpb").show();
    });
	
	$(".editcomment").click(function() {
        $(".review-detail .commentTable").hide();
        $(".review-detail .comment-detail .editcomment").hide();

        $(this).parents(".comment-detail").find(".editcommentform").show();
        $(".editcommentform .cancelEditBtn").click(function() {
            $(this).parents(".comment-detail").find(".editcommentform").hide();
            $(".review-detail .comment-detail .editcomment").show();
        });
        $(".editcommentform .publishEditBtn").click(function() {
            if ($(this).parents(".editcommentform").find(".editDesc").val() == "") {
                submitReviewError($(this).parents(".comment-detail").attr("title"), "incorrectedit");
            } else {
            	submitComment($(this).parents(".review-detail").attr("id"), $(this).parents(".comment-detail").attr("title"), "editcomment");
            }
        });
    });
	
	$(".commentreview").click(function() {
		$(".errormsg_addcomment").hide();
		
		$("#closeWriteReviewBtn").hide();
		$("#openWriteReviewBtn").show();
		$("#writeReview").hide();
		
		$(".review-detail .commentTable").hide();
        $(".review-detail .editreview").hide();
        $(".review-detail .editcomment").hide();

        $(this).parents(".review-detail").find(".commentTable").html($("#commentTemplate").html());
        $(this).parents(".review-detail").find(".commentTable").show();
        $(this).parents(".review-detail").find(".commentTable .editcomment").show();
        
    	$(".commentDesc").val($("#commentDescDefault").val());
    	$(".commentDesc").addClass("noFocus");
    	$(".commentDesc").focus();
        
        $(".commentTable .cancelEditBtn").click(function() {
            $(this).parents(".review-detail").find(".commentTable").html("");
            $(this).parents(".review-detail").find(".commentTable").css("height", "1px");
            $(".review-detail .editreview").show();
            $(".review-detail .editcomment").show();
            
            if ($("#mpb .content").attr("name") == "comment") {
                $("#mpb").remove();
            }
        });
        $(".commentTable .publishEditBtn").click(function() {
        	var commentDescValue = $(this).parents(".commentTable").find(".commentDesc").val();
        	
            if (commentDescValue == "" || commentDescValue == $("#commentDescDefault").val()) {
                submitReviewError($(this).parents(".review-detail").attr("id"), "blank");
            } else {
            	il_rid = $(this).parents(".review-detail").attr("id");
            	il_operation = "addcomment";
                submitReview($(this).parents(".review-detail").attr("id"), "addcomment");
            }
        });

        $(".commentTable .commentDesc").focus(function() {
           	if($(this).attr("value") == $("#commentDescDefault").val()){
           		$(this).addClass("noFocus");
    		}
        });
        $(".commentTable .commentDesc").keydown(function() {
        	$(".errorMpb").remove();
    		if($(this).attr("value") == $("#commentDescDefault").val()){
    			$(this).attr("value", "");
    			$(this).removeClass("noFocus");
    		}
        });
        $(".commentTable .commentDesc").click(function() {
        	
        	$(this).keydown();
        });
      //  $(document).click(hideCommentBox);
    });
	
	function hideCommentBox(event){
		if ($(event.target).parents("form.editcomment").length < 1) {
			$('form.editcomment').hide();
			$(document).unbind("click", hideCommentBox);
		}
	};
	$(".abusereview").click(function() {
        $(".hoverpop").remove();
        $("body").append(mpbTemplate);
        $("#mpb").css("width", "185px");
        $("#mpb .content").css("text-align", "center");
        $("#mpb .l").html("<div class='n' style='margin-top:12px;'></div>");
        $("#mpb .l").css("vertical-align", "top");

        var abuseRid = $(this).parents(".review-detail").attr("id");
        $("#mpb .content").load("/ajax/confirmpopup.jsp?confirmationMessage=general.reportabuse",function() {
	        $("#mpb .deleteYes").click(function() {
	            $("#mpb").remove();
	            $.ajax({
	                url: "/abuseReview",
	                type: "POST",
	                data: "abusedReviewId=" + abuseRid,
	                success: function(returnValue) {
	                    if (returnValue.indexOf('ok_') > -1) {
	                        if ($.cookie("il_abuse_review") == null) {
	                            $.cookie("il_abuse_review", "_" + abuseRid + "_", { path: '/'});
	                        } else {
	                            $.cookie("il_abuse_review", $.cookie("il_abuse_review") + "_" + abuseRid + "_", { path: '/'});
	                        }
	                        $("#messageabusereview_"+abuseRid).load("/ajax/showerror.jsp?error=abusemessage.review");
	                        $("#messageabusereview_"+abuseRid).show();
	                    } else {
	                        alert("abuse failed");
	                    }
	                }
	            });
	        });
	        $("#mpb .deleteNo").click(function() {
	            $("#mpb").remove();
	            return false;
	        });
        });
        $("#mpb").css("top", ($(this).offset().top - 17) + "px");
        $("#mpb").css("left", ($(this).offset().left + $(this).width() + 15) + "px");
        $("#mpb").show();
    });

    $(".abusecomment").click(function() {
        $(".hoverpop").remove();
        $("body").append(mpbTemplate);
        $("#mpb").css("width", "185px");
        $("#mpb .content").css("text-align", "center");
        $("#mpb .l").html("<div class='n' style='margin-top:12px;'></div>");
        $("#mpb .l").css("vertical-align", "top");

        var abuseCid = $(this).parents(".comment-detail").attr("title");
        $("#mpb .content").load("/ajax/confirmpopup.jsp?confirmationMessage=general.reportabuse",function() {
	        $("#mpb .deleteYes").click(function() {
	            $("#mpb").remove();
	            $.ajax({
	                url: "/abuseComment",
	                type: "POST",
	                data: "abusedCommentId=" + abuseCid,
	                success: function(returnValue) {
	                    if (returnValue.indexOf('ok_') > -1) {
	                        if ($.cookie("il_abuse_comment") == null) {
	                            $.cookie("il_abuse_comment", "_" + abuseCid + "_", { path: '/'});
	                        } else {
	                            $.cookie("il_abuse_comment", $.cookie("il_abuse_comment") + "_" + abuseCid + "_", { path: '/'});
	                        }
	                        
	                        $("#messageabusecomment_"+abuseCid).load("/ajax/showerror.jsp?error=abusemessage.comment");
	                        $("#messageabusecomment_"+abuseCid).show();
	                        
	                    } else {
	                        alert("abuse failed");
	                    }
	                }
	            });
	        });
	        $("#mpb .deleteNo").click(function() {
	            $("#mpb").remove();
	            return false;
	        });
        });
        $("#mpb").css("top", ($(this).offset().top - 17) + "px");
        $("#mpb").css("left", ($(this).offset().left + $(this).width() + 15) + "px");
        $("#mpb").show();
    });
    
    $("#ratingstars").hover(function() {
        if ($("#mpb .content").attr("name") == "rating") {
            $("#mpb").remove();
        }
    }, function() {
    }); 
	 
    //from dc.js;
	registerDetailInfoRequestClick();
	
	var urlAnchor =  location.hash;
	//TABS on the DETAIL page 
	$(".tab_content").hide(); //Hide all content
	var activetab = $("ul.tab-bar li:first");
	if("#reviews-detail" == urlAnchor) {
		activetab = $("ul.tab-bar li.review-child");
		$(".tab_content#reviews").show(); //Show first tab content
	} else if($("ul.tab-bar li.promo-child").length > 0) {
		activetab = $("ul.tab-bar li.promo-child");
		$(".tab_content#promotions").show(); //Show first tab content
	} else {
		$(".tab_content:first").show(); //Show first tab content
	}
	activetab.html("<strong>"+activetab.html()+"</strong>").show(); //Activate first tab
		
	$(".advertiser-links .writereview").click(function() {
		try{
			openDetailTab($("ul.tab-bar .review-child"));
			openWriteReviewForm();
		} catch (e) {
		}
	});
	
	$("ul.tab-bar li").click(function() {
		openDetailTab($(this));
	});
	
	$(".reviews-detail").click(function() {
		$("ul.tab-bar li.review-child").click();
	});
	loadTimedMap(); 
}); 

function registerFeedbackRequestClick(){
	
        var country = $("#countryforlink").val();
        
        loadPopupBox($("#feedback").attr('title'));
        showPopupBoxLoading();
        
        $.ajax({
            url: "/feedbacknew",
            type: "POST",
            data: "companyId=" + $("#companyId").val(),
            success: function(html) {
                $("#reviewContainer").html(html);
               
                $("#submitFeedBack").click(function() {
//                	var keywordNum = $("#existedKeywordIndex").val();
                	var data = "";
                	data += "&action=" + $("#action").val();
    				data += "&originalCompanyName=" + encodeURIComponent($("#originalCompanyName").val());
    				data += "&originalCompanyId=" + $("#originalCompanyId").val();
    				data += "&companyCountry=" + $("#companyCountry").val();
    				data += "&noActivities=" + $('#noActivities').val()
    				data += "&doubleListings=" + $('#doubleListings').val()
    				data += "&name=" + encodeURIComponent($("#name").val());
    				data += "&street=" + encodeURIComponent($("#street").val());
    				data += "&phone1=" + $("#phone1").val();
    				data += "&mail=" + encodeURIComponent($("#mail").val());
    				data += "&website=" + encodeURIComponent($("#website").val());
/*    				for (i=0; i<keywordNum; i++) {
    					if ($(".exitedKeywords_"+i).val() != "") {
    						data += "&excistingKeywords=" + $(".exitedKeywords_"+i).val();
    					}
    				}*/
    					
    				data += "&ownName=" + encodeURIComponent($("#ownName").val());
    				data += "&ownMail=" + encodeURIComponent($("#ownMail").val());

    				
    				$.ajax({
    					url: "/feedbacknew/submit",
    					type:"POST",
    					data:data,
    					success: function(returnValue) {
    						if (returnValue.match("ir.mailsend") != null) {
    							$(".feedbackform").css('font-weight','bold').css('text-align','center').load("/ajax/getresourcemessage.jsp?messagekey=error.message.ir.mailsend", function() {});
    							dcsMultiTrack('DCSext._hit_type','click','WT.dl', '99','DCSext.linktype','update form success');
    						} else {
    							$("#inforRequestErrorLine").show();
	    						$("#inforRequestErrorLine").load("/ajax/showerror.jsp?msg="+ returnValue, function() {}); 
	    						dcsMultiTrack('DCSext._hit_type','click','WT.dl', '99','DCSext.linktype','update form failure');
    						}
    						$("#container").html(returnValue);
    					}
    				});
                }); 
                
                $("#popupWindow").show();
            }
        });
       
   
}

function openDetailTab(currentTab) {
	$("ul.tab-bar li strong").parent().html($("ul.tab-bar li strong").html());
	currentTab.html("<strong>"+currentTab.html()+"</strong>"); 
	$(".tab_content").hide();
	var activeTab = currentTab.attr("id"); 
	$(activeTab).fadeIn();
	return false;
}

function openWriteReviewForm() {
	$("#openWriteReviewBtn").hide();
	$("#closeWriteReviewBtn").show();
	$("#writeReview").show();
}

function submitReviewError(id, errorType) {
	if (errorType.indexOf("incorrectedit") > -1) {
		if($("#errormsg_editreview_" + id).size() > 0){
			$("#errormsg_editreview_" + id).load("/ajax/showerror.jsp?msg=submitreview." + errorType);
		    $("#errormsg_editreview_" + id).show();
		}
		if($("#errormsg_editcomment_" + id).size() > 0){
			$("#errormsg_editcomment_" + id).load("/ajax/showerror.jsp?msg=submitreview." + errorType);
		    $("#errormsg_editcomment_" + id).show();
		}
	} else if(errorType.indexOf("blank") > -1){
		$(".errormsg_addcomment").load("/ajax/showerror.jsp?msg=comment." + errorType);
	    $(".errormsg_addcomment").show();
	} else {
		if($("#errormsg_addreview").size() > 0){
			$("#errormsg_addreview").load("/ajax/showerror.jsp?msg=submitreview." + errorType);
		    $("#errormsg_addreview").show();
		}
	} 
}
//END submitReviewError()

function submitReview(il_rid, il_operation) {
	il_cid = $("#companyId").val();
	
	if (il_operation == "addreview") {
	    il_review = $("form.writereview .my-review .reviewDesc").val();
	    if(il_review == $("form.writereview .my-review #reviewDescDefault").val()){
	    	il_review = "";
	    }
		il_title = $("form.writereview .my-review  .reviewTitle").val();
	    il_rating = $("form.writereview .rating").val();
	    if ($(".my-review form.writereview .optIn").attr("checked")) {
	        il_optIn = "true";
	    } else {
	        il_optIn = "false";
	    }
	} else if (il_operation == "addcomment") {
	    il_review = $("#" + il_rid + " .commentDesc").val();
	} else {
	    il_review = $("#" + il_rid + " .editDesc").val();
	    il_title = $("#" + il_rid + " .editTitle").val();
	    il_rating = $("#" + il_rid + " .editRating").val();
	    il_optIn = $("#" + il_rid + " .editOptIn").val();
	}
	
	il_review = $.trim(il_review);

	/*	if (il_operation == "addreview" || il_operation == "editreview") {
	    il_title = $.trim(cleanUpString(il_title));
	}*/
	
	if (il_cid == "" || il_operation == "") {
	    alert("Error on page, please reload!");
	    return;
	}

	var loggedIn = true;
	if ($.cookie("il_mail") == null || $.cookie("il_dname") == null || $.cookie("il_pass") == null) {
		loggedIn = false;
	}

	if (il_operation == "addreview" && !loggedIn) {
		
		$.ajax({
		    url: "/ajax/login.jsp?actionType=submitReview",
		    type: "POST",
		    success: function(html) {
		    	$.facebox(html);
				$('html, body').animate({scrollTop:0}, 'slow');
		    }
		});
	} else if (il_operation == "addcomment" && !loggedIn) {
		$.ajax({
		    url: "/ajax/login.jsp?actionType=submitComment",
		    type: "POST",
		    success: function(html) {
		    	$.facebox(html);
				$('html, body').animate({scrollTop:0}, 'slow');
		    }
		});
		
	} else {
	   submitReviewInfo(il_rid, null, il_cid, il_rating, il_title, il_review, il_optIn, il_operation, null, null, null, null);
	}
}
// END submitReview()

function alert1(msg) {
	alert(msg);
}



function loginAndSubmit(parent, il_cid, il_rid, il_operation) {

	
	var mail = $(parent + " .loginMailBox").val();
	var pass = $(parent + " .loginPassBox").val();
	data = "mail=" + mail + "&pass=" + pass;
	$.ajax({
	    url: "/login",
	    type: "POST",
	    data: data,
	    success: function(returnValue) {
	        returnValue = trim(returnValue);
	        if (returnValue == 'ok') {	
	            getReviewAndSubmit(parent, il_cid, il_rid, il_operation, null, null, null, null);
	
	        } else {
	
	        	
	        	var errorPageUrl = "/ajax/jsError.jsp?msg=" + returnValue;
				if (returnValue.indexOf("expired.account") > -1) {
					errorParams = returnValue.split("#");
					errorPageUrl = "/ajax/jsError.jsp?msg=" + errorParams[0] + "&userId=" + errorParams[1] + "&mail=" + errorParams[2];
				} 
	        	
	            $(parent + " .error").show();
	            $(parent + " .error").load(errorPageUrl);
	        }
	    }
	});
}
// END loginAndSubmit()

function signupAndSubmit(parent, il_cid, il_rid, il_operation) {

	
	var name = $(parent + " .loginNameSBox").val();
	var mail = $(parent + " .loginMailSBox").val();
	var pass = $(parent + " .loginPassSBox").val();
	var pass2 = $(parent + " .loginPass2SBox").val();
	
	var cbDev = "false";
	var cbPartner = "false";
	data = "name=" + name
	        + "&mail=" + mail
	        + "&pass=" + pass
	        + "&cbDev=" + cbDev
	        + "&cbPartner=" + cbPartner
	        + "&action=signup"
	        + "&pass2=" + pass2
	        + "&operation=" + il_operation;
	
	$.ajax({
	    url: "/login/signup",
	    type: "POST",
	    data: data,
	    success: function(returnValue) {
	        returnValue = trim(returnValue);
	        params = returnValue.split('//');
	        if (params[0] == "ok" && params.length == 4) {
	            var il_uid = params[1];
	            var il_mail = params[2];
	            var il_pass = params[3];
	            getReviewAndSubmit(parent, il_cid, il_rid, il_operation, "signup", il_uid, il_mail, il_pass);
	        } else {

	            $(parent + " .error").show();
	            if (returnValue.indexOf("username.suggest") > -1) {
	                var userName = returnValue.substring(30);
	                $(parent + " .error").load("/ajax/jsError.jsp?username=" + userName);
	            } else {
	                $(parent + " .error").load("/ajax/jsError.jsp?msg=" + returnValue);
	            }
	        }
	    }
	});
}
// END signupAndSubmit()

function getReviewAndSubmit(parent, il_cid, il_rid, il_operation, il_signup, il_uid, il_mail, il_pass) {
	il_review = null;
	il_title = null;
	il_rating = null;
	il_optIn = null;

	if (il_operation == "addreview") {
	    il_review = $("form.writereview .reviewDesc").val();
	    il_title = $("form.writereview .reviewTitle").val();
	    il_rating = $("form.writereview .rating").val();
	    if ($("form.writereview .optIn").attr("checked")) {
	        il_optIn = "true";
	    } else {
	        il_optIn = "false";
	    }
	} else if (il_operation == "addcomment") {
	    il_review = $(parent + " .commentDesc").val();
	} else {
	    il_review = $(parent + " .editDesc").val();
	    il_title = $(parent + " .editTitle").val();
	    il_rating = $(parent + " .editRating").val();
	    il_optIn = $(parent + " .editOptIn").val();
	}
	
	il_review = $.trim(il_review);
/*	if (il_rid == null) {
	    il_title = $.trim(cleanUpString(il_title));
	}*/
	
	if (il_cid == "") {
	    alert("Error on page, please reload!");
	    return;
	}
	submitReviewInfo(il_rid, null, il_cid, il_rating, il_title, il_review, il_optIn, il_operation, il_signup, il_uid, il_mail, il_pass);
}
// END getReviewAndSubmit()

function submitReviewInfo(il_rid, il_cmmntid, cid, il_rating, il_title, il_review, il_optIn, il_operation, signup, il_uid, il_mail, il_pass) {

	//il_review = il_review.replace(/\n/g, "#br#").replace(/\n\n+/g, '#br##br#');
	var il_countryid = $("#countryISO2").val();
	
	data = "reviewId=" + il_rid +
	       "&commentId=" + il_cmmntid +
	       "&companyId=" + cid +
	       "&countryISO2=" + il_countryid +
	       "&rating=" + il_rating +
	       "&title=" + encodeURIComponent(il_title) +
	       "&text=" + encodeURIComponent(il_review) +
	       "&operation=" + il_operation;
	
	if (il_uid != null &&  il_mail != null && il_pass != null) {
	    data = data +
	           "&userId=" + il_uid +
	           "&mail=" + il_mail +
	           "&pass=" + il_pass;
	}
	
	var url = "/review";
	if( il_operation.indexOf("editreview") > -1) {
		url = "/editReview";
	}
	if( il_operation.indexOf("comment") > -1) {
		url = "/comment";
	}
	if( il_operation.indexOf("editcomment") > -1) {
		url = "/editComment";
	}
	
	$.ajax({
	    url: url,
	    type: "POST",
	    data: data,
	    success: function(returnValue) {
	        if (returnValue.indexOf('ok_') > -1) {
	        	
	        	dcsMultiTrack('DCSext._hit_type','click','DCSext.linktype', 'review added','WT.dl','99');
	        	
	        	var url = window.location.href;
	        	if(url.indexOf('#') > -1) url = url.substring(0,url.indexOf('#'));
	        	if(url.indexOf('?') > -1) url = url.substring(0,url.indexOf('?'));
	            var objectId = returnValue.replace(/ok_/, '');
	            var parameters = "type=writereview&reviewid=" + objectId+'&rand='+Math.random()+"#reviews";

	            if (signup != null && signup == "signup") {
	                if (il_operation.indexOf("addcomment") > -1) {
	                    var parameters_new = "type=sWritecomment&commentid=" + objectId+'&rand='+Math.random()+"#reviews";
	                    parameters = parameters_new;
	                } else {
	                	var parameters_new = "type=sWritereview&reviewid=" + objectId+'&rand='+Math.random()+"#reviews";
	                	parameters = parameters_new;           	
	                }
	            } else {
	                if (il_operation.indexOf("addcomment") > -1) {
	                	var parameters_new = "type=writecomment&commentid=" + objectId+'&rand='+Math.random()+"#reviews";
	                	parameters = parameters_new;
	                } else if (il_operation.indexOf("editcomment") > -1) {
	                	var parameters_new = "type=editcomment&commentid=" + objectId+'&rand='+Math.random()+"#reviews";
	                	parameters = parameters_new;
	                } else if (il_operation.indexOf("editreview") > -1) {
	                	var parameters_new = "type=editreview&reviewid=" + objectId+'&rand='+Math.random()+"#reviews";
	                	parameters = parameters_new;
	                } 
	            }
	        	window.location = url+"?"+parameters;  
	        	
	        } else {
	        	
	        	dcsMultiTrack('DCSext._hit_type','click','DCSext.linktype', 'review form failure','WT.dl','99');
	        	
	        	if (il_operation.indexOf("addreview") > -1) {

	                $("#errormsg_addreview").load("/ajax/showerror.jsp?newMessage=" + encodeURIComponent(returnValue));
	                $("#errormsg_addreview").show();                        	
	            }
	            if( il_operation.indexOf("editreview") > -1) {

	                $("#errormsg_editreview_"+il_rid).load("/ajax/showerror.jsp?newMessage=" + encodeURIComponent(returnValue));
	                $("#errormsg_editreview_"+il_rid).show();
	            }
	            if (il_operation.indexOf("addcomment") > -1) {

	            	$(".errormsg_addcomment").load("/ajax/showerror.jsp?newMessage=" + encodeURIComponent(returnValue));
	                $(".errormsg_addcomment").show();
	            }
	            if( il_operation.indexOf("editcomment") > -1) {

	            	$("#errormsg_editcomment_"+il_cmmntid).load("/ajax/showerror.jsp?newMessage=" + encodeURIComponent(returnValue));
	                $("#errormsg_editcomment_"+il_cmmntid).show();
	            }
	        }
	    }
	});
}
// END submitReviewInfo()

function submitComment(il_reviewid, il_commentid, il_operation) {
    var il_comment = null;
    var il_companyid = $("#countryISO2").val();

   	il_comment = $("#comment_" + il_commentid + " .editDesc").val();

    il_comment = $.trim(il_comment);

    if (il_companyid == "" || il_operation == "") {
        alert("Error on page, please reload!");
        return;
    }

    submitReviewInfo(il_reviewid, il_commentid, il_companyid, null, null, il_comment, null, il_operation, null, null, null, null);
}// END submitComment()

var fixedStar = 0;
function lightUp(nr) {
    for (var i = 0; i <= nr; i++) {
        $("#star" + i).css("background-image", "url(/img/star_on_big.png)");
    }
    for (var i = nr + 1; i <= 5; i++) {
        $("#star" + i).css("background-image", "url(/img/star_off_big.png)");
    }
    return false;
}

function lightDown() {
    if (fixedStar > 0) {
        for (var i = 1; i <= fixedStar; i++) {
            $("#star" + i).css("background-image", "url(/img/star_on_big.png)");
        }
        for (var i = fixedStar + 1; i <= 5; i++) {
            $("#star" + i).css("background-image", "url(/img/star_off_big.png)");
        }
    } else {
        for (var i = 1; i <= 5; i++) {
            $("#star" + i).css("background-image", "url(/img/star_off_big.png)");
        }
    }
    return false;
}

function setStar(nr) {
    fixedStar = nr;
}

function addStar(rating) {
    setStar(rating);
    $("#rating").val(rating);
    $("#errormsg_addreview").hide();
	$('#writeReview').slideDown();
	$(".reviewDesc").val($("#reviewDescDefault").val()).focus();
//	$(document).click(hideReviewBox);
    return false;
}

function hideReviewBox(event){
	if ($(event.target).parents("form.writereview").length < 1) {
		$("#rating").val(0);
		$('.writereview .star').css('background-image','url("/img/star_off_big.png")');
		$('#writeReview').slideUp();
		$(".reviewDesc").blur();
		$(document).unbind("click", hideReviewBox);
	}
};

function cleanUpString(messedUp) {
    var cleanedUp = "";
    messedUp = messedUp.replace(/([^\x01-\x7E])/g,function(word){return'&#'+word.charCodeAt(0)+ ';';})
    if (messedUp != undefined && messedUp.length > 0) {
        for (i = 0; i < messedUp.length; i++) {
            var foundCharCode = messedUp.charCodeAt(i);
            var foundChar = messedUp.charAt(i);

            if (foundCharCode == 39) {
                cleanedUp += '&#39;';
            } else if (foundCharCode == 34) {
                cleanedUp += '&quot;';
            } else if (foundCharCode == 8364) {
                cleanedUp += '&euro;';
            } else {
                cleanedUp += foundChar;
            }
        }
    }
    return cleanedUp;
}

function submitTomTomRouteDetail() {
	if ($("#routeBoxForm .route-txt").val().length > 0) {
		var url = $(".tomTomRouteDetail").attr("href");
		$("form#routeBoxForm").attr("action", url.replace("/home/", "/" + $("#routeBoxForm .route-txt").val() + "/"));
		$("#routeBoxForm #planRouteButton").attr("href", url.replace("/home/", "/" + $("#routeBoxForm .route-txt").val() + "/"));
		setTimeout(function() {
			$("#planRouteBox").hide();
			$(document).unbind("click", removeTomTomRouteBox);
		}, 1000);
	}
};

function removeTomTomRouteBoxDetail(event){
	if ($(event.target).parents("#planRouteBox").length < 1) {
		$("#planRouteBox").hide();
		$(document).unbind("click", removeTomTomRouteBox);
	}
};
