var hasRated = false;
var userRating = 0;

function cancel_starRating() {
    document.getElementById("commentBox").style.display = "none";
	pageTracker._trackPageview(window.location + '_feedback_cancel');
}


function rollOnStar(starNum){
	if(!hasRated){
		for(s=1;s<=starNum;s++){ document.getElementById("star"+s).src = "/visitors/images/rating/star_on.gif"; }
		for(s=starNum+1;s<=5;s++){ document.getElementById("star"+s).src = "/visitors/images/rating/star_off.gif"; }
	}
}

function rollOffStar(){
	if(!hasRated){ for(s=1;s<=5;s++){ document.getElementById("star"+s).src = "/visitors/images/rating/star_off.gif"; } }
}

function clickStar(starNum) {
    if (!hasRated) {
		pageTracker._trackPageview(window.location + "_rated_" + starNum);
		hasRated = true;
		userRating = starNum;
		showCommentBox("from_stars");
		document.getElementById("rating").innerHTML = starNum;
		rollOnStar_saved(starNum);
		cookie_response = getremotevalue("/visitors/rating_cookie.aspx?rate=" + starNum + "&url=" + escape(this_url));
	}	
}



function rollOnStar_saved(starNum){
	for(s=1;s<=starNum;s++){ document.getElementById("star"+s).src = "/visitors/images/rating/star_on.gif"; }
	for(s=starNum+1;s<=5;s++){ document.getElementById("star"+s).src = "/visitors/images/rating/star_off.gif"; }
}

function showCommentBox(str_from) {

    document.getElementById("rating").innerHTML = userRating;
    document.getElementById("rateCommentThanks").style.display = "none";
    document.getElementById("rateCommentForm").style.display = "block";
    document.getElementById("commentBox").style.display = "block";

    if (str_from == "from_stars") {
        document.getElementById("message_improve").style.display = "none";
        document.getElementById("message_stars").style.display = "block";
    }

    if (str_from == "from_improve") {
        document.getElementById("message_stars").style.display = "none";
        document.getElementById("message_improve").style.display = "block";
		pageTracker._trackPageview(window.location + '_improve');
    }
    
}

function closeCommentBox(){
	document.getElementById("commentBox").style.display = "none";
}

function clearEmail(){
	$("#commentBox input").each(function(i){
		if( $(this).attr("name").indexOf("textBox_email") > 0 ){
			if($(this).attr("value")=="Enter Email"){
				$(this).attr("value", "");
			}
		}
	});
}

function clearMessage(){
	$("#commentBox textarea").each(function(i){
		if( $(this).attr("name").indexOf("textBox_message") > 0 ){
			if($(this).attr("value")=="Enter Your Comments"){
				$(this).attr("value", "");
			}
		}
	});
}


function clearInput(obj, str) {
    //alert("clicked - clear Input");
    if (obj.value == str){ obj.value = ""; }   
}



function setPrompts(){
	$("#commentBox textarea").each(function(i){
		if( $(this).attr("name").indexOf("textBox_message") > -1 ){
		    $(this).attr("value", "Enter Your Comments");
		    $(this).click(clearMessage());
		}
	});
	$("#commentBox input").each(function(i){
		if( $(this).attr("name").indexOf("textBox_email") > -1 ){
		    $(this).attr("value", "Enter Email");
		    $(this).click(clearEmail());
		}
	});	
}

function submitMessage(){
	showCommentBox();
	document.getElementById("rateCommentThanks").style.display = "block";
	document.getElementById("rateCommentForm").style.display = "none";
}




function getremotevalue(url) {
    var content = '';

    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (request) {
        request.open("GET", url, false);
        request.send(null);

        if (request.status == 200) {
            content = request.responseText;
        } else {
            content = 'Error: ' + request.status + ' ' + request.statusText;
        }
    }
    return content;
}



function submit_rate_tool() {
    response_str = "success";
    if (document.getElementById("textBox_email").value != "" && document.getElementById("textBox_email").value != "Enter Email" && document.getElementById("textBox_message").value != "" && document.getElementById("textBox_message").value != "Enter Your Comments")
    {
        response_str = getremotevalue("/visitors/ask_a_local.aspx?rate=" + userRating + "&email=" + document.getElementById("textBox_email").value.stripTags() + "&message=" + escape(document.getElementById("textBox_message").value.stripTags()) + "&url=" + escape(this_url));
    }

       
	if (response_str.trim() == "success") {
		document.getElementById("rateCommentForm").style.display = "none";
		document.getElementById("rateCommentThanks").style.display = "block";
		pageTracker._trackPageview(window.location + '_feedback_submit');
	} else {
		document.getElementById("rateCommentForm").style.display = "none";
		document.getElementById("rateCommentErr").style.display = "block";
	}
	return false;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }
String.prototype.stripTags = function() { return this.replace(/<\/?[^>]+>/gi,""); }


