function submitSearchSort(sid) {

    sort = '';

    //PROVIDE FEEDBACK
    if (document.getElementById('searchContent')) {
        document.getElementById('searchContent').innerHTML = '<div style="padding: 20px 0px 0px 0px;">sorting...</div>'
    }

    //GET SORT VALUE
    if (document.getElementById('searchSort')) {
        var sort = document.getElementById('searchSort').value;
    }
	var vars = "action=sortSearchPage&sid="+sid+"&sort="+sort;
	
	//SUBMIT OVERVIEW	
	ajax_json_call('/filebin/ajax/php/libraryAJAX.php', 'POST', vars);		    

}
function validateSubmitComment(f,pid) {
	var err ="";
	var requiredArr = Array("ratingReview","headline","comments","recommededReview");
	var requiredArrName = Array("Rating","Headline","Comments","Recommendation");
	
	for (var i=0; i<requiredArr.length; i++) {
		var curInput = eval("f."+requiredArr[i]);
		if(curInput.value=="") {
			err+=("Your "+requiredArrName[i]+" is blank or invalid"+"\r\n");
		}
	}
	if (err!="") { 
		alert(err);
		return false;
	} else {
	    var vars = "action=submitComment&pid="+pid+"&ratingReview="+f.ratingReview.value+"&headline="+f.headline.value+"&comments="+f.comments.value+"&recommededReview="+f.recommededReview.value;
	    
	    //PROVIDE FEEDBACK
	    if (document.getElementById('leave_comment')) {
		document.getElementById('leave_comment').innerHTML = 'submitting...'
	    }
	    if (document.getElementById('comment_area')) {
		document.getElementById('comment_area').innerHTML = 'loading...'
	    }
    
	    //SUBMIT OVERVIEW	
	    ajax_json_call('/filebin/ajax/php/libraryAJAX.php', 'POST', vars, 'showSubmitted');
        }
}
function showSubmitted() {
    //DISPLAY AREAS WITH SELECTED FILTERS
    if (document.getElementById('leave_comment')) {
	document.getElementById('leave_comment').innerHTML = '<table cellpadding="0" cellspacing="0" class="comment_table"><tr><td><div class="alert_good">Your review has been submitted.</div></td></tr></table>';
    }    
}
function resetSearchPage(sid) {

    //PROVIDE FEEDBACK
    if (document.getElementById('searchFilterDisplayRightReset')) {
        document.getElementById('searchFilterDisplayRightReset').innerHTML = 'resetting...'
    }
    if (document.getElementById('resetPageMiddle')) {
        document.getElementById('resetPageMiddle').innerHTML = 'resetting...'
    }


	var vars = "action=resetSearchPage&sid="+sid;
	
	//SUBMIT OVERVIEW	
	ajax_json_call('/filebin/ajax/php/libraryAJAX.php', 'POST', vars, 'checkSelectedForDisplay');		    

}
function deleteFilter(sid, label, value, fieldName, searchTable, filterTable, extraConditions, type) {

   var msgDivRight =  "deleteItemRight_"+label+"_"+value;
   var msgDivMiddle =  "deleteItemMiddle_"+label+"_"+value;

	//PROVIDE FEEDBACK
	if (document.getElementById(msgDivRight)) {
		document.getElementById(msgDivRight).innerHTML = "deleting...";
	}
	if (document.getElementById(msgDivMiddle)) {
		document.getElementById(msgDivMiddle).innerHTML = "deleting...";
	}

	var vars = "action=deleteFilter&sid="+sid+"&value="+encodeURIComponent(value)+"&label="+label+"&fieldName="+fieldName+"&searchTable="+searchTable+"&filterTable="+filterTable+"&extraConditions="+extraConditions+"&type="+type;
	
	//SUBMIT OVERVIEW	
	ajax_json_call('/filebin/ajax/php/libraryAJAX.php', 'POST', vars, 'checkSelectedForDisplay');		    

}
function checkSelectedForDisplay() {

    //CHECK FOR ACTIVE FILTERS
    var selectedFilters = $(".selectedFilterRight");
   
    if (selectedFilters.length > 0) {

        //DISPLAY AREAS WITH SELECTED FILTERS
        if (document.getElementById('searchFilterDisplayRightHolder') && document.getElementById('searchFilterDisplayMiddleHolder')) {
            document.getElementById('searchFilterDisplayRightHolder').style.display = "block";
            document.getElementById('searchFilterDisplayMiddleHolder').style.display = "block"; 
        }

    } else {
    
        //HIDE AREAS WITH SELECTED FILTERS        
        if (document.getElementById('searchFilterDisplayRightHolder') && document.getElementById('searchFilterDisplayMiddleHolder')) {
            document.getElementById('searchFilterDisplayRightHolder').style.display = "none";
            document.getElementById('searchFilterDisplayMiddleHolder').style.display = "none"; 
        }
        
    }
    
}
//AJAX HANDLERS
function ajax_json_call(callUrl, callType, callVars, onCompleteFunc) {
	try {
          
		$.ajax({
			type: callType,
			url: callUrl,
			data: callVars,
			dataType: "json",
			success: function(data) {
				if (data) {
					process_success_ajax_json_call(data);
				} else {
					//error
				}
		 	},
			complete: function() {
				//if we need to run something discard if we get data or not
				if (eval("typeof " + onCompleteFunc + " == 'function'")) {
					eval(onCompleteFunc+'()');
				}
			}
		});
	} catch (e) {
		//nothing for now
		alert(e);
	} 
        
}
//SEARCH / FILTER QUERY
function getMoreSearchResults(sid, iteration) {

	//PROVIDE FEEDBACK
	if (document.getElementById('extra_search_button')) {
		document.getElementById('extra_search_button').innerHTML = '<img src="filebin/images/retrieving_btn.gif" alt="Retrieving..." />';
	}
	
	var vars = "action=getMoreSearchResults&iteration="+iteration+"&sid="+sid;
	
	ajax_json_call('/filebin/ajax/php/libraryAJAX.php', 'POST', vars);
	
	return false;
	
}
function submitFilter(sid, label, value, optionLabel, fieldName, searchTable, filterTable, extraConditions, type) {

    try {

    var msgDiv =  "filterItem_"+label+"_"+value;

	//PROVIDE FEEDBACK
	if (document.getElementById(msgDiv)) {
		document.getElementById(msgDiv).innerHTML = "filtering...";
	}
    if (document.getElementById('searchFilterDisplayMiddleHead')) {
        document.getElementById('searchFilterDisplayMiddleHead').style.display = "inline";
    }

	var vars = "action=addFilter&sid="+sid+"&value="+encodeURIComponent(value)+"&optionLabel="+encodeURIComponent(optionLabel)+"&label="+label+"&fieldName="+fieldName+"&searchTable="+searchTable+"&filterTable="+filterTable+"&extraConditions="+extraConditions+"&type="+type;

    //alert(vars);

	//SUBMIT OVERVIEW	
	ajax_json_call('/filebin/ajax/php/libraryAJAX.php', 'POST', vars, 'checkSelectedForDisplay');		    

    }
    
    catch(e) {
        alert(e);  
    }
    
}

