//this was originally included in search.html include
//due to one or two template if statements


//TODO try to refactor this one
function dispatchSimSearch(imageNo) {
    var collectionID = "";
    var licType = "";

    var options = '&page=1';
    //TODO find out what was so_reset doing here
    var searchform = document.searchform;
    var isSearchable = isDefined(searchform.so_color);

    if ( isDefined(searchform.collectionID) ) {
        if ( isDefined(searchform.licType) ) {
            if ( searchform.licType[0].checked ) {	//  ALL checkbox
                for (i = 0; i < searchform.collectionID.length; i++) {
    			    collectionID += "," + searchform.collectionID[i].value;
	    	    }
                collectionID += ",RF";
                licType = ",ALL"
            }

            if ( searchform.licType[1].checked ) {	//	RM checkbox
                for (i = 0; i < searchform.collectionID.length; i++) {
                    if ( searchform.collectionID[i].checked )
		    		    collectionID += "," + searchform.collectionID[i].value;
		        }
                collectionID += ",RF";
                licType = ",RM"
            }

            if ( searchform.licType[2].checked ) {	//	RF checkbox
                for (i = 0; i < searchform.collectionID.length; i++) {
    			    collectionID += "," + searchform.collectionID[i].value;
    		    }
                collectionID += ",RF";
                licType = ",RF"
            }
        } else {
            for (i = 0; i < searchform.collectionID.length; i++) {
                if ( searchform.collectionID[i].checked )
                    collectionID += "," + searchform.collectionID[i].value;
            }
        }
    } else {

        if ( isDefined(searchform.licType) ) {
			for (i = 0; i < searchform.licType.length; i++) {
			    if ( searchform.licType[i].checked ) {
			        licType += "," + searchform.licType[i].value;
    		    }
	        }
        } else
		    collectionID += "," + userAllowedCollection;
    }

    if ( licType != "" )
	    	licType = licType.substring(1);

    if ((collectionID == "") && (licType == "")) {

        alert("Please select a collection.");
        return false;
    }

    collectionID = collectionID.substring(1);
    //licType = licType.substring(1);

    if (isDefined(searchform.so_color)) {
        options += encodeFilter(searchform.so_color);
        options += encodeFilter(searchform.so_blacknwhite);
        options += encodeFilter(searchform.so_horizontal);
        options += encodeFilter(searchform.so_vertical);
        options += encodeFilter(searchform.so_square);
        options += encodeFilter(searchform.so_panoramic);
    }

    if (collectionID != "")
        location = "/search/index.html?query=" + escapeAA("simSearch:") + imageNo + options + "&licType=" + licType;
    else
        location = "/search/index.html?query=" + escapeAA("simSearch:") + imageNo + options + "&licType=" + licType;

    return false;
}

function encodeFilter(filter) {
    var query;

    if ((filter.checked && (filter.value != "on")) || (!filter.checked && (filter.value != "off")))
        query = filter.checked ? "&" + filter.name + "=on" : "&" + filter.name + "=off";
    else
        query = "";

    return query;
}

function activateParseLog(event){
    if (event.keyCode == 13){
        if (event.shiftKey){
            document.searchform.showParseLog.value = "1";
        }
    }
    return false;
}


