var ajaxServiceUrl = "/wcpa/servlet/org.oclc.lac.ui.ajax.ServiceServlet";

jQuery(document).ready(makeSocPageLoadComplete());	// located in soc.jsp

function socPageLoadComplete(userName, oclcNum, baseOclcNo, addTagsExpanded, addListsExpanded, userHasReviews) {
	if ((userName.length > 0) && (userHasReviews == 'true')) {
        // fill in the user's rating for this item (if they've rated it).
        jQuery.get(ajaxServiceUrl, {serviceCommand: 'getUserReview', itemId: baseOclcNo, creatorId: userName}, function(data) {
            if (data != "") {
                var userReview = YAHOO.lang.JSON.parse(data);
                if (userReview != null)
                {
                    document.getElementById("soc-rev").href = "/oclc/" + oclcNum + "?page=newreview&oclcnum=" + baseOclcNo;
                    document.getElementById("ratingLabel").innerHTML = "Uw waardering:";
                    fillInRating(userReview.rating);

                    if (userReview.hasDraft)
                    {
                        document.getElementById("soc-rev").innerHTML = "Uw beoordeling (concept)";
                        document.getElementById("soc-rev").title = "Doorgaan met het bewerken van uw beoordeling";
                        document.getElementById("rateRevPromptMsg").innerHTML = "Uw heeft een concept-beoordeling voor dit item. Wilt u deze verder bewerken?";
                    }
                    else if (userReview.hasPublished)
                    {
                        document.getElementById("soc-rev").innerHTML = "Uw Beoordeling";
                        document.getElementById("soc-rev").title = "Uw beoordeling bewerken";
                        document.getElementById("rateRevPromptMsg").innerHTML = "Uw heeft een beoordeling voor dit item gepubliceerd. Wilt u deze ook bewerken?";
                    }
                }
            }
        });
	}
	
	if (addTagsExpanded) {
	    jQuery('#soc-tag').click();
	}
	if (addListsExpanded) {
	    jQuery('#soc-list').click();
	}
}

function fillInRating(rating)
{
    var starList = document.getElementById("ratingStarList-socbar");
    if (rating == 0)      starList.className = "rating-small none-small";
    else if (rating == 1) starList.className = "rating-small one-small";
    else if (rating == 2) starList.className = "rating-small two-small";
    else if (rating == 3) starList.className = "rating-small three-small";
    else if (rating == 4) starList.className = "rating-small four-small";
    else if (rating == 5) starList.className = "rating-small five-small";

}
var myRating = "";
var rated = false;

function showRatingPromptDiv(canRate, rating)
{
    removeAllMessages();
    if (!canRate || rated)
    {
        $('cantRateDiv').style.display = 'block';
        $('ratingDoneDiv').style.display = 'none';
    }
    else
    {
        myRating = rating;
        $('ratingpromptDiv').style.display = 'block';
    }
}

function gotoNewReview(oclcNum, baseOclcNo, page)
{
   $('ratingpromptDiv').style.display = 'none';
   document.location.href = "/oclc/" + oclcNum + "?page=" + page + "&oclcnum="+ baseOclcNo + "&ratingchoice=" + myRating + "&client=worldcat.org-detailed_record";
   return false;
}

function gotoNewRating(authenticated, baseOclcNo)
{
    $('ratingpromptDiv').style.display = 'none';
	jQuery.get(ajaxServiceUrl, {serviceCommand: 'doNewRating', itemId: baseOclcNo, ratingchoice: myRating}, function(data) {
		var rateresp = YAHOO.lang.JSON.parse(data);
        if (rateresp.ratingsaved == "true")
        {
            $('cantRateDiv').style.display = 'none';
            $('ratingDoneDiv').style.display = 'block';
            if (authenticated)
            {
                rated = true;
            }
            else
            {
                fillInRating(myRating);
            }
        }
        else if (rateresp.alreadyrated == "true")
        {
            $('cantRateDiv').style.display = 'block';
            $('ratingDoneDiv').style.display = 'none';
            rated = true;
        }
	});
    return false;
}
function removeAllMessages()
{
    //clear out any previous messages that might've been displayed
    $('cantRateDiv').style.display = 'none';
    $('ratingDoneDiv').style.display = 'none';
}

function loadaddtags(tagid)
{
    var value = document.getElementById('tags_'+tagid).innerHTML;
    var ctrl = document.getElementById('soc-tag-add');
    var text = ctrl.value;
    value = value.replace('&amp;','&');
    //value = value.replace('&amp','&');
    if(text == ""){
        text = text + trimAll(value);
    }
    else{
        text = text + "," + trimAll(value);
    }

     document.getElementById('soc-tag-add').value = text;
    var pos = ctrl.value.length;

      if(ctrl.setSelectionRange){
        ctrl.focus();
        ctrl.setSelectionRange(pos,pos);
      }else
          if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
      }
    document.getElementById('soc-tag-add').focus();
}

function trimAll(sString) {
    return sString.replace(/^\s*|\s*$/g,'');
}

function addTagsToItem(){
    var data = document.getElementById('soc-tag-add').value;
//    var legalChars = /^[\w|&|\-|'|\s|,]*$/; // include letters,digits,underscore,hyphen,ampersand,apostophe & Space
    var illegal = /[\.><?\/+=)(*^%#@!~`\:\;\[\]{}\\¡¿©®<>¢£"""«»°±¼½¾×÷???¤±¶§¬...º°]/;

	var splitData = data.split(',');

    for(var i=0; i < splitData.length; i++){
		while(splitData[i].substr(0,1) == " ")
            splitData[i] = splitData[i].substr(1,splitData[i].length);
        while(splitData[i].substr(splitData[i].length - 1,1) == " ")
            splitData[i] = splitData[i].substr(0,splitData[i].length - 1);
		if(splitData[i].length <= 0 || splitData[i].length > 75){
			if(splitData[i].length > 75){
				document.getElementById('savetagerror').style.display = "block";
			}
            else{
               if(i==0){
                    document.getElementById('error_entertagsandapply').style.display = "block";
               }else{
                    document.getElementById('error_oneemptytag').style.display = "block";
               }
            }

            return false;
		} else {
			if(splitData[i].match(illegal)){
				document.getElementById('savetagerror').style.display = "block";
//				document.getElementById('confirm').style.display = "none";
				return false;
			}
		}
	}

	return true;
}

function doLoginForReview(oclcNum, baseOclcNo, rating)
{
    var redirect = "/oclc/" + oclcNum + "?page=newreview&oclcnum=" + baseOclcNo;
    if (rating > 0)
    {
        redirect += "&ratingchoice=" + rating;
    }
    showLoginPopupDiv('Schrijf een online beoordeling',
                      'om een online beoordeling te schrijven.',
                      '/account/' + oclcNum + '?page=login&redirect=' + redirect);
    return false;
}

function closeAddTagsButton(){
    dropdowncontent.hidediv('soc-tag-sub');
    document.getElementById('savetagerror').style.display='none';
    document.getElementById('error_entertagsandapply').style.display='none';
}