function process_success_ajax_json_call(data) {
	//var formElements = {formType:['input', 'type', 'radio', 'password', 'textarea', 'checkbox', 'select']}
	if (data) {
		var alert_content="";
		$.each(data, function(id, dataHolder) {
			//alert(dataHolder.jAction+" "+dataHolder.jContentReturn+" "+id);
			if ($("#"+id) && id!="alert_msg" && dataHolder.jAction) {
				//dataHolder is array (jAction,contentReturn,insertId)
				if (dataHolder.jAction.indexOf('insert')>-1) {
					//insertAfter, insertBefore
					$(dataHolder.jContentReturn)[dataHolder.jAction]('#'+dataHolder.jHelpId);
				} else {
					//replace/set new value
					//alert(dataHolder.jAction+" "+dataHolder.jContentReturn+" "+id);
					$("#"+id)[dataHolder.jAction](dataHolder.jContentReturn);
				}
			}
			//display js alert
			if (id=="alert_msg") alert_content += content+"\n";
		});
		if (alert_content != "") alert(alert_content);
	}
	else {
		//process error here...
	}
}


function handOver(id) {
	var length = 5;
	for(i=1; i<=length; i++) {
		var hand_id = "hand_"+i;
		if (document.getElementById(hand_id)) {
			if (i <= id) {			
				document.getElementById(hand_id).className="hand_highlighted";
			} else {
				document.getElementById(hand_id).className="hand_dull";			
			}
		}
	}
	
	//UPDATE TEXT
	//handTextUpdate(id);

}
function handOut() {
	var length = 5;
	var current_id = document.getElementById("ratingReview").value;
	for(i=1; i<=length; i++) {
		var hand_id = "hand_"+i;
		if (document.getElementById(hand_id)) {
			document.getElementById(hand_id).className="hand_dull";			
		}
	}
	
	//UPDATE TEXT
	if (document.getElementById("reviewHandsText")) {
		document.getElementById("reviewHandsText").innerHTML = "";
	}	
	
	if (current_id != "") {
		handClick(current_id);
	}

}
function handClick(id) {
	var length = 5;
	for(i=1; i<=length; i++) {
		var hand_id = "hand_"+i;
		if (document.getElementById(hand_id)) {
			if (i <= id) {			
				document.getElementById(hand_id).className="hand_selected";
			} else {
				document.getElementById(hand_id).className="hand_dull";			
			}
		}
	}
	
	//UPDATE TEXT
	//handTextUpdate(id);	
	
	document.getElementById("ratingReview").value = id;
}

function handTextUpdate(id) {

	var text = "";

	if (id == 1) {
		text = "Poor";
	} else if (id == 2) {
		text = "Nothing special";
	} else if (id == 3) {
		text = "Average";
	} else if (id == 4) {
		text = "Outstanding";
	} else if (id == 5) {
		text = "Exceeded expectations";
	}

	if (document.getElementById("reviewHandsText")) {
		$("#reviewHandsText").html(text);
	}
	
}

function recommendOver(id) {
	var length = 2;
	for(i=1; i<=length; i++) {
		var recommend_id = "recommend_"+i;
		if (document.getElementById(recommend_id)) {
			if (i == id) {
				if (i==1) document.getElementById(recommend_id).className="recommend_over";
				else if (i==2) document.getElementById(recommend_id).className="not_recommend_over";
			} else {
				if (i==1) document.getElementById(recommend_id).className="recommend_off";
				else if (i==2) document.getElementById(recommend_id).className="not_recommend_off";		
			}
		}
	}
	
	//UPDATE TEXT
	//handTextUpdate(id);

}
function recommendOut() {
	var length = 2;
	var current_id = document.getElementById("recommededReview").value;
	for(i=1; i<=length; i++) {
		var recommend_id = "recommend_"+i;
		if (document.getElementById(recommend_id)) {
			if (i==1) document.getElementById(recommend_id).className="recommend_off";
			else if (i==2) document.getElementById(recommend_id).className="not_recommend_off";		
		}
	}
		
	
	if (current_id != "") {
		recommendClick(current_id);
	}

}
function recommendClick(id) {
	var length = 2;
	for(i=1; i<=length; i++) {
		var recommend_id = "recommend_"+i;
		if (document.getElementById(recommend_id)) {
			if (i == id) {			
				if (i==1) document.getElementById(recommend_id).className="recommend_on";
				else if (i==2) document.getElementById(recommend_id).className="not_recommend_on";
			} else {
				if (i==1) document.getElementById(recommend_id).className="recommend_off";
				else if (i==2) document.getElementById(recommend_id).className="not_recommend_off";			
			}
		}
	}
	
	//UPDATE TEXT
	//handTextUpdate(id);	
	
	document.getElementById("recommededReview").value = id;
}


function toggleReview(id) {
	var hiddenDiv = "indDivReviewCommentExtra_"+id;
	var linkDiv = "extraLink_"+id;
	var linkCloseDiv = "extraLinkClose_"+id;
	
	//USE JQUERY TO HIDE/SHOW EXTRA LINKS
	if (document.getElementById(hiddenDiv) && document.getElementById(linkDiv)) {
		$("#"+hiddenDiv).slideToggle("fast");
	}
	
	//UPDATE TEXT
	if (document.getElementById(hiddenDiv).style.height == "1px") {
		document.getElementById(linkDiv).innerHTML = '<a href="#" onclick="toggleReview(\''+id+'\'); return false;">Close -</a>';
		
		//SHOW CLOSE LINK
    	if (document.getElementById(linkCloseDiv)) {
    	    document.getElementById(linkCloseDiv).style.display = 'block';
    	}
		
	} else {
		document.getElementById(linkDiv).innerHTML = '<a href="#" onclick="toggleReview(\''+id+'\'); return false;">Read More +</a>';		
		
		//CLOSE SECOND CLOSE LINK
    	if (document.getElementById(linkCloseDiv)) {
    	    document.getElementById(linkCloseDiv).style.display = 'none';
    	}		
		
	}
}
